1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for C-Media CMI8328-based soundcards, such as AudioExcel AV500
4 * Copyright (c) 2012 Ondrej Zary
6 * AudioExcel AV500 card consists of:
7 * - CMI8328 - main chip (SB Pro emulation, gameport, OPL3, MPU401, CD-ROM)
8 * - CS4231A - WSS codec
9 * - Dream SAM9233+GMS950400+RAM+ROM: Wavetable MIDI, connected to MPU401
12 #include <linux/init.h>
13 #include <linux/isa.h>
14 #include <linux/module.h>
15 #include <linux/gameport.h>
17 #include <sound/core.h>
18 #include <sound/wss.h>
19 #include <sound/opl3.h>
20 #include <sound/mpu401.h>
21 #define SNDRV_LEGACY_FIND_FREE_IOPORT
22 #define SNDRV_LEGACY_FIND_FREE_IRQ
23 #define SNDRV_LEGACY_FIND_FREE_DMA
24 #include <sound/initval.h>
26 MODULE_AUTHOR("Ondrej Zary <linux@rainbow-software.org>");
27 MODULE_DESCRIPTION("C-Media CMI8328");
28 MODULE_LICENSE("GPL");
30 #if IS_ENABLED(CONFIG_GAMEPORT)
31 #define SUPPORT_JOYSTICK 1
34 /* I/O port is configured by jumpers on the card to one of these */
35 static const int cmi8328_ports[] = { 0x530, 0xe80, 0xf40, 0x604 };
36 #define CMI8328_MAX ARRAY_SIZE(cmi8328_ports)
38 static int index[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = -1};
39 static char *id[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = NULL};
40 static long port[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
41 static int irq[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
42 static int dma1[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
43 static int dma2[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
44 static long mpuport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
45 static int mpuirq[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
46 #ifdef SUPPORT_JOYSTICK
47 static bool gameport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = true};
50 module_param_array(index, int, NULL, 0444);
51 MODULE_PARM_DESC(index, "Index value for CMI8328 soundcard.");
52 module_param_array(id, charp, NULL, 0444);
53 MODULE_PARM_DESC(id, "ID string for CMI8328 soundcard.");
55 module_param_hw_array(port, long, ioport, NULL, 0444);
56 MODULE_PARM_DESC(port, "Port # for CMI8328 driver.");
57 module_param_hw_array(irq, int, irq, NULL, 0444);
58 MODULE_PARM_DESC(irq, "IRQ # for CMI8328 driver.");
59 module_param_hw_array(dma1, int, dma, NULL, 0444);
60 MODULE_PARM_DESC(dma1, "DMA1 for CMI8328 driver.");
61 module_param_hw_array(dma2, int, dma, NULL, 0444);
62 MODULE_PARM_DESC(dma2, "DMA2 for CMI8328 driver.");
64 module_param_hw_array(mpuport, long, ioport, NULL, 0444);
65 MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8328 driver.");
66 module_param_hw_array(mpuirq, int, irq, NULL, 0444);
67 MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8328 MPU-401 port.");
68 #ifdef SUPPORT_JOYSTICK
69 module_param_array(gameport, bool, NULL, 0444);
70 MODULE_PARM_DESC(gameport, "Enable gameport.");
77 struct snd_card *card;
79 #ifdef SUPPORT_JOYSTICK
80 struct gameport *gameport;
84 /* CMI8328 configuration registers */
86 #define CFG1_SB_DISABLE (1 << 0)
87 #define CFG1_GAMEPORT (1 << 1)
89 * bit 0: SB: 0=enabled, 1=disabled
90 * bit 1: gameport: 0=disabled, 1=enabled
91 * bits 2-4: SB IRQ: 001=3, 010=5, 011=7, 100=9, 101=10, 110=11
92 * bits 5-6: SB DMA: 00=disabled (when SB disabled), 01=DMA0, 10=DMA1, 11=DMA3
93 * bit 7: SB port: 0=0x220, 1=0x240
96 #define CFG2_MPU_ENABLE (1 << 2)
98 * bits 0-1: CD-ROM mode: 00=disabled, 01=Panasonic, 10=Sony/Mitsumi/Wearnes,
100 * bit 2: MPU401: 0=disabled, 1=enabled
101 * bits 3-4: MPU401 IRQ: 00=3, 01=5, 10=7, 11=9,
102 * bits 5-7: MPU401 port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x332,
107 * bits 0-2: CD-ROM IRQ: 000=disabled, 001=3, 010=5, 011=7, 100=9, 101=10,
109 * bits 3-4: CD-ROM DMA: 00=disabled, 01=DMA0, 10=DMA1, 11=DMA3
110 * bits 5-7: CD-ROM port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x340,
111 101=0x350, 110=0x360, 111=0x370
114 static u8 snd_cmi8328_cfg_read(u16 port, u8 reg)
116 outb(0x43, port + 3);
117 outb(0x21, port + 3);
122 static void snd_cmi8328_cfg_write(u16 port, u8 reg, u8 val)
124 outb(0x43, port + 3);
125 outb(0x21, port + 3);
127 outb(val, port + 3); /* yes, value goes to the same port as index */
131 static void snd_cmi8328_cfg_save(u16 port, u8 cfg[])
133 cfg[0] = snd_cmi8328_cfg_read(port, CFG1);
134 cfg[1] = snd_cmi8328_cfg_read(port, CFG2);
135 cfg[2] = snd_cmi8328_cfg_read(port, CFG3);
138 static void snd_cmi8328_cfg_restore(u16 port, u8 cfg[])
140 snd_cmi8328_cfg_write(port, CFG1, cfg[0]);
141 snd_cmi8328_cfg_write(port, CFG2, cfg[1]);
142 snd_cmi8328_cfg_write(port, CFG3, cfg[2]);
144 #endif /* CONFIG_PM */
146 static int snd_cmi8328_mixer(struct snd_wss *chip)
148 struct snd_card *card;
149 struct snd_ctl_elem_id id1, id2;
154 memset(&id1, 0, sizeof(id1));
155 memset(&id2, 0, sizeof(id2));
156 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
157 /* rename AUX0 switch to CD */
158 strcpy(id1.name, "Aux Playback Switch");
159 strcpy(id2.name, "CD Playback Switch");
160 err = snd_ctl_rename_id(card, &id1, &id2);
162 snd_printk(KERN_ERR "error renaming control\n");
165 /* rename AUX0 volume to CD */
166 strcpy(id1.name, "Aux Playback Volume");
167 strcpy(id2.name, "CD Playback Volume");
168 err = snd_ctl_rename_id(card, &id1, &id2);
170 snd_printk(KERN_ERR "error renaming control\n");
173 /* rename AUX1 switch to Synth */
174 strcpy(id1.name, "Aux Playback Switch");
176 strcpy(id2.name, "Synth Playback Switch");
177 err = snd_ctl_rename_id(card, &id1, &id2);
179 snd_printk(KERN_ERR "error renaming control\n");
182 /* rename AUX1 volume to Synth */
183 strcpy(id1.name, "Aux Playback Volume");
185 strcpy(id2.name, "Synth Playback Volume");
186 err = snd_ctl_rename_id(card, &id1, &id2);
188 snd_printk(KERN_ERR "error renaming control\n");
195 /* find index of an item in "-1"-ended array */
196 static int array_find(const int array[], int item)
200 for (i = 0; array[i] != -1; i++)
201 if (array[i] == item)
206 /* the same for long */
207 static int array_find_l(const long array[], long item)
211 for (i = 0; array[i] != -1; i++)
212 if (array[i] == item)
218 static int snd_cmi8328_probe(struct device *pdev, unsigned int ndev)
220 struct snd_card *card;
221 struct snd_opl3 *opl3;
222 struct snd_cmi8328 *cmi;
223 #ifdef SUPPORT_JOYSTICK
224 struct resource *res;
227 static const long mpu_ports[] = { 0x330, 0x300, 0x310, 0x320, 0x332, 0x334,
229 static const u8 mpu_port_bits[] = { 3, 0, 1, 2, 4, 5, 6 };
230 static const int mpu_irqs[] = { 9, 7, 5, 3, -1 };
231 static const u8 mpu_irq_bits[] = { 3, 2, 1, 0 };
232 static const int irqs[] = { 9, 10, 11, 7, -1 };
233 static const u8 irq_bits[] = { 2, 3, 4, 1 };
234 static const int dma1s[] = { 3, 1, 0, -1 };
235 static const u8 dma_bits[] = { 3, 2, 1 };
236 static const int dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} };
237 u16 port = cmi8328_ports[ndev];
240 /* 0xff is invalid configuration (but settable - hope it isn't set) */
241 if (snd_cmi8328_cfg_read(port, CFG1) == 0xff)
243 /* the SB disable bit must NEVER EVER be cleared or the WSS dies */
244 snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE);
245 if (snd_cmi8328_cfg_read(port, CFG1) != CFG1_SB_DISABLE)
247 /* disable everything first */
248 snd_cmi8328_cfg_write(port, CFG2, 0); /* disable CDROM and MPU401 */
249 snd_cmi8328_cfg_write(port, CFG3, 0); /* disable CDROM IRQ and DMA */
251 if (irq[ndev] == SNDRV_AUTO_IRQ) {
252 irq[ndev] = snd_legacy_find_free_irq(irqs);
254 snd_printk(KERN_ERR "unable to find a free IRQ\n");
258 if (dma1[ndev] == SNDRV_AUTO_DMA) {
259 dma1[ndev] = snd_legacy_find_free_dma(dma1s);
260 if (dma1[ndev] < 0) {
261 snd_printk(KERN_ERR "unable to find a free DMA1\n");
265 if (dma2[ndev] == SNDRV_AUTO_DMA) {
266 dma2[ndev] = snd_legacy_find_free_dma(dma2s[dma1[ndev] % 4]);
267 if (dma2[ndev] < 0) {
268 snd_printk(KERN_WARNING "unable to find a free DMA2, full-duplex will not work\n");
272 /* configure WSS IRQ... */
273 pos = array_find(irqs, irq[ndev]);
275 snd_printk(KERN_ERR "invalid IRQ %d\n", irq[ndev]);
278 val = irq_bits[pos] << 3;
280 pos = array_find(dma1s, dma1[ndev]);
282 snd_printk(KERN_ERR "invalid DMA1 %d\n", dma1[ndev]);
285 val |= dma_bits[pos];
287 if (dma2[ndev] >= 0 && dma1[ndev] != dma2[ndev]) {
288 pos = array_find(dma2s[dma1[ndev]], dma2[ndev]);
290 snd_printk(KERN_ERR "invalid DMA2 %d\n", dma2[ndev]);
293 val |= 0x04; /* enable separate capture DMA */
297 err = snd_card_new(pdev, index[ndev], id[ndev], THIS_MODULE,
298 sizeof(struct snd_cmi8328), &card);
301 cmi = card->private_data;
306 err = snd_wss_create(card, port + 4, -1, irq[ndev], dma1[ndev],
307 dma2[ndev], WSS_HW_DETECT, 0, &cmi->wss);
311 err = snd_wss_pcm(cmi->wss, 0);
315 err = snd_wss_mixer(cmi->wss);
318 err = snd_cmi8328_mixer(cmi->wss);
322 if (snd_wss_timer(cmi->wss, 0) < 0)
323 snd_printk(KERN_WARNING "error initializing WSS timer\n");
325 if (mpuport[ndev] == SNDRV_AUTO_PORT) {
326 mpuport[ndev] = snd_legacy_find_free_ioport(mpu_ports, 2);
327 if (mpuport[ndev] < 0)
328 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
330 if (mpuirq[ndev] == SNDRV_AUTO_IRQ) {
331 mpuirq[ndev] = snd_legacy_find_free_irq(mpu_irqs);
332 if (mpuirq[ndev] < 0)
333 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
335 /* enable and configure MPU401 */
336 if (mpuport[ndev] > 0 && mpuirq[ndev] > 0) {
337 val = CFG2_MPU_ENABLE;
338 pos = array_find_l(mpu_ports, mpuport[ndev]);
340 snd_printk(KERN_WARNING "invalid MPU401 port 0x%lx\n",
343 val |= mpu_port_bits[pos] << 5;
344 pos = array_find(mpu_irqs, mpuirq[ndev]);
346 snd_printk(KERN_WARNING "invalid MPU401 IRQ %d\n",
349 val |= mpu_irq_bits[pos] << 3;
350 snd_cmi8328_cfg_write(port, CFG2, val);
351 if (snd_mpu401_uart_new(card, 0,
352 MPU401_HW_MPU401, mpuport[ndev],
353 0, mpuirq[ndev], NULL) < 0)
354 snd_printk(KERN_ERR "error initializing MPU401\n");
358 /* OPL3 is hardwired to 0x388 and cannot be disabled */
359 if (snd_opl3_create(card, 0x388, 0x38a, OPL3_HW_AUTO, 0, &opl3) < 0)
360 snd_printk(KERN_ERR "error initializing OPL3\n");
362 if (snd_opl3_hwdep_new(opl3, 0, 1, NULL) < 0)
363 snd_printk(KERN_WARNING "error initializing OPL3 hwdep\n");
365 strcpy(card->driver, "CMI8328");
366 strcpy(card->shortname, "C-Media CMI8328");
367 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d,%d",
368 card->shortname, cmi->wss->port, irq[ndev], dma1[ndev],
369 (dma2[ndev] >= 0) ? dma2[ndev] : dma1[ndev]);
371 dev_set_drvdata(pdev, card);
372 err = snd_card_register(card);
375 #ifdef SUPPORT_JOYSTICK
378 /* gameport is hardwired to 0x200 */
379 res = request_region(0x200, 8, "CMI8328 gameport");
381 snd_printk(KERN_WARNING "unable to allocate gameport I/O port\n");
383 struct gameport *gp = cmi->gameport = gameport_allocate_port();
385 release_and_free_resource(res);
387 gameport_set_name(gp, "CMI8328 Gameport");
388 gameport_set_phys(gp, "%s/gameport0", dev_name(pdev));
389 gameport_set_dev_parent(gp, pdev);
391 gameport_set_port_data(gp, res);
392 /* Enable gameport */
393 snd_cmi8328_cfg_write(port, CFG1,
394 CFG1_SB_DISABLE | CFG1_GAMEPORT);
395 gameport_register_port(gp);
406 static int snd_cmi8328_remove(struct device *pdev, unsigned int dev)
408 struct snd_card *card = dev_get_drvdata(pdev);
409 struct snd_cmi8328 *cmi = card->private_data;
411 #ifdef SUPPORT_JOYSTICK
413 struct resource *res = gameport_get_port_data(cmi->gameport);
414 gameport_unregister_port(cmi->gameport);
415 release_and_free_resource(res);
418 /* disable everything */
419 snd_cmi8328_cfg_write(cmi->port, CFG1, CFG1_SB_DISABLE);
420 snd_cmi8328_cfg_write(cmi->port, CFG2, 0);
421 snd_cmi8328_cfg_write(cmi->port, CFG3, 0);
427 static int snd_cmi8328_suspend(struct device *pdev, unsigned int n,
430 struct snd_card *card = dev_get_drvdata(pdev);
431 struct snd_cmi8328 *cmi;
433 if (!card) /* ignore absent devices */
435 cmi = card->private_data;
436 snd_cmi8328_cfg_save(cmi->port, cmi->cfg);
437 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
438 cmi->wss->suspend(cmi->wss);
443 static int snd_cmi8328_resume(struct device *pdev, unsigned int n)
445 struct snd_card *card = dev_get_drvdata(pdev);
446 struct snd_cmi8328 *cmi;
448 if (!card) /* ignore absent devices */
450 cmi = card->private_data;
451 snd_cmi8328_cfg_restore(cmi->port, cmi->cfg);
452 outb(cmi->wss_cfg, cmi->port);
453 cmi->wss->resume(cmi->wss);
454 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
460 static struct isa_driver snd_cmi8328_driver = {
461 .probe = snd_cmi8328_probe,
462 .remove = snd_cmi8328_remove,
464 .suspend = snd_cmi8328_suspend,
465 .resume = snd_cmi8328_resume,
472 module_isa_driver(snd_cmi8328_driver, CMI8328_MAX);