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)
38 #include <linux/config.h>
39 #include <linux/module.h>
41 #include "sound_config.h"
42 #include "soundmodule.h"
44 /* Useful control port indexes: */
45 #define OPL3SA2_MASTER_LEFT 0x07
46 #define OPL3SA2_MASTER_RIGHT 0x08
47 #define OPL3SA2_MIC 0x09
48 #define OPL3SA2_MISC 0x0A
50 #define OPL3SA3_WIDE 0x14
51 #define OPL3SA3_BASS 0x15
52 #define OPL3SA3_TREBLE 0x16
54 /* Useful constants: */
55 #define DEFAULT_VOLUME 50
56 #define DEFAULT_MIC 50
57 #define DEFAULT_TIMBRE 0
59 #define CHIPSET_UNKNOWN -1
62 * These are used both as masks against what the card returns,
65 #define CHIPSET_OPL3SA2 1
66 #define CHIPSET_OPL3SA3 2
67 #define CHIPSET_OPL3SAX 4
72 /* What's my version? */
73 static int chipset
= CHIPSET_UNKNOWN
;
75 /* Oh well, let's just cache the name */
76 static char chipset_name
[16];
78 /* Where's my mixer */
79 static int opl3sa2_mixer
= -1;
81 /* Bag o' mixer data */
82 typedef struct opl3sa2_mixerdata
{
83 unsigned short cfg_port
;
84 unsigned short padding
;
86 unsigned int volume_l
;
87 unsigned int volume_r
;
93 #ifdef CONFIG_OPL3SA2_CTRL_BASE
94 /* Set control port if compiled into the kernel */
95 static opl3sa2_mixerdata opl3sa2_data
= { CONFIG_OPL3SA2_CTRL_BASE
, };
97 static opl3sa2_mixerdata opl3sa2_data
;
100 static opl3sa2_mixerdata
*devc
= &opl3sa2_data
;
103 /* Standard read and write functions */
105 static void opl3sa2_write(unsigned short port
,
110 outb(data
, port
+ 1);
114 static void opl3sa2_read(unsigned short port
,
119 *data
= inb(port
+ 1);
123 /* All of the mixer functions... */
125 static void opl3sa2_set_volume(opl3sa2_mixerdata
*devc
, int left
, int right
)
127 static unsigned char scale
[101] = {
128 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
129 0x0e, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
130 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
131 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
132 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
133 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
134 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05,
135 0x05, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
136 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02,
137 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
144 /* If level is zero, turn on mute */
148 opl3sa2_write(devc
->cfg_port
, OPL3SA2_MASTER_LEFT
, vol
);
152 /* If level is zero, turn on mute */
156 opl3sa2_write(devc
->cfg_port
, OPL3SA2_MASTER_RIGHT
, vol
);
160 static void opl3sa2_set_mic(opl3sa2_mixerdata
*devc
, int level
)
162 unsigned char vol
= 0x1F;
164 if((level
>= 0) && (level
<= 100))
165 vol
= 0x1F - (unsigned char) (0x1F * level
/ 100L);
167 /* If level is zero, turn on mute */
171 opl3sa2_write(devc
->cfg_port
, OPL3SA2_MIC
, vol
);
175 static void opl3sa3_set_bass(opl3sa2_mixerdata
*devc
, int level
)
179 bass
= level
? ((unsigned char) (0x07 * level
/ 100L)) : 0;
182 opl3sa2_write(devc
->cfg_port
, OPL3SA3_BASS
, bass
);
186 static void opl3sa3_set_treble(opl3sa2_mixerdata
*devc
, int level
)
188 unsigned char treble
;
190 treble
= level
? ((unsigned char) (0x07 * level
/ 100L)) : 0;
191 treble
|= (treble
<< 4);
193 opl3sa2_write(devc
->cfg_port
, OPL3SA3_TREBLE
, treble
);
197 static void opl3sa2_mixer_reset(opl3sa2_mixerdata
*devc
)
201 opl3sa2_set_volume(devc
, DEFAULT_VOLUME
, DEFAULT_VOLUME
);
202 devc
->volume_l
= devc
->volume_r
= DEFAULT_VOLUME
;
204 opl3sa2_set_mic(devc
, DEFAULT_MIC
);
205 devc
->mic
= DEFAULT_MIC
;
207 opl3sa3_set_bass(devc
, DEFAULT_TIMBRE
);
208 opl3sa3_set_treble(devc
, DEFAULT_TIMBRE
);
209 devc
->bass
= devc
->treble
= DEFAULT_TIMBRE
;
214 static void arg_to_volume_mono(unsigned int volume
, int *aleft
)
218 left
= volume
& 0x00ff;
225 static void arg_to_volume_stereo(unsigned int volume
, int *aleft
, int *aright
)
227 arg_to_volume_mono(volume
, aleft
);
228 arg_to_volume_mono(volume
>> 8, aright
);
232 static int ret_vol_mono(int left
)
234 return ((left
<< 8) | left
);
238 static int ret_vol_stereo(int left
, int right
)
240 return ((right
<< 8) | left
);
244 static int call_ad_mixer(opl3sa2_mixerdata
*devc
, unsigned int cmd
, caddr_t arg
)
246 if(devc
->ad_mixer_dev
!= -1)
247 return mixer_devs
[devc
->ad_mixer_dev
]->ioctl(devc
->ad_mixer_dev
,
255 static int opl3sa2_mixer_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
257 int cmdf
= cmd
& 0xff;
259 opl3sa2_mixerdata
* devc
= (opl3sa2_mixerdata
*) mixer_devs
[dev
]->devc
;
263 case SOUND_MIXER_VOLUME
:
264 case SOUND_MIXER_MIC
:
265 case SOUND_MIXER_BASS
:
266 case SOUND_MIXER_TREBLE
:
267 case SOUND_MIXER_DEVMASK
:
268 case SOUND_MIXER_STEREODEVS
:
269 case SOUND_MIXER_RECMASK
:
270 case SOUND_MIXER_CAPS
:
271 case SOUND_MIXER_RECSRC
:
275 return call_ad_mixer(devc
, cmd
, arg
);
278 if(((cmd
>> 8) & 0xff) != 'M')
281 if(_SIOC_DIR (cmd
) & _SIOC_WRITE
)
285 case SOUND_MIXER_RECSRC
:
286 if(devc
->ad_mixer_dev
!= -1)
287 return call_ad_mixer(devc
, cmd
, arg
);
295 case SOUND_MIXER_VOLUME
:
296 arg_to_volume_stereo(*(unsigned int*)arg
,
299 opl3sa2_set_volume(devc
, devc
->volume_l
,
301 *(int*)arg
= ret_vol_stereo(devc
->volume_l
,
305 case SOUND_MIXER_MIC
:
306 arg_to_volume_mono(*(unsigned int*)arg
,
308 opl3sa2_set_mic(devc
, devc
->mic
);
309 *(int*)arg
= ret_vol_mono(devc
->mic
);
312 case SOUND_MIXER_BASS
:
313 if(chipset
!= CHIPSET_OPL3SA2
)
315 arg_to_volume_mono(*(unsigned int*)arg
,
317 opl3sa3_set_bass(devc
, devc
->bass
);
318 *(int*)arg
= ret_vol_mono(devc
->bass
);
323 case SOUND_MIXER_TREBLE
:
324 if(chipset
!= CHIPSET_OPL3SA2
)
326 arg_to_volume_mono(*(unsigned int *)arg
,
328 opl3sa3_set_treble(devc
, devc
->treble
);
329 *(int*)arg
= ret_vol_mono(devc
->treble
);
345 case SOUND_MIXER_DEVMASK
:
346 if(call_ad_mixer(devc
, cmd
, arg
) == -EINVAL
)
347 *(int*)arg
= 0; /* no mixer devices */
349 *(int*)arg
|= (SOUND_MASK_VOLUME
| SOUND_MASK_MIC
);
351 /* OPL3-SA2 has no bass and treble mixers */
352 if(chipset
!= CHIPSET_OPL3SA2
)
353 *(int*)arg
|= (SOUND_MASK_BASS
|
357 case SOUND_MIXER_STEREODEVS
:
358 if(call_ad_mixer(devc
, cmd
, arg
) == -EINVAL
)
359 *(int*)arg
= 0; /* no stereo devices */
360 *(int*)arg
|= SOUND_MASK_VOLUME
;
363 case SOUND_MIXER_RECMASK
:
364 if(devc
->ad_mixer_dev
!= -1)
366 return call_ad_mixer(devc
, cmd
, arg
);
370 /* No recording devices */
371 return (*(int*)arg
= 0);
374 case SOUND_MIXER_CAPS
:
375 if(devc
->ad_mixer_dev
!= -1)
377 return call_ad_mixer(devc
, cmd
, arg
);
381 *(int*)arg
= SOUND_CAP_EXCL_INPUT
;
385 case SOUND_MIXER_RECSRC
:
386 if(devc
->ad_mixer_dev
!= -1)
388 return call_ad_mixer(devc
, cmd
, arg
);
392 /* No recording source */
393 return (*(int*)arg
= 0);
396 case SOUND_MIXER_VOLUME
:
397 *(int*)arg
= ret_vol_stereo(devc
->volume_l
,
401 case SOUND_MIXER_MIC
:
402 *(int*)arg
= ret_vol_mono(devc
->mic
);
405 case SOUND_MIXER_BASS
:
406 if(chipset
!= CHIPSET_OPL3SA2
)
408 *(int*)arg
= ret_vol_mono(devc
->bass
);
416 case SOUND_MIXER_TREBLE
:
417 if(chipset
!= CHIPSET_OPL3SA2
)
419 *(int*)arg
= ret_vol_mono(devc
->treble
);
434 static struct mixer_operations opl3sa2_mixer_operations
=
441 /* End of mixer-related stuff */
444 int probe_opl3sa2_mpu(struct address_info
*hw_config
)
446 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
447 return probe_mpu401(hw_config
);
454 void attach_opl3sa2_mpu(struct address_info
*hw_config
)
456 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
457 attach_mpu401(hw_config
);
462 void unload_opl3sa2_mpu(struct address_info
*hw_config
)
464 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
465 unload_mpu401(hw_config
);
470 int probe_opl3sa2_mss(struct address_info
*hw_config
)
472 return probe_ms_sound(hw_config
);
476 void attach_opl3sa2_mss(struct address_info
*hw_config
)
480 /* Create pretty names for mixer stuff */
481 strncpy(mixer_name
, chipset_name
, 16);
482 strncat(mixer_name
, " and AD1848 (through MSS)", 64);
484 strncpy(opl3sa2_mixer_operations
.name
, chipset_name
, 16);
485 strncat(opl3sa2_mixer_operations
.name
, "-AD1848", 64);
487 /* Install master mixer */
488 devc
->ad_mixer_dev
= -1;
489 if((opl3sa2_mixer
= sound_install_mixer(MIXER_DRIVER_VERSION
,
491 &opl3sa2_mixer_operations
,
492 sizeof(struct mixer_operations
),
495 printk(KERN_ERR
"Could not install %s master mixer\n", chipset_name
);
499 opl3sa2_mixer_reset(devc
);
501 attach_ms_sound(hw_config
); /* Slot 0 */
502 if(hw_config
->slots
[0] != -1)
504 /* Did the MSS driver install? */
505 if(num_mixers
== (opl3sa2_mixer
+ 2))
507 /* The MSS mixer is installed */
508 devc
->ad_mixer_dev
= audio_devs
[hw_config
->slots
[0]]->mixer_dev
;
510 /* Reroute mixers appropiately */
511 AD1848_REROUTE(SOUND_MIXER_LINE1
, SOUND_MIXER_CD
);
512 AD1848_REROUTE(SOUND_MIXER_LINE2
, SOUND_MIXER_SYNTH
);
513 AD1848_REROUTE(SOUND_MIXER_LINE3
, SOUND_MIXER_LINE
);
519 void unload_opl3sa2_mss(struct address_info
*hw_config
)
521 unload_ms_sound(hw_config
);
525 int probe_opl3sa2(struct address_info
*hw_config
)
527 unsigned char version
= 0;
531 * Verify that the I/O port range is free.
533 if(check_region(hw_config
->io_base
, 2))
536 "%s: Control I/O port 0x%03x not free\n",
543 * Determine chipset type (SA2, SA3, or SAx)
547 * Look at chipset version in lower 3 bits of index 0x0A, miscellaneous
549 opl3sa2_read(hw_config
->io_base
,
551 (unsigned char*) &version
);
554 /* Match version number to appropiate chipset */
555 if(version
& CHIPSET_OPL3SAX
)
557 chipset
= CHIPSET_OPL3SAX
;
559 printk(KERN_INFO
"Found OPL3-SAx (YMF719)\n");
563 if(version
& CHIPSET_OPL3SA3
)
565 chipset
= CHIPSET_OPL3SA3
;
567 printk(KERN_INFO
"Found OPL3-SA3 (YMF715)\n");
571 if(version
& CHIPSET_OPL3SA2
)
573 chipset
= CHIPSET_OPL3SA2
;
575 printk(KERN_INFO
"Found OPL3-SA2 (YMF711)\n");
579 chipset
= CHIPSET_UNKNOWN
;
582 "Unknown Yamaha audio controller version\n");
584 "%s: chipset version = %x\n",
591 if(chipset
!= CHIPSET_UNKNOWN
)
593 /* Generate a pretty name */
594 sprintf(chipset_name
, "OPL3-SA%c", tag
);
595 #if defined(CONFIG_OPL3SA2_MPU_BASE) && !defined(MODULE)
596 sound_getconf(SNDCARD_OPL3SA2_MPU
)->always_detect
= 1;
604 void attach_opl3sa2(struct address_info
*hw_config
)
606 request_region(hw_config
->io_base
, 2, chipset_name
);
608 devc
->cfg_port
= hw_config
->io_base
;
612 void unload_opl3sa2(struct address_info
*hw_config
)
614 /* Release control ports */
615 release_region(hw_config
->io_base
, 2);
618 if(opl3sa2_mixer
>= 0)
619 sound_unload_mixerdev(opl3sa2_mixer
);
632 MODULE_PARM(io
, "i");
633 MODULE_PARM_DESC(io
, "Set i/o base of OPL3-SA2 or SA3 card (usually 0x370)");
635 MODULE_PARM(mss_io
, "i");
636 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)");
638 MODULE_PARM(mpu_io
, "i");
639 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)");
641 MODULE_PARM(irq
, "i");
642 MODULE_PARM_DESC(mss_irq
, "Set MSS (audio) IRQ (5, 7, 9, 10, 11, 12)");
644 MODULE_PARM(dma
, "i");
645 MODULE_PARM_DESC(dma
, "Set MSS (audio) first DMA channel (0, 1, 3)");
647 MODULE_PARM(dma2
, "i");
648 MODULE_PARM_DESC(dma2
, "Set MSS (audio) second DMA channel (0, 1, 3)");
650 MODULE_DESCRIPTION("Module for OPL3-SA2 and SA3 sound cards (uses AD1848 MSS driver).");
651 MODULE_AUTHOR("Scott Murray <scottm@interlog.com>");
655 struct address_info cfg
;
656 struct address_info mss_cfg
;
657 struct address_info mpu_cfg
;
661 * Install a OPL3SA2 based card.
663 * Need to have ad1848 and mpu401 loaded ready.
665 int init_module(void)
669 if(io
== -1 || irq
== -1 || dma
== -1 || dma2
== -1 || mss_io
== -1)
672 "%s: io, mss_io, irq, dma, and dma2 must be set.\n",
677 /* Our own config: */
683 /* The MSS config: */
684 mss_cfg
.io_base
= mss_io
;
688 mss_cfg
.card_subtype
= 1; /* No IRQ or DMA setup */
690 /* Call me paranoid: */
691 for(i
= 0; i
< 6; i
++)
693 cfg
.slots
[i
] = mss_cfg
.slots
[i
] = mpu_cfg
.slots
[i
] = -1;
696 if(probe_opl3sa2(&cfg
) == 0)
701 if(probe_opl3sa2_mss(&mss_cfg
) == 0)
706 attach_opl3sa2(&cfg
);
707 attach_opl3sa2_mss(&mss_cfg
);
709 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
713 mpu_cfg
.io_base
= mpu_io
;
716 mpu_cfg
.always_detect
= 1; /* It's there, so use shared IRQs */
718 if(probe_opl3sa2_mpu(&mpu_cfg
))
720 attach_opl3sa2_mpu(&mpu_cfg
);
729 void cleanup_module(void)
731 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
732 if(mpu_cfg
.slots
[1] != -1)
734 unload_opl3sa2_mpu(&mpu_cfg
);
737 unload_opl3sa2_mss(&mss_cfg
);
738 unload_opl3sa2(&cfg
);
743 #endif /* CONFIG_OPL3SA2 */