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