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
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
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.
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"
48 /* Useful control port indexes: */
49 #define OPL3SA2_MASTER_LEFT 0x07
50 #define OPL3SA2_MASTER_RIGHT 0x08
51 #define OPL3SA2_MIC 0x09
52 #define OPL3SA2_MISC 0x0A
54 #define OPL3SA3_WIDE 0x14
55 #define OPL3SA3_BASS 0x15
56 #define OPL3SA3_TREBLE 0x16
58 /* Useful constants: */
59 #define DEFAULT_VOLUME 50
60 #define DEFAULT_MIC 50
61 #define DEFAULT_TIMBRE 0
63 #define CHIPSET_UNKNOWN -1
66 * These are used both as masks against what the card returns,
69 #define CHIPSET_OPL3SA2 1
70 #define CHIPSET_OPL3SA3 2
71 #define CHIPSET_OPL3SAX 4
74 /* What's my version? */
75 static int chipset = CHIPSET_UNKNOWN;
77 /* Oh well, let's just cache the name */
78 static char chipset_name[16];
80 /* Where's my mixer */
81 static int opl3sa2_mixer = -1;
83 /* Bag o' mixer data */
84 typedef struct opl3sa2_mixerdata {
85 unsigned short cfg_port;
86 unsigned short padding;
88 unsigned int volume_l;
89 unsigned int volume_r;
95 #ifdef CONFIG_OPL3SA2_CTRL_BASE
96 /* Set control port if compiled into the kernel */
97 static opl3sa2_mixerdata opl3sa2_data = { CONFIG_OPL3SA2_CTRL_BASE, };
99 static opl3sa2_mixerdata opl3sa2_data;
102 static opl3sa2_mixerdata *devc = &opl3sa2_data;
105 /* Standard read and write functions */
107 static void opl3sa2_write(unsigned short port,
112 outb(data, port + 1);
116 static void opl3sa2_read(unsigned short port,
121 *data = inb(port + 1);
125 /* All of the mixer functions... */
127 static void opl3sa2_set_volume(opl3sa2_mixerdata *devc, int left, int right)
129 static unsigned char scale[101] = {
130 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
131 0x0e, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
132 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
133 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
134 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
135 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
136 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05,
137 0x05, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
138 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02,
139 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
146 /* If level is zero, turn on mute */
150 opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol);
154 /* If level is zero, turn on mute */
158 opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_RIGHT, vol);
162 static void opl3sa2_set_mic(opl3sa2_mixerdata *devc, int level)
164 unsigned char vol = 0x1F;
166 if((level >= 0) && (level <= 100))
167 vol = 0x1F - (unsigned char) (0x1F * level / 100L);
169 /* If level is zero, turn on mute */
173 opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol);
177 static void opl3sa3_set_bass(opl3sa2_mixerdata *devc, int level)
181 bass = level ? ((unsigned char) (0x07 * level / 100L)) : 0;
184 opl3sa2_write(devc->cfg_port, OPL3SA3_BASS, bass);
188 static void opl3sa3_set_treble(opl3sa2_mixerdata *devc, int level)
190 unsigned char treble;
192 treble = level ? ((unsigned char) (0x07 * level / 100L)) : 0;
193 treble |= (treble << 4);
195 opl3sa2_write(devc->cfg_port, OPL3SA3_TREBLE, treble);
199 static void opl3sa2_mixer_reset(opl3sa2_mixerdata *devc)
203 opl3sa2_set_volume(devc, DEFAULT_VOLUME, DEFAULT_VOLUME);
204 devc->volume_l = devc->volume_r = DEFAULT_VOLUME;
206 opl3sa2_set_mic(devc, DEFAULT_MIC);
207 devc->mic = DEFAULT_MIC;
209 opl3sa3_set_bass(devc, DEFAULT_TIMBRE);
210 opl3sa3_set_treble(devc, DEFAULT_TIMBRE);
211 devc->bass = devc->treble = DEFAULT_TIMBRE;
216 static void arg_to_volume_mono(unsigned int volume, int *aleft)
220 left = volume & 0x00ff;
227 static void arg_to_volume_stereo(unsigned int volume, int *aleft, int *aright)
229 arg_to_volume_mono(volume, aleft);
230 arg_to_volume_mono(volume >> 8, aright);
234 static int ret_vol_mono(int left)
236 return ((left << 8) | left);
240 static int ret_vol_stereo(int left, int right)
242 return ((right << 8) | left);
246 static int call_ad_mixer(opl3sa2_mixerdata *devc, unsigned int cmd, caddr_t arg)
248 if(devc->ad_mixer_dev != -1)
249 return mixer_devs[devc->ad_mixer_dev]->ioctl(devc->ad_mixer_dev,
257 static int opl3sa2_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
259 int cmdf = cmd & 0xff;
261 opl3sa2_mixerdata* devc = (opl3sa2_mixerdata*) mixer_devs[dev]->devc;
265 case SOUND_MIXER_VOLUME:
266 case SOUND_MIXER_MIC:
267 case SOUND_MIXER_BASS:
268 case SOUND_MIXER_TREBLE:
269 case SOUND_MIXER_DEVMASK:
270 case SOUND_MIXER_STEREODEVS:
271 case SOUND_MIXER_RECMASK:
272 case SOUND_MIXER_CAPS:
273 case SOUND_MIXER_RECSRC:
277 return call_ad_mixer(devc, cmd, arg);
280 if(((cmd >> 8) & 0xff) != 'M')
283 if(_SIOC_DIR (cmd) & _SIOC_WRITE)
287 case SOUND_MIXER_RECSRC:
288 if(devc->ad_mixer_dev != -1)
289 return call_ad_mixer(devc, cmd, arg);
297 case SOUND_MIXER_VOLUME:
298 arg_to_volume_stereo(*(unsigned int*)arg,
301 opl3sa2_set_volume(devc, devc->volume_l,
303 *(int*)arg = ret_vol_stereo(devc->volume_l,
307 case SOUND_MIXER_MIC:
308 arg_to_volume_mono(*(unsigned int*)arg,
310 opl3sa2_set_mic(devc, devc->mic);
311 *(int*)arg = ret_vol_mono(devc->mic);
314 case SOUND_MIXER_BASS:
315 if(chipset != CHIPSET_OPL3SA2)
317 arg_to_volume_mono(*(unsigned int*)arg,
319 opl3sa3_set_bass(devc, devc->bass);
320 *(int*)arg = ret_vol_mono(devc->bass);
325 case SOUND_MIXER_TREBLE:
326 if(chipset != CHIPSET_OPL3SA2)
328 arg_to_volume_mono(*(unsigned int *)arg,
330 opl3sa3_set_treble(devc, devc->treble);
331 *(int*)arg = ret_vol_mono(devc->treble);
347 case SOUND_MIXER_DEVMASK:
348 if(call_ad_mixer(devc, cmd, arg) == -EINVAL)
349 *(int*)arg = 0; /* no mixer devices */
351 *(int*)arg |= (SOUND_MASK_VOLUME | SOUND_MASK_MIC);
353 /* OPL3-SA2 has no bass and treble mixers */
354 if(chipset != CHIPSET_OPL3SA2)
355 *(int*)arg |= (SOUND_MASK_BASS |
359 case SOUND_MIXER_STEREODEVS:
360 if(call_ad_mixer(devc, cmd, arg) == -EINVAL)
361 *(int*)arg = 0; /* no stereo devices */
362 *(int*)arg |= SOUND_MASK_VOLUME;
365 case SOUND_MIXER_RECMASK:
366 if(devc->ad_mixer_dev != -1)
368 return call_ad_mixer(devc, cmd, arg);
372 /* No recording devices */
373 return (*(int*)arg = 0);
376 case SOUND_MIXER_CAPS:
377 if(devc->ad_mixer_dev != -1)
379 return call_ad_mixer(devc, cmd, arg);
383 *(int*)arg = SOUND_CAP_EXCL_INPUT;
387 case SOUND_MIXER_RECSRC:
388 if(devc->ad_mixer_dev != -1)
390 return call_ad_mixer(devc, cmd, arg);
394 /* No recording source */
395 return (*(int*)arg = 0);
398 case SOUND_MIXER_VOLUME:
399 *(int*)arg = ret_vol_stereo(devc->volume_l,
403 case SOUND_MIXER_MIC:
404 *(int*)arg = ret_vol_mono(devc->mic);
407 case SOUND_MIXER_BASS:
408 if(chipset != CHIPSET_OPL3SA2)
410 *(int*)arg = ret_vol_mono(devc->bass);
418 case SOUND_MIXER_TREBLE:
419 if(chipset != CHIPSET_OPL3SA2)
421 *(int*)arg = ret_vol_mono(devc->treble);
436 static struct mixer_operations opl3sa2_mixer_operations =
441 ioctl: 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, THIS_MODULE);
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)
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,
486 &opl3sa2_mixer_operations,
487 sizeof(struct mixer_operations),
490 printk(KERN_ERR "Could not install %s master mixer\n", chipset_name);
494 opl3sa2_mixer_reset(devc);
496 attach_ms_sound(hw_config, THIS_MODULE); /* 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;
526 * Verify that the I/O port range is free.
528 if(check_region(hw_config->io_base, 2))
531 "%s: Control I/O port 0x%03x not free\n",
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,
546 (unsigned char*) &version);
549 /* Match version number to appropiate chipset */
550 if(version & CHIPSET_OPL3SAX)
552 chipset = CHIPSET_OPL3SAX;
554 printk(KERN_INFO "Found OPL3-SAx (YMF719)\n");
558 if(version & CHIPSET_OPL3SA3)
560 chipset = CHIPSET_OPL3SA3;
562 printk(KERN_INFO "Found OPL3-SA3 (YMF715)\n");
566 if(version & CHIPSET_OPL3SA2)
568 chipset = CHIPSET_OPL3SA2;
570 printk(KERN_INFO "Found OPL3-SA2 (YMF711)\n");
574 chipset = CHIPSET_UNKNOWN;
577 "Unknown Yamaha audio controller version\n");
579 "%s: chipset version = %x\n",
586 if(chipset != CHIPSET_UNKNOWN)
588 /* Generate a pretty name */
589 sprintf(chipset_name, "OPL3-SA%c", tag);
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);
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)
655 /* Our own config: */
661 /* The MSS config: */
662 cfg2.io_base = mss_io;
666 cfg2.card_subtype = 1; /* No IRQ or DMA setup */
668 cfg_mpu.io_base = mpu_io;
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");
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)
689 if(probe_opl3sa2_mss(&cfg2) == 0)
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);
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);
716 module_init(init_opl3sa2);
717 module_exit(cleanup_opl3sa2);
720 static int __init setup_opl3sa2(char *str)
722 /* io, irq, dma, dma2 */
725 str = get_options(str, ARRAY_SIZE(ints), ints);
737 __setup("opl3sa2=", setup_opl3sa2);