1 // SPDX-License-Identifier: GPL-2.0-or-later
3 card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
4 Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
6 Part of this code was developed at the Italian Ministry of Air Defence,
7 Sixth Division (oh, che pace ...), Rome.
9 Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/isa.h>
17 #include <linux/delay.h>
18 #include <linux/pnp.h>
19 #include <linux/module.h>
22 #include <sound/core.h>
23 #include <sound/tlv.h>
24 #include <sound/wss.h>
25 #include <sound/mpu401.h>
26 #include <sound/opl3.h>
28 #include <sound/opl4.h>
30 #define SNDRV_LEGACY_FIND_FREE_IOPORT
31 #define SNDRV_LEGACY_FIND_FREE_IRQ
32 #define SNDRV_LEGACY_FIND_FREE_DMA
33 #include <sound/initval.h>
35 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
36 MODULE_LICENSE("GPL");
38 MODULE_DESCRIPTION("OPTi93X");
39 MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
42 MODULE_DESCRIPTION("OPTi92X - CS4231");
43 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
44 "{OPTi,82C925 (CS4231)}}");
46 MODULE_DESCRIPTION("OPTi92X - AD1848");
47 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
48 "{OPTi,82C925 (AD1848)},"
53 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
54 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
55 //static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
57 static bool isapnp = true; /* Enable ISA PnP detection */
59 static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
60 static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
61 static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
62 static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
63 static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
64 static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
65 #if defined(CS4231) || defined(OPTi93X)
66 static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
67 #endif /* CS4231 || OPTi93X */
69 module_param(index, int, 0444);
70 MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
71 module_param(id, charp, 0444);
72 MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
73 //module_param(enable, bool, 0444);
74 //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
76 module_param(isapnp, bool, 0444);
77 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
79 module_param_hw(port, long, ioport, 0444);
80 MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
81 module_param_hw(mpu_port, long, ioport, 0444);
82 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
83 module_param_hw(fm_port, long, ioport, 0444);
84 MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
85 module_param_hw(irq, int, irq, 0444);
86 MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
87 module_param_hw(mpu_irq, int, irq, 0444);
88 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
89 module_param_hw(dma1, int, dma, 0444);
90 MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
91 #if defined(CS4231) || defined(OPTi93X)
92 module_param_hw(dma2, int, dma, 0444);
93 MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
94 #endif /* CS4231 || OPTi93X */
96 #define OPTi9XX_HW_82C928 1
97 #define OPTi9XX_HW_82C929 2
98 #define OPTi9XX_HW_82C924 3
99 #define OPTi9XX_HW_82C925 4
100 #define OPTi9XX_HW_82C930 5
101 #define OPTi9XX_HW_82C931 6
102 #define OPTi9XX_HW_82C933 7
103 #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
105 #define OPTi9XX_MC_REG(n) n
109 #define OPTi93X_STATUS 0x02
110 #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
112 #define OPTi93X_IRQ_PLAYBACK 0x04
113 #define OPTi93X_IRQ_CAPTURE 0x08
118 unsigned short hardware;
119 unsigned char password;
122 unsigned long mc_base;
123 struct resource *res_mc_base;
124 unsigned long mc_base_size;
126 unsigned long mc_indir_index;
127 struct resource *res_mc_indir;
129 struct snd_wss *codec;
130 unsigned long pwd_reg;
138 static int snd_opti9xx_pnp_is_probed;
142 static const struct pnp_card_device_id snd_opti9xx_pnpids[] = {
146 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } },
147 .driver_data = 0x0924 },
150 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } },
151 .driver_data = 0x0925 },
154 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } },
155 .driver_data = 0x0931 },
160 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
162 #endif /* CONFIG_PNP */
164 #define DEV_NAME KBUILD_MODNAME
166 static const char * const snd_opti9xx_names[] = {
170 "82C930", "82C931", "82C933"
173 static int snd_opti9xx_init(struct snd_opti9xx *chip,
174 unsigned short hardware)
176 static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
178 chip->hardware = hardware;
179 strcpy(chip->name, snd_opti9xx_names[hardware]);
181 spin_lock_init(&chip->lock);
187 if (isapnp && chip->mc_base)
188 /* PnP resource gives the least 10 bits */
189 chip->mc_base |= 0xc00;
191 #endif /* CONFIG_PNP */
193 chip->mc_base = 0xf8c;
194 chip->mc_base_size = opti9xx_mc_size[hardware];
197 chip->mc_base_size = opti9xx_mc_size[hardware];
202 case OPTi9XX_HW_82C928:
203 case OPTi9XX_HW_82C929:
204 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
208 case OPTi9XX_HW_82C924:
209 case OPTi9XX_HW_82C925:
210 chip->password = 0xe5;
215 case OPTi9XX_HW_82C930:
216 case OPTi9XX_HW_82C931:
217 case OPTi9XX_HW_82C933:
218 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
219 if (!chip->mc_indir_index)
220 chip->mc_indir_index = 0xe0e;
221 chip->password = 0xe4;
227 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
233 static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
237 unsigned char retval = 0xff;
239 spin_lock_irqsave(&chip->lock, flags);
240 outb(chip->password, chip->mc_base + chip->pwd_reg);
242 switch (chip->hardware) {
244 case OPTi9XX_HW_82C924:
245 case OPTi9XX_HW_82C925:
247 outb(reg, chip->mc_base + 8);
248 outb(chip->password, chip->mc_base + chip->pwd_reg);
249 retval = inb(chip->mc_base + 9);
254 case OPTi9XX_HW_82C928:
255 case OPTi9XX_HW_82C929:
256 retval = inb(chip->mc_base + reg);
260 case OPTi9XX_HW_82C930:
261 case OPTi9XX_HW_82C931:
262 case OPTi9XX_HW_82C933:
263 outb(reg, chip->mc_indir_index);
264 outb(chip->password, chip->mc_base + chip->pwd_reg);
265 retval = inb(chip->mc_indir_index + 1);
270 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
273 spin_unlock_irqrestore(&chip->lock, flags);
277 static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
282 spin_lock_irqsave(&chip->lock, flags);
283 outb(chip->password, chip->mc_base + chip->pwd_reg);
285 switch (chip->hardware) {
287 case OPTi9XX_HW_82C924:
288 case OPTi9XX_HW_82C925:
290 outb(reg, chip->mc_base + 8);
291 outb(chip->password, chip->mc_base + chip->pwd_reg);
292 outb(value, chip->mc_base + 9);
297 case OPTi9XX_HW_82C928:
298 case OPTi9XX_HW_82C929:
299 outb(value, chip->mc_base + reg);
303 case OPTi9XX_HW_82C930:
304 case OPTi9XX_HW_82C931:
305 case OPTi9XX_HW_82C933:
306 outb(reg, chip->mc_indir_index);
307 outb(chip->password, chip->mc_base + chip->pwd_reg);
308 outb(value, chip->mc_indir_index + 1);
313 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
316 spin_unlock_irqrestore(&chip->lock, flags);
320 static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip,
321 unsigned char reg, unsigned char value, unsigned char mask)
323 unsigned char oldval = snd_opti9xx_read(chip, reg);
325 snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask));
328 static int snd_opti9xx_configure(struct snd_opti9xx *chip,
330 int irq, int dma1, int dma2,
331 long mpu_port, int mpu_irq)
333 unsigned char wss_base_bits;
334 unsigned char irq_bits;
335 unsigned char dma_bits;
336 unsigned char mpu_port_bits = 0;
337 unsigned char mpu_irq_bits;
339 switch (chip->hardware) {
341 case OPTi9XX_HW_82C924:
342 /* opti 929 mode (?), OPL3 clock output, audio enable */
343 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
344 /* enable wave audio */
345 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
348 case OPTi9XX_HW_82C925:
349 /* enable WSS mode */
350 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
351 /* OPL3 FM synthesis */
352 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
353 /* disable Sound Blaster IRQ and DMA */
354 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
356 /* cs4231/4248 fix enabled */
357 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
359 /* cs4231/4248 fix disabled */
360 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
364 case OPTi9XX_HW_82C928:
365 case OPTi9XX_HW_82C929:
366 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
367 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
369 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
371 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
373 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
375 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
380 case OPTi9XX_HW_82C931:
381 /* disable 3D sound (set GPIO1 as output, low) */
382 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
385 case OPTi9XX_HW_82C933:
387 * The BTC 1817DW has QS1000 wavetable which is connected
388 * to the serial digital input of the OPTI931.
390 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
392 * This bit sets OPTI931 to automaticaly select FM
393 * or digital input signal.
395 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
398 case OPTi9XX_HW_82C930:
399 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
400 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
401 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
402 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
404 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
409 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
413 /* PnP resource says it decodes only 10 bits of address */
414 switch (port & 0x3ff) {
416 chip->wss_base = 0x530;
417 wss_base_bits = 0x00;
420 chip->wss_base = 0x604;
421 wss_base_bits = 0x03;
424 chip->wss_base = 0xe80;
425 wss_base_bits = 0x01;
428 chip->wss_base = 0xf40;
429 wss_base_bits = 0x02;
432 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
435 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
443 //#endif /* OPTi93X */
457 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
458 goto __skip_resources;
472 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
473 goto __skip_resources;
476 #if defined(CS4231) || defined(OPTi93X)
478 snd_printk(KERN_ERR "don't want to share dmas\n");
487 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
488 goto __skip_resources;
491 #endif /* CS4231 || OPTi93X */
494 outb(irq_bits << 3 | dma_bits, chip->wss_base);
496 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
500 if (chip->hardware > OPTi9XX_HW_82C928) {
506 mpu_port_bits = 0x03;
509 mpu_port_bits = 0x02;
512 mpu_port_bits = 0x01;
515 mpu_port_bits = 0x00;
518 snd_printk(KERN_WARNING
519 "MPU-401 port 0x%lx not valid\n", mpu_port);
537 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
542 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
543 (mpu_port <= 0) ? 0x00 :
544 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
554 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
555 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
556 static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
558 static const struct snd_kcontrol_new snd_opti93x_controls[] = {
559 WSS_DOUBLE("Master Playback Switch", 0,
560 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
561 WSS_DOUBLE_TLV("Master Playback Volume", 0,
562 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
563 db_scale_5bit_3db_step),
564 WSS_DOUBLE_TLV("PCM Playback Volume", 0,
565 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
567 WSS_DOUBLE_TLV("FM Playback Volume", 0,
568 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
569 db_scale_4bit_12db_max),
570 WSS_DOUBLE("Line Playback Switch", 0,
571 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
572 WSS_DOUBLE_TLV("Line Playback Volume", 0,
573 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
574 db_scale_4bit_12db_max),
575 WSS_DOUBLE("Mic Playback Switch", 0,
576 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
577 WSS_DOUBLE_TLV("Mic Playback Volume", 0,
578 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
579 db_scale_4bit_12db_max),
580 WSS_DOUBLE_TLV("CD Playback Volume", 0,
581 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
582 db_scale_4bit_12db_max),
583 WSS_DOUBLE("Aux Playback Switch", 0,
584 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
585 WSS_DOUBLE_TLV("Aux Playback Volume", 0,
586 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
587 db_scale_4bit_12db_max),
590 static int snd_opti93x_mixer(struct snd_wss *chip)
592 struct snd_card *card;
594 struct snd_ctl_elem_id id1, id2;
597 if (snd_BUG_ON(!chip || !chip->pcm))
602 strcpy(card->mixername, chip->pcm->name);
604 memset(&id1, 0, sizeof(id1));
605 memset(&id2, 0, sizeof(id2));
606 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
607 /* reassign AUX0 switch to CD */
608 strcpy(id1.name, "Aux Playback Switch");
609 strcpy(id2.name, "CD Playback Switch");
610 err = snd_ctl_rename_id(card, &id1, &id2);
612 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
615 /* reassign AUX1 switch to FM */
616 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
617 strcpy(id2.name, "FM Playback Switch");
618 err = snd_ctl_rename_id(card, &id1, &id2);
620 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
623 /* remove AUX1 volume */
624 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
625 snd_ctl_remove_id(card, &id1);
627 /* Replace WSS volume controls with OPTi93x volume controls */
629 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
630 strcpy(id1.name, snd_opti93x_controls[idx].name);
631 snd_ctl_remove_id(card, &id1);
633 err = snd_ctl_add(card,
634 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
641 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
643 struct snd_opti9xx *chip = dev_id;
644 struct snd_wss *codec = chip->codec;
645 unsigned char status;
650 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
651 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
652 snd_pcm_period_elapsed(codec->playback_substream);
653 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
654 snd_wss_overrange(codec);
655 snd_pcm_period_elapsed(codec->capture_substream);
657 outb(0x00, OPTi93X_PORT(codec, STATUS));
663 static int snd_opti9xx_read_check(struct snd_opti9xx *chip)
670 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
672 if (chip->res_mc_base == NULL)
675 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
676 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
677 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
680 chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
682 if (chip->res_mc_indir == NULL)
685 spin_lock_irqsave(&chip->lock, flags);
686 outb(chip->password, chip->mc_base + chip->pwd_reg);
687 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
688 spin_unlock_irqrestore(&chip->lock, flags);
690 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
691 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
692 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
695 release_and_free_resource(chip->res_mc_indir);
696 chip->res_mc_indir = NULL;
698 release_and_free_resource(chip->res_mc_base);
699 chip->res_mc_base = NULL;
704 static int snd_card_opti9xx_detect(struct snd_card *card,
705 struct snd_opti9xx *chip)
710 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
712 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
714 err = snd_opti9xx_init(chip, i);
718 err = snd_opti9xx_read_check(chip);
722 chip->mc_indir_index = 0;
729 static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
730 struct pnp_card_link *card,
731 const struct pnp_card_device_id *pid)
733 struct pnp_dev *pdev;
735 struct pnp_dev *devmpu;
737 struct pnp_dev *devmc;
740 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
744 err = pnp_activate_dev(pdev);
746 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
751 port = pnp_port_start(pdev, 0) - 4;
752 fm_port = pnp_port_start(pdev, 1) + 8;
753 /* adjust mc_indir_index - some cards report it at 0xe?d,
754 other at 0xe?c but it really is always at 0xe?e */
755 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
757 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
761 err = pnp_activate_dev(devmc);
763 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
767 port = pnp_port_start(pdev, 1);
768 fm_port = pnp_port_start(pdev, 2) + 8;
770 * The MC(0) is never accessed and card does not
771 * include it in the PnP resource range. OPTI93x include it.
773 chip->mc_base = pnp_port_start(devmc, 0) - 1;
774 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
776 irq = pnp_irq(pdev, 0);
777 dma1 = pnp_dma(pdev, 0);
778 #if defined(CS4231) || defined(OPTi93X)
779 dma2 = pnp_dma(pdev, 1);
780 #endif /* CS4231 || OPTi93X */
782 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
784 if (devmpu && mpu_port > 0) {
785 err = pnp_activate_dev(devmpu);
787 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
790 mpu_port = pnp_port_start(devmpu, 0);
791 mpu_irq = pnp_irq(devmpu, 0);
794 return pid->driver_data;
796 #endif /* CONFIG_PNP */
798 static void snd_card_opti9xx_free(struct snd_card *card)
800 struct snd_opti9xx *chip = card->private_data;
805 disable_irq(chip->irq);
806 free_irq(chip->irq, chip);
808 release_and_free_resource(chip->res_mc_indir);
810 release_and_free_resource(chip->res_mc_base);
814 static int snd_opti9xx_probe(struct snd_card *card)
816 static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
819 struct snd_opti9xx *chip = card->private_data;
820 struct snd_wss *codec;
821 struct snd_rawmidi *rmidi;
822 struct snd_hwdep *synth;
824 #if defined(CS4231) || defined(OPTi93X)
830 if (port == SNDRV_AUTO_PORT) {
831 port = snd_legacy_find_free_ioport(possible_ports, 4);
833 snd_printk(KERN_ERR "unable to find a free WSS port\n");
837 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
842 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
844 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
852 error = snd_wss_pcm(codec, 0);
855 error = snd_wss_mixer(codec);
859 error = snd_opti93x_mixer(codec);
864 error = snd_wss_timer(codec, 0);
869 error = request_irq(irq, snd_opti93x_interrupt,
870 0, DEV_NAME" - WSS", chip);
872 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
877 card->sync_irq = chip->irq;
878 strcpy(card->driver, chip->name);
879 sprintf(card->shortname, "OPTi %s", card->driver);
880 #if defined(CS4231) || defined(OPTi93X)
881 snprintf(card->longname, sizeof(card->longname),
882 "%s, %s at 0x%lx, irq %d, dma %d&%d",
883 card->shortname, codec->pcm->name,
884 chip->wss_base + 4, irq, dma1, xdma2);
886 snprintf(card->longname, sizeof(card->longname),
887 "%s, %s at 0x%lx, irq %d, dma %d",
888 card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
890 #endif /* CS4231 || OPTi93X */
892 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
895 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
896 mpu_port, 0, mpu_irq, &rmidi);
898 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
902 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
903 struct snd_opl3 *opl3 = NULL;
905 if (chip->hardware == OPTi9XX_HW_82C928 ||
906 chip->hardware == OPTi9XX_HW_82C929 ||
907 chip->hardware == OPTi9XX_HW_82C924) {
908 struct snd_opl4 *opl4;
909 /* assume we have an OPL4 */
910 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
912 if (snd_opl4_create(card, fm_port, fm_port - 8,
913 2, &opl3, &opl4) < 0) {
914 /* no luck, use OPL3 instead */
915 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
919 #endif /* !OPTi93X */
920 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
921 OPL3_HW_AUTO, 0, &opl3) < 0) {
922 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
923 fm_port, fm_port + 4 - 1);
926 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
932 return snd_card_register(card);
935 static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp)
937 struct snd_card *card;
940 err = snd_card_new(pdev, index, id, THIS_MODULE,
941 sizeof(struct snd_opti9xx), &card);
944 card->private_free = snd_card_opti9xx_free;
949 static int snd_opti9xx_isa_match(struct device *devptr,
953 if (snd_opti9xx_pnp_is_probed)
961 static int snd_opti9xx_isa_probe(struct device *devptr,
964 struct snd_card *card;
966 static const long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
968 static const int possible_irqs[] = {5, 9, 10, 11, 7, -1};
970 static const int possible_irqs[] = {9, 10, 11, 7, -1};
972 static const int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
973 static const int possible_dma1s[] = {3, 1, 0, -1};
974 #if defined(CS4231) || defined(OPTi93X)
975 static const int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
976 #endif /* CS4231 || OPTi93X */
978 if (mpu_port == SNDRV_AUTO_PORT) {
979 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
980 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
984 if (irq == SNDRV_AUTO_IRQ) {
985 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
986 snd_printk(KERN_ERR "unable to find a free IRQ\n");
990 if (mpu_irq == SNDRV_AUTO_IRQ) {
991 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
992 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
996 if (dma1 == SNDRV_AUTO_DMA) {
997 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
998 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1002 #if defined(CS4231) || defined(OPTi93X)
1003 if (dma2 == SNDRV_AUTO_DMA) {
1004 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1005 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1011 error = snd_opti9xx_card_new(devptr, &card);
1015 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1016 snd_card_free(card);
1019 if ((error = snd_opti9xx_probe(card)) < 0) {
1020 snd_card_free(card);
1023 dev_set_drvdata(devptr, card);
1027 static int snd_opti9xx_isa_remove(struct device *devptr,
1030 snd_card_free(dev_get_drvdata(devptr));
1035 static int snd_opti9xx_suspend(struct snd_card *card)
1037 struct snd_opti9xx *chip = card->private_data;
1039 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1040 chip->codec->suspend(chip->codec);
1044 static int snd_opti9xx_resume(struct snd_card *card)
1046 struct snd_opti9xx *chip = card->private_data;
1048 #if defined(CS4231) || defined(OPTi93X)
1054 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1058 chip->codec->resume(chip->codec);
1059 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1063 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1066 return snd_opti9xx_suspend(dev_get_drvdata(dev));
1069 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1071 return snd_opti9xx_resume(dev_get_drvdata(dev));
1075 static struct isa_driver snd_opti9xx_driver = {
1076 .match = snd_opti9xx_isa_match,
1077 .probe = snd_opti9xx_isa_probe,
1078 .remove = snd_opti9xx_isa_remove,
1080 .suspend = snd_opti9xx_isa_suspend,
1081 .resume = snd_opti9xx_isa_resume,
1089 static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1090 const struct pnp_card_device_id *pid)
1092 struct snd_card *card;
1094 struct snd_opti9xx *chip;
1096 if (snd_opti9xx_pnp_is_probed)
1100 error = snd_opti9xx_card_new(&pcard->card->dev, &card);
1103 chip = card->private_data;
1105 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1108 hw = OPTi9XX_HW_82C924;
1111 hw = OPTi9XX_HW_82C925;
1114 hw = OPTi9XX_HW_82C931;
1117 snd_card_free(card);
1121 if ((error = snd_opti9xx_init(chip, hw))) {
1122 snd_card_free(card);
1125 error = snd_opti9xx_read_check(chip);
1127 snd_printk(KERN_ERR "OPTI chip not found\n");
1128 snd_card_free(card);
1131 if ((error = snd_opti9xx_probe(card)) < 0) {
1132 snd_card_free(card);
1135 pnp_set_card_drvdata(pcard, card);
1136 snd_opti9xx_pnp_is_probed = 1;
1140 static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard)
1142 snd_card_free(pnp_get_card_drvdata(pcard));
1143 pnp_set_card_drvdata(pcard, NULL);
1144 snd_opti9xx_pnp_is_probed = 0;
1148 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1151 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1154 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1156 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1160 static struct pnp_card_driver opti9xx_pnpc_driver = {
1161 .flags = PNP_DRIVER_RES_DISABLE,
1163 .id_table = snd_opti9xx_pnpids,
1164 .probe = snd_opti9xx_pnp_probe,
1165 .remove = snd_opti9xx_pnp_remove,
1167 .suspend = snd_opti9xx_pnp_suspend,
1168 .resume = snd_opti9xx_pnp_resume,
1174 #define CHIP_NAME "82C93x"
1176 #define CHIP_NAME "82C92x"
1179 static int __init alsa_card_opti9xx_init(void)
1182 pnp_register_card_driver(&opti9xx_pnpc_driver);
1183 if (snd_opti9xx_pnp_is_probed)
1185 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1187 return isa_register_driver(&snd_opti9xx_driver, 1);
1190 static void __exit alsa_card_opti9xx_exit(void)
1192 if (!snd_opti9xx_pnp_is_probed) {
1193 isa_unregister_driver(&snd_opti9xx_driver);
1197 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1201 module_init(alsa_card_opti9xx_init)
1202 module_exit(alsa_card_opti9xx_exit)