2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
3 * Hannu Savolainen 1993-1996,
6 * Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)
8 * Most if code is ported from OSS/Lite.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sound/opl3.h>
28 #include <linux/delay.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/ioport.h>
32 #include <sound/minors.h>
34 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Hannu Savolainen 1993-1996, Rob Hooft");
35 MODULE_DESCRIPTION("Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)");
36 MODULE_LICENSE("GPL");
40 extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
42 void snd_opl2_command(opl3_t * opl3, unsigned short cmd, unsigned char val)
48 * The original 2-OP synth requires a quite long delay
49 * after writing to a register.
52 port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
54 spin_lock_irqsave(&opl3->reg_lock, flags);
56 outb((unsigned char) cmd, port);
59 outb((unsigned char) val, port + 1);
62 spin_unlock_irqrestore(&opl3->reg_lock, flags);
65 void snd_opl3_command(opl3_t * opl3, unsigned short cmd, unsigned char val)
71 * The OPL-3 survives with just two INBs
72 * after writing to a register.
75 port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
77 spin_lock_irqsave(&opl3->reg_lock, flags);
79 outb((unsigned char) cmd, port);
83 outb((unsigned char) val, port + 1);
87 spin_unlock_irqrestore(&opl3->reg_lock, flags);
90 void snd_opl3_cs4281_command(opl3_t * opl3, unsigned short cmd, unsigned char val)
96 * CS4281 requires a special access to I/O registers
99 port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
101 spin_lock_irqsave(&opl3->reg_lock, flags);
103 writel((unsigned int)cmd, port << 2);
106 writel((unsigned int)val, (port + 1) << 2);
109 spin_unlock_irqrestore(&opl3->reg_lock, flags);
112 static int snd_opl3_detect(opl3_t * opl3)
115 * This function returns 1 if the FM chip is present at the given I/O port
116 * The detection algorithm plays with the timer built in the FM chip and
117 * looks for a change in the status register.
119 * Note! The timers of the FM chip are not connected to AdLib (and compatible)
122 * Note2! The chip is initialized if detected.
125 unsigned char stat1, stat2, signature;
127 /* Reset timers 1 and 2 */
128 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
129 /* Reset the IRQ of the FM chip */
130 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
131 signature = stat1 = inb(opl3->l_port); /* Status register */
132 if ((stat1 & 0xe0) != 0x00) { /* Should be 0x00 */
133 snd_printd("OPL3: stat1 = 0x%x\n", stat1);
136 /* Set timer1 to 0xff */
137 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 0xff);
138 /* Unmask and start timer 1 */
139 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER2_MASK | OPL3_TIMER1_START);
140 /* Now we have to delay at least 80us */
142 /* Read status after timers have expired */
143 stat2 = inb(opl3->l_port);
144 /* Stop the timers */
145 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
146 /* Reset the IRQ of the FM chip */
147 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
148 if ((stat2 & 0xe0) != 0xc0) { /* There is no YM3812 */
149 snd_printd("OPL3: stat2 = 0x%x\n", stat2);
153 /* If the toplevel code knows exactly the type of chip, don't try
155 if (opl3->hardware != OPL3_HW_AUTO)
158 /* There is a FM chip on this address. Detect the type (OPL2 to OPL4) */
159 if (signature == 0x06) { /* OPL2 */
160 opl3->hardware = OPL3_HW_OPL2;
163 * If we had an OPL4 chip, opl3->hardware would have been set
164 * by the OPL4 driver; so we can assume OPL3 here.
166 snd_assert(opl3->r_port != 0, return -ENODEV);
167 opl3->hardware = OPL3_HW_OPL3;
180 static int snd_opl3_timer1_start(snd_timer_t * timer)
187 opl3 = snd_timer_chip(timer);
188 spin_lock_irqsave(&opl3->timer_lock, flags);
189 ticks = timer->sticks;
190 tmp = (opl3->timer_enable | OPL3_TIMER1_START) & ~OPL3_TIMER1_MASK;
191 opl3->timer_enable = tmp;
192 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 256 - ticks); /* timer 1 count */
193 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
194 spin_unlock_irqrestore(&opl3->timer_lock, flags);
198 static int snd_opl3_timer1_stop(snd_timer_t * timer)
204 opl3 = snd_timer_chip(timer);
205 spin_lock_irqsave(&opl3->timer_lock, flags);
206 tmp = (opl3->timer_enable | OPL3_TIMER1_MASK) & ~OPL3_TIMER1_START;
207 opl3->timer_enable = tmp;
208 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
209 spin_unlock_irqrestore(&opl3->timer_lock, flags);
217 static int snd_opl3_timer2_start(snd_timer_t * timer)
224 opl3 = snd_timer_chip(timer);
225 spin_lock_irqsave(&opl3->timer_lock, flags);
226 ticks = timer->sticks;
227 tmp = (opl3->timer_enable | OPL3_TIMER2_START) & ~OPL3_TIMER2_MASK;
228 opl3->timer_enable = tmp;
229 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER2, 256 - ticks); /* timer 1 count */
230 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
231 spin_unlock_irqrestore(&opl3->timer_lock, flags);
235 static int snd_opl3_timer2_stop(snd_timer_t * timer)
241 opl3 = snd_timer_chip(timer);
242 spin_lock_irqsave(&opl3->timer_lock, flags);
243 tmp = (opl3->timer_enable | OPL3_TIMER2_MASK) & ~OPL3_TIMER2_START;
244 opl3->timer_enable = tmp;
245 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
246 spin_unlock_irqrestore(&opl3->timer_lock, flags);
254 static struct _snd_timer_hardware snd_opl3_timer1 =
256 .flags = SNDRV_TIMER_HW_STOP,
259 .start = snd_opl3_timer1_start,
260 .stop = snd_opl3_timer1_stop,
263 static struct _snd_timer_hardware snd_opl3_timer2 =
265 .flags = SNDRV_TIMER_HW_STOP,
266 .resolution = 320000,
268 .start = snd_opl3_timer2_start,
269 .stop = snd_opl3_timer2_stop,
272 static int snd_opl3_timer1_init(opl3_t * opl3, int timer_no)
274 snd_timer_t *timer = NULL;
278 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
279 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
280 tid.card = opl3->card->number;
281 tid.device = timer_no;
283 if ((err = snd_timer_new(opl3->card, "AdLib timer #1", &tid, &timer)) >= 0) {
284 strcpy(timer->name, "AdLib timer #1");
285 timer->private_data = opl3;
286 timer->hw = snd_opl3_timer1;
288 opl3->timer1 = timer;
292 static int snd_opl3_timer2_init(opl3_t * opl3, int timer_no)
294 snd_timer_t *timer = NULL;
298 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
299 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
300 tid.card = opl3->card->number;
301 tid.device = timer_no;
303 if ((err = snd_timer_new(opl3->card, "AdLib timer #2", &tid, &timer)) >= 0) {
304 strcpy(timer->name, "AdLib timer #2");
305 timer->private_data = opl3;
306 timer->hw = snd_opl3_timer2;
308 opl3->timer2 = timer;
316 void snd_opl3_interrupt(snd_hwdep_t * hw)
318 unsigned char status;
325 opl3 = snd_magic_cast(opl3_t, hw->private_data, return);
326 status = inb(opl3->l_port);
328 snd_printk("AdLib IRQ status = 0x%x\n", status);
330 if (!(status & 0x80))
334 timer = opl3->timer1;
335 snd_timer_interrupt(timer, timer->sticks);
338 timer = opl3->timer2;
339 snd_timer_interrupt(timer, timer->sticks);
347 static int snd_opl3_free(opl3_t *opl3)
349 if (opl3->res_l_port) {
350 release_resource(opl3->res_l_port);
351 kfree_nocheck(opl3->res_l_port);
353 if (opl3->res_r_port) {
354 release_resource(opl3->res_r_port);
355 kfree_nocheck(opl3->res_r_port);
357 snd_magic_kfree(opl3);
361 static int snd_opl3_dev_free(snd_device_t *device)
363 opl3_t *opl3 = snd_magic_cast(opl3_t, device->device_data, return -ENXIO);
364 return snd_opl3_free(opl3);
367 int snd_opl3_create(snd_card_t * card,
368 unsigned long l_port,
369 unsigned long r_port,
370 unsigned short hardware,
376 static snd_device_ops_t ops = {
377 .dev_free = snd_opl3_dev_free,
382 opl3 = snd_magic_kcalloc(opl3_t, 0, GFP_KERNEL);
387 goto __step1; /* ports are already reserved */
389 if ((opl3->res_l_port = request_region(l_port, 2, "OPL2/3 (left)")) == NULL) {
390 snd_printk(KERN_ERR "opl3: can't grab left port 0x%lx\n", l_port);
395 (opl3->res_r_port = request_region(r_port, 2, "OPL2/3 (right)")) == NULL) {
396 snd_printk(KERN_ERR "opl3: can't grab right port 0x%lx\n", r_port);
404 opl3->hardware = hardware;
405 opl3->l_port = l_port;
406 opl3->r_port = r_port;
408 spin_lock_init(&opl3->reg_lock);
409 spin_lock_init(&opl3->timer_lock);
410 init_MUTEX(&opl3->access_mutex);
412 switch (opl3->hardware) {
413 /* some hardware doesn't support timers */
414 case OPL3_HW_OPL3_SV:
415 case OPL3_HW_OPL3_CS:
416 case OPL3_HW_OPL3_FM801:
417 opl3->command = &snd_opl3_command;
419 case OPL3_HW_OPL3_CS4281:
420 opl3->command = &snd_opl3_cs4281_command;
423 opl3->command = &snd_opl2_command;
424 if ((err = snd_opl3_detect(opl3)) < 0) {
425 snd_printd("OPL2/3 chip not detected at 0x%lx/0x%lx\n",
426 opl3->l_port, opl3->r_port);
430 /* detect routine returns correct hardware type */
431 switch (opl3->hardware & OPL3_HW_MASK) {
434 opl3->command = &snd_opl3_command;
438 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
439 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); /* Melodic mode */
441 switch (opl3->hardware & OPL3_HW_MASK) {
443 opl3->max_voices = MAX_OPL2_VOICES;
447 opl3->max_voices = MAX_OPL3_VOICES;
448 snd_assert(opl3->r_port != 0, snd_opl3_free(opl3); return -ENODEV);
449 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_MODE, OPL3_OPL3_ENABLE); /* Enter OPL3 mode */
451 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, opl3, &ops)) < 0) {
460 int snd_opl3_timer_new(opl3_t * opl3, int timer1_dev, int timer2_dev)
465 if ((err = snd_opl3_timer1_init(opl3, timer1_dev)) < 0)
467 if (timer2_dev >= 0) {
468 if ((err = snd_opl3_timer2_init(opl3, timer2_dev)) < 0) {
469 snd_device_free(opl3->card, opl3->timer1);
477 int snd_opl3_hwdep_new(opl3_t * opl3,
478 int device, int seq_device,
479 snd_hwdep_t ** rhwdep)
482 snd_card_t *card = opl3->card;
488 /* create hardware dependent device (direct FM) */
490 if ((err = snd_hwdep_new(card, "OPL2/OPL3", device, &hw)) < 0) {
491 snd_device_free(card, opl3);
494 hw->private_data = opl3;
495 #ifdef CONFIG_SND_OSSEMUL
497 hw->oss_type = SNDRV_OSS_DEVICE_TYPE_DMFM;
498 sprintf(hw->oss_dev, "dmfm%i", card->number);
501 strcpy(hw->name, hw->id);
502 switch (opl3->hardware & OPL3_HW_MASK) {
504 strcpy(hw->name, "OPL2 FM");
505 hw->iface = SNDRV_HWDEP_IFACE_OPL2;
508 strcpy(hw->name, "OPL3 FM");
509 hw->iface = SNDRV_HWDEP_IFACE_OPL3;
512 strcpy(hw->name, "OPL4 FM");
513 hw->iface = SNDRV_HWDEP_IFACE_OPL4;
517 /* operators - only ioctl */
518 hw->ops.open = snd_opl3_open;
519 hw->ops.ioctl = snd_opl3_ioctl;
520 hw->ops.release = snd_opl3_release;
522 opl3->seq_dev_num = seq_device;
523 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
524 if (snd_seq_device_new(card, seq_device, SNDRV_SEQ_DEV_ID_OPL3,
525 sizeof(opl3_t*), &opl3->seq_dev) >= 0) {
526 strcpy(opl3->seq_dev->name, hw->name);
527 *(opl3_t**)SNDRV_SEQ_DEVICE_ARGPTR(opl3->seq_dev) = opl3;
535 EXPORT_SYMBOL(snd_opl3_interrupt);
536 EXPORT_SYMBOL(snd_opl3_create);
537 EXPORT_SYMBOL(snd_opl3_timer_new);
538 EXPORT_SYMBOL(snd_opl3_hwdep_new);
541 EXPORT_SYMBOL(snd_opl3_regmap);
542 EXPORT_SYMBOL(snd_opl3_reset);
548 static int __init alsa_opl3_init(void)
553 static void __exit alsa_opl3_exit(void)
557 module_init(alsa_opl3_init)
558 module_exit(alsa_opl3_exit)