1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Matt Wu <Matt_Wu@acersoftech.com.cn> 4 * Apr 26, 2001 5 * Routines for control of ALi pci audio M5451 6 * 7 * BUGS: 8 * -- 9 * 10 * TODO: 11 * -- 12 */ 13 14 #include <linux/io.h> 15 #include <linux/delay.h> 16 #include <linux/interrupt.h> 17 #include <linux/init.h> 18 #include <linux/pci.h> 19 #include <linux/slab.h> 20 #include <linux/module.h> 21 #include <linux/dma-mapping.h> 22 #include <sound/core.h> 23 #include <sound/pcm.h> 24 #include <sound/info.h> 25 #include <sound/ac97_codec.h> 26 #include <sound/mpu401.h> 27 #include <sound/initval.h> 28 29 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>"); 30 MODULE_DESCRIPTION("ALI M5451"); 31 MODULE_LICENSE("GPL"); 32 MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}"); 33 34 static int index = SNDRV_DEFAULT_IDX1; /* Index */ 35 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ 36 static int pcm_channels = 32; 37 static bool spdif; 38 39 module_param(index, int, 0444); 40 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio."); 41 module_param(id, charp, 0444); 42 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio."); 43 module_param(pcm_channels, int, 0444); 44 MODULE_PARM_DESC(pcm_channels, "PCM Channels"); 45 module_param(spdif, bool, 0444); 46 MODULE_PARM_DESC(spdif, "Support SPDIF I/O"); 47 48 /* just for backward compatibility */ 49 static bool enable; 50 module_param(enable, bool, 0444); 51 52 53 /* 54 * Constants definition 55 */ 56 57 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451) 58 59 60 #define ALI_CHANNELS 32 61 62 #define ALI_PCM_IN_CHANNEL 31 63 #define ALI_SPDIF_IN_CHANNEL 19 64 #define ALI_SPDIF_OUT_CHANNEL 15 65 #define ALI_CENTER_CHANNEL 24 66 #define ALI_LEF_CHANNEL 23 67 #define ALI_SURR_LEFT_CHANNEL 26 68 #define ALI_SURR_RIGHT_CHANNEL 25 69 #define ALI_MODEM_IN_CHANNEL 21 70 #define ALI_MODEM_OUT_CHANNEL 20 71 72 #define SNDRV_ALI_VOICE_TYPE_PCM 01 73 #define SNDRV_ALI_VOICE_TYPE_OTH 02 74 75 #define ALI_5451_V02 0x02 76 77 /* 78 * Direct Registers 79 */ 80 81 #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */ 82 #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */ 83 #define ALI_LEGACY_DMAR11 0x0b /* MOD */ 84 #define ALI_LEGACY_DMAR15 0x0f /* MMR */ 85 #define ALI_MPUR0 0x20 86 #define ALI_MPUR1 0x21 87 #define ALI_MPUR2 0x22 88 #define ALI_MPUR3 0x23 89 90 #define ALI_AC97_WRITE 0x40 91 #define ALI_AC97_READ 0x44 92 93 #define ALI_SCTRL 0x48 94 #define ALI_SPDIF_OUT_ENABLE 0x20 95 #define ALI_SCTRL_LINE_IN2 (1 << 9) 96 #define ALI_SCTRL_GPIO_IN2 (1 << 13) 97 #define ALI_SCTRL_LINE_OUT_EN (1 << 20) 98 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23) 99 #define ALI_SCTRL_CODEC1_READY (1 << 24) 100 #define ALI_SCTRL_CODEC2_READY (1 << 25) 101 #define ALI_AC97_GPIO 0x4c 102 #define ALI_AC97_GPIO_ENABLE 0x8000 103 #define ALI_AC97_GPIO_DATA_SHIFT 16 104 #define ALI_SPDIF_CS 0x70 105 #define ALI_SPDIF_CTRL 0x74 106 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02 107 #define ALI_SPDIF_IN_CH_STATUS 0x40 108 #define ALI_SPDIF_OUT_CH_STATUS 0xbf 109 #define ALI_START 0x80 110 #define ALI_STOP 0x84 111 #define ALI_CSPF 0x90 112 #define ALI_AINT 0x98 113 #define ALI_GC_CIR 0xa0 114 #define ENDLP_IE 0x00001000 115 #define MIDLP_IE 0x00002000 116 #define ALI_AINTEN 0xa4 117 #define ALI_VOLUME 0xa8 118 #define ALI_SBDELTA_DELTA_R 0xac 119 #define ALI_MISCINT 0xb0 120 #define ADDRESS_IRQ 0x00000020 121 #define TARGET_REACHED 0x00008000 122 #define MIXER_OVERFLOW 0x00000800 123 #define MIXER_UNDERFLOW 0x00000400 124 #define GPIO_IRQ 0x01000000 125 #define ALI_SBBL_SBCL 0xc0 126 #define ALI_SBCTRL_SBE2R_SBDD 0xc4 127 #define ALI_STIMER 0xc8 128 #define ALI_GLOBAL_CONTROL 0xd4 129 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */ 130 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */ 131 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */ 132 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */ 133 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */ 134 135 #define ALI_CSO_ALPHA_FMS 0xe0 136 #define ALI_LBA 0xe4 137 #define ALI_ESO_DELTA 0xe8 138 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0 139 #define ALI_EBUF1 0xf4 140 #define ALI_EBUF2 0xf8 141 142 #define ALI_REG(codec, x) ((codec)->port + x) 143 144 #define MAX_CODECS 2 145 146 147 struct snd_ali; 148 struct snd_ali_voice; 149 150 struct snd_ali_channel_control { 151 /* register data */ 152 struct REGDATA { 153 unsigned int start; 154 unsigned int stop; 155 unsigned int aint; 156 unsigned int ainten; 157 } data; 158 159 /* register addresses */ 160 struct REGS { 161 unsigned int start; 162 unsigned int stop; 163 unsigned int aint; 164 unsigned int ainten; 165 unsigned int ac97read; 166 unsigned int ac97write; 167 } regs; 168 169 }; 170 171 struct snd_ali_voice { 172 unsigned int number; 173 unsigned int use :1, 174 pcm :1, 175 midi :1, 176 mode :1, 177 synth :1, 178 running :1; 179 180 /* PCM data */ 181 struct snd_ali *codec; 182 struct snd_pcm_substream *substream; 183 struct snd_ali_voice *extra; 184 185 int eso; /* final ESO value for channel */ 186 int count; /* runtime->period_size */ 187 188 /* --- */ 189 190 void *private_data; 191 void (*private_free)(void *private_data); 192 }; 193 194 195 struct snd_alidev { 196 197 struct snd_ali_voice voices[ALI_CHANNELS]; 198 199 unsigned int chcnt; /* num of opened channels */ 200 unsigned int chmap; /* bitmap for opened channels */ 201 unsigned int synthcount; 202 203 }; 204 205 206 #define ALI_GLOBAL_REGS 56 207 #define ALI_CHANNEL_REGS 8 208 struct snd_ali_image { 209 u32 regs[ALI_GLOBAL_REGS]; 210 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS]; 211 }; 212 213 214 struct snd_ali { 215 int irq; 216 unsigned long port; 217 unsigned char revision; 218 219 unsigned int hw_initialized :1; 220 unsigned int spdif_support :1; 221 222 struct pci_dev *pci; 223 struct pci_dev *pci_m1533; 224 struct pci_dev *pci_m7101; 225 226 struct snd_card *card; 227 struct snd_pcm *pcm[MAX_CODECS]; 228 struct snd_alidev synth; 229 struct snd_ali_channel_control chregs; 230 231 /* S/PDIF Mask */ 232 unsigned int spdif_mask; 233 234 unsigned int spurious_irq_count; 235 unsigned int spurious_irq_max_delta; 236 237 unsigned int num_of_codecs; 238 239 struct snd_ac97_bus *ac97_bus; 240 struct snd_ac97 *ac97[MAX_CODECS]; 241 unsigned short ac97_ext_id; 242 unsigned short ac97_ext_status; 243 244 spinlock_t reg_lock; 245 spinlock_t voice_alloc; 246 247 #ifdef CONFIG_PM_SLEEP 248 struct snd_ali_image *image; 249 #endif 250 }; 251 252 static const struct pci_device_id snd_ali_ids[] = { 253 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0}, 254 {0, } 255 }; 256 MODULE_DEVICE_TABLE(pci, snd_ali_ids); 257 258 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int); 259 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short); 260 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short, 261 unsigned short); 262 263 /* 264 * AC97 ACCESS 265 */ 266 267 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec, 268 unsigned int port) 269 { 270 return (unsigned int)inl(ALI_REG(codec, port)); 271 } 272 273 static inline void snd_ali_5451_poke(struct snd_ali *codec, 274 unsigned int port, 275 unsigned int val) 276 { 277 outl((unsigned int)val, ALI_REG(codec, port)); 278 } 279 280 static int snd_ali_codec_ready(struct snd_ali *codec, 281 unsigned int port) 282 { 283 unsigned long end_time; 284 unsigned int res; 285 286 end_time = jiffies + msecs_to_jiffies(250); 287 288 for (;;) { 289 res = snd_ali_5451_peek(codec,port); 290 if (!(res & 0x8000)) 291 return 0; 292 if (!time_after_eq(end_time, jiffies)) 293 break; 294 schedule_timeout_uninterruptible(1); 295 } 296 297 snd_ali_5451_poke(codec, port, res & ~0x8000); 298 dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n "); 299 return -EIO; 300 } 301 302 static int snd_ali_stimer_ready(struct snd_ali *codec) 303 { 304 unsigned long end_time; 305 unsigned long dwChk1,dwChk2; 306 307 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER); 308 end_time = jiffies + msecs_to_jiffies(250); 309 310 for (;;) { 311 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER); 312 if (dwChk2 != dwChk1) 313 return 0; 314 if (!time_after_eq(end_time, jiffies)) 315 break; 316 schedule_timeout_uninterruptible(1); 317 } 318 319 dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n"); 320 return -EIO; 321 } 322 323 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary, 324 unsigned short reg, 325 unsigned short val) 326 { 327 unsigned int dwVal; 328 unsigned int port; 329 330 if (reg >= 0x80) { 331 dev_err(codec->card->dev, 332 "ali_codec_poke: reg(%xh) invalid.\n", reg); 333 return; 334 } 335 336 port = codec->chregs.regs.ac97write; 337 338 if (snd_ali_codec_ready(codec, port) < 0) 339 return; 340 if (snd_ali_stimer_ready(codec) < 0) 341 return; 342 343 dwVal = (unsigned int) (reg & 0xff); 344 dwVal |= 0x8000 | (val << 16); 345 if (secondary) 346 dwVal |= 0x0080; 347 if (codec->revision == ALI_5451_V02) 348 dwVal |= 0x0100; 349 350 snd_ali_5451_poke(codec, port, dwVal); 351 352 return ; 353 } 354 355 static unsigned short snd_ali_codec_peek(struct snd_ali *codec, 356 int secondary, 357 unsigned short reg) 358 { 359 unsigned int dwVal; 360 unsigned int port; 361 362 if (reg >= 0x80) { 363 dev_err(codec->card->dev, 364 "ali_codec_peek: reg(%xh) invalid.\n", reg); 365 return ~0; 366 } 367 368 port = codec->chregs.regs.ac97read; 369 370 if (snd_ali_codec_ready(codec, port) < 0) 371 return ~0; 372 if (snd_ali_stimer_ready(codec) < 0) 373 return ~0; 374 375 dwVal = (unsigned int) (reg & 0xff); 376 dwVal |= 0x8000; /* bit 15*/ 377 if (secondary) 378 dwVal |= 0x0080; 379 380 snd_ali_5451_poke(codec, port, dwVal); 381 382 if (snd_ali_stimer_ready(codec) < 0) 383 return ~0; 384 if (snd_ali_codec_ready(codec, port) < 0) 385 return ~0; 386 387 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16; 388 } 389 390 static void snd_ali_codec_write(struct snd_ac97 *ac97, 391 unsigned short reg, 392 unsigned short val ) 393 { 394 struct snd_ali *codec = ac97->private_data; 395 396 dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val); 397 if (reg == AC97_GPIO_STATUS) { 398 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE, 399 ALI_REG(codec, ALI_AC97_GPIO)); 400 return; 401 } 402 snd_ali_codec_poke(codec, ac97->num, reg, val); 403 return ; 404 } 405 406 407 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97, 408 unsigned short reg) 409 { 410 struct snd_ali *codec = ac97->private_data; 411 412 dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg); 413 return snd_ali_codec_peek(codec, ac97->num, reg); 414 } 415 416 /* 417 * AC97 Reset 418 */ 419 420 static int snd_ali_reset_5451(struct snd_ali *codec) 421 { 422 struct pci_dev *pci_dev; 423 unsigned short wCount, wReg; 424 unsigned int dwVal; 425 426 pci_dev = codec->pci_m1533; 427 if (pci_dev) { 428 pci_read_config_dword(pci_dev, 0x7c, &dwVal); 429 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000); 430 mdelay(5); 431 pci_read_config_dword(pci_dev, 0x7c, &dwVal); 432 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff); 433 mdelay(5); 434 } 435 436 pci_dev = codec->pci; 437 pci_read_config_dword(pci_dev, 0x44, &dwVal); 438 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000); 439 udelay(500); 440 pci_read_config_dword(pci_dev, 0x44, &dwVal); 441 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff); 442 mdelay(5); 443 444 wCount = 200; 445 while(wCount--) { 446 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN); 447 if ((wReg & 0x000f) == 0x000f) 448 return 0; 449 mdelay(5); 450 } 451 452 /* non-fatal if you have a non PM capable codec */ 453 /* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */ 454 return 0; 455 } 456 457 /* 458 * ALI 5451 Controller 459 */ 460 461 static void snd_ali_enable_special_channel(struct snd_ali *codec, 462 unsigned int channel) 463 { 464 unsigned long dwVal; 465 466 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)); 467 dwVal |= 1 << (channel & 0x0000001f); 468 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 469 } 470 471 static void snd_ali_disable_special_channel(struct snd_ali *codec, 472 unsigned int channel) 473 { 474 unsigned long dwVal; 475 476 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)); 477 dwVal &= ~(1 << (channel & 0x0000001f)); 478 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 479 } 480 481 static void snd_ali_enable_address_interrupt(struct snd_ali *codec) 482 { 483 unsigned int gc; 484 485 gc = inl(ALI_REG(codec, ALI_GC_CIR)); 486 gc |= ENDLP_IE; 487 gc |= MIDLP_IE; 488 outl( gc, ALI_REG(codec, ALI_GC_CIR)); 489 } 490 491 static void snd_ali_disable_address_interrupt(struct snd_ali *codec) 492 { 493 unsigned int gc; 494 495 gc = inl(ALI_REG(codec, ALI_GC_CIR)); 496 gc &= ~ENDLP_IE; 497 gc &= ~MIDLP_IE; 498 outl(gc, ALI_REG(codec, ALI_GC_CIR)); 499 } 500 501 static void snd_ali_disable_voice_irq(struct snd_ali *codec, 502 unsigned int channel) 503 { 504 unsigned int mask; 505 struct snd_ali_channel_control *pchregs = &(codec->chregs); 506 507 dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel); 508 509 mask = 1 << (channel & 0x1f); 510 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten)); 511 pchregs->data.ainten &= ~mask; 512 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten)); 513 } 514 515 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel) 516 { 517 unsigned int idx = channel & 0x1f; 518 519 if (codec->synth.chcnt >= ALI_CHANNELS){ 520 dev_err(codec->card->dev, 521 "ali_alloc_pcm_channel: no free channels.\n"); 522 return -1; 523 } 524 525 if (!(codec->synth.chmap & (1 << idx))) { 526 codec->synth.chmap |= 1 << idx; 527 codec->synth.chcnt++; 528 dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx); 529 return idx; 530 } 531 return -1; 532 } 533 534 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec) 535 { 536 int idx; 537 int result = -1; 538 539 dev_dbg(codec->card->dev, 540 "find_free_channel: for %s\n", rec ? "rec" : "pcm"); 541 542 /* recording */ 543 if (rec) { 544 if (codec->spdif_support && 545 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & 546 ALI_SPDIF_IN_SUPPORT)) 547 idx = ALI_SPDIF_IN_CHANNEL; 548 else 549 idx = ALI_PCM_IN_CHANNEL; 550 551 result = snd_ali_alloc_pcm_channel(codec, idx); 552 if (result >= 0) 553 return result; 554 else { 555 dev_err(codec->card->dev, 556 "ali_find_free_channel: record channel is busy now.\n"); 557 return -1; 558 } 559 } 560 561 /* playback... */ 562 if (codec->spdif_support && 563 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & 564 ALI_SPDIF_OUT_CH_ENABLE)) { 565 idx = ALI_SPDIF_OUT_CHANNEL; 566 result = snd_ali_alloc_pcm_channel(codec, idx); 567 if (result >= 0) 568 return result; 569 else 570 dev_err(codec->card->dev, 571 "ali_find_free_channel: S/PDIF out channel is in busy now.\n"); 572 } 573 574 for (idx = 0; idx < ALI_CHANNELS; idx++) { 575 result = snd_ali_alloc_pcm_channel(codec, idx); 576 if (result >= 0) 577 return result; 578 } 579 dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n"); 580 return -1; 581 } 582 583 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel) 584 { 585 unsigned int idx = channel & 0x0000001f; 586 587 dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel); 588 589 if (channel < 0 || channel >= ALI_CHANNELS) 590 return; 591 592 if (!(codec->synth.chmap & (1 << idx))) { 593 dev_err(codec->card->dev, 594 "ali_free_channel_pcm: channel %d is not in use.\n", 595 channel); 596 return; 597 } else { 598 codec->synth.chmap &= ~(1 << idx); 599 codec->synth.chcnt--; 600 } 601 } 602 603 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel) 604 { 605 unsigned int mask = 1 << (channel & 0x1f); 606 607 dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel); 608 outl(mask, ALI_REG(codec, codec->chregs.regs.stop)); 609 } 610 611 /* 612 * S/PDIF Part 613 */ 614 615 static void snd_ali_delay(struct snd_ali *codec,int interval) 616 { 617 unsigned long begintimer,currenttimer; 618 619 begintimer = inl(ALI_REG(codec, ALI_STIMER)); 620 currenttimer = inl(ALI_REG(codec, ALI_STIMER)); 621 622 while (currenttimer < begintimer + interval) { 623 if (snd_ali_stimer_ready(codec) < 0) 624 break; 625 currenttimer = inl(ALI_REG(codec, ALI_STIMER)); 626 cpu_relax(); 627 } 628 } 629 630 static void snd_ali_detect_spdif_rate(struct snd_ali *codec) 631 { 632 u16 wval; 633 u16 count = 0; 634 u8 bval, R1 = 0, R2; 635 636 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1)); 637 bval |= 0x1F; 638 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1)); 639 640 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) { 641 count ++; 642 snd_ali_delay(codec, 6); 643 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1)); 644 R1 = bval & 0x1F; 645 } 646 647 if (count > 50000) { 648 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n"); 649 return; 650 } 651 652 for (count = 0; count <= 50000; count++) { 653 snd_ali_delay(codec, 6); 654 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1)); 655 R2 = bval & 0x1F; 656 if (R2 != R1) 657 R1 = R2; 658 else 659 break; 660 } 661 662 if (count > 50000) { 663 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n"); 664 return; 665 } 666 667 if (R2 >= 0x0b && R2 <= 0x0e) { 668 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2)); 669 wval &= 0xe0f0; 670 wval |= (0x09 << 8) | 0x05; 671 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2)); 672 673 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0; 674 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3)); 675 } else if (R2 == 0x12) { 676 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2)); 677 wval &= 0xe0f0; 678 wval |= (0x0e << 8) | 0x08; 679 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2)); 680 681 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0; 682 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3)); 683 } 684 } 685 686 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec) 687 { 688 u32 dwRate; 689 u8 bval; 690 691 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); 692 bval &= 0x7f; 693 bval |= 0x40; 694 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL)); 695 696 snd_ali_detect_spdif_rate(codec); 697 698 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)); 699 bval &= 0x0f; 700 701 switch (bval) { 702 case 0: dwRate = 44100; break; 703 case 1: dwRate = 48000; break; 704 case 2: dwRate = 32000; break; 705 default: dwRate = 0; break; 706 } 707 708 return dwRate; 709 } 710 711 static void snd_ali_enable_spdif_in(struct snd_ali *codec) 712 { 713 unsigned int dwVal; 714 715 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)); 716 dwVal |= ALI_SPDIF_IN_SUPPORT; 717 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 718 719 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); 720 dwVal |= 0x02; 721 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL)); 722 723 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL); 724 } 725 726 static void snd_ali_disable_spdif_in(struct snd_ali *codec) 727 { 728 unsigned int dwVal; 729 730 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)); 731 dwVal &= ~ALI_SPDIF_IN_SUPPORT; 732 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 733 734 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL); 735 } 736 737 738 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate) 739 { 740 unsigned char bVal; 741 unsigned int dwRate; 742 743 switch (rate) { 744 case 32000: dwRate = 0x300; break; 745 case 48000: dwRate = 0x200; break; 746 default: dwRate = 0; break; 747 } 748 749 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); 750 bVal &= (unsigned char)(~(1<<6)); 751 752 bVal |= 0x80; /* select right */ 753 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL)); 754 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2)); 755 756 bVal &= ~0x80; /* select left */ 757 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL)); 758 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2)); 759 } 760 761 static void snd_ali_enable_spdif_out(struct snd_ali *codec) 762 { 763 unsigned short wVal; 764 unsigned char bVal; 765 struct pci_dev *pci_dev; 766 767 pci_dev = codec->pci_m1533; 768 if (pci_dev == NULL) 769 return; 770 pci_read_config_byte(pci_dev, 0x61, &bVal); 771 bVal |= 0x40; 772 pci_write_config_byte(pci_dev, 0x61, bVal); 773 pci_read_config_byte(pci_dev, 0x7d, &bVal); 774 bVal |= 0x01; 775 pci_write_config_byte(pci_dev, 0x7d, bVal); 776 777 pci_read_config_byte(pci_dev, 0x7e, &bVal); 778 bVal &= (~0x20); 779 bVal |= 0x10; 780 pci_write_config_byte(pci_dev, 0x7e, bVal); 781 782 bVal = inb(ALI_REG(codec, ALI_SCTRL)); 783 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL)); 784 785 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); 786 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL)); 787 788 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL)); 789 wVal |= ALI_SPDIF_OUT_SEL_PCM; 790 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 791 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL); 792 } 793 794 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec) 795 { 796 unsigned short wVal; 797 798 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL)); 799 wVal &= ~ALI_SPDIF_OUT_SEL_PCM; 800 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 801 /* 802 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS)); 803 if (flag & ALI_SPDIF_OUT_NON_PCM) 804 wVal |= 0x0002; 805 else 806 wVal &= (~0x0002); 807 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS)); 808 */ 809 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL); 810 } 811 812 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec) 813 { 814 unsigned short wVal; 815 816 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL)); 817 wVal |= ALI_SPDIF_OUT_SEL_PCM; 818 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 819 820 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL); 821 } 822 823 static void snd_ali_disable_spdif_out(struct snd_ali *codec) 824 { 825 unsigned char bVal; 826 827 bVal = inb(ALI_REG(codec, ALI_SCTRL)); 828 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL)); 829 830 snd_ali_disable_spdif_chnout(codec); 831 } 832 833 static void snd_ali_update_ptr(struct snd_ali *codec, int channel) 834 { 835 struct snd_ali_voice *pvoice; 836 struct snd_ali_channel_control *pchregs; 837 unsigned int old, mask; 838 839 pchregs = &(codec->chregs); 840 841 /* check if interrupt occurred for channel */ 842 old = pchregs->data.aint; 843 mask = 1U << (channel & 0x1f); 844 845 if (!(old & mask)) 846 return; 847 848 pvoice = &codec->synth.voices[channel]; 849 850 udelay(100); 851 spin_lock(&codec->reg_lock); 852 853 if (pvoice->pcm && pvoice->substream) { 854 /* pcm interrupt */ 855 if (pvoice->running) { 856 dev_dbg(codec->card->dev, 857 "update_ptr: cso=%4.4x cspf=%d.\n", 858 inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)), 859 (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask); 860 spin_unlock(&codec->reg_lock); 861 snd_pcm_period_elapsed(pvoice->substream); 862 spin_lock(&codec->reg_lock); 863 } else { 864 snd_ali_stop_voice(codec, channel); 865 snd_ali_disable_voice_irq(codec, channel); 866 } 867 } else if (codec->synth.voices[channel].synth) { 868 /* synth interrupt */ 869 } else if (codec->synth.voices[channel].midi) { 870 /* midi interrupt */ 871 } else { 872 /* unknown interrupt */ 873 snd_ali_stop_voice(codec, channel); 874 snd_ali_disable_voice_irq(codec, channel); 875 } 876 spin_unlock(&codec->reg_lock); 877 outl(mask,ALI_REG(codec,pchregs->regs.aint)); 878 pchregs->data.aint = old & (~mask); 879 } 880 881 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id) 882 { 883 struct snd_ali *codec = dev_id; 884 int channel; 885 unsigned int audio_int; 886 struct snd_ali_channel_control *pchregs; 887 888 if (codec == NULL || !codec->hw_initialized) 889 return IRQ_NONE; 890 891 audio_int = inl(ALI_REG(codec, ALI_MISCINT)); 892 if (!audio_int) 893 return IRQ_NONE; 894 895 pchregs = &(codec->chregs); 896 if (audio_int & ADDRESS_IRQ) { 897 /* get interrupt status for all channels */ 898 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint)); 899 for (channel = 0; channel < ALI_CHANNELS; channel++) 900 snd_ali_update_ptr(codec, channel); 901 } 902 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), 903 ALI_REG(codec, ALI_MISCINT)); 904 905 return IRQ_HANDLED; 906 } 907 908 909 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec, 910 int type, int rec, int channel) 911 { 912 struct snd_ali_voice *pvoice; 913 int idx; 914 915 dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec); 916 917 spin_lock_irq(&codec->voice_alloc); 918 if (type == SNDRV_ALI_VOICE_TYPE_PCM) { 919 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) : 920 snd_ali_find_free_channel(codec,rec); 921 if (idx < 0) { 922 dev_err(codec->card->dev, "ali_alloc_voice: err.\n"); 923 spin_unlock_irq(&codec->voice_alloc); 924 return NULL; 925 } 926 pvoice = &(codec->synth.voices[idx]); 927 pvoice->codec = codec; 928 pvoice->use = 1; 929 pvoice->pcm = 1; 930 pvoice->mode = rec; 931 spin_unlock_irq(&codec->voice_alloc); 932 return pvoice; 933 } 934 spin_unlock_irq(&codec->voice_alloc); 935 return NULL; 936 } 937 938 939 static void snd_ali_free_voice(struct snd_ali * codec, 940 struct snd_ali_voice *pvoice) 941 { 942 void (*private_free)(void *); 943 void *private_data; 944 945 dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number); 946 if (!pvoice->use) 947 return; 948 snd_ali_clear_voices(codec, pvoice->number, pvoice->number); 949 spin_lock_irq(&codec->voice_alloc); 950 private_free = pvoice->private_free; 951 private_data = pvoice->private_data; 952 pvoice->private_free = NULL; 953 pvoice->private_data = NULL; 954 if (pvoice->pcm) 955 snd_ali_free_channel_pcm(codec, pvoice->number); 956 pvoice->use = pvoice->pcm = pvoice->synth = 0; 957 pvoice->substream = NULL; 958 spin_unlock_irq(&codec->voice_alloc); 959 if (private_free) 960 private_free(private_data); 961 } 962 963 964 static void snd_ali_clear_voices(struct snd_ali *codec, 965 unsigned int v_min, 966 unsigned int v_max) 967 { 968 unsigned int i; 969 970 for (i = v_min; i <= v_max; i++) { 971 snd_ali_stop_voice(codec, i); 972 snd_ali_disable_voice_irq(codec, i); 973 } 974 } 975 976 static void snd_ali_write_voice_regs(struct snd_ali *codec, 977 unsigned int Channel, 978 unsigned int LBA, 979 unsigned int CSO, 980 unsigned int ESO, 981 unsigned int DELTA, 982 unsigned int ALPHA_FMS, 983 unsigned int GVSEL, 984 unsigned int PAN, 985 unsigned int VOL, 986 unsigned int CTRL, 987 unsigned int EC) 988 { 989 unsigned int ctlcmds[4]; 990 991 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR)); 992 993 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff); 994 ctlcmds[1] = LBA; 995 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff); 996 ctlcmds[3] = (GVSEL << 31) | 997 ((PAN & 0x0000007f) << 24) | 998 ((VOL & 0x000000ff) << 16) | 999 ((CTRL & 0x0000000f) << 12) | 1000 (EC & 0x00000fff); 1001 1002 outb(Channel, ALI_REG(codec, ALI_GC_CIR)); 1003 1004 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS)); 1005 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA)); 1006 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA)); 1007 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC)); 1008 1009 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */ 1010 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */ 1011 } 1012 1013 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec) 1014 { 1015 unsigned int delta; 1016 1017 if (rate < 4000) 1018 rate = 4000; 1019 if (rate > 48000) 1020 rate = 48000; 1021 1022 if (rec) { 1023 if (rate == 44100) 1024 delta = 0x116a; 1025 else if (rate == 8000) 1026 delta = 0x6000; 1027 else if (rate == 48000) 1028 delta = 0x1000; 1029 else 1030 delta = ((48000 << 12) / rate) & 0x0000ffff; 1031 } else { 1032 if (rate == 44100) 1033 delta = 0xeb3; 1034 else if (rate == 8000) 1035 delta = 0x2ab; 1036 else if (rate == 48000) 1037 delta = 0x1000; 1038 else 1039 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff; 1040 } 1041 1042 return delta; 1043 } 1044 1045 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream) 1046 { 1047 unsigned int CTRL; 1048 struct snd_pcm_runtime *runtime = substream->runtime; 1049 1050 /* set ctrl mode 1051 CTRL default: 8-bit (unsigned) mono, loop mode enabled 1052 */ 1053 CTRL = 0x00000001; 1054 if (snd_pcm_format_width(runtime->format) == 16) 1055 CTRL |= 0x00000008; /* 16-bit data */ 1056 if (!snd_pcm_format_unsigned(runtime->format)) 1057 CTRL |= 0x00000002; /* signed data */ 1058 if (runtime->channels > 1) 1059 CTRL |= 0x00000004; /* stereo data */ 1060 return CTRL; 1061 } 1062 1063 /* 1064 * PCM part 1065 */ 1066 1067 static int snd_ali_trigger(struct snd_pcm_substream *substream, 1068 int cmd) 1069 1070 { 1071 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1072 struct snd_pcm_substream *s; 1073 unsigned int what, whati, capture_flag; 1074 struct snd_ali_voice *pvoice, *evoice; 1075 unsigned int val; 1076 int do_start; 1077 1078 switch (cmd) { 1079 case SNDRV_PCM_TRIGGER_START: 1080 case SNDRV_PCM_TRIGGER_RESUME: 1081 do_start = 1; 1082 break; 1083 case SNDRV_PCM_TRIGGER_STOP: 1084 case SNDRV_PCM_TRIGGER_SUSPEND: 1085 do_start = 0; 1086 break; 1087 default: 1088 return -EINVAL; 1089 } 1090 1091 what = whati = capture_flag = 0; 1092 snd_pcm_group_for_each_entry(s, substream) { 1093 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) { 1094 pvoice = s->runtime->private_data; 1095 evoice = pvoice->extra; 1096 what |= 1 << (pvoice->number & 0x1f); 1097 if (evoice == NULL) 1098 whati |= 1 << (pvoice->number & 0x1f); 1099 else { 1100 whati |= 1 << (evoice->number & 0x1f); 1101 what |= 1 << (evoice->number & 0x1f); 1102 } 1103 if (do_start) { 1104 pvoice->running = 1; 1105 if (evoice != NULL) 1106 evoice->running = 1; 1107 } else { 1108 pvoice->running = 0; 1109 if (evoice != NULL) 1110 evoice->running = 0; 1111 } 1112 snd_pcm_trigger_done(s, substream); 1113 if (pvoice->mode) 1114 capture_flag = 1; 1115 } 1116 } 1117 spin_lock(&codec->reg_lock); 1118 if (!do_start) 1119 outl(what, ALI_REG(codec, ALI_STOP)); 1120 val = inl(ALI_REG(codec, ALI_AINTEN)); 1121 if (do_start) 1122 val |= whati; 1123 else 1124 val &= ~whati; 1125 outl(val, ALI_REG(codec, ALI_AINTEN)); 1126 if (do_start) 1127 outl(what, ALI_REG(codec, ALI_START)); 1128 dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati); 1129 spin_unlock(&codec->reg_lock); 1130 1131 return 0; 1132 } 1133 1134 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream, 1135 struct snd_pcm_hw_params *hw_params) 1136 { 1137 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1138 struct snd_pcm_runtime *runtime = substream->runtime; 1139 struct snd_ali_voice *pvoice = runtime->private_data; 1140 struct snd_ali_voice *evoice = pvoice->extra; 1141 1142 /* voice management */ 1143 1144 if (params_buffer_size(hw_params) / 2 != 1145 params_period_size(hw_params)) { 1146 if (!evoice) { 1147 evoice = snd_ali_alloc_voice(codec, 1148 SNDRV_ALI_VOICE_TYPE_PCM, 1149 0, -1); 1150 if (!evoice) 1151 return -ENOMEM; 1152 pvoice->extra = evoice; 1153 evoice->substream = substream; 1154 } 1155 } else { 1156 if (evoice) { 1157 snd_ali_free_voice(codec, evoice); 1158 pvoice->extra = evoice = NULL; 1159 } 1160 } 1161 1162 return 0; 1163 } 1164 1165 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream) 1166 { 1167 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1168 struct snd_pcm_runtime *runtime = substream->runtime; 1169 struct snd_ali_voice *pvoice = runtime->private_data; 1170 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL; 1171 1172 if (evoice) { 1173 snd_ali_free_voice(codec, evoice); 1174 pvoice->extra = NULL; 1175 } 1176 return 0; 1177 } 1178 1179 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream) 1180 { 1181 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1182 struct snd_pcm_runtime *runtime = substream->runtime; 1183 struct snd_ali_voice *pvoice = runtime->private_data; 1184 struct snd_ali_voice *evoice = pvoice->extra; 1185 1186 unsigned int LBA; 1187 unsigned int Delta; 1188 unsigned int ESO; 1189 unsigned int CTRL; 1190 unsigned int GVSEL; 1191 unsigned int PAN; 1192 unsigned int VOL; 1193 unsigned int EC; 1194 1195 dev_dbg(codec->card->dev, "playback_prepare ...\n"); 1196 1197 spin_lock_irq(&codec->reg_lock); 1198 1199 /* set Delta (rate) value */ 1200 Delta = snd_ali_convert_rate(runtime->rate, 0); 1201 1202 if (pvoice->number == ALI_SPDIF_IN_CHANNEL || 1203 pvoice->number == ALI_PCM_IN_CHANNEL) 1204 snd_ali_disable_special_channel(codec, pvoice->number); 1205 else if (codec->spdif_support && 1206 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & 1207 ALI_SPDIF_OUT_CH_ENABLE) 1208 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) { 1209 snd_ali_set_spdif_out_rate(codec, runtime->rate); 1210 Delta = 0x1000; 1211 } 1212 1213 /* set Loop Back Address */ 1214 LBA = runtime->dma_addr; 1215 1216 /* set interrupt count size */ 1217 pvoice->count = runtime->period_size; 1218 1219 /* set target ESO for channel */ 1220 pvoice->eso = runtime->buffer_size; 1221 1222 dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n", 1223 pvoice->eso, pvoice->count); 1224 1225 /* set ESO to capture first MIDLP interrupt */ 1226 ESO = pvoice->eso -1; 1227 /* set ctrl mode */ 1228 CTRL = snd_ali_control_mode(substream); 1229 1230 GVSEL = 1; 1231 PAN = 0; 1232 VOL = 0; 1233 EC = 0; 1234 dev_dbg(codec->card->dev, "playback_prepare:\n"); 1235 dev_dbg(codec->card->dev, 1236 "ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n", 1237 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL); 1238 snd_ali_write_voice_regs(codec, 1239 pvoice->number, 1240 LBA, 1241 0, /* cso */ 1242 ESO, 1243 Delta, 1244 0, /* alpha */ 1245 GVSEL, 1246 PAN, 1247 VOL, 1248 CTRL, 1249 EC); 1250 if (evoice) { 1251 evoice->count = pvoice->count; 1252 evoice->eso = pvoice->count << 1; 1253 ESO = evoice->eso - 1; 1254 snd_ali_write_voice_regs(codec, 1255 evoice->number, 1256 LBA, 1257 0, /* cso */ 1258 ESO, 1259 Delta, 1260 0, /* alpha */ 1261 GVSEL, 1262 0x7f, 1263 0x3ff, 1264 CTRL, 1265 EC); 1266 } 1267 spin_unlock_irq(&codec->reg_lock); 1268 return 0; 1269 } 1270 1271 1272 static int snd_ali_prepare(struct snd_pcm_substream *substream) 1273 { 1274 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1275 struct snd_pcm_runtime *runtime = substream->runtime; 1276 struct snd_ali_voice *pvoice = runtime->private_data; 1277 unsigned int LBA; 1278 unsigned int Delta; 1279 unsigned int ESO; 1280 unsigned int CTRL; 1281 unsigned int GVSEL; 1282 unsigned int PAN; 1283 unsigned int VOL; 1284 unsigned int EC; 1285 u8 bValue; 1286 1287 spin_lock_irq(&codec->reg_lock); 1288 1289 dev_dbg(codec->card->dev, "ali_prepare...\n"); 1290 1291 snd_ali_enable_special_channel(codec,pvoice->number); 1292 1293 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL || 1294 pvoice->number == ALI_MODEM_OUT_CHANNEL) ? 1295 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode); 1296 1297 /* Prepare capture intr channel */ 1298 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) { 1299 1300 unsigned int rate; 1301 1302 spin_unlock_irq(&codec->reg_lock); 1303 if (codec->revision != ALI_5451_V02) 1304 return -1; 1305 1306 rate = snd_ali_get_spdif_in_rate(codec); 1307 if (rate == 0) { 1308 dev_warn(codec->card->dev, 1309 "ali_capture_prepare: spdif rate detect err!\n"); 1310 rate = 48000; 1311 } 1312 spin_lock_irq(&codec->reg_lock); 1313 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL)); 1314 if (bValue & 0x10) { 1315 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL)); 1316 dev_warn(codec->card->dev, 1317 "clear SPDIF parity error flag.\n"); 1318 } 1319 1320 if (rate != 48000) 1321 Delta = ((rate << 12) / runtime->rate) & 0x00ffff; 1322 } 1323 1324 /* set target ESO for channel */ 1325 pvoice->eso = runtime->buffer_size; 1326 1327 /* set interrupt count size */ 1328 pvoice->count = runtime->period_size; 1329 1330 /* set Loop Back Address */ 1331 LBA = runtime->dma_addr; 1332 1333 /* set ESO to capture first MIDLP interrupt */ 1334 ESO = pvoice->eso - 1; 1335 CTRL = snd_ali_control_mode(substream); 1336 GVSEL = 0; 1337 PAN = 0x00; 1338 VOL = 0x00; 1339 EC = 0; 1340 1341 snd_ali_write_voice_regs( codec, 1342 pvoice->number, 1343 LBA, 1344 0, /* cso */ 1345 ESO, 1346 Delta, 1347 0, /* alpha */ 1348 GVSEL, 1349 PAN, 1350 VOL, 1351 CTRL, 1352 EC); 1353 1354 spin_unlock_irq(&codec->reg_lock); 1355 1356 return 0; 1357 } 1358 1359 1360 static snd_pcm_uframes_t 1361 snd_ali_playback_pointer(struct snd_pcm_substream *substream) 1362 { 1363 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1364 struct snd_pcm_runtime *runtime = substream->runtime; 1365 struct snd_ali_voice *pvoice = runtime->private_data; 1366 unsigned int cso; 1367 1368 spin_lock(&codec->reg_lock); 1369 if (!pvoice->running) { 1370 spin_unlock(&codec->reg_lock); 1371 return 0; 1372 } 1373 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR)); 1374 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)); 1375 spin_unlock(&codec->reg_lock); 1376 dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso); 1377 1378 cso %= runtime->buffer_size; 1379 return cso; 1380 } 1381 1382 1383 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream) 1384 { 1385 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1386 struct snd_pcm_runtime *runtime = substream->runtime; 1387 struct snd_ali_voice *pvoice = runtime->private_data; 1388 unsigned int cso; 1389 1390 spin_lock(&codec->reg_lock); 1391 if (!pvoice->running) { 1392 spin_unlock(&codec->reg_lock); 1393 return 0; 1394 } 1395 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR)); 1396 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)); 1397 spin_unlock(&codec->reg_lock); 1398 1399 cso %= runtime->buffer_size; 1400 return cso; 1401 } 1402 1403 static const struct snd_pcm_hardware snd_ali_playback = 1404 { 1405 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 1406 SNDRV_PCM_INFO_BLOCK_TRANSFER | 1407 SNDRV_PCM_INFO_MMAP_VALID | 1408 SNDRV_PCM_INFO_RESUME | 1409 SNDRV_PCM_INFO_SYNC_START), 1410 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | 1411 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE), 1412 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, 1413 .rate_min = 4000, 1414 .rate_max = 48000, 1415 .channels_min = 1, 1416 .channels_max = 2, 1417 .buffer_bytes_max = (256*1024), 1418 .period_bytes_min = 64, 1419 .period_bytes_max = (256*1024), 1420 .periods_min = 1, 1421 .periods_max = 1024, 1422 .fifo_size = 0, 1423 }; 1424 1425 /* 1426 * Capture support device description 1427 */ 1428 1429 static const struct snd_pcm_hardware snd_ali_capture = 1430 { 1431 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 1432 SNDRV_PCM_INFO_BLOCK_TRANSFER | 1433 SNDRV_PCM_INFO_MMAP_VALID | 1434 SNDRV_PCM_INFO_RESUME | 1435 SNDRV_PCM_INFO_SYNC_START), 1436 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | 1437 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE), 1438 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, 1439 .rate_min = 4000, 1440 .rate_max = 48000, 1441 .channels_min = 1, 1442 .channels_max = 2, 1443 .buffer_bytes_max = (128*1024), 1444 .period_bytes_min = 64, 1445 .period_bytes_max = (128*1024), 1446 .periods_min = 1, 1447 .periods_max = 1024, 1448 .fifo_size = 0, 1449 }; 1450 1451 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime) 1452 { 1453 struct snd_ali_voice *pvoice = runtime->private_data; 1454 1455 if (pvoice) 1456 snd_ali_free_voice(pvoice->codec, pvoice); 1457 } 1458 1459 static int snd_ali_open(struct snd_pcm_substream *substream, int rec, 1460 int channel, const struct snd_pcm_hardware *phw) 1461 { 1462 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1463 struct snd_pcm_runtime *runtime = substream->runtime; 1464 struct snd_ali_voice *pvoice; 1465 1466 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec, 1467 channel); 1468 if (!pvoice) 1469 return -EAGAIN; 1470 1471 pvoice->substream = substream; 1472 runtime->private_data = pvoice; 1473 runtime->private_free = snd_ali_pcm_free_substream; 1474 1475 runtime->hw = *phw; 1476 snd_pcm_set_sync(substream); 1477 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 1478 0, 64*1024); 1479 return 0; 1480 } 1481 1482 static int snd_ali_playback_open(struct snd_pcm_substream *substream) 1483 { 1484 return snd_ali_open(substream, 0, -1, &snd_ali_playback); 1485 } 1486 1487 static int snd_ali_capture_open(struct snd_pcm_substream *substream) 1488 { 1489 return snd_ali_open(substream, 1, -1, &snd_ali_capture); 1490 } 1491 1492 static int snd_ali_playback_close(struct snd_pcm_substream *substream) 1493 { 1494 return 0; 1495 } 1496 1497 static int snd_ali_close(struct snd_pcm_substream *substream) 1498 { 1499 struct snd_ali *codec = snd_pcm_substream_chip(substream); 1500 struct snd_ali_voice *pvoice = substream->runtime->private_data; 1501 1502 snd_ali_disable_special_channel(codec,pvoice->number); 1503 1504 return 0; 1505 } 1506 1507 static const struct snd_pcm_ops snd_ali_playback_ops = { 1508 .open = snd_ali_playback_open, 1509 .close = snd_ali_playback_close, 1510 .hw_params = snd_ali_playback_hw_params, 1511 .hw_free = snd_ali_playback_hw_free, 1512 .prepare = snd_ali_playback_prepare, 1513 .trigger = snd_ali_trigger, 1514 .pointer = snd_ali_playback_pointer, 1515 }; 1516 1517 static const struct snd_pcm_ops snd_ali_capture_ops = { 1518 .open = snd_ali_capture_open, 1519 .close = snd_ali_close, 1520 .prepare = snd_ali_prepare, 1521 .trigger = snd_ali_trigger, 1522 .pointer = snd_ali_pointer, 1523 }; 1524 1525 /* 1526 * Modem PCM 1527 */ 1528 1529 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream, 1530 struct snd_pcm_hw_params *hw_params) 1531 { 1532 struct snd_ali *chip = snd_pcm_substream_chip(substream); 1533 unsigned int modem_num = chip->num_of_codecs - 1; 1534 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE, 1535 params_rate(hw_params)); 1536 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0); 1537 return 0; 1538 } 1539 1540 static const struct snd_pcm_hardware snd_ali_modem = 1541 { 1542 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 1543 SNDRV_PCM_INFO_BLOCK_TRANSFER | 1544 SNDRV_PCM_INFO_MMAP_VALID | 1545 SNDRV_PCM_INFO_RESUME | 1546 SNDRV_PCM_INFO_SYNC_START), 1547 .formats = SNDRV_PCM_FMTBIT_S16_LE, 1548 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 | 1549 SNDRV_PCM_RATE_16000), 1550 .rate_min = 8000, 1551 .rate_max = 16000, 1552 .channels_min = 1, 1553 .channels_max = 1, 1554 .buffer_bytes_max = (256*1024), 1555 .period_bytes_min = 64, 1556 .period_bytes_max = (256*1024), 1557 .periods_min = 1, 1558 .periods_max = 1024, 1559 .fifo_size = 0, 1560 }; 1561 1562 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec, 1563 int channel) 1564 { 1565 static const unsigned int rates[] = {8000, 9600, 12000, 16000}; 1566 static const struct snd_pcm_hw_constraint_list hw_constraint_rates = { 1567 .count = ARRAY_SIZE(rates), 1568 .list = rates, 1569 .mask = 0, 1570 }; 1571 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem); 1572 1573 if (err) 1574 return err; 1575 return snd_pcm_hw_constraint_list(substream->runtime, 0, 1576 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates); 1577 } 1578 1579 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream) 1580 { 1581 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL); 1582 } 1583 1584 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream) 1585 { 1586 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL); 1587 } 1588 1589 static const struct snd_pcm_ops snd_ali_modem_playback_ops = { 1590 .open = snd_ali_modem_playback_open, 1591 .close = snd_ali_close, 1592 .hw_params = snd_ali_modem_hw_params, 1593 .prepare = snd_ali_prepare, 1594 .trigger = snd_ali_trigger, 1595 .pointer = snd_ali_pointer, 1596 }; 1597 1598 static const struct snd_pcm_ops snd_ali_modem_capture_ops = { 1599 .open = snd_ali_modem_capture_open, 1600 .close = snd_ali_close, 1601 .hw_params = snd_ali_modem_hw_params, 1602 .prepare = snd_ali_prepare, 1603 .trigger = snd_ali_trigger, 1604 .pointer = snd_ali_pointer, 1605 }; 1606 1607 1608 struct ali_pcm_description { 1609 char *name; 1610 unsigned int playback_num; 1611 unsigned int capture_num; 1612 const struct snd_pcm_ops *playback_ops; 1613 const struct snd_pcm_ops *capture_ops; 1614 unsigned short class; 1615 }; 1616 1617 1618 static void snd_ali_pcm_free(struct snd_pcm *pcm) 1619 { 1620 struct snd_ali *codec = pcm->private_data; 1621 codec->pcm[pcm->device] = NULL; 1622 } 1623 1624 1625 static int snd_ali_pcm(struct snd_ali *codec, int device, 1626 struct ali_pcm_description *desc) 1627 { 1628 struct snd_pcm *pcm; 1629 int err; 1630 1631 err = snd_pcm_new(codec->card, desc->name, device, 1632 desc->playback_num, desc->capture_num, &pcm); 1633 if (err < 0) { 1634 dev_err(codec->card->dev, 1635 "snd_ali_pcm: err called snd_pcm_new.\n"); 1636 return err; 1637 } 1638 pcm->private_data = codec; 1639 pcm->private_free = snd_ali_pcm_free; 1640 if (desc->playback_ops) 1641 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 1642 desc->playback_ops); 1643 if (desc->capture_ops) 1644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, 1645 desc->capture_ops); 1646 1647 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, 1648 &codec->pci->dev, 64*1024, 128*1024); 1649 1650 pcm->info_flags = 0; 1651 pcm->dev_class = desc->class; 1652 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX; 1653 strcpy(pcm->name, desc->name); 1654 codec->pcm[0] = pcm; 1655 return 0; 1656 } 1657 1658 static struct ali_pcm_description ali_pcms[] = { 1659 { .name = "ALI 5451", 1660 .playback_num = ALI_CHANNELS, 1661 .capture_num = 1, 1662 .playback_ops = &snd_ali_playback_ops, 1663 .capture_ops = &snd_ali_capture_ops 1664 }, 1665 { .name = "ALI 5451 modem", 1666 .playback_num = 1, 1667 .capture_num = 1, 1668 .playback_ops = &snd_ali_modem_playback_ops, 1669 .capture_ops = &snd_ali_modem_capture_ops, 1670 .class = SNDRV_PCM_CLASS_MODEM 1671 } 1672 }; 1673 1674 static int snd_ali_build_pcms(struct snd_ali *codec) 1675 { 1676 int i, err; 1677 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) { 1678 err = snd_ali_pcm(codec, i, &ali_pcms[i]); 1679 if (err < 0) 1680 return err; 1681 } 1682 return 0; 1683 } 1684 1685 1686 #define ALI5451_SPDIF(xname, xindex, value) \ 1687 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\ 1688 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \ 1689 .put = snd_ali5451_spdif_put, .private_value = value} 1690 1691 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info 1692 1693 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol, 1694 struct snd_ctl_elem_value *ucontrol) 1695 { 1696 struct snd_ali *codec = kcontrol->private_data; 1697 unsigned int spdif_enable; 1698 1699 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0; 1700 1701 spin_lock_irq(&codec->reg_lock); 1702 switch (kcontrol->private_value) { 1703 case 0: 1704 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0; 1705 break; 1706 case 1: 1707 spdif_enable = ((codec->spdif_mask & 0x02) && 1708 (codec->spdif_mask & 0x04)) ? 1 : 0; 1709 break; 1710 case 2: 1711 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0; 1712 break; 1713 default: 1714 break; 1715 } 1716 ucontrol->value.integer.value[0] = spdif_enable; 1717 spin_unlock_irq(&codec->reg_lock); 1718 return 0; 1719 } 1720 1721 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol, 1722 struct snd_ctl_elem_value *ucontrol) 1723 { 1724 struct snd_ali *codec = kcontrol->private_data; 1725 unsigned int change = 0, spdif_enable = 0; 1726 1727 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0; 1728 1729 spin_lock_irq(&codec->reg_lock); 1730 switch (kcontrol->private_value) { 1731 case 0: 1732 change = (codec->spdif_mask & 0x02) ? 1 : 0; 1733 change = change ^ spdif_enable; 1734 if (change) { 1735 if (spdif_enable) { 1736 codec->spdif_mask |= 0x02; 1737 snd_ali_enable_spdif_out(codec); 1738 } else { 1739 codec->spdif_mask &= ~(0x02); 1740 codec->spdif_mask &= ~(0x04); 1741 snd_ali_disable_spdif_out(codec); 1742 } 1743 } 1744 break; 1745 case 1: 1746 change = (codec->spdif_mask & 0x04) ? 1 : 0; 1747 change = change ^ spdif_enable; 1748 if (change && (codec->spdif_mask & 0x02)) { 1749 if (spdif_enable) { 1750 codec->spdif_mask |= 0x04; 1751 snd_ali_enable_spdif_chnout(codec); 1752 } else { 1753 codec->spdif_mask &= ~(0x04); 1754 snd_ali_disable_spdif_chnout(codec); 1755 } 1756 } 1757 break; 1758 case 2: 1759 change = (codec->spdif_mask & 0x01) ? 1 : 0; 1760 change = change ^ spdif_enable; 1761 if (change) { 1762 if (spdif_enable) { 1763 codec->spdif_mask |= 0x01; 1764 snd_ali_enable_spdif_in(codec); 1765 } else { 1766 codec->spdif_mask &= ~(0x01); 1767 snd_ali_disable_spdif_in(codec); 1768 } 1769 } 1770 break; 1771 default: 1772 break; 1773 } 1774 spin_unlock_irq(&codec->reg_lock); 1775 1776 return change; 1777 } 1778 1779 static const struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = { 1780 /* spdif aplayback switch */ 1781 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */ 1782 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0), 1783 /* spdif out to spdif channel */ 1784 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1), 1785 /* spdif in from spdif channel */ 1786 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2) 1787 }; 1788 1789 static int snd_ali_mixer(struct snd_ali *codec) 1790 { 1791 struct snd_ac97_template ac97; 1792 unsigned int idx; 1793 int i, err; 1794 static const struct snd_ac97_bus_ops ops = { 1795 .write = snd_ali_codec_write, 1796 .read = snd_ali_codec_read, 1797 }; 1798 1799 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus); 1800 if (err < 0) 1801 return err; 1802 1803 memset(&ac97, 0, sizeof(ac97)); 1804 ac97.private_data = codec; 1805 1806 for (i = 0; i < codec->num_of_codecs; i++) { 1807 ac97.num = i; 1808 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]); 1809 if (err < 0) { 1810 dev_err(codec->card->dev, 1811 "ali mixer %d creating error.\n", i); 1812 if (i == 0) 1813 return err; 1814 codec->num_of_codecs = 1; 1815 break; 1816 } 1817 } 1818 1819 if (codec->spdif_support) { 1820 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) { 1821 err = snd_ctl_add(codec->card, 1822 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec)); 1823 if (err < 0) 1824 return err; 1825 } 1826 } 1827 return 0; 1828 } 1829 1830 #ifdef CONFIG_PM_SLEEP 1831 static int ali_suspend(struct device *dev) 1832 { 1833 struct snd_card *card = dev_get_drvdata(dev); 1834 struct snd_ali *chip = card->private_data; 1835 struct snd_ali_image *im; 1836 int i, j; 1837 1838 im = chip->image; 1839 if (!im) 1840 return 0; 1841 1842 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1843 for (i = 0; i < chip->num_of_codecs; i++) 1844 snd_ac97_suspend(chip->ac97[i]); 1845 1846 spin_lock_irq(&chip->reg_lock); 1847 1848 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT)); 1849 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */ 1850 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP)); 1851 1852 /* disable all IRQ bits */ 1853 outl(0, ALI_REG(chip, ALI_MISCINT)); 1854 1855 for (i = 0; i < ALI_GLOBAL_REGS; i++) { 1856 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP)) 1857 continue; 1858 im->regs[i] = inl(ALI_REG(chip, i*4)); 1859 } 1860 1861 for (i = 0; i < ALI_CHANNELS; i++) { 1862 outb(i, ALI_REG(chip, ALI_GC_CIR)); 1863 for (j = 0; j < ALI_CHANNEL_REGS; j++) 1864 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0)); 1865 } 1866 1867 /* stop all HW channel */ 1868 outl(0xffffffff, ALI_REG(chip, ALI_STOP)); 1869 1870 spin_unlock_irq(&chip->reg_lock); 1871 return 0; 1872 } 1873 1874 static int ali_resume(struct device *dev) 1875 { 1876 struct snd_card *card = dev_get_drvdata(dev); 1877 struct snd_ali *chip = card->private_data; 1878 struct snd_ali_image *im; 1879 int i, j; 1880 1881 im = chip->image; 1882 if (!im) 1883 return 0; 1884 1885 spin_lock_irq(&chip->reg_lock); 1886 1887 for (i = 0; i < ALI_CHANNELS; i++) { 1888 outb(i, ALI_REG(chip, ALI_GC_CIR)); 1889 for (j = 0; j < ALI_CHANNEL_REGS; j++) 1890 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0)); 1891 } 1892 1893 for (i = 0; i < ALI_GLOBAL_REGS; i++) { 1894 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || 1895 (i*4 == ALI_START)) 1896 continue; 1897 outl(im->regs[i], ALI_REG(chip, i*4)); 1898 } 1899 1900 /* start HW channel */ 1901 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START)); 1902 /* restore IRQ enable bits */ 1903 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT)); 1904 1905 spin_unlock_irq(&chip->reg_lock); 1906 1907 for (i = 0 ; i < chip->num_of_codecs; i++) 1908 snd_ac97_resume(chip->ac97[i]); 1909 1910 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1911 return 0; 1912 } 1913 1914 static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume); 1915 #define ALI_PM_OPS &ali_pm 1916 #else 1917 #define ALI_PM_OPS NULL 1918 #endif /* CONFIG_PM_SLEEP */ 1919 1920 static int snd_ali_free(struct snd_ali * codec) 1921 { 1922 if (codec->hw_initialized) 1923 snd_ali_disable_address_interrupt(codec); 1924 if (codec->irq >= 0) 1925 free_irq(codec->irq, codec); 1926 if (codec->port) 1927 pci_release_regions(codec->pci); 1928 pci_disable_device(codec->pci); 1929 #ifdef CONFIG_PM_SLEEP 1930 kfree(codec->image); 1931 #endif 1932 pci_dev_put(codec->pci_m1533); 1933 pci_dev_put(codec->pci_m7101); 1934 kfree(codec); 1935 return 0; 1936 } 1937 1938 static int snd_ali_chip_init(struct snd_ali *codec) 1939 { 1940 unsigned int legacy; 1941 unsigned char temp; 1942 struct pci_dev *pci_dev; 1943 1944 dev_dbg(codec->card->dev, "chip initializing ...\n"); 1945 1946 if (snd_ali_reset_5451(codec)) { 1947 dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n"); 1948 return -1; 1949 } 1950 1951 if (codec->revision == ALI_5451_V02) { 1952 pci_dev = codec->pci_m1533; 1953 pci_read_config_byte(pci_dev, 0x59, &temp); 1954 temp |= 0x80; 1955 pci_write_config_byte(pci_dev, 0x59, temp); 1956 1957 pci_dev = codec->pci_m7101; 1958 pci_read_config_byte(pci_dev, 0xb8, &temp); 1959 temp |= 0x20; 1960 pci_write_config_byte(pci_dev, 0xB8, temp); 1961 } 1962 1963 pci_read_config_dword(codec->pci, 0x44, &legacy); 1964 legacy &= 0xff00ff00; 1965 legacy |= 0x000800aa; 1966 pci_write_config_dword(codec->pci, 0x44, legacy); 1967 1968 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL)); 1969 outl(0x00000000, ALI_REG(codec, ALI_AINTEN)); 1970 outl(0xffffffff, ALI_REG(codec, ALI_AINT)); 1971 outl(0x00000000, ALI_REG(codec, ALI_VOLUME)); 1972 outb(0x10, ALI_REG(codec, ALI_MPUR2)); 1973 1974 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID); 1975 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, 1976 AC97_EXTENDED_STATUS); 1977 if (codec->spdif_support) { 1978 snd_ali_enable_spdif_out(codec); 1979 codec->spdif_mask = 0x00000002; 1980 } 1981 1982 codec->num_of_codecs = 1; 1983 1984 /* secondary codec - modem */ 1985 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) { 1986 codec->num_of_codecs++; 1987 outl(inl(ALI_REG(codec, ALI_SCTRL)) | 1988 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 | 1989 ALI_SCTRL_LINE_OUT_EN), 1990 ALI_REG(codec, ALI_SCTRL)); 1991 } 1992 1993 dev_dbg(codec->card->dev, "chip initialize succeed.\n"); 1994 return 0; 1995 1996 } 1997 1998 /* proc for register dump */ 1999 static void snd_ali_proc_read(struct snd_info_entry *entry, 2000 struct snd_info_buffer *buf) 2001 { 2002 struct snd_ali *codec = entry->private_data; 2003 int i; 2004 for (i = 0; i < 256 ; i+= 4) 2005 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i))); 2006 } 2007 2008 static void snd_ali_proc_init(struct snd_ali *codec) 2009 { 2010 snd_card_ro_proc_new(codec->card, "ali5451", codec, snd_ali_proc_read); 2011 } 2012 2013 static int snd_ali_resources(struct snd_ali *codec) 2014 { 2015 int err; 2016 2017 dev_dbg(codec->card->dev, "resources allocation ...\n"); 2018 err = pci_request_regions(codec->pci, "ALI 5451"); 2019 if (err < 0) 2020 return err; 2021 codec->port = pci_resource_start(codec->pci, 0); 2022 2023 if (request_irq(codec->pci->irq, snd_ali_card_interrupt, 2024 IRQF_SHARED, KBUILD_MODNAME, codec)) { 2025 dev_err(codec->card->dev, "Unable to request irq.\n"); 2026 return -EBUSY; 2027 } 2028 codec->irq = codec->pci->irq; 2029 codec->card->sync_irq = codec->irq; 2030 dev_dbg(codec->card->dev, "resources allocated.\n"); 2031 return 0; 2032 } 2033 static int snd_ali_dev_free(struct snd_device *device) 2034 { 2035 struct snd_ali *codec = device->device_data; 2036 snd_ali_free(codec); 2037 return 0; 2038 } 2039 2040 static int snd_ali_create(struct snd_card *card, 2041 struct pci_dev *pci, 2042 int pcm_streams, 2043 int spdif_support, 2044 struct snd_ali **r_ali) 2045 { 2046 struct snd_ali *codec; 2047 int i, err; 2048 unsigned short cmdw; 2049 static const struct snd_device_ops ops = { 2050 .dev_free = snd_ali_dev_free, 2051 }; 2052 2053 *r_ali = NULL; 2054 2055 dev_dbg(card->dev, "creating ...\n"); 2056 2057 /* enable PCI device */ 2058 err = pci_enable_device(pci); 2059 if (err < 0) 2060 return err; 2061 /* check, if we can restrict PCI DMA transfers to 31 bits */ 2062 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(31)) < 0 || 2063 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(31)) < 0) { 2064 dev_err(card->dev, 2065 "architecture does not support 31bit PCI busmaster DMA\n"); 2066 pci_disable_device(pci); 2067 return -ENXIO; 2068 } 2069 2070 codec = kzalloc(sizeof(*codec), GFP_KERNEL); 2071 if (!codec) { 2072 pci_disable_device(pci); 2073 return -ENOMEM; 2074 } 2075 2076 spin_lock_init(&codec->reg_lock); 2077 spin_lock_init(&codec->voice_alloc); 2078 2079 codec->card = card; 2080 codec->pci = pci; 2081 codec->irq = -1; 2082 codec->revision = pci->revision; 2083 codec->spdif_support = spdif_support; 2084 2085 if (pcm_streams < 1) 2086 pcm_streams = 1; 2087 if (pcm_streams > 32) 2088 pcm_streams = 32; 2089 2090 pci_set_master(pci); 2091 pci_read_config_word(pci, PCI_COMMAND, &cmdw); 2092 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) { 2093 cmdw |= PCI_COMMAND_IO; 2094 pci_write_config_word(pci, PCI_COMMAND, cmdw); 2095 } 2096 pci_set_master(pci); 2097 2098 if (snd_ali_resources(codec)) { 2099 snd_ali_free(codec); 2100 return -EBUSY; 2101 } 2102 2103 codec->synth.chmap = 0; 2104 codec->synth.chcnt = 0; 2105 codec->spdif_mask = 0; 2106 codec->synth.synthcount = 0; 2107 2108 if (codec->revision == ALI_5451_V02) 2109 codec->chregs.regs.ac97read = ALI_AC97_WRITE; 2110 else 2111 codec->chregs.regs.ac97read = ALI_AC97_READ; 2112 codec->chregs.regs.ac97write = ALI_AC97_WRITE; 2113 2114 codec->chregs.regs.start = ALI_START; 2115 codec->chregs.regs.stop = ALI_STOP; 2116 codec->chregs.regs.aint = ALI_AINT; 2117 codec->chregs.regs.ainten = ALI_AINTEN; 2118 2119 codec->chregs.data.start = 0x00; 2120 codec->chregs.data.stop = 0x00; 2121 codec->chregs.data.aint = 0x00; 2122 codec->chregs.data.ainten = 0x00; 2123 2124 /* M1533: southbridge */ 2125 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL); 2126 if (!codec->pci_m1533) { 2127 dev_err(card->dev, "cannot find ALi 1533 chip.\n"); 2128 snd_ali_free(codec); 2129 return -ENODEV; 2130 } 2131 /* M7101: power management */ 2132 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL); 2133 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) { 2134 dev_err(card->dev, "cannot find ALi 7101 chip.\n"); 2135 snd_ali_free(codec); 2136 return -ENODEV; 2137 } 2138 2139 dev_dbg(card->dev, "snd_device_new is called.\n"); 2140 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops); 2141 if (err < 0) { 2142 snd_ali_free(codec); 2143 return err; 2144 } 2145 2146 /* initialise synth voices*/ 2147 for (i = 0; i < ALI_CHANNELS; i++) 2148 codec->synth.voices[i].number = i; 2149 2150 err = snd_ali_chip_init(codec); 2151 if (err < 0) { 2152 dev_err(card->dev, "ali create: chip init error.\n"); 2153 return err; 2154 } 2155 2156 #ifdef CONFIG_PM_SLEEP 2157 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL); 2158 if (!codec->image) 2159 dev_warn(card->dev, "can't allocate apm buffer\n"); 2160 #endif 2161 2162 snd_ali_enable_address_interrupt(codec); 2163 codec->hw_initialized = 1; 2164 2165 *r_ali = codec; 2166 dev_dbg(card->dev, "created.\n"); 2167 return 0; 2168 } 2169 2170 static int snd_ali_probe(struct pci_dev *pci, 2171 const struct pci_device_id *pci_id) 2172 { 2173 struct snd_card *card; 2174 struct snd_ali *codec; 2175 int err; 2176 2177 dev_dbg(&pci->dev, "probe ...\n"); 2178 2179 err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card); 2180 if (err < 0) 2181 return err; 2182 2183 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec); 2184 if (err < 0) 2185 goto error; 2186 card->private_data = codec; 2187 2188 dev_dbg(&pci->dev, "mixer building ...\n"); 2189 err = snd_ali_mixer(codec); 2190 if (err < 0) 2191 goto error; 2192 2193 dev_dbg(&pci->dev, "pcm building ...\n"); 2194 err = snd_ali_build_pcms(codec); 2195 if (err < 0) 2196 goto error; 2197 2198 snd_ali_proc_init(codec); 2199 2200 strcpy(card->driver, "ALI5451"); 2201 strcpy(card->shortname, "ALI 5451"); 2202 2203 sprintf(card->longname, "%s at 0x%lx, irq %i", 2204 card->shortname, codec->port, codec->irq); 2205 2206 dev_dbg(&pci->dev, "register card.\n"); 2207 err = snd_card_register(card); 2208 if (err < 0) 2209 goto error; 2210 2211 pci_set_drvdata(pci, card); 2212 return 0; 2213 2214 error: 2215 snd_card_free(card); 2216 return err; 2217 } 2218 2219 static void snd_ali_remove(struct pci_dev *pci) 2220 { 2221 snd_card_free(pci_get_drvdata(pci)); 2222 } 2223 2224 static struct pci_driver ali5451_driver = { 2225 .name = KBUILD_MODNAME, 2226 .id_table = snd_ali_ids, 2227 .probe = snd_ali_probe, 2228 .remove = snd_ali_remove, 2229 .driver = { 2230 .pm = ALI_PM_OPS, 2231 }, 2232 }; 2233 2234 module_pci_driver(ali5451_driver); 2235