1 /* 2 * Driver for SiS7019 Audio Accelerator 3 * 4 * Copyright (C) 2004-2007, David Dillow 5 * Written by David Dillow <dave@thedillows.org> 6 * Inspired by the Trident 4D-WaveDX/NX driver. 7 * 8 * All rights reserved. 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation, version 2. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 #include <linux/init.h> 25 #include <linux/pci.h> 26 #include <linux/time.h> 27 #include <linux/moduleparam.h> 28 #include <linux/interrupt.h> 29 #include <linux/delay.h> 30 #include <sound/core.h> 31 #include <sound/ac97_codec.h> 32 #include <sound/initval.h> 33 #include "sis7019.h" 34 35 MODULE_AUTHOR("David Dillow <dave@thedillows.org>"); 36 MODULE_DESCRIPTION("SiS7019"); 37 MODULE_LICENSE("GPL"); 38 MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}"); 39 40 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ 41 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ 42 static int enable = 1; 43 static int codecs = 1; 44 45 module_param(index, int, 0444); 46 MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator."); 47 module_param(id, charp, 0444); 48 MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator."); 49 module_param(enable, bool, 0444); 50 MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator."); 51 module_param(codecs, int, 0444); 52 MODULE_PARM_DESC(codecs, "Set bit to indicate that codec number is expected to be present (default 1)"); 53 54 static struct pci_device_id snd_sis7019_ids[] = { 55 { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) }, 56 { 0, } 57 }; 58 59 MODULE_DEVICE_TABLE(pci, snd_sis7019_ids); 60 61 /* There are three timing modes for the voices. 62 * 63 * For both playback and capture, when the buffer is one or two periods long, 64 * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt 65 * to let us know when the periods have ended. 66 * 67 * When performing playback with more than two periods per buffer, we set 68 * the "Stop Sample Offset" and tell the hardware to interrupt us when we 69 * reach it. We then update the offset and continue on until we are 70 * interrupted for the next period. 71 * 72 * Capture channels do not have a SSO, so we allocate a playback channel to 73 * use as a timer for the capture periods. We use the SSO on the playback 74 * channel to clock out virtual periods, and adjust the virtual period length 75 * to maintain synchronization. This algorithm came from the Trident driver. 76 * 77 * FIXME: It'd be nice to make use of some of the synth features in the 78 * hardware, but a woeful lack of documentation is a significant roadblock. 79 */ 80 struct voice { 81 u16 flags; 82 #define VOICE_IN_USE 1 83 #define VOICE_CAPTURE 2 84 #define VOICE_SSO_TIMING 4 85 #define VOICE_SYNC_TIMING 8 86 u16 sync_cso; 87 u16 period_size; 88 u16 buffer_size; 89 u16 sync_period_size; 90 u16 sync_buffer_size; 91 u32 sso; 92 u32 vperiod; 93 struct snd_pcm_substream *substream; 94 struct voice *timing; 95 void __iomem *ctrl_base; 96 void __iomem *wave_base; 97 void __iomem *sync_base; 98 int num; 99 }; 100 101 /* We need four pages to store our wave parameters during a suspend. If 102 * we're not doing power management, we still need to allocate a page 103 * for the silence buffer. 104 */ 105 #ifdef CONFIG_PM 106 #define SIS_SUSPEND_PAGES 4 107 #else 108 #define SIS_SUSPEND_PAGES 1 109 #endif 110 111 struct sis7019 { 112 unsigned long ioport; 113 void __iomem *ioaddr; 114 int irq; 115 int codecs_present; 116 117 struct pci_dev *pci; 118 struct snd_pcm *pcm; 119 struct snd_card *card; 120 struct snd_ac97 *ac97[3]; 121 122 /* Protect against more than one thread hitting the AC97 123 * registers (in a more polite manner than pounding the hardware 124 * semaphore) 125 */ 126 struct mutex ac97_mutex; 127 128 /* voice_lock protects allocation/freeing of the voice descriptions 129 */ 130 spinlock_t voice_lock; 131 132 struct voice voices[64]; 133 struct voice capture_voice; 134 135 /* Allocate pages to store the internal wave state during 136 * suspends. When we're operating, this can be used as a silence 137 * buffer for a timing channel. 138 */ 139 void *suspend_state[SIS_SUSPEND_PAGES]; 140 141 int silence_users; 142 dma_addr_t silence_dma_addr; 143 }; 144 145 /* These values are also used by the module param 'codecs' to indicate 146 * which codecs should be present. 147 */ 148 #define SIS_PRIMARY_CODEC_PRESENT 0x0001 149 #define SIS_SECONDARY_CODEC_PRESENT 0x0002 150 #define SIS_TERTIARY_CODEC_PRESENT 0x0004 151 152 /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a 153 * documented range of 8-0xfff8 samples. Given that they are 0-based, 154 * that places our period/buffer range at 9-0xfff9 samples. That makes the 155 * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and 156 * max samples / min samples gives us the max periods in a buffer. 157 * 158 * We'll add a constraint upon open that limits the period and buffer sample 159 * size to values that are legal for the hardware. 160 */ 161 static struct snd_pcm_hardware sis_playback_hw_info = { 162 .info = (SNDRV_PCM_INFO_MMAP | 163 SNDRV_PCM_INFO_MMAP_VALID | 164 SNDRV_PCM_INFO_INTERLEAVED | 165 SNDRV_PCM_INFO_BLOCK_TRANSFER | 166 SNDRV_PCM_INFO_SYNC_START | 167 SNDRV_PCM_INFO_RESUME), 168 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | 169 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE), 170 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, 171 .rate_min = 4000, 172 .rate_max = 48000, 173 .channels_min = 1, 174 .channels_max = 2, 175 .buffer_bytes_max = (0xfff9 * 4), 176 .period_bytes_min = 9, 177 .period_bytes_max = (0xfff9 * 4), 178 .periods_min = 1, 179 .periods_max = (0xfff9 / 9), 180 }; 181 182 static struct snd_pcm_hardware sis_capture_hw_info = { 183 .info = (SNDRV_PCM_INFO_MMAP | 184 SNDRV_PCM_INFO_MMAP_VALID | 185 SNDRV_PCM_INFO_INTERLEAVED | 186 SNDRV_PCM_INFO_BLOCK_TRANSFER | 187 SNDRV_PCM_INFO_SYNC_START | 188 SNDRV_PCM_INFO_RESUME), 189 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | 190 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE), 191 .rates = SNDRV_PCM_RATE_48000, 192 .rate_min = 4000, 193 .rate_max = 48000, 194 .channels_min = 1, 195 .channels_max = 2, 196 .buffer_bytes_max = (0xfff9 * 4), 197 .period_bytes_min = 9, 198 .period_bytes_max = (0xfff9 * 4), 199 .periods_min = 1, 200 .periods_max = (0xfff9 / 9), 201 }; 202 203 static void sis_update_sso(struct voice *voice, u16 period) 204 { 205 void __iomem *base = voice->ctrl_base; 206 207 voice->sso += period; 208 if (voice->sso >= voice->buffer_size) 209 voice->sso -= voice->buffer_size; 210 211 /* Enforce the documented hardware minimum offset */ 212 if (voice->sso < 8) 213 voice->sso = 8; 214 215 /* The SSO is in the upper 16 bits of the register. */ 216 writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2); 217 } 218 219 static void sis_update_voice(struct voice *voice) 220 { 221 if (voice->flags & VOICE_SSO_TIMING) { 222 sis_update_sso(voice, voice->period_size); 223 } else if (voice->flags & VOICE_SYNC_TIMING) { 224 int sync; 225 226 /* If we've not hit the end of the virtual period, update 227 * our records and keep going. 228 */ 229 if (voice->vperiod > voice->period_size) { 230 voice->vperiod -= voice->period_size; 231 if (voice->vperiod < voice->period_size) 232 sis_update_sso(voice, voice->vperiod); 233 else 234 sis_update_sso(voice, voice->period_size); 235 return; 236 } 237 238 /* Calculate our relative offset between the target and 239 * the actual CSO value. Since we're operating in a loop, 240 * if the value is more than half way around, we can 241 * consider ourselves wrapped. 242 */ 243 sync = voice->sync_cso; 244 sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO); 245 if (sync > (voice->sync_buffer_size / 2)) 246 sync -= voice->sync_buffer_size; 247 248 /* If sync is positive, then we interrupted too early, and 249 * we'll need to come back in a few samples and try again. 250 * There's a minimum wait, as it takes some time for the DMA 251 * engine to startup, etc... 252 */ 253 if (sync > 0) { 254 if (sync < 16) 255 sync = 16; 256 sis_update_sso(voice, sync); 257 return; 258 } 259 260 /* Ok, we interrupted right on time, or (hopefully) just 261 * a bit late. We'll adjst our next waiting period based 262 * on how close we got. 263 * 264 * We need to stay just behind the actual channel to ensure 265 * it really is past a period when we get our interrupt -- 266 * otherwise we'll fall into the early code above and have 267 * a minimum wait time, which makes us quite late here, 268 * eating into the user's time to refresh the buffer, esp. 269 * if using small periods. 270 * 271 * If we're less than 9 samples behind, we're on target. 272 */ 273 if (sync > -9) 274 voice->vperiod = voice->sync_period_size + 1; 275 else 276 voice->vperiod = voice->sync_period_size - 4; 277 278 if (voice->vperiod < voice->buffer_size) { 279 sis_update_sso(voice, voice->vperiod); 280 voice->vperiod = 0; 281 } else 282 sis_update_sso(voice, voice->period_size); 283 284 sync = voice->sync_cso + voice->sync_period_size; 285 if (sync >= voice->sync_buffer_size) 286 sync -= voice->sync_buffer_size; 287 voice->sync_cso = sync; 288 } 289 290 snd_pcm_period_elapsed(voice->substream); 291 } 292 293 static void sis_voice_irq(u32 status, struct voice *voice) 294 { 295 int bit; 296 297 while (status) { 298 bit = __ffs(status); 299 status >>= bit + 1; 300 voice += bit; 301 sis_update_voice(voice); 302 voice++; 303 } 304 } 305 306 static irqreturn_t sis_interrupt(int irq, void *dev) 307 { 308 struct sis7019 *sis = dev; 309 unsigned long io = sis->ioport; 310 struct voice *voice; 311 u32 intr, status; 312 313 /* We only use the DMA interrupts, and we don't enable any other 314 * source of interrupts. But, it is possible to see an interupt 315 * status that didn't actually interrupt us, so eliminate anything 316 * we're not expecting to avoid falsely claiming an IRQ, and an 317 * ensuing endless loop. 318 */ 319 intr = inl(io + SIS_GISR); 320 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS | 321 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS; 322 if (!intr) 323 return IRQ_NONE; 324 325 do { 326 status = inl(io + SIS_PISR_A); 327 if (status) { 328 sis_voice_irq(status, sis->voices); 329 outl(status, io + SIS_PISR_A); 330 } 331 332 status = inl(io + SIS_PISR_B); 333 if (status) { 334 sis_voice_irq(status, &sis->voices[32]); 335 outl(status, io + SIS_PISR_B); 336 } 337 338 status = inl(io + SIS_RISR); 339 if (status) { 340 voice = &sis->capture_voice; 341 if (!voice->timing) 342 snd_pcm_period_elapsed(voice->substream); 343 344 outl(status, io + SIS_RISR); 345 } 346 347 outl(intr, io + SIS_GISR); 348 intr = inl(io + SIS_GISR); 349 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS | 350 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS; 351 } while (intr); 352 353 return IRQ_HANDLED; 354 } 355 356 static u32 sis_rate_to_delta(unsigned int rate) 357 { 358 u32 delta; 359 360 /* This was copied from the trident driver, but it seems its gotten 361 * around a bit... nevertheless, it works well. 362 * 363 * We special case 44100 and 8000 since rounding with the equation 364 * does not give us an accurate enough value. For 11025 and 22050 365 * the equation gives us the best answer. All other frequencies will 366 * also use the equation. JDW 367 */ 368 if (rate == 44100) 369 delta = 0xeb3; 370 else if (rate == 8000) 371 delta = 0x2ab; 372 else if (rate == 48000) 373 delta = 0x1000; 374 else 375 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff; 376 return delta; 377 } 378 379 static void __sis_map_silence(struct sis7019 *sis) 380 { 381 /* Helper function: must hold sis->voice_lock on entry */ 382 if (!sis->silence_users) 383 sis->silence_dma_addr = pci_map_single(sis->pci, 384 sis->suspend_state[0], 385 4096, PCI_DMA_TODEVICE); 386 sis->silence_users++; 387 } 388 389 static void __sis_unmap_silence(struct sis7019 *sis) 390 { 391 /* Helper function: must hold sis->voice_lock on entry */ 392 sis->silence_users--; 393 if (!sis->silence_users) 394 pci_unmap_single(sis->pci, sis->silence_dma_addr, 4096, 395 PCI_DMA_TODEVICE); 396 } 397 398 static void sis_free_voice(struct sis7019 *sis, struct voice *voice) 399 { 400 unsigned long flags; 401 402 spin_lock_irqsave(&sis->voice_lock, flags); 403 if (voice->timing) { 404 __sis_unmap_silence(sis); 405 voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | 406 VOICE_SYNC_TIMING); 407 voice->timing = NULL; 408 } 409 voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING); 410 spin_unlock_irqrestore(&sis->voice_lock, flags); 411 } 412 413 static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis) 414 { 415 /* Must hold the voice_lock on entry */ 416 struct voice *voice; 417 int i; 418 419 for (i = 0; i < 64; i++) { 420 voice = &sis->voices[i]; 421 if (voice->flags & VOICE_IN_USE) 422 continue; 423 voice->flags |= VOICE_IN_USE; 424 goto found_one; 425 } 426 voice = NULL; 427 428 found_one: 429 return voice; 430 } 431 432 static struct voice *sis_alloc_playback_voice(struct sis7019 *sis) 433 { 434 struct voice *voice; 435 unsigned long flags; 436 437 spin_lock_irqsave(&sis->voice_lock, flags); 438 voice = __sis_alloc_playback_voice(sis); 439 spin_unlock_irqrestore(&sis->voice_lock, flags); 440 441 return voice; 442 } 443 444 static int sis_alloc_timing_voice(struct snd_pcm_substream *substream, 445 struct snd_pcm_hw_params *hw_params) 446 { 447 struct sis7019 *sis = snd_pcm_substream_chip(substream); 448 struct snd_pcm_runtime *runtime = substream->runtime; 449 struct voice *voice = runtime->private_data; 450 unsigned int period_size, buffer_size; 451 unsigned long flags; 452 int needed; 453 454 /* If there are one or two periods per buffer, we don't need a 455 * timing voice, as we can use the capture channel's interrupts 456 * to clock out the periods. 457 */ 458 period_size = params_period_size(hw_params); 459 buffer_size = params_buffer_size(hw_params); 460 needed = (period_size != buffer_size && 461 period_size != (buffer_size / 2)); 462 463 if (needed && !voice->timing) { 464 spin_lock_irqsave(&sis->voice_lock, flags); 465 voice->timing = __sis_alloc_playback_voice(sis); 466 if (voice->timing) 467 __sis_map_silence(sis); 468 spin_unlock_irqrestore(&sis->voice_lock, flags); 469 if (!voice->timing) 470 return -ENOMEM; 471 voice->timing->substream = substream; 472 } else if (!needed && voice->timing) { 473 sis_free_voice(sis, voice); 474 voice->timing = NULL; 475 } 476 477 return 0; 478 } 479 480 static int sis_playback_open(struct snd_pcm_substream *substream) 481 { 482 struct sis7019 *sis = snd_pcm_substream_chip(substream); 483 struct snd_pcm_runtime *runtime = substream->runtime; 484 struct voice *voice; 485 486 voice = sis_alloc_playback_voice(sis); 487 if (!voice) 488 return -EAGAIN; 489 490 voice->substream = substream; 491 runtime->private_data = voice; 492 runtime->hw = sis_playback_hw_info; 493 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 494 9, 0xfff9); 495 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 496 9, 0xfff9); 497 snd_pcm_set_sync(substream); 498 return 0; 499 } 500 501 static int sis_substream_close(struct snd_pcm_substream *substream) 502 { 503 struct sis7019 *sis = snd_pcm_substream_chip(substream); 504 struct snd_pcm_runtime *runtime = substream->runtime; 505 struct voice *voice = runtime->private_data; 506 507 sis_free_voice(sis, voice); 508 return 0; 509 } 510 511 static int sis_playback_hw_params(struct snd_pcm_substream *substream, 512 struct snd_pcm_hw_params *hw_params) 513 { 514 return snd_pcm_lib_malloc_pages(substream, 515 params_buffer_bytes(hw_params)); 516 } 517 518 static int sis_hw_free(struct snd_pcm_substream *substream) 519 { 520 return snd_pcm_lib_free_pages(substream); 521 } 522 523 static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream) 524 { 525 struct snd_pcm_runtime *runtime = substream->runtime; 526 struct voice *voice = runtime->private_data; 527 void __iomem *ctrl_base = voice->ctrl_base; 528 void __iomem *wave_base = voice->wave_base; 529 u32 format, dma_addr, control, sso_eso, delta, reg; 530 u16 leo; 531 532 /* We rely on the PCM core to ensure that the parameters for this 533 * substream do not change on us while we're programming the HW. 534 */ 535 format = 0; 536 if (snd_pcm_format_width(runtime->format) == 8) 537 format |= SIS_PLAY_DMA_FORMAT_8BIT; 538 if (!snd_pcm_format_signed(runtime->format)) 539 format |= SIS_PLAY_DMA_FORMAT_UNSIGNED; 540 if (runtime->channels == 1) 541 format |= SIS_PLAY_DMA_FORMAT_MONO; 542 543 /* The baseline setup is for a single period per buffer, and 544 * we add bells and whistles as needed from there. 545 */ 546 dma_addr = runtime->dma_addr; 547 leo = runtime->buffer_size - 1; 548 control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO; 549 sso_eso = leo; 550 551 if (runtime->period_size == (runtime->buffer_size / 2)) { 552 control |= SIS_PLAY_DMA_INTR_AT_MLP; 553 } else if (runtime->period_size != runtime->buffer_size) { 554 voice->flags |= VOICE_SSO_TIMING; 555 voice->sso = runtime->period_size - 1; 556 voice->period_size = runtime->period_size; 557 voice->buffer_size = runtime->buffer_size; 558 559 control &= ~SIS_PLAY_DMA_INTR_AT_LEO; 560 control |= SIS_PLAY_DMA_INTR_AT_SSO; 561 sso_eso |= (runtime->period_size - 1) << 16; 562 } 563 564 delta = sis_rate_to_delta(runtime->rate); 565 566 /* Ok, we're ready to go, set up the channel. 567 */ 568 writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO); 569 writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE); 570 writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL); 571 writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO); 572 573 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4) 574 writel(0, wave_base + reg); 575 576 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL); 577 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION); 578 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE | 579 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE | 580 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE, 581 wave_base + SIS_WAVE_CHANNEL_CONTROL); 582 583 /* Force PCI writes to post. */ 584 readl(ctrl_base); 585 586 return 0; 587 } 588 589 static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 590 { 591 struct sis7019 *sis = snd_pcm_substream_chip(substream); 592 unsigned long io = sis->ioport; 593 struct snd_pcm_substream *s; 594 struct voice *voice; 595 void *chip; 596 int starting; 597 u32 record = 0; 598 u32 play[2] = { 0, 0 }; 599 600 /* No locks needed, as the PCM core will hold the locks on the 601 * substreams, and the HW will only start/stop the indicated voices 602 * without changing the state of the others. 603 */ 604 switch (cmd) { 605 case SNDRV_PCM_TRIGGER_START: 606 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 607 case SNDRV_PCM_TRIGGER_RESUME: 608 starting = 1; 609 break; 610 case SNDRV_PCM_TRIGGER_STOP: 611 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 612 case SNDRV_PCM_TRIGGER_SUSPEND: 613 starting = 0; 614 break; 615 default: 616 return -EINVAL; 617 } 618 619 snd_pcm_group_for_each_entry(s, substream) { 620 /* Make sure it is for us... */ 621 chip = snd_pcm_substream_chip(s); 622 if (chip != sis) 623 continue; 624 625 voice = s->runtime->private_data; 626 if (voice->flags & VOICE_CAPTURE) { 627 record |= 1 << voice->num; 628 voice = voice->timing; 629 } 630 631 /* voice could be NULL if this a recording stream, and it 632 * doesn't have an external timing channel. 633 */ 634 if (voice) 635 play[voice->num / 32] |= 1 << (voice->num & 0x1f); 636 637 snd_pcm_trigger_done(s, substream); 638 } 639 640 if (starting) { 641 if (record) 642 outl(record, io + SIS_RECORD_START_REG); 643 if (play[0]) 644 outl(play[0], io + SIS_PLAY_START_A_REG); 645 if (play[1]) 646 outl(play[1], io + SIS_PLAY_START_B_REG); 647 } else { 648 if (record) 649 outl(record, io + SIS_RECORD_STOP_REG); 650 if (play[0]) 651 outl(play[0], io + SIS_PLAY_STOP_A_REG); 652 if (play[1]) 653 outl(play[1], io + SIS_PLAY_STOP_B_REG); 654 } 655 return 0; 656 } 657 658 static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream) 659 { 660 struct snd_pcm_runtime *runtime = substream->runtime; 661 struct voice *voice = runtime->private_data; 662 u32 cso; 663 664 cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO); 665 cso &= 0xffff; 666 return cso; 667 } 668 669 static int sis_capture_open(struct snd_pcm_substream *substream) 670 { 671 struct sis7019 *sis = snd_pcm_substream_chip(substream); 672 struct snd_pcm_runtime *runtime = substream->runtime; 673 struct voice *voice = &sis->capture_voice; 674 unsigned long flags; 675 676 /* FIXME: The driver only supports recording from one channel 677 * at the moment, but it could support more. 678 */ 679 spin_lock_irqsave(&sis->voice_lock, flags); 680 if (voice->flags & VOICE_IN_USE) 681 voice = NULL; 682 else 683 voice->flags |= VOICE_IN_USE; 684 spin_unlock_irqrestore(&sis->voice_lock, flags); 685 686 if (!voice) 687 return -EAGAIN; 688 689 voice->substream = substream; 690 runtime->private_data = voice; 691 runtime->hw = sis_capture_hw_info; 692 runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC]; 693 snd_pcm_limit_hw_rates(runtime); 694 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 695 9, 0xfff9); 696 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 697 9, 0xfff9); 698 snd_pcm_set_sync(substream); 699 return 0; 700 } 701 702 static int sis_capture_hw_params(struct snd_pcm_substream *substream, 703 struct snd_pcm_hw_params *hw_params) 704 { 705 struct sis7019 *sis = snd_pcm_substream_chip(substream); 706 int rc; 707 708 rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE, 709 params_rate(hw_params)); 710 if (rc) 711 goto out; 712 713 rc = snd_pcm_lib_malloc_pages(substream, 714 params_buffer_bytes(hw_params)); 715 if (rc < 0) 716 goto out; 717 718 rc = sis_alloc_timing_voice(substream, hw_params); 719 720 out: 721 return rc; 722 } 723 724 static void sis_prepare_timing_voice(struct voice *voice, 725 struct snd_pcm_substream *substream) 726 { 727 struct sis7019 *sis = snd_pcm_substream_chip(substream); 728 struct snd_pcm_runtime *runtime = substream->runtime; 729 struct voice *timing = voice->timing; 730 void __iomem *play_base = timing->ctrl_base; 731 void __iomem *wave_base = timing->wave_base; 732 u16 buffer_size, period_size; 733 u32 format, control, sso_eso, delta; 734 u32 vperiod, sso, reg; 735 736 /* Set our initial buffer and period as large as we can given a 737 * single page of silence. 738 */ 739 buffer_size = 4096 / runtime->channels; 740 buffer_size /= snd_pcm_format_size(runtime->format, 1); 741 period_size = buffer_size; 742 743 /* Initially, we want to interrupt just a bit behind the end of 744 * the period we're clocking out. 10 samples seems to give a good 745 * delay. 746 * 747 * We want to spread our interrupts throughout the virtual period, 748 * so that we don't end up with two interrupts back to back at the 749 * end -- this helps minimize the effects of any jitter. Adjust our 750 * clocking period size so that the last period is at least a fourth 751 * of a full period. 752 * 753 * This is all moot if we don't need to use virtual periods. 754 */ 755 vperiod = runtime->period_size + 10; 756 if (vperiod > period_size) { 757 u16 tail = vperiod % period_size; 758 u16 quarter_period = period_size / 4; 759 760 if (tail && tail < quarter_period) { 761 u16 loops = vperiod / period_size; 762 763 tail = quarter_period - tail; 764 tail += loops - 1; 765 tail /= loops; 766 period_size -= tail; 767 } 768 769 sso = period_size - 1; 770 } else { 771 /* The initial period will fit inside the buffer, so we 772 * don't need to use virtual periods -- disable them. 773 */ 774 period_size = runtime->period_size; 775 sso = vperiod - 1; 776 vperiod = 0; 777 } 778 779 /* The interrupt handler implements the timing syncronization, so 780 * setup its state. 781 */ 782 timing->flags |= VOICE_SYNC_TIMING; 783 timing->sync_base = voice->ctrl_base; 784 timing->sync_cso = runtime->period_size - 1; 785 timing->sync_period_size = runtime->period_size; 786 timing->sync_buffer_size = runtime->buffer_size; 787 timing->period_size = period_size; 788 timing->buffer_size = buffer_size; 789 timing->sso = sso; 790 timing->vperiod = vperiod; 791 792 /* Using unsigned samples with the all-zero silence buffer 793 * forces the output to the lower rail, killing playback. 794 * So ignore unsigned vs signed -- it doesn't change the timing. 795 */ 796 format = 0; 797 if (snd_pcm_format_width(runtime->format) == 8) 798 format = SIS_CAPTURE_DMA_FORMAT_8BIT; 799 if (runtime->channels == 1) 800 format |= SIS_CAPTURE_DMA_FORMAT_MONO; 801 802 control = timing->buffer_size - 1; 803 control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO; 804 sso_eso = timing->buffer_size - 1; 805 sso_eso |= timing->sso << 16; 806 807 delta = sis_rate_to_delta(runtime->rate); 808 809 /* We've done the math, now configure the channel. 810 */ 811 writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO); 812 writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE); 813 writel(control, play_base + SIS_PLAY_DMA_CONTROL); 814 writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO); 815 816 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4) 817 writel(0, wave_base + reg); 818 819 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL); 820 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION); 821 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE | 822 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE | 823 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE, 824 wave_base + SIS_WAVE_CHANNEL_CONTROL); 825 } 826 827 static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream) 828 { 829 struct snd_pcm_runtime *runtime = substream->runtime; 830 struct voice *voice = runtime->private_data; 831 void __iomem *rec_base = voice->ctrl_base; 832 u32 format, dma_addr, control; 833 u16 leo; 834 835 /* We rely on the PCM core to ensure that the parameters for this 836 * substream do not change on us while we're programming the HW. 837 */ 838 format = 0; 839 if (snd_pcm_format_width(runtime->format) == 8) 840 format = SIS_CAPTURE_DMA_FORMAT_8BIT; 841 if (!snd_pcm_format_signed(runtime->format)) 842 format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED; 843 if (runtime->channels == 1) 844 format |= SIS_CAPTURE_DMA_FORMAT_MONO; 845 846 dma_addr = runtime->dma_addr; 847 leo = runtime->buffer_size - 1; 848 control = leo | SIS_CAPTURE_DMA_LOOP; 849 850 /* If we've got more than two periods per buffer, then we have 851 * use a timing voice to clock out the periods. Otherwise, we can 852 * use the capture channel's interrupts. 853 */ 854 if (voice->timing) { 855 sis_prepare_timing_voice(voice, substream); 856 } else { 857 control |= SIS_CAPTURE_DMA_INTR_AT_LEO; 858 if (runtime->period_size != runtime->buffer_size) 859 control |= SIS_CAPTURE_DMA_INTR_AT_MLP; 860 } 861 862 writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO); 863 writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE); 864 writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL); 865 866 /* Force the writes to post. */ 867 readl(rec_base); 868 869 return 0; 870 } 871 872 static struct snd_pcm_ops sis_playback_ops = { 873 .open = sis_playback_open, 874 .close = sis_substream_close, 875 .ioctl = snd_pcm_lib_ioctl, 876 .hw_params = sis_playback_hw_params, 877 .hw_free = sis_hw_free, 878 .prepare = sis_pcm_playback_prepare, 879 .trigger = sis_pcm_trigger, 880 .pointer = sis_pcm_pointer, 881 }; 882 883 static struct snd_pcm_ops sis_capture_ops = { 884 .open = sis_capture_open, 885 .close = sis_substream_close, 886 .ioctl = snd_pcm_lib_ioctl, 887 .hw_params = sis_capture_hw_params, 888 .hw_free = sis_hw_free, 889 .prepare = sis_pcm_capture_prepare, 890 .trigger = sis_pcm_trigger, 891 .pointer = sis_pcm_pointer, 892 }; 893 894 static int __devinit sis_pcm_create(struct sis7019 *sis) 895 { 896 struct snd_pcm *pcm; 897 int rc; 898 899 /* We have 64 voices, and the driver currently records from 900 * only one channel, though that could change in the future. 901 */ 902 rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm); 903 if (rc) 904 return rc; 905 906 pcm->private_data = sis; 907 strcpy(pcm->name, "SiS7019"); 908 sis->pcm = pcm; 909 910 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops); 911 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops); 912 913 /* Try to preallocate some memory, but it's not the end of the 914 * world if this fails. 915 */ 916 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 917 snd_dma_pci_data(sis->pci), 64*1024, 128*1024); 918 919 return 0; 920 } 921 922 static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd) 923 { 924 unsigned long io = sis->ioport; 925 unsigned short val = 0xffff; 926 u16 status; 927 u16 rdy; 928 int count; 929 static const u16 codec_ready[3] = { 930 SIS_AC97_STATUS_CODEC_READY, 931 SIS_AC97_STATUS_CODEC2_READY, 932 SIS_AC97_STATUS_CODEC3_READY, 933 }; 934 935 rdy = codec_ready[codec]; 936 937 938 /* Get the AC97 semaphore -- software first, so we don't spin 939 * pounding out IO reads on the hardware semaphore... 940 */ 941 mutex_lock(&sis->ac97_mutex); 942 943 count = 0xffff; 944 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count) 945 udelay(1); 946 947 if (!count) 948 goto timeout; 949 950 /* ... and wait for any outstanding commands to complete ... 951 */ 952 count = 0xffff; 953 do { 954 status = inw(io + SIS_AC97_STATUS); 955 if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY)) 956 break; 957 958 udelay(1); 959 } while (--count); 960 961 if (!count) 962 goto timeout_sema; 963 964 /* ... before sending our command and waiting for it to finish ... 965 */ 966 outl(cmd, io + SIS_AC97_CMD); 967 udelay(10); 968 969 count = 0xffff; 970 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count) 971 udelay(1); 972 973 /* ... and reading the results (if any). 974 */ 975 val = inl(io + SIS_AC97_CMD) >> 16; 976 977 timeout_sema: 978 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA); 979 timeout: 980 mutex_unlock(&sis->ac97_mutex); 981 982 if (!count) { 983 printk(KERN_ERR "sis7019: ac97 codec %d timeout cmd 0x%08x\n", 984 codec, cmd); 985 } 986 987 return val; 988 } 989 990 static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg, 991 unsigned short val) 992 { 993 static const u32 cmd[3] = { 994 SIS_AC97_CMD_CODEC_WRITE, 995 SIS_AC97_CMD_CODEC2_WRITE, 996 SIS_AC97_CMD_CODEC3_WRITE, 997 }; 998 sis_ac97_rw(ac97->private_data, ac97->num, 999 (val << 16) | (reg << 8) | cmd[ac97->num]); 1000 } 1001 1002 static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg) 1003 { 1004 static const u32 cmd[3] = { 1005 SIS_AC97_CMD_CODEC_READ, 1006 SIS_AC97_CMD_CODEC2_READ, 1007 SIS_AC97_CMD_CODEC3_READ, 1008 }; 1009 return sis_ac97_rw(ac97->private_data, ac97->num, 1010 (reg << 8) | cmd[ac97->num]); 1011 } 1012 1013 static int __devinit sis_mixer_create(struct sis7019 *sis) 1014 { 1015 struct snd_ac97_bus *bus; 1016 struct snd_ac97_template ac97; 1017 static struct snd_ac97_bus_ops ops = { 1018 .write = sis_ac97_write, 1019 .read = sis_ac97_read, 1020 }; 1021 int rc; 1022 1023 memset(&ac97, 0, sizeof(ac97)); 1024 ac97.private_data = sis; 1025 1026 rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus); 1027 if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT) 1028 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]); 1029 ac97.num = 1; 1030 if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)) 1031 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]); 1032 ac97.num = 2; 1033 if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)) 1034 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]); 1035 1036 /* If we return an error here, then snd_card_free() should 1037 * free up any ac97 codecs that got created, as well as the bus. 1038 */ 1039 return rc; 1040 } 1041 1042 static void sis_free_suspend(struct sis7019 *sis) 1043 { 1044 int i; 1045 1046 for (i = 0; i < SIS_SUSPEND_PAGES; i++) 1047 kfree(sis->suspend_state[i]); 1048 } 1049 1050 static int sis_chip_free(struct sis7019 *sis) 1051 { 1052 /* Reset the chip, and disable all interrputs. 1053 */ 1054 outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR); 1055 udelay(10); 1056 outl(0, sis->ioport + SIS_GCR); 1057 outl(0, sis->ioport + SIS_GIER); 1058 1059 /* Now, free everything we allocated. 1060 */ 1061 if (sis->irq >= 0) 1062 free_irq(sis->irq, sis); 1063 1064 if (sis->ioaddr) 1065 iounmap(sis->ioaddr); 1066 1067 pci_release_regions(sis->pci); 1068 pci_disable_device(sis->pci); 1069 1070 sis_free_suspend(sis); 1071 return 0; 1072 } 1073 1074 static int sis_dev_free(struct snd_device *dev) 1075 { 1076 struct sis7019 *sis = dev->device_data; 1077 return sis_chip_free(sis); 1078 } 1079 1080 static int sis_chip_init(struct sis7019 *sis) 1081 { 1082 unsigned long io = sis->ioport; 1083 void __iomem *ioaddr = sis->ioaddr; 1084 unsigned long timeout; 1085 u16 status; 1086 int count; 1087 int i; 1088 1089 /* Reset the audio controller 1090 */ 1091 outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR); 1092 udelay(10); 1093 outl(0, io + SIS_GCR); 1094 1095 /* Get the AC-link semaphore, and reset the codecs 1096 */ 1097 count = 0xffff; 1098 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count) 1099 udelay(1); 1100 1101 if (!count) 1102 return -EIO; 1103 1104 outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD); 1105 udelay(10); 1106 1107 count = 0xffff; 1108 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count) 1109 udelay(1); 1110 1111 /* Command complete, we can let go of the semaphore now. 1112 */ 1113 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA); 1114 if (!count) 1115 return -EIO; 1116 1117 /* Now that we've finished the reset, find out what's attached. 1118 * There are some codec/board combinations that take an extremely 1119 * long time to come up. 350+ ms has been observed in the field, 1120 * so we'll give them up to 500ms. 1121 */ 1122 sis->codecs_present = 0; 1123 timeout = msecs_to_jiffies(500) + jiffies; 1124 while (time_before_eq(jiffies, timeout)) { 1125 status = inl(io + SIS_AC97_STATUS); 1126 if (status & SIS_AC97_STATUS_CODEC_READY) 1127 sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT; 1128 if (status & SIS_AC97_STATUS_CODEC2_READY) 1129 sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT; 1130 if (status & SIS_AC97_STATUS_CODEC3_READY) 1131 sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT; 1132 1133 if (sis->codecs_present == codecs) 1134 break; 1135 1136 msleep(1); 1137 } 1138 1139 /* All done, check for errors. 1140 */ 1141 if (!sis->codecs_present) { 1142 printk(KERN_ERR "sis7019: could not find any codecs\n"); 1143 return -EIO; 1144 } 1145 1146 if (sis->codecs_present != codecs) { 1147 printk(KERN_WARNING "sis7019: missing codecs, found %0x, expected %0x\n", 1148 sis->codecs_present, codecs); 1149 } 1150 1151 /* Let the hardware know that the audio driver is alive, 1152 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and 1153 * record channels. We're going to want to use Variable Rate Audio 1154 * for recording, to avoid needlessly resampling from 48kHZ. 1155 */ 1156 outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF); 1157 outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE | 1158 SIS_AC97_CONF_PCM_CAP_MIC_ENABLE | 1159 SIS_AC97_CONF_PCM_CAP_LR_ENABLE | 1160 SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF); 1161 1162 /* All AC97 PCM slots should be sourced from sub-mixer 0. 1163 */ 1164 outl(0, io + SIS_AC97_PSR); 1165 1166 /* There is only one valid DMA setup for a PCI environment. 1167 */ 1168 outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR); 1169 1170 /* Reset the syncronization groups for all of the channels 1171 * to be asyncronous. If we start doing SPDIF or 5.1 sound, etc. 1172 * we'll need to change how we handle these. Until then, we just 1173 * assign sub-mixer 0 to all playback channels, and avoid any 1174 * attenuation on the audio. 1175 */ 1176 outl(0, io + SIS_PLAY_SYNC_GROUP_A); 1177 outl(0, io + SIS_PLAY_SYNC_GROUP_B); 1178 outl(0, io + SIS_PLAY_SYNC_GROUP_C); 1179 outl(0, io + SIS_PLAY_SYNC_GROUP_D); 1180 outl(0, io + SIS_MIXER_SYNC_GROUP); 1181 1182 for (i = 0; i < 64; i++) { 1183 writel(i, SIS_MIXER_START_ADDR(ioaddr, i)); 1184 writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN | 1185 SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i)); 1186 } 1187 1188 /* Don't attenuate any audio set for the wave amplifier. 1189 * 1190 * FIXME: Maximum attenuation is set for the music amp, which will 1191 * need to change if we start using the synth engine. 1192 */ 1193 outl(0xffff0000, io + SIS_WEVCR); 1194 1195 /* Ensure that the wave engine is in normal operating mode. 1196 */ 1197 outl(0, io + SIS_WECCR); 1198 1199 /* Go ahead and enable the DMA interrupts. They won't go live 1200 * until we start a channel. 1201 */ 1202 outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE | 1203 SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER); 1204 1205 return 0; 1206 } 1207 1208 #ifdef CONFIG_PM 1209 static int sis_suspend(struct pci_dev *pci, pm_message_t state) 1210 { 1211 struct snd_card *card = pci_get_drvdata(pci); 1212 struct sis7019 *sis = card->private_data; 1213 void __iomem *ioaddr = sis->ioaddr; 1214 int i; 1215 1216 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1217 snd_pcm_suspend_all(sis->pcm); 1218 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT) 1219 snd_ac97_suspend(sis->ac97[0]); 1220 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT) 1221 snd_ac97_suspend(sis->ac97[1]); 1222 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT) 1223 snd_ac97_suspend(sis->ac97[2]); 1224 1225 /* snd_pcm_suspend_all() stopped all channels, so we're quiescent. 1226 */ 1227 if (sis->irq >= 0) { 1228 free_irq(sis->irq, sis); 1229 sis->irq = -1; 1230 } 1231 1232 /* Save the internal state away 1233 */ 1234 for (i = 0; i < 4; i++) { 1235 memcpy_fromio(sis->suspend_state[i], ioaddr, 4096); 1236 ioaddr += 4096; 1237 } 1238 1239 pci_disable_device(pci); 1240 pci_save_state(pci); 1241 pci_set_power_state(pci, pci_choose_state(pci, state)); 1242 return 0; 1243 } 1244 1245 static int sis_resume(struct pci_dev *pci) 1246 { 1247 struct snd_card *card = pci_get_drvdata(pci); 1248 struct sis7019 *sis = card->private_data; 1249 void __iomem *ioaddr = sis->ioaddr; 1250 int i; 1251 1252 pci_set_power_state(pci, PCI_D0); 1253 pci_restore_state(pci); 1254 1255 if (pci_enable_device(pci) < 0) { 1256 printk(KERN_ERR "sis7019: unable to re-enable device\n"); 1257 goto error; 1258 } 1259 1260 if (sis_chip_init(sis)) { 1261 printk(KERN_ERR "sis7019: unable to re-init controller\n"); 1262 goto error; 1263 } 1264 1265 if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED, 1266 card->shortname, sis)) { 1267 printk(KERN_ERR "sis7019: unable to regain IRQ %d\n", pci->irq); 1268 goto error; 1269 } 1270 1271 /* Restore saved state, then clear out the page we use for the 1272 * silence buffer. 1273 */ 1274 for (i = 0; i < 4; i++) { 1275 memcpy_toio(ioaddr, sis->suspend_state[i], 4096); 1276 ioaddr += 4096; 1277 } 1278 1279 memset(sis->suspend_state[0], 0, 4096); 1280 1281 sis->irq = pci->irq; 1282 pci_set_master(pci); 1283 1284 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT) 1285 snd_ac97_resume(sis->ac97[0]); 1286 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT) 1287 snd_ac97_resume(sis->ac97[1]); 1288 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT) 1289 snd_ac97_resume(sis->ac97[2]); 1290 1291 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1292 return 0; 1293 1294 error: 1295 snd_card_disconnect(card); 1296 return -EIO; 1297 } 1298 #endif /* CONFIG_PM */ 1299 1300 static int sis_alloc_suspend(struct sis7019 *sis) 1301 { 1302 int i; 1303 1304 /* We need 16K to store the internal wave engine state during a 1305 * suspend, but we don't need it to be contiguous, so play nice 1306 * with the memory system. We'll also use this area for a silence 1307 * buffer. 1308 */ 1309 for (i = 0; i < SIS_SUSPEND_PAGES; i++) { 1310 sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL); 1311 if (!sis->suspend_state[i]) 1312 return -ENOMEM; 1313 } 1314 memset(sis->suspend_state[0], 0, 4096); 1315 1316 return 0; 1317 } 1318 1319 static int __devinit sis_chip_create(struct snd_card *card, 1320 struct pci_dev *pci) 1321 { 1322 struct sis7019 *sis = card->private_data; 1323 struct voice *voice; 1324 static struct snd_device_ops ops = { 1325 .dev_free = sis_dev_free, 1326 }; 1327 int rc; 1328 int i; 1329 1330 rc = pci_enable_device(pci); 1331 if (rc) 1332 goto error_out; 1333 1334 if (pci_set_dma_mask(pci, DMA_BIT_MASK(30)) < 0) { 1335 printk(KERN_ERR "sis7019: architecture does not support " 1336 "30-bit PCI busmaster DMA"); 1337 goto error_out_enabled; 1338 } 1339 1340 memset(sis, 0, sizeof(*sis)); 1341 mutex_init(&sis->ac97_mutex); 1342 spin_lock_init(&sis->voice_lock); 1343 sis->card = card; 1344 sis->pci = pci; 1345 sis->irq = -1; 1346 sis->ioport = pci_resource_start(pci, 0); 1347 1348 rc = pci_request_regions(pci, "SiS7019"); 1349 if (rc) { 1350 printk(KERN_ERR "sis7019: unable request regions\n"); 1351 goto error_out_enabled; 1352 } 1353 1354 rc = -EIO; 1355 sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000); 1356 if (!sis->ioaddr) { 1357 printk(KERN_ERR "sis7019: unable to remap MMIO, aborting\n"); 1358 goto error_out_cleanup; 1359 } 1360 1361 rc = sis_alloc_suspend(sis); 1362 if (rc < 0) { 1363 printk(KERN_ERR "sis7019: unable to allocate state storage\n"); 1364 goto error_out_cleanup; 1365 } 1366 1367 rc = sis_chip_init(sis); 1368 if (rc) 1369 goto error_out_cleanup; 1370 1371 if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED, 1372 card->shortname, sis)) { 1373 printk(KERN_ERR "unable to allocate irq %d\n", sis->irq); 1374 goto error_out_cleanup; 1375 } 1376 1377 sis->irq = pci->irq; 1378 pci_set_master(pci); 1379 1380 for (i = 0; i < 64; i++) { 1381 voice = &sis->voices[i]; 1382 voice->num = i; 1383 voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i); 1384 voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i); 1385 } 1386 1387 voice = &sis->capture_voice; 1388 voice->flags = VOICE_CAPTURE; 1389 voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN; 1390 voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num); 1391 1392 rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops); 1393 if (rc) 1394 goto error_out_cleanup; 1395 1396 snd_card_set_dev(card, &pci->dev); 1397 1398 return 0; 1399 1400 error_out_cleanup: 1401 sis_chip_free(sis); 1402 1403 error_out_enabled: 1404 pci_disable_device(pci); 1405 1406 error_out: 1407 return rc; 1408 } 1409 1410 static int __devinit snd_sis7019_probe(struct pci_dev *pci, 1411 const struct pci_device_id *pci_id) 1412 { 1413 struct snd_card *card; 1414 struct sis7019 *sis; 1415 int rc; 1416 1417 rc = -ENOENT; 1418 if (!enable) 1419 goto error_out; 1420 1421 /* The user can specify which codecs should be present so that we 1422 * can wait for them to show up if they are slow to recover from 1423 * the AC97 cold reset. We default to a single codec, the primary. 1424 * 1425 * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2. 1426 */ 1427 codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT | 1428 SIS_TERTIARY_CODEC_PRESENT; 1429 if (!codecs) 1430 codecs = SIS_PRIMARY_CODEC_PRESENT; 1431 1432 rc = snd_card_create(index, id, THIS_MODULE, sizeof(*sis), &card); 1433 if (rc < 0) 1434 goto error_out; 1435 1436 strcpy(card->driver, "SiS7019"); 1437 strcpy(card->shortname, "SiS7019"); 1438 rc = sis_chip_create(card, pci); 1439 if (rc) 1440 goto card_error_out; 1441 1442 sis = card->private_data; 1443 1444 rc = sis_mixer_create(sis); 1445 if (rc) 1446 goto card_error_out; 1447 1448 rc = sis_pcm_create(sis); 1449 if (rc) 1450 goto card_error_out; 1451 1452 snprintf(card->longname, sizeof(card->longname), 1453 "%s Audio Accelerator with %s at 0x%lx, irq %d", 1454 card->shortname, snd_ac97_get_short_name(sis->ac97[0]), 1455 sis->ioport, sis->irq); 1456 1457 rc = snd_card_register(card); 1458 if (rc) 1459 goto card_error_out; 1460 1461 pci_set_drvdata(pci, card); 1462 return 0; 1463 1464 card_error_out: 1465 snd_card_free(card); 1466 1467 error_out: 1468 return rc; 1469 } 1470 1471 static void __devexit snd_sis7019_remove(struct pci_dev *pci) 1472 { 1473 snd_card_free(pci_get_drvdata(pci)); 1474 pci_set_drvdata(pci, NULL); 1475 } 1476 1477 static struct pci_driver sis7019_driver = { 1478 .name = "SiS7019", 1479 .id_table = snd_sis7019_ids, 1480 .probe = snd_sis7019_probe, 1481 .remove = __devexit_p(snd_sis7019_remove), 1482 1483 #ifdef CONFIG_PM 1484 .suspend = sis_suspend, 1485 .resume = sis_resume, 1486 #endif 1487 }; 1488 1489 static int __init sis7019_init(void) 1490 { 1491 return pci_register_driver(&sis7019_driver); 1492 } 1493 1494 static void __exit sis7019_exit(void) 1495 { 1496 pci_unregister_driver(&sis7019_driver); 1497 } 1498 1499 module_init(sis7019_init); 1500 module_exit(sis7019_exit); 1501
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.