Import 2.3.18pre1
[davej-history.git] / drivers / sound / opl3sa2.c
blob68c37778f841ed75a89cacee4ca3beb965b45151
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)
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,
63 * and as constants.
65 #define CHIPSET_OPL3SA2 1
66 #define CHIPSET_OPL3SA3 2
67 #define CHIPSET_OPL3SAX 4
70 #ifdef CONFIG_OPL3SA2
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;
85 int ad_mixer_dev;
86 unsigned int volume_l;
87 unsigned int volume_r;
88 unsigned int mic;
89 unsigned int bass;
90 unsigned int treble;
91 } opl3sa2_mixerdata;
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, };
96 #else
97 static opl3sa2_mixerdata opl3sa2_data;
98 #endif
100 static opl3sa2_mixerdata *devc = &opl3sa2_data;
103 /* Standard read and write functions */
105 static void opl3sa2_write(unsigned short port,
106 unsigned char index,
107 unsigned char data)
109 outb_p(index, port);
110 outb(data, port + 1);
114 static void opl3sa2_read(unsigned short port,
115 unsigned char index,
116 unsigned char* data)
118 outb_p(index, 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,
138 0x00
140 unsigned char vol;
142 vol = scale[left];
144 /* If level is zero, turn on mute */
145 if(!left)
146 vol |= 0x80;
148 opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol);
150 vol = scale[right];
152 /* If level is zero, turn on mute */
153 if(!right)
154 vol |= 0x80;
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 */
168 if(!level)
169 vol |= 0x80;
171 opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol);
175 static void opl3sa3_set_bass(opl3sa2_mixerdata *devc, int level)
177 unsigned char bass;
179 bass = level ? ((unsigned char) (0x07 * level / 100L)) : 0;
180 bass |= (bass << 4);
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)
199 if(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)
216 int left;
218 left = volume & 0x00ff;
219 if (left > 100)
220 left = 100;
221 *aleft = left;
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,
248 cmd,
249 arg);
250 else
251 return -EINVAL;
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;
261 switch(cmdf)
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:
272 break;
274 default:
275 return call_ad_mixer(devc, cmd, arg);
278 if(((cmd >> 8) & 0xff) != 'M')
279 return -EINVAL;
281 if(_SIOC_DIR (cmd) & _SIOC_WRITE)
283 switch (cmdf)
285 case SOUND_MIXER_RECSRC:
286 if(devc->ad_mixer_dev != -1)
287 return call_ad_mixer(devc, cmd, arg);
288 else
290 if(*(int*)arg != 0)
291 return -EINVAL;
292 return 0;
295 case SOUND_MIXER_VOLUME:
296 arg_to_volume_stereo(*(unsigned int*)arg,
297 &devc->volume_l,
298 &devc->volume_r);
299 opl3sa2_set_volume(devc, devc->volume_l,
300 devc->volume_r);
301 *(int*)arg = ret_vol_stereo(devc->volume_l,
302 devc->volume_r);
303 return 0;
305 case SOUND_MIXER_MIC:
306 arg_to_volume_mono(*(unsigned int*)arg,
307 &devc->mic);
308 opl3sa2_set_mic(devc, devc->mic);
309 *(int*)arg = ret_vol_mono(devc->mic);
310 return 0;
312 case SOUND_MIXER_BASS:
313 if(chipset != CHIPSET_OPL3SA2)
315 arg_to_volume_mono(*(unsigned int*)arg,
316 &devc->bass);
317 opl3sa3_set_bass(devc, devc->bass);
318 *(int*)arg = ret_vol_mono(devc->bass);
319 return 0;
321 return -EINVAL;
323 case SOUND_MIXER_TREBLE:
324 if(chipset != CHIPSET_OPL3SA2)
326 arg_to_volume_mono(*(unsigned int *)arg,
327 &devc->treble);
328 opl3sa3_set_treble(devc, devc->treble);
329 *(int*)arg = ret_vol_mono(devc->treble);
330 return 0;
332 return -EINVAL;
334 default:
335 return -EINVAL;
338 else
341 * Return parameters
343 switch (cmdf)
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 |
354 SOUND_MASK_TREBLE);
355 return 0;
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;
361 return 0;
363 case SOUND_MIXER_RECMASK:
364 if(devc->ad_mixer_dev != -1)
366 return call_ad_mixer(devc, cmd, arg);
368 else
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);
379 else
381 *(int*)arg = SOUND_CAP_EXCL_INPUT;
382 return 0;
385 case SOUND_MIXER_RECSRC:
386 if(devc->ad_mixer_dev != -1)
388 return call_ad_mixer(devc, cmd, arg);
390 else
392 /* No recording source */
393 return (*(int*)arg = 0);
396 case SOUND_MIXER_VOLUME:
397 *(int*)arg = ret_vol_stereo(devc->volume_l,
398 devc->volume_r);
399 return 0;
401 case SOUND_MIXER_MIC:
402 *(int*)arg = ret_vol_mono(devc->mic);
403 return 0;
405 case SOUND_MIXER_BASS:
406 if(chipset != CHIPSET_OPL3SA2)
408 *(int*)arg = ret_vol_mono(devc->bass);
409 return 0;
411 else
413 return -EINVAL;
416 case SOUND_MIXER_TREBLE:
417 if(chipset != CHIPSET_OPL3SA2)
419 *(int*)arg = ret_vol_mono(devc->treble);
420 return 0;
422 else
424 return -EINVAL;
427 default:
428 return -EINVAL;
434 static struct mixer_operations opl3sa2_mixer_operations =
436 "Yamaha",
438 opl3sa2_mixer_ioctl
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);
448 #else
449 return 0;
450 #endif
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);
458 #endif
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);
466 #endif
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)
478 char mixer_name[64];
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,
490 mixer_name,
491 &opl3sa2_mixer_operations,
492 sizeof(struct mixer_operations),
493 devc)) < 0)
495 printk(KERN_ERR "Could not install %s master mixer\n", chipset_name);
496 return;
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;
528 char tag;
531 * Verify that the I/O port range is free.
533 if(check_region(hw_config->io_base, 2))
535 printk(KERN_ERR
536 "%s: Control I/O port 0x%03x not free\n",
537 __FILE__,
538 hw_config->io_base);
539 return 0;
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,
550 OPL3SA2_MISC,
551 (unsigned char*) &version);
552 version &= 0x07;
554 /* Match version number to appropiate chipset */
555 if(version & CHIPSET_OPL3SAX)
557 chipset = CHIPSET_OPL3SAX;
558 tag = 'x';
559 printk(KERN_INFO "Found OPL3-SAx (YMF719)\n");
561 else
563 if(version & CHIPSET_OPL3SA3)
565 chipset = CHIPSET_OPL3SA3;
566 tag = '3';
567 printk(KERN_INFO "Found OPL3-SA3 (YMF715)\n");
569 else
571 if(version & CHIPSET_OPL3SA2)
573 chipset = CHIPSET_OPL3SA2;
574 tag = '2';
575 printk(KERN_INFO "Found OPL3-SA2 (YMF711)\n");
577 else
579 chipset = CHIPSET_UNKNOWN;
580 tag = '?';
581 printk(KERN_ERR
582 "Unknown Yamaha audio controller version\n");
583 printk(KERN_INFO
584 "%s: chipset version = %x\n",
585 __FILE__,
586 version);
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;
597 #endif
598 return 1;
600 return 0;
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);
617 /* Unload mixer */
618 if(opl3sa2_mixer >= 0)
619 sound_unload_mixerdev(opl3sa2_mixer);
623 #ifdef MODULE
625 int io = -1;
626 int mss_io = -1;
627 int mpu_io = -1;
628 int irq = -1;
629 int dma = -1;
630 int dma2 = -1;
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>");
653 EXPORT_NO_SYMBOLS;
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)
667 int i;
669 if(io == -1 || irq == -1 || dma == -1 || dma2 == -1 || mss_io == -1)
671 printk(KERN_ERR
672 "%s: io, mss_io, irq, dma, and dma2 must be set.\n",
673 __FILE__);
674 return -EINVAL;
677 /* Our own config: */
678 cfg.io_base = io;
679 cfg.irq = irq;
680 cfg.dma = dma;
681 cfg.dma2 = dma2;
683 /* The MSS config: */
684 mss_cfg.io_base = mss_io;
685 mss_cfg.irq = irq;
686 mss_cfg.dma = dma;
687 mss_cfg.dma2 = dma2;
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)
698 return -ENODEV;
701 if(probe_opl3sa2_mss(&mss_cfg) == 0)
703 return -ENODEV;
706 attach_opl3sa2(&cfg);
707 attach_opl3sa2_mss(&mss_cfg);
709 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
710 if(mpu_io != -1)
712 /* MPU config: */
713 mpu_cfg.io_base = mpu_io;
714 mpu_cfg.irq = irq;
715 mpu_cfg.dma = dma;
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);
723 #endif
724 SOUND_LOCK;
725 return 0;
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);
736 #endif
737 unload_opl3sa2_mss(&mss_cfg);
738 unload_opl3sa2(&cfg);
739 SOUND_LOCK_END;
742 #endif /* MODULE */
743 #endif /* CONFIG_OPL3SA2 */