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"
44 #include "soundmodule.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,
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
;
89 unsigned int volume_l
;
90 unsigned int volume_r
;
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
, };
100 static opl3sa2_mixerdata opl3sa2_data
;
103 static opl3sa2_mixerdata
*devc
= &opl3sa2_data
;
106 /* Standard read and write functions */
108 static void opl3sa2_write(unsigned short port
,
113 outb(data
, port
+ 1);
117 static void opl3sa2_read(unsigned short 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,
147 /* If level is zero, turn on mute */
151 opl3sa2_write(devc
->cfg_port
, OPL3SA2_MASTER_LEFT
, vol
);
155 /* If level is zero, turn on mute */
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 */
174 opl3sa2_write(devc
->cfg_port
, OPL3SA2_MIC
, vol
);
178 static void opl3sa3_set_bass(opl3sa2_mixerdata
*devc
, int level
)
182 bass
= level
? ((unsigned char) (0x07 * level
/ 100L)) : 0;
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
)
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
)
221 left
= volume
& 0x00ff;
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
,
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
;
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
:
278 return call_ad_mixer(devc
, cmd
, arg
);
281 if(((cmd
>> 8) & 0xff) != 'M')
284 if(_SIOC_DIR (cmd
) & _SIOC_WRITE
)
288 case SOUND_MIXER_RECSRC
:
289 if(devc
->ad_mixer_dev
!= -1)
290 return call_ad_mixer(devc
, cmd
, arg
);
298 case SOUND_MIXER_VOLUME
:
299 arg_to_volume_stereo(*(unsigned int*)arg
,
302 opl3sa2_set_volume(devc
, devc
->volume_l
,
304 *(int*)arg
= ret_vol_stereo(devc
->volume_l
,
308 case SOUND_MIXER_MIC
:
309 arg_to_volume_mono(*(unsigned int*)arg
,
311 opl3sa2_set_mic(devc
, devc
->mic
);
312 *(int*)arg
= ret_vol_mono(devc
->mic
);
315 case SOUND_MIXER_BASS
:
316 if(chipset
!= CHIPSET_OPL3SA2
)
318 arg_to_volume_mono(*(unsigned int*)arg
,
320 opl3sa3_set_bass(devc
, devc
->bass
);
321 *(int*)arg
= ret_vol_mono(devc
->bass
);
326 case SOUND_MIXER_TREBLE
:
327 if(chipset
!= CHIPSET_OPL3SA2
)
329 arg_to_volume_mono(*(unsigned int *)arg
,
331 opl3sa3_set_treble(devc
, devc
->treble
);
332 *(int*)arg
= ret_vol_mono(devc
->treble
);
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
|
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
;
366 case SOUND_MIXER_RECMASK
:
367 if(devc
->ad_mixer_dev
!= -1)
369 return call_ad_mixer(devc
, cmd
, arg
);
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
);
384 *(int*)arg
= SOUND_CAP_EXCL_INPUT
;
388 case SOUND_MIXER_RECSRC
:
389 if(devc
->ad_mixer_dev
!= -1)
391 return call_ad_mixer(devc
, cmd
, arg
);
395 /* No recording source */
396 return (*(int*)arg
= 0);
399 case SOUND_MIXER_VOLUME
:
400 *(int*)arg
= ret_vol_stereo(devc
->volume_l
,
404 case SOUND_MIXER_MIC
:
405 *(int*)arg
= ret_vol_mono(devc
->mic
);
408 case SOUND_MIXER_BASS
:
409 if(chipset
!= CHIPSET_OPL3SA2
)
411 *(int*)arg
= ret_vol_mono(devc
->bass
);
419 case SOUND_MIXER_TREBLE
:
420 if(chipset
!= CHIPSET_OPL3SA2
)
422 *(int*)arg
= ret_vol_mono(devc
->treble
);
437 static struct mixer_operations opl3sa2_mixer_operations
=
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
)
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
); /* 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
);
717 module_init(init_opl3sa2
);
718 module_exit(cleanup_opl3sa2
);
721 static int __init
setup_opl3sa2(char *str
)
723 /* io, irq, dma, dma2 */
726 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
738 __setup("opl3sa2=", setup_opl3sa2
);