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 #else /* OPTi93X */ 40 #ifdef CS4231 41 MODULE_DESCRIPTION("OPTi92X - CS4231"); 42 #else /* CS4231 */ 43 MODULE_DESCRIPTION("OPTi92X - AD1848"); 44 #endif /* CS4231 */ 45 #endif /* OPTi93X */ 46 47 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ 48 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ 49 //static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */ 50 #ifdef CONFIG_PNP 51 static bool isapnp = true; /* Enable ISA PnP detection */ 52 #endif 53 static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */ 54 static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */ 55 static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */ 56 static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */ 57 static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */ 58 static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ 59 #if defined(CS4231) || defined(OPTi93X) 60 static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ 61 #endif /* CS4231 || OPTi93X */ 62 63 module_param(index, int, 0444); 64 MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard."); 65 module_param(id, charp, 0444); 66 MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard."); 67 //module_param(enable, bool, 0444); 68 //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard."); 69 #ifdef CONFIG_PNP 70 module_param(isapnp, bool, 0444); 71 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard."); 72 #endif 73 module_param_hw(port, long, ioport, 0444); 74 MODULE_PARM_DESC(port, "WSS port # for opti9xx driver."); 75 module_param_hw(mpu_port, long, ioport, 0444); 76 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver."); 77 module_param_hw(fm_port, long, ioport, 0444); 78 MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver."); 79 module_param_hw(irq, int, irq, 0444); 80 MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver."); 81 module_param_hw(mpu_irq, int, irq, 0444); 82 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver."); 83 module_param_hw(dma1, int, dma, 0444); 84 MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver."); 85 #if defined(CS4231) || defined(OPTi93X) 86 module_param_hw(dma2, int, dma, 0444); 87 MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver."); 88 #endif /* CS4231 || OPTi93X */ 89 90 #define OPTi9XX_HW_82C928 1 91 #define OPTi9XX_HW_82C929 2 92 #define OPTi9XX_HW_82C924 3 93 #define OPTi9XX_HW_82C925 4 94 #define OPTi9XX_HW_82C930 5 95 #define OPTi9XX_HW_82C931 6 96 #define OPTi9XX_HW_82C933 7 97 #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933 98 99 #define OPTi9XX_MC_REG(n) n 100 101 #ifdef OPTi93X 102 103 #define OPTi93X_STATUS 0x02 104 #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r) 105 106 #define OPTi93X_IRQ_PLAYBACK 0x04 107 #define OPTi93X_IRQ_CAPTURE 0x08 108 109 #endif /* OPTi93X */ 110 111 struct snd_opti9xx { 112 struct snd_card *card; 113 unsigned short hardware; 114 unsigned char password; 115 char name[7]; 116 117 unsigned long mc_base; 118 struct resource *res_mc_base; 119 unsigned long mc_base_size; 120 #ifdef OPTi93X 121 unsigned long mc_indir_index; 122 struct resource *res_mc_indir; 123 #endif /* OPTi93X */ 124 struct snd_wss *codec; 125 unsigned long pwd_reg; 126 127 spinlock_t lock; 128 129 long wss_base; 130 int irq; 131 }; 132 133 static int snd_opti9xx_pnp_is_probed; 134 135 #ifdef CONFIG_PNP 136 137 static const struct pnp_card_device_id snd_opti9xx_pnpids[] = { 138 #ifndef OPTi93X 139 /* OPTi 82C924 */ 140 { .id = "OPT0924", 141 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } }, 142 .driver_data = 0x0924 }, 143 /* OPTi 82C925 */ 144 { .id = "OPT0925", 145 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } }, 146 .driver_data = 0x0925 }, 147 #else 148 /* OPTi 82C931/3 */ 149 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, 150 .driver_data = 0x0931 }, 151 #endif /* OPTi93X */ 152 { .id = "" } 153 }; 154 155 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids); 156 157 #endif /* CONFIG_PNP */ 158 159 #define DEV_NAME KBUILD_MODNAME 160 161 static const char * const snd_opti9xx_names[] = { 162 "unknown", 163 "82C928", "82C929", 164 "82C924", "82C925", 165 "82C930", "82C931", "82C933" 166 }; 167 168 static int snd_opti9xx_init(struct snd_opti9xx *chip, 169 unsigned short hardware) 170 { 171 static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2}; 172 173 chip->hardware = hardware; 174 strcpy(chip->name, snd_opti9xx_names[hardware]); 175 176 spin_lock_init(&chip->lock); 177 178 chip->irq = -1; 179 180 #ifndef OPTi93X 181 #ifdef CONFIG_PNP 182 if (isapnp && chip->mc_base) 183 /* PnP resource gives the least 10 bits */ 184 chip->mc_base |= 0xc00; 185 else 186 #endif /* CONFIG_PNP */ 187 { 188 chip->mc_base = 0xf8c; 189 chip->mc_base_size = opti9xx_mc_size[hardware]; 190 } 191 #else 192 chip->mc_base_size = opti9xx_mc_size[hardware]; 193 #endif 194 195 switch (hardware) { 196 #ifndef OPTi93X 197 case OPTi9XX_HW_82C928: 198 case OPTi9XX_HW_82C929: 199 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3; 200 chip->pwd_reg = 3; 201 break; 202 203 case OPTi9XX_HW_82C924: 204 case OPTi9XX_HW_82C925: 205 chip->password = 0xe5; 206 chip->pwd_reg = 3; 207 break; 208 #else /* OPTi93X */ 209 210 case OPTi9XX_HW_82C930: 211 case OPTi9XX_HW_82C931: 212 case OPTi9XX_HW_82C933: 213 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d; 214 if (!chip->mc_indir_index) 215 chip->mc_indir_index = 0xe0e; 216 chip->password = 0xe4; 217 chip->pwd_reg = 0; 218 break; 219 #endif /* OPTi93X */ 220 221 default: 222 dev_err(chip->card->dev, "chip %d not supported\n", hardware); 223 return -ENODEV; 224 } 225 return 0; 226 } 227 228 static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip, 229 unsigned char reg) 230 { 231 unsigned long flags; 232 unsigned char retval = 0xff; 233 234 spin_lock_irqsave(&chip->lock, flags); 235 outb(chip->password, chip->mc_base + chip->pwd_reg); 236 237 switch (chip->hardware) { 238 #ifndef OPTi93X 239 case OPTi9XX_HW_82C924: 240 case OPTi9XX_HW_82C925: 241 if (reg > 7) { 242 outb(reg, chip->mc_base + 8); 243 outb(chip->password, chip->mc_base + chip->pwd_reg); 244 retval = inb(chip->mc_base + 9); 245 break; 246 } 247 fallthrough; 248 249 case OPTi9XX_HW_82C928: 250 case OPTi9XX_HW_82C929: 251 retval = inb(chip->mc_base + reg); 252 break; 253 #else /* OPTi93X */ 254 255 case OPTi9XX_HW_82C930: 256 case OPTi9XX_HW_82C931: 257 case OPTi9XX_HW_82C933: 258 outb(reg, chip->mc_indir_index); 259 outb(chip->password, chip->mc_base + chip->pwd_reg); 260 retval = inb(chip->mc_indir_index + 1); 261 break; 262 #endif /* OPTi93X */ 263 264 default: 265 dev_err(chip->card->dev, "chip %d not supported\n", chip->hardware); 266 } 267 268 spin_unlock_irqrestore(&chip->lock, flags); 269 return retval; 270 } 271 272 static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg, 273 unsigned char value) 274 { 275 unsigned long flags; 276 277 spin_lock_irqsave(&chip->lock, flags); 278 outb(chip->password, chip->mc_base + chip->pwd_reg); 279 280 switch (chip->hardware) { 281 #ifndef OPTi93X 282 case OPTi9XX_HW_82C924: 283 case OPTi9XX_HW_82C925: 284 if (reg > 7) { 285 outb(reg, chip->mc_base + 8); 286 outb(chip->password, chip->mc_base + chip->pwd_reg); 287 outb(value, chip->mc_base + 9); 288 break; 289 } 290 fallthrough; 291 292 case OPTi9XX_HW_82C928: 293 case OPTi9XX_HW_82C929: 294 outb(value, chip->mc_base + reg); 295 break; 296 #else /* OPTi93X */ 297 298 case OPTi9XX_HW_82C930: 299 case OPTi9XX_HW_82C931: 300 case OPTi9XX_HW_82C933: 301 outb(reg, chip->mc_indir_index); 302 outb(chip->password, chip->mc_base + chip->pwd_reg); 303 outb(value, chip->mc_indir_index + 1); 304 break; 305 #endif /* OPTi93X */ 306 307 default: 308 dev_err(chip->card->dev, "chip %d not supported\n", chip->hardware); 309 } 310 311 spin_unlock_irqrestore(&chip->lock, flags); 312 } 313 314 315 static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip, 316 unsigned char reg, unsigned char value, unsigned char mask) 317 { 318 unsigned char oldval = snd_opti9xx_read(chip, reg); 319 320 snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask)); 321 } 322 323 static int snd_opti9xx_configure(struct snd_opti9xx *chip, 324 long port, 325 int irq, int dma1, int dma2, 326 long mpu_port, int mpu_irq) 327 { 328 unsigned char wss_base_bits; 329 unsigned char irq_bits; 330 unsigned char dma_bits; 331 unsigned char mpu_port_bits = 0; 332 unsigned char mpu_irq_bits; 333 334 switch (chip->hardware) { 335 #ifndef OPTi93X 336 case OPTi9XX_HW_82C924: 337 /* opti 929 mode (?), OPL3 clock output, audio enable */ 338 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc); 339 /* enable wave audio */ 340 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02); 341 fallthrough; 342 343 case OPTi9XX_HW_82C925: 344 /* enable WSS mode */ 345 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80); 346 /* OPL3 FM synthesis */ 347 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20); 348 /* disable Sound Blaster IRQ and DMA */ 349 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff); 350 #ifdef CS4231 351 /* cs4231/4248 fix enabled */ 352 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); 353 #else 354 /* cs4231/4248 fix disabled */ 355 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); 356 #endif /* CS4231 */ 357 break; 358 359 case OPTi9XX_HW_82C928: 360 case OPTi9XX_HW_82C929: 361 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80); 362 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20); 363 /* 364 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae); 365 */ 366 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c); 367 #ifdef CS4231 368 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); 369 #else 370 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); 371 #endif /* CS4231 */ 372 break; 373 374 #else /* OPTi93X */ 375 case OPTi9XX_HW_82C931: 376 /* disable 3D sound (set GPIO1 as output, low) */ 377 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c); 378 fallthrough; 379 380 case OPTi9XX_HW_82C933: 381 /* 382 * The BTC 1817DW has QS1000 wavetable which is connected 383 * to the serial digital input of the OPTI931. 384 */ 385 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff); 386 /* 387 * This bit sets OPTI931 to automaticaly select FM 388 * or digital input signal. 389 */ 390 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01); 391 fallthrough; 392 393 case OPTi9XX_HW_82C930: 394 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03); 395 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff); 396 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 | 397 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04), 398 0x34); 399 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf); 400 break; 401 #endif /* OPTi93X */ 402 403 default: 404 dev_err(chip->card->dev, "chip %d not supported\n", chip->hardware); 405 return -EINVAL; 406 } 407 408 /* PnP resource says it decodes only 10 bits of address */ 409 switch (port & 0x3ff) { 410 case 0x130: 411 chip->wss_base = 0x530; 412 wss_base_bits = 0x00; 413 break; 414 case 0x204: 415 chip->wss_base = 0x604; 416 wss_base_bits = 0x03; 417 break; 418 case 0x280: 419 chip->wss_base = 0xe80; 420 wss_base_bits = 0x01; 421 break; 422 case 0x340: 423 chip->wss_base = 0xf40; 424 wss_base_bits = 0x02; 425 break; 426 default: 427 dev_warn(chip->card->dev, "WSS port 0x%lx not valid\n", port); 428 goto __skip_base; 429 } 430 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30); 431 432 __skip_base: 433 switch (irq) { 434 //#ifdef OPTi93X 435 case 5: 436 irq_bits = 0x05; 437 break; 438 //#endif /* OPTi93X */ 439 case 7: 440 irq_bits = 0x01; 441 break; 442 case 9: 443 irq_bits = 0x02; 444 break; 445 case 10: 446 irq_bits = 0x03; 447 break; 448 case 11: 449 irq_bits = 0x04; 450 break; 451 default: 452 dev_warn(chip->card->dev, "WSS irq # %d not valid\n", irq); 453 goto __skip_resources; 454 } 455 456 switch (dma1) { 457 case 0: 458 dma_bits = 0x01; 459 break; 460 case 1: 461 dma_bits = 0x02; 462 break; 463 case 3: 464 dma_bits = 0x03; 465 break; 466 default: 467 dev_warn(chip->card->dev, "WSS dma1 # %d not valid\n", dma1); 468 goto __skip_resources; 469 } 470 471 #if defined(CS4231) || defined(OPTi93X) 472 if (dma1 == dma2) { 473 dev_err(chip->card->dev, "don't want to share dmas\n"); 474 return -EBUSY; 475 } 476 477 switch (dma2) { 478 case 0: 479 case 1: 480 break; 481 default: 482 dev_warn(chip->card->dev, "WSS dma2 # %d not valid\n", dma2); 483 goto __skip_resources; 484 } 485 dma_bits |= 0x04; 486 #endif /* CS4231 || OPTi93X */ 487 488 #ifndef OPTi93X 489 outb(irq_bits << 3 | dma_bits, chip->wss_base); 490 #else /* OPTi93X */ 491 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits)); 492 #endif /* OPTi93X */ 493 494 __skip_resources: 495 if (chip->hardware > OPTi9XX_HW_82C928) { 496 switch (mpu_port) { 497 case 0: 498 case -1: 499 break; 500 case 0x300: 501 mpu_port_bits = 0x03; 502 break; 503 case 0x310: 504 mpu_port_bits = 0x02; 505 break; 506 case 0x320: 507 mpu_port_bits = 0x01; 508 break; 509 case 0x330: 510 mpu_port_bits = 0x00; 511 break; 512 default: 513 dev_warn(chip->card->dev, 514 "MPU-401 port 0x%lx not valid\n", mpu_port); 515 goto __skip_mpu; 516 } 517 518 switch (mpu_irq) { 519 case 5: 520 mpu_irq_bits = 0x02; 521 break; 522 case 7: 523 mpu_irq_bits = 0x03; 524 break; 525 case 9: 526 mpu_irq_bits = 0x00; 527 break; 528 case 10: 529 mpu_irq_bits = 0x01; 530 break; 531 default: 532 dev_warn(chip->card->dev, "MPU-401 irq # %d not valid\n", 533 mpu_irq); 534 goto __skip_mpu; 535 } 536 537 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 538 (mpu_port <= 0) ? 0x00 : 539 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3, 540 0xf8); 541 } 542 __skip_mpu: 543 544 return 0; 545 } 546 547 #ifdef OPTi93X 548 549 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0); 550 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0); 551 static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0); 552 553 static const struct snd_kcontrol_new snd_opti93x_controls[] = { 554 WSS_DOUBLE("Master Playback Switch", 0, 555 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1), 556 WSS_DOUBLE_TLV("Master Playback Volume", 0, 557 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1, 558 db_scale_5bit_3db_step), 559 WSS_DOUBLE_TLV("PCM Playback Volume", 0, 560 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1, 561 db_scale_5bit), 562 WSS_DOUBLE_TLV("FM Playback Volume", 0, 563 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1, 564 db_scale_4bit_12db_max), 565 WSS_DOUBLE("Line Playback Switch", 0, 566 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1), 567 WSS_DOUBLE_TLV("Line Playback Volume", 0, 568 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1, 569 db_scale_4bit_12db_max), 570 WSS_DOUBLE("Mic Playback Switch", 0, 571 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1), 572 WSS_DOUBLE_TLV("Mic Playback Volume", 0, 573 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1, 574 db_scale_4bit_12db_max), 575 WSS_DOUBLE_TLV("CD Playback Volume", 0, 576 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1, 577 db_scale_4bit_12db_max), 578 WSS_DOUBLE("Aux Playback Switch", 0, 579 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1), 580 WSS_DOUBLE_TLV("Aux Playback Volume", 0, 581 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1, 582 db_scale_4bit_12db_max), 583 }; 584 585 static int snd_opti93x_mixer(struct snd_wss *chip) 586 { 587 struct snd_card *card; 588 unsigned int idx; 589 struct snd_ctl_elem_id id1, id2; 590 int err; 591 592 if (snd_BUG_ON(!chip || !chip->pcm)) 593 return -EINVAL; 594 595 card = chip->card; 596 597 strcpy(card->mixername, chip->pcm->name); 598 599 memset(&id1, 0, sizeof(id1)); 600 memset(&id2, 0, sizeof(id2)); 601 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 602 /* reassign AUX0 switch to CD */ 603 strcpy(id1.name, "Aux Playback Switch"); 604 strcpy(id2.name, "CD Playback Switch"); 605 err = snd_ctl_rename_id(card, &id1, &id2); 606 if (err < 0) { 607 dev_err(card->dev, "Cannot rename opti93x control\n"); 608 return err; 609 } 610 /* reassign AUX1 switch to FM */ 611 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; 612 strcpy(id2.name, "FM Playback Switch"); 613 err = snd_ctl_rename_id(card, &id1, &id2); 614 if (err < 0) { 615 dev_err(card->dev, "Cannot rename opti93x control\n"); 616 return err; 617 } 618 /* remove AUX1 volume */ 619 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1; 620 snd_ctl_remove_id(card, &id1); 621 622 /* Replace WSS volume controls with OPTi93x volume controls */ 623 id1.index = 0; 624 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) { 625 strcpy(id1.name, snd_opti93x_controls[idx].name); 626 snd_ctl_remove_id(card, &id1); 627 628 err = snd_ctl_add(card, 629 snd_ctl_new1(&snd_opti93x_controls[idx], chip)); 630 if (err < 0) 631 return err; 632 } 633 return 0; 634 } 635 636 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id) 637 { 638 struct snd_opti9xx *chip = dev_id; 639 struct snd_wss *codec = chip->codec; 640 unsigned char status; 641 642 if (!codec) 643 return IRQ_HANDLED; 644 645 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11)); 646 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream) 647 snd_pcm_period_elapsed(codec->playback_substream); 648 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) { 649 snd_wss_overrange(codec); 650 snd_pcm_period_elapsed(codec->capture_substream); 651 } 652 outb(0x00, OPTi93X_PORT(codec, STATUS)); 653 return IRQ_HANDLED; 654 } 655 656 #endif /* OPTi93X */ 657 658 static int snd_opti9xx_read_check(struct snd_card *card, 659 struct snd_opti9xx *chip) 660 { 661 unsigned char value; 662 #ifdef OPTi93X 663 unsigned long flags; 664 #endif 665 666 chip->res_mc_base = 667 devm_request_region(card->dev, chip->mc_base, 668 chip->mc_base_size, "OPTi9xx MC"); 669 if (!chip->res_mc_base) 670 return -EBUSY; 671 #ifndef OPTi93X 672 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)); 673 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1))) 674 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1))) 675 return 0; 676 #else /* OPTi93X */ 677 chip->res_mc_indir = 678 devm_request_region(card->dev, chip->mc_indir_index, 2, 679 "OPTi93x MC"); 680 if (!chip->res_mc_indir) 681 return -EBUSY; 682 683 spin_lock_irqsave(&chip->lock, flags); 684 outb(chip->password, chip->mc_base + chip->pwd_reg); 685 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base); 686 spin_unlock_irqrestore(&chip->lock, flags); 687 688 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)); 689 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value); 690 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value) 691 return 0; 692 693 devm_release_resource(card->dev, chip->res_mc_indir); 694 chip->res_mc_indir = NULL; 695 #endif /* OPTi93X */ 696 devm_release_resource(card->dev, chip->res_mc_base); 697 chip->res_mc_base = NULL; 698 699 return -ENODEV; 700 } 701 702 static int snd_card_opti9xx_detect(struct snd_card *card, 703 struct snd_opti9xx *chip) 704 { 705 int i, err; 706 707 #ifndef OPTi93X 708 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) { 709 #else 710 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) { 711 #endif 712 err = snd_opti9xx_init(chip, i); 713 if (err < 0) 714 return err; 715 716 err = snd_opti9xx_read_check(card, chip); 717 if (err == 0) 718 return 1; 719 #ifdef OPTi93X 720 chip->mc_indir_index = 0; 721 #endif 722 } 723 return -ENODEV; 724 } 725 726 #ifdef CONFIG_PNP 727 static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip, 728 struct pnp_card_link *card, 729 const struct pnp_card_device_id *pid) 730 { 731 struct pnp_dev *pdev; 732 int err; 733 struct pnp_dev *devmpu; 734 #ifndef OPTi93X 735 struct pnp_dev *devmc; 736 #endif 737 738 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL); 739 if (pdev == NULL) 740 return -EBUSY; 741 742 err = pnp_activate_dev(pdev); 743 if (err < 0) { 744 dev_err(chip->card->dev, "AUDIO pnp configure failure: %d\n", err); 745 return err; 746 } 747 748 #ifdef OPTi93X 749 port = pnp_port_start(pdev, 0) - 4; 750 fm_port = pnp_port_start(pdev, 1) + 8; 751 /* adjust mc_indir_index - some cards report it at 0xe?d, 752 other at 0xe?c but it really is always at 0xe?e */ 753 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe; 754 #else 755 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL); 756 if (devmc == NULL) 757 return -EBUSY; 758 759 err = pnp_activate_dev(devmc); 760 if (err < 0) { 761 dev_err(chip->card->dev, "MC pnp configure failure: %d\n", err); 762 return err; 763 } 764 765 port = pnp_port_start(pdev, 1); 766 fm_port = pnp_port_start(pdev, 2) + 8; 767 /* 768 * The MC(0) is never accessed and card does not 769 * include it in the PnP resource range. OPTI93x include it. 770 */ 771 chip->mc_base = pnp_port_start(devmc, 0) - 1; 772 chip->mc_base_size = pnp_port_len(devmc, 0) + 1; 773 #endif /* OPTi93X */ 774 irq = pnp_irq(pdev, 0); 775 dma1 = pnp_dma(pdev, 0); 776 #if defined(CS4231) || defined(OPTi93X) 777 dma2 = pnp_dma(pdev, 1); 778 #endif /* CS4231 || OPTi93X */ 779 780 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL); 781 782 if (devmpu && mpu_port > 0) { 783 err = pnp_activate_dev(devmpu); 784 if (err < 0) { 785 dev_err(chip->card->dev, "MPU401 pnp configure failure\n"); 786 mpu_port = -1; 787 } else { 788 mpu_port = pnp_port_start(devmpu, 0); 789 mpu_irq = pnp_irq(devmpu, 0); 790 } 791 } 792 return pid->driver_data; 793 } 794 #endif /* CONFIG_PNP */ 795 796 static int snd_opti9xx_probe(struct snd_card *card) 797 { 798 static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1}; 799 int error; 800 int xdma2; 801 struct snd_opti9xx *chip = card->private_data; 802 struct snd_wss *codec; 803 struct snd_rawmidi *rmidi; 804 struct snd_hwdep *synth; 805 806 #if defined(CS4231) || defined(OPTi93X) 807 xdma2 = dma2; 808 #else 809 xdma2 = -1; 810 #endif 811 812 if (port == SNDRV_AUTO_PORT) { 813 port = snd_legacy_find_free_ioport(possible_ports, 4); 814 if (port < 0) { 815 dev_err(card->dev, "unable to find a free WSS port\n"); 816 return -EBUSY; 817 } 818 } 819 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, 820 mpu_port, mpu_irq); 821 if (error) 822 return error; 823 824 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2, 825 #ifdef OPTi93X 826 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ, 827 #else 828 WSS_HW_DETECT, 0, 829 #endif 830 &codec); 831 if (error < 0) 832 return error; 833 chip->codec = codec; 834 error = snd_wss_pcm(codec, 0); 835 if (error < 0) 836 return error; 837 error = snd_wss_mixer(codec); 838 if (error < 0) 839 return error; 840 #ifdef OPTi93X 841 error = snd_opti93x_mixer(codec); 842 if (error < 0) 843 return error; 844 #endif 845 #ifdef CS4231 846 error = snd_wss_timer(codec, 0); 847 if (error < 0) 848 return error; 849 #endif 850 #ifdef OPTi93X 851 error = devm_request_irq(card->dev, irq, snd_opti93x_interrupt, 852 0, DEV_NAME" - WSS", chip); 853 if (error < 0) { 854 dev_err(card->dev, "opti9xx: can't grab IRQ %d\n", irq); 855 return error; 856 } 857 #endif 858 chip->irq = irq; 859 card->sync_irq = chip->irq; 860 strcpy(card->driver, chip->name); 861 sprintf(card->shortname, "OPTi %s", card->driver); 862 #if defined(CS4231) || defined(OPTi93X) 863 scnprintf(card->longname, sizeof(card->longname), 864 "%s, %s at 0x%lx, irq %d, dma %d&%d", 865 card->shortname, codec->pcm->name, 866 chip->wss_base + 4, irq, dma1, xdma2); 867 #else 868 scnprintf(card->longname, sizeof(card->longname), 869 "%s, %s at 0x%lx, irq %d, dma %d", 870 card->shortname, codec->pcm->name, chip->wss_base + 4, irq, 871 dma1); 872 #endif /* CS4231 || OPTi93X */ 873 874 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT) 875 rmidi = NULL; 876 else { 877 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, 878 mpu_port, 0, mpu_irq, &rmidi); 879 if (error) 880 dev_warn(card->dev, "no MPU-401 device at 0x%lx?\n", 881 mpu_port); 882 } 883 884 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) { 885 struct snd_opl3 *opl3 = NULL; 886 #ifndef OPTi93X 887 if (chip->hardware == OPTi9XX_HW_82C928 || 888 chip->hardware == OPTi9XX_HW_82C929 || 889 chip->hardware == OPTi9XX_HW_82C924) { 890 struct snd_opl4 *opl4; 891 /* assume we have an OPL4 */ 892 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 893 0x20, 0x20); 894 if (snd_opl4_create(card, fm_port, fm_port - 8, 895 2, &opl3, &opl4) < 0) { 896 /* no luck, use OPL3 instead */ 897 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 898 0x00, 0x20); 899 } 900 } 901 #endif /* !OPTi93X */ 902 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2, 903 OPL3_HW_AUTO, 0, &opl3) < 0) { 904 dev_warn(card->dev, "no OPL device at 0x%lx-0x%lx\n", 905 fm_port, fm_port + 4 - 1); 906 } 907 if (opl3) { 908 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth); 909 if (error < 0) 910 return error; 911 } 912 } 913 914 return snd_card_register(card); 915 } 916 917 static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp) 918 { 919 struct snd_card *card; 920 int err; 921 922 err = snd_devm_card_new(pdev, index, id, THIS_MODULE, 923 sizeof(struct snd_opti9xx), &card); 924 if (err < 0) 925 return err; 926 *cardp = card; 927 return 0; 928 } 929 930 static int snd_opti9xx_isa_match(struct device *devptr, 931 unsigned int dev) 932 { 933 #ifdef CONFIG_PNP 934 if (snd_opti9xx_pnp_is_probed) 935 return 0; 936 if (isapnp) 937 return 0; 938 #endif 939 return 1; 940 } 941 942 static int snd_opti9xx_isa_probe(struct device *devptr, 943 unsigned int dev) 944 { 945 struct snd_card *card; 946 int error; 947 static const long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1}; 948 #ifdef OPTi93X 949 static const int possible_irqs[] = {5, 9, 10, 11, 7, -1}; 950 #else 951 static const int possible_irqs[] = {9, 10, 11, 7, -1}; 952 #endif /* OPTi93X */ 953 static const int possible_mpu_irqs[] = {5, 9, 10, 7, -1}; 954 static const int possible_dma1s[] = {3, 1, 0, -1}; 955 #if defined(CS4231) || defined(OPTi93X) 956 static const int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}}; 957 #endif /* CS4231 || OPTi93X */ 958 959 if (mpu_port == SNDRV_AUTO_PORT) { 960 mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2); 961 if (mpu_port < 0) { 962 dev_err(devptr, "unable to find a free MPU401 port\n"); 963 return -EBUSY; 964 } 965 } 966 if (irq == SNDRV_AUTO_IRQ) { 967 irq = snd_legacy_find_free_irq(possible_irqs); 968 if (irq < 0) { 969 dev_err(devptr, "unable to find a free IRQ\n"); 970 return -EBUSY; 971 } 972 } 973 if (mpu_irq == SNDRV_AUTO_IRQ) { 974 mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs); 975 if (mpu_irq < 0) { 976 dev_err(devptr, "unable to find a free MPU401 IRQ\n"); 977 return -EBUSY; 978 } 979 } 980 if (dma1 == SNDRV_AUTO_DMA) { 981 dma1 = snd_legacy_find_free_dma(possible_dma1s); 982 if (dma1 < 0) { 983 dev_err(devptr, "unable to find a free DMA1\n"); 984 return -EBUSY; 985 } 986 } 987 #if defined(CS4231) || defined(OPTi93X) 988 if (dma2 == SNDRV_AUTO_DMA) { 989 dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]); 990 if (dma2 < 0) { 991 dev_err(devptr, "unable to find a free DMA2\n"); 992 return -EBUSY; 993 } 994 } 995 #endif 996 997 error = snd_opti9xx_card_new(devptr, &card); 998 if (error < 0) 999 return error; 1000 1001 error = snd_card_opti9xx_detect(card, card->private_data); 1002 if (error < 0) 1003 return error; 1004 error = snd_opti9xx_probe(card); 1005 if (error < 0) 1006 return error; 1007 dev_set_drvdata(devptr, card); 1008 return 0; 1009 } 1010 1011 #ifdef CONFIG_PM 1012 static int snd_opti9xx_suspend(struct snd_card *card) 1013 { 1014 struct snd_opti9xx *chip = card->private_data; 1015 1016 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1017 chip->codec->suspend(chip->codec); 1018 return 0; 1019 } 1020 1021 static int snd_opti9xx_resume(struct snd_card *card) 1022 { 1023 struct snd_opti9xx *chip = card->private_data; 1024 int error, xdma2; 1025 #if defined(CS4231) || defined(OPTi93X) 1026 xdma2 = dma2; 1027 #else 1028 xdma2 = -1; 1029 #endif 1030 1031 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, 1032 mpu_port, mpu_irq); 1033 if (error) 1034 return error; 1035 chip->codec->resume(chip->codec); 1036 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1037 return 0; 1038 } 1039 1040 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n, 1041 pm_message_t state) 1042 { 1043 return snd_opti9xx_suspend(dev_get_drvdata(dev)); 1044 } 1045 1046 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n) 1047 { 1048 return snd_opti9xx_resume(dev_get_drvdata(dev)); 1049 } 1050 #endif 1051 1052 static struct isa_driver snd_opti9xx_driver = { 1053 .match = snd_opti9xx_isa_match, 1054 .probe = snd_opti9xx_isa_probe, 1055 #ifdef CONFIG_PM 1056 .suspend = snd_opti9xx_isa_suspend, 1057 .resume = snd_opti9xx_isa_resume, 1058 #endif 1059 .driver = { 1060 .name = DEV_NAME 1061 }, 1062 }; 1063 1064 #ifdef CONFIG_PNP 1065 static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard, 1066 const struct pnp_card_device_id *pid) 1067 { 1068 struct snd_card *card; 1069 int error, hw; 1070 struct snd_opti9xx *chip; 1071 1072 if (snd_opti9xx_pnp_is_probed) 1073 return -EBUSY; 1074 if (! isapnp) 1075 return -ENODEV; 1076 error = snd_opti9xx_card_new(&pcard->card->dev, &card); 1077 if (error < 0) 1078 return error; 1079 chip = card->private_data; 1080 chip->card = card; 1081 1082 hw = snd_card_opti9xx_pnp(chip, pcard, pid); 1083 switch (hw) { 1084 case 0x0924: 1085 hw = OPTi9XX_HW_82C924; 1086 break; 1087 case 0x0925: 1088 hw = OPTi9XX_HW_82C925; 1089 break; 1090 case 0x0931: 1091 hw = OPTi9XX_HW_82C931; 1092 break; 1093 default: 1094 return -ENODEV; 1095 } 1096 1097 error = snd_opti9xx_init(chip, hw); 1098 if (error) 1099 return error; 1100 error = snd_opti9xx_read_check(card, chip); 1101 if (error) { 1102 dev_err(card->dev, "OPTI chip not found\n"); 1103 return error; 1104 } 1105 error = snd_opti9xx_probe(card); 1106 if (error < 0) 1107 return error; 1108 pnp_set_card_drvdata(pcard, card); 1109 snd_opti9xx_pnp_is_probed = 1; 1110 return 0; 1111 } 1112 1113 static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard) 1114 { 1115 snd_opti9xx_pnp_is_probed = 0; 1116 } 1117 1118 #ifdef CONFIG_PM 1119 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard, 1120 pm_message_t state) 1121 { 1122 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard)); 1123 } 1124 1125 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard) 1126 { 1127 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard)); 1128 } 1129 #endif 1130 1131 static struct pnp_card_driver opti9xx_pnpc_driver = { 1132 .flags = PNP_DRIVER_RES_DISABLE, 1133 .name = DEV_NAME, 1134 .id_table = snd_opti9xx_pnpids, 1135 .probe = snd_opti9xx_pnp_probe, 1136 .remove = snd_opti9xx_pnp_remove, 1137 #ifdef CONFIG_PM 1138 .suspend = snd_opti9xx_pnp_suspend, 1139 .resume = snd_opti9xx_pnp_resume, 1140 #endif 1141 }; 1142 #endif 1143 1144 #ifdef OPTi93X 1145 #define CHIP_NAME "82C93x" 1146 #else 1147 #define CHIP_NAME "82C92x" 1148 #endif 1149 1150 static int __init alsa_card_opti9xx_init(void) 1151 { 1152 #ifdef CONFIG_PNP 1153 pnp_register_card_driver(&opti9xx_pnpc_driver); 1154 if (snd_opti9xx_pnp_is_probed) 1155 return 0; 1156 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1157 #endif 1158 return isa_register_driver(&snd_opti9xx_driver, 1); 1159 } 1160 1161 static void __exit alsa_card_opti9xx_exit(void) 1162 { 1163 if (!snd_opti9xx_pnp_is_probed) { 1164 isa_unregister_driver(&snd_opti9xx_driver); 1165 return; 1166 } 1167 #ifdef CONFIG_PNP 1168 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1169 #endif 1170 } 1171 1172 module_init(alsa_card_opti9xx_init) 1173 module_exit(alsa_card_opti9xx_exit) 1174