RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / oss / ac97_codec.c
blob5a57de807e82d8a4e39b889c51bd9e5db03d116a
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 the Intel
25 * audio homepage: http://developer.intel.com/ial/scalableplatforms/audio/
27 * The specification itself is currently available at:
28 * ftp://download.intel.com/ial/scalableplatforms/ac97r22.pdf
30 **************************************************************************
32 * History
33 * May 02, 2003 Liam Girdwood <lrg@slimlogic.co.uk>
34 * Removed non existant WM9700
35 * Added support for WM9705, WM9708, WM9709, WM9710, WM9711
36 * WM9712 and WM9717
37 * Mar 28, 2002 Randolph Bentson <bentson@holmsjoen.com>
38 * corrections to support WM9707 in ViewPad 1000
39 * v0.4 Mar 15 2000 Ollie Lho
40 * dual codecs support verified with 4 channels output
41 * v0.3 Feb 22 2000 Ollie Lho
42 * bug fix for record mask setting
43 * v0.2 Feb 10 2000 Ollie Lho
44 * add ac97_read_proc for /proc/driver/{vendor}/ac97
45 * v0.1 Jan 14 2000 Ollie Lho <ollie@sis.com.tw>
46 * Isolated from trident.c to support multiple ac97 codec
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52 #include <linux/errno.h>
53 #include <linux/bitops.h>
54 #include <linux/delay.h>
55 #include <linux/pci.h>
56 #include <linux/ac97_codec.h>
57 #include <asm/uaccess.h>
58 #include <linux/mutex.h>
60 #define CODEC_ID_BUFSZ 14
62 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel);
63 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
64 unsigned int left, unsigned int right);
65 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val );
66 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask);
67 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg);
69 static int ac97_init_mixer(struct ac97_codec *codec);
71 static int wolfson_init03(struct ac97_codec * codec);
72 static int wolfson_init04(struct ac97_codec * codec);
73 static int wolfson_init05(struct ac97_codec * codec);
74 static int wolfson_init11(struct ac97_codec * codec);
75 static int wolfson_init13(struct ac97_codec * codec);
76 static int tritech_init(struct ac97_codec * codec);
77 static int tritech_maestro_init(struct ac97_codec * codec);
78 static int sigmatel_9708_init(struct ac97_codec *codec);
79 static int sigmatel_9721_init(struct ac97_codec *codec);
80 static int sigmatel_9744_init(struct ac97_codec *codec);
81 static int ad1886_init(struct ac97_codec *codec);
82 static int eapd_control(struct ac97_codec *codec, int);
83 static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
84 static int cmedia_init(struct ac97_codec * codec);
85 static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
86 static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
90 * AC97 operations.
92 * If you are adding a codec then you should be able to use
93 * eapd_ops - any codec that supports EAPD amp control (most)
94 * null_ops - any ancient codec that supports nothing
96 * The three functions are
97 * init - used for non AC97 standard initialisation
98 * amplifier - used to do amplifier control (1=on 0=off)
99 * digital - switch to digital modes (0 = analog)
101 * Not all codecs support all features, not all drivers use all the
102 * operations yet
105 static struct ac97_ops null_ops = { NULL, NULL, NULL };
106 static struct ac97_ops default_ops = { NULL, eapd_control, NULL };
107 static struct ac97_ops default_digital_ops = { NULL, eapd_control, generic_digital_control};
108 static struct ac97_ops wolfson_ops03 = { wolfson_init03, NULL, NULL };
109 static struct ac97_ops wolfson_ops04 = { wolfson_init04, NULL, NULL };
110 static struct ac97_ops wolfson_ops05 = { wolfson_init05, NULL, NULL };
111 static struct ac97_ops wolfson_ops11 = { wolfson_init11, NULL, NULL };
112 static struct ac97_ops wolfson_ops13 = { wolfson_init13, NULL, NULL };
113 static struct ac97_ops tritech_ops = { tritech_init, NULL, NULL };
114 static struct ac97_ops tritech_m_ops = { tritech_maestro_init, NULL, NULL };
115 static struct ac97_ops sigmatel_9708_ops = { sigmatel_9708_init, NULL, NULL };
116 static struct ac97_ops sigmatel_9721_ops = { sigmatel_9721_init, NULL, NULL };
117 static struct ac97_ops sigmatel_9744_ops = { sigmatel_9744_init, NULL, NULL };
118 static struct ac97_ops crystal_digital_ops = { NULL, eapd_control, crystal_digital_control };
119 static struct ac97_ops ad1886_ops = { ad1886_init, eapd_control, NULL };
120 static struct ac97_ops cmedia_ops = { NULL, eapd_control, NULL};
121 static struct ac97_ops cmedia_digital_ops = { cmedia_init, eapd_control, cmedia_digital_control};
123 /* sorted by vendor/device id */
124 static const struct {
125 u32 id;
126 char *name;
127 struct ac97_ops *ops;
128 int flags;
129 } ac97_codec_ids[] = {
130 {0x41445303, "Analog Devices AD1819", &null_ops},
131 {0x41445340, "Analog Devices AD1881", &null_ops},
132 {0x41445348, "Analog Devices AD1881A", &null_ops},
133 {0x41445360, "Analog Devices AD1885", &default_ops},
134 {0x41445361, "Analog Devices AD1886", &ad1886_ops},
135 {0x41445370, "Analog Devices AD1981", &null_ops},
136 {0x41445372, "Analog Devices AD1981A", &null_ops},
137 {0x41445374, "Analog Devices AD1981B", &null_ops},
138 {0x41445460, "Analog Devices AD1885", &default_ops},
139 {0x41445461, "Analog Devices AD1886", &ad1886_ops},
140 {0x414B4D00, "Asahi Kasei AK4540", &null_ops},
141 {0x414B4D01, "Asahi Kasei AK4542", &null_ops},
142 {0x414B4D02, "Asahi Kasei AK4543", &null_ops},
143 {0x414C4326, "ALC100P", &null_ops},
144 {0x414C4710, "ALC200/200P", &null_ops},
145 {0x414C4720, "ALC650", &default_digital_ops},
146 {0x434D4941, "CMedia", &cmedia_ops, AC97_NO_PCM_VOLUME },
147 {0x434D4942, "CMedia", &cmedia_ops, AC97_NO_PCM_VOLUME },
148 {0x434D4961, "CMedia", &cmedia_digital_ops, AC97_NO_PCM_VOLUME },
149 {0x43525900, "Cirrus Logic CS4297", &default_ops},
150 {0x43525903, "Cirrus Logic CS4297", &default_ops},
151 {0x43525913, "Cirrus Logic CS4297A rev A", &default_ops},
152 {0x43525914, "Cirrus Logic CS4297A rev B", &default_ops},
153 {0x43525923, "Cirrus Logic CS4298", &null_ops},
154 {0x4352592B, "Cirrus Logic CS4294", &null_ops},
155 {0x4352592D, "Cirrus Logic CS4294", &null_ops},
156 {0x43525931, "Cirrus Logic CS4299 rev A", &crystal_digital_ops},
157 {0x43525933, "Cirrus Logic CS4299 rev C", &crystal_digital_ops},
158 {0x43525934, "Cirrus Logic CS4299 rev D", &crystal_digital_ops},
159 {0x43585430, "CXT48", &default_ops, AC97_DELUDED_MODEM },
160 {0x43585442, "CXT66", &default_ops, AC97_DELUDED_MODEM },
161 {0x44543031, "Diamond Technology DT0893", &default_ops},
162 {0x45838308, "ESS Allegro ES1988", &null_ops},
163 {0x49434511, "ICE1232", &null_ops}, /* I hope --jk */
164 {0x4e534331, "National Semiconductor LM4549", &null_ops},
165 {0x53494c22, "Silicon Laboratory Si3036", &null_ops},
166 {0x53494c23, "Silicon Laboratory Si3038", &null_ops},
167 {0x545200FF, "TriTech TR?????", &tritech_m_ops},
168 {0x54524102, "TriTech TR28022", &null_ops},
169 {0x54524103, "TriTech TR28023", &null_ops},
170 {0x54524106, "TriTech TR28026", &null_ops},
171 {0x54524108, "TriTech TR28028", &tritech_ops},
172 {0x54524123, "TriTech TR A5", &null_ops},
173 {0x574D4C03, "Wolfson WM9703/07/08/17", &wolfson_ops03},
174 {0x574D4C04, "Wolfson WM9704M/WM9704Q", &wolfson_ops04},
175 {0x574D4C05, "Wolfson WM9705/WM9710", &wolfson_ops05},
176 {0x574D4C09, "Wolfson WM9709", &null_ops},
177 {0x574D4C12, "Wolfson WM9711/9712", &wolfson_ops11},
178 {0x574D4C13, "Wolfson WM9713", &wolfson_ops13, AC97_DEFAULT_POWER_OFF},
179 {0x83847600, "SigmaTel STAC????", &null_ops},
180 {0x83847604, "SigmaTel STAC9701/3/4/5", &null_ops},
181 {0x83847605, "SigmaTel STAC9704", &null_ops},
182 {0x83847608, "SigmaTel STAC9708", &sigmatel_9708_ops},
183 {0x83847609, "SigmaTel STAC9721/23", &sigmatel_9721_ops},
184 {0x83847644, "SigmaTel STAC9744/45", &sigmatel_9744_ops},
185 {0x83847652, "SigmaTel STAC9752/53", &default_ops},
186 {0x83847656, "SigmaTel STAC9756/57", &sigmatel_9744_ops},
187 {0x83847666, "SigmaTel STAC9750T", &sigmatel_9744_ops},
188 {0x83847684, "SigmaTel STAC9783/84?", &null_ops},
189 {0x57454301, "Winbond 83971D", &null_ops},
192 /* this table has default mixer values for all OSS mixers. */
193 static struct mixer_defaults {
194 int mixer;
195 unsigned int value;
196 } mixer_defaults[SOUND_MIXER_NRDEVICES] = {
197 /* all values 0 -> 100 in bytes */
198 {SOUND_MIXER_VOLUME, 0x4343},
199 {SOUND_MIXER_BASS, 0x4343},
200 {SOUND_MIXER_TREBLE, 0x4343},
201 {SOUND_MIXER_PCM, 0x4343},
202 {SOUND_MIXER_SPEAKER, 0x4343},
203 {SOUND_MIXER_LINE, 0x4343},
204 {SOUND_MIXER_MIC, 0x0000},
205 {SOUND_MIXER_CD, 0x4343},
206 {SOUND_MIXER_ALTPCM, 0x4343},
207 {SOUND_MIXER_IGAIN, 0x4343},
208 {SOUND_MIXER_LINE1, 0x4343},
209 {SOUND_MIXER_PHONEIN, 0x4343},
210 {SOUND_MIXER_PHONEOUT, 0x4343},
211 {SOUND_MIXER_VIDEO, 0x4343},
212 {-1,0}
215 /* table to scale scale from OSS mixer value to AC97 mixer register value */
216 static struct ac97_mixer_hw {
217 unsigned char offset;
218 int scale;
219 } ac97_hw[SOUND_MIXER_NRDEVICES]= {
220 [SOUND_MIXER_VOLUME] = {AC97_MASTER_VOL_STEREO,64},
221 [SOUND_MIXER_BASS] = {AC97_MASTER_TONE, 16},
222 [SOUND_MIXER_TREBLE] = {AC97_MASTER_TONE, 16},
223 [SOUND_MIXER_PCM] = {AC97_PCMOUT_VOL, 32},
224 [SOUND_MIXER_SPEAKER] = {AC97_PCBEEP_VOL, 16},
225 [SOUND_MIXER_LINE] = {AC97_LINEIN_VOL, 32},
226 [SOUND_MIXER_MIC] = {AC97_MIC_VOL, 32},
227 [SOUND_MIXER_CD] = {AC97_CD_VOL, 32},
228 [SOUND_MIXER_ALTPCM] = {AC97_HEADPHONE_VOL, 64},
229 [SOUND_MIXER_IGAIN] = {AC97_RECORD_GAIN, 16},
230 [SOUND_MIXER_LINE1] = {AC97_AUX_VOL, 32},
231 [SOUND_MIXER_PHONEIN] = {AC97_PHONE_VOL, 32},
232 [SOUND_MIXER_PHONEOUT] = {AC97_MASTER_VOL_MONO, 64},
233 [SOUND_MIXER_VIDEO] = {AC97_VIDEO_VOL, 32},
236 /* the following tables allow us to go from OSS <-> ac97 quickly. */
237 enum ac97_recsettings {
238 AC97_REC_MIC=0,
239 AC97_REC_CD,
240 AC97_REC_VIDEO,
241 AC97_REC_AUX,
242 AC97_REC_LINE,
243 AC97_REC_STEREO, /* combination of all enabled outputs.. */
244 AC97_REC_MONO, /*.. or the mono equivalent */
245 AC97_REC_PHONE
248 static const unsigned int ac97_rm2oss[] = {
249 [AC97_REC_MIC] = SOUND_MIXER_MIC,
250 [AC97_REC_CD] = SOUND_MIXER_CD,
251 [AC97_REC_VIDEO] = SOUND_MIXER_VIDEO,
252 [AC97_REC_AUX] = SOUND_MIXER_LINE1,
253 [AC97_REC_LINE] = SOUND_MIXER_LINE,
254 [AC97_REC_STEREO]= SOUND_MIXER_IGAIN,
255 [AC97_REC_PHONE] = SOUND_MIXER_PHONEIN
258 /* indexed by bit position */
259 static const unsigned int ac97_oss_rm[] = {
260 [SOUND_MIXER_MIC] = AC97_REC_MIC,
261 [SOUND_MIXER_CD] = AC97_REC_CD,
262 [SOUND_MIXER_VIDEO] = AC97_REC_VIDEO,
263 [SOUND_MIXER_LINE1] = AC97_REC_AUX,
264 [SOUND_MIXER_LINE] = AC97_REC_LINE,
265 [SOUND_MIXER_IGAIN] = AC97_REC_STEREO,
266 [SOUND_MIXER_PHONEIN] = AC97_REC_PHONE
269 static LIST_HEAD(codecs);
270 static LIST_HEAD(codec_drivers);
271 static DEFINE_MUTEX(codec_mutex);
273 /* reads the given OSS mixer from the ac97 the caller must have insured that the ac97 knows
274 about that given mixer, and should be holding a spinlock for the card */
275 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel)
277 u16 val;
278 int ret = 0;
279 int scale;
280 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
282 val = codec->codec_read(codec , mh->offset);
284 if (val & AC97_MUTE) {
285 ret = 0;
286 } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
287 /* nice stereo mixers .. */
288 int left,right;
290 left = (val >> 8) & 0x7f;
291 right = val & 0x7f;
293 if (oss_channel == SOUND_MIXER_IGAIN) {
294 right = (right * 100) / mh->scale;
295 left = (left * 100) / mh->scale;
296 } else {
297 /* these may have 5 or 6 bit resolution */
298 if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
299 scale = (1 << codec->bit_resolution);
300 else
301 scale = mh->scale;
303 right = 100 - ((right * 100) / scale);
304 left = 100 - ((left * 100) / scale);
306 ret = left | (right << 8);
307 } else if (oss_channel == SOUND_MIXER_SPEAKER) {
308 ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
309 } else if (oss_channel == SOUND_MIXER_PHONEIN) {
310 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
311 } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
312 scale = (1 << codec->bit_resolution);
313 ret = 100 - (((val & 0x1f) * 100) / scale);
314 } else if (oss_channel == SOUND_MIXER_MIC) {
315 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
316 /* the low bit is optional in the tone sliders and masking
317 it lets us avoid the 0xf 'bypass'.. */
318 } else if (oss_channel == SOUND_MIXER_BASS) {
319 ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
320 } else if (oss_channel == SOUND_MIXER_TREBLE) {
321 ret = 100 - (((val & 0xe) * 100) / mh->scale);
324 #ifdef DEBUG
325 printk("ac97_codec: read OSS mixer %2d (%s ac97 register 0x%02x), "
326 "0x%04x -> 0x%04x\n",
327 oss_channel, codec->id ? "Secondary" : "Primary",
328 mh->offset, val, ret);
329 #endif
331 return ret;
334 /* write the OSS encoded volume to the given OSS encoded mixer, again caller's job to
335 make sure all is well in arg land, call with spinlock held */
336 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
337 unsigned int left, unsigned int right)
339 u16 val = 0;
340 int scale;
341 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
343 #ifdef DEBUG
344 printk("ac97_codec: wrote OSS mixer %2d (%s ac97 register 0x%02x), "
345 "left vol:%2d, right vol:%2d:",
346 oss_channel, codec->id ? "Secondary" : "Primary",
347 mh->offset, left, right);
348 #endif
350 if (AC97_STEREO_MASK & (1 << oss_channel)) {
351 /* stereo mixers */
352 if (left == 0 && right == 0) {
353 val = AC97_MUTE;
354 } else {
355 if (oss_channel == SOUND_MIXER_IGAIN) {
356 right = (right * mh->scale) / 100;
357 left = (left * mh->scale) / 100;
358 if (right >= mh->scale)
359 right = mh->scale-1;
360 if (left >= mh->scale)
361 left = mh->scale-1;
362 } else {
363 /* these may have 5 or 6 bit resolution */
364 if (oss_channel == SOUND_MIXER_VOLUME ||
365 oss_channel == SOUND_MIXER_ALTPCM)
366 scale = (1 << codec->bit_resolution);
367 else
368 scale = mh->scale;
370 right = ((100 - right) * scale) / 100;
371 left = ((100 - left) * scale) / 100;
372 if (right >= scale)
373 right = scale-1;
374 if (left >= scale)
375 left = scale-1;
377 val = (left << 8) | right;
379 } else if (oss_channel == SOUND_MIXER_BASS) {
380 val = codec->codec_read(codec , mh->offset) & ~0x0f00;
381 left = ((100 - left) * mh->scale) / 100;
382 if (left >= mh->scale)
383 left = mh->scale-1;
384 val |= (left << 8) & 0x0e00;
385 } else if (oss_channel == SOUND_MIXER_TREBLE) {
386 val = codec->codec_read(codec , mh->offset) & ~0x000f;
387 left = ((100 - left) * mh->scale) / 100;
388 if (left >= mh->scale)
389 left = mh->scale-1;
390 val |= left & 0x000e;
391 } else if(left == 0) {
392 val = AC97_MUTE;
393 } else if (oss_channel == SOUND_MIXER_SPEAKER) {
394 left = ((100 - left) * mh->scale) / 100;
395 if (left >= mh->scale)
396 left = mh->scale-1;
397 val = left << 1;
398 } else if (oss_channel == SOUND_MIXER_PHONEIN) {
399 left = ((100 - left) * mh->scale) / 100;
400 if (left >= mh->scale)
401 left = mh->scale-1;
402 val = left;
403 } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
404 scale = (1 << codec->bit_resolution);
405 left = ((100 - left) * scale) / 100;
406 if (left >= mh->scale)
407 left = mh->scale-1;
408 val = left;
409 } else if (oss_channel == SOUND_MIXER_MIC) {
410 val = codec->codec_read(codec , mh->offset) & ~0x801f;
411 left = ((100 - left) * mh->scale) / 100;
412 if (left >= mh->scale)
413 left = mh->scale-1;
414 val |= left;
415 /* the low bit is optional in the tone sliders and masking
416 it lets us avoid the 0xf 'bypass'.. */
418 #ifdef DEBUG
419 printk(" 0x%04x", val);
420 #endif
422 codec->codec_write(codec, mh->offset, val);
424 #ifdef DEBUG
425 val = codec->codec_read(codec, mh->offset);
426 printk(" -> 0x%04x\n", val);
427 #endif
430 /* a thin wrapper for write_mixer */
431 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val )
433 unsigned int left,right;
435 /* cleanse input a little */
436 right = ((val >> 8) & 0xff) ;
437 left = (val & 0xff) ;
439 if (right > 100) right = 100;
440 if (left > 100) left = 100;
442 codec->mixer_state[oss_mixer] = (right << 8) | left;
443 codec->write_mixer(codec, oss_mixer, left, right);
446 /* read or write the recmask, the ac97 can really have left and right recording
447 inputs independantly set, but OSS doesn't seem to want us to express that to
448 the user. the caller guarantees that we have a supported bit set, and they
449 must be holding the card's spinlock */
450 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask)
452 unsigned int val;
454 if (rw) {
455 /* read it from the card */
456 val = codec->codec_read(codec, AC97_RECORD_SELECT);
457 #ifdef DEBUG
458 printk("ac97_codec: ac97 recmask to set to 0x%04x\n", val);
459 #endif
460 return (1 << ac97_rm2oss[val & 0x07]);
463 /* else, write the first set in the mask as the
464 output */
465 /* clear out current set value first (AC97 supports only 1 input!) */
466 val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
467 if (mask != val)
468 mask &= ~val;
470 val = ffs(mask);
471 val = ac97_oss_rm[val-1];
472 val |= val << 8; /* set both channels */
474 #ifdef DEBUG
475 printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
476 #endif
478 codec->codec_write(codec, AC97_RECORD_SELECT, val);
480 return 0;
483 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
485 int i, val = 0;
487 if (cmd == SOUND_MIXER_INFO) {
488 mixer_info info;
489 memset(&info, 0, sizeof(info));
490 strlcpy(info.id, codec->name, sizeof(info.id));
491 strlcpy(info.name, codec->name, sizeof(info.name));
492 info.modify_counter = codec->modcnt;
493 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
494 return -EFAULT;
495 return 0;
497 if (cmd == SOUND_OLD_MIXER_INFO) {
498 _old_mixer_info info;
499 memset(&info, 0, sizeof(info));
500 strlcpy(info.id, codec->name, sizeof(info.id));
501 strlcpy(info.name, codec->name, sizeof(info.name));
502 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
503 return -EFAULT;
504 return 0;
507 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
508 return -EINVAL;
510 if (cmd == OSS_GETVERSION)
511 return put_user(SOUND_VERSION, (int __user *)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) {
517 val = 0;
518 } else {
519 val = codec->recmask_io(codec, 1, 0);
521 break;
523 case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
524 val = codec->supported_mixers;
525 break;
527 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
528 val = codec->record_sources;
529 break;
531 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
532 val = codec->stereo_mixers;
533 break;
535 case SOUND_MIXER_CAPS:
536 val = SOUND_CAP_EXCL_INPUT;
537 break;
539 default: /* read a specific mixer */
540 i = _IOC_NR(cmd);
542 if (!supported_mixer(codec, i))
543 return -EINVAL;
545 /* do we ever want to touch the hardware? */
546 /* val = codec->read_mixer(codec, i); */
547 val = codec->mixer_state[i];
548 break;
550 return put_user(val, (int __user *)arg);
553 if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
554 codec->modcnt++;
555 if (get_user(val, (int __user *)arg))
556 return -EFAULT;
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;
561 if (!val) return 0;
562 if (!(val &= codec->record_sources)) return -EINVAL;
564 codec->recmask_io(codec, 0, val);
566 return 0;
567 default: /* write a specific mixer */
568 i = _IOC_NR(cmd);
570 if (!supported_mixer(codec, i))
571 return -EINVAL;
573 ac97_set_mixer(codec, i, val);
575 return 0;
578 return -EINVAL;
582 * codec_id - Turn id1/id2 into a PnP string
583 * @id1: Vendor ID1
584 * @id2: Vendor ID2
585 * @buf: CODEC_ID_BUFSZ byte buffer
587 * Fills buf with a zero terminated PnP ident string for the id1/id2
588 * pair. For convenience the return is the passed in buffer pointer.
591 static char *codec_id(u16 id1, u16 id2, char *buf)
593 if(id1&0x8080) {
594 snprintf(buf, CODEC_ID_BUFSZ, "0x%04x:0x%04x", id1, id2);
595 } else {
596 buf[0] = (id1 >> 8);
597 buf[1] = (id1 & 0xFF);
598 buf[2] = (id2 >> 8);
599 snprintf(buf+3, CODEC_ID_BUFSZ - 3, "%d", id2&0xFF);
601 return buf;
605 * ac97_check_modem - Check if the Codec is a modem
606 * @codec: codec to check
608 * Return true if the device is an AC97 1.0 or AC97 2.0 modem
611 static int ac97_check_modem(struct ac97_codec *codec)
613 /* Check for an AC97 1.0 soft modem (ID1) */
614 if(codec->codec_read(codec, AC97_RESET) & 2)
615 return 1;
616 /* Check for an AC97 2.x soft modem */
617 codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
618 if(codec->codec_read(codec, AC97_EXTENDED_MODEM_ID) & 1)
619 return 1;
620 return 0;
625 * ac97_alloc_codec - Allocate an AC97 codec
627 * Returns a new AC97 codec structure. AC97 codecs may become
628 * refcounted soon so this interface is needed. Returns with
629 * one reference taken.
632 struct ac97_codec *ac97_alloc_codec(void)
634 struct ac97_codec *codec = kzalloc(sizeof(struct ac97_codec), GFP_KERNEL);
635 if(!codec)
636 return NULL;
638 spin_lock_init(&codec->lock);
639 INIT_LIST_HEAD(&codec->list);
640 return codec;
643 EXPORT_SYMBOL(ac97_alloc_codec);
646 * ac97_release_codec - Release an AC97 codec
647 * @codec: codec to release
649 * Release an allocated AC97 codec. This will be refcounted in
650 * time but for the moment is trivial. Calls the unregister
651 * handler if the codec is now defunct.
654 void ac97_release_codec(struct ac97_codec *codec)
656 /* Remove from the list first, we don't want to be
657 "rediscovered" */
658 mutex_lock(&codec_mutex);
659 list_del(&codec->list);
660 mutex_unlock(&codec_mutex);
662 * The driver needs to deal with internal
663 * locking to avoid accidents here.
665 if(codec->driver)
666 codec->driver->remove(codec, codec->driver);
667 kfree(codec);
670 EXPORT_SYMBOL(ac97_release_codec);
673 * ac97_probe_codec - Initialize and setup AC97-compatible codec
674 * @codec: (in/out) Kernel info for a single AC97 codec
676 * Reset the AC97 codec, then initialize the mixer and
677 * the rest of the @codec structure.
679 * The codec_read and codec_write fields of @codec are
680 * required to be setup and working when this function
681 * is called. All other fields are set by this function.
683 * codec_wait field of @codec can optionally be provided
684 * when calling this function. If codec_wait is not %NULL,
685 * this function will call codec_wait any time it is
686 * necessary to wait for the audio chip to reach the
687 * codec-ready state. If codec_wait is %NULL, then
688 * the default behavior is to call schedule_timeout.
689 * Currently codec_wait is used to wait for AC97 codec
690 * reset to complete.
692 * Some codecs will power down when a register reset is
693 * performed. We now check for such codecs.
695 * Returns 1 (true) on success, or 0 (false) on failure.
698 int ac97_probe_codec(struct ac97_codec *codec)
700 u16 id1, id2;
701 u16 audio;
702 int i;
703 char cidbuf[CODEC_ID_BUFSZ];
704 u16 f;
705 struct list_head *l;
706 struct ac97_driver *d;
708 /* wait for codec-ready state */
709 if (codec->codec_wait)
710 codec->codec_wait(codec);
711 else
712 udelay(10);
714 /* will the codec power down if register reset ? */
715 id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
716 id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
717 codec->name = NULL;
718 codec->codec_ops = &null_ops;
719 for (i = 0; i < ARRAY_SIZE(ac97_codec_ids); i++) {
720 if (ac97_codec_ids[i].id == ((id1 << 16) | id2)) {
721 codec->type = ac97_codec_ids[i].id;
722 codec->name = ac97_codec_ids[i].name;
723 codec->codec_ops = ac97_codec_ids[i].ops;
724 codec->flags = ac97_codec_ids[i].flags;
725 break;
729 codec->model = (id1 << 16) | id2;
730 if ((codec->flags & AC97_DEFAULT_POWER_OFF) == 0) {
731 /* reset codec and wait for the ready bit before we continue */
732 codec->codec_write(codec, AC97_RESET, 0L);
733 if (codec->codec_wait)
734 codec->codec_wait(codec);
735 else
736 udelay(10);
739 if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
740 printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
741 (codec->id & 0x2) ? (codec->id&1 ? "4th" : "Tertiary")
742 : (codec->id&1 ? "Secondary": "Primary"));
743 return 0;
746 /* probe for Modem Codec */
747 codec->modem = ac97_check_modem(codec);
749 /* enable SPDIF */
750 f = codec->codec_read(codec, AC97_EXTENDED_STATUS);
751 if((codec->codec_ops == &null_ops) && (f & 4))
752 codec->codec_ops = &default_digital_ops;
754 /* A device which thinks its a modem but isnt */
755 if(codec->flags & AC97_DELUDED_MODEM)
756 codec->modem = 0;
758 if (codec->name == NULL)
759 codec->name = "Unknown";
760 printk(KERN_INFO "ac97_codec: AC97 %s codec, id: %s (%s)\n",
761 codec->modem ? "Modem" : (audio ? "Audio" : ""),
762 codec_id(id1, id2, cidbuf), codec->name);
764 if(!ac97_init_mixer(codec))
765 return 0;
768 * Attach last so the caller can override the mixer
769 * callbacks.
772 mutex_lock(&codec_mutex);
773 list_add(&codec->list, &codecs);
775 list_for_each(l, &codec_drivers) {
776 d = list_entry(l, struct ac97_driver, list);
777 if ((codec->model ^ d->codec_id) & d->codec_mask)
778 continue;
779 if(d->probe(codec, d) == 0)
781 codec->driver = d;
782 break;
786 mutex_unlock(&codec_mutex);
787 return 1;
790 static int ac97_init_mixer(struct ac97_codec *codec)
792 u16 cap;
793 int i;
795 cap = codec->codec_read(codec, AC97_RESET);
797 /* mixer masks */
798 codec->supported_mixers = AC97_SUPPORTED_MASK;
799 codec->stereo_mixers = AC97_STEREO_MASK;
800 codec->record_sources = AC97_RECORD_MASK;
801 if (!(cap & 0x04))
802 codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
803 if (!(cap & 0x10))
804 codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
807 /* detect bit resolution */
808 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
809 if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x2020)
810 codec->bit_resolution = 6;
811 else
812 codec->bit_resolution = 5;
814 /* generic OSS to AC97 wrapper */
815 codec->read_mixer = ac97_read_mixer;
816 codec->write_mixer = ac97_write_mixer;
817 codec->recmask_io = ac97_recmask_io;
818 codec->mixer_ioctl = ac97_mixer_ioctl;
820 /* initialize mixer channel volumes */
821 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
822 struct mixer_defaults *md = &mixer_defaults[i];
823 if (md->mixer == -1)
824 break;
825 if (!supported_mixer(codec, md->mixer))
826 continue;
827 ac97_set_mixer(codec, md->mixer, md->value);
830 /* codec specific initialization for 4-6 channel output or secondary codec stuff */
831 if (codec->codec_ops->init != NULL) {
832 codec->codec_ops->init(codec);
836 * Volume is MUTE only on this device. We have to initialise
837 * it but its useless beyond that.
839 if(codec->flags & AC97_NO_PCM_VOLUME)
841 codec->supported_mixers &= ~SOUND_MASK_PCM;
842 printk(KERN_WARNING "AC97 codec does not have proper volume support.\n");
844 return 1;
847 #define AC97_SIGMATEL_ANALOG 0x6c /* Analog Special */
848 #define AC97_SIGMATEL_DAC2INVERT 0x6e
849 #define AC97_SIGMATEL_BIAS1 0x70
850 #define AC97_SIGMATEL_BIAS2 0x72
851 #define AC97_SIGMATEL_MULTICHN 0x74 /* Multi-Channel programming */
852 #define AC97_SIGMATEL_CIC1 0x76
853 #define AC97_SIGMATEL_CIC2 0x78
856 static int sigmatel_9708_init(struct ac97_codec * codec)
858 u16 codec72, codec6c;
860 codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
861 codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
863 if ((codec72==0) && (codec6c==0)) {
864 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
865 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
866 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
867 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
868 } else if ((codec72==0x8000) && (codec6c==0)) {
869 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
870 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
871 codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
872 } else if ((codec72==0x8000) && (codec6c==0x0080)) {
873 /* nothing */
875 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
876 return 0;
880 static int sigmatel_9721_init(struct ac97_codec * codec)
882 /* Only set up secondary codec */
883 if (codec->id == 0)
884 return 0;
886 codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
888 /* initialize SigmaTel STAC9721/23 as secondary codec, decoding AC link
889 sloc 3,4 = 0x01, slot 7,8 = 0x00, */
890 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
892 /* we don't have the crystal when we are on an AMR card, so use
893 BIT_CLK as our clock source. Write the magic word ABBA and read
894 back to enable register 0x78 */
895 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
896 codec->codec_read(codec, AC97_SIGMATEL_CIC1);
898 /* sync all the clocks*/
899 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
901 return 0;
905 static int sigmatel_9744_init(struct ac97_codec * codec)
907 // patch for SigmaTel
908 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
909 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000); // is this correct? --jk
910 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
911 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
912 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
913 return 0;
916 static int cmedia_init(struct ac97_codec *codec)
918 /* Initialise the CMedia 9739 */
920 We could set various options here
921 Register 0x20 bit 0x100 sets mic as center bass
922 Also do multi_channel_ctrl &=~0x3000 |=0x1000
924 For now we set up the GPIO and PC beep
927 u16 v;
929 /* MIC */
930 codec->codec_write(codec, 0x64, 0x3000);
931 v = codec->codec_read(codec, 0x64);
932 v &= ~0x8000;
933 codec->codec_write(codec, 0x64, v);
934 codec->codec_write(codec, 0x70, 0x0100);
935 codec->codec_write(codec, 0x72, 0x0020);
936 return 0;
939 #define AC97_WM97XX_FMIXER_VOL 0x72
940 #define AC97_WM97XX_RMIXER_VOL 0x74
941 #define AC97_WM97XX_TEST 0x5a
942 #define AC97_WM9704_RPCM_VOL 0x70
943 #define AC97_WM9711_OUT3VOL 0x16
945 static int wolfson_init03(struct ac97_codec * codec)
947 /* this is known to work for the ViewSonic ViewPad 1000 */
948 codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
949 codec->codec_write(codec, AC97_GENERAL_PURPOSE, 0x8000);
950 return 0;
953 static int wolfson_init04(struct ac97_codec * codec)
955 codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
956 codec->codec_write(codec, AC97_WM97XX_RMIXER_VOL, 0x0808);
958 // patch for DVD noise
959 codec->codec_write(codec, AC97_WM97XX_TEST, 0x0200);
961 // init vol as PCM vol
962 codec->codec_write(codec, AC97_WM9704_RPCM_VOL,
963 codec->codec_read(codec, AC97_PCMOUT_VOL));
965 /* set rear surround volume */
966 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
967 return 0;
970 /* WM9705, WM9710 */
971 static int wolfson_init05(struct ac97_codec * codec)
973 /* set front mixer volume */
974 codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
975 return 0;
978 /* WM9711, WM9712 */
979 static int wolfson_init11(struct ac97_codec * codec)
981 /* stop pop's during suspend/resume */
982 codec->codec_write(codec, AC97_WM97XX_TEST,
983 codec->codec_read(codec, AC97_WM97XX_TEST) & 0xffbf);
985 /* set out3 volume */
986 codec->codec_write(codec, AC97_WM9711_OUT3VOL, 0x0808);
987 return 0;
990 /* WM9713 */
991 static int wolfson_init13(struct ac97_codec * codec)
993 codec->codec_write(codec, AC97_RECORD_GAIN, 0x00a0);
994 codec->codec_write(codec, AC97_POWER_CONTROL, 0x0000);
995 codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0xDA00);
996 codec->codec_write(codec, AC97_EXTEND_MODEM_STAT, 0x3810);
997 codec->codec_write(codec, AC97_PHONE_VOL, 0x0808);
998 codec->codec_write(codec, AC97_PCBEEP_VOL, 0x0808);
1000 return 0;
1003 static int tritech_init(struct ac97_codec * codec)
1005 codec->codec_write(codec, 0x26, 0x0300);
1006 codec->codec_write(codec, 0x26, 0x0000);
1007 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
1008 codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
1009 return 0;
1013 /* copied from drivers/sound/maestro.c */
1014 static int tritech_maestro_init(struct ac97_codec * codec)
1016 /* no idea what this does */
1017 codec->codec_write(codec, 0x2A, 0x0001);
1018 codec->codec_write(codec, 0x2C, 0x0000);
1019 codec->codec_write(codec, 0x2C, 0XFFFF);
1020 return 0;
1026 #define AC97_AD1886_JACK_SENSE 0x72
1028 static int ad1886_init(struct ac97_codec * codec)
1030 /* from AD1886 Specs */
1031 codec->codec_write(codec, AC97_AD1886_JACK_SENSE, 0x0010);
1032 return 0;
1039 * This is basically standard AC97. It should work as a default for
1040 * almost all modern codecs. Note that some cards wire EAPD *backwards*
1041 * That side of it is up to the card driver not us to cope with.
1045 static int eapd_control(struct ac97_codec * codec, int on)
1047 if(on)
1048 codec->codec_write(codec, AC97_POWER_CONTROL,
1049 codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
1050 else
1051 codec->codec_write(codec, AC97_POWER_CONTROL,
1052 codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
1053 return 0;
1056 static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1058 u16 reg;
1060 reg = codec->codec_read(codec, AC97_SPDIF_CONTROL);
1062 switch(rate)
1064 /* Off by default */
1065 default:
1066 case 0:
1067 reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1068 codec->codec_write(codec, AC97_EXTENDED_STATUS, (reg & ~AC97_EA_SPDIF));
1069 if(rate == 0)
1070 return 0;
1071 return -EINVAL;
1072 case 1:
1073 reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
1074 break;
1075 case 2:
1076 reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
1077 break;
1078 case 3:
1079 reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
1080 break;
1083 reg &= ~AC97_SC_CC_MASK;
1084 reg |= (mode & AUDIO_CCMASK) << 6;
1086 if(mode & AUDIO_DIGITAL)
1087 reg |= 2;
1088 if(mode & AUDIO_PRO)
1089 reg |= 1;
1090 if(mode & AUDIO_DRS)
1091 reg |= 0x4000;
1093 codec->codec_write(codec, AC97_SPDIF_CONTROL, reg);
1095 reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1096 reg &= (AC97_EA_SLOT_MASK);
1097 reg |= AC97_EA_VRA | AC97_EA_SPDIF | slots;
1098 codec->codec_write(codec, AC97_EXTENDED_STATUS, reg);
1100 reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1101 if(!(reg & 0x0400))
1103 codec->codec_write(codec, AC97_EXTENDED_STATUS, reg & ~ AC97_EA_SPDIF);
1104 return -EINVAL;
1106 return 0;
1110 * Crystal digital audio control (CS4299)
1113 static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1115 u16 cv;
1117 if(mode & AUDIO_DIGITAL)
1118 return -EINVAL;
1120 switch(rate)
1122 case 0: cv = 0x0; break; /* SPEN off */
1123 case 48000: cv = 0x8004; break; /* 48KHz digital */
1124 case 44100: cv = 0x8104; break; /* 44.1KHz digital */
1125 case 32768: /* 32Khz */
1126 default:
1127 return -EINVAL;
1129 codec->codec_write(codec, 0x68, cv);
1130 return 0;
1134 * CMedia digital audio control
1135 * Needs more work.
1138 static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1140 u16 cv;
1142 if(mode & AUDIO_DIGITAL)
1143 return -EINVAL;
1145 switch(rate)
1147 case 0: cv = 0x0001; break; /* SPEN off */
1148 case 48000: cv = 0x0009; break; /* 48KHz digital */
1149 default:
1150 return -EINVAL;
1152 codec->codec_write(codec, 0x2A, 0x05c4);
1153 codec->codec_write(codec, 0x6C, cv);
1155 /* Switch on mix to surround */
1156 cv = codec->codec_read(codec, 0x64);
1157 cv &= ~0x0200;
1158 if(mode)
1159 cv |= 0x0200;
1160 codec->codec_write(codec, 0x64, cv);
1161 return 0;
1165 /* copied from drivers/sound/maestro.c */
1166 #if 0 /* there has been 1 person on the planet with a pt101 that we
1167 know of. If they care, they can put this back in :) */
1168 static int pt101_init(struct ac97_codec * codec)
1170 printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
1171 /* who knows.. */
1172 codec->codec_write(codec, 0x2A, 0x0001);
1173 codec->codec_write(codec, 0x2C, 0x0000);
1174 codec->codec_write(codec, 0x2C, 0xFFFF);
1175 codec->codec_write(codec, 0x10, 0x9F1F);
1176 codec->codec_write(codec, 0x12, 0x0808);
1177 codec->codec_write(codec, 0x14, 0x9F1F);
1178 codec->codec_write(codec, 0x16, 0x9F1F);
1179 codec->codec_write(codec, 0x18, 0x0404);
1180 codec->codec_write(codec, 0x1A, 0x0000);
1181 codec->codec_write(codec, 0x1C, 0x0000);
1182 codec->codec_write(codec, 0x02, 0x0404);
1183 codec->codec_write(codec, 0x04, 0x0808);
1184 codec->codec_write(codec, 0x0C, 0x801F);
1185 codec->codec_write(codec, 0x0E, 0x801F);
1186 return 0;
1188 #endif
1191 EXPORT_SYMBOL(ac97_probe_codec);
1193 MODULE_LICENSE("GPL");