1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <linux/module.h>
49#include <linux/kernel.h>
50#include <linux/slab.h>
51#include <linux/string.h>
52#include <linux/errno.h>
53#include <linux/bitops.h>
54#include <linux/delay.h>
55#include <linux/pci.h>
56#include <linux/ac97_codec.h>
57#include <asm/uaccess.h>
58#include <linux/mutex.h>
59
60#define CODEC_ID_BUFSZ 14
61
62static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel);
63static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
64 unsigned int left, unsigned int right);
65static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val );
66static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask);
67static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg);
68
69static int ac97_init_mixer(struct ac97_codec *codec);
70
71static int wolfson_init03(struct ac97_codec * codec);
72static int wolfson_init04(struct ac97_codec * codec);
73static int wolfson_init05(struct ac97_codec * codec);
74static int wolfson_init11(struct ac97_codec * codec);
75static int wolfson_init13(struct ac97_codec * codec);
76static int tritech_init(struct ac97_codec * codec);
77static int tritech_maestro_init(struct ac97_codec * codec);
78static int sigmatel_9708_init(struct ac97_codec *codec);
79static int sigmatel_9721_init(struct ac97_codec *codec);
80static int sigmatel_9744_init(struct ac97_codec *codec);
81static int ad1886_init(struct ac97_codec *codec);
82static int eapd_control(struct ac97_codec *codec, int);
83static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
84static int cmedia_init(struct ac97_codec * codec);
85static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
86static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105static struct ac97_ops null_ops = { NULL, NULL, NULL };
106static struct ac97_ops default_ops = { NULL, eapd_control, NULL };
107static struct ac97_ops default_digital_ops = { NULL, eapd_control, generic_digital_control};
108static struct ac97_ops wolfson_ops03 = { wolfson_init03, NULL, NULL };
109static struct ac97_ops wolfson_ops04 = { wolfson_init04, NULL, NULL };
110static struct ac97_ops wolfson_ops05 = { wolfson_init05, NULL, NULL };
111static struct ac97_ops wolfson_ops11 = { wolfson_init11, NULL, NULL };
112static struct ac97_ops wolfson_ops13 = { wolfson_init13, NULL, NULL };
113static struct ac97_ops tritech_ops = { tritech_init, NULL, NULL };
114static struct ac97_ops tritech_m_ops = { tritech_maestro_init, NULL, NULL };
115static struct ac97_ops sigmatel_9708_ops = { sigmatel_9708_init, NULL, NULL };
116static struct ac97_ops sigmatel_9721_ops = { sigmatel_9721_init, NULL, NULL };
117static struct ac97_ops sigmatel_9744_ops = { sigmatel_9744_init, NULL, NULL };
118static struct ac97_ops crystal_digital_ops = { NULL, eapd_control, crystal_digital_control };
119static struct ac97_ops ad1886_ops = { ad1886_init, eapd_control, NULL };
120static struct ac97_ops cmedia_ops = { NULL, eapd_control, NULL};
121static struct ac97_ops cmedia_digital_ops = { cmedia_init, eapd_control, cmedia_digital_control};
122
123
124static 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},
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},
190};
191
192
193static struct mixer_defaults {
194 int mixer;
195 unsigned int value;
196} mixer_defaults[SOUND_MIXER_NRDEVICES] = {
197
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}
213};
214
215
216static 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},
234};
235
236
237enum 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,
244 AC97_REC_MONO,
245 AC97_REC_PHONE
246};
247
248static 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
256};
257
258
259static 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
267};
268
269static LIST_HEAD(codecs);
270static LIST_HEAD(codec_drivers);
271static DEFINE_MUTEX(codec_mutex);
272
273
274
275static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel)
276{
277 u16 val;
278 int ret = 0;
279 int scale;
280 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
281
282 val = codec->codec_read(codec , mh->offset);
283
284 if (val & AC97_MUTE) {
285 ret = 0;
286 } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
287
288 int left,right;
289
290 left = (val >> 8) & 0x7f;
291 right = val & 0x7f;
292
293 if (oss_channel == SOUND_MIXER_IGAIN) {
294 right = (right * 100) / mh->scale;
295 left = (left * 100) / mh->scale;
296 } else {
297
298 if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
299 scale = (1 << codec->bit_resolution);
300 else
301 scale = mh->scale;
302
303 right = 100 - ((right * 100) / scale);
304 left = 100 - ((left * 100) / scale);
305 }
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
317
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);
322 }
323
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
330
331 return ret;
332}
333
334
335
336static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
337 unsigned int left, unsigned int right)
338{
339 u16 val = 0;
340 int scale;
341 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
342
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
349
350 if (AC97_STEREO_MASK & (1 << oss_channel)) {
351
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
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;
369
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;
376 }
377 val = (left << 8) | right;
378 }
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
416
417 }
418#ifdef DEBUG
419 printk(" 0x%04x", val);
420#endif
421
422 codec->codec_write(codec, mh->offset, val);
423
424#ifdef DEBUG
425 val = codec->codec_read(codec, mh->offset);
426 printk(" -> 0x%04x\n", val);
427#endif
428}
429
430
431static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val )
432{
433 unsigned int left,right;
434
435
436 right = ((val >> 8) & 0xff) ;
437 left = (val & 0xff) ;
438
439 if (right > 100) right = 100;
440 if (left > 100) left = 100;
441
442 codec->mixer_state[oss_mixer] = (right << 8) | left;
443 codec->write_mixer(codec, oss_mixer, left, right);
444}
445
446
447
448
449
450static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask)
451{
452 unsigned int val;
453
454 if (rw) {
455
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]);
461 }
462
463
464
465
466 val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
467 if (mask != val)
468 mask &= ~val;
469
470 val = ffs(mask);
471 val = ac97_oss_rm[val-1];
472 val |= val << 8;
473
474#ifdef DEBUG
475 printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
476#endif
477
478 codec->codec_write(codec, AC97_RECORD_SELECT, val);
479
480 return 0;
481};
482
483static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
484{
485 int i, val = 0;
486
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;
496 }
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;
505 }
506
507 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
508 return -EINVAL;
509
510 if (cmd == OSS_GETVERSION)
511 return put_user(SOUND_VERSION, (int __user *)arg);
512
513 if (_SIOC_DIR(cmd) == _SIOC_READ) {
514 switch (_IOC_NR(cmd)) {
515 case SOUND_MIXER_RECSRC:
516 if (!codec->recmask_io) {
517 val = 0;
518 } else {
519 val = codec->recmask_io(codec, 1, 0);
520 }
521 break;
522
523 case SOUND_MIXER_DEVMASK:
524 val = codec->supported_mixers;
525 break;
526
527 case SOUND_MIXER_RECMASK:
528 val = codec->record_sources;
529 break;
530
531 case SOUND_MIXER_STEREODEVS:
532 val = codec->stereo_mixers;
533 break;
534
535 case SOUND_MIXER_CAPS:
536 val = SOUND_CAP_EXCL_INPUT;
537 break;
538
539 default:
540 i = _IOC_NR(cmd);
541
542 if (!supported_mixer(codec, i))
543 return -EINVAL;
544
545
546
547 val = codec->mixer_state[i];
548 break;
549 }
550 return put_user(val, (int __user *)arg);
551 }
552
553 if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
554 codec->modcnt++;
555 if (get_user(val, (int __user *)arg))
556 return -EFAULT;
557
558 switch (_IOC_NR(cmd)) {
559 case SOUND_MIXER_RECSRC:
560 if (!codec->recmask_io) return -EINVAL;
561 if (!val) return 0;
562 if (!(val &= codec->record_sources)) return -EINVAL;
563
564 codec->recmask_io(codec, 0, val);
565
566 return 0;
567 default:
568 i = _IOC_NR(cmd);
569
570 if (!supported_mixer(codec, i))
571 return -EINVAL;
572
573 ac97_set_mixer(codec, i, val);
574
575 return 0;
576 }
577 }
578 return -EINVAL;
579}
580
581
582
583
584
585
586
587
588
589
590
591static char *codec_id(u16 id1, u16 id2, char *buf)
592{
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);
600 }
601 return buf;
602}
603
604
605
606
607
608
609
610
611static int ac97_check_modem(struct ac97_codec *codec)
612{
613
614 if(codec->codec_read(codec, AC97_RESET) & 2)
615 return 1;
616
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;
621}
622
623
624
625
626
627
628
629
630
631
632struct ac97_codec *ac97_alloc_codec(void)
633{
634 struct ac97_codec *codec = kzalloc(sizeof(struct ac97_codec), GFP_KERNEL);
635 if(!codec)
636 return NULL;
637
638 spin_lock_init(&codec->lock);
639 INIT_LIST_HEAD(&codec->list);
640 return codec;
641}
642
643EXPORT_SYMBOL(ac97_alloc_codec);
644
645
646
647
648
649
650
651
652
653
654void ac97_release_codec(struct ac97_codec *codec)
655{
656
657
658 mutex_lock(&codec_mutex);
659 list_del(&codec->list);
660 mutex_unlock(&codec_mutex);
661
662
663
664
665 if(codec->driver)
666 codec->driver->remove(codec, codec->driver);
667 kfree(codec);
668}
669
670EXPORT_SYMBOL(ac97_release_codec);
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698int ac97_probe_codec(struct ac97_codec *codec)
699{
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;
707
708
709 if (codec->codec_wait)
710 codec->codec_wait(codec);
711 else
712 udelay(10);
713
714
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;
726 }
727 }
728
729 codec->model = (id1 << 16) | id2;
730 if ((codec->flags & AC97_DEFAULT_POWER_OFF) == 0) {
731
732 codec->codec_write(codec, AC97_RESET, 0L);
733 if (codec->codec_wait)
734 codec->codec_wait(codec);
735 else
736 udelay(10);
737 }
738
739
740
741
742
743
744
745 if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
746 printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
747 (codec->id & 0x2) ? (codec->id&1 ? "4th" : "Tertiary")
748 : (codec->id&1 ? "Secondary": "Primary"));
749 return 0;
750 }
751
752
753 codec->modem = ac97_check_modem(codec);
754
755
756 f = codec->codec_read(codec, AC97_EXTENDED_STATUS);
757 if((codec->codec_ops == &null_ops) && (f & 4))
758 codec->codec_ops = &default_digital_ops;
759
760
761 if(codec->flags & AC97_DELUDED_MODEM)
762 codec->modem = 0;
763
764 if (codec->name == NULL)
765 codec->name = "Unknown";
766 printk(KERN_INFO "ac97_codec: AC97 %s codec, id: %s (%s)\n",
767 codec->modem ? "Modem" : (audio ? "Audio" : ""),
768 codec_id(id1, id2, cidbuf), codec->name);
769
770 if(!ac97_init_mixer(codec))
771 return 0;
772
773
774
775
776
777
778 mutex_lock(&codec_mutex);
779 list_add(&codec->list, &codecs);
780
781 list_for_each(l, &codec_drivers) {
782 d = list_entry(l, struct ac97_driver, list);
783 if ((codec->model ^ d->codec_id) & d->codec_mask)
784 continue;
785 if(d->probe(codec, d) == 0)
786 {
787 codec->driver = d;
788 break;
789 }
790 }
791
792 mutex_unlock(&codec_mutex);
793 return 1;
794}
795
796static int ac97_init_mixer(struct ac97_codec *codec)
797{
798 u16 cap;
799 int i;
800
801 cap = codec->codec_read(codec, AC97_RESET);
802
803
804 codec->supported_mixers = AC97_SUPPORTED_MASK;
805 codec->stereo_mixers = AC97_STEREO_MASK;
806 codec->record_sources = AC97_RECORD_MASK;
807 if (!(cap & 0x04))
808 codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
809 if (!(cap & 0x10))
810 codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
811
812
813
814 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
815 if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x2020)
816 codec->bit_resolution = 6;
817 else
818 codec->bit_resolution = 5;
819
820
821 codec->read_mixer = ac97_read_mixer;
822 codec->write_mixer = ac97_write_mixer;
823 codec->recmask_io = ac97_recmask_io;
824 codec->mixer_ioctl = ac97_mixer_ioctl;
825
826
827 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
828 struct mixer_defaults *md = &mixer_defaults[i];
829 if (md->mixer == -1)
830 break;
831 if (!supported_mixer(codec, md->mixer))
832 continue;
833 ac97_set_mixer(codec, md->mixer, md->value);
834 }
835
836
837 if (codec->codec_ops->init != NULL) {
838 codec->codec_ops->init(codec);
839 }
840
841
842
843
844
845 if(codec->flags & AC97_NO_PCM_VOLUME)
846 {
847 codec->supported_mixers &= ~SOUND_MASK_PCM;
848 printk(KERN_WARNING "AC97 codec does not have proper volume support.\n");
849 }
850 return 1;
851}
852
853#define AC97_SIGMATEL_ANALOG 0x6c
854#define AC97_SIGMATEL_DAC2INVERT 0x6e
855#define AC97_SIGMATEL_BIAS1 0x70
856#define AC97_SIGMATEL_BIAS2 0x72
857#define AC97_SIGMATEL_MULTICHN 0x74
858#define AC97_SIGMATEL_CIC1 0x76
859#define AC97_SIGMATEL_CIC2 0x78
860
861
862static int sigmatel_9708_init(struct ac97_codec * codec)
863{
864 u16 codec72, codec6c;
865
866 codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
867 codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
868
869 if ((codec72==0) && (codec6c==0)) {
870 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
871 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
872 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
873 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
874 } else if ((codec72==0x8000) && (codec6c==0)) {
875 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
876 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
877 codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
878 } else if ((codec72==0x8000) && (codec6c==0x0080)) {
879
880 }
881 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
882 return 0;
883}
884
885
886static int sigmatel_9721_init(struct ac97_codec * codec)
887{
888
889 if (codec->id == 0)
890 return 0;
891
892 codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
893
894
895
896 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
897
898
899
900
901 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
902 codec->codec_read(codec, AC97_SIGMATEL_CIC1);
903
904
905 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
906
907 return 0;
908}
909
910
911static int sigmatel_9744_init(struct ac97_codec * codec)
912{
913
914 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
915 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000);
916 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
917 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
918 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
919 return 0;
920}
921
922static int cmedia_init(struct ac97_codec *codec)
923{
924
925
926
927
928
929
930
931
932
933 u16 v;
934
935
936 codec->codec_write(codec, 0x64, 0x3000);
937 v = codec->codec_read(codec, 0x64);
938 v &= ~0x8000;
939 codec->codec_write(codec, 0x64, v);
940 codec->codec_write(codec, 0x70, 0x0100);
941 codec->codec_write(codec, 0x72, 0x0020);
942 return 0;
943}
944
945#define AC97_WM97XX_FMIXER_VOL 0x72
946#define AC97_WM97XX_RMIXER_VOL 0x74
947#define AC97_WM97XX_TEST 0x5a
948#define AC97_WM9704_RPCM_VOL 0x70
949#define AC97_WM9711_OUT3VOL 0x16
950
951static int wolfson_init03(struct ac97_codec * codec)
952{
953
954 codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
955 codec->codec_write(codec, AC97_GENERAL_PURPOSE, 0x8000);
956 return 0;
957}
958
959static int wolfson_init04(struct ac97_codec * codec)
960{
961 codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
962 codec->codec_write(codec, AC97_WM97XX_RMIXER_VOL, 0x0808);
963
964
965 codec->codec_write(codec, AC97_WM97XX_TEST, 0x0200);
966
967
968 codec->codec_write(codec, AC97_WM9704_RPCM_VOL,
969 codec->codec_read(codec, AC97_PCMOUT_VOL));
970
971
972 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
973 return 0;
974}
975
976
977static int wolfson_init05(struct ac97_codec * codec)
978{
979
980 codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
981 return 0;
982}
983
984
985static int wolfson_init11(struct ac97_codec * codec)
986{
987
988 codec->codec_write(codec, AC97_WM97XX_TEST,
989 codec->codec_read(codec, AC97_WM97XX_TEST) & 0xffbf);
990
991
992 codec->codec_write(codec, AC97_WM9711_OUT3VOL, 0x0808);
993 return 0;
994}
995
996
997static int wolfson_init13(struct ac97_codec * codec)
998{
999 codec->codec_write(codec, AC97_RECORD_GAIN, 0x00a0);
1000 codec->codec_write(codec, AC97_POWER_CONTROL, 0x0000);
1001 codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0xDA00);
1002 codec->codec_write(codec, AC97_EXTEND_MODEM_STAT, 0x3810);
1003 codec->codec_write(codec, AC97_PHONE_VOL, 0x0808);
1004 codec->codec_write(codec, AC97_PCBEEP_VOL, 0x0808);
1005
1006 return 0;
1007}
1008
1009static int tritech_init(struct ac97_codec * codec)
1010{
1011 codec->codec_write(codec, 0x26, 0x0300);
1012 codec->codec_write(codec, 0x26, 0x0000);
1013 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
1014 codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
1015 return 0;
1016}
1017
1018
1019
1020static int tritech_maestro_init(struct ac97_codec * codec)
1021{
1022
1023 codec->codec_write(codec, 0x2A, 0x0001);
1024 codec->codec_write(codec, 0x2C, 0x0000);
1025 codec->codec_write(codec, 0x2C, 0XFFFF);
1026 return 0;
1027}
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038#define AC97_AD1886_JACK_SENSE 0x72
1039
1040static int ad1886_init(struct ac97_codec * codec)
1041{
1042
1043 codec->codec_write(codec, AC97_AD1886_JACK_SENSE, 0x0010);
1044 return 0;
1045}
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057static int eapd_control(struct ac97_codec * codec, int on)
1058{
1059 if(on)
1060 codec->codec_write(codec, AC97_POWER_CONTROL,
1061 codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
1062 else
1063 codec->codec_write(codec, AC97_POWER_CONTROL,
1064 codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
1065 return 0;
1066}
1067
1068static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1069{
1070 u16 reg;
1071
1072 reg = codec->codec_read(codec, AC97_SPDIF_CONTROL);
1073
1074 switch(rate)
1075 {
1076
1077 default:
1078 case 0:
1079 reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1080 codec->codec_write(codec, AC97_EXTENDED_STATUS, (reg & ~AC97_EA_SPDIF));
1081 if(rate == 0)
1082 return 0;
1083 return -EINVAL;
1084 case 1:
1085 reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
1086 break;
1087 case 2:
1088 reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
1089 break;
1090 case 3:
1091 reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
1092 break;
1093 }
1094
1095 reg &= ~AC97_SC_CC_MASK;
1096 reg |= (mode & AUDIO_CCMASK) << 6;
1097
1098 if(mode & AUDIO_DIGITAL)
1099 reg |= 2;
1100 if(mode & AUDIO_PRO)
1101 reg |= 1;
1102 if(mode & AUDIO_DRS)
1103 reg |= 0x4000;
1104
1105 codec->codec_write(codec, AC97_SPDIF_CONTROL, reg);
1106
1107 reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1108 reg &= (AC97_EA_SLOT_MASK);
1109 reg |= AC97_EA_VRA | AC97_EA_SPDIF | slots;
1110 codec->codec_write(codec, AC97_EXTENDED_STATUS, reg);
1111
1112 reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1113 if(!(reg & 0x0400))
1114 {
1115 codec->codec_write(codec, AC97_EXTENDED_STATUS, reg & ~ AC97_EA_SPDIF);
1116 return -EINVAL;
1117 }
1118 return 0;
1119}
1120
1121
1122
1123
1124
1125static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1126{
1127 u16 cv;
1128
1129 if(mode & AUDIO_DIGITAL)
1130 return -EINVAL;
1131
1132 switch(rate)
1133 {
1134 case 0: cv = 0x0; break;
1135 case 48000: cv = 0x8004; break;
1136 case 44100: cv = 0x8104; break;
1137 case 32768:
1138 default:
1139 return -EINVAL;
1140 }
1141 codec->codec_write(codec, 0x68, cv);
1142 return 0;
1143}
1144
1145
1146
1147
1148
1149
1150static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1151{
1152 u16 cv;
1153
1154 if(mode & AUDIO_DIGITAL)
1155 return -EINVAL;
1156
1157 switch(rate)
1158 {
1159 case 0: cv = 0x0001; break;
1160 case 48000: cv = 0x0009; break;
1161 default:
1162 return -EINVAL;
1163 }
1164 codec->codec_write(codec, 0x2A, 0x05c4);
1165 codec->codec_write(codec, 0x6C, cv);
1166
1167
1168 cv = codec->codec_read(codec, 0x64);
1169 cv &= ~0x0200;
1170 if(mode)
1171 cv |= 0x0200;
1172 codec->codec_write(codec, 0x64, cv);
1173 return 0;
1174}
1175
1176
1177
1178#if 0
1179
1180static int pt101_init(struct ac97_codec * codec)
1181{
1182 printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
1183
1184 codec->codec_write(codec, 0x2A, 0x0001);
1185 codec->codec_write(codec, 0x2C, 0x0000);
1186 codec->codec_write(codec, 0x2C, 0xFFFF);
1187 codec->codec_write(codec, 0x10, 0x9F1F);
1188 codec->codec_write(codec, 0x12, 0x0808);
1189 codec->codec_write(codec, 0x14, 0x9F1F);
1190 codec->codec_write(codec, 0x16, 0x9F1F);
1191 codec->codec_write(codec, 0x18, 0x0404);
1192 codec->codec_write(codec, 0x1A, 0x0000);
1193 codec->codec_write(codec, 0x1C, 0x0000);
1194 codec->codec_write(codec, 0x02, 0x0404);
1195 codec->codec_write(codec, 0x04, 0x0808);
1196 codec->codec_write(codec, 0x0C, 0x801F);
1197 codec->codec_write(codec, 0x0E, 0x801F);
1198 return 0;
1199}
1200#endif
1201
1202
1203EXPORT_SYMBOL(ac97_probe_codec);
1204
1205MODULE_LICENSE("GPL");
1206
1207