Merge with 2.4.0-test3-pre4.
[linux-2.6/linux-mips.git] / drivers / sound / opl3sa2.c
blob23f85733e2da97acb95eb60dfaf29bcf2e459db5
1 /*
2 * sound/opl3sa2.c
4 * A low level driver for Yamaha OPL3-SA2 and SA3 cards.
5 * SAx cards should work, as they are just variants of the SA3.
7 * Copyright 1998, 1999 Scott Murray <scottm@interlog.com>
9 * Originally based on the CS4232 driver (in cs4232.c) by Hannu Savolainen
10 * and others. Now incorporates code/ideas from pss.c, also by Hannu
11 * Savolainen. Both of those files are distributed with the following
12 * license:
14 * "Copyright (C) by Hannu Savolainen 1993-1997
16 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
17 * Version 2 (June 1991). See the "COPYING" file distributed with this software
18 * for more info."
20 * As such, in accordance with the above license, this file, opl3sa2.c, is
21 * distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2 (June 1991).
22 * See the "COPYING" file distributed with this software for more information.
24 * Change History
25 * --------------
26 * Scott Murray Original driver (Jun 14, 1998)
27 * Paul J.Y. Lahaie Changed probing / attach code order
28 * Scott Murray Added mixer support (Dec 03, 1998)
29 * Scott Murray Changed detection code to be more forgiving,
30 * added force option as last resort,
31 * fixed ioctl return values. (Dec 30, 1998)
32 * Scott Murray Simpler detection code should work all the time now
33 * (with thanks to Ben Hutchings for the heuristic),
34 * removed now unnecessary force option. (Jan 5, 1999)
35 * Christoph Hellwig Adapted to module_init/module_exit (Mar 4, 2000)
39 #include <linux/config.h>
40 #include <linux/init.h>
41 #include <linux/module.h>
43 #include "sound_config.h"
44 #include "soundmodule.h"
46 #include "ad1848.h"
47 #include "mpu401.h"
49 /* Useful control port indexes: */
50 #define OPL3SA2_MASTER_LEFT 0x07
51 #define OPL3SA2_MASTER_RIGHT 0x08
52 #define OPL3SA2_MIC 0x09
53 #define OPL3SA2_MISC 0x0A
55 #define OPL3SA3_WIDE 0x14
56 #define OPL3SA3_BASS 0x15
57 #define OPL3SA3_TREBLE 0x16
59 /* Useful constants: */
60 #define DEFAULT_VOLUME 50
61 #define DEFAULT_MIC 50
62 #define DEFAULT_TIMBRE 0
64 #define CHIPSET_UNKNOWN -1
67 * These are used both as masks against what the card returns,
68 * and as constants.
70 #define CHIPSET_OPL3SA2 1
71 #define CHIPSET_OPL3SA3 2
72 #define CHIPSET_OPL3SAX 4
75 /* What's my version? */
76 static int chipset = CHIPSET_UNKNOWN;
78 /* Oh well, let's just cache the name */
79 static char chipset_name[16];
81 /* Where's my mixer */
82 static int opl3sa2_mixer = -1;
84 /* Bag o' mixer data */
85 typedef struct opl3sa2_mixerdata {
86 unsigned short cfg_port;
87 unsigned short padding;
88 int ad_mixer_dev;
89 unsigned int volume_l;
90 unsigned int volume_r;
91 unsigned int mic;
92 unsigned int bass;
93 unsigned int treble;
94 } opl3sa2_mixerdata;
96 #ifdef CONFIG_OPL3SA2_CTRL_BASE
97 /* Set control port if compiled into the kernel */
98 static opl3sa2_mixerdata opl3sa2_data = { CONFIG_OPL3SA2_CTRL_BASE, };
99 #else
100 static opl3sa2_mixerdata opl3sa2_data;
101 #endif
103 static opl3sa2_mixerdata *devc = &opl3sa2_data;
106 /* Standard read and write functions */
108 static void opl3sa2_write(unsigned short port,
109 unsigned char index,
110 unsigned char data)
112 outb_p(index, port);
113 outb(data, port + 1);
117 static void opl3sa2_read(unsigned short port,
118 unsigned char index,
119 unsigned char* data)
121 outb_p(index, port);
122 *data = inb(port + 1);
126 /* All of the mixer functions... */
128 static void opl3sa2_set_volume(opl3sa2_mixerdata *devc, int left, int right)
130 static unsigned char scale[101] = {
131 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
132 0x0e, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
133 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
134 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
135 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
136 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
137 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05,
138 0x05, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
139 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02,
140 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
141 0x00
143 unsigned char vol;
145 vol = scale[left];
147 /* If level is zero, turn on mute */
148 if(!left)
149 vol |= 0x80;
151 opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol);
153 vol = scale[right];
155 /* If level is zero, turn on mute */
156 if(!right)
157 vol |= 0x80;
159 opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_RIGHT, vol);
163 static void opl3sa2_set_mic(opl3sa2_mixerdata *devc, int level)
165 unsigned char vol = 0x1F;
167 if((level >= 0) && (level <= 100))
168 vol = 0x1F - (unsigned char) (0x1F * level / 100L);
170 /* If level is zero, turn on mute */
171 if(!level)
172 vol |= 0x80;
174 opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol);
178 static void opl3sa3_set_bass(opl3sa2_mixerdata *devc, int level)
180 unsigned char bass;
182 bass = level ? ((unsigned char) (0x07 * level / 100L)) : 0;
183 bass |= (bass << 4);
185 opl3sa2_write(devc->cfg_port, OPL3SA3_BASS, bass);
189 static void opl3sa3_set_treble(opl3sa2_mixerdata *devc, int level)
191 unsigned char treble;
193 treble = level ? ((unsigned char) (0x07 * level / 100L)) : 0;
194 treble |= (treble << 4);
196 opl3sa2_write(devc->cfg_port, OPL3SA3_TREBLE, treble);
200 static void opl3sa2_mixer_reset(opl3sa2_mixerdata *devc)
202 if(devc)
204 opl3sa2_set_volume(devc, DEFAULT_VOLUME, DEFAULT_VOLUME);
205 devc->volume_l = devc->volume_r = DEFAULT_VOLUME;
207 opl3sa2_set_mic(devc, DEFAULT_MIC);
208 devc->mic = DEFAULT_MIC;
210 opl3sa3_set_bass(devc, DEFAULT_TIMBRE);
211 opl3sa3_set_treble(devc, DEFAULT_TIMBRE);
212 devc->bass = devc->treble = DEFAULT_TIMBRE;
217 static void arg_to_volume_mono(unsigned int volume, int *aleft)
219 int left;
221 left = volume & 0x00ff;
222 if (left > 100)
223 left = 100;
224 *aleft = left;
228 static void arg_to_volume_stereo(unsigned int volume, int *aleft, int *aright)
230 arg_to_volume_mono(volume, aleft);
231 arg_to_volume_mono(volume >> 8, aright);
235 static int ret_vol_mono(int left)
237 return ((left << 8) | left);
241 static int ret_vol_stereo(int left, int right)
243 return ((right << 8) | left);
247 static int call_ad_mixer(opl3sa2_mixerdata *devc, unsigned int cmd, caddr_t arg)
249 if(devc->ad_mixer_dev != -1)
250 return mixer_devs[devc->ad_mixer_dev]->ioctl(devc->ad_mixer_dev,
251 cmd,
252 arg);
253 else
254 return -EINVAL;
258 static int opl3sa2_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
260 int cmdf = cmd & 0xff;
262 opl3sa2_mixerdata* devc = (opl3sa2_mixerdata*) mixer_devs[dev]->devc;
264 switch(cmdf)
266 case SOUND_MIXER_VOLUME:
267 case SOUND_MIXER_MIC:
268 case SOUND_MIXER_BASS:
269 case SOUND_MIXER_TREBLE:
270 case SOUND_MIXER_DEVMASK:
271 case SOUND_MIXER_STEREODEVS:
272 case SOUND_MIXER_RECMASK:
273 case SOUND_MIXER_CAPS:
274 case SOUND_MIXER_RECSRC:
275 break;
277 default:
278 return call_ad_mixer(devc, cmd, arg);
281 if(((cmd >> 8) & 0xff) != 'M')
282 return -EINVAL;
284 if(_SIOC_DIR (cmd) & _SIOC_WRITE)
286 switch (cmdf)
288 case SOUND_MIXER_RECSRC:
289 if(devc->ad_mixer_dev != -1)
290 return call_ad_mixer(devc, cmd, arg);
291 else
293 if(*(int*)arg != 0)
294 return -EINVAL;
295 return 0;
298 case SOUND_MIXER_VOLUME:
299 arg_to_volume_stereo(*(unsigned int*)arg,
300 &devc->volume_l,
301 &devc->volume_r);
302 opl3sa2_set_volume(devc, devc->volume_l,
303 devc->volume_r);
304 *(int*)arg = ret_vol_stereo(devc->volume_l,
305 devc->volume_r);
306 return 0;
308 case SOUND_MIXER_MIC:
309 arg_to_volume_mono(*(unsigned int*)arg,
310 &devc->mic);
311 opl3sa2_set_mic(devc, devc->mic);
312 *(int*)arg = ret_vol_mono(devc->mic);
313 return 0;
315 case SOUND_MIXER_BASS:
316 if(chipset != CHIPSET_OPL3SA2)
318 arg_to_volume_mono(*(unsigned int*)arg,
319 &devc->bass);
320 opl3sa3_set_bass(devc, devc->bass);
321 *(int*)arg = ret_vol_mono(devc->bass);
322 return 0;
324 return -EINVAL;
326 case SOUND_MIXER_TREBLE:
327 if(chipset != CHIPSET_OPL3SA2)
329 arg_to_volume_mono(*(unsigned int *)arg,
330 &devc->treble);
331 opl3sa3_set_treble(devc, devc->treble);
332 *(int*)arg = ret_vol_mono(devc->treble);
333 return 0;
335 return -EINVAL;
337 default:
338 return -EINVAL;
341 else
344 * Return parameters
346 switch (cmdf)
348 case SOUND_MIXER_DEVMASK:
349 if(call_ad_mixer(devc, cmd, arg) == -EINVAL)
350 *(int*)arg = 0; /* no mixer devices */
352 *(int*)arg |= (SOUND_MASK_VOLUME | SOUND_MASK_MIC);
354 /* OPL3-SA2 has no bass and treble mixers */
355 if(chipset != CHIPSET_OPL3SA2)
356 *(int*)arg |= (SOUND_MASK_BASS |
357 SOUND_MASK_TREBLE);
358 return 0;
360 case SOUND_MIXER_STEREODEVS:
361 if(call_ad_mixer(devc, cmd, arg) == -EINVAL)
362 *(int*)arg = 0; /* no stereo devices */
363 *(int*)arg |= SOUND_MASK_VOLUME;
364 return 0;
366 case SOUND_MIXER_RECMASK:
367 if(devc->ad_mixer_dev != -1)
369 return call_ad_mixer(devc, cmd, arg);
371 else
373 /* No recording devices */
374 return (*(int*)arg = 0);
377 case SOUND_MIXER_CAPS:
378 if(devc->ad_mixer_dev != -1)
380 return call_ad_mixer(devc, cmd, arg);
382 else
384 *(int*)arg = SOUND_CAP_EXCL_INPUT;
385 return 0;
388 case SOUND_MIXER_RECSRC:
389 if(devc->ad_mixer_dev != -1)
391 return call_ad_mixer(devc, cmd, arg);
393 else
395 /* No recording source */
396 return (*(int*)arg = 0);
399 case SOUND_MIXER_VOLUME:
400 *(int*)arg = ret_vol_stereo(devc->volume_l,
401 devc->volume_r);
402 return 0;
404 case SOUND_MIXER_MIC:
405 *(int*)arg = ret_vol_mono(devc->mic);
406 return 0;
408 case SOUND_MIXER_BASS:
409 if(chipset != CHIPSET_OPL3SA2)
411 *(int*)arg = ret_vol_mono(devc->bass);
412 return 0;
414 else
416 return -EINVAL;
419 case SOUND_MIXER_TREBLE:
420 if(chipset != CHIPSET_OPL3SA2)
422 *(int*)arg = ret_vol_mono(devc->treble);
423 return 0;
425 else
427 return -EINVAL;
430 default:
431 return -EINVAL;
437 static struct mixer_operations opl3sa2_mixer_operations =
439 "Yamaha",
441 opl3sa2_mixer_ioctl
444 /* End of mixer-related stuff */
447 static inline int __init probe_opl3sa2_mpu(struct address_info *hw_config)
449 return probe_mpu401(hw_config);
453 static inline void __init attach_opl3sa2_mpu(struct address_info *hw_config)
455 attach_mpu401(hw_config);
459 static inline void __exit unload_opl3sa2_mpu(struct address_info *hw_config)
461 unload_mpu401(hw_config);
465 static inline int __init probe_opl3sa2_mss(struct address_info *hw_config)
467 return probe_ms_sound(hw_config);
471 static void __init attach_opl3sa2_mss(struct address_info *hw_config)
473 char mixer_name[64];
475 /* Create pretty names for mixer stuff */
476 strncpy(mixer_name, chipset_name, 16);
477 strncat(mixer_name, " and AD1848 (through MSS)", 64);
479 strncpy(opl3sa2_mixer_operations.name, chipset_name, 16);
480 strncat(opl3sa2_mixer_operations.name, "-AD1848", 64);
482 /* Install master mixer */
483 devc->ad_mixer_dev = -1;
484 if((opl3sa2_mixer = sound_install_mixer(MIXER_DRIVER_VERSION,
485 mixer_name,
486 &opl3sa2_mixer_operations,
487 sizeof(struct mixer_operations),
488 devc)) < 0)
490 printk(KERN_ERR "Could not install %s master mixer\n", chipset_name);
491 return;
494 opl3sa2_mixer_reset(devc);
496 attach_ms_sound(hw_config); /* Slot 0 */
497 if(hw_config->slots[0] != -1)
499 /* Did the MSS driver install? */
500 if(num_mixers == (opl3sa2_mixer + 2))
502 /* The MSS mixer is installed */
503 devc->ad_mixer_dev = audio_devs[hw_config->slots[0]]->mixer_dev;
505 /* Reroute mixers appropiately */
506 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
507 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
508 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
514 static inline void __exit unload_opl3sa2_mss(struct address_info *hw_config)
516 unload_ms_sound(hw_config);
520 static int __init probe_opl3sa2(struct address_info *hw_config)
522 unsigned char version = 0;
523 char tag;
526 * Verify that the I/O port range is free.
528 if(check_region(hw_config->io_base, 2))
530 printk(KERN_ERR
531 "%s: Control I/O port 0x%03x not free\n",
532 __FILE__,
533 hw_config->io_base);
534 return 0;
538 * Determine chipset type (SA2, SA3, or SAx)
542 * Look at chipset version in lower 3 bits of index 0x0A, miscellaneous
544 opl3sa2_read(hw_config->io_base,
545 OPL3SA2_MISC,
546 (unsigned char*) &version);
547 version &= 0x07;
549 /* Match version number to appropiate chipset */
550 if(version & CHIPSET_OPL3SAX)
552 chipset = CHIPSET_OPL3SAX;
553 tag = 'x';
554 printk(KERN_INFO "Found OPL3-SAx (YMF719)\n");
556 else
558 if(version & CHIPSET_OPL3SA3)
560 chipset = CHIPSET_OPL3SA3;
561 tag = '3';
562 printk(KERN_INFO "Found OPL3-SA3 (YMF715)\n");
564 else
566 if(version & CHIPSET_OPL3SA2)
568 chipset = CHIPSET_OPL3SA2;
569 tag = '2';
570 printk(KERN_INFO "Found OPL3-SA2 (YMF711)\n");
572 else
574 chipset = CHIPSET_UNKNOWN;
575 tag = '?';
576 printk(KERN_ERR
577 "Unknown Yamaha audio controller version\n");
578 printk(KERN_INFO
579 "%s: chipset version = %x\n",
580 __FILE__,
581 version);
586 if(chipset != CHIPSET_UNKNOWN)
588 /* Generate a pretty name */
589 sprintf(chipset_name, "OPL3-SA%c", tag);
590 return 1;
592 return 0;
596 static void __init attach_opl3sa2(struct address_info *hw_config)
598 request_region(hw_config->io_base, 2, chipset_name);
600 devc->cfg_port = hw_config->io_base;
604 static void __exit unload_opl3sa2(struct address_info *hw_config)
606 /* Release control ports */
607 release_region(hw_config->io_base, 2);
609 /* Unload mixer */
610 if(opl3sa2_mixer >= 0)
611 sound_unload_mixerdev(opl3sa2_mixer);
614 static struct address_info cfg;
615 static struct address_info cfg2;
616 static struct address_info cfg_mpu;
618 static int __initdata io = -1;
619 static int __initdata mss_io = -1;
620 static int __initdata mpu_io = -1;
621 static int __initdata irq = -1;
622 static int __initdata dma = -1;
623 static int __initdata dma2 = -1;
625 MODULE_PARM(io, "i");
626 MODULE_PARM_DESC(io, "Set i/o base of OPL3-SA2 or SA3 card (usually 0x370)");
628 MODULE_PARM(mss_io, "i");
629 MODULE_PARM_DESC(mss_io, "Set MSS (audio) I/O base (0x530, 0xE80, or other. Address must end in 0 or 4 and must be from 0x530 to 0xF48)");
631 MODULE_PARM(mpu_io, "i");
632 MODULE_PARM_DESC(mpu_io, "Set MIDI I/O base (0x330 or other. Address must be on 4 location boundaries and must be from 0x300 to 0x334)");
634 MODULE_PARM(irq, "i");
635 MODULE_PARM_DESC(mss_irq, "Set MSS (audio) IRQ (5, 7, 9, 10, 11, 12)");
637 MODULE_PARM(dma, "i");
638 MODULE_PARM_DESC(dma, "Set MSS (audio) first DMA channel (0, 1, 3)");
640 MODULE_PARM(dma2, "i");
641 MODULE_PARM_DESC(dma2, "Set MSS (audio) second DMA channel (0, 1, 3)");
643 MODULE_DESCRIPTION("Module for OPL3-SA2 and SA3 sound cards (uses AD1848 MSS driver).");
644 MODULE_AUTHOR("Scott Murray <scottm@interlog.com>");
647 * Install a OPL3SA2 based card.
649 * Need to have ad1848 and mpu401 loaded ready.
651 static int __init init_opl3sa2(void)
653 int i;
655 /* Our own config: */
656 cfg.io_base = io;
657 cfg.irq = irq;
658 cfg.dma = dma;
659 cfg.dma2 = dma2;
661 /* The MSS config: */
662 cfg2.io_base = mss_io;
663 cfg2.irq = irq;
664 cfg2.dma = dma;
665 cfg2.dma2 = dma2;
666 cfg2.card_subtype = 1; /* No IRQ or DMA setup */
668 cfg_mpu.io_base = mpu_io;
669 cfg_mpu.irq = irq;
670 cfg_mpu.dma = dma;
671 cfg_mpu.always_detect = 1; /* It's there, so use shared IRQs */
673 if(cfg.io_base == -1 || cfg.irq == -1 || cfg.dma == -1 || cfg.dma2 == -1 || cfg2.io_base == -1) {
674 printk(KERN_ERR "opl3sa2: io, mss_io, irq, dma, and dma2 must be set.\n");
675 return -EINVAL;
678 /* Call me paranoid: */
679 for(i = 0; i < 6; i++)
681 cfg.slots[i] = cfg2.slots[i] = cfg_mpu.slots[i] = -1;
684 if(probe_opl3sa2(&cfg) == 0)
686 return -ENODEV;
689 if(probe_opl3sa2_mss(&cfg2) == 0)
691 return -ENODEV;
694 attach_opl3sa2(&cfg);
695 attach_opl3sa2_mss(&cfg2);
697 if(cfg_mpu.io_base != -1) {
698 if(probe_opl3sa2_mpu(&cfg_mpu)) {
699 attach_opl3sa2_mpu(&cfg_mpu);
702 SOUND_LOCK;
703 return 0;
707 static void __exit cleanup_opl3sa2(void)
709 if(cfg_mpu.slots[1] != -1) {
710 unload_opl3sa2_mpu(&cfg_mpu);
712 unload_opl3sa2_mss(&cfg2);
713 unload_opl3sa2(&cfg);
714 SOUND_LOCK_END;
717 module_init(init_opl3sa2);
718 module_exit(cleanup_opl3sa2);
720 #ifndef MODULE
721 static int __init setup_opl3sa2(char *str)
723 /* io, irq, dma, dma2 */
724 int ints[7];
726 str = get_options(str, ARRAY_SIZE(ints), ints);
728 io = ints[1];
729 irq = ints[2];
730 dma = ints[3];
731 dma2 = ints[4];
732 mss_io = ints[5];
733 mpu_io = ints[6];
735 return 1;
738 __setup("opl3sa2=", setup_opl3sa2);
739 #endif