Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Driver for C-Media CMI8328-based soundcards, such as AudioExcel AV500
0004  * Copyright (c) 2012 Ondrej Zary
0005  *
0006  * AudioExcel AV500 card consists of:
0007  *  - CMI8328 - main chip (SB Pro emulation, gameport, OPL3, MPU401, CD-ROM)
0008  *  - CS4231A - WSS codec
0009  *  - Dream SAM9233+GMS950400+RAM+ROM: Wavetable MIDI, connected to MPU401
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/isa.h>
0014 #include <linux/module.h>
0015 #include <linux/gameport.h>
0016 #include <asm/dma.h>
0017 #include <sound/core.h>
0018 #include <sound/wss.h>
0019 #include <sound/opl3.h>
0020 #include <sound/mpu401.h>
0021 #define SNDRV_LEGACY_FIND_FREE_IOPORT
0022 #define SNDRV_LEGACY_FIND_FREE_IRQ
0023 #define SNDRV_LEGACY_FIND_FREE_DMA
0024 #include <sound/initval.h>
0025 
0026 MODULE_AUTHOR("Ondrej Zary <linux@rainbow-software.org>");
0027 MODULE_DESCRIPTION("C-Media CMI8328");
0028 MODULE_LICENSE("GPL");
0029 
0030 #if IS_ENABLED(CONFIG_GAMEPORT)
0031 #define SUPPORT_JOYSTICK 1
0032 #endif
0033 
0034 /* I/O port is configured by jumpers on the card to one of these */
0035 static const int cmi8328_ports[] = { 0x530, 0xe80, 0xf40, 0x604 };
0036 #define CMI8328_MAX ARRAY_SIZE(cmi8328_ports)
0037 
0038 static int index[CMI8328_MAX] =     {[0 ... (CMI8328_MAX-1)] = -1};
0039 static char *id[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = NULL};
0040 static long port[CMI8328_MAX] =     {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
0041 static int irq[CMI8328_MAX] =       {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
0042 static int dma1[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
0043 static int dma2[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
0044 static long mpuport[CMI8328_MAX] =  {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
0045 static int mpuirq[CMI8328_MAX] =    {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
0046 #ifdef SUPPORT_JOYSTICK
0047 static bool gameport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = true};
0048 #endif
0049 
0050 module_param_array(index, int, NULL, 0444);
0051 MODULE_PARM_DESC(index, "Index value for CMI8328 soundcard.");
0052 module_param_array(id, charp, NULL, 0444);
0053 MODULE_PARM_DESC(id, "ID string for CMI8328 soundcard.");
0054 
0055 module_param_hw_array(port, long, ioport, NULL, 0444);
0056 MODULE_PARM_DESC(port, "Port # for CMI8328 driver.");
0057 module_param_hw_array(irq, int, irq, NULL, 0444);
0058 MODULE_PARM_DESC(irq, "IRQ # for CMI8328 driver.");
0059 module_param_hw_array(dma1, int, dma, NULL, 0444);
0060 MODULE_PARM_DESC(dma1, "DMA1 for CMI8328 driver.");
0061 module_param_hw_array(dma2, int, dma, NULL, 0444);
0062 MODULE_PARM_DESC(dma2, "DMA2 for CMI8328 driver.");
0063 
0064 module_param_hw_array(mpuport, long, ioport, NULL, 0444);
0065 MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8328 driver.");
0066 module_param_hw_array(mpuirq, int, irq, NULL, 0444);
0067 MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8328 MPU-401 port.");
0068 #ifdef SUPPORT_JOYSTICK
0069 module_param_array(gameport, bool, NULL, 0444);
0070 MODULE_PARM_DESC(gameport, "Enable gameport.");
0071 #endif
0072 
0073 struct snd_cmi8328 {
0074     u16 port;
0075     u8 cfg[3];
0076     u8 wss_cfg;
0077     struct snd_card *card;
0078     struct snd_wss *wss;
0079 #ifdef SUPPORT_JOYSTICK
0080     struct gameport *gameport;
0081 #endif
0082 };
0083 
0084 /* CMI8328 configuration registers */
0085 #define CFG1 0x61
0086 #define CFG1_SB_DISABLE (1 << 0)
0087 #define CFG1_GAMEPORT   (1 << 1)
0088 /*
0089  * bit 0:    SB: 0=enabled, 1=disabled
0090  * bit 1:    gameport: 0=disabled, 1=enabled
0091  * bits 2-4: SB IRQ: 001=3, 010=5, 011=7, 100=9, 101=10, 110=11
0092  * bits 5-6: SB DMA: 00=disabled (when SB disabled), 01=DMA0, 10=DMA1, 11=DMA3
0093  * bit 7:    SB port: 0=0x220, 1=0x240
0094  */
0095 #define CFG2 0x62
0096 #define CFG2_MPU_ENABLE (1 << 2)
0097 /*
0098  * bits 0-1: CD-ROM mode: 00=disabled, 01=Panasonic, 10=Sony/Mitsumi/Wearnes,
0099               11=IDE
0100  * bit 2:    MPU401: 0=disabled, 1=enabled
0101  * bits 3-4: MPU401 IRQ: 00=3, 01=5, 10=7, 11=9,
0102  * bits 5-7: MPU401 port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x332,
0103               101=0x334, 110=0x336
0104  */
0105 #define CFG3 0x63
0106 /*
0107  * bits 0-2: CD-ROM IRQ: 000=disabled, 001=3, 010=5, 011=7, 100=9, 101=10,
0108              110=11
0109  * bits 3-4: CD-ROM DMA: 00=disabled, 01=DMA0, 10=DMA1, 11=DMA3
0110  * bits 5-7: CD-ROM port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x340,
0111               101=0x350, 110=0x360, 111=0x370
0112  */
0113 
0114 static u8 snd_cmi8328_cfg_read(u16 port, u8 reg)
0115 {
0116     outb(0x43, port + 3);
0117     outb(0x21, port + 3);
0118     outb(reg, port + 3);
0119     return inb(port);
0120 }
0121 
0122 static void snd_cmi8328_cfg_write(u16 port, u8 reg, u8 val)
0123 {
0124     outb(0x43, port + 3);
0125     outb(0x21, port + 3);
0126     outb(reg, port + 3);
0127     outb(val, port + 3);    /* yes, value goes to the same port as index */
0128 }
0129 
0130 #ifdef CONFIG_PM
0131 static void snd_cmi8328_cfg_save(u16 port, u8 cfg[])
0132 {
0133     cfg[0] = snd_cmi8328_cfg_read(port, CFG1);
0134     cfg[1] = snd_cmi8328_cfg_read(port, CFG2);
0135     cfg[2] = snd_cmi8328_cfg_read(port, CFG3);
0136 }
0137 
0138 static void snd_cmi8328_cfg_restore(u16 port, u8 cfg[])
0139 {
0140     snd_cmi8328_cfg_write(port, CFG1, cfg[0]);
0141     snd_cmi8328_cfg_write(port, CFG2, cfg[1]);
0142     snd_cmi8328_cfg_write(port, CFG3, cfg[2]);
0143 }
0144 #endif /* CONFIG_PM */
0145 
0146 static int snd_cmi8328_mixer(struct snd_wss *chip)
0147 {
0148     struct snd_card *card;
0149     struct snd_ctl_elem_id id1, id2;
0150     int err;
0151 
0152     card = chip->card;
0153 
0154     memset(&id1, 0, sizeof(id1));
0155     memset(&id2, 0, sizeof(id2));
0156     id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
0157     /* rename AUX0 switch to CD */
0158     strcpy(id1.name, "Aux Playback Switch");
0159     strcpy(id2.name, "CD Playback Switch");
0160     err = snd_ctl_rename_id(card, &id1, &id2);
0161     if (err < 0) {
0162         snd_printk(KERN_ERR "error renaming control\n");
0163         return err;
0164     }
0165     /* rename AUX0 volume to CD */
0166     strcpy(id1.name, "Aux Playback Volume");
0167     strcpy(id2.name, "CD Playback Volume");
0168     err = snd_ctl_rename_id(card, &id1, &id2);
0169     if (err < 0) {
0170         snd_printk(KERN_ERR "error renaming control\n");
0171         return err;
0172     }
0173     /* rename AUX1 switch to Synth */
0174     strcpy(id1.name, "Aux Playback Switch");
0175     id1.index = 1;
0176     strcpy(id2.name, "Synth Playback Switch");
0177     err = snd_ctl_rename_id(card, &id1, &id2);
0178     if (err < 0) {
0179         snd_printk(KERN_ERR "error renaming control\n");
0180         return err;
0181     }
0182     /* rename AUX1 volume to Synth */
0183     strcpy(id1.name, "Aux Playback Volume");
0184     id1.index = 1;
0185     strcpy(id2.name, "Synth Playback Volume");
0186     err = snd_ctl_rename_id(card, &id1, &id2);
0187     if (err < 0) {
0188         snd_printk(KERN_ERR "error renaming control\n");
0189         return err;
0190     }
0191 
0192     return 0;
0193 }
0194 
0195 /* find index of an item in "-1"-ended array */
0196 static int array_find(const int array[], int item)
0197 {
0198     int i;
0199 
0200     for (i = 0; array[i] != -1; i++)
0201         if (array[i] == item)
0202             return i;
0203 
0204     return -1;
0205 }
0206 /* the same for long */
0207 static int array_find_l(const long array[], long item)
0208 {
0209     int i;
0210 
0211     for (i = 0; array[i] != -1; i++)
0212         if (array[i] == item)
0213             return i;
0214 
0215     return -1;
0216 }
0217 
0218 static int snd_cmi8328_probe(struct device *pdev, unsigned int ndev)
0219 {
0220     struct snd_card *card;
0221     struct snd_opl3 *opl3;
0222     struct snd_cmi8328 *cmi;
0223 #ifdef SUPPORT_JOYSTICK
0224     struct resource *res;
0225 #endif
0226     int err, pos;
0227     static const long mpu_ports[] = { 0x330, 0x300, 0x310, 0x320, 0x332, 0x334,
0228                    0x336, -1 };
0229     static const u8 mpu_port_bits[] = { 3, 0, 1, 2, 4, 5, 6 };
0230     static const int mpu_irqs[] = { 9, 7, 5, 3, -1 };
0231     static const u8 mpu_irq_bits[] = { 3, 2, 1, 0 };
0232     static const int irqs[] = { 9, 10, 11, 7, -1 };
0233     static const u8 irq_bits[] = { 2, 3, 4, 1 };
0234     static const int dma1s[] = { 3, 1, 0, -1 };
0235     static const u8 dma_bits[] = { 3, 2, 1 };
0236     static const int dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} };
0237     u16 port = cmi8328_ports[ndev];
0238     u8 val;
0239 
0240     /* 0xff is invalid configuration (but settable - hope it isn't set) */
0241     if (snd_cmi8328_cfg_read(port, CFG1) == 0xff)
0242         return -ENODEV;
0243     /* the SB disable bit must NEVER EVER be cleared or the WSS dies */
0244     snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE);
0245     if (snd_cmi8328_cfg_read(port, CFG1) != CFG1_SB_DISABLE)
0246         return -ENODEV;
0247     /* disable everything first */
0248     snd_cmi8328_cfg_write(port, CFG2, 0);   /* disable CDROM and MPU401 */
0249     snd_cmi8328_cfg_write(port, CFG3, 0);   /* disable CDROM IRQ and DMA */
0250 
0251     if (irq[ndev] == SNDRV_AUTO_IRQ) {
0252         irq[ndev] = snd_legacy_find_free_irq(irqs);
0253         if (irq[ndev] < 0) {
0254             snd_printk(KERN_ERR "unable to find a free IRQ\n");
0255             return -EBUSY;
0256         }
0257     }
0258     if (dma1[ndev] == SNDRV_AUTO_DMA) {
0259         dma1[ndev] = snd_legacy_find_free_dma(dma1s);
0260         if (dma1[ndev] < 0) {
0261             snd_printk(KERN_ERR "unable to find a free DMA1\n");
0262             return -EBUSY;
0263         }
0264     }
0265     if (dma2[ndev] == SNDRV_AUTO_DMA) {
0266         dma2[ndev] = snd_legacy_find_free_dma(dma2s[dma1[ndev] % 4]);
0267         if (dma2[ndev] < 0) {
0268             snd_printk(KERN_WARNING "unable to find a free DMA2, full-duplex will not work\n");
0269             dma2[ndev] = -1;
0270         }
0271     }
0272     /* configure WSS IRQ... */
0273     pos = array_find(irqs, irq[ndev]);
0274     if (pos < 0) {
0275         snd_printk(KERN_ERR "invalid IRQ %d\n", irq[ndev]);
0276         return -EINVAL;
0277     }
0278     val = irq_bits[pos] << 3;
0279     /* ...and DMA... */
0280     pos = array_find(dma1s, dma1[ndev]);
0281     if (pos < 0) {
0282         snd_printk(KERN_ERR "invalid DMA1 %d\n", dma1[ndev]);
0283         return -EINVAL;
0284     }
0285     val |= dma_bits[pos];
0286     /* ...and DMA2 */
0287     if (dma2[ndev] >= 0 && dma1[ndev] != dma2[ndev]) {
0288         pos = array_find(dma2s[dma1[ndev]], dma2[ndev]);
0289         if (pos < 0) {
0290             snd_printk(KERN_ERR "invalid DMA2 %d\n", dma2[ndev]);
0291             return -EINVAL;
0292         }
0293         val |= 0x04; /* enable separate capture DMA */
0294     }
0295     outb(val, port);
0296 
0297     err = snd_devm_card_new(pdev, index[ndev], id[ndev], THIS_MODULE,
0298                 sizeof(struct snd_cmi8328), &card);
0299     if (err < 0)
0300         return err;
0301     cmi = card->private_data;
0302     cmi->card = card;
0303     cmi->port = port;
0304     cmi->wss_cfg = val;
0305 
0306     err = snd_wss_create(card, port + 4, -1, irq[ndev], dma1[ndev],
0307             dma2[ndev], WSS_HW_DETECT, 0, &cmi->wss);
0308     if (err < 0)
0309         return err;
0310 
0311     err = snd_wss_pcm(cmi->wss, 0);
0312     if (err < 0)
0313         return err;
0314 
0315     err = snd_wss_mixer(cmi->wss);
0316     if (err < 0)
0317         return err;
0318     err = snd_cmi8328_mixer(cmi->wss);
0319     if (err < 0)
0320         return err;
0321 
0322     if (snd_wss_timer(cmi->wss, 0) < 0)
0323         snd_printk(KERN_WARNING "error initializing WSS timer\n");
0324 
0325     if (mpuport[ndev] == SNDRV_AUTO_PORT) {
0326         mpuport[ndev] = snd_legacy_find_free_ioport(mpu_ports, 2);
0327         if (mpuport[ndev] < 0)
0328             snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
0329     }
0330     if (mpuirq[ndev] == SNDRV_AUTO_IRQ) {
0331         mpuirq[ndev] = snd_legacy_find_free_irq(mpu_irqs);
0332         if (mpuirq[ndev] < 0)
0333             snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
0334     }
0335     /* enable and configure MPU401 */
0336     if (mpuport[ndev] > 0 && mpuirq[ndev] > 0) {
0337         val = CFG2_MPU_ENABLE;
0338         pos = array_find_l(mpu_ports, mpuport[ndev]);
0339         if (pos < 0)
0340             snd_printk(KERN_WARNING "invalid MPU401 port 0x%lx\n",
0341                                 mpuport[ndev]);
0342         else {
0343             val |= mpu_port_bits[pos] << 5;
0344             pos = array_find(mpu_irqs, mpuirq[ndev]);
0345             if (pos < 0)
0346                 snd_printk(KERN_WARNING "invalid MPU401 IRQ %d\n",
0347                                 mpuirq[ndev]);
0348             else {
0349                 val |= mpu_irq_bits[pos] << 3;
0350                 snd_cmi8328_cfg_write(port, CFG2, val);
0351                 if (snd_mpu401_uart_new(card, 0,
0352                         MPU401_HW_MPU401, mpuport[ndev],
0353                         0, mpuirq[ndev], NULL) < 0)
0354                     snd_printk(KERN_ERR "error initializing MPU401\n");
0355             }
0356         }
0357     }
0358     /* OPL3 is hardwired to 0x388 and cannot be disabled */
0359     if (snd_opl3_create(card, 0x388, 0x38a, OPL3_HW_AUTO, 0, &opl3) < 0)
0360         snd_printk(KERN_ERR "error initializing OPL3\n");
0361     else
0362         if (snd_opl3_hwdep_new(opl3, 0, 1, NULL) < 0)
0363             snd_printk(KERN_WARNING "error initializing OPL3 hwdep\n");
0364 
0365     strcpy(card->driver, "CMI8328");
0366     strcpy(card->shortname, "C-Media CMI8328");
0367     sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d,%d",
0368         card->shortname, cmi->wss->port, irq[ndev], dma1[ndev],
0369         (dma2[ndev] >= 0) ? dma2[ndev] : dma1[ndev]);
0370 
0371     dev_set_drvdata(pdev, card);
0372     err = snd_card_register(card);
0373     if (err < 0)
0374         return err;
0375 #ifdef SUPPORT_JOYSTICK
0376     if (!gameport[ndev])
0377         return 0;
0378     /* gameport is hardwired to 0x200 */
0379     res = devm_request_region(pdev, 0x200, 8, "CMI8328 gameport");
0380     if (!res)
0381         snd_printk(KERN_WARNING "unable to allocate gameport I/O port\n");
0382     else {
0383         struct gameport *gp = cmi->gameport = gameport_allocate_port();
0384         if (cmi->gameport) {
0385             gameport_set_name(gp, "CMI8328 Gameport");
0386             gameport_set_phys(gp, "%s/gameport0", dev_name(pdev));
0387             gameport_set_dev_parent(gp, pdev);
0388             gp->io = 0x200;
0389             /* Enable gameport */
0390             snd_cmi8328_cfg_write(port, CFG1,
0391                     CFG1_SB_DISABLE | CFG1_GAMEPORT);
0392             gameport_register_port(gp);
0393         }
0394     }
0395 #endif
0396     return 0;
0397 }
0398 
0399 static void snd_cmi8328_remove(struct device *pdev, unsigned int dev)
0400 {
0401     struct snd_card *card = dev_get_drvdata(pdev);
0402     struct snd_cmi8328 *cmi = card->private_data;
0403 
0404 #ifdef SUPPORT_JOYSTICK
0405     if (cmi->gameport)
0406         gameport_unregister_port(cmi->gameport);
0407 #endif
0408     /* disable everything */
0409     snd_cmi8328_cfg_write(cmi->port, CFG1, CFG1_SB_DISABLE);
0410     snd_cmi8328_cfg_write(cmi->port, CFG2, 0);
0411     snd_cmi8328_cfg_write(cmi->port, CFG3, 0);
0412 }
0413 
0414 #ifdef CONFIG_PM
0415 static int snd_cmi8328_suspend(struct device *pdev, unsigned int n,
0416                 pm_message_t state)
0417 {
0418     struct snd_card *card = dev_get_drvdata(pdev);
0419     struct snd_cmi8328 *cmi;
0420 
0421     if (!card)  /* ignore absent devices */
0422         return 0;
0423     cmi = card->private_data;
0424     snd_cmi8328_cfg_save(cmi->port, cmi->cfg);
0425     snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
0426     cmi->wss->suspend(cmi->wss);
0427 
0428     return 0;
0429 }
0430 
0431 static int snd_cmi8328_resume(struct device *pdev, unsigned int n)
0432 {
0433     struct snd_card *card = dev_get_drvdata(pdev);
0434     struct snd_cmi8328 *cmi;
0435 
0436     if (!card)  /* ignore absent devices */
0437         return 0;
0438     cmi = card->private_data;
0439     snd_cmi8328_cfg_restore(cmi->port, cmi->cfg);
0440     outb(cmi->wss_cfg, cmi->port);
0441     cmi->wss->resume(cmi->wss);
0442     snd_power_change_state(card, SNDRV_CTL_POWER_D0);
0443 
0444     return 0;
0445 }
0446 #endif
0447 
0448 static struct isa_driver snd_cmi8328_driver = {
0449     .probe      = snd_cmi8328_probe,
0450     .remove     = snd_cmi8328_remove,
0451 #ifdef CONFIG_PM
0452     .suspend    = snd_cmi8328_suspend,
0453     .resume     = snd_cmi8328_resume,
0454 #endif
0455     .driver     = {
0456         .name   = "cmi8328"
0457     },
0458 };
0459 
0460 module_isa_driver(snd_cmi8328_driver, CMI8328_MAX);