1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards. 4 Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it> 5 6 Part of this code was developed at the Italian Ministry of Air Defence, 7 Sixth Division (oh, che pace ...), Rome. 8 9 Thanks to Maria Grazia Pollarini, Salvatore Vassallo. 10 11 */ 12 13 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> 20 #include <linux/io.h> 21 #include <asm/dma.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> 27 #ifndef OPTi93X 28 #include <sound/opl4.h> 29 #endif 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> 34 35 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>"); 36 MODULE_LICENSE("GPL"); 37 #ifdef OPTi93X 38 MODULE_DESCRIPTION("OPTi93X"); 39 MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}"); 40 #else /* OPTi93X */ 41 #ifdef CS4231 42 MODULE_DESCRIPTION("OPTi92X - CS4231"); 43 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)}," 44 "{OPTi,82C925 (CS4231)}}"); 45 #else /* CS4231 */ 46 MODULE_DESCRIPTION("OPTi92X - AD1848"); 47 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)}," 48 "{OPTi,82C925 (AD1848)}," 49 "{OAK,Mozart}}"); 50 #endif /* CS4231 */ 51 #endif /* OPTi93X */ 52 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 */ 56 #ifdef CONFIG_PNP 57 static bool isapnp = true; /* Enable ISA PnP detection */ 58 #endif 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 */ 68 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."); 75 #ifdef CONFIG_PNP 76 module_param(isapnp, bool, 0444); 77 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard."); 78 #endif 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 */ 95 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 104 105 #define OPTi9XX_MC_REG(n) n 106 107 #ifdef OPTi93X 108 109 #define OPTi93X_STATUS 0x02 110 #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r) 111 112 #define OPTi93X_IRQ_PLAYBACK 0x04 113 #define OPTi93X_IRQ_CAPTURE 0x08 114 115 #endif /* OPTi93X */ 116 117 struct snd_opti9xx { 118 unsigned short hardware; 119 unsigned char password; 120 char name[7]; 121 122 unsigned long mc_base; 123 struct resource *res_mc_base; 124 unsigned long mc_base_size; 125 #ifdef OPTi93X 126 unsigned long mc_indir_index; 127 struct resource *res_mc_indir; 128 #endif /* OPTi93X */ 129 struct snd_wss *codec; 130 unsigned long pwd_reg; 131 132 spinlock_t lock; 133 134 long wss_base; 135 int irq; 136 }; 137 138 static int snd_opti9xx_pnp_is_probed; 139 140 #ifdef CONFIG_PNP 141 142 static const struct pnp_card_device_id snd_opti9xx_pnpids[] = { 143 #ifndef OPTi93X 144 /* OPTi 82C924 */ 145 { .id = "OPT0924", 146 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } }, 147 .driver_data = 0x0924 }, 148 /* OPTi 82C925 */ 149 { .id = "OPT0925", 150 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } }, 151 .driver_data = 0x0925 }, 152 #else 153 /* OPTi 82C931/3 */ 154 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, 155 .driver_data = 0x0931 }, 156 #endif /* OPTi93X */ 157 { .id = "" } 158 }; 159 160 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids); 161 162 #endif /* CONFIG_PNP */ 163 164 #define DEV_NAME KBUILD_MODNAME 165 166 static const char * const snd_opti9xx_names[] = { 167 "unknown", 168 "82C928", "82C929", 169 "82C924", "82C925", 170 "82C930", "82C931", "82C933" 171 }; 172 173 static int snd_opti9xx_init(struct snd_opti9xx *chip, 174 unsigned short hardware) 175 { 176 static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2}; 177 178 chip->hardware = hardware; 179 strcpy(chip->name, snd_opti9xx_names[hardware]); 180 181 spin_lock_init(&chip->lock); 182 183 chip->irq = -1; 184 185 #ifndef OPTi93X 186 #ifdef CONFIG_PNP 187 if (isapnp && chip->mc_base) 188 /* PnP resource gives the least 10 bits */ 189 chip->mc_base |= 0xc00; 190 else 191 #endif /* CONFIG_PNP */ 192 { 193 chip->mc_base = 0xf8c; 194 chip->mc_base_size = opti9xx_mc_size[hardware]; 195 } 196 #else 197 chip->mc_base_size = opti9xx_mc_size[hardware]; 198 #endif 199 200 switch (hardware) { 201 #ifndef OPTi93X 202 case OPTi9XX_HW_82C928: 203 case OPTi9XX_HW_82C929: 204 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3; 205 chip->pwd_reg = 3; 206 break; 207 208 case OPTi9XX_HW_82C924: 209 case OPTi9XX_HW_82C925: 210 chip->password = 0xe5; 211 chip->pwd_reg = 3; 212 break; 213 #else /* OPTi93X */ 214 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; 222 chip->pwd_reg = 0; 223 break; 224 #endif /* OPTi93X */ 225 226 default: 227 snd_printk(KERN_ERR "chip %d not supported\n", hardware); 228 return -ENODEV; 229 } 230 return 0; 231 } 232 233 static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip, 234 unsigned char reg) 235 { 236 unsigned long flags; 237 unsigned char retval = 0xff; 238 239 spin_lock_irqsave(&chip->lock, flags); 240 outb(chip->password, chip->mc_base + chip->pwd_reg); 241 242 switch (chip->hardware) { 243 #ifndef OPTi93X 244 case OPTi9XX_HW_82C924: 245 case OPTi9XX_HW_82C925: 246 if (reg > 7) { 247 outb(reg, chip->mc_base + 8); 248 outb(chip->password, chip->mc_base + chip->pwd_reg); 249 retval = inb(chip->mc_base + 9); 250 break; 251 } 252 fallthrough; 253 254 case OPTi9XX_HW_82C928: 255 case OPTi9XX_HW_82C929: 256 retval = inb(chip->mc_base + reg); 257 break; 258 #else /* OPTi93X */ 259 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); 266 break; 267 #endif /* OPTi93X */ 268 269 default: 270 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); 271 } 272 273 spin_unlock_irqrestore(&chip->lock, flags); 274 return retval; 275 } 276 277 static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg, 278 unsigned char value) 279 { 280 unsigned long flags; 281 282 spin_lock_irqsave(&chip->lock, flags); 283 outb(chip->password, chip->mc_base + chip->pwd_reg); 284 285 switch (chip->hardware) { 286 #ifndef OPTi93X 287 case OPTi9XX_HW_82C924: 288 case OPTi9XX_HW_82C925: 289 if (reg > 7) { 290 outb(reg, chip->mc_base + 8); 291 outb(chip->password, chip->mc_base + chip->pwd_reg); 292 outb(value, chip->mc_base + 9); 293 break; 294 } 295 fallthrough; 296 297 case OPTi9XX_HW_82C928: 298 case OPTi9XX_HW_82C929: 299 outb(value, chip->mc_base + reg); 300 break; 301 #else /* OPTi93X */ 302 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); 309 break; 310 #endif /* OPTi93X */ 311 312 default: 313 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); 314 } 315 316 spin_unlock_irqrestore(&chip->lock, flags); 317 } 318 319 320 static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip, 321 unsigned char reg, unsigned char value, unsigned char mask) 322 { 323 unsigned char oldval = snd_opti9xx_read(chip, reg); 324 325 snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask)); 326 } 327 328 static int snd_opti9xx_configure(struct snd_opti9xx *chip, 329 long port, 330 int irq, int dma1, int dma2, 331 long mpu_port, int mpu_irq) 332 { 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; 338 339 switch (chip->hardware) { 340 #ifndef OPTi93X 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); 346 fallthrough; 347 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); 355 #ifdef CS4231 356 /* cs4231/4248 fix enabled */ 357 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); 358 #else 359 /* cs4231/4248 fix disabled */ 360 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); 361 #endif /* CS4231 */ 362 break; 363 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); 368 /* 369 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae); 370 */ 371 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c); 372 #ifdef CS4231 373 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); 374 #else 375 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); 376 #endif /* CS4231 */ 377 break; 378 379 #else /* OPTi93X */ 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); 383 fallthrough; 384 385 case OPTi9XX_HW_82C933: 386 /* 387 * The BTC 1817DW has QS1000 wavetable which is connected 388 * to the serial digital input of the OPTI931. 389 */ 390 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff); 391 /* 392 * This bit sets OPTI931 to automaticaly select FM 393 * or digital input signal. 394 */ 395 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01); 396 fallthrough; 397 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), 403 0x34); 404 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf); 405 break; 406 #endif /* OPTi93X */ 407 408 default: 409 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); 410 return -EINVAL; 411 } 412 413 /* PnP resource says it decodes only 10 bits of address */ 414 switch (port & 0x3ff) { 415 case 0x130: 416 chip->wss_base = 0x530; 417 wss_base_bits = 0x00; 418 break; 419 case 0x204: 420 chip->wss_base = 0x604; 421 wss_base_bits = 0x03; 422 break; 423 case 0x280: 424 chip->wss_base = 0xe80; 425 wss_base_bits = 0x01; 426 break; 427 case 0x340: 428 chip->wss_base = 0xf40; 429 wss_base_bits = 0x02; 430 break; 431 default: 432 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port); 433 goto __skip_base; 434 } 435 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30); 436 437 __skip_base: 438 switch (irq) { 439 //#ifdef OPTi93X 440 case 5: 441 irq_bits = 0x05; 442 break; 443 //#endif /* OPTi93X */ 444 case 7: 445 irq_bits = 0x01; 446 break; 447 case 9: 448 irq_bits = 0x02; 449 break; 450 case 10: 451 irq_bits = 0x03; 452 break; 453 case 11: 454 irq_bits = 0x04; 455 break; 456 default: 457 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq); 458 goto __skip_resources; 459 } 460 461 switch (dma1) { 462 case 0: 463 dma_bits = 0x01; 464 break; 465 case 1: 466 dma_bits = 0x02; 467 break; 468 case 3: 469 dma_bits = 0x03; 470 break; 471 default: 472 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1); 473 goto __skip_resources; 474 } 475 476 #if defined(CS4231) || defined(OPTi93X) 477 if (dma1 == dma2) { 478 snd_printk(KERN_ERR "don't want to share dmas\n"); 479 return -EBUSY; 480 } 481 482 switch (dma2) { 483 case 0: 484 case 1: 485 break; 486 default: 487 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2); 488 goto __skip_resources; 489 } 490 dma_bits |= 0x04; 491 #endif /* CS4231 || OPTi93X */ 492 493 #ifndef OPTi93X 494 outb(irq_bits << 3 | dma_bits, chip->wss_base); 495 #else /* OPTi93X */ 496 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits)); 497 #endif /* OPTi93X */ 498 499 __skip_resources: 500 if (chip->hardware > OPTi9XX_HW_82C928) { 501 switch (mpu_port) { 502 case 0: 503 case -1: 504 break; 505 case 0x300: 506 mpu_port_bits = 0x03; 507 break; 508 case 0x310: 509 mpu_port_bits = 0x02; 510 break; 511 case 0x320: 512 mpu_port_bits = 0x01; 513 break; 514 case 0x330: 515 mpu_port_bits = 0x00; 516 break; 517 default: 518 snd_printk(KERN_WARNING 519 "MPU-401 port 0x%lx not valid\n", mpu_port); 520 goto __skip_mpu; 521 } 522 523 switch (mpu_irq) { 524 case 5: 525 mpu_irq_bits = 0x02; 526 break; 527 case 7: 528 mpu_irq_bits = 0x03; 529 break; 530 case 9: 531 mpu_irq_bits = 0x00; 532 break; 533 case 10: 534 mpu_irq_bits = 0x01; 535 break; 536 default: 537 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n", 538 mpu_irq); 539 goto __skip_mpu; 540 } 541 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, 545 0xf8); 546 } 547 __skip_mpu: 548 549 return 0; 550 } 551 552 #ifdef OPTi93X 553 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); 557 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, 566 db_scale_5bit), 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), 588 }; 589 590 static int snd_opti93x_mixer(struct snd_wss *chip) 591 { 592 struct snd_card *card; 593 unsigned int idx; 594 struct snd_ctl_elem_id id1, id2; 595 int err; 596 597 if (snd_BUG_ON(!chip || !chip->pcm)) 598 return -EINVAL; 599 600 card = chip->card; 601 602 strcpy(card->mixername, chip->pcm->name); 603 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); 611 if (err < 0) { 612 snd_printk(KERN_ERR "Cannot rename opti93x control\n"); 613 return err; 614 } 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); 619 if (err < 0) { 620 snd_printk(KERN_ERR "Cannot rename opti93x control\n"); 621 return err; 622 } 623 /* remove AUX1 volume */ 624 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1; 625 snd_ctl_remove_id(card, &id1); 626 627 /* Replace WSS volume controls with OPTi93x volume controls */ 628 id1.index = 0; 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); 632 633 err = snd_ctl_add(card, 634 snd_ctl_new1(&snd_opti93x_controls[idx], chip)); 635 if (err < 0) 636 return err; 637 } 638 return 0; 639 } 640 641 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id) 642 { 643 struct snd_opti9xx *chip = dev_id; 644 struct snd_wss *codec = chip->codec; 645 unsigned char status; 646 647 if (!codec) 648 return IRQ_HANDLED; 649 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); 656 } 657 outb(0x00, OPTi93X_PORT(codec, STATUS)); 658 return IRQ_HANDLED; 659 } 660 661 #endif /* OPTi93X */ 662 663 static int snd_opti9xx_read_check(struct snd_opti9xx *chip) 664 { 665 unsigned char value; 666 #ifdef OPTi93X 667 unsigned long flags; 668 #endif 669 670 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, 671 "OPTi9xx MC"); 672 if (chip->res_mc_base == NULL) 673 return -EBUSY; 674 #ifndef OPTi93X 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))) 678 return 0; 679 #else /* OPTi93X */ 680 chip->res_mc_indir = request_region(chip->mc_indir_index, 2, 681 "OPTi93x MC"); 682 if (chip->res_mc_indir == NULL) 683 return -EBUSY; 684 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); 689 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) 693 return 0; 694 695 release_and_free_resource(chip->res_mc_indir); 696 chip->res_mc_indir = NULL; 697 #endif /* OPTi93X */ 698 release_and_free_resource(chip->res_mc_base); 699 chip->res_mc_base = NULL; 700 701 return -ENODEV; 702 } 703 704 static int snd_card_opti9xx_detect(struct snd_card *card, 705 struct snd_opti9xx *chip) 706 { 707 int i, err; 708 709 #ifndef OPTi93X 710 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) { 711 #else 712 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) { 713 #endif 714 err = snd_opti9xx_init(chip, i); 715 if (err < 0) 716 return err; 717 718 err = snd_opti9xx_read_check(chip); 719 if (err == 0) 720 return 1; 721 #ifdef OPTi93X 722 chip->mc_indir_index = 0; 723 #endif 724 } 725 return -ENODEV; 726 } 727 728 #ifdef CONFIG_PNP 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) 732 { 733 struct pnp_dev *pdev; 734 int err; 735 struct pnp_dev *devmpu; 736 #ifndef OPTi93X 737 struct pnp_dev *devmc; 738 #endif 739 740 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL); 741 if (pdev == NULL) 742 return -EBUSY; 743 744 err = pnp_activate_dev(pdev); 745 if (err < 0) { 746 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err); 747 return err; 748 } 749 750 #ifdef OPTi93X 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; 756 #else 757 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL); 758 if (devmc == NULL) 759 return -EBUSY; 760 761 err = pnp_activate_dev(devmc); 762 if (err < 0) { 763 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err); 764 return err; 765 } 766 767 port = pnp_port_start(pdev, 1); 768 fm_port = pnp_port_start(pdev, 2) + 8; 769 /* 770 * The MC(0) is never accessed and card does not 771 * include it in the PnP resource range. OPTI93x include it. 772 */ 773 chip->mc_base = pnp_port_start(devmc, 0) - 1; 774 chip->mc_base_size = pnp_port_len(devmc, 0) + 1; 775 #endif /* OPTi93X */ 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 */ 781 782 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL); 783 784 if (devmpu && mpu_port > 0) { 785 err = pnp_activate_dev(devmpu); 786 if (err < 0) { 787 snd_printk(KERN_ERR "MPU401 pnp configure failure\n"); 788 mpu_port = -1; 789 } else { 790 mpu_port = pnp_port_start(devmpu, 0); 791 mpu_irq = pnp_irq(devmpu, 0); 792 } 793 } 794 return pid->driver_data; 795 } 796 #endif /* CONFIG_PNP */ 797 798 static void snd_card_opti9xx_free(struct snd_card *card) 799 { 800 struct snd_opti9xx *chip = card->private_data; 801 802 if (chip) { 803 #ifdef OPTi93X 804 if (chip->irq > 0) { 805 disable_irq(chip->irq); 806 free_irq(chip->irq, chip); 807 } 808 release_and_free_resource(chip->res_mc_indir); 809 #endif 810 release_and_free_resource(chip->res_mc_base); 811 } 812 } 813 814 static int snd_opti9xx_probe(struct snd_card *card) 815 { 816 static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1}; 817 int error; 818 int xdma2; 819 struct snd_opti9xx *chip = card->private_data; 820 struct snd_wss *codec; 821 struct snd_rawmidi *rmidi; 822 struct snd_hwdep *synth; 823 824 #if defined(CS4231) || defined(OPTi93X) 825 xdma2 = dma2; 826 #else 827 xdma2 = -1; 828 #endif 829 830 if (port == SNDRV_AUTO_PORT) { 831 port = snd_legacy_find_free_ioport(possible_ports, 4); 832 if (port < 0) { 833 snd_printk(KERN_ERR "unable to find a free WSS port\n"); 834 return -EBUSY; 835 } 836 } 837 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, 838 mpu_port, mpu_irq); 839 if (error) 840 return error; 841 842 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2, 843 #ifdef OPTi93X 844 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ, 845 #else 846 WSS_HW_DETECT, 0, 847 #endif 848 &codec); 849 if (error < 0) 850 return error; 851 chip->codec = codec; 852 error = snd_wss_pcm(codec, 0); 853 if (error < 0) 854 return error; 855 error = snd_wss_mixer(codec); 856 if (error < 0) 857 return error; 858 #ifdef OPTi93X 859 error = snd_opti93x_mixer(codec); 860 if (error < 0) 861 return error; 862 #endif 863 #ifdef CS4231 864 error = snd_wss_timer(codec, 0); 865 if (error < 0) 866 return error; 867 #endif 868 #ifdef OPTi93X 869 error = request_irq(irq, snd_opti93x_interrupt, 870 0, DEV_NAME" - WSS", chip); 871 if (error < 0) { 872 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq); 873 return error; 874 } 875 #endif 876 chip->irq = 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); 885 #else 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, 889 dma1); 890 #endif /* CS4231 || OPTi93X */ 891 892 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT) 893 rmidi = NULL; 894 else { 895 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, 896 mpu_port, 0, mpu_irq, &rmidi); 897 if (error) 898 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n", 899 mpu_port); 900 } 901 902 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) { 903 struct snd_opl3 *opl3 = NULL; 904 #ifndef OPTi93X 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), 911 0x20, 0x20); 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), 916 0x00, 0x20); 917 } 918 } 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); 924 } 925 if (opl3) { 926 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth); 927 if (error < 0) 928 return error; 929 } 930 } 931 932 return snd_card_register(card); 933 } 934 935 static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp) 936 { 937 struct snd_card *card; 938 int err; 939 940 err = snd_card_new(pdev, index, id, THIS_MODULE, 941 sizeof(struct snd_opti9xx), &card); 942 if (err < 0) 943 return err; 944 card->private_free = snd_card_opti9xx_free; 945 *cardp = card; 946 return 0; 947 } 948 949 static int snd_opti9xx_isa_match(struct device *devptr, 950 unsigned int dev) 951 { 952 #ifdef CONFIG_PNP 953 if (snd_opti9xx_pnp_is_probed) 954 return 0; 955 if (isapnp) 956 return 0; 957 #endif 958 return 1; 959 } 960 961 static int snd_opti9xx_isa_probe(struct device *devptr, 962 unsigned int dev) 963 { 964 struct snd_card *card; 965 int error; 966 static const long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1}; 967 #ifdef OPTi93X 968 static const int possible_irqs[] = {5, 9, 10, 11, 7, -1}; 969 #else 970 static const int possible_irqs[] = {9, 10, 11, 7, -1}; 971 #endif /* OPTi93X */ 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 */ 977 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"); 981 return -EBUSY; 982 } 983 } 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"); 987 return -EBUSY; 988 } 989 } 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"); 993 return -EBUSY; 994 } 995 } 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"); 999 return -EBUSY; 1000 } 1001 } 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"); 1006 return -EBUSY; 1007 } 1008 } 1009 #endif 1010 1011 error = snd_opti9xx_card_new(devptr, &card); 1012 if (error < 0) 1013 return error; 1014 1015 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) { 1016 snd_card_free(card); 1017 return error; 1018 } 1019 if ((error = snd_opti9xx_probe(card)) < 0) { 1020 snd_card_free(card); 1021 return error; 1022 } 1023 dev_set_drvdata(devptr, card); 1024 return 0; 1025 } 1026 1027 static int snd_opti9xx_isa_remove(struct device *devptr, 1028 unsigned int dev) 1029 { 1030 snd_card_free(dev_get_drvdata(devptr)); 1031 return 0; 1032 } 1033 1034 #ifdef CONFIG_PM 1035 static int snd_opti9xx_suspend(struct snd_card *card) 1036 { 1037 struct snd_opti9xx *chip = card->private_data; 1038 1039 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1040 chip->codec->suspend(chip->codec); 1041 return 0; 1042 } 1043 1044 static int snd_opti9xx_resume(struct snd_card *card) 1045 { 1046 struct snd_opti9xx *chip = card->private_data; 1047 int error, xdma2; 1048 #if defined(CS4231) || defined(OPTi93X) 1049 xdma2 = dma2; 1050 #else 1051 xdma2 = -1; 1052 #endif 1053 1054 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, 1055 mpu_port, mpu_irq); 1056 if (error) 1057 return error; 1058 chip->codec->resume(chip->codec); 1059 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1060 return 0; 1061 } 1062 1063 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n, 1064 pm_message_t state) 1065 { 1066 return snd_opti9xx_suspend(dev_get_drvdata(dev)); 1067 } 1068 1069 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n) 1070 { 1071 return snd_opti9xx_resume(dev_get_drvdata(dev)); 1072 } 1073 #endif 1074 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, 1079 #ifdef CONFIG_PM 1080 .suspend = snd_opti9xx_isa_suspend, 1081 .resume = snd_opti9xx_isa_resume, 1082 #endif 1083 .driver = { 1084 .name = DEV_NAME 1085 }, 1086 }; 1087 1088 #ifdef CONFIG_PNP 1089 static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard, 1090 const struct pnp_card_device_id *pid) 1091 { 1092 struct snd_card *card; 1093 int error, hw; 1094 struct snd_opti9xx *chip; 1095 1096 if (snd_opti9xx_pnp_is_probed) 1097 return -EBUSY; 1098 if (! isapnp) 1099 return -ENODEV; 1100 error = snd_opti9xx_card_new(&pcard->card->dev, &card); 1101 if (error < 0) 1102 return error; 1103 chip = card->private_data; 1104 1105 hw = snd_card_opti9xx_pnp(chip, pcard, pid); 1106 switch (hw) { 1107 case 0x0924: 1108 hw = OPTi9XX_HW_82C924; 1109 break; 1110 case 0x0925: 1111 hw = OPTi9XX_HW_82C925; 1112 break; 1113 case 0x0931: 1114 hw = OPTi9XX_HW_82C931; 1115 break; 1116 default: 1117 snd_card_free(card); 1118 return -ENODEV; 1119 } 1120 1121 if ((error = snd_opti9xx_init(chip, hw))) { 1122 snd_card_free(card); 1123 return error; 1124 } 1125 error = snd_opti9xx_read_check(chip); 1126 if (error) { 1127 snd_printk(KERN_ERR "OPTI chip not found\n"); 1128 snd_card_free(card); 1129 return error; 1130 } 1131 if ((error = snd_opti9xx_probe(card)) < 0) { 1132 snd_card_free(card); 1133 return error; 1134 } 1135 pnp_set_card_drvdata(pcard, card); 1136 snd_opti9xx_pnp_is_probed = 1; 1137 return 0; 1138 } 1139 1140 static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard) 1141 { 1142 snd_card_free(pnp_get_card_drvdata(pcard)); 1143 pnp_set_card_drvdata(pcard, NULL); 1144 snd_opti9xx_pnp_is_probed = 0; 1145 } 1146 1147 #ifdef CONFIG_PM 1148 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard, 1149 pm_message_t state) 1150 { 1151 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard)); 1152 } 1153 1154 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard) 1155 { 1156 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard)); 1157 } 1158 #endif 1159 1160 static struct pnp_card_driver opti9xx_pnpc_driver = { 1161 .flags = PNP_DRIVER_RES_DISABLE, 1162 .name = DEV_NAME, 1163 .id_table = snd_opti9xx_pnpids, 1164 .probe = snd_opti9xx_pnp_probe, 1165 .remove = snd_opti9xx_pnp_remove, 1166 #ifdef CONFIG_PM 1167 .suspend = snd_opti9xx_pnp_suspend, 1168 .resume = snd_opti9xx_pnp_resume, 1169 #endif 1170 }; 1171 #endif 1172 1173 #ifdef OPTi93X 1174 #define CHIP_NAME "82C93x" 1175 #else 1176 #define CHIP_NAME "82C92x" 1177 #endif 1178 1179 static int __init alsa_card_opti9xx_init(void) 1180 { 1181 #ifdef CONFIG_PNP 1182 pnp_register_card_driver(&opti9xx_pnpc_driver); 1183 if (snd_opti9xx_pnp_is_probed) 1184 return 0; 1185 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1186 #endif 1187 return isa_register_driver(&snd_opti9xx_driver, 1); 1188 } 1189 1190 static void __exit alsa_card_opti9xx_exit(void) 1191 { 1192 if (!snd_opti9xx_pnp_is_probed) { 1193 isa_unregister_driver(&snd_opti9xx_driver); 1194 return; 1195 } 1196 #ifdef CONFIG_PNP 1197 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1198 #endif 1199 } 1200 1201 module_init(alsa_card_opti9xx_init) 1202 module_exit(alsa_card_opti9xx_exit) 1203