2 * Maintained by Jaroslav Kysela <perex@suse.cz>
3 * Originated by audio@tridentmicro.com
4 * Fri Feb 19 15:55:28 MST 1999
5 * Routines for control of Trident 4DWave (DX and NX) chip
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
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 License for more details.
22 * You should have received a copy of the GNU General Public License
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
27 * SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
30 #include <sound/driver.h>
31 #include <linux/delay.h>
32 #include <linux/init.h>
33 #include <linux/interrupt.h>
34 #include <linux/pci.h>
35 #include <linux/slab.h>
36 #include <linux/vmalloc.h>
37 #include <linux/gameport.h>
39 #include <sound/core.h>
40 #include <sound/info.h>
41 #include <sound/control.h>
42 #include <sound/trident.h>
43 #include <sound/asoundef.h>
47 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
48 struct snd_trident_voice * voice,
49 struct snd_pcm_substream *substream);
50 static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
51 struct snd_trident_voice * voice,
52 struct snd_pcm_substream *substream);
53 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id,
54 struct pt_regs *regs);
56 static int snd_trident_suspend(struct snd_card *card, pm_message_t state);
57 static int snd_trident_resume(struct snd_card *card);
59 static int snd_trident_sis_reset(struct snd_trident *trident);
61 static void snd_trident_clear_voices(struct snd_trident * trident,
62 unsigned short v_min, unsigned short v_max);
63 static int snd_trident_free(struct snd_trident *trident);
71 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
73 unsigned int val, tmp;
75 printk("Trident voice %i:\n", voice);
76 outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
77 val = inl(TRID_REG(trident, CH_LBA));
78 printk("LBA: 0x%x\n", val);
79 val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
80 printk("GVSel: %i\n", val >> 31);
81 printk("Pan: 0x%x\n", (val >> 24) & 0x7f);
82 printk("Vol: 0x%x\n", (val >> 16) & 0xff);
83 printk("CTRL: 0x%x\n", (val >> 12) & 0x0f);
84 printk("EC: 0x%x\n", val & 0x0fff);
85 if (trident->device != TRIDENT_DEVICE_ID_NX) {
86 val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
87 printk("CSO: 0x%x\n", val >> 16);
88 printk("Alpha: 0x%x\n", (val >> 4) & 0x0fff);
89 printk("FMS: 0x%x\n", val & 0x0f);
90 val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
91 printk("ESO: 0x%x\n", val >> 16);
92 printk("Delta: 0x%x\n", val & 0xffff);
93 val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
94 } else { // TRIDENT_DEVICE_ID_NX
95 val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
96 tmp = (val >> 24) & 0xff;
97 printk("CSO: 0x%x\n", val & 0x00ffffff);
98 val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
99 tmp |= (val >> 16) & 0xff00;
100 printk("Delta: 0x%x\n", tmp);
101 printk("ESO: 0x%x\n", val & 0x00ffffff);
102 val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
103 printk("Alpha: 0x%x\n", val >> 20);
104 printk("FMS: 0x%x\n", (val >> 16) & 0x0f);
106 printk("FMC: 0x%x\n", (val >> 14) & 3);
107 printk("RVol: 0x%x\n", (val >> 7) & 0x7f);
108 printk("CVol: 0x%x\n", val & 0x7f);
112 /*---------------------------------------------------------------------------
113 unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
115 Description: This routine will do all of the reading from the external
118 Parameters: ac97 - ac97 codec structure
119 reg - CODEC register index, from AC97 Hal.
121 returns: 16 bit value read from the AC97.
123 ---------------------------------------------------------------------------*/
124 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
126 unsigned int data = 0, treg;
127 unsigned short count = 0xffff;
129 struct snd_trident *trident = ac97->private_data;
131 spin_lock_irqsave(&trident->reg_lock, flags);
132 if (trident->device == TRIDENT_DEVICE_ID_DX) {
133 data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
134 outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
136 data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
137 if ((data & DX_AC97_BUSY_READ) == 0)
140 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
141 data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
142 treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
143 outl(data, TRID_REG(trident, treg));
145 data = inl(TRID_REG(trident, treg));
146 if ((data & 0x00000C00) == 0)
149 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
150 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
152 data |= SI_AC97_SECONDARY;
153 outl(data, TRID_REG(trident, SI_AC97_READ));
155 data = inl(TRID_REG(trident, SI_AC97_READ));
156 if ((data & (SI_AC97_BUSY_READ)) == 0)
161 if (count == 0 && !trident->ac97_detect) {
162 snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
167 spin_unlock_irqrestore(&trident->reg_lock, flags);
168 return ((unsigned short) (data >> 16));
171 /*---------------------------------------------------------------------------
172 void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
173 unsigned short wdata)
175 Description: This routine will do all of the writing to the external
178 Parameters: ac97 - ac97 codec structure
179 reg - CODEC register index, from AC97 Hal.
180 data - Lower 16 bits are the data to write to CODEC.
182 returns: TRUE if everything went ok, else FALSE.
184 ---------------------------------------------------------------------------*/
185 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
186 unsigned short wdata)
188 unsigned int address, data;
189 unsigned short count = 0xffff;
191 struct snd_trident *trident = ac97->private_data;
193 data = ((unsigned long) wdata) << 16;
195 spin_lock_irqsave(&trident->reg_lock, flags);
196 if (trident->device == TRIDENT_DEVICE_ID_DX) {
197 address = DX_ACR0_AC97_W;
199 /* read AC-97 write register status */
201 if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
205 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
206 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
207 address = NX_ACR1_AC97_W;
209 /* read AC-97 write register status */
211 if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
215 data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
216 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
217 address = SI_AC97_WRITE;
219 /* read AC-97 write register status */
221 if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
225 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
227 data |= SI_AC97_SECONDARY;
229 address = 0; /* keep GCC happy */
230 count = 0; /* return */
234 spin_unlock_irqrestore(&trident->reg_lock, flags);
237 outl(data, TRID_REG(trident, address));
238 spin_unlock_irqrestore(&trident->reg_lock, flags);
241 /*---------------------------------------------------------------------------
242 void snd_trident_enable_eso(struct snd_trident *trident)
244 Description: This routine will enable end of loop interrupts.
245 End of loop interrupts will occur when a running
247 Also enables middle of loop interrupts.
249 Parameters: trident - pointer to target device class for 4DWave.
251 ---------------------------------------------------------------------------*/
253 static void snd_trident_enable_eso(struct snd_trident * trident)
257 val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
260 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
262 outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
265 /*---------------------------------------------------------------------------
266 void snd_trident_disable_eso(struct snd_trident *trident)
268 Description: This routine will disable end of loop interrupts.
269 End of loop interrupts will occur when a running
271 Also disables middle of loop interrupts.
274 trident - pointer to target device class for 4DWave.
276 returns: TRUE if everything went ok, else FALSE.
278 ---------------------------------------------------------------------------*/
280 static void snd_trident_disable_eso(struct snd_trident * trident)
284 tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
287 outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
290 /*---------------------------------------------------------------------------
291 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
293 Description: Start a voice, any channel 0 thru 63.
294 This routine automatically handles the fact that there are
295 more than 32 channels available.
297 Parameters : voice - Voice number 0 thru n.
298 trident - pointer to target device class for 4DWave.
302 ---------------------------------------------------------------------------*/
304 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
306 unsigned int mask = 1 << (voice & 0x1f);
307 unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
309 outl(mask, TRID_REG(trident, reg));
312 /*---------------------------------------------------------------------------
313 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
315 Description: Stop a voice, any channel 0 thru 63.
316 This routine automatically handles the fact that there are
317 more than 32 channels available.
319 Parameters : voice - Voice number 0 thru n.
320 trident - pointer to target device class for 4DWave.
324 ---------------------------------------------------------------------------*/
326 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
328 unsigned int mask = 1 << (voice & 0x1f);
329 unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
331 outl(mask, TRID_REG(trident, reg));
334 /*---------------------------------------------------------------------------
335 int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
337 Description: Allocate hardware channel in Bank B (32-63).
339 Parameters : trident - pointer to target device class for 4DWave.
341 Return Value: hardware channel - 32-63 or -1 when no channel is available
343 ---------------------------------------------------------------------------*/
345 static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
349 if (trident->ChanPCMcnt >= trident->ChanPCM)
351 for (idx = 31; idx >= 0; idx--) {
352 if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
353 trident->ChanMap[T4D_BANK_B] |= 1 << idx;
354 trident->ChanPCMcnt++;
361 /*---------------------------------------------------------------------------
362 void snd_trident_free_pcm_channel(int channel)
364 Description: Free hardware channel in Bank B (32-63)
366 Parameters : trident - pointer to target device class for 4DWave.
367 channel - hardware channel number 0-63
371 ---------------------------------------------------------------------------*/
373 static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
375 if (channel < 32 || channel > 63)
378 if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
379 trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
380 trident->ChanPCMcnt--;
384 /*---------------------------------------------------------------------------
385 unsigned int snd_trident_allocate_synth_channel(void)
387 Description: Allocate hardware channel in Bank A (0-31).
389 Parameters : trident - pointer to target device class for 4DWave.
391 Return Value: hardware channel - 0-31 or -1 when no channel is available
393 ---------------------------------------------------------------------------*/
395 static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
399 for (idx = 31; idx >= 0; idx--) {
400 if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
401 trident->ChanMap[T4D_BANK_A] |= 1 << idx;
402 trident->synth.ChanSynthCount++;
409 /*---------------------------------------------------------------------------
410 void snd_trident_free_synth_channel( int channel )
412 Description: Free hardware channel in Bank B (0-31).
414 Parameters : trident - pointer to target device class for 4DWave.
415 channel - hardware channel number 0-63
419 ---------------------------------------------------------------------------*/
421 static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
423 if (channel < 0 || channel > 31)
426 if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
427 trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
428 trident->synth.ChanSynthCount--;
432 /*---------------------------------------------------------------------------
433 snd_trident_write_voice_regs
435 Description: This routine will complete and write the 5 hardware channel
436 registers to hardware.
438 Paramters: trident - pointer to target device class for 4DWave.
439 voice - synthesizer voice structure
442 ---------------------------------------------------------------------------*/
444 void snd_trident_write_voice_regs(struct snd_trident * trident,
445 struct snd_trident_voice * voice)
447 unsigned int FmcRvolCvol;
448 unsigned int regs[5];
450 regs[1] = voice->LBA;
451 regs[4] = (voice->GVSel << 31) |
452 ((voice->Pan & 0x0000007f) << 24) |
453 ((voice->CTRL & 0x0000000f) << 12);
454 FmcRvolCvol = ((voice->FMC & 3) << 14) |
455 ((voice->RVol & 0x7f) << 7) |
456 (voice->CVol & 0x7f);
458 switch (trident->device) {
459 case TRIDENT_DEVICE_ID_SI7018:
460 regs[4] |= voice->number > 31 ?
461 (voice->Vol & 0x000003ff) :
462 ((voice->Vol & 0x00003fc) << (16-2)) |
463 (voice->EC & 0x00000fff);
464 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
465 (voice->FMS & 0x0000000f);
466 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
467 regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
469 case TRIDENT_DEVICE_ID_DX:
470 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
471 (voice->EC & 0x00000fff);
472 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
473 (voice->FMS & 0x0000000f);
474 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
475 regs[3] = FmcRvolCvol;
477 case TRIDENT_DEVICE_ID_NX:
478 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
479 (voice->EC & 0x00000fff);
480 regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
481 regs[2] = ((voice->Delta << 16) & 0xff000000) |
482 (voice->ESO & 0x00ffffff);
483 regs[3] = (voice->Alpha << 20) |
484 ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
491 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
492 outl(regs[0], TRID_REG(trident, CH_START + 0));
493 outl(regs[1], TRID_REG(trident, CH_START + 4));
494 outl(regs[2], TRID_REG(trident, CH_START + 8));
495 outl(regs[3], TRID_REG(trident, CH_START + 12));
496 outl(regs[4], TRID_REG(trident, CH_START + 16));
499 printk("written %i channel:\n", voice->number);
500 printk(" regs[0] = 0x%x/0x%x\n", regs[0], inl(TRID_REG(trident, CH_START + 0)));
501 printk(" regs[1] = 0x%x/0x%x\n", regs[1], inl(TRID_REG(trident, CH_START + 4)));
502 printk(" regs[2] = 0x%x/0x%x\n", regs[2], inl(TRID_REG(trident, CH_START + 8)));
503 printk(" regs[3] = 0x%x/0x%x\n", regs[3], inl(TRID_REG(trident, CH_START + 12)));
504 printk(" regs[4] = 0x%x/0x%x\n", regs[4], inl(TRID_REG(trident, CH_START + 16)));
508 /*---------------------------------------------------------------------------
509 snd_trident_write_cso_reg
511 Description: This routine will write the new CSO offset
512 register to hardware.
514 Paramters: trident - pointer to target device class for 4DWave.
515 voice - synthesizer voice structure
518 ---------------------------------------------------------------------------*/
520 static void snd_trident_write_cso_reg(struct snd_trident * trident,
521 struct snd_trident_voice * voice,
525 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
526 if (trident->device != TRIDENT_DEVICE_ID_NX) {
527 outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
529 outl((voice->Delta << 24) |
530 (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
534 /*---------------------------------------------------------------------------
535 snd_trident_write_eso_reg
537 Description: This routine will write the new ESO offset
538 register to hardware.
540 Paramters: trident - pointer to target device class for 4DWave.
541 voice - synthesizer voice structure
544 ---------------------------------------------------------------------------*/
546 static void snd_trident_write_eso_reg(struct snd_trident * trident,
547 struct snd_trident_voice * voice,
551 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
552 if (trident->device != TRIDENT_DEVICE_ID_NX) {
553 outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
555 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
556 TRID_REG(trident, CH_NX_DELTA_ESO));
560 /*---------------------------------------------------------------------------
561 snd_trident_write_vol_reg
563 Description: This routine will write the new voice volume
564 register to hardware.
566 Paramters: trident - pointer to target device class for 4DWave.
567 voice - synthesizer voice structure
568 Vol - new voice volume
570 ---------------------------------------------------------------------------*/
572 static void snd_trident_write_vol_reg(struct snd_trident * trident,
573 struct snd_trident_voice * voice,
577 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
578 switch (trident->device) {
579 case TRIDENT_DEVICE_ID_DX:
580 case TRIDENT_DEVICE_ID_NX:
581 outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
583 case TRIDENT_DEVICE_ID_SI7018:
584 // printk("voice->Vol = 0x%x\n", voice->Vol);
585 outw((voice->CTRL << 12) | voice->Vol,
586 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
591 /*---------------------------------------------------------------------------
592 snd_trident_write_pan_reg
594 Description: This routine will write the new voice pan
595 register to hardware.
597 Paramters: trident - pointer to target device class for 4DWave.
598 voice - synthesizer voice structure
601 ---------------------------------------------------------------------------*/
603 static void snd_trident_write_pan_reg(struct snd_trident * trident,
604 struct snd_trident_voice * voice,
608 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
609 outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
610 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
613 /*---------------------------------------------------------------------------
614 snd_trident_write_rvol_reg
616 Description: This routine will write the new reverb volume
617 register to hardware.
619 Paramters: trident - pointer to target device class for 4DWave.
620 voice - synthesizer voice structure
621 RVol - new reverb volume
623 ---------------------------------------------------------------------------*/
625 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
626 struct snd_trident_voice * voice,
630 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
631 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
632 (voice->CVol & 0x007f),
633 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
634 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
637 /*---------------------------------------------------------------------------
638 snd_trident_write_cvol_reg
640 Description: This routine will write the new chorus volume
641 register to hardware.
643 Paramters: trident - pointer to target device class for 4DWave.
644 voice - synthesizer voice structure
645 CVol - new chorus volume
647 ---------------------------------------------------------------------------*/
649 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
650 struct snd_trident_voice * voice,
654 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
655 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
656 (voice->CVol & 0x007f),
657 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
658 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
661 /*---------------------------------------------------------------------------
662 snd_trident_convert_rate
664 Description: This routine converts rate in HZ to hardware delta value.
666 Paramters: trident - pointer to target device class for 4DWave.
667 rate - Real or Virtual channel number.
669 Returns: Delta value.
671 ---------------------------------------------------------------------------*/
672 static unsigned int snd_trident_convert_rate(unsigned int rate)
676 // We special case 44100 and 8000 since rounding with the equation
677 // does not give us an accurate enough value. For 11025 and 22050
678 // the equation gives us the best answer. All other frequencies will
679 // also use the equation. JDW
682 else if (rate == 8000)
684 else if (rate == 48000)
687 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
691 /*---------------------------------------------------------------------------
692 snd_trident_convert_adc_rate
694 Description: This routine converts rate in HZ to hardware delta value.
696 Paramters: trident - pointer to target device class for 4DWave.
697 rate - Real or Virtual channel number.
699 Returns: Delta value.
701 ---------------------------------------------------------------------------*/
702 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
706 // We special case 44100 and 8000 since rounding with the equation
707 // does not give us an accurate enough value. For 11025 and 22050
708 // the equation gives us the best answer. All other frequencies will
709 // also use the equation. JDW
712 else if (rate == 8000)
714 else if (rate == 48000)
717 delta = ((48000 << 12) / rate) & 0x0000ffff;
721 /*---------------------------------------------------------------------------
722 snd_trident_spurious_threshold
724 Description: This routine converts rate in HZ to spurious threshold.
726 Paramters: trident - pointer to target device class for 4DWave.
727 rate - Real or Virtual channel number.
729 Returns: Delta value.
731 ---------------------------------------------------------------------------*/
732 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
733 unsigned int period_size)
735 unsigned int res = (rate * period_size) / 48000;
743 /*---------------------------------------------------------------------------
744 snd_trident_control_mode
746 Description: This routine returns a control mode for a PCM channel.
748 Paramters: trident - pointer to target device class for 4DWave.
749 substream - PCM substream
751 Returns: Control value.
753 ---------------------------------------------------------------------------*/
754 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
757 struct snd_pcm_runtime *runtime = substream->runtime;
760 CTRL default: 8-bit (unsigned) mono, loop mode enabled
763 if (snd_pcm_format_width(runtime->format) == 16)
764 CTRL |= 0x00000008; // 16-bit data
765 if (snd_pcm_format_signed(runtime->format))
766 CTRL |= 0x00000002; // signed data
767 if (runtime->channels > 1)
768 CTRL |= 0x00000004; // stereo data
776 /*---------------------------------------------------------------------------
779 Description: Device I/O control handler for playback/capture parameters.
781 Paramters: substream - PCM substream class
782 cmd - what ioctl message to process
783 arg - additional message infoarg
785 Returns: Error status
787 ---------------------------------------------------------------------------*/
789 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
793 /* FIXME: it seems that with small periods the behaviour of
794 trident hardware is unpredictable and interrupt generator
796 return snd_pcm_lib_ioctl(substream, cmd, arg);
799 /*---------------------------------------------------------------------------
800 snd_trident_allocate_pcm_mem
802 Description: Allocate PCM ring buffer for given substream
804 Parameters: substream - PCM substream class
805 hw_params - hardware parameters
807 Returns: Error status
809 ---------------------------------------------------------------------------*/
811 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
812 struct snd_pcm_hw_params *hw_params)
814 struct snd_trident *trident = snd_pcm_substream_chip(substream);
815 struct snd_pcm_runtime *runtime = substream->runtime;
816 struct snd_trident_voice *voice = runtime->private_data;
819 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
821 if (trident->tlb.entries) {
822 if (err > 0) { /* change */
824 snd_trident_free_pages(trident, voice->memblk);
825 voice->memblk = snd_trident_alloc_pages(trident, substream);
826 if (voice->memblk == NULL)
833 /*---------------------------------------------------------------------------
834 snd_trident_allocate_evoice
836 Description: Allocate extra voice as interrupt generator
838 Parameters: substream - PCM substream class
839 hw_params - hardware parameters
841 Returns: Error status
843 ---------------------------------------------------------------------------*/
845 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
846 struct snd_pcm_hw_params *hw_params)
848 struct snd_trident *trident = snd_pcm_substream_chip(substream);
849 struct snd_pcm_runtime *runtime = substream->runtime;
850 struct snd_trident_voice *voice = runtime->private_data;
851 struct snd_trident_voice *evoice = voice->extra;
853 /* voice management */
855 if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
856 if (evoice == NULL) {
857 evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
860 voice->extra = evoice;
861 evoice->substream = substream;
864 if (evoice != NULL) {
865 snd_trident_free_voice(trident, evoice);
866 voice->extra = evoice = NULL;
873 /*---------------------------------------------------------------------------
874 snd_trident_hw_params
876 Description: Set the hardware parameters for the playback device.
878 Parameters: substream - PCM substream class
879 hw_params - hardware parameters
881 Returns: Error status
883 ---------------------------------------------------------------------------*/
885 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
886 struct snd_pcm_hw_params *hw_params)
890 err = snd_trident_allocate_pcm_mem(substream, hw_params);
892 err = snd_trident_allocate_evoice(substream, hw_params);
896 /*---------------------------------------------------------------------------
897 snd_trident_playback_hw_free
899 Description: Release the hardware resources for the playback device.
901 Parameters: substream - PCM substream class
903 Returns: Error status
905 ---------------------------------------------------------------------------*/
907 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
909 struct snd_trident *trident = snd_pcm_substream_chip(substream);
910 struct snd_pcm_runtime *runtime = substream->runtime;
911 struct snd_trident_voice *voice = runtime->private_data;
912 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
914 if (trident->tlb.entries) {
915 if (voice && voice->memblk) {
916 snd_trident_free_pages(trident, voice->memblk);
917 voice->memblk = NULL;
920 snd_pcm_lib_free_pages(substream);
921 if (evoice != NULL) {
922 snd_trident_free_voice(trident, evoice);
928 /*---------------------------------------------------------------------------
929 snd_trident_playback_prepare
931 Description: Prepare playback device for playback.
933 Parameters: substream - PCM substream class
935 Returns: Error status
937 ---------------------------------------------------------------------------*/
939 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
941 struct snd_trident *trident = snd_pcm_substream_chip(substream);
942 struct snd_pcm_runtime *runtime = substream->runtime;
943 struct snd_trident_voice *voice = runtime->private_data;
944 struct snd_trident_voice *evoice = voice->extra;
945 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
947 spin_lock_irq(&trident->reg_lock);
949 /* set delta (rate) value */
950 voice->Delta = snd_trident_convert_rate(runtime->rate);
951 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
953 /* set Loop Begin Address */
955 voice->LBA = voice->memblk->offset;
957 voice->LBA = runtime->dma_addr;
960 voice->ESO = runtime->buffer_size - 1; /* in samples */
961 voice->CTRL = snd_trident_control_mode(substream);
967 voice->Vol = mix->vol;
968 voice->RVol = mix->rvol;
969 voice->CVol = mix->cvol;
970 voice->Pan = mix->pan;
971 voice->Attribute = 0;
973 voice->Attribute = (1<<(30-16))|(2<<(26-16))|
974 (0<<(24-16))|(0x1f<<(19-16));
976 voice->Attribute = 0;
979 snd_trident_write_voice_regs(trident, voice);
981 if (evoice != NULL) {
982 evoice->Delta = voice->Delta;
983 evoice->spurious_threshold = voice->spurious_threshold;
984 evoice->LBA = voice->LBA;
986 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
987 evoice->CTRL = voice->CTRL;
989 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
993 evoice->Vol = 0x3ff; /* mute */
994 evoice->RVol = evoice->CVol = 0x7f; /* mute */
995 evoice->Pan = 0x7f; /* mute */
997 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
998 (0<<(24-16))|(0x1f<<(19-16));
1000 evoice->Attribute = 0;
1002 snd_trident_write_voice_regs(trident, evoice);
1004 evoice->isync_mark = runtime->period_size;
1005 evoice->ESO = (runtime->period_size * 2) - 1;
1008 spin_unlock_irq(&trident->reg_lock);
1013 /*---------------------------------------------------------------------------
1014 snd_trident_capture_hw_params
1016 Description: Set the hardware parameters for the capture device.
1018 Parameters: substream - PCM substream class
1019 hw_params - hardware parameters
1021 Returns: Error status
1023 ---------------------------------------------------------------------------*/
1025 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1026 struct snd_pcm_hw_params *hw_params)
1028 return snd_trident_allocate_pcm_mem(substream, hw_params);
1031 /*---------------------------------------------------------------------------
1032 snd_trident_capture_prepare
1034 Description: Prepare capture device for playback.
1036 Parameters: substream - PCM substream class
1038 Returns: Error status
1040 ---------------------------------------------------------------------------*/
1042 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1044 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1045 struct snd_pcm_runtime *runtime = substream->runtime;
1046 struct snd_trident_voice *voice = runtime->private_data;
1047 unsigned int val, ESO_bytes;
1049 spin_lock_irq(&trident->reg_lock);
1051 // Initilize the channel and set channel Mode
1052 outb(0, TRID_REG(trident, LEGACY_DMAR15));
1054 // Set DMA channel operation mode register
1055 outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1057 // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
1058 voice->LBA = runtime->dma_addr;
1059 outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
1061 voice->LBA = voice->memblk->offset;
1064 ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
1065 outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
1066 outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
1069 // Set channel sample rate, 4.12 format
1070 val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
1071 outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
1073 // Set channel interrupt blk length
1074 if (snd_pcm_format_width(runtime->format) == 16) {
1075 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1077 val = (unsigned short) (ESO_bytes - 1);
1080 outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1082 // Right now, set format and start to run captureing,
1083 // continuous run loop enable.
1084 trident->bDMAStart = 0x19; // 0001 1001b
1086 if (snd_pcm_format_width(runtime->format) == 16)
1087 trident->bDMAStart |= 0x80;
1088 if (snd_pcm_format_signed(runtime->format))
1089 trident->bDMAStart |= 0x20;
1090 if (runtime->channels > 1)
1091 trident->bDMAStart |= 0x40;
1093 // Prepare capture intr channel
1095 voice->Delta = snd_trident_convert_rate(runtime->rate);
1096 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1098 voice->isync_mark = runtime->period_size;
1099 voice->isync_max = runtime->buffer_size;
1101 // Set voice parameters
1103 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1104 voice->CTRL = snd_trident_control_mode(substream);
1109 voice->Pan = 0x7f; /* mute */
1110 voice->Vol = 0x3ff; /* mute */
1114 voice->Attribute = 0;
1116 snd_trident_write_voice_regs(trident, voice);
1118 spin_unlock_irq(&trident->reg_lock);
1122 /*---------------------------------------------------------------------------
1123 snd_trident_si7018_capture_hw_params
1125 Description: Set the hardware parameters for the capture device.
1127 Parameters: substream - PCM substream class
1128 hw_params - hardware parameters
1130 Returns: Error status
1132 ---------------------------------------------------------------------------*/
1134 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1135 struct snd_pcm_hw_params *hw_params)
1139 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1142 return snd_trident_allocate_evoice(substream, hw_params);
1145 /*---------------------------------------------------------------------------
1146 snd_trident_si7018_capture_hw_free
1148 Description: Release the hardware resources for the capture device.
1150 Parameters: substream - PCM substream class
1152 Returns: Error status
1154 ---------------------------------------------------------------------------*/
1156 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1158 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1159 struct snd_pcm_runtime *runtime = substream->runtime;
1160 struct snd_trident_voice *voice = runtime->private_data;
1161 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1163 snd_pcm_lib_free_pages(substream);
1164 if (evoice != NULL) {
1165 snd_trident_free_voice(trident, evoice);
1166 voice->extra = NULL;
1171 /*---------------------------------------------------------------------------
1172 snd_trident_si7018_capture_prepare
1174 Description: Prepare capture device for playback.
1176 Parameters: substream - PCM substream class
1178 Returns: Error status
1180 ---------------------------------------------------------------------------*/
1182 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1184 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1185 struct snd_pcm_runtime *runtime = substream->runtime;
1186 struct snd_trident_voice *voice = runtime->private_data;
1187 struct snd_trident_voice *evoice = voice->extra;
1189 spin_lock_irq(&trident->reg_lock);
1191 voice->LBA = runtime->dma_addr;
1192 voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
1193 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1195 // Set voice parameters
1197 voice->ESO = runtime->buffer_size - 1; /* in samples */
1198 voice->CTRL = snd_trident_control_mode(substream);
1203 voice->Pan = T4D_DEFAULT_PCM_PAN;
1209 voice->Attribute = (2 << (30-16)) |
1214 snd_trident_write_voice_regs(trident, voice);
1216 if (evoice != NULL) {
1217 evoice->Delta = snd_trident_convert_rate(runtime->rate);
1218 evoice->spurious_threshold = voice->spurious_threshold;
1219 evoice->LBA = voice->LBA;
1221 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1222 evoice->CTRL = voice->CTRL;
1228 evoice->Vol = 0x3ff; /* mute */
1229 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1230 evoice->Pan = 0x7f; /* mute */
1231 evoice->Attribute = 0;
1232 snd_trident_write_voice_regs(trident, evoice);
1234 evoice->isync_mark = runtime->period_size;
1235 evoice->ESO = (runtime->period_size * 2) - 1;
1238 spin_unlock_irq(&trident->reg_lock);
1242 /*---------------------------------------------------------------------------
1243 snd_trident_foldback_prepare
1245 Description: Prepare foldback capture device for playback.
1247 Parameters: substream - PCM substream class
1249 Returns: Error status
1251 ---------------------------------------------------------------------------*/
1253 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1255 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1256 struct snd_pcm_runtime *runtime = substream->runtime;
1257 struct snd_trident_voice *voice = runtime->private_data;
1258 struct snd_trident_voice *evoice = voice->extra;
1260 spin_lock_irq(&trident->reg_lock);
1262 /* Set channel buffer Address */
1264 voice->LBA = voice->memblk->offset;
1266 voice->LBA = runtime->dma_addr;
1268 /* set target ESO for channel */
1269 voice->ESO = runtime->buffer_size - 1; /* in samples */
1271 /* set sample rate */
1272 voice->Delta = 0x1000;
1273 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1276 voice->CTRL = snd_trident_control_mode(substream);
1281 voice->Pan = 0x7f; /* mute */
1282 voice->Vol = 0x3ff; /* mute */
1286 voice->Attribute = 0;
1288 /* set up capture channel */
1289 outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1291 snd_trident_write_voice_regs(trident, voice);
1293 if (evoice != NULL) {
1294 evoice->Delta = voice->Delta;
1295 evoice->spurious_threshold = voice->spurious_threshold;
1296 evoice->LBA = voice->LBA;
1298 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1299 evoice->CTRL = voice->CTRL;
1301 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1305 evoice->Vol = 0x3ff; /* mute */
1306 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1307 evoice->Pan = 0x7f; /* mute */
1308 evoice->Attribute = 0;
1309 snd_trident_write_voice_regs(trident, evoice);
1311 evoice->isync_mark = runtime->period_size;
1312 evoice->ESO = (runtime->period_size * 2) - 1;
1315 spin_unlock_irq(&trident->reg_lock);
1319 /*---------------------------------------------------------------------------
1320 snd_trident_spdif_hw_params
1322 Description: Set the hardware parameters for the spdif device.
1324 Parameters: substream - PCM substream class
1325 hw_params - hardware parameters
1327 Returns: Error status
1329 ---------------------------------------------------------------------------*/
1331 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1332 struct snd_pcm_hw_params *hw_params)
1334 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1335 unsigned int old_bits = 0, change = 0;
1338 err = snd_trident_allocate_pcm_mem(substream, hw_params);
1342 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1343 err = snd_trident_allocate_evoice(substream, hw_params);
1348 /* prepare SPDIF channel */
1349 spin_lock_irq(&trident->reg_lock);
1350 old_bits = trident->spdif_pcm_bits;
1351 if (old_bits & IEC958_AES0_PROFESSIONAL)
1352 trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
1354 trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
1355 if (params_rate(hw_params) >= 48000) {
1356 trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
1357 trident->spdif_pcm_bits |=
1358 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1359 IEC958_AES0_PRO_FS_48000 :
1360 (IEC958_AES3_CON_FS_48000 << 24);
1362 else if (params_rate(hw_params) >= 44100) {
1363 trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
1364 trident->spdif_pcm_bits |=
1365 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1366 IEC958_AES0_PRO_FS_44100 :
1367 (IEC958_AES3_CON_FS_44100 << 24);
1370 trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
1371 trident->spdif_pcm_bits |=
1372 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1373 IEC958_AES0_PRO_FS_32000 :
1374 (IEC958_AES3_CON_FS_32000 << 24);
1376 change = old_bits != trident->spdif_pcm_bits;
1377 spin_unlock_irq(&trident->reg_lock);
1380 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1385 /*---------------------------------------------------------------------------
1386 snd_trident_spdif_prepare
1388 Description: Prepare SPDIF device for playback.
1390 Parameters: substream - PCM substream class
1392 Returns: Error status
1394 ---------------------------------------------------------------------------*/
1396 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1398 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1399 struct snd_pcm_runtime *runtime = substream->runtime;
1400 struct snd_trident_voice *voice = runtime->private_data;
1401 struct snd_trident_voice *evoice = voice->extra;
1402 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1403 unsigned int RESO, LBAO;
1406 spin_lock_irq(&trident->reg_lock);
1408 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1410 /* set delta (rate) value */
1411 voice->Delta = snd_trident_convert_rate(runtime->rate);
1412 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1414 /* set Loop Back Address */
1415 LBAO = runtime->dma_addr;
1417 voice->LBA = voice->memblk->offset;
1423 voice->isync_mark = runtime->period_size;
1424 voice->isync_max = runtime->buffer_size;
1426 /* set target ESO for channel */
1427 RESO = runtime->buffer_size - 1;
1428 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1431 voice->CTRL = snd_trident_control_mode(substream);
1443 voice->Attribute = 0;
1445 /* prepare surrogate IRQ channel */
1446 snd_trident_write_voice_regs(trident, voice);
1448 outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
1449 outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
1450 outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
1451 outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
1452 outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
1454 /* set SPDIF setting */
1455 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1456 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1460 /* set delta (rate) value */
1461 voice->Delta = 0x800;
1462 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1464 /* set Loop Begin Address */
1466 voice->LBA = voice->memblk->offset;
1468 voice->LBA = runtime->dma_addr;
1471 voice->ESO = runtime->buffer_size - 1; /* in samples */
1472 voice->CTRL = snd_trident_control_mode(substream);
1478 voice->Vol = mix->vol;
1479 voice->RVol = mix->rvol;
1480 voice->CVol = mix->cvol;
1481 voice->Pan = mix->pan;
1482 voice->Attribute = (1<<(30-16))|(7<<(26-16))|
1483 (0<<(24-16))|(0<<(19-16));
1485 snd_trident_write_voice_regs(trident, voice);
1487 if (evoice != NULL) {
1488 evoice->Delta = voice->Delta;
1489 evoice->spurious_threshold = voice->spurious_threshold;
1490 evoice->LBA = voice->LBA;
1492 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1493 evoice->CTRL = voice->CTRL;
1495 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1499 evoice->Vol = 0x3ff; /* mute */
1500 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1501 evoice->Pan = 0x7f; /* mute */
1502 evoice->Attribute = 0;
1503 snd_trident_write_voice_regs(trident, evoice);
1505 evoice->isync_mark = runtime->period_size;
1506 evoice->ESO = (runtime->period_size * 2) - 1;
1509 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1510 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1512 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1513 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1515 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1518 spin_unlock_irq(&trident->reg_lock);
1523 /*---------------------------------------------------------------------------
1526 Description: Start/stop devices
1528 Parameters: substream - PCM substream class
1529 cmd - trigger command (STOP, GO)
1531 Returns: Error status
1533 ---------------------------------------------------------------------------*/
1535 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1539 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1540 struct list_head *pos;
1541 struct snd_pcm_substream *s;
1542 unsigned int what, whati, capture_flag, spdif_flag;
1543 struct snd_trident_voice *voice, *evoice;
1544 unsigned int val, go;
1547 case SNDRV_PCM_TRIGGER_START:
1548 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1549 case SNDRV_PCM_TRIGGER_RESUME:
1552 case SNDRV_PCM_TRIGGER_STOP:
1553 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1554 case SNDRV_PCM_TRIGGER_SUSPEND:
1560 what = whati = capture_flag = spdif_flag = 0;
1561 spin_lock(&trident->reg_lock);
1562 val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
1563 snd_pcm_group_for_each(pos, substream) {
1564 s = snd_pcm_group_substream_entry(pos);
1565 if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
1566 voice = s->runtime->private_data;
1567 evoice = voice->extra;
1568 what |= 1 << (voice->number & 0x1f);
1569 if (evoice == NULL) {
1570 whati |= 1 << (voice->number & 0x1f);
1572 what |= 1 << (evoice->number & 0x1f);
1573 whati |= 1 << (evoice->number & 0x1f);
1575 evoice->stimer = val;
1579 voice->stimer = val;
1583 snd_pcm_trigger_done(s, substream);
1591 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1592 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1593 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1595 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1596 val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
1597 outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1601 outl(what, TRID_REG(trident, T4D_STOP_B));
1602 val = inl(TRID_REG(trident, T4D_AINTEN_B));
1608 outl(val, TRID_REG(trident, T4D_AINTEN_B));
1610 outl(what, TRID_REG(trident, T4D_START_B));
1612 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1613 outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1615 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1616 outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1618 spin_unlock(&trident->reg_lock);
1622 /*---------------------------------------------------------------------------
1623 snd_trident_playback_pointer
1625 Description: This routine return the playback position
1627 Parameters: substream - PCM substream class
1629 Returns: position of buffer
1631 ---------------------------------------------------------------------------*/
1633 static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1635 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1636 struct snd_pcm_runtime *runtime = substream->runtime;
1637 struct snd_trident_voice *voice = runtime->private_data;
1640 if (!voice->running)
1643 spin_lock(&trident->reg_lock);
1645 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
1647 if (trident->device != TRIDENT_DEVICE_ID_NX) {
1648 cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
1649 } else { // ID_4DWAVE_NX
1650 cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
1653 spin_unlock(&trident->reg_lock);
1655 if (cso >= runtime->buffer_size)
1661 /*---------------------------------------------------------------------------
1662 snd_trident_capture_pointer
1664 Description: This routine return the capture position
1666 Paramters: pcm1 - PCM device class
1668 Returns: position of buffer
1670 ---------------------------------------------------------------------------*/
1672 static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1674 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1675 struct snd_pcm_runtime *runtime = substream->runtime;
1676 struct snd_trident_voice *voice = runtime->private_data;
1677 unsigned int result;
1679 if (!voice->running)
1682 result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
1683 if (runtime->channels > 1)
1686 result = runtime->buffer_size - result;
1691 /*---------------------------------------------------------------------------
1692 snd_trident_spdif_pointer
1694 Description: This routine return the SPDIF playback position
1696 Parameters: substream - PCM substream class
1698 Returns: position of buffer
1700 ---------------------------------------------------------------------------*/
1702 static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1704 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1705 struct snd_pcm_runtime *runtime = substream->runtime;
1706 struct snd_trident_voice *voice = runtime->private_data;
1707 unsigned int result;
1709 if (!voice->running)
1712 result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
1718 * Playback support device description
1721 static struct snd_pcm_hardware snd_trident_playback =
1723 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1724 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1725 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1726 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1727 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1728 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1729 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1734 .buffer_bytes_max = (256*1024),
1735 .period_bytes_min = 64,
1736 .period_bytes_max = (256*1024),
1738 .periods_max = 1024,
1743 * Capture support device description
1746 static struct snd_pcm_hardware snd_trident_capture =
1748 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1749 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1750 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1751 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1752 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1753 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1754 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1759 .buffer_bytes_max = (128*1024),
1760 .period_bytes_min = 64,
1761 .period_bytes_max = (128*1024),
1763 .periods_max = 1024,
1768 * Foldback capture support device description
1771 static struct snd_pcm_hardware snd_trident_foldback =
1773 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1774 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1775 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1776 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1777 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1778 .rates = SNDRV_PCM_RATE_48000,
1783 .buffer_bytes_max = (128*1024),
1784 .period_bytes_min = 64,
1785 .period_bytes_max = (128*1024),
1787 .periods_max = 1024,
1792 * SPDIF playback support device description
1795 static struct snd_pcm_hardware snd_trident_spdif =
1797 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1798 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1799 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1800 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1801 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1802 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1803 SNDRV_PCM_RATE_48000),
1808 .buffer_bytes_max = (128*1024),
1809 .period_bytes_min = 64,
1810 .period_bytes_max = (128*1024),
1812 .periods_max = 1024,
1816 static struct snd_pcm_hardware snd_trident_spdif_7018 =
1818 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1819 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1820 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1821 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1822 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1823 .rates = SNDRV_PCM_RATE_48000,
1828 .buffer_bytes_max = (128*1024),
1829 .period_bytes_min = 64,
1830 .period_bytes_max = (128*1024),
1832 .periods_max = 1024,
1836 static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1838 struct snd_trident_voice *voice = runtime->private_data;
1839 struct snd_trident *trident;
1842 trident = voice->trident;
1843 snd_trident_free_voice(trident, voice);
1847 static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1849 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1850 struct snd_pcm_runtime *runtime = substream->runtime;
1851 struct snd_trident_voice *voice;
1853 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1856 snd_trident_pcm_mixer_build(trident, voice, substream);
1857 voice->substream = substream;
1858 runtime->private_data = voice;
1859 runtime->private_free = snd_trident_pcm_free_substream;
1860 runtime->hw = snd_trident_playback;
1861 snd_pcm_set_sync(substream);
1862 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1866 /*---------------------------------------------------------------------------
1867 snd_trident_playback_close
1869 Description: This routine will close the 4DWave playback device. For now
1870 we will simply free the dma transfer buffer.
1872 Parameters: substream - PCM substream class
1874 ---------------------------------------------------------------------------*/
1875 static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1877 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1878 struct snd_pcm_runtime *runtime = substream->runtime;
1879 struct snd_trident_voice *voice = runtime->private_data;
1881 snd_trident_pcm_mixer_free(trident, voice, substream);
1885 /*---------------------------------------------------------------------------
1886 snd_trident_spdif_open
1888 Description: This routine will open the 4DWave SPDIF device.
1890 Parameters: substream - PCM substream class
1892 Returns: status - success or failure flag
1894 ---------------------------------------------------------------------------*/
1896 static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1898 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1899 struct snd_trident_voice *voice;
1900 struct snd_pcm_runtime *runtime = substream->runtime;
1902 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1906 voice->substream = substream;
1907 spin_lock_irq(&trident->reg_lock);
1908 trident->spdif_pcm_bits = trident->spdif_bits;
1909 spin_unlock_irq(&trident->reg_lock);
1911 runtime->private_data = voice;
1912 runtime->private_free = snd_trident_pcm_free_substream;
1913 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1914 runtime->hw = snd_trident_spdif;
1916 runtime->hw = snd_trident_spdif_7018;
1919 trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1920 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1921 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1923 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1928 /*---------------------------------------------------------------------------
1929 snd_trident_spdif_close
1931 Description: This routine will close the 4DWave SPDIF device.
1933 Parameters: substream - PCM substream class
1935 ---------------------------------------------------------------------------*/
1937 static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1939 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1942 spin_lock_irq(&trident->reg_lock);
1943 // restore default SPDIF setting
1944 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1945 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1946 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
1948 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
1949 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1950 if (trident->spdif_ctrl) {
1955 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1957 spin_unlock_irq(&trident->reg_lock);
1958 trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1959 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1960 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1964 /*---------------------------------------------------------------------------
1965 snd_trident_capture_open
1967 Description: This routine will open the 4DWave capture device.
1969 Parameters: substream - PCM substream class
1971 Returns: status - success or failure flag
1973 ---------------------------------------------------------------------------*/
1975 static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1977 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1978 struct snd_trident_voice *voice;
1979 struct snd_pcm_runtime *runtime = substream->runtime;
1981 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1985 voice->substream = substream;
1986 runtime->private_data = voice;
1987 runtime->private_free = snd_trident_pcm_free_substream;
1988 runtime->hw = snd_trident_capture;
1989 snd_pcm_set_sync(substream);
1990 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1994 /*---------------------------------------------------------------------------
1995 snd_trident_capture_close
1997 Description: This routine will close the 4DWave capture device. For now
1998 we will simply free the dma transfer buffer.
2000 Parameters: substream - PCM substream class
2002 ---------------------------------------------------------------------------*/
2003 static int snd_trident_capture_close(struct snd_pcm_substream *substream)
2008 /*---------------------------------------------------------------------------
2009 snd_trident_foldback_open
2011 Description: This routine will open the 4DWave foldback capture device.
2013 Parameters: substream - PCM substream class
2015 Returns: status - success or failure flag
2017 ---------------------------------------------------------------------------*/
2019 static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
2021 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2022 struct snd_trident_voice *voice;
2023 struct snd_pcm_runtime *runtime = substream->runtime;
2025 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
2028 voice->foldback_chan = substream->number;
2029 voice->substream = substream;
2030 runtime->private_data = voice;
2031 runtime->private_free = snd_trident_pcm_free_substream;
2032 runtime->hw = snd_trident_foldback;
2033 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
2037 /*---------------------------------------------------------------------------
2038 snd_trident_foldback_close
2040 Description: This routine will close the 4DWave foldback capture device.
2041 For now we will simply free the dma transfer buffer.
2043 Parameters: substream - PCM substream class
2045 ---------------------------------------------------------------------------*/
2046 static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
2048 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2049 struct snd_trident_voice *voice;
2050 struct snd_pcm_runtime *runtime = substream->runtime;
2051 voice = runtime->private_data;
2053 /* stop capture channel */
2054 spin_lock_irq(&trident->reg_lock);
2055 outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
2056 spin_unlock_irq(&trident->reg_lock);
2060 /*---------------------------------------------------------------------------
2062 ---------------------------------------------------------------------------*/
2064 static struct snd_pcm_ops snd_trident_playback_ops = {
2065 .open = snd_trident_playback_open,
2066 .close = snd_trident_playback_close,
2067 .ioctl = snd_trident_ioctl,
2068 .hw_params = snd_trident_hw_params,
2069 .hw_free = snd_trident_hw_free,
2070 .prepare = snd_trident_playback_prepare,
2071 .trigger = snd_trident_trigger,
2072 .pointer = snd_trident_playback_pointer,
2075 static struct snd_pcm_ops snd_trident_nx_playback_ops = {
2076 .open = snd_trident_playback_open,
2077 .close = snd_trident_playback_close,
2078 .ioctl = snd_trident_ioctl,
2079 .hw_params = snd_trident_hw_params,
2080 .hw_free = snd_trident_hw_free,
2081 .prepare = snd_trident_playback_prepare,
2082 .trigger = snd_trident_trigger,
2083 .pointer = snd_trident_playback_pointer,
2084 .page = snd_pcm_sgbuf_ops_page,
2087 static struct snd_pcm_ops snd_trident_capture_ops = {
2088 .open = snd_trident_capture_open,
2089 .close = snd_trident_capture_close,
2090 .ioctl = snd_trident_ioctl,
2091 .hw_params = snd_trident_capture_hw_params,
2092 .hw_free = snd_trident_hw_free,
2093 .prepare = snd_trident_capture_prepare,
2094 .trigger = snd_trident_trigger,
2095 .pointer = snd_trident_capture_pointer,
2098 static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
2099 .open = snd_trident_capture_open,
2100 .close = snd_trident_capture_close,
2101 .ioctl = snd_trident_ioctl,
2102 .hw_params = snd_trident_si7018_capture_hw_params,
2103 .hw_free = snd_trident_si7018_capture_hw_free,
2104 .prepare = snd_trident_si7018_capture_prepare,
2105 .trigger = snd_trident_trigger,
2106 .pointer = snd_trident_playback_pointer,
2109 static struct snd_pcm_ops snd_trident_foldback_ops = {
2110 .open = snd_trident_foldback_open,
2111 .close = snd_trident_foldback_close,
2112 .ioctl = snd_trident_ioctl,
2113 .hw_params = snd_trident_hw_params,
2114 .hw_free = snd_trident_hw_free,
2115 .prepare = snd_trident_foldback_prepare,
2116 .trigger = snd_trident_trigger,
2117 .pointer = snd_trident_playback_pointer,
2120 static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
2121 .open = snd_trident_foldback_open,
2122 .close = snd_trident_foldback_close,
2123 .ioctl = snd_trident_ioctl,
2124 .hw_params = snd_trident_hw_params,
2125 .hw_free = snd_trident_hw_free,
2126 .prepare = snd_trident_foldback_prepare,
2127 .trigger = snd_trident_trigger,
2128 .pointer = snd_trident_playback_pointer,
2129 .page = snd_pcm_sgbuf_ops_page,
2132 static struct snd_pcm_ops snd_trident_spdif_ops = {
2133 .open = snd_trident_spdif_open,
2134 .close = snd_trident_spdif_close,
2135 .ioctl = snd_trident_ioctl,
2136 .hw_params = snd_trident_spdif_hw_params,
2137 .hw_free = snd_trident_hw_free,
2138 .prepare = snd_trident_spdif_prepare,
2139 .trigger = snd_trident_trigger,
2140 .pointer = snd_trident_spdif_pointer,
2143 static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
2144 .open = snd_trident_spdif_open,
2145 .close = snd_trident_spdif_close,
2146 .ioctl = snd_trident_ioctl,
2147 .hw_params = snd_trident_spdif_hw_params,
2148 .hw_free = snd_trident_hw_free,
2149 .prepare = snd_trident_spdif_prepare,
2150 .trigger = snd_trident_trigger,
2151 .pointer = snd_trident_playback_pointer,
2154 /*---------------------------------------------------------------------------
2157 Description: This routine registers the 4DWave device for PCM support.
2159 Paramters: trident - pointer to target device class for 4DWave.
2163 ---------------------------------------------------------------------------*/
2165 int __devinit snd_trident_pcm(struct snd_trident * trident,
2166 int device, struct snd_pcm ** rpcm)
2168 struct snd_pcm *pcm;
2173 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
2176 pcm->private_data = trident;
2178 if (trident->tlb.entries) {
2179 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
2181 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
2183 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2184 trident->device != TRIDENT_DEVICE_ID_SI7018 ?
2185 &snd_trident_capture_ops :
2186 &snd_trident_si7018_capture_ops);
2188 pcm->info_flags = 0;
2189 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
2190 strcpy(pcm->name, "Trident 4DWave");
2193 if (trident->tlb.entries) {
2194 struct snd_pcm_substream *substream;
2195 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
2196 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
2197 snd_dma_pci_data(trident->pci),
2199 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2200 SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
2203 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2204 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2212 /*---------------------------------------------------------------------------
2213 snd_trident_foldback_pcm
2215 Description: This routine registers the 4DWave device for foldback PCM support.
2217 Paramters: trident - pointer to target device class for 4DWave.
2221 ---------------------------------------------------------------------------*/
2223 int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
2224 int device, struct snd_pcm ** rpcm)
2226 struct snd_pcm *foldback;
2229 struct snd_pcm_substream *substream;
2233 if (trident->device == TRIDENT_DEVICE_ID_NX)
2235 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
2238 foldback->private_data = trident;
2239 if (trident->tlb.entries)
2240 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
2242 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
2243 foldback->info_flags = 0;
2244 strcpy(foldback->name, "Trident 4DWave");
2245 substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2246 strcpy(substream->name, "Front Mixer");
2247 substream = substream->next;
2248 strcpy(substream->name, "Reverb Mixer");
2249 substream = substream->next;
2250 strcpy(substream->name, "Chorus Mixer");
2251 if (num_chan == 4) {
2252 substream = substream->next;
2253 strcpy(substream->name, "Second AC'97 ADC");
2255 trident->foldback = foldback;
2257 if (trident->tlb.entries)
2258 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
2259 snd_dma_pci_data(trident->pci), 0, 128*1024);
2261 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
2262 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2269 /*---------------------------------------------------------------------------
2272 Description: This routine registers the 4DWave-NX device for SPDIF support.
2274 Paramters: trident - pointer to target device class for 4DWave-NX.
2278 ---------------------------------------------------------------------------*/
2280 int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
2281 int device, struct snd_pcm ** rpcm)
2283 struct snd_pcm *spdif;
2288 if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
2291 spdif->private_data = trident;
2292 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2293 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
2295 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
2297 spdif->info_flags = 0;
2298 strcpy(spdif->name, "Trident 4DWave IEC958");
2299 trident->spdif = spdif;
2301 snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2313 /*---------------------------------------------------------------------------
2314 snd_trident_spdif_control
2316 Description: enable/disable S/PDIF out from ac97 mixer
2317 ---------------------------------------------------------------------------*/
2319 static int snd_trident_spdif_control_info(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_info *uinfo)
2322 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2324 uinfo->value.integer.min = 0;
2325 uinfo->value.integer.max = 1;
2329 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_value *ucontrol)
2332 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2335 spin_lock_irq(&trident->reg_lock);
2336 val = trident->spdif_ctrl;
2337 ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2338 spin_unlock_irq(&trident->reg_lock);
2342 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_value *ucontrol)
2345 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2349 val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2350 spin_lock_irq(&trident->reg_lock);
2351 /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2352 change = trident->spdif_ctrl != val;
2353 trident->spdif_ctrl = val;
2354 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2355 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2356 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2357 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2360 if (trident->spdif == NULL) {
2362 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2363 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2366 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2369 spin_unlock_irq(&trident->reg_lock);
2373 static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
2375 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2376 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2377 .info = snd_trident_spdif_control_info,
2378 .get = snd_trident_spdif_control_get,
2379 .put = snd_trident_spdif_control_put,
2380 .private_value = 0x28,
2383 /*---------------------------------------------------------------------------
2384 snd_trident_spdif_default
2386 Description: put/get the S/PDIF default settings
2387 ---------------------------------------------------------------------------*/
2389 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2390 struct snd_ctl_elem_info *uinfo)
2392 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2397 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2398 struct snd_ctl_elem_value *ucontrol)
2400 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2402 spin_lock_irq(&trident->reg_lock);
2403 ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2404 ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2405 ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2406 ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2407 spin_unlock_irq(&trident->reg_lock);
2411 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2412 struct snd_ctl_elem_value *ucontrol)
2414 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2418 val = (ucontrol->value.iec958.status[0] << 0) |
2419 (ucontrol->value.iec958.status[1] << 8) |
2420 (ucontrol->value.iec958.status[2] << 16) |
2421 (ucontrol->value.iec958.status[3] << 24);
2422 spin_lock_irq(&trident->reg_lock);
2423 change = trident->spdif_bits != val;
2424 trident->spdif_bits = val;
2425 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2426 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2427 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2429 if (trident->spdif == NULL)
2430 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2432 spin_unlock_irq(&trident->reg_lock);
2436 static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
2438 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2439 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2440 .info = snd_trident_spdif_default_info,
2441 .get = snd_trident_spdif_default_get,
2442 .put = snd_trident_spdif_default_put
2445 /*---------------------------------------------------------------------------
2446 snd_trident_spdif_mask
2448 Description: put/get the S/PDIF mask
2449 ---------------------------------------------------------------------------*/
2451 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_info *uinfo)
2454 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2459 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
2462 ucontrol->value.iec958.status[0] = 0xff;
2463 ucontrol->value.iec958.status[1] = 0xff;
2464 ucontrol->value.iec958.status[2] = 0xff;
2465 ucontrol->value.iec958.status[3] = 0xff;
2469 static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
2471 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2472 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2473 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2474 .info = snd_trident_spdif_mask_info,
2475 .get = snd_trident_spdif_mask_get,
2478 /*---------------------------------------------------------------------------
2479 snd_trident_spdif_stream
2481 Description: put/get the S/PDIF stream settings
2482 ---------------------------------------------------------------------------*/
2484 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_info *uinfo)
2487 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2492 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2493 struct snd_ctl_elem_value *ucontrol)
2495 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2497 spin_lock_irq(&trident->reg_lock);
2498 ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2499 ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2500 ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2501 ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2502 spin_unlock_irq(&trident->reg_lock);
2506 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2507 struct snd_ctl_elem_value *ucontrol)
2509 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2513 val = (ucontrol->value.iec958.status[0] << 0) |
2514 (ucontrol->value.iec958.status[1] << 8) |
2515 (ucontrol->value.iec958.status[2] << 16) |
2516 (ucontrol->value.iec958.status[3] << 24);
2517 spin_lock_irq(&trident->reg_lock);
2518 change = trident->spdif_pcm_bits != val;
2519 trident->spdif_pcm_bits = val;
2520 if (trident->spdif != NULL) {
2521 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2522 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2524 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2527 spin_unlock_irq(&trident->reg_lock);
2531 static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
2533 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2534 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2535 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2536 .info = snd_trident_spdif_stream_info,
2537 .get = snd_trident_spdif_stream_get,
2538 .put = snd_trident_spdif_stream_put
2541 /*---------------------------------------------------------------------------
2542 snd_trident_ac97_control
2544 Description: enable/disable rear path for ac97
2545 ---------------------------------------------------------------------------*/
2547 static int snd_trident_ac97_control_info(struct snd_kcontrol *kcontrol,
2548 struct snd_ctl_elem_info *uinfo)
2550 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2552 uinfo->value.integer.min = 0;
2553 uinfo->value.integer.max = 1;
2557 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2560 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2563 spin_lock_irq(&trident->reg_lock);
2564 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2565 ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2566 spin_unlock_irq(&trident->reg_lock);
2570 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2571 struct snd_ctl_elem_value *ucontrol)
2573 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2577 spin_lock_irq(&trident->reg_lock);
2578 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2579 val &= ~(1 << kcontrol->private_value);
2580 if (ucontrol->value.integer.value[0])
2581 val |= 1 << kcontrol->private_value;
2582 change = val != trident->ac97_ctrl;
2583 trident->ac97_ctrl = val;
2584 outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2585 spin_unlock_irq(&trident->reg_lock);
2589 static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
2591 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2592 .name = "Rear Path",
2593 .info = snd_trident_ac97_control_info,
2594 .get = snd_trident_ac97_control_get,
2595 .put = snd_trident_ac97_control_put,
2599 /*---------------------------------------------------------------------------
2600 snd_trident_vol_control
2602 Description: wave & music volume control
2603 ---------------------------------------------------------------------------*/
2605 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2608 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2610 uinfo->value.integer.min = 0;
2611 uinfo->value.integer.max = 255;
2615 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2616 struct snd_ctl_elem_value *ucontrol)
2618 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2621 val = trident->musicvol_wavevol;
2622 ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2623 ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2627 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2628 struct snd_ctl_elem_value *ucontrol)
2630 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2634 spin_lock_irq(&trident->reg_lock);
2635 val = trident->musicvol_wavevol;
2636 val &= ~(0xffff << kcontrol->private_value);
2637 val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2638 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2639 change = val != trident->musicvol_wavevol;
2640 outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2641 spin_unlock_irq(&trident->reg_lock);
2645 static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
2647 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2648 .name = "Music Playback Volume",
2649 .info = snd_trident_vol_control_info,
2650 .get = snd_trident_vol_control_get,
2651 .put = snd_trident_vol_control_put,
2652 .private_value = 16,
2655 static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
2657 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2658 .name = "Wave Playback Volume",
2659 .info = snd_trident_vol_control_info,
2660 .get = snd_trident_vol_control_get,
2661 .put = snd_trident_vol_control_put,
2665 /*---------------------------------------------------------------------------
2666 snd_trident_pcm_vol_control
2668 Description: PCM front volume control
2669 ---------------------------------------------------------------------------*/
2671 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_info *uinfo)
2674 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2676 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2678 uinfo->value.integer.min = 0;
2679 uinfo->value.integer.max = 255;
2680 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2681 uinfo->value.integer.max = 1023;
2685 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
2688 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2689 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2691 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2692 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2694 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2699 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2702 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2703 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2707 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2708 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2710 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2712 spin_lock_irq(&trident->reg_lock);
2713 change = val != mix->vol;
2715 if (mix->voice != NULL)
2716 snd_trident_write_vol_reg(trident, mix->voice, val);
2717 spin_unlock_irq(&trident->reg_lock);
2721 static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
2723 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2724 .name = "PCM Front Playback Volume",
2725 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2727 .info = snd_trident_pcm_vol_control_info,
2728 .get = snd_trident_pcm_vol_control_get,
2729 .put = snd_trident_pcm_vol_control_put,
2732 /*---------------------------------------------------------------------------
2733 snd_trident_pcm_pan_control
2735 Description: PCM front pan control
2736 ---------------------------------------------------------------------------*/
2738 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_info *uinfo)
2741 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2743 uinfo->value.integer.min = 0;
2744 uinfo->value.integer.max = 127;
2748 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_value *ucontrol)
2751 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2752 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2754 ucontrol->value.integer.value[0] = mix->pan;
2755 if (ucontrol->value.integer.value[0] & 0x40) {
2756 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2758 ucontrol->value.integer.value[0] |= 0x40;
2763 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2764 struct snd_ctl_elem_value *ucontrol)
2766 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2767 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2771 if (ucontrol->value.integer.value[0] & 0x40)
2772 val = ucontrol->value.integer.value[0] & 0x3f;
2774 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2775 spin_lock_irq(&trident->reg_lock);
2776 change = val != mix->pan;
2778 if (mix->voice != NULL)
2779 snd_trident_write_pan_reg(trident, mix->voice, val);
2780 spin_unlock_irq(&trident->reg_lock);
2784 static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
2786 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2787 .name = "PCM Pan Playback Control",
2788 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2790 .info = snd_trident_pcm_pan_control_info,
2791 .get = snd_trident_pcm_pan_control_get,
2792 .put = snd_trident_pcm_pan_control_put,
2795 /*---------------------------------------------------------------------------
2796 snd_trident_pcm_rvol_control
2798 Description: PCM reverb volume control
2799 ---------------------------------------------------------------------------*/
2801 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_info *uinfo)
2804 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2806 uinfo->value.integer.min = 0;
2807 uinfo->value.integer.max = 127;
2811 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2812 struct snd_ctl_elem_value *ucontrol)
2814 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2815 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2817 ucontrol->value.integer.value[0] = 127 - mix->rvol;
2821 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_value *ucontrol)
2824 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2825 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2829 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2830 spin_lock_irq(&trident->reg_lock);
2831 change = val != mix->rvol;
2833 if (mix->voice != NULL)
2834 snd_trident_write_rvol_reg(trident, mix->voice, val);
2835 spin_unlock_irq(&trident->reg_lock);
2839 static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
2841 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2842 .name = "PCM Reverb Playback Volume",
2843 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2845 .info = snd_trident_pcm_rvol_control_info,
2846 .get = snd_trident_pcm_rvol_control_get,
2847 .put = snd_trident_pcm_rvol_control_put,
2850 /*---------------------------------------------------------------------------
2851 snd_trident_pcm_cvol_control
2853 Description: PCM chorus volume control
2854 ---------------------------------------------------------------------------*/
2856 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_info *uinfo)
2859 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2861 uinfo->value.integer.min = 0;
2862 uinfo->value.integer.max = 127;
2866 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
2869 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2870 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2872 ucontrol->value.integer.value[0] = 127 - mix->cvol;
2876 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol)
2879 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2880 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2884 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2885 spin_lock_irq(&trident->reg_lock);
2886 change = val != mix->cvol;
2888 if (mix->voice != NULL)
2889 snd_trident_write_cvol_reg(trident, mix->voice, val);
2890 spin_unlock_irq(&trident->reg_lock);
2894 static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
2896 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2897 .name = "PCM Chorus Playback Volume",
2898 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2900 .info = snd_trident_pcm_cvol_control_info,
2901 .get = snd_trident_pcm_cvol_control_get,
2902 .put = snd_trident_pcm_cvol_control_put,
2905 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2906 struct snd_kcontrol *kctl,
2907 int num, int activate)
2909 struct snd_ctl_elem_id id;
2914 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2916 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2917 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2918 SNDRV_CTL_EVENT_MASK_INFO,
2919 snd_ctl_build_ioff(&id, kctl, num));
2922 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2923 struct snd_trident_pcm_mixer *tmix,
2924 int num, int activate)
2926 snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2927 snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2928 snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2929 snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2932 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2933 struct snd_trident_voice *voice,
2934 struct snd_pcm_substream *substream)
2936 struct snd_trident_pcm_mixer *tmix;
2938 snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
2939 tmix = &trident->pcm_mixer[substream->number];
2940 tmix->voice = voice;
2941 tmix->vol = T4D_DEFAULT_PCM_VOL;
2942 tmix->pan = T4D_DEFAULT_PCM_PAN;
2943 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2944 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2945 snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2949 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2951 struct snd_trident_pcm_mixer *tmix;
2953 snd_assert(trident != NULL && substream != NULL, return -EINVAL);
2954 tmix = &trident->pcm_mixer[substream->number];
2956 snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2960 /*---------------------------------------------------------------------------
2963 Description: This routine registers the 4DWave device for mixer support.
2965 Paramters: trident - pointer to target device class for 4DWave.
2969 ---------------------------------------------------------------------------*/
2971 static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
2973 struct snd_ac97_template _ac97;
2974 struct snd_card *card = trident->card;
2975 struct snd_kcontrol *kctl;
2976 struct snd_ctl_elem_value *uctl;
2977 int idx, err, retries = 2;
2978 static struct snd_ac97_bus_ops ops = {
2979 .write = snd_trident_codec_write,
2980 .read = snd_trident_codec_read,
2983 uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
2987 if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
2990 memset(&_ac97, 0, sizeof(_ac97));
2991 _ac97.private_data = trident;
2992 trident->ac97_detect = 1;
2995 if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
2996 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2997 if ((err = snd_trident_sis_reset(trident)) < 0)
3006 /* secondary codec? */
3007 if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
3008 (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
3010 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
3012 snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
3013 #if 0 // only for my testing purpose --jk
3015 struct snd_ac97 *mc97;
3016 err = snd_ac97_modem(trident->card, &_ac97, &mc97);
3018 snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
3023 trident->ac97_detect = 0;
3025 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3026 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3028 kctl->put(kctl, uctl);
3029 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3031 kctl->put(kctl, uctl);
3032 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3034 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3037 for (idx = 0; idx < 32; idx++) {
3038 struct snd_trident_pcm_mixer *tmix;
3040 tmix = &trident->pcm_mixer[idx];
3043 if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3045 if ((err = snd_ctl_add(card, trident->ctl_vol)))
3048 if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3050 if ((err = snd_ctl_add(card, trident->ctl_pan)))
3053 if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3055 if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3058 if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3060 if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3063 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3064 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3066 kctl->put(kctl, uctl);
3068 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3070 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3075 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3077 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3079 idx = kctl->id.index;
3080 if ((err = snd_ctl_add(card, kctl)) < 0)
3082 kctl->put(kctl, uctl);
3084 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3089 kctl->id.index = idx;
3090 kctl->id.device = pcm_spdif_device;
3091 if ((err = snd_ctl_add(card, kctl)) < 0)
3094 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3099 kctl->id.index = idx;
3100 kctl->id.device = pcm_spdif_device;
3101 if ((err = snd_ctl_add(card, kctl)) < 0)
3104 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3109 kctl->id.index = idx;
3110 kctl->id.device = pcm_spdif_device;
3111 if ((err = snd_ctl_add(card, kctl)) < 0)
3113 trident->spdif_pcm_ctl = kctl;
3129 * gameport interface
3132 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
3134 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3136 struct snd_trident *chip = gameport_get_port_data(gameport);
3138 snd_assert(chip, return 0);
3139 return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3142 static void snd_trident_gameport_trigger(struct gameport *gameport)
3144 struct snd_trident *chip = gameport_get_port_data(gameport);
3146 snd_assert(chip, return);
3147 outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3150 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3152 struct snd_trident *chip = gameport_get_port_data(gameport);
3155 snd_assert(chip, return 0);
3157 *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3159 for (i = 0; i < 4; i++) {
3160 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3161 if (axes[i] == 0xffff) axes[i] = -1;
3167 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3169 struct snd_trident *chip = gameport_get_port_data(gameport);
3171 snd_assert(chip, return 0);
3174 case GAMEPORT_MODE_COOKED:
3175 outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3178 case GAMEPORT_MODE_RAW:
3179 outb(0, TRID_REG(chip, GAMEPORT_GCR));
3186 int __devinit snd_trident_create_gameport(struct snd_trident *chip)
3188 struct gameport *gp;
3190 chip->gameport = gp = gameport_allocate_port();
3192 printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
3196 gameport_set_name(gp, "Trident 4DWave");
3197 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3198 gameport_set_dev_parent(gp, &chip->pci->dev);
3200 gameport_set_port_data(gp, chip);
3202 gp->read = snd_trident_gameport_read;
3203 gp->trigger = snd_trident_gameport_trigger;
3204 gp->cooked_read = snd_trident_gameport_cooked_read;
3205 gp->open = snd_trident_gameport_open;
3207 gameport_register_port(gp);
3212 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3214 if (chip->gameport) {
3215 gameport_unregister_port(chip->gameport);
3216 chip->gameport = NULL;
3220 int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3221 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3222 #endif /* CONFIG_GAMEPORT */
3227 static inline void do_delay(struct snd_trident *chip)
3229 schedule_timeout_uninterruptible(1);
3236 static int snd_trident_sis_reset(struct snd_trident *trident)
3238 unsigned long end_time;
3242 r = trident->in_suspend ? 0 : 2; /* count of retries */
3244 pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
3246 pci_write_config_byte(trident->pci, 0x46, 0x00);
3248 /* disable AC97 GPIO interrupt */
3249 outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3250 /* initialize serial interface, force cold reset */
3251 i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3252 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3254 /* remove cold reset */
3256 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3258 /* wait, until the codec is ready */
3259 end_time = (jiffies + (HZ * 3) / 4) + 1;
3261 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3264 } while (time_after_eq(end_time, jiffies));
3265 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3267 end_time = jiffies + HZ;
3270 } while (time_after_eq(end_time, jiffies));
3271 goto __si7018_retry;
3274 /* wait for the second codec */
3276 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3279 } while (time_after_eq(end_time, jiffies));
3280 /* enable 64 channel mode */
3281 outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3289 static void snd_trident_proc_read(struct snd_info_entry *entry,
3290 struct snd_info_buffer *buffer)
3292 struct snd_trident *trident = entry->private_data;
3295 switch (trident->device) {
3296 case TRIDENT_DEVICE_ID_SI7018:
3297 s = "SiS 7018 Audio";
3299 case TRIDENT_DEVICE_ID_DX:
3300 s = "Trident 4DWave PCI DX";
3302 case TRIDENT_DEVICE_ID_NX:
3303 s = "Trident 4DWave PCI NX";
3308 snd_iprintf(buffer, "%s\n\n", s);
3309 snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
3310 snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3311 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3312 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3313 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3314 snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3315 if (trident->tlb.entries) {
3316 snd_iprintf(buffer,"\nVirtual Memory\n");
3317 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3318 snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
3319 snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3322 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3323 snd_iprintf(buffer,"\nWavetable Synth\n");
3324 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
3325 snd_iprintf(buffer, "Memory Used : %d\n", trident->synth.current_size);
3326 snd_iprintf(buffer, "Memory Free : %d\n", (trident->synth.max_size-trident->synth.current_size));
3330 static void __devinit snd_trident_proc_init(struct snd_trident * trident)
3332 struct snd_info_entry *entry;
3333 const char *s = "trident";
3335 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3337 if (! snd_card_proc_new(trident->card, s, &entry))
3338 snd_info_set_text_ops(entry, trident, 1024, snd_trident_proc_read);
3341 static int snd_trident_dev_free(struct snd_device *device)
3343 struct snd_trident *trident = device->device_data;
3344 return snd_trident_free(trident);
3347 /*---------------------------------------------------------------------------
3348 snd_trident_tlb_alloc
3350 Description: Allocate and set up the TLB page table on 4D NX.
3351 Each entry has 4 bytes (physical PCI address).
3353 Paramters: trident - pointer to target device class for 4DWave.
3355 Returns: 0 or negative error code
3357 ---------------------------------------------------------------------------*/
3359 static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
3363 /* TLB array must be aligned to 16kB !!! so we allocate
3364 32kB region and correct offset when necessary */
3366 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3367 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3368 snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
3371 trident->tlb.entries = (unsigned int*)(((unsigned long)trident->tlb.buffer.area + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1));
3372 trident->tlb.entries_dmaaddr = (trident->tlb.buffer.addr + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1);
3373 /* allocate shadow TLB page table (virtual addresses) */
3374 trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
3375 if (trident->tlb.shadow_entries == NULL) {
3376 snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
3379 /* allocate and setup silent page and initialise TLB entries */
3380 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3381 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3382 snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
3385 memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3386 for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3387 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3388 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3391 /* use emu memory block manager code to manage tlb page allocation */
3392 trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3393 if (trident->tlb.memhdr == NULL)
3396 trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3401 * initialize 4D DX chip
3404 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3406 outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3407 outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3408 outl(0, TRID_REG(trident, T4D_AINTEN_A));
3409 outl(0, TRID_REG(trident, T4D_AINTEN_B));
3412 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3414 struct pci_dev *pci = trident->pci;
3415 unsigned long end_time;
3417 /* reset the legacy configuration and whole audio/wavetable block */
3418 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3419 pci_write_config_byte(pci, 0x44, 0); /* ports */
3420 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3421 pci_write_config_byte(pci, 0x46, 4); /* reset */
3423 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3426 /* warm reset of the AC'97 codec */
3427 outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3429 outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3430 /* DAC on, disable SB IRQ and try to force ADC valid signal */
3431 trident->ac97_ctrl = 0x0000004a;
3432 outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3433 /* wait, until the codec is ready */
3434 end_time = (jiffies + (HZ * 3) / 4) + 1;
3436 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3439 } while (time_after_eq(end_time, jiffies));
3440 snd_printk(KERN_ERR "AC'97 codec ready error\n");
3444 snd_trident_stop_all_voices(trident);
3450 * initialize 4D NX chip
3452 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3454 struct pci_dev *pci = trident->pci;
3455 unsigned long end_time;
3457 /* reset the legacy configuration and whole audio/wavetable block */
3458 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3459 pci_write_config_byte(pci, 0x44, 0); /* ports */
3460 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3462 pci_write_config_byte(pci, 0x46, 1); /* reset */
3464 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3467 /* warm reset of the AC'97 codec */
3468 outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3470 outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3471 /* wait, until the codec is ready */
3472 end_time = (jiffies + (HZ * 3) / 4) + 1;
3474 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3477 } while (time_after_eq(end_time, jiffies));
3478 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3483 trident->ac97_ctrl = 0x00000002;
3484 outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3485 /* disable SB IRQ */
3486 outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3488 snd_trident_stop_all_voices(trident);
3490 if (trident->tlb.entries != NULL) {
3492 /* enable virtual addressing via TLB */
3493 i = trident->tlb.entries_dmaaddr;
3495 outl(i, TRID_REG(trident, NX_TLBC));
3497 outl(0, TRID_REG(trident, NX_TLBC));
3499 /* initialize S/PDIF */
3500 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3501 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3507 * initialize sis7018 chip
3509 static int snd_trident_sis_init(struct snd_trident *trident)
3513 if ((err = snd_trident_sis_reset(trident)) < 0)
3516 snd_trident_stop_all_voices(trident);
3518 /* initialize S/PDIF */
3519 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3524 /*---------------------------------------------------------------------------
3527 Description: This routine will create the device specific class for
3528 the 4DWave card. It will also perform basic initialization.
3530 Paramters: card - which card to create
3531 pci - interface to PCI bus resource info
3532 dma1ptr - playback dma buffer
3533 dma2ptr - capture dma buffer
3534 irqptr - interrupt resource info
3536 Returns: 4DWave device class private data
3538 ---------------------------------------------------------------------------*/
3540 int __devinit snd_trident_create(struct snd_card *card,
3541 struct pci_dev *pci,
3543 int pcm_spdif_device,
3544 int max_wavetable_size,
3545 struct snd_trident ** rtrident)
3547 struct snd_trident *trident;
3549 struct snd_trident_voice *voice;
3550 struct snd_trident_pcm_mixer *tmix;
3551 static struct snd_device_ops ops = {
3552 .dev_free = snd_trident_dev_free,
3557 /* enable PCI device */
3558 if ((err = pci_enable_device(pci)) < 0)
3560 /* check, if we can restrict PCI DMA transfers to 30 bits */
3561 if (pci_set_dma_mask(pci, 0x3fffffff) < 0 ||
3562 pci_set_consistent_dma_mask(pci, 0x3fffffff) < 0) {
3563 snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
3564 pci_disable_device(pci);
3568 trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3569 if (trident == NULL) {
3570 pci_disable_device(pci);
3573 trident->device = (pci->vendor << 16) | pci->device;
3574 trident->card = card;
3576 spin_lock_init(&trident->reg_lock);
3577 spin_lock_init(&trident->event_lock);
3578 spin_lock_init(&trident->voice_alloc);
3579 if (pcm_streams < 1)
3581 if (pcm_streams > 32)
3583 trident->ChanPCM = pcm_streams;
3584 if (max_wavetable_size < 0 )
3585 max_wavetable_size = 0;
3586 trident->synth.max_size = max_wavetable_size * 1024;
3589 trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3590 pci_set_master(pci);
3592 if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3594 pci_disable_device(pci);
3597 trident->port = pci_resource_start(pci, 0);
3599 if (request_irq(pci->irq, snd_trident_interrupt, SA_INTERRUPT|SA_SHIRQ,
3600 "Trident Audio", trident)) {
3601 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
3602 snd_trident_free(trident);
3605 trident->irq = pci->irq;
3607 /* allocate 16k-aligned TLB for NX cards */
3608 trident->tlb.entries = NULL;
3609 trident->tlb.buffer.area = NULL;
3610 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3611 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3612 snd_trident_free(trident);
3617 trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3619 /* initialize chip */
3620 switch (trident->device) {
3621 case TRIDENT_DEVICE_ID_DX:
3622 err = snd_trident_4d_dx_init(trident);
3624 case TRIDENT_DEVICE_ID_NX:
3625 err = snd_trident_4d_nx_init(trident);
3627 case TRIDENT_DEVICE_ID_SI7018:
3628 err = snd_trident_sis_init(trident);
3635 snd_trident_free(trident);
3639 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3640 snd_trident_free(trident);
3644 if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3647 /* initialise synth voices */
3648 for (i = 0; i < 64; i++) {
3649 voice = &trident->synth.voices[i];
3651 voice->trident = trident;
3653 /* initialize pcm mixer entries */
3654 for (i = 0; i < 32; i++) {
3655 tmix = &trident->pcm_mixer[i];
3656 tmix->vol = T4D_DEFAULT_PCM_VOL;
3657 tmix->pan = T4D_DEFAULT_PCM_PAN;
3658 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3659 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3662 snd_trident_enable_eso(trident);
3665 snd_card_set_pm_callback(card, snd_trident_suspend, snd_trident_resume, trident);
3666 snd_trident_proc_init(trident);
3667 snd_card_set_dev(card, &pci->dev);
3668 *rtrident = trident;
3672 /*---------------------------------------------------------------------------
3675 Description: This routine will free the device specific class for
3678 Paramters: trident - device specific private data for 4DWave card
3682 ---------------------------------------------------------------------------*/
3684 static int snd_trident_free(struct snd_trident *trident)
3686 snd_trident_free_gameport(trident);
3687 snd_trident_disable_eso(trident);
3688 // Disable S/PDIF out
3689 if (trident->device == TRIDENT_DEVICE_ID_NX)
3690 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3691 else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3692 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3694 if (trident->tlb.buffer.area) {
3695 outl(0, TRID_REG(trident, NX_TLBC));
3696 if (trident->tlb.memhdr)
3697 snd_util_memhdr_free(trident->tlb.memhdr);
3698 if (trident->tlb.silent_page.area)
3699 snd_dma_free_pages(&trident->tlb.silent_page);
3700 vfree(trident->tlb.shadow_entries);
3701 snd_dma_free_pages(&trident->tlb.buffer);
3703 if (trident->irq >= 0)
3704 free_irq(trident->irq, trident);
3705 pci_release_regions(trident->pci);
3706 pci_disable_device(trident->pci);
3711 /*---------------------------------------------------------------------------
3712 snd_trident_interrupt
3714 Description: ISR for Trident 4DWave device
3716 Paramters: trident - device specific private data for 4DWave card
3718 Problems: It seems that Trident chips generates interrupts more than
3719 one time in special cases. The spurious interrupts are
3720 detected via sample timer (T4D_STIMER) and computing
3721 corresponding delta value. The limits are detected with
3722 the method try & fail so it is possible that it won't
3723 work on all computers. [jaroslav]
3727 ---------------------------------------------------------------------------*/
3729 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3731 struct snd_trident *trident = dev_id;
3732 unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3734 struct snd_trident_voice *voice;
3736 audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3737 if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3739 if (audio_int & ADDRESS_IRQ) {
3740 // get interrupt status for all channels
3741 spin_lock(&trident->reg_lock);
3742 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3743 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3746 outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
3748 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3751 for (channel = 63; channel >= 32; channel--) {
3752 mask = 1 << (channel&0x1f);
3753 if ((chn_int & mask) == 0)
3755 voice = &trident->synth.voices[channel];
3756 if (!voice->pcm || voice->substream == NULL) {
3757 outl(mask, TRID_REG(trident, T4D_STOP_B));
3760 delta = (int)stimer - (int)voice->stimer;
3763 if ((unsigned int)delta < voice->spurious_threshold) {
3764 /* do some statistics here */
3765 trident->spurious_irq_count++;
3766 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3767 trident->spurious_irq_max_delta = delta;
3770 voice->stimer = stimer;
3772 if (!voice->isync3) {
3773 tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3774 if (trident->bDMAStart & 0x40)
3777 tmp = voice->isync_max - tmp;
3779 tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3781 if (tmp < voice->isync_mark) {
3783 tmp = voice->isync_ESO - 7;
3785 tmp = voice->isync_ESO + 2;
3786 /* update ESO for IRQ voice to preserve sync */
3787 snd_trident_stop_voice(trident, voice->number);
3788 snd_trident_write_eso_reg(trident, voice, tmp);
3789 snd_trident_start_voice(trident, voice->number);
3791 } else if (voice->isync2) {
3793 /* write original ESO and update CSO for IRQ voice to preserve sync */
3794 snd_trident_stop_voice(trident, voice->number);
3795 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3796 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3797 snd_trident_start_voice(trident, voice->number);
3801 /* update CSO for extra voice to preserve sync */
3802 snd_trident_stop_voice(trident, voice->extra->number);
3803 snd_trident_write_cso_reg(trident, voice->extra, 0);
3804 snd_trident_start_voice(trident, voice->extra->number);
3807 spin_unlock(&trident->reg_lock);
3808 snd_pcm_period_elapsed(voice->substream);
3809 spin_lock(&trident->reg_lock);
3811 outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
3813 spin_unlock(&trident->reg_lock);
3815 if (audio_int & MPU401_IRQ) {
3816 if (trident->rmidi) {
3817 snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data, regs);
3819 inb(TRID_REG(trident, T4D_MPUR0));
3822 // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3826 /*---------------------------------------------------------------------------
3827 snd_trident_attach_synthesizer
3829 Description: Attach synthesizer hooks
3831 Paramters: trident - device specific private data for 4DWave card
3835 ---------------------------------------------------------------------------*/
3836 int snd_trident_attach_synthesizer(struct snd_trident *trident)
3838 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3839 if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
3840 sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
3841 strcpy(trident->seq_dev->name, "4DWave");
3842 *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
3848 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3850 struct snd_trident_voice *pvoice;
3851 unsigned long flags;
3854 spin_lock_irqsave(&trident->voice_alloc, flags);
3855 if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3856 idx = snd_trident_allocate_pcm_channel(trident);
3858 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3861 pvoice = &trident->synth.voices[idx];
3864 pvoice->capture = 0;
3866 pvoice->memblk = NULL;
3867 pvoice->substream = NULL;
3868 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3871 if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3872 idx = snd_trident_allocate_synth_channel(trident);
3874 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3877 pvoice = &trident->synth.voices[idx];
3880 pvoice->client = client;
3881 pvoice->port = port;
3882 pvoice->memblk = NULL;
3883 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3886 if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3888 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3892 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3894 unsigned long flags;
3895 void (*private_free)(struct snd_trident_voice *);
3898 if (voice == NULL || !voice->use)
3900 snd_trident_clear_voices(trident, voice->number, voice->number);
3901 spin_lock_irqsave(&trident->voice_alloc, flags);
3902 private_free = voice->private_free;
3903 private_data = voice->private_data;
3904 voice->private_free = NULL;
3905 voice->private_data = NULL;
3907 snd_trident_free_pcm_channel(trident, voice->number);
3909 snd_trident_free_synth_channel(trident, voice->number);
3910 voice->use = voice->pcm = voice->synth = voice->midi = 0;
3911 voice->capture = voice->spdif = 0;
3912 voice->sample_ops = NULL;
3913 voice->substream = NULL;
3914 voice->extra = NULL;
3915 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3917 private_free(voice);
3920 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3922 unsigned int i, val, mask[2] = { 0, 0 };
3924 snd_assert(v_min <= 63, return);
3925 snd_assert(v_max <= 63, return);
3926 for (i = v_min; i <= v_max; i++)
3927 mask[i >> 5] |= 1 << (i & 0x1f);
3929 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3930 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3931 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3934 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3935 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3936 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3941 static int snd_trident_suspend(struct snd_card *card, pm_message_t state)
3943 struct snd_trident *trident = card->pm_private_data;
3945 trident->in_suspend = 1;
3946 snd_pcm_suspend_all(trident->pcm);
3947 if (trident->foldback)
3948 snd_pcm_suspend_all(trident->foldback);
3950 snd_pcm_suspend_all(trident->spdif);
3952 snd_ac97_suspend(trident->ac97);
3953 if (trident->ac97_sec)
3954 snd_ac97_suspend(trident->ac97_sec);
3956 switch (trident->device) {
3957 case TRIDENT_DEVICE_ID_DX:
3958 case TRIDENT_DEVICE_ID_NX:
3960 case TRIDENT_DEVICE_ID_SI7018:
3963 pci_disable_device(trident->pci);
3967 static int snd_trident_resume(struct snd_card *card)
3969 struct snd_trident *trident = card->pm_private_data;
3971 pci_enable_device(trident->pci);
3972 if (pci_set_dma_mask(trident->pci, 0x3fffffff) < 0 ||
3973 pci_set_consistent_dma_mask(trident->pci, 0x3fffffff) < 0)
3974 snd_printk(KERN_WARNING "trident: can't set the proper DMA mask\n");
3975 pci_set_master(trident->pci); /* to be sure */
3977 switch (trident->device) {
3978 case TRIDENT_DEVICE_ID_DX:
3979 snd_trident_4d_dx_init(trident);
3981 case TRIDENT_DEVICE_ID_NX:
3982 snd_trident_4d_nx_init(trident);
3984 case TRIDENT_DEVICE_ID_SI7018:
3985 snd_trident_sis_init(trident);
3989 snd_ac97_resume(trident->ac97);
3990 if (trident->ac97_sec)
3991 snd_ac97_resume(trident->ac97_sec);
3993 /* restore some registers */
3994 outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3996 snd_trident_enable_eso(trident);
3998 trident->in_suspend = 0;
4001 #endif /* CONFIG_PM */
4003 EXPORT_SYMBOL(snd_trident_alloc_voice);
4004 EXPORT_SYMBOL(snd_trident_free_voice);
4005 EXPORT_SYMBOL(snd_trident_start_voice);
4006 EXPORT_SYMBOL(snd_trident_stop_voice);
4007 EXPORT_SYMBOL(snd_trident_write_voice_regs);
4008 /* trident_memory.c symbols */
4009 EXPORT_SYMBOL(snd_trident_synth_alloc);
4010 EXPORT_SYMBOL(snd_trident_synth_free);
4011 EXPORT_SYMBOL(snd_trident_synth_copy_from_user);