11da177e4SLinus Torvalds /* 2c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz>, 31da177e4SLinus Torvalds * Hannu Savolainen 1993-1996, 41da177e4SLinus Torvalds * Rob Hooft 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips) 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Most if code is ported from OSS/Lite. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 111da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 121da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 131da177e4SLinus Torvalds * (at your option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 161da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 171da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 181da177e4SLinus Torvalds * GNU General Public License for more details. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 211da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 221da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <sound/opl3.h> 27*6cbbfe1cSTakashi Iwai #include <linux/io.h> 281da177e4SLinus Torvalds #include <linux/delay.h> 29da155d5bSPaul Gortmaker #include <linux/module.h> 301da177e4SLinus Torvalds #include <linux/init.h> 311da177e4SLinus Torvalds #include <linux/slab.h> 321da177e4SLinus Torvalds #include <linux/ioport.h> 331da177e4SLinus Torvalds #include <sound/minors.h> 341da177e4SLinus Torvalds 35c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Hannu Savolainen 1993-1996, Rob Hooft"); 361da177e4SLinus Torvalds MODULE_DESCRIPTION("Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)"); 371da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds extern char snd_opl3_regmap[MAX_OPL2_VOICES][4]; 401da177e4SLinus Torvalds 415b1646a8STakashi Iwai static void snd_opl2_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val) 421da177e4SLinus Torvalds { 431da177e4SLinus Torvalds unsigned long flags; 441da177e4SLinus Torvalds unsigned long port; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* 471da177e4SLinus Torvalds * The original 2-OP synth requires a quite long delay 481da177e4SLinus Torvalds * after writing to a register. 491da177e4SLinus Torvalds */ 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds spin_lock_irqsave(&opl3->reg_lock, flags); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds outb((unsigned char) cmd, port); 561da177e4SLinus Torvalds udelay(10); 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds outb((unsigned char) val, port + 1); 591da177e4SLinus Torvalds udelay(30); 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->reg_lock, flags); 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 645b1646a8STakashi Iwai static void snd_opl3_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val) 651da177e4SLinus Torvalds { 661da177e4SLinus Torvalds unsigned long flags; 671da177e4SLinus Torvalds unsigned long port; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* 701da177e4SLinus Torvalds * The OPL-3 survives with just two INBs 711da177e4SLinus Torvalds * after writing to a register. 721da177e4SLinus Torvalds */ 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds spin_lock_irqsave(&opl3->reg_lock, flags); 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds outb((unsigned char) cmd, port); 791da177e4SLinus Torvalds inb(opl3->l_port); 801da177e4SLinus Torvalds inb(opl3->l_port); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds outb((unsigned char) val, port + 1); 831da177e4SLinus Torvalds inb(opl3->l_port); 841da177e4SLinus Torvalds inb(opl3->l_port); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->reg_lock, flags); 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds 895b1646a8STakashi Iwai static int snd_opl3_detect(struct snd_opl3 * opl3) 901da177e4SLinus Torvalds { 911da177e4SLinus Torvalds /* 921da177e4SLinus Torvalds * This function returns 1 if the FM chip is present at the given I/O port 931da177e4SLinus Torvalds * The detection algorithm plays with the timer built in the FM chip and 941da177e4SLinus Torvalds * looks for a change in the status register. 951da177e4SLinus Torvalds * 961da177e4SLinus Torvalds * Note! The timers of the FM chip are not connected to AdLib (and compatible) 971da177e4SLinus Torvalds * boards. 981da177e4SLinus Torvalds * 991da177e4SLinus Torvalds * Note2! The chip is initialized if detected. 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds unsigned char stat1, stat2, signature; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* Reset timers 1 and 2 */ 1051da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK); 1061da177e4SLinus Torvalds /* Reset the IRQ of the FM chip */ 1071da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET); 1081da177e4SLinus Torvalds signature = stat1 = inb(opl3->l_port); /* Status register */ 1091da177e4SLinus Torvalds if ((stat1 & 0xe0) != 0x00) { /* Should be 0x00 */ 1101da177e4SLinus Torvalds snd_printd("OPL3: stat1 = 0x%x\n", stat1); 1111da177e4SLinus Torvalds return -ENODEV; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds /* Set timer1 to 0xff */ 1141da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 0xff); 1151da177e4SLinus Torvalds /* Unmask and start timer 1 */ 1161da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER2_MASK | OPL3_TIMER1_START); 1171da177e4SLinus Torvalds /* Now we have to delay at least 80us */ 1181da177e4SLinus Torvalds udelay(200); 1191da177e4SLinus Torvalds /* Read status after timers have expired */ 1201da177e4SLinus Torvalds stat2 = inb(opl3->l_port); 1211da177e4SLinus Torvalds /* Stop the timers */ 1221da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK); 1231da177e4SLinus Torvalds /* Reset the IRQ of the FM chip */ 1241da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET); 1251da177e4SLinus Torvalds if ((stat2 & 0xe0) != 0xc0) { /* There is no YM3812 */ 1261da177e4SLinus Torvalds snd_printd("OPL3: stat2 = 0x%x\n", stat2); 1271da177e4SLinus Torvalds return -ENODEV; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* If the toplevel code knows exactly the type of chip, don't try 1311da177e4SLinus Torvalds to detect it. */ 1321da177e4SLinus Torvalds if (opl3->hardware != OPL3_HW_AUTO) 1331da177e4SLinus Torvalds return 0; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* There is a FM chip on this address. Detect the type (OPL2 to OPL4) */ 1361da177e4SLinus Torvalds if (signature == 0x06) { /* OPL2 */ 1371da177e4SLinus Torvalds opl3->hardware = OPL3_HW_OPL2; 1381da177e4SLinus Torvalds } else { 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * If we had an OPL4 chip, opl3->hardware would have been set 1411da177e4SLinus Torvalds * by the OPL4 driver; so we can assume OPL3 here. 1421da177e4SLinus Torvalds */ 1435e246b85STakashi Iwai if (snd_BUG_ON(!opl3->r_port)) 1445e246b85STakashi Iwai return -ENODEV; 1451da177e4SLinus Torvalds opl3->hardware = OPL3_HW_OPL3; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds return 0; 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds /* 1511da177e4SLinus Torvalds * AdLib timers 1521da177e4SLinus Torvalds */ 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 1551da177e4SLinus Torvalds * Timer 1 - 80us 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds 1585b1646a8STakashi Iwai static int snd_opl3_timer1_start(struct snd_timer * timer) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds unsigned long flags; 1611da177e4SLinus Torvalds unsigned char tmp; 1621da177e4SLinus Torvalds unsigned int ticks; 1635b1646a8STakashi Iwai struct snd_opl3 *opl3; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds opl3 = snd_timer_chip(timer); 1661da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags); 1671da177e4SLinus Torvalds ticks = timer->sticks; 1681da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER1_START) & ~OPL3_TIMER1_MASK; 1691da177e4SLinus Torvalds opl3->timer_enable = tmp; 1701da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 256 - ticks); /* timer 1 count */ 1711da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */ 1721da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags); 1731da177e4SLinus Torvalds return 0; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1765b1646a8STakashi Iwai static int snd_opl3_timer1_stop(struct snd_timer * timer) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds unsigned long flags; 1791da177e4SLinus Torvalds unsigned char tmp; 1805b1646a8STakashi Iwai struct snd_opl3 *opl3; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds opl3 = snd_timer_chip(timer); 1831da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags); 1841da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER1_MASK) & ~OPL3_TIMER1_START; 1851da177e4SLinus Torvalds opl3->timer_enable = tmp; 1861da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */ 1871da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags); 1881da177e4SLinus Torvalds return 0; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* 1921da177e4SLinus Torvalds * Timer 2 - 320us 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds 1955b1646a8STakashi Iwai static int snd_opl3_timer2_start(struct snd_timer * timer) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds unsigned long flags; 1981da177e4SLinus Torvalds unsigned char tmp; 1991da177e4SLinus Torvalds unsigned int ticks; 2005b1646a8STakashi Iwai struct snd_opl3 *opl3; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds opl3 = snd_timer_chip(timer); 2031da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags); 2041da177e4SLinus Torvalds ticks = timer->sticks; 2051da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER2_START) & ~OPL3_TIMER2_MASK; 2061da177e4SLinus Torvalds opl3->timer_enable = tmp; 2071da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER2, 256 - ticks); /* timer 1 count */ 2081da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */ 2091da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags); 2101da177e4SLinus Torvalds return 0; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2135b1646a8STakashi Iwai static int snd_opl3_timer2_stop(struct snd_timer * timer) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds unsigned long flags; 2161da177e4SLinus Torvalds unsigned char tmp; 2175b1646a8STakashi Iwai struct snd_opl3 *opl3; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds opl3 = snd_timer_chip(timer); 2201da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags); 2211da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER2_MASK) & ~OPL3_TIMER2_START; 2221da177e4SLinus Torvalds opl3->timer_enable = tmp; 2231da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */ 2241da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags); 2251da177e4SLinus Torvalds return 0; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds */ 2311da177e4SLinus Torvalds 2325b1646a8STakashi Iwai static struct snd_timer_hardware snd_opl3_timer1 = 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds .flags = SNDRV_TIMER_HW_STOP, 2351da177e4SLinus Torvalds .resolution = 80000, 2361da177e4SLinus Torvalds .ticks = 256, 2371da177e4SLinus Torvalds .start = snd_opl3_timer1_start, 2381da177e4SLinus Torvalds .stop = snd_opl3_timer1_stop, 2391da177e4SLinus Torvalds }; 2401da177e4SLinus Torvalds 2415b1646a8STakashi Iwai static struct snd_timer_hardware snd_opl3_timer2 = 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds .flags = SNDRV_TIMER_HW_STOP, 2441da177e4SLinus Torvalds .resolution = 320000, 2451da177e4SLinus Torvalds .ticks = 256, 2461da177e4SLinus Torvalds .start = snd_opl3_timer2_start, 2471da177e4SLinus Torvalds .stop = snd_opl3_timer2_stop, 2481da177e4SLinus Torvalds }; 2491da177e4SLinus Torvalds 2505b1646a8STakashi Iwai static int snd_opl3_timer1_init(struct snd_opl3 * opl3, int timer_no) 2511da177e4SLinus Torvalds { 2525b1646a8STakashi Iwai struct snd_timer *timer = NULL; 2535b1646a8STakashi Iwai struct snd_timer_id tid; 2541da177e4SLinus Torvalds int err; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds tid.dev_class = SNDRV_TIMER_CLASS_CARD; 2571da177e4SLinus Torvalds tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; 2581da177e4SLinus Torvalds tid.card = opl3->card->number; 2591da177e4SLinus Torvalds tid.device = timer_no; 2601da177e4SLinus Torvalds tid.subdevice = 0; 2611da177e4SLinus Torvalds if ((err = snd_timer_new(opl3->card, "AdLib timer #1", &tid, &timer)) >= 0) { 2621da177e4SLinus Torvalds strcpy(timer->name, "AdLib timer #1"); 2631da177e4SLinus Torvalds timer->private_data = opl3; 2641da177e4SLinus Torvalds timer->hw = snd_opl3_timer1; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds opl3->timer1 = timer; 2671da177e4SLinus Torvalds return err; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2705b1646a8STakashi Iwai static int snd_opl3_timer2_init(struct snd_opl3 * opl3, int timer_no) 2711da177e4SLinus Torvalds { 2725b1646a8STakashi Iwai struct snd_timer *timer = NULL; 2735b1646a8STakashi Iwai struct snd_timer_id tid; 2741da177e4SLinus Torvalds int err; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds tid.dev_class = SNDRV_TIMER_CLASS_CARD; 2771da177e4SLinus Torvalds tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; 2781da177e4SLinus Torvalds tid.card = opl3->card->number; 2791da177e4SLinus Torvalds tid.device = timer_no; 2801da177e4SLinus Torvalds tid.subdevice = 0; 2811da177e4SLinus Torvalds if ((err = snd_timer_new(opl3->card, "AdLib timer #2", &tid, &timer)) >= 0) { 2821da177e4SLinus Torvalds strcpy(timer->name, "AdLib timer #2"); 2831da177e4SLinus Torvalds timer->private_data = opl3; 2841da177e4SLinus Torvalds timer->hw = snd_opl3_timer2; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds opl3->timer2 = timer; 2871da177e4SLinus Torvalds return err; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds */ 2931da177e4SLinus Torvalds 2945b1646a8STakashi Iwai void snd_opl3_interrupt(struct snd_hwdep * hw) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds unsigned char status; 2975b1646a8STakashi Iwai struct snd_opl3 *opl3; 2985b1646a8STakashi Iwai struct snd_timer *timer; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds if (hw == NULL) 3011da177e4SLinus Torvalds return; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds opl3 = hw->private_data; 3041da177e4SLinus Torvalds status = inb(opl3->l_port); 3051da177e4SLinus Torvalds #if 0 30645203832STakashi Iwai snd_printk(KERN_DEBUG "AdLib IRQ status = 0x%x\n", status); 3071da177e4SLinus Torvalds #endif 3081da177e4SLinus Torvalds if (!(status & 0x80)) 3091da177e4SLinus Torvalds return; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds if (status & 0x40) { 3121da177e4SLinus Torvalds timer = opl3->timer1; 3131da177e4SLinus Torvalds snd_timer_interrupt(timer, timer->sticks); 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds if (status & 0x20) { 3161da177e4SLinus Torvalds timer = opl3->timer2; 3171da177e4SLinus Torvalds snd_timer_interrupt(timer, timer->sticks); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 321ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_interrupt); 322ac19e19bSTakashi Iwai 3231da177e4SLinus Torvalds /* 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds */ 3261da177e4SLinus Torvalds 3275b1646a8STakashi Iwai static int snd_opl3_free(struct snd_opl3 *opl3) 3281da177e4SLinus Torvalds { 3295e246b85STakashi Iwai if (snd_BUG_ON(!opl3)) 3305e246b85STakashi Iwai return -ENXIO; 3311da177e4SLinus Torvalds if (opl3->private_free) 3321da177e4SLinus Torvalds opl3->private_free(opl3); 333224a0332STakashi Iwai snd_opl3_clear_patches(opl3); 334b1d5776dSTakashi Iwai release_and_free_resource(opl3->res_l_port); 335b1d5776dSTakashi Iwai release_and_free_resource(opl3->res_r_port); 3361da177e4SLinus Torvalds kfree(opl3); 3371da177e4SLinus Torvalds return 0; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3405b1646a8STakashi Iwai static int snd_opl3_dev_free(struct snd_device *device) 3411da177e4SLinus Torvalds { 3425b1646a8STakashi Iwai struct snd_opl3 *opl3 = device->device_data; 3431da177e4SLinus Torvalds return snd_opl3_free(opl3); 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3465b1646a8STakashi Iwai int snd_opl3_new(struct snd_card *card, 3471da177e4SLinus Torvalds unsigned short hardware, 3485b1646a8STakashi Iwai struct snd_opl3 **ropl3) 3491da177e4SLinus Torvalds { 3505b1646a8STakashi Iwai static struct snd_device_ops ops = { 3511da177e4SLinus Torvalds .dev_free = snd_opl3_dev_free, 3521da177e4SLinus Torvalds }; 3535b1646a8STakashi Iwai struct snd_opl3 *opl3; 3541da177e4SLinus Torvalds int err; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds *ropl3 = NULL; 357561b220aSTakashi Iwai opl3 = kzalloc(sizeof(*opl3), GFP_KERNEL); 35873e77ba0STakashi Iwai if (opl3 == NULL) { 35973e77ba0STakashi Iwai snd_printk(KERN_ERR "opl3: cannot allocate\n"); 3601da177e4SLinus Torvalds return -ENOMEM; 36173e77ba0STakashi Iwai } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds opl3->card = card; 3641da177e4SLinus Torvalds opl3->hardware = hardware; 3651da177e4SLinus Torvalds spin_lock_init(&opl3->reg_lock); 3661da177e4SLinus Torvalds spin_lock_init(&opl3->timer_lock); 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds if ((err = snd_device_new(card, SNDRV_DEV_CODEC, opl3, &ops)) < 0) { 3691da177e4SLinus Torvalds snd_opl3_free(opl3); 3701da177e4SLinus Torvalds return err; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds *ropl3 = opl3; 3741da177e4SLinus Torvalds return 0; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 377ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_new); 378ac19e19bSTakashi Iwai 3795b1646a8STakashi Iwai int snd_opl3_init(struct snd_opl3 *opl3) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds if (! opl3->command) { 3821da177e4SLinus Torvalds printk(KERN_ERR "snd_opl3_init: command not defined!\n"); 3831da177e4SLinus Torvalds return -EINVAL; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT); 3871da177e4SLinus Torvalds /* Melodic mode */ 3881da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds switch (opl3->hardware & OPL3_HW_MASK) { 3911da177e4SLinus Torvalds case OPL3_HW_OPL2: 3921da177e4SLinus Torvalds opl3->max_voices = MAX_OPL2_VOICES; 3931da177e4SLinus Torvalds break; 3941da177e4SLinus Torvalds case OPL3_HW_OPL3: 3951da177e4SLinus Torvalds case OPL3_HW_OPL4: 3961da177e4SLinus Torvalds opl3->max_voices = MAX_OPL3_VOICES; 3971da177e4SLinus Torvalds /* Enter OPL3 mode */ 3981da177e4SLinus Torvalds opl3->command(opl3, OPL3_RIGHT | OPL3_REG_MODE, OPL3_OPL3_ENABLE); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds return 0; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 403ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_init); 404ac19e19bSTakashi Iwai 4055b1646a8STakashi Iwai int snd_opl3_create(struct snd_card *card, 4061da177e4SLinus Torvalds unsigned long l_port, 4071da177e4SLinus Torvalds unsigned long r_port, 4081da177e4SLinus Torvalds unsigned short hardware, 4091da177e4SLinus Torvalds int integrated, 4105b1646a8STakashi Iwai struct snd_opl3 ** ropl3) 4111da177e4SLinus Torvalds { 4125b1646a8STakashi Iwai struct snd_opl3 *opl3; 4131da177e4SLinus Torvalds int err; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds *ropl3 = NULL; 4161da177e4SLinus Torvalds if ((err = snd_opl3_new(card, hardware, &opl3)) < 0) 4171da177e4SLinus Torvalds return err; 4181da177e4SLinus Torvalds if (! integrated) { 4191da177e4SLinus Torvalds if ((opl3->res_l_port = request_region(l_port, 2, "OPL2/3 (left)")) == NULL) { 4201da177e4SLinus Torvalds snd_printk(KERN_ERR "opl3: can't grab left port 0x%lx\n", l_port); 421676338a1STakashi Iwai snd_device_free(card, opl3); 4221da177e4SLinus Torvalds return -EBUSY; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds if (r_port != 0 && 4251da177e4SLinus Torvalds (opl3->res_r_port = request_region(r_port, 2, "OPL2/3 (right)")) == NULL) { 4261da177e4SLinus Torvalds snd_printk(KERN_ERR "opl3: can't grab right port 0x%lx\n", r_port); 427676338a1STakashi Iwai snd_device_free(card, opl3); 4281da177e4SLinus Torvalds return -EBUSY; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds opl3->l_port = l_port; 4321da177e4SLinus Torvalds opl3->r_port = r_port; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds switch (opl3->hardware) { 4351da177e4SLinus Torvalds /* some hardware doesn't support timers */ 4361da177e4SLinus Torvalds case OPL3_HW_OPL3_SV: 4371da177e4SLinus Torvalds case OPL3_HW_OPL3_CS: 4381da177e4SLinus Torvalds case OPL3_HW_OPL3_FM801: 4391da177e4SLinus Torvalds opl3->command = &snd_opl3_command; 4401da177e4SLinus Torvalds break; 4411da177e4SLinus Torvalds default: 4421da177e4SLinus Torvalds opl3->command = &snd_opl2_command; 4431da177e4SLinus Torvalds if ((err = snd_opl3_detect(opl3)) < 0) { 4441da177e4SLinus Torvalds snd_printd("OPL2/3 chip not detected at 0x%lx/0x%lx\n", 4451da177e4SLinus Torvalds opl3->l_port, opl3->r_port); 446676338a1STakashi Iwai snd_device_free(card, opl3); 4471da177e4SLinus Torvalds return err; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds /* detect routine returns correct hardware type */ 4501da177e4SLinus Torvalds switch (opl3->hardware & OPL3_HW_MASK) { 4511da177e4SLinus Torvalds case OPL3_HW_OPL3: 4521da177e4SLinus Torvalds case OPL3_HW_OPL4: 4531da177e4SLinus Torvalds opl3->command = &snd_opl3_command; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds snd_opl3_init(opl3); 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds *ropl3 = opl3; 4601da177e4SLinus Torvalds return 0; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 463ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_create); 464ac19e19bSTakashi Iwai 4655b1646a8STakashi Iwai int snd_opl3_timer_new(struct snd_opl3 * opl3, int timer1_dev, int timer2_dev) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds int err; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds if (timer1_dev >= 0) 4701da177e4SLinus Torvalds if ((err = snd_opl3_timer1_init(opl3, timer1_dev)) < 0) 4711da177e4SLinus Torvalds return err; 4721da177e4SLinus Torvalds if (timer2_dev >= 0) { 4731da177e4SLinus Torvalds if ((err = snd_opl3_timer2_init(opl3, timer2_dev)) < 0) { 4741da177e4SLinus Torvalds snd_device_free(opl3->card, opl3->timer1); 4751da177e4SLinus Torvalds opl3->timer1 = NULL; 4761da177e4SLinus Torvalds return err; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds return 0; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 482ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_timer_new); 483ac19e19bSTakashi Iwai 4845b1646a8STakashi Iwai int snd_opl3_hwdep_new(struct snd_opl3 * opl3, 4851da177e4SLinus Torvalds int device, int seq_device, 4865b1646a8STakashi Iwai struct snd_hwdep ** rhwdep) 4871da177e4SLinus Torvalds { 4885b1646a8STakashi Iwai struct snd_hwdep *hw; 4895b1646a8STakashi Iwai struct snd_card *card = opl3->card; 4901da177e4SLinus Torvalds int err; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds if (rhwdep) 4931da177e4SLinus Torvalds *rhwdep = NULL; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* create hardware dependent device (direct FM) */ 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds if ((err = snd_hwdep_new(card, "OPL2/OPL3", device, &hw)) < 0) { 4981da177e4SLinus Torvalds snd_device_free(card, opl3); 4991da177e4SLinus Torvalds return err; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds hw->private_data = opl3; 50205c1afe7STakashi Iwai hw->exclusive = 1; 5031da177e4SLinus Torvalds #ifdef CONFIG_SND_OSSEMUL 50480d7d771STakashi Iwai if (device == 0) 5051da177e4SLinus Torvalds hw->oss_type = SNDRV_OSS_DEVICE_TYPE_DMFM; 5061da177e4SLinus Torvalds #endif 5071da177e4SLinus Torvalds strcpy(hw->name, hw->id); 5081da177e4SLinus Torvalds switch (opl3->hardware & OPL3_HW_MASK) { 5091da177e4SLinus Torvalds case OPL3_HW_OPL2: 5101da177e4SLinus Torvalds strcpy(hw->name, "OPL2 FM"); 5111da177e4SLinus Torvalds hw->iface = SNDRV_HWDEP_IFACE_OPL2; 5121da177e4SLinus Torvalds break; 5131da177e4SLinus Torvalds case OPL3_HW_OPL3: 5141da177e4SLinus Torvalds strcpy(hw->name, "OPL3 FM"); 5151da177e4SLinus Torvalds hw->iface = SNDRV_HWDEP_IFACE_OPL3; 5161da177e4SLinus Torvalds break; 5171da177e4SLinus Torvalds case OPL3_HW_OPL4: 5181da177e4SLinus Torvalds strcpy(hw->name, "OPL4 FM"); 5191da177e4SLinus Torvalds hw->iface = SNDRV_HWDEP_IFACE_OPL4; 5201da177e4SLinus Torvalds break; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /* operators - only ioctl */ 5241da177e4SLinus Torvalds hw->ops.open = snd_opl3_open; 5251da177e4SLinus Torvalds hw->ops.ioctl = snd_opl3_ioctl; 526224a0332STakashi Iwai hw->ops.write = snd_opl3_write; 5271da177e4SLinus Torvalds hw->ops.release = snd_opl3_release; 5281da177e4SLinus Torvalds 52904576525STakashi Iwai opl3->hwdep = hw; 5301da177e4SLinus Torvalds opl3->seq_dev_num = seq_device; 5311da177e4SLinus Torvalds #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE)) 5321da177e4SLinus Torvalds if (snd_seq_device_new(card, seq_device, SNDRV_SEQ_DEV_ID_OPL3, 5335b1646a8STakashi Iwai sizeof(struct snd_opl3 *), &opl3->seq_dev) >= 0) { 5341da177e4SLinus Torvalds strcpy(opl3->seq_dev->name, hw->name); 5355b1646a8STakashi Iwai *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(opl3->seq_dev) = opl3; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds #endif 5381da177e4SLinus Torvalds if (rhwdep) 5391da177e4SLinus Torvalds *rhwdep = hw; 5401da177e4SLinus Torvalds return 0; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds EXPORT_SYMBOL(snd_opl3_hwdep_new); 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* 5461da177e4SLinus Torvalds * INIT part 5471da177e4SLinus Torvalds */ 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds static int __init alsa_opl3_init(void) 5501da177e4SLinus Torvalds { 5511da177e4SLinus Torvalds return 0; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds static void __exit alsa_opl3_exit(void) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds module_init(alsa_opl3_init) 5591da177e4SLinus Torvalds module_exit(alsa_opl3_exit) 560