11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
3c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
41da177e4SLinus Torvalds * Hannu Savolainen 1993-1996,
51da177e4SLinus Torvalds * Rob Hooft
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Most if code is ported from OSS/Lite.
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #include <sound/opl3.h>
136cbbfe1cSTakashi Iwai #include <linux/io.h>
141da177e4SLinus Torvalds #include <linux/delay.h>
15da155d5bSPaul Gortmaker #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/ioport.h>
191da177e4SLinus Torvalds #include <sound/minors.h>
207e9c20f4STakashi Iwai #include "opl3_voice.h"
211da177e4SLinus Torvalds
22c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Hannu Savolainen 1993-1996, Rob Hooft");
231da177e4SLinus Torvalds MODULE_DESCRIPTION("Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)");
241da177e4SLinus Torvalds MODULE_LICENSE("GPL");
251da177e4SLinus Torvalds
snd_opl2_command(struct snd_opl3 * opl3,unsigned short cmd,unsigned char val)265b1646a8STakashi Iwai static void snd_opl2_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val)
271da177e4SLinus Torvalds {
281da177e4SLinus Torvalds unsigned long flags;
291da177e4SLinus Torvalds unsigned long port;
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds * The original 2-OP synth requires a quite long delay
331da177e4SLinus Torvalds * after writing to a register.
341da177e4SLinus Torvalds */
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds spin_lock_irqsave(&opl3->reg_lock, flags);
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds outb((unsigned char) cmd, port);
411da177e4SLinus Torvalds udelay(10);
421da177e4SLinus Torvalds
431da177e4SLinus Torvalds outb((unsigned char) val, port + 1);
441da177e4SLinus Torvalds udelay(30);
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->reg_lock, flags);
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds
snd_opl3_command(struct snd_opl3 * opl3,unsigned short cmd,unsigned char val)495b1646a8STakashi Iwai static void snd_opl3_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds unsigned long flags;
521da177e4SLinus Torvalds unsigned long port;
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds * The OPL-3 survives with just two INBs
561da177e4SLinus Torvalds * after writing to a register.
571da177e4SLinus Torvalds */
581da177e4SLinus Torvalds
591da177e4SLinus Torvalds port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds spin_lock_irqsave(&opl3->reg_lock, flags);
621da177e4SLinus Torvalds
631da177e4SLinus Torvalds outb((unsigned char) cmd, port);
641da177e4SLinus Torvalds inb(opl3->l_port);
651da177e4SLinus Torvalds inb(opl3->l_port);
661da177e4SLinus Torvalds
671da177e4SLinus Torvalds outb((unsigned char) val, port + 1);
681da177e4SLinus Torvalds inb(opl3->l_port);
691da177e4SLinus Torvalds inb(opl3->l_port);
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->reg_lock, flags);
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds
snd_opl3_detect(struct snd_opl3 * opl3)745b1646a8STakashi Iwai static int snd_opl3_detect(struct snd_opl3 * opl3)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds /*
771da177e4SLinus Torvalds * This function returns 1 if the FM chip is present at the given I/O port
781da177e4SLinus Torvalds * The detection algorithm plays with the timer built in the FM chip and
791da177e4SLinus Torvalds * looks for a change in the status register.
801da177e4SLinus Torvalds *
811da177e4SLinus Torvalds * Note! The timers of the FM chip are not connected to AdLib (and compatible)
821da177e4SLinus Torvalds * boards.
831da177e4SLinus Torvalds *
841da177e4SLinus Torvalds * Note2! The chip is initialized if detected.
851da177e4SLinus Torvalds */
861da177e4SLinus Torvalds
871da177e4SLinus Torvalds unsigned char stat1, stat2, signature;
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds /* Reset timers 1 and 2 */
901da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
911da177e4SLinus Torvalds /* Reset the IRQ of the FM chip */
921da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
931da177e4SLinus Torvalds signature = stat1 = inb(opl3->l_port); /* Status register */
941da177e4SLinus Torvalds if ((stat1 & 0xe0) != 0x00) { /* Should be 0x00 */
951da177e4SLinus Torvalds snd_printd("OPL3: stat1 = 0x%x\n", stat1);
961da177e4SLinus Torvalds return -ENODEV;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds /* Set timer1 to 0xff */
991da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 0xff);
1001da177e4SLinus Torvalds /* Unmask and start timer 1 */
1011da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER2_MASK | OPL3_TIMER1_START);
1021da177e4SLinus Torvalds /* Now we have to delay at least 80us */
1031da177e4SLinus Torvalds udelay(200);
1041da177e4SLinus Torvalds /* Read status after timers have expired */
1051da177e4SLinus Torvalds stat2 = inb(opl3->l_port);
1061da177e4SLinus Torvalds /* Stop the timers */
1071da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
1081da177e4SLinus Torvalds /* Reset the IRQ of the FM chip */
1091da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
1101da177e4SLinus Torvalds if ((stat2 & 0xe0) != 0xc0) { /* There is no YM3812 */
1111da177e4SLinus Torvalds snd_printd("OPL3: stat2 = 0x%x\n", stat2);
1121da177e4SLinus Torvalds return -ENODEV;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds
1151da177e4SLinus Torvalds /* If the toplevel code knows exactly the type of chip, don't try
1161da177e4SLinus Torvalds to detect it. */
1171da177e4SLinus Torvalds if (opl3->hardware != OPL3_HW_AUTO)
1181da177e4SLinus Torvalds return 0;
1191da177e4SLinus Torvalds
1201da177e4SLinus Torvalds /* There is a FM chip on this address. Detect the type (OPL2 to OPL4) */
1211da177e4SLinus Torvalds if (signature == 0x06) { /* OPL2 */
1221da177e4SLinus Torvalds opl3->hardware = OPL3_HW_OPL2;
1231da177e4SLinus Torvalds } else {
1241da177e4SLinus Torvalds /*
1251da177e4SLinus Torvalds * If we had an OPL4 chip, opl3->hardware would have been set
1261da177e4SLinus Torvalds * by the OPL4 driver; so we can assume OPL3 here.
1271da177e4SLinus Torvalds */
1285e246b85STakashi Iwai if (snd_BUG_ON(!opl3->r_port))
1295e246b85STakashi Iwai return -ENODEV;
1301da177e4SLinus Torvalds opl3->hardware = OPL3_HW_OPL3;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds return 0;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds * AdLib timers
1371da177e4SLinus Torvalds */
1381da177e4SLinus Torvalds
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds * Timer 1 - 80us
1411da177e4SLinus Torvalds */
1421da177e4SLinus Torvalds
snd_opl3_timer1_start(struct snd_timer * timer)1435b1646a8STakashi Iwai static int snd_opl3_timer1_start(struct snd_timer * timer)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds unsigned long flags;
1461da177e4SLinus Torvalds unsigned char tmp;
1471da177e4SLinus Torvalds unsigned int ticks;
1485b1646a8STakashi Iwai struct snd_opl3 *opl3;
1491da177e4SLinus Torvalds
1501da177e4SLinus Torvalds opl3 = snd_timer_chip(timer);
1511da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags);
1521da177e4SLinus Torvalds ticks = timer->sticks;
1531da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER1_START) & ~OPL3_TIMER1_MASK;
1541da177e4SLinus Torvalds opl3->timer_enable = tmp;
1551da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 256 - ticks); /* timer 1 count */
1561da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
1571da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags);
1581da177e4SLinus Torvalds return 0;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds
snd_opl3_timer1_stop(struct snd_timer * timer)1615b1646a8STakashi Iwai static int snd_opl3_timer1_stop(struct snd_timer * timer)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds unsigned long flags;
1641da177e4SLinus Torvalds unsigned char tmp;
1655b1646a8STakashi Iwai struct snd_opl3 *opl3;
1661da177e4SLinus Torvalds
1671da177e4SLinus Torvalds opl3 = snd_timer_chip(timer);
1681da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags);
1691da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER1_MASK) & ~OPL3_TIMER1_START;
1701da177e4SLinus Torvalds opl3->timer_enable = tmp;
1711da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
1721da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags);
1731da177e4SLinus Torvalds return 0;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds
1761da177e4SLinus Torvalds /*
1771da177e4SLinus Torvalds * Timer 2 - 320us
1781da177e4SLinus Torvalds */
1791da177e4SLinus Torvalds
snd_opl3_timer2_start(struct snd_timer * timer)1805b1646a8STakashi Iwai static int snd_opl3_timer2_start(struct snd_timer * timer)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds unsigned long flags;
1831da177e4SLinus Torvalds unsigned char tmp;
1841da177e4SLinus Torvalds unsigned int ticks;
1855b1646a8STakashi Iwai struct snd_opl3 *opl3;
1861da177e4SLinus Torvalds
1871da177e4SLinus Torvalds opl3 = snd_timer_chip(timer);
1881da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags);
1891da177e4SLinus Torvalds ticks = timer->sticks;
1901da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER2_START) & ~OPL3_TIMER2_MASK;
1911da177e4SLinus Torvalds opl3->timer_enable = tmp;
1921da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER2, 256 - ticks); /* timer 1 count */
1931da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
1941da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags);
1951da177e4SLinus Torvalds return 0;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds
snd_opl3_timer2_stop(struct snd_timer * timer)1985b1646a8STakashi Iwai static int snd_opl3_timer2_stop(struct snd_timer * timer)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds unsigned long flags;
2011da177e4SLinus Torvalds unsigned char tmp;
2025b1646a8STakashi Iwai struct snd_opl3 *opl3;
2031da177e4SLinus Torvalds
2041da177e4SLinus Torvalds opl3 = snd_timer_chip(timer);
2051da177e4SLinus Torvalds spin_lock_irqsave(&opl3->timer_lock, flags);
2061da177e4SLinus Torvalds tmp = (opl3->timer_enable | OPL3_TIMER2_MASK) & ~OPL3_TIMER2_START;
2071da177e4SLinus Torvalds opl3->timer_enable = tmp;
2081da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
2091da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->timer_lock, flags);
2101da177e4SLinus Torvalds return 0;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds /*
2141da177e4SLinus Torvalds
2151da177e4SLinus Torvalds */
2161da177e4SLinus Torvalds
2175ff16a3dSTakashi Iwai static const struct snd_timer_hardware snd_opl3_timer1 =
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds .flags = SNDRV_TIMER_HW_STOP,
2201da177e4SLinus Torvalds .resolution = 80000,
2211da177e4SLinus Torvalds .ticks = 256,
2221da177e4SLinus Torvalds .start = snd_opl3_timer1_start,
2231da177e4SLinus Torvalds .stop = snd_opl3_timer1_stop,
2241da177e4SLinus Torvalds };
2251da177e4SLinus Torvalds
2265ff16a3dSTakashi Iwai static const struct snd_timer_hardware snd_opl3_timer2 =
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds .flags = SNDRV_TIMER_HW_STOP,
2291da177e4SLinus Torvalds .resolution = 320000,
2301da177e4SLinus Torvalds .ticks = 256,
2311da177e4SLinus Torvalds .start = snd_opl3_timer2_start,
2321da177e4SLinus Torvalds .stop = snd_opl3_timer2_stop,
2331da177e4SLinus Torvalds };
2341da177e4SLinus Torvalds
snd_opl3_timer1_init(struct snd_opl3 * opl3,int timer_no)2355b1646a8STakashi Iwai static int snd_opl3_timer1_init(struct snd_opl3 * opl3, int timer_no)
2361da177e4SLinus Torvalds {
2375b1646a8STakashi Iwai struct snd_timer *timer = NULL;
2385b1646a8STakashi Iwai struct snd_timer_id tid;
2391da177e4SLinus Torvalds int err;
2401da177e4SLinus Torvalds
2411da177e4SLinus Torvalds tid.dev_class = SNDRV_TIMER_CLASS_CARD;
2421da177e4SLinus Torvalds tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
2431da177e4SLinus Torvalds tid.card = opl3->card->number;
2441da177e4SLinus Torvalds tid.device = timer_no;
2451da177e4SLinus Torvalds tid.subdevice = 0;
246*9c78e803STakashi Iwai err = snd_timer_new(opl3->card, "AdLib timer #1", &tid, &timer);
247*9c78e803STakashi Iwai if (err >= 0) {
2481da177e4SLinus Torvalds strcpy(timer->name, "AdLib timer #1");
2491da177e4SLinus Torvalds timer->private_data = opl3;
2501da177e4SLinus Torvalds timer->hw = snd_opl3_timer1;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds opl3->timer1 = timer;
2531da177e4SLinus Torvalds return err;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds
snd_opl3_timer2_init(struct snd_opl3 * opl3,int timer_no)2565b1646a8STakashi Iwai static int snd_opl3_timer2_init(struct snd_opl3 * opl3, int timer_no)
2571da177e4SLinus Torvalds {
2585b1646a8STakashi Iwai struct snd_timer *timer = NULL;
2595b1646a8STakashi Iwai struct snd_timer_id tid;
2601da177e4SLinus Torvalds int err;
2611da177e4SLinus Torvalds
2621da177e4SLinus Torvalds tid.dev_class = SNDRV_TIMER_CLASS_CARD;
2631da177e4SLinus Torvalds tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
2641da177e4SLinus Torvalds tid.card = opl3->card->number;
2651da177e4SLinus Torvalds tid.device = timer_no;
2661da177e4SLinus Torvalds tid.subdevice = 0;
267*9c78e803STakashi Iwai err = snd_timer_new(opl3->card, "AdLib timer #2", &tid, &timer);
268*9c78e803STakashi Iwai if (err >= 0) {
2691da177e4SLinus Torvalds strcpy(timer->name, "AdLib timer #2");
2701da177e4SLinus Torvalds timer->private_data = opl3;
2711da177e4SLinus Torvalds timer->hw = snd_opl3_timer2;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds opl3->timer2 = timer;
2741da177e4SLinus Torvalds return err;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds
2771da177e4SLinus Torvalds /*
2781da177e4SLinus Torvalds
2791da177e4SLinus Torvalds */
2801da177e4SLinus Torvalds
snd_opl3_interrupt(struct snd_hwdep * hw)2815b1646a8STakashi Iwai void snd_opl3_interrupt(struct snd_hwdep * hw)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds unsigned char status;
2845b1646a8STakashi Iwai struct snd_opl3 *opl3;
2855b1646a8STakashi Iwai struct snd_timer *timer;
2861da177e4SLinus Torvalds
2871da177e4SLinus Torvalds if (hw == NULL)
2881da177e4SLinus Torvalds return;
2891da177e4SLinus Torvalds
2901da177e4SLinus Torvalds opl3 = hw->private_data;
2911da177e4SLinus Torvalds status = inb(opl3->l_port);
2921da177e4SLinus Torvalds #if 0
29345203832STakashi Iwai snd_printk(KERN_DEBUG "AdLib IRQ status = 0x%x\n", status);
2941da177e4SLinus Torvalds #endif
2951da177e4SLinus Torvalds if (!(status & 0x80))
2961da177e4SLinus Torvalds return;
2971da177e4SLinus Torvalds
2981da177e4SLinus Torvalds if (status & 0x40) {
2991da177e4SLinus Torvalds timer = opl3->timer1;
3001da177e4SLinus Torvalds snd_timer_interrupt(timer, timer->sticks);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds if (status & 0x20) {
3031da177e4SLinus Torvalds timer = opl3->timer2;
3041da177e4SLinus Torvalds snd_timer_interrupt(timer, timer->sticks);
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds
308ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_interrupt);
309ac19e19bSTakashi Iwai
3101da177e4SLinus Torvalds /*
3111da177e4SLinus Torvalds
3121da177e4SLinus Torvalds */
3131da177e4SLinus Torvalds
snd_opl3_free(struct snd_opl3 * opl3)3145b1646a8STakashi Iwai static int snd_opl3_free(struct snd_opl3 *opl3)
3151da177e4SLinus Torvalds {
3165e246b85STakashi Iwai if (snd_BUG_ON(!opl3))
3175e246b85STakashi Iwai return -ENXIO;
3181da177e4SLinus Torvalds if (opl3->private_free)
3191da177e4SLinus Torvalds opl3->private_free(opl3);
320224a0332STakashi Iwai snd_opl3_clear_patches(opl3);
321b1d5776dSTakashi Iwai release_and_free_resource(opl3->res_l_port);
322b1d5776dSTakashi Iwai release_and_free_resource(opl3->res_r_port);
3231da177e4SLinus Torvalds kfree(opl3);
3241da177e4SLinus Torvalds return 0;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds
snd_opl3_dev_free(struct snd_device * device)3275b1646a8STakashi Iwai static int snd_opl3_dev_free(struct snd_device *device)
3281da177e4SLinus Torvalds {
3295b1646a8STakashi Iwai struct snd_opl3 *opl3 = device->device_data;
3301da177e4SLinus Torvalds return snd_opl3_free(opl3);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds
snd_opl3_new(struct snd_card * card,unsigned short hardware,struct snd_opl3 ** ropl3)3335b1646a8STakashi Iwai int snd_opl3_new(struct snd_card *card,
3341da177e4SLinus Torvalds unsigned short hardware,
3355b1646a8STakashi Iwai struct snd_opl3 **ropl3)
3361da177e4SLinus Torvalds {
33734273b23STakashi Iwai static const struct snd_device_ops ops = {
3381da177e4SLinus Torvalds .dev_free = snd_opl3_dev_free,
3391da177e4SLinus Torvalds };
3405b1646a8STakashi Iwai struct snd_opl3 *opl3;
3411da177e4SLinus Torvalds int err;
3421da177e4SLinus Torvalds
3431da177e4SLinus Torvalds *ropl3 = NULL;
344561b220aSTakashi Iwai opl3 = kzalloc(sizeof(*opl3), GFP_KERNEL);
3452b223a91SMarkus Elfring if (!opl3)
3461da177e4SLinus Torvalds return -ENOMEM;
3471da177e4SLinus Torvalds
3481da177e4SLinus Torvalds opl3->card = card;
3491da177e4SLinus Torvalds opl3->hardware = hardware;
3501da177e4SLinus Torvalds spin_lock_init(&opl3->reg_lock);
3511da177e4SLinus Torvalds spin_lock_init(&opl3->timer_lock);
3521da177e4SLinus Torvalds
353*9c78e803STakashi Iwai err = snd_device_new(card, SNDRV_DEV_CODEC, opl3, &ops);
354*9c78e803STakashi Iwai if (err < 0) {
3551da177e4SLinus Torvalds snd_opl3_free(opl3);
3561da177e4SLinus Torvalds return err;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds
3591da177e4SLinus Torvalds *ropl3 = opl3;
3601da177e4SLinus Torvalds return 0;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds
363ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_new);
364ac19e19bSTakashi Iwai
snd_opl3_init(struct snd_opl3 * opl3)3655b1646a8STakashi Iwai int snd_opl3_init(struct snd_opl3 *opl3)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds if (! opl3->command) {
3681da177e4SLinus Torvalds printk(KERN_ERR "snd_opl3_init: command not defined!\n");
3691da177e4SLinus Torvalds return -EINVAL;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds
3721da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
3731da177e4SLinus Torvalds /* Melodic mode */
3741da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00);
3751da177e4SLinus Torvalds
3761da177e4SLinus Torvalds switch (opl3->hardware & OPL3_HW_MASK) {
3771da177e4SLinus Torvalds case OPL3_HW_OPL2:
3781da177e4SLinus Torvalds opl3->max_voices = MAX_OPL2_VOICES;
3791da177e4SLinus Torvalds break;
3801da177e4SLinus Torvalds case OPL3_HW_OPL3:
3811da177e4SLinus Torvalds case OPL3_HW_OPL4:
3821da177e4SLinus Torvalds opl3->max_voices = MAX_OPL3_VOICES;
3831da177e4SLinus Torvalds /* Enter OPL3 mode */
3841da177e4SLinus Torvalds opl3->command(opl3, OPL3_RIGHT | OPL3_REG_MODE, OPL3_OPL3_ENABLE);
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds return 0;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds
389ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_init);
390ac19e19bSTakashi Iwai
snd_opl3_create(struct snd_card * card,unsigned long l_port,unsigned long r_port,unsigned short hardware,int integrated,struct snd_opl3 ** ropl3)3915b1646a8STakashi Iwai int snd_opl3_create(struct snd_card *card,
3921da177e4SLinus Torvalds unsigned long l_port,
3931da177e4SLinus Torvalds unsigned long r_port,
3941da177e4SLinus Torvalds unsigned short hardware,
3951da177e4SLinus Torvalds int integrated,
3965b1646a8STakashi Iwai struct snd_opl3 ** ropl3)
3971da177e4SLinus Torvalds {
3985b1646a8STakashi Iwai struct snd_opl3 *opl3;
3991da177e4SLinus Torvalds int err;
4001da177e4SLinus Torvalds
4011da177e4SLinus Torvalds *ropl3 = NULL;
402*9c78e803STakashi Iwai err = snd_opl3_new(card, hardware, &opl3);
403*9c78e803STakashi Iwai if (err < 0)
4041da177e4SLinus Torvalds return err;
4051da177e4SLinus Torvalds if (! integrated) {
406*9c78e803STakashi Iwai opl3->res_l_port = request_region(l_port, 2, "OPL2/3 (left)");
407*9c78e803STakashi Iwai if (!opl3->res_l_port) {
4081da177e4SLinus Torvalds snd_printk(KERN_ERR "opl3: can't grab left port 0x%lx\n", l_port);
409676338a1STakashi Iwai snd_device_free(card, opl3);
4101da177e4SLinus Torvalds return -EBUSY;
4111da177e4SLinus Torvalds }
412*9c78e803STakashi Iwai if (r_port != 0) {
413*9c78e803STakashi Iwai opl3->res_r_port = request_region(r_port, 2, "OPL2/3 (right)");
414*9c78e803STakashi Iwai if (!opl3->res_r_port) {
4151da177e4SLinus Torvalds snd_printk(KERN_ERR "opl3: can't grab right port 0x%lx\n", r_port);
416676338a1STakashi Iwai snd_device_free(card, opl3);
4171da177e4SLinus Torvalds return -EBUSY;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds }
420*9c78e803STakashi Iwai }
4211da177e4SLinus Torvalds opl3->l_port = l_port;
4221da177e4SLinus Torvalds opl3->r_port = r_port;
4231da177e4SLinus Torvalds
4241da177e4SLinus Torvalds switch (opl3->hardware) {
4251da177e4SLinus Torvalds /* some hardware doesn't support timers */
4261da177e4SLinus Torvalds case OPL3_HW_OPL3_SV:
4271da177e4SLinus Torvalds case OPL3_HW_OPL3_CS:
4281da177e4SLinus Torvalds case OPL3_HW_OPL3_FM801:
4291da177e4SLinus Torvalds opl3->command = &snd_opl3_command;
4301da177e4SLinus Torvalds break;
4311da177e4SLinus Torvalds default:
4321da177e4SLinus Torvalds opl3->command = &snd_opl2_command;
433*9c78e803STakashi Iwai err = snd_opl3_detect(opl3);
434*9c78e803STakashi Iwai if (err < 0) {
4351da177e4SLinus Torvalds snd_printd("OPL2/3 chip not detected at 0x%lx/0x%lx\n",
4361da177e4SLinus Torvalds opl3->l_port, opl3->r_port);
437676338a1STakashi Iwai snd_device_free(card, opl3);
4381da177e4SLinus Torvalds return err;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds /* detect routine returns correct hardware type */
4411da177e4SLinus Torvalds switch (opl3->hardware & OPL3_HW_MASK) {
4421da177e4SLinus Torvalds case OPL3_HW_OPL3:
4431da177e4SLinus Torvalds case OPL3_HW_OPL4:
4441da177e4SLinus Torvalds opl3->command = &snd_opl3_command;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds
4481da177e4SLinus Torvalds snd_opl3_init(opl3);
4491da177e4SLinus Torvalds
4501da177e4SLinus Torvalds *ropl3 = opl3;
4511da177e4SLinus Torvalds return 0;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds
454ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_create);
455ac19e19bSTakashi Iwai
snd_opl3_timer_new(struct snd_opl3 * opl3,int timer1_dev,int timer2_dev)4565b1646a8STakashi Iwai int snd_opl3_timer_new(struct snd_opl3 * opl3, int timer1_dev, int timer2_dev)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds int err;
4591da177e4SLinus Torvalds
460*9c78e803STakashi Iwai if (timer1_dev >= 0) {
461*9c78e803STakashi Iwai err = snd_opl3_timer1_init(opl3, timer1_dev);
462*9c78e803STakashi Iwai if (err < 0)
4631da177e4SLinus Torvalds return err;
464*9c78e803STakashi Iwai }
4651da177e4SLinus Torvalds if (timer2_dev >= 0) {
466*9c78e803STakashi Iwai err = snd_opl3_timer2_init(opl3, timer2_dev);
467*9c78e803STakashi Iwai if (err < 0) {
4681da177e4SLinus Torvalds snd_device_free(opl3->card, opl3->timer1);
4691da177e4SLinus Torvalds opl3->timer1 = NULL;
4701da177e4SLinus Torvalds return err;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds return 0;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds
476ac19e19bSTakashi Iwai EXPORT_SYMBOL(snd_opl3_timer_new);
477ac19e19bSTakashi Iwai
snd_opl3_hwdep_new(struct snd_opl3 * opl3,int device,int seq_device,struct snd_hwdep ** rhwdep)4785b1646a8STakashi Iwai int snd_opl3_hwdep_new(struct snd_opl3 * opl3,
4791da177e4SLinus Torvalds int device, int seq_device,
4805b1646a8STakashi Iwai struct snd_hwdep ** rhwdep)
4811da177e4SLinus Torvalds {
4825b1646a8STakashi Iwai struct snd_hwdep *hw;
4835b1646a8STakashi Iwai struct snd_card *card = opl3->card;
4841da177e4SLinus Torvalds int err;
4851da177e4SLinus Torvalds
4861da177e4SLinus Torvalds if (rhwdep)
4871da177e4SLinus Torvalds *rhwdep = NULL;
4881da177e4SLinus Torvalds
4891da177e4SLinus Torvalds /* create hardware dependent device (direct FM) */
4901da177e4SLinus Torvalds
491*9c78e803STakashi Iwai err = snd_hwdep_new(card, "OPL2/OPL3", device, &hw);
492*9c78e803STakashi Iwai if (err < 0) {
4931da177e4SLinus Torvalds snd_device_free(card, opl3);
4941da177e4SLinus Torvalds return err;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds hw->private_data = opl3;
49705c1afe7STakashi Iwai hw->exclusive = 1;
4981da177e4SLinus Torvalds #ifdef CONFIG_SND_OSSEMUL
49980d7d771STakashi Iwai if (device == 0)
5001da177e4SLinus Torvalds hw->oss_type = SNDRV_OSS_DEVICE_TYPE_DMFM;
5011da177e4SLinus Torvalds #endif
5021da177e4SLinus Torvalds strcpy(hw->name, hw->id);
5031da177e4SLinus Torvalds switch (opl3->hardware & OPL3_HW_MASK) {
5041da177e4SLinus Torvalds case OPL3_HW_OPL2:
5051da177e4SLinus Torvalds strcpy(hw->name, "OPL2 FM");
5061da177e4SLinus Torvalds hw->iface = SNDRV_HWDEP_IFACE_OPL2;
5071da177e4SLinus Torvalds break;
5081da177e4SLinus Torvalds case OPL3_HW_OPL3:
5091da177e4SLinus Torvalds strcpy(hw->name, "OPL3 FM");
5101da177e4SLinus Torvalds hw->iface = SNDRV_HWDEP_IFACE_OPL3;
5111da177e4SLinus Torvalds break;
5121da177e4SLinus Torvalds case OPL3_HW_OPL4:
5131da177e4SLinus Torvalds strcpy(hw->name, "OPL4 FM");
5141da177e4SLinus Torvalds hw->iface = SNDRV_HWDEP_IFACE_OPL4;
5151da177e4SLinus Torvalds break;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds
5181da177e4SLinus Torvalds /* operators - only ioctl */
5191da177e4SLinus Torvalds hw->ops.open = snd_opl3_open;
5201da177e4SLinus Torvalds hw->ops.ioctl = snd_opl3_ioctl;
521224a0332STakashi Iwai hw->ops.write = snd_opl3_write;
5221da177e4SLinus Torvalds hw->ops.release = snd_opl3_release;
5231da177e4SLinus Torvalds
52404576525STakashi Iwai opl3->hwdep = hw;
5251da177e4SLinus Torvalds opl3->seq_dev_num = seq_device;
526111b0cdbSTakashi Iwai #if IS_ENABLED(CONFIG_SND_SEQUENCER)
5271da177e4SLinus Torvalds if (snd_seq_device_new(card, seq_device, SNDRV_SEQ_DEV_ID_OPL3,
5285b1646a8STakashi Iwai sizeof(struct snd_opl3 *), &opl3->seq_dev) >= 0) {
5291da177e4SLinus Torvalds strcpy(opl3->seq_dev->name, hw->name);
5305b1646a8STakashi Iwai *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(opl3->seq_dev) = opl3;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds #endif
5331da177e4SLinus Torvalds if (rhwdep)
5341da177e4SLinus Torvalds *rhwdep = hw;
5351da177e4SLinus Torvalds return 0;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds
5381da177e4SLinus Torvalds EXPORT_SYMBOL(snd_opl3_hwdep_new);
539