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);
55 static int snd_trident_sis_reset(struct snd_trident *trident);
57 static void snd_trident_clear_voices(struct snd_trident * trident,
58 unsigned short v_min, unsigned short v_max);
59 static int snd_trident_free(struct snd_trident *trident);
67 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
69 unsigned int val, tmp;
71 printk("Trident voice %i:\n", voice);
72 outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
73 val = inl(TRID_REG(trident, CH_LBA));
74 printk("LBA: 0x%x\n", val);
75 val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
76 printk("GVSel: %i\n", val >> 31);
77 printk("Pan: 0x%x\n", (val >> 24) & 0x7f);
78 printk("Vol: 0x%x\n", (val >> 16) & 0xff);
79 printk("CTRL: 0x%x\n", (val >> 12) & 0x0f);
80 printk("EC: 0x%x\n", val & 0x0fff);
81 if (trident->device != TRIDENT_DEVICE_ID_NX) {
82 val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
83 printk("CSO: 0x%x\n", val >> 16);
84 printk("Alpha: 0x%x\n", (val >> 4) & 0x0fff);
85 printk("FMS: 0x%x\n", val & 0x0f);
86 val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
87 printk("ESO: 0x%x\n", val >> 16);
88 printk("Delta: 0x%x\n", val & 0xffff);
89 val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
90 } else { // TRIDENT_DEVICE_ID_NX
91 val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
92 tmp = (val >> 24) & 0xff;
93 printk("CSO: 0x%x\n", val & 0x00ffffff);
94 val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
95 tmp |= (val >> 16) & 0xff00;
96 printk("Delta: 0x%x\n", tmp);
97 printk("ESO: 0x%x\n", val & 0x00ffffff);
98 val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
99 printk("Alpha: 0x%x\n", val >> 20);
100 printk("FMS: 0x%x\n", (val >> 16) & 0x0f);
102 printk("FMC: 0x%x\n", (val >> 14) & 3);
103 printk("RVol: 0x%x\n", (val >> 7) & 0x7f);
104 printk("CVol: 0x%x\n", val & 0x7f);
108 /*---------------------------------------------------------------------------
109 unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
111 Description: This routine will do all of the reading from the external
114 Parameters: ac97 - ac97 codec structure
115 reg - CODEC register index, from AC97 Hal.
117 returns: 16 bit value read from the AC97.
119 ---------------------------------------------------------------------------*/
120 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
122 unsigned int data = 0, treg;
123 unsigned short count = 0xffff;
125 struct snd_trident *trident = ac97->private_data;
127 spin_lock_irqsave(&trident->reg_lock, flags);
128 if (trident->device == TRIDENT_DEVICE_ID_DX) {
129 data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
130 outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
132 data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
133 if ((data & DX_AC97_BUSY_READ) == 0)
136 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
137 data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
138 treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
139 outl(data, TRID_REG(trident, treg));
141 data = inl(TRID_REG(trident, treg));
142 if ((data & 0x00000C00) == 0)
145 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
146 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
148 data |= SI_AC97_SECONDARY;
149 outl(data, TRID_REG(trident, SI_AC97_READ));
151 data = inl(TRID_REG(trident, SI_AC97_READ));
152 if ((data & (SI_AC97_BUSY_READ)) == 0)
157 if (count == 0 && !trident->ac97_detect) {
158 snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
163 spin_unlock_irqrestore(&trident->reg_lock, flags);
164 return ((unsigned short) (data >> 16));
167 /*---------------------------------------------------------------------------
168 void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
169 unsigned short wdata)
171 Description: This routine will do all of the writing to the external
174 Parameters: ac97 - ac97 codec structure
175 reg - CODEC register index, from AC97 Hal.
176 data - Lower 16 bits are the data to write to CODEC.
178 returns: TRUE if everything went ok, else FALSE.
180 ---------------------------------------------------------------------------*/
181 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
182 unsigned short wdata)
184 unsigned int address, data;
185 unsigned short count = 0xffff;
187 struct snd_trident *trident = ac97->private_data;
189 data = ((unsigned long) wdata) << 16;
191 spin_lock_irqsave(&trident->reg_lock, flags);
192 if (trident->device == TRIDENT_DEVICE_ID_DX) {
193 address = DX_ACR0_AC97_W;
195 /* read AC-97 write register status */
197 if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
201 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
202 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
203 address = NX_ACR1_AC97_W;
205 /* read AC-97 write register status */
207 if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
211 data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
212 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
213 address = SI_AC97_WRITE;
215 /* read AC-97 write register status */
217 if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
221 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
223 data |= SI_AC97_SECONDARY;
225 address = 0; /* keep GCC happy */
226 count = 0; /* return */
230 spin_unlock_irqrestore(&trident->reg_lock, flags);
233 outl(data, TRID_REG(trident, address));
234 spin_unlock_irqrestore(&trident->reg_lock, flags);
237 /*---------------------------------------------------------------------------
238 void snd_trident_enable_eso(struct snd_trident *trident)
240 Description: This routine will enable end of loop interrupts.
241 End of loop interrupts will occur when a running
243 Also enables middle of loop interrupts.
245 Parameters: trident - pointer to target device class for 4DWave.
247 ---------------------------------------------------------------------------*/
249 static void snd_trident_enable_eso(struct snd_trident * trident)
253 val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
256 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
258 outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
261 /*---------------------------------------------------------------------------
262 void snd_trident_disable_eso(struct snd_trident *trident)
264 Description: This routine will disable end of loop interrupts.
265 End of loop interrupts will occur when a running
267 Also disables middle of loop interrupts.
270 trident - pointer to target device class for 4DWave.
272 returns: TRUE if everything went ok, else FALSE.
274 ---------------------------------------------------------------------------*/
276 static void snd_trident_disable_eso(struct snd_trident * trident)
280 tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
283 outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
286 /*---------------------------------------------------------------------------
287 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
289 Description: Start a voice, any channel 0 thru 63.
290 This routine automatically handles the fact that there are
291 more than 32 channels available.
293 Parameters : voice - Voice number 0 thru n.
294 trident - pointer to target device class for 4DWave.
298 ---------------------------------------------------------------------------*/
300 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
302 unsigned int mask = 1 << (voice & 0x1f);
303 unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
305 outl(mask, TRID_REG(trident, reg));
308 /*---------------------------------------------------------------------------
309 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
311 Description: Stop a voice, any channel 0 thru 63.
312 This routine automatically handles the fact that there are
313 more than 32 channels available.
315 Parameters : voice - Voice number 0 thru n.
316 trident - pointer to target device class for 4DWave.
320 ---------------------------------------------------------------------------*/
322 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
324 unsigned int mask = 1 << (voice & 0x1f);
325 unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
327 outl(mask, TRID_REG(trident, reg));
330 /*---------------------------------------------------------------------------
331 int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
333 Description: Allocate hardware channel in Bank B (32-63).
335 Parameters : trident - pointer to target device class for 4DWave.
337 Return Value: hardware channel - 32-63 or -1 when no channel is available
339 ---------------------------------------------------------------------------*/
341 static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
345 if (trident->ChanPCMcnt >= trident->ChanPCM)
347 for (idx = 31; idx >= 0; idx--) {
348 if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
349 trident->ChanMap[T4D_BANK_B] |= 1 << idx;
350 trident->ChanPCMcnt++;
357 /*---------------------------------------------------------------------------
358 void snd_trident_free_pcm_channel(int channel)
360 Description: Free hardware channel in Bank B (32-63)
362 Parameters : trident - pointer to target device class for 4DWave.
363 channel - hardware channel number 0-63
367 ---------------------------------------------------------------------------*/
369 static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
371 if (channel < 32 || channel > 63)
374 if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
375 trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
376 trident->ChanPCMcnt--;
380 /*---------------------------------------------------------------------------
381 unsigned int snd_trident_allocate_synth_channel(void)
383 Description: Allocate hardware channel in Bank A (0-31).
385 Parameters : trident - pointer to target device class for 4DWave.
387 Return Value: hardware channel - 0-31 or -1 when no channel is available
389 ---------------------------------------------------------------------------*/
391 static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
395 for (idx = 31; idx >= 0; idx--) {
396 if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
397 trident->ChanMap[T4D_BANK_A] |= 1 << idx;
398 trident->synth.ChanSynthCount++;
405 /*---------------------------------------------------------------------------
406 void snd_trident_free_synth_channel( int channel )
408 Description: Free hardware channel in Bank B (0-31).
410 Parameters : trident - pointer to target device class for 4DWave.
411 channel - hardware channel number 0-63
415 ---------------------------------------------------------------------------*/
417 static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
419 if (channel < 0 || channel > 31)
422 if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
423 trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
424 trident->synth.ChanSynthCount--;
428 /*---------------------------------------------------------------------------
429 snd_trident_write_voice_regs
431 Description: This routine will complete and write the 5 hardware channel
432 registers to hardware.
434 Paramters: trident - pointer to target device class for 4DWave.
435 voice - synthesizer voice structure
438 ---------------------------------------------------------------------------*/
440 void snd_trident_write_voice_regs(struct snd_trident * trident,
441 struct snd_trident_voice * voice)
443 unsigned int FmcRvolCvol;
444 unsigned int regs[5];
446 regs[1] = voice->LBA;
447 regs[4] = (voice->GVSel << 31) |
448 ((voice->Pan & 0x0000007f) << 24) |
449 ((voice->CTRL & 0x0000000f) << 12);
450 FmcRvolCvol = ((voice->FMC & 3) << 14) |
451 ((voice->RVol & 0x7f) << 7) |
452 (voice->CVol & 0x7f);
454 switch (trident->device) {
455 case TRIDENT_DEVICE_ID_SI7018:
456 regs[4] |= voice->number > 31 ?
457 (voice->Vol & 0x000003ff) :
458 ((voice->Vol & 0x00003fc) << (16-2)) |
459 (voice->EC & 0x00000fff);
460 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
461 (voice->FMS & 0x0000000f);
462 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
463 regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
465 case TRIDENT_DEVICE_ID_DX:
466 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
467 (voice->EC & 0x00000fff);
468 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
469 (voice->FMS & 0x0000000f);
470 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
471 regs[3] = FmcRvolCvol;
473 case TRIDENT_DEVICE_ID_NX:
474 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
475 (voice->EC & 0x00000fff);
476 regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
477 regs[2] = ((voice->Delta << 16) & 0xff000000) |
478 (voice->ESO & 0x00ffffff);
479 regs[3] = (voice->Alpha << 20) |
480 ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
487 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
488 outl(regs[0], TRID_REG(trident, CH_START + 0));
489 outl(regs[1], TRID_REG(trident, CH_START + 4));
490 outl(regs[2], TRID_REG(trident, CH_START + 8));
491 outl(regs[3], TRID_REG(trident, CH_START + 12));
492 outl(regs[4], TRID_REG(trident, CH_START + 16));
495 printk("written %i channel:\n", voice->number);
496 printk(" regs[0] = 0x%x/0x%x\n", regs[0], inl(TRID_REG(trident, CH_START + 0)));
497 printk(" regs[1] = 0x%x/0x%x\n", regs[1], inl(TRID_REG(trident, CH_START + 4)));
498 printk(" regs[2] = 0x%x/0x%x\n", regs[2], inl(TRID_REG(trident, CH_START + 8)));
499 printk(" regs[3] = 0x%x/0x%x\n", regs[3], inl(TRID_REG(trident, CH_START + 12)));
500 printk(" regs[4] = 0x%x/0x%x\n", regs[4], inl(TRID_REG(trident, CH_START + 16)));
504 /*---------------------------------------------------------------------------
505 snd_trident_write_cso_reg
507 Description: This routine will write the new CSO offset
508 register to hardware.
510 Paramters: trident - pointer to target device class for 4DWave.
511 voice - synthesizer voice structure
514 ---------------------------------------------------------------------------*/
516 static void snd_trident_write_cso_reg(struct snd_trident * trident,
517 struct snd_trident_voice * voice,
521 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
522 if (trident->device != TRIDENT_DEVICE_ID_NX) {
523 outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
525 outl((voice->Delta << 24) |
526 (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
530 /*---------------------------------------------------------------------------
531 snd_trident_write_eso_reg
533 Description: This routine will write the new ESO offset
534 register to hardware.
536 Paramters: trident - pointer to target device class for 4DWave.
537 voice - synthesizer voice structure
540 ---------------------------------------------------------------------------*/
542 static void snd_trident_write_eso_reg(struct snd_trident * trident,
543 struct snd_trident_voice * voice,
547 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
548 if (trident->device != TRIDENT_DEVICE_ID_NX) {
549 outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
551 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
552 TRID_REG(trident, CH_NX_DELTA_ESO));
556 /*---------------------------------------------------------------------------
557 snd_trident_write_vol_reg
559 Description: This routine will write the new voice volume
560 register to hardware.
562 Paramters: trident - pointer to target device class for 4DWave.
563 voice - synthesizer voice structure
564 Vol - new voice volume
566 ---------------------------------------------------------------------------*/
568 static void snd_trident_write_vol_reg(struct snd_trident * trident,
569 struct snd_trident_voice * voice,
573 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
574 switch (trident->device) {
575 case TRIDENT_DEVICE_ID_DX:
576 case TRIDENT_DEVICE_ID_NX:
577 outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
579 case TRIDENT_DEVICE_ID_SI7018:
580 // printk("voice->Vol = 0x%x\n", voice->Vol);
581 outw((voice->CTRL << 12) | voice->Vol,
582 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
587 /*---------------------------------------------------------------------------
588 snd_trident_write_pan_reg
590 Description: This routine will write the new voice pan
591 register to hardware.
593 Paramters: trident - pointer to target device class for 4DWave.
594 voice - synthesizer voice structure
597 ---------------------------------------------------------------------------*/
599 static void snd_trident_write_pan_reg(struct snd_trident * trident,
600 struct snd_trident_voice * voice,
604 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
605 outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
606 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
609 /*---------------------------------------------------------------------------
610 snd_trident_write_rvol_reg
612 Description: This routine will write the new reverb volume
613 register to hardware.
615 Paramters: trident - pointer to target device class for 4DWave.
616 voice - synthesizer voice structure
617 RVol - new reverb volume
619 ---------------------------------------------------------------------------*/
621 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
622 struct snd_trident_voice * voice,
626 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
627 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
628 (voice->CVol & 0x007f),
629 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
630 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
633 /*---------------------------------------------------------------------------
634 snd_trident_write_cvol_reg
636 Description: This routine will write the new chorus volume
637 register to hardware.
639 Paramters: trident - pointer to target device class for 4DWave.
640 voice - synthesizer voice structure
641 CVol - new chorus volume
643 ---------------------------------------------------------------------------*/
645 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
646 struct snd_trident_voice * voice,
650 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
651 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
652 (voice->CVol & 0x007f),
653 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
654 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
657 /*---------------------------------------------------------------------------
658 snd_trident_convert_rate
660 Description: This routine converts rate in HZ to hardware delta value.
662 Paramters: trident - pointer to target device class for 4DWave.
663 rate - Real or Virtual channel number.
665 Returns: Delta value.
667 ---------------------------------------------------------------------------*/
668 static unsigned int snd_trident_convert_rate(unsigned int rate)
672 // We special case 44100 and 8000 since rounding with the equation
673 // does not give us an accurate enough value. For 11025 and 22050
674 // the equation gives us the best answer. All other frequencies will
675 // also use the equation. JDW
678 else if (rate == 8000)
680 else if (rate == 48000)
683 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
687 /*---------------------------------------------------------------------------
688 snd_trident_convert_adc_rate
690 Description: This routine converts rate in HZ to hardware delta value.
692 Paramters: trident - pointer to target device class for 4DWave.
693 rate - Real or Virtual channel number.
695 Returns: Delta value.
697 ---------------------------------------------------------------------------*/
698 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
702 // We special case 44100 and 8000 since rounding with the equation
703 // does not give us an accurate enough value. For 11025 and 22050
704 // the equation gives us the best answer. All other frequencies will
705 // also use the equation. JDW
708 else if (rate == 8000)
710 else if (rate == 48000)
713 delta = ((48000 << 12) / rate) & 0x0000ffff;
717 /*---------------------------------------------------------------------------
718 snd_trident_spurious_threshold
720 Description: This routine converts rate in HZ to spurious threshold.
722 Paramters: trident - pointer to target device class for 4DWave.
723 rate - Real or Virtual channel number.
725 Returns: Delta value.
727 ---------------------------------------------------------------------------*/
728 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
729 unsigned int period_size)
731 unsigned int res = (rate * period_size) / 48000;
739 /*---------------------------------------------------------------------------
740 snd_trident_control_mode
742 Description: This routine returns a control mode for a PCM channel.
744 Paramters: trident - pointer to target device class for 4DWave.
745 substream - PCM substream
747 Returns: Control value.
749 ---------------------------------------------------------------------------*/
750 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
753 struct snd_pcm_runtime *runtime = substream->runtime;
756 CTRL default: 8-bit (unsigned) mono, loop mode enabled
759 if (snd_pcm_format_width(runtime->format) == 16)
760 CTRL |= 0x00000008; // 16-bit data
761 if (snd_pcm_format_signed(runtime->format))
762 CTRL |= 0x00000002; // signed data
763 if (runtime->channels > 1)
764 CTRL |= 0x00000004; // stereo data
772 /*---------------------------------------------------------------------------
775 Description: Device I/O control handler for playback/capture parameters.
777 Paramters: substream - PCM substream class
778 cmd - what ioctl message to process
779 arg - additional message infoarg
781 Returns: Error status
783 ---------------------------------------------------------------------------*/
785 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
789 /* FIXME: it seems that with small periods the behaviour of
790 trident hardware is unpredictable and interrupt generator
792 return snd_pcm_lib_ioctl(substream, cmd, arg);
795 /*---------------------------------------------------------------------------
796 snd_trident_allocate_pcm_mem
798 Description: Allocate PCM ring buffer for given substream
800 Parameters: substream - PCM substream class
801 hw_params - hardware parameters
803 Returns: Error status
805 ---------------------------------------------------------------------------*/
807 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
808 struct snd_pcm_hw_params *hw_params)
810 struct snd_trident *trident = snd_pcm_substream_chip(substream);
811 struct snd_pcm_runtime *runtime = substream->runtime;
812 struct snd_trident_voice *voice = runtime->private_data;
815 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
817 if (trident->tlb.entries) {
818 if (err > 0) { /* change */
820 snd_trident_free_pages(trident, voice->memblk);
821 voice->memblk = snd_trident_alloc_pages(trident, substream);
822 if (voice->memblk == NULL)
829 /*---------------------------------------------------------------------------
830 snd_trident_allocate_evoice
832 Description: Allocate extra voice as interrupt generator
834 Parameters: substream - PCM substream class
835 hw_params - hardware parameters
837 Returns: Error status
839 ---------------------------------------------------------------------------*/
841 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
842 struct snd_pcm_hw_params *hw_params)
844 struct snd_trident *trident = snd_pcm_substream_chip(substream);
845 struct snd_pcm_runtime *runtime = substream->runtime;
846 struct snd_trident_voice *voice = runtime->private_data;
847 struct snd_trident_voice *evoice = voice->extra;
849 /* voice management */
851 if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
852 if (evoice == NULL) {
853 evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
856 voice->extra = evoice;
857 evoice->substream = substream;
860 if (evoice != NULL) {
861 snd_trident_free_voice(trident, evoice);
862 voice->extra = evoice = NULL;
869 /*---------------------------------------------------------------------------
870 snd_trident_hw_params
872 Description: Set the hardware parameters for the playback device.
874 Parameters: substream - PCM substream class
875 hw_params - hardware parameters
877 Returns: Error status
879 ---------------------------------------------------------------------------*/
881 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
882 struct snd_pcm_hw_params *hw_params)
886 err = snd_trident_allocate_pcm_mem(substream, hw_params);
888 err = snd_trident_allocate_evoice(substream, hw_params);
892 /*---------------------------------------------------------------------------
893 snd_trident_playback_hw_free
895 Description: Release the hardware resources for the playback device.
897 Parameters: substream - PCM substream class
899 Returns: Error status
901 ---------------------------------------------------------------------------*/
903 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
905 struct snd_trident *trident = snd_pcm_substream_chip(substream);
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 struct snd_trident_voice *voice = runtime->private_data;
908 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
910 if (trident->tlb.entries) {
911 if (voice && voice->memblk) {
912 snd_trident_free_pages(trident, voice->memblk);
913 voice->memblk = NULL;
916 snd_pcm_lib_free_pages(substream);
917 if (evoice != NULL) {
918 snd_trident_free_voice(trident, evoice);
924 /*---------------------------------------------------------------------------
925 snd_trident_playback_prepare
927 Description: Prepare playback device for playback.
929 Parameters: substream - PCM substream class
931 Returns: Error status
933 ---------------------------------------------------------------------------*/
935 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
937 struct snd_trident *trident = snd_pcm_substream_chip(substream);
938 struct snd_pcm_runtime *runtime = substream->runtime;
939 struct snd_trident_voice *voice = runtime->private_data;
940 struct snd_trident_voice *evoice = voice->extra;
941 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
943 spin_lock_irq(&trident->reg_lock);
945 /* set delta (rate) value */
946 voice->Delta = snd_trident_convert_rate(runtime->rate);
947 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
949 /* set Loop Begin Address */
951 voice->LBA = voice->memblk->offset;
953 voice->LBA = runtime->dma_addr;
956 voice->ESO = runtime->buffer_size - 1; /* in samples */
957 voice->CTRL = snd_trident_control_mode(substream);
963 voice->Vol = mix->vol;
964 voice->RVol = mix->rvol;
965 voice->CVol = mix->cvol;
966 voice->Pan = mix->pan;
967 voice->Attribute = 0;
969 voice->Attribute = (1<<(30-16))|(2<<(26-16))|
970 (0<<(24-16))|(0x1f<<(19-16));
972 voice->Attribute = 0;
975 snd_trident_write_voice_regs(trident, voice);
977 if (evoice != NULL) {
978 evoice->Delta = voice->Delta;
979 evoice->spurious_threshold = voice->spurious_threshold;
980 evoice->LBA = voice->LBA;
982 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
983 evoice->CTRL = voice->CTRL;
985 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
989 evoice->Vol = 0x3ff; /* mute */
990 evoice->RVol = evoice->CVol = 0x7f; /* mute */
991 evoice->Pan = 0x7f; /* mute */
993 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
994 (0<<(24-16))|(0x1f<<(19-16));
996 evoice->Attribute = 0;
998 snd_trident_write_voice_regs(trident, evoice);
1000 evoice->isync_mark = runtime->period_size;
1001 evoice->ESO = (runtime->period_size * 2) - 1;
1004 spin_unlock_irq(&trident->reg_lock);
1009 /*---------------------------------------------------------------------------
1010 snd_trident_capture_hw_params
1012 Description: Set the hardware parameters for the capture device.
1014 Parameters: substream - PCM substream class
1015 hw_params - hardware parameters
1017 Returns: Error status
1019 ---------------------------------------------------------------------------*/
1021 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1022 struct snd_pcm_hw_params *hw_params)
1024 return snd_trident_allocate_pcm_mem(substream, hw_params);
1027 /*---------------------------------------------------------------------------
1028 snd_trident_capture_prepare
1030 Description: Prepare capture device for playback.
1032 Parameters: substream - PCM substream class
1034 Returns: Error status
1036 ---------------------------------------------------------------------------*/
1038 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1040 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1041 struct snd_pcm_runtime *runtime = substream->runtime;
1042 struct snd_trident_voice *voice = runtime->private_data;
1043 unsigned int val, ESO_bytes;
1045 spin_lock_irq(&trident->reg_lock);
1047 // Initilize the channel and set channel Mode
1048 outb(0, TRID_REG(trident, LEGACY_DMAR15));
1050 // Set DMA channel operation mode register
1051 outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1053 // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
1054 voice->LBA = runtime->dma_addr;
1055 outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
1057 voice->LBA = voice->memblk->offset;
1060 ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
1061 outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
1062 outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
1065 // Set channel sample rate, 4.12 format
1066 val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
1067 outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
1069 // Set channel interrupt blk length
1070 if (snd_pcm_format_width(runtime->format) == 16) {
1071 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1073 val = (unsigned short) (ESO_bytes - 1);
1076 outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1078 // Right now, set format and start to run captureing,
1079 // continuous run loop enable.
1080 trident->bDMAStart = 0x19; // 0001 1001b
1082 if (snd_pcm_format_width(runtime->format) == 16)
1083 trident->bDMAStart |= 0x80;
1084 if (snd_pcm_format_signed(runtime->format))
1085 trident->bDMAStart |= 0x20;
1086 if (runtime->channels > 1)
1087 trident->bDMAStart |= 0x40;
1089 // Prepare capture intr channel
1091 voice->Delta = snd_trident_convert_rate(runtime->rate);
1092 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1094 voice->isync_mark = runtime->period_size;
1095 voice->isync_max = runtime->buffer_size;
1097 // Set voice parameters
1099 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1100 voice->CTRL = snd_trident_control_mode(substream);
1105 voice->Pan = 0x7f; /* mute */
1106 voice->Vol = 0x3ff; /* mute */
1110 voice->Attribute = 0;
1112 snd_trident_write_voice_regs(trident, voice);
1114 spin_unlock_irq(&trident->reg_lock);
1118 /*---------------------------------------------------------------------------
1119 snd_trident_si7018_capture_hw_params
1121 Description: Set the hardware parameters for the capture device.
1123 Parameters: substream - PCM substream class
1124 hw_params - hardware parameters
1126 Returns: Error status
1128 ---------------------------------------------------------------------------*/
1130 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1131 struct snd_pcm_hw_params *hw_params)
1135 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1138 return snd_trident_allocate_evoice(substream, hw_params);
1141 /*---------------------------------------------------------------------------
1142 snd_trident_si7018_capture_hw_free
1144 Description: Release the hardware resources for the capture device.
1146 Parameters: substream - PCM substream class
1148 Returns: Error status
1150 ---------------------------------------------------------------------------*/
1152 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1154 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1155 struct snd_pcm_runtime *runtime = substream->runtime;
1156 struct snd_trident_voice *voice = runtime->private_data;
1157 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1159 snd_pcm_lib_free_pages(substream);
1160 if (evoice != NULL) {
1161 snd_trident_free_voice(trident, evoice);
1162 voice->extra = NULL;
1167 /*---------------------------------------------------------------------------
1168 snd_trident_si7018_capture_prepare
1170 Description: Prepare capture device for playback.
1172 Parameters: substream - PCM substream class
1174 Returns: Error status
1176 ---------------------------------------------------------------------------*/
1178 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1180 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1181 struct snd_pcm_runtime *runtime = substream->runtime;
1182 struct snd_trident_voice *voice = runtime->private_data;
1183 struct snd_trident_voice *evoice = voice->extra;
1185 spin_lock_irq(&trident->reg_lock);
1187 voice->LBA = runtime->dma_addr;
1188 voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
1189 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1191 // Set voice parameters
1193 voice->ESO = runtime->buffer_size - 1; /* in samples */
1194 voice->CTRL = snd_trident_control_mode(substream);
1199 voice->Pan = T4D_DEFAULT_PCM_PAN;
1205 voice->Attribute = (2 << (30-16)) |
1210 snd_trident_write_voice_regs(trident, voice);
1212 if (evoice != NULL) {
1213 evoice->Delta = snd_trident_convert_rate(runtime->rate);
1214 evoice->spurious_threshold = voice->spurious_threshold;
1215 evoice->LBA = voice->LBA;
1217 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1218 evoice->CTRL = voice->CTRL;
1224 evoice->Vol = 0x3ff; /* mute */
1225 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1226 evoice->Pan = 0x7f; /* mute */
1227 evoice->Attribute = 0;
1228 snd_trident_write_voice_regs(trident, evoice);
1230 evoice->isync_mark = runtime->period_size;
1231 evoice->ESO = (runtime->period_size * 2) - 1;
1234 spin_unlock_irq(&trident->reg_lock);
1238 /*---------------------------------------------------------------------------
1239 snd_trident_foldback_prepare
1241 Description: Prepare foldback capture device for playback.
1243 Parameters: substream - PCM substream class
1245 Returns: Error status
1247 ---------------------------------------------------------------------------*/
1249 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1251 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1252 struct snd_pcm_runtime *runtime = substream->runtime;
1253 struct snd_trident_voice *voice = runtime->private_data;
1254 struct snd_trident_voice *evoice = voice->extra;
1256 spin_lock_irq(&trident->reg_lock);
1258 /* Set channel buffer Address */
1260 voice->LBA = voice->memblk->offset;
1262 voice->LBA = runtime->dma_addr;
1264 /* set target ESO for channel */
1265 voice->ESO = runtime->buffer_size - 1; /* in samples */
1267 /* set sample rate */
1268 voice->Delta = 0x1000;
1269 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1272 voice->CTRL = snd_trident_control_mode(substream);
1277 voice->Pan = 0x7f; /* mute */
1278 voice->Vol = 0x3ff; /* mute */
1282 voice->Attribute = 0;
1284 /* set up capture channel */
1285 outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1287 snd_trident_write_voice_regs(trident, voice);
1289 if (evoice != NULL) {
1290 evoice->Delta = voice->Delta;
1291 evoice->spurious_threshold = voice->spurious_threshold;
1292 evoice->LBA = voice->LBA;
1294 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1295 evoice->CTRL = voice->CTRL;
1297 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1301 evoice->Vol = 0x3ff; /* mute */
1302 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1303 evoice->Pan = 0x7f; /* mute */
1304 evoice->Attribute = 0;
1305 snd_trident_write_voice_regs(trident, evoice);
1307 evoice->isync_mark = runtime->period_size;
1308 evoice->ESO = (runtime->period_size * 2) - 1;
1311 spin_unlock_irq(&trident->reg_lock);
1315 /*---------------------------------------------------------------------------
1316 snd_trident_spdif_hw_params
1318 Description: Set the hardware parameters for the spdif device.
1320 Parameters: substream - PCM substream class
1321 hw_params - hardware parameters
1323 Returns: Error status
1325 ---------------------------------------------------------------------------*/
1327 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1328 struct snd_pcm_hw_params *hw_params)
1330 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1331 unsigned int old_bits = 0, change = 0;
1334 err = snd_trident_allocate_pcm_mem(substream, hw_params);
1338 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1339 err = snd_trident_allocate_evoice(substream, hw_params);
1344 /* prepare SPDIF channel */
1345 spin_lock_irq(&trident->reg_lock);
1346 old_bits = trident->spdif_pcm_bits;
1347 if (old_bits & IEC958_AES0_PROFESSIONAL)
1348 trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
1350 trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
1351 if (params_rate(hw_params) >= 48000) {
1352 trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
1353 trident->spdif_pcm_bits |=
1354 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1355 IEC958_AES0_PRO_FS_48000 :
1356 (IEC958_AES3_CON_FS_48000 << 24);
1358 else if (params_rate(hw_params) >= 44100) {
1359 trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
1360 trident->spdif_pcm_bits |=
1361 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1362 IEC958_AES0_PRO_FS_44100 :
1363 (IEC958_AES3_CON_FS_44100 << 24);
1366 trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
1367 trident->spdif_pcm_bits |=
1368 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1369 IEC958_AES0_PRO_FS_32000 :
1370 (IEC958_AES3_CON_FS_32000 << 24);
1372 change = old_bits != trident->spdif_pcm_bits;
1373 spin_unlock_irq(&trident->reg_lock);
1376 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1381 /*---------------------------------------------------------------------------
1382 snd_trident_spdif_prepare
1384 Description: Prepare SPDIF device for playback.
1386 Parameters: substream - PCM substream class
1388 Returns: Error status
1390 ---------------------------------------------------------------------------*/
1392 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1394 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1395 struct snd_pcm_runtime *runtime = substream->runtime;
1396 struct snd_trident_voice *voice = runtime->private_data;
1397 struct snd_trident_voice *evoice = voice->extra;
1398 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1399 unsigned int RESO, LBAO;
1402 spin_lock_irq(&trident->reg_lock);
1404 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1406 /* set delta (rate) value */
1407 voice->Delta = snd_trident_convert_rate(runtime->rate);
1408 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1410 /* set Loop Back Address */
1411 LBAO = runtime->dma_addr;
1413 voice->LBA = voice->memblk->offset;
1419 voice->isync_mark = runtime->period_size;
1420 voice->isync_max = runtime->buffer_size;
1422 /* set target ESO for channel */
1423 RESO = runtime->buffer_size - 1;
1424 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1427 voice->CTRL = snd_trident_control_mode(substream);
1439 voice->Attribute = 0;
1441 /* prepare surrogate IRQ channel */
1442 snd_trident_write_voice_regs(trident, voice);
1444 outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
1445 outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
1446 outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
1447 outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
1448 outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
1450 /* set SPDIF setting */
1451 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1452 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1456 /* set delta (rate) value */
1457 voice->Delta = 0x800;
1458 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1460 /* set Loop Begin Address */
1462 voice->LBA = voice->memblk->offset;
1464 voice->LBA = runtime->dma_addr;
1467 voice->ESO = runtime->buffer_size - 1; /* in samples */
1468 voice->CTRL = snd_trident_control_mode(substream);
1474 voice->Vol = mix->vol;
1475 voice->RVol = mix->rvol;
1476 voice->CVol = mix->cvol;
1477 voice->Pan = mix->pan;
1478 voice->Attribute = (1<<(30-16))|(7<<(26-16))|
1479 (0<<(24-16))|(0<<(19-16));
1481 snd_trident_write_voice_regs(trident, voice);
1483 if (evoice != NULL) {
1484 evoice->Delta = voice->Delta;
1485 evoice->spurious_threshold = voice->spurious_threshold;
1486 evoice->LBA = voice->LBA;
1488 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1489 evoice->CTRL = voice->CTRL;
1491 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1495 evoice->Vol = 0x3ff; /* mute */
1496 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1497 evoice->Pan = 0x7f; /* mute */
1498 evoice->Attribute = 0;
1499 snd_trident_write_voice_regs(trident, evoice);
1501 evoice->isync_mark = runtime->period_size;
1502 evoice->ESO = (runtime->period_size * 2) - 1;
1505 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1506 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1508 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1509 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1511 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1514 spin_unlock_irq(&trident->reg_lock);
1519 /*---------------------------------------------------------------------------
1522 Description: Start/stop devices
1524 Parameters: substream - PCM substream class
1525 cmd - trigger command (STOP, GO)
1527 Returns: Error status
1529 ---------------------------------------------------------------------------*/
1531 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1535 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1536 struct list_head *pos;
1537 struct snd_pcm_substream *s;
1538 unsigned int what, whati, capture_flag, spdif_flag;
1539 struct snd_trident_voice *voice, *evoice;
1540 unsigned int val, go;
1543 case SNDRV_PCM_TRIGGER_START:
1544 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1545 case SNDRV_PCM_TRIGGER_RESUME:
1548 case SNDRV_PCM_TRIGGER_STOP:
1549 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1550 case SNDRV_PCM_TRIGGER_SUSPEND:
1556 what = whati = capture_flag = spdif_flag = 0;
1557 spin_lock(&trident->reg_lock);
1558 val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
1559 snd_pcm_group_for_each(pos, substream) {
1560 s = snd_pcm_group_substream_entry(pos);
1561 if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
1562 voice = s->runtime->private_data;
1563 evoice = voice->extra;
1564 what |= 1 << (voice->number & 0x1f);
1565 if (evoice == NULL) {
1566 whati |= 1 << (voice->number & 0x1f);
1568 what |= 1 << (evoice->number & 0x1f);
1569 whati |= 1 << (evoice->number & 0x1f);
1571 evoice->stimer = val;
1575 voice->stimer = val;
1579 snd_pcm_trigger_done(s, substream);
1587 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1588 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1589 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1591 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1592 val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
1593 outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1597 outl(what, TRID_REG(trident, T4D_STOP_B));
1598 val = inl(TRID_REG(trident, T4D_AINTEN_B));
1604 outl(val, TRID_REG(trident, T4D_AINTEN_B));
1606 outl(what, TRID_REG(trident, T4D_START_B));
1608 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1609 outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1611 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1612 outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1614 spin_unlock(&trident->reg_lock);
1618 /*---------------------------------------------------------------------------
1619 snd_trident_playback_pointer
1621 Description: This routine return the playback position
1623 Parameters: substream - PCM substream class
1625 Returns: position of buffer
1627 ---------------------------------------------------------------------------*/
1629 static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1631 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1632 struct snd_pcm_runtime *runtime = substream->runtime;
1633 struct snd_trident_voice *voice = runtime->private_data;
1636 if (!voice->running)
1639 spin_lock(&trident->reg_lock);
1641 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
1643 if (trident->device != TRIDENT_DEVICE_ID_NX) {
1644 cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
1645 } else { // ID_4DWAVE_NX
1646 cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
1649 spin_unlock(&trident->reg_lock);
1651 if (cso >= runtime->buffer_size)
1657 /*---------------------------------------------------------------------------
1658 snd_trident_capture_pointer
1660 Description: This routine return the capture position
1662 Paramters: pcm1 - PCM device class
1664 Returns: position of buffer
1666 ---------------------------------------------------------------------------*/
1668 static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1670 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1671 struct snd_pcm_runtime *runtime = substream->runtime;
1672 struct snd_trident_voice *voice = runtime->private_data;
1673 unsigned int result;
1675 if (!voice->running)
1678 result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
1679 if (runtime->channels > 1)
1682 result = runtime->buffer_size - result;
1687 /*---------------------------------------------------------------------------
1688 snd_trident_spdif_pointer
1690 Description: This routine return the SPDIF playback position
1692 Parameters: substream - PCM substream class
1694 Returns: position of buffer
1696 ---------------------------------------------------------------------------*/
1698 static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1700 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1701 struct snd_pcm_runtime *runtime = substream->runtime;
1702 struct snd_trident_voice *voice = runtime->private_data;
1703 unsigned int result;
1705 if (!voice->running)
1708 result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
1714 * Playback support device description
1717 static struct snd_pcm_hardware snd_trident_playback =
1719 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1720 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1721 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1722 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1723 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1724 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1725 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1730 .buffer_bytes_max = (256*1024),
1731 .period_bytes_min = 64,
1732 .period_bytes_max = (256*1024),
1734 .periods_max = 1024,
1739 * Capture support device description
1742 static struct snd_pcm_hardware snd_trident_capture =
1744 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1745 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1746 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1747 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1748 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1749 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1750 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1755 .buffer_bytes_max = (128*1024),
1756 .period_bytes_min = 64,
1757 .period_bytes_max = (128*1024),
1759 .periods_max = 1024,
1764 * Foldback capture support device description
1767 static struct snd_pcm_hardware snd_trident_foldback =
1769 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1770 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1771 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1772 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1773 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1774 .rates = SNDRV_PCM_RATE_48000,
1779 .buffer_bytes_max = (128*1024),
1780 .period_bytes_min = 64,
1781 .period_bytes_max = (128*1024),
1783 .periods_max = 1024,
1788 * SPDIF playback support device description
1791 static struct snd_pcm_hardware snd_trident_spdif =
1793 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1794 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1795 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1796 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1797 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1798 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1799 SNDRV_PCM_RATE_48000),
1804 .buffer_bytes_max = (128*1024),
1805 .period_bytes_min = 64,
1806 .period_bytes_max = (128*1024),
1808 .periods_max = 1024,
1812 static struct snd_pcm_hardware snd_trident_spdif_7018 =
1814 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1815 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1816 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1817 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1818 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1819 .rates = SNDRV_PCM_RATE_48000,
1824 .buffer_bytes_max = (128*1024),
1825 .period_bytes_min = 64,
1826 .period_bytes_max = (128*1024),
1828 .periods_max = 1024,
1832 static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1834 struct snd_trident_voice *voice = runtime->private_data;
1835 struct snd_trident *trident;
1838 trident = voice->trident;
1839 snd_trident_free_voice(trident, voice);
1843 static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1845 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1846 struct snd_pcm_runtime *runtime = substream->runtime;
1847 struct snd_trident_voice *voice;
1849 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1852 snd_trident_pcm_mixer_build(trident, voice, substream);
1853 voice->substream = substream;
1854 runtime->private_data = voice;
1855 runtime->private_free = snd_trident_pcm_free_substream;
1856 runtime->hw = snd_trident_playback;
1857 snd_pcm_set_sync(substream);
1858 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1862 /*---------------------------------------------------------------------------
1863 snd_trident_playback_close
1865 Description: This routine will close the 4DWave playback device. For now
1866 we will simply free the dma transfer buffer.
1868 Parameters: substream - PCM substream class
1870 ---------------------------------------------------------------------------*/
1871 static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1873 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1874 struct snd_pcm_runtime *runtime = substream->runtime;
1875 struct snd_trident_voice *voice = runtime->private_data;
1877 snd_trident_pcm_mixer_free(trident, voice, substream);
1881 /*---------------------------------------------------------------------------
1882 snd_trident_spdif_open
1884 Description: This routine will open the 4DWave SPDIF device.
1886 Parameters: substream - PCM substream class
1888 Returns: status - success or failure flag
1890 ---------------------------------------------------------------------------*/
1892 static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1894 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1895 struct snd_trident_voice *voice;
1896 struct snd_pcm_runtime *runtime = substream->runtime;
1898 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1902 voice->substream = substream;
1903 spin_lock_irq(&trident->reg_lock);
1904 trident->spdif_pcm_bits = trident->spdif_bits;
1905 spin_unlock_irq(&trident->reg_lock);
1907 runtime->private_data = voice;
1908 runtime->private_free = snd_trident_pcm_free_substream;
1909 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1910 runtime->hw = snd_trident_spdif;
1912 runtime->hw = snd_trident_spdif_7018;
1915 trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1916 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1917 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1919 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1924 /*---------------------------------------------------------------------------
1925 snd_trident_spdif_close
1927 Description: This routine will close the 4DWave SPDIF device.
1929 Parameters: substream - PCM substream class
1931 ---------------------------------------------------------------------------*/
1933 static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1935 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1938 spin_lock_irq(&trident->reg_lock);
1939 // restore default SPDIF setting
1940 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1941 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1942 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
1944 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
1945 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1946 if (trident->spdif_ctrl) {
1951 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1953 spin_unlock_irq(&trident->reg_lock);
1954 trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1955 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1956 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1960 /*---------------------------------------------------------------------------
1961 snd_trident_capture_open
1963 Description: This routine will open the 4DWave capture device.
1965 Parameters: substream - PCM substream class
1967 Returns: status - success or failure flag
1969 ---------------------------------------------------------------------------*/
1971 static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1973 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1974 struct snd_trident_voice *voice;
1975 struct snd_pcm_runtime *runtime = substream->runtime;
1977 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1981 voice->substream = substream;
1982 runtime->private_data = voice;
1983 runtime->private_free = snd_trident_pcm_free_substream;
1984 runtime->hw = snd_trident_capture;
1985 snd_pcm_set_sync(substream);
1986 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1990 /*---------------------------------------------------------------------------
1991 snd_trident_capture_close
1993 Description: This routine will close the 4DWave capture device. For now
1994 we will simply free the dma transfer buffer.
1996 Parameters: substream - PCM substream class
1998 ---------------------------------------------------------------------------*/
1999 static int snd_trident_capture_close(struct snd_pcm_substream *substream)
2004 /*---------------------------------------------------------------------------
2005 snd_trident_foldback_open
2007 Description: This routine will open the 4DWave foldback capture device.
2009 Parameters: substream - PCM substream class
2011 Returns: status - success or failure flag
2013 ---------------------------------------------------------------------------*/
2015 static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
2017 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2018 struct snd_trident_voice *voice;
2019 struct snd_pcm_runtime *runtime = substream->runtime;
2021 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
2024 voice->foldback_chan = substream->number;
2025 voice->substream = substream;
2026 runtime->private_data = voice;
2027 runtime->private_free = snd_trident_pcm_free_substream;
2028 runtime->hw = snd_trident_foldback;
2029 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
2033 /*---------------------------------------------------------------------------
2034 snd_trident_foldback_close
2036 Description: This routine will close the 4DWave foldback capture device.
2037 For now we will simply free the dma transfer buffer.
2039 Parameters: substream - PCM substream class
2041 ---------------------------------------------------------------------------*/
2042 static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
2044 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2045 struct snd_trident_voice *voice;
2046 struct snd_pcm_runtime *runtime = substream->runtime;
2047 voice = runtime->private_data;
2049 /* stop capture channel */
2050 spin_lock_irq(&trident->reg_lock);
2051 outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
2052 spin_unlock_irq(&trident->reg_lock);
2056 /*---------------------------------------------------------------------------
2058 ---------------------------------------------------------------------------*/
2060 static struct snd_pcm_ops snd_trident_playback_ops = {
2061 .open = snd_trident_playback_open,
2062 .close = snd_trident_playback_close,
2063 .ioctl = snd_trident_ioctl,
2064 .hw_params = snd_trident_hw_params,
2065 .hw_free = snd_trident_hw_free,
2066 .prepare = snd_trident_playback_prepare,
2067 .trigger = snd_trident_trigger,
2068 .pointer = snd_trident_playback_pointer,
2071 static struct snd_pcm_ops snd_trident_nx_playback_ops = {
2072 .open = snd_trident_playback_open,
2073 .close = snd_trident_playback_close,
2074 .ioctl = snd_trident_ioctl,
2075 .hw_params = snd_trident_hw_params,
2076 .hw_free = snd_trident_hw_free,
2077 .prepare = snd_trident_playback_prepare,
2078 .trigger = snd_trident_trigger,
2079 .pointer = snd_trident_playback_pointer,
2080 .page = snd_pcm_sgbuf_ops_page,
2083 static struct snd_pcm_ops snd_trident_capture_ops = {
2084 .open = snd_trident_capture_open,
2085 .close = snd_trident_capture_close,
2086 .ioctl = snd_trident_ioctl,
2087 .hw_params = snd_trident_capture_hw_params,
2088 .hw_free = snd_trident_hw_free,
2089 .prepare = snd_trident_capture_prepare,
2090 .trigger = snd_trident_trigger,
2091 .pointer = snd_trident_capture_pointer,
2094 static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
2095 .open = snd_trident_capture_open,
2096 .close = snd_trident_capture_close,
2097 .ioctl = snd_trident_ioctl,
2098 .hw_params = snd_trident_si7018_capture_hw_params,
2099 .hw_free = snd_trident_si7018_capture_hw_free,
2100 .prepare = snd_trident_si7018_capture_prepare,
2101 .trigger = snd_trident_trigger,
2102 .pointer = snd_trident_playback_pointer,
2105 static struct snd_pcm_ops snd_trident_foldback_ops = {
2106 .open = snd_trident_foldback_open,
2107 .close = snd_trident_foldback_close,
2108 .ioctl = snd_trident_ioctl,
2109 .hw_params = snd_trident_hw_params,
2110 .hw_free = snd_trident_hw_free,
2111 .prepare = snd_trident_foldback_prepare,
2112 .trigger = snd_trident_trigger,
2113 .pointer = snd_trident_playback_pointer,
2116 static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
2117 .open = snd_trident_foldback_open,
2118 .close = snd_trident_foldback_close,
2119 .ioctl = snd_trident_ioctl,
2120 .hw_params = snd_trident_hw_params,
2121 .hw_free = snd_trident_hw_free,
2122 .prepare = snd_trident_foldback_prepare,
2123 .trigger = snd_trident_trigger,
2124 .pointer = snd_trident_playback_pointer,
2125 .page = snd_pcm_sgbuf_ops_page,
2128 static struct snd_pcm_ops snd_trident_spdif_ops = {
2129 .open = snd_trident_spdif_open,
2130 .close = snd_trident_spdif_close,
2131 .ioctl = snd_trident_ioctl,
2132 .hw_params = snd_trident_spdif_hw_params,
2133 .hw_free = snd_trident_hw_free,
2134 .prepare = snd_trident_spdif_prepare,
2135 .trigger = snd_trident_trigger,
2136 .pointer = snd_trident_spdif_pointer,
2139 static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
2140 .open = snd_trident_spdif_open,
2141 .close = snd_trident_spdif_close,
2142 .ioctl = snd_trident_ioctl,
2143 .hw_params = snd_trident_spdif_hw_params,
2144 .hw_free = snd_trident_hw_free,
2145 .prepare = snd_trident_spdif_prepare,
2146 .trigger = snd_trident_trigger,
2147 .pointer = snd_trident_playback_pointer,
2150 /*---------------------------------------------------------------------------
2153 Description: This routine registers the 4DWave device for PCM support.
2155 Paramters: trident - pointer to target device class for 4DWave.
2159 ---------------------------------------------------------------------------*/
2161 int __devinit snd_trident_pcm(struct snd_trident * trident,
2162 int device, struct snd_pcm ** rpcm)
2164 struct snd_pcm *pcm;
2169 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
2172 pcm->private_data = trident;
2174 if (trident->tlb.entries) {
2175 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
2177 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
2179 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2180 trident->device != TRIDENT_DEVICE_ID_SI7018 ?
2181 &snd_trident_capture_ops :
2182 &snd_trident_si7018_capture_ops);
2184 pcm->info_flags = 0;
2185 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
2186 strcpy(pcm->name, "Trident 4DWave");
2189 if (trident->tlb.entries) {
2190 struct snd_pcm_substream *substream;
2191 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
2192 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
2193 snd_dma_pci_data(trident->pci),
2195 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2196 SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
2199 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2200 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2208 /*---------------------------------------------------------------------------
2209 snd_trident_foldback_pcm
2211 Description: This routine registers the 4DWave device for foldback PCM support.
2213 Paramters: trident - pointer to target device class for 4DWave.
2217 ---------------------------------------------------------------------------*/
2219 int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
2220 int device, struct snd_pcm ** rpcm)
2222 struct snd_pcm *foldback;
2225 struct snd_pcm_substream *substream;
2229 if (trident->device == TRIDENT_DEVICE_ID_NX)
2231 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
2234 foldback->private_data = trident;
2235 if (trident->tlb.entries)
2236 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
2238 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
2239 foldback->info_flags = 0;
2240 strcpy(foldback->name, "Trident 4DWave");
2241 substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2242 strcpy(substream->name, "Front Mixer");
2243 substream = substream->next;
2244 strcpy(substream->name, "Reverb Mixer");
2245 substream = substream->next;
2246 strcpy(substream->name, "Chorus Mixer");
2247 if (num_chan == 4) {
2248 substream = substream->next;
2249 strcpy(substream->name, "Second AC'97 ADC");
2251 trident->foldback = foldback;
2253 if (trident->tlb.entries)
2254 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
2255 snd_dma_pci_data(trident->pci), 0, 128*1024);
2257 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
2258 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2265 /*---------------------------------------------------------------------------
2268 Description: This routine registers the 4DWave-NX device for SPDIF support.
2270 Paramters: trident - pointer to target device class for 4DWave-NX.
2274 ---------------------------------------------------------------------------*/
2276 int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
2277 int device, struct snd_pcm ** rpcm)
2279 struct snd_pcm *spdif;
2284 if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
2287 spdif->private_data = trident;
2288 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2289 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
2291 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
2293 spdif->info_flags = 0;
2294 strcpy(spdif->name, "Trident 4DWave IEC958");
2295 trident->spdif = spdif;
2297 snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2309 /*---------------------------------------------------------------------------
2310 snd_trident_spdif_control
2312 Description: enable/disable S/PDIF out from ac97 mixer
2313 ---------------------------------------------------------------------------*/
2315 static int snd_trident_spdif_control_info(struct snd_kcontrol *kcontrol,
2316 struct snd_ctl_elem_info *uinfo)
2318 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2320 uinfo->value.integer.min = 0;
2321 uinfo->value.integer.max = 1;
2325 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2326 struct snd_ctl_elem_value *ucontrol)
2328 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2331 spin_lock_irq(&trident->reg_lock);
2332 val = trident->spdif_ctrl;
2333 ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2334 spin_unlock_irq(&trident->reg_lock);
2338 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
2341 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2345 val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2346 spin_lock_irq(&trident->reg_lock);
2347 /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2348 change = trident->spdif_ctrl != val;
2349 trident->spdif_ctrl = val;
2350 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2351 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2352 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2353 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2356 if (trident->spdif == NULL) {
2358 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2359 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2362 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2365 spin_unlock_irq(&trident->reg_lock);
2369 static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
2371 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2372 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2373 .info = snd_trident_spdif_control_info,
2374 .get = snd_trident_spdif_control_get,
2375 .put = snd_trident_spdif_control_put,
2376 .private_value = 0x28,
2379 /*---------------------------------------------------------------------------
2380 snd_trident_spdif_default
2382 Description: put/get the S/PDIF default settings
2383 ---------------------------------------------------------------------------*/
2385 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_info *uinfo)
2388 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2393 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
2396 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2398 spin_lock_irq(&trident->reg_lock);
2399 ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2400 ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2401 ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2402 ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2403 spin_unlock_irq(&trident->reg_lock);
2407 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_value *ucontrol)
2410 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2414 val = (ucontrol->value.iec958.status[0] << 0) |
2415 (ucontrol->value.iec958.status[1] << 8) |
2416 (ucontrol->value.iec958.status[2] << 16) |
2417 (ucontrol->value.iec958.status[3] << 24);
2418 spin_lock_irq(&trident->reg_lock);
2419 change = trident->spdif_bits != val;
2420 trident->spdif_bits = val;
2421 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2422 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2423 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2425 if (trident->spdif == NULL)
2426 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2428 spin_unlock_irq(&trident->reg_lock);
2432 static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
2434 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2435 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2436 .info = snd_trident_spdif_default_info,
2437 .get = snd_trident_spdif_default_get,
2438 .put = snd_trident_spdif_default_put
2441 /*---------------------------------------------------------------------------
2442 snd_trident_spdif_mask
2444 Description: put/get the S/PDIF mask
2445 ---------------------------------------------------------------------------*/
2447 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_info *uinfo)
2450 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2455 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2458 ucontrol->value.iec958.status[0] = 0xff;
2459 ucontrol->value.iec958.status[1] = 0xff;
2460 ucontrol->value.iec958.status[2] = 0xff;
2461 ucontrol->value.iec958.status[3] = 0xff;
2465 static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
2467 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2468 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2469 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2470 .info = snd_trident_spdif_mask_info,
2471 .get = snd_trident_spdif_mask_get,
2474 /*---------------------------------------------------------------------------
2475 snd_trident_spdif_stream
2477 Description: put/get the S/PDIF stream settings
2478 ---------------------------------------------------------------------------*/
2480 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_info *uinfo)
2483 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2488 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_value *ucontrol)
2491 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2493 spin_lock_irq(&trident->reg_lock);
2494 ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2495 ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2496 ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2497 ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2498 spin_unlock_irq(&trident->reg_lock);
2502 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2503 struct snd_ctl_elem_value *ucontrol)
2505 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2509 val = (ucontrol->value.iec958.status[0] << 0) |
2510 (ucontrol->value.iec958.status[1] << 8) |
2511 (ucontrol->value.iec958.status[2] << 16) |
2512 (ucontrol->value.iec958.status[3] << 24);
2513 spin_lock_irq(&trident->reg_lock);
2514 change = trident->spdif_pcm_bits != val;
2515 trident->spdif_pcm_bits = val;
2516 if (trident->spdif != NULL) {
2517 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2518 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2520 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2523 spin_unlock_irq(&trident->reg_lock);
2527 static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
2529 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2530 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2531 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2532 .info = snd_trident_spdif_stream_info,
2533 .get = snd_trident_spdif_stream_get,
2534 .put = snd_trident_spdif_stream_put
2537 /*---------------------------------------------------------------------------
2538 snd_trident_ac97_control
2540 Description: enable/disable rear path for ac97
2541 ---------------------------------------------------------------------------*/
2543 static int snd_trident_ac97_control_info(struct snd_kcontrol *kcontrol,
2544 struct snd_ctl_elem_info *uinfo)
2546 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2548 uinfo->value.integer.min = 0;
2549 uinfo->value.integer.max = 1;
2553 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_value *ucontrol)
2556 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2559 spin_lock_irq(&trident->reg_lock);
2560 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2561 ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2562 spin_unlock_irq(&trident->reg_lock);
2566 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_value *ucontrol)
2569 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2573 spin_lock_irq(&trident->reg_lock);
2574 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2575 val &= ~(1 << kcontrol->private_value);
2576 if (ucontrol->value.integer.value[0])
2577 val |= 1 << kcontrol->private_value;
2578 change = val != trident->ac97_ctrl;
2579 trident->ac97_ctrl = val;
2580 outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2581 spin_unlock_irq(&trident->reg_lock);
2585 static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
2587 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2588 .name = "Rear Path",
2589 .info = snd_trident_ac97_control_info,
2590 .get = snd_trident_ac97_control_get,
2591 .put = snd_trident_ac97_control_put,
2595 /*---------------------------------------------------------------------------
2596 snd_trident_vol_control
2598 Description: wave & music volume control
2599 ---------------------------------------------------------------------------*/
2601 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_info *uinfo)
2604 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2606 uinfo->value.integer.min = 0;
2607 uinfo->value.integer.max = 255;
2611 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2612 struct snd_ctl_elem_value *ucontrol)
2614 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2617 val = trident->musicvol_wavevol;
2618 ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2619 ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2623 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2624 struct snd_ctl_elem_value *ucontrol)
2626 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2630 spin_lock_irq(&trident->reg_lock);
2631 val = trident->musicvol_wavevol;
2632 val &= ~(0xffff << kcontrol->private_value);
2633 val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2634 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2635 change = val != trident->musicvol_wavevol;
2636 outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2637 spin_unlock_irq(&trident->reg_lock);
2641 static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
2643 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2644 .name = "Music Playback Volume",
2645 .info = snd_trident_vol_control_info,
2646 .get = snd_trident_vol_control_get,
2647 .put = snd_trident_vol_control_put,
2648 .private_value = 16,
2651 static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
2653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2654 .name = "Wave Playback Volume",
2655 .info = snd_trident_vol_control_info,
2656 .get = snd_trident_vol_control_get,
2657 .put = snd_trident_vol_control_put,
2661 /*---------------------------------------------------------------------------
2662 snd_trident_pcm_vol_control
2664 Description: PCM front volume control
2665 ---------------------------------------------------------------------------*/
2667 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_info *uinfo)
2670 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2672 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2674 uinfo->value.integer.min = 0;
2675 uinfo->value.integer.max = 255;
2676 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2677 uinfo->value.integer.max = 1023;
2681 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2684 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2685 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2687 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2688 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2690 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2695 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2696 struct snd_ctl_elem_value *ucontrol)
2698 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2699 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2703 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2704 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2706 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2708 spin_lock_irq(&trident->reg_lock);
2709 change = val != mix->vol;
2711 if (mix->voice != NULL)
2712 snd_trident_write_vol_reg(trident, mix->voice, val);
2713 spin_unlock_irq(&trident->reg_lock);
2717 static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
2719 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2720 .name = "PCM Front Playback Volume",
2721 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2723 .info = snd_trident_pcm_vol_control_info,
2724 .get = snd_trident_pcm_vol_control_get,
2725 .put = snd_trident_pcm_vol_control_put,
2728 /*---------------------------------------------------------------------------
2729 snd_trident_pcm_pan_control
2731 Description: PCM front pan control
2732 ---------------------------------------------------------------------------*/
2734 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_info *uinfo)
2737 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2739 uinfo->value.integer.min = 0;
2740 uinfo->value.integer.max = 127;
2744 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2747 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2748 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2750 ucontrol->value.integer.value[0] = mix->pan;
2751 if (ucontrol->value.integer.value[0] & 0x40) {
2752 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2754 ucontrol->value.integer.value[0] |= 0x40;
2759 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2760 struct snd_ctl_elem_value *ucontrol)
2762 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2763 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2767 if (ucontrol->value.integer.value[0] & 0x40)
2768 val = ucontrol->value.integer.value[0] & 0x3f;
2770 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2771 spin_lock_irq(&trident->reg_lock);
2772 change = val != mix->pan;
2774 if (mix->voice != NULL)
2775 snd_trident_write_pan_reg(trident, mix->voice, val);
2776 spin_unlock_irq(&trident->reg_lock);
2780 static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
2782 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2783 .name = "PCM Pan Playback Control",
2784 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2786 .info = snd_trident_pcm_pan_control_info,
2787 .get = snd_trident_pcm_pan_control_get,
2788 .put = snd_trident_pcm_pan_control_put,
2791 /*---------------------------------------------------------------------------
2792 snd_trident_pcm_rvol_control
2794 Description: PCM reverb volume control
2795 ---------------------------------------------------------------------------*/
2797 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_info *uinfo)
2800 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2802 uinfo->value.integer.min = 0;
2803 uinfo->value.integer.max = 127;
2807 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
2810 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2811 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2813 ucontrol->value.integer.value[0] = 127 - mix->rvol;
2817 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_value *ucontrol)
2820 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2821 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2825 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2826 spin_lock_irq(&trident->reg_lock);
2827 change = val != mix->rvol;
2829 if (mix->voice != NULL)
2830 snd_trident_write_rvol_reg(trident, mix->voice, val);
2831 spin_unlock_irq(&trident->reg_lock);
2835 static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
2837 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2838 .name = "PCM Reverb Playback Volume",
2839 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2841 .info = snd_trident_pcm_rvol_control_info,
2842 .get = snd_trident_pcm_rvol_control_get,
2843 .put = snd_trident_pcm_rvol_control_put,
2846 /*---------------------------------------------------------------------------
2847 snd_trident_pcm_cvol_control
2849 Description: PCM chorus volume control
2850 ---------------------------------------------------------------------------*/
2852 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_info *uinfo)
2855 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2857 uinfo->value.integer.min = 0;
2858 uinfo->value.integer.max = 127;
2862 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
2865 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2866 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2868 ucontrol->value.integer.value[0] = 127 - mix->cvol;
2872 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
2875 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2876 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2880 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2881 spin_lock_irq(&trident->reg_lock);
2882 change = val != mix->cvol;
2884 if (mix->voice != NULL)
2885 snd_trident_write_cvol_reg(trident, mix->voice, val);
2886 spin_unlock_irq(&trident->reg_lock);
2890 static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
2892 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2893 .name = "PCM Chorus Playback Volume",
2894 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2896 .info = snd_trident_pcm_cvol_control_info,
2897 .get = snd_trident_pcm_cvol_control_get,
2898 .put = snd_trident_pcm_cvol_control_put,
2901 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2902 struct snd_kcontrol *kctl,
2903 int num, int activate)
2905 struct snd_ctl_elem_id id;
2910 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2912 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2913 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2914 SNDRV_CTL_EVENT_MASK_INFO,
2915 snd_ctl_build_ioff(&id, kctl, num));
2918 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2919 struct snd_trident_pcm_mixer *tmix,
2920 int num, int activate)
2922 snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2923 snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2924 snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2925 snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2928 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2929 struct snd_trident_voice *voice,
2930 struct snd_pcm_substream *substream)
2932 struct snd_trident_pcm_mixer *tmix;
2934 snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
2935 tmix = &trident->pcm_mixer[substream->number];
2936 tmix->voice = voice;
2937 tmix->vol = T4D_DEFAULT_PCM_VOL;
2938 tmix->pan = T4D_DEFAULT_PCM_PAN;
2939 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2940 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2941 snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2945 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2947 struct snd_trident_pcm_mixer *tmix;
2949 snd_assert(trident != NULL && substream != NULL, return -EINVAL);
2950 tmix = &trident->pcm_mixer[substream->number];
2952 snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2956 /*---------------------------------------------------------------------------
2959 Description: This routine registers the 4DWave device for mixer support.
2961 Paramters: trident - pointer to target device class for 4DWave.
2965 ---------------------------------------------------------------------------*/
2967 static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
2969 struct snd_ac97_template _ac97;
2970 struct snd_card *card = trident->card;
2971 struct snd_kcontrol *kctl;
2972 struct snd_ctl_elem_value *uctl;
2973 int idx, err, retries = 2;
2974 static struct snd_ac97_bus_ops ops = {
2975 .write = snd_trident_codec_write,
2976 .read = snd_trident_codec_read,
2979 uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
2983 if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
2986 memset(&_ac97, 0, sizeof(_ac97));
2987 _ac97.private_data = trident;
2988 trident->ac97_detect = 1;
2991 if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
2992 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2993 if ((err = snd_trident_sis_reset(trident)) < 0)
3002 /* secondary codec? */
3003 if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
3004 (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
3006 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
3008 snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
3009 #if 0 // only for my testing purpose --jk
3011 struct snd_ac97 *mc97;
3012 err = snd_ac97_modem(trident->card, &_ac97, &mc97);
3014 snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
3019 trident->ac97_detect = 0;
3021 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3022 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3024 kctl->put(kctl, uctl);
3025 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3027 kctl->put(kctl, uctl);
3028 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3030 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3033 for (idx = 0; idx < 32; idx++) {
3034 struct snd_trident_pcm_mixer *tmix;
3036 tmix = &trident->pcm_mixer[idx];
3039 if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3041 if ((err = snd_ctl_add(card, trident->ctl_vol)))
3044 if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3046 if ((err = snd_ctl_add(card, trident->ctl_pan)))
3049 if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3051 if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3054 if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3056 if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3059 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3060 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3062 kctl->put(kctl, uctl);
3064 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3066 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3071 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3073 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3075 idx = kctl->id.index;
3076 if ((err = snd_ctl_add(card, kctl)) < 0)
3078 kctl->put(kctl, uctl);
3080 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3085 kctl->id.index = idx;
3086 kctl->id.device = pcm_spdif_device;
3087 if ((err = snd_ctl_add(card, kctl)) < 0)
3090 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3095 kctl->id.index = idx;
3096 kctl->id.device = pcm_spdif_device;
3097 if ((err = snd_ctl_add(card, kctl)) < 0)
3100 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3105 kctl->id.index = idx;
3106 kctl->id.device = pcm_spdif_device;
3107 if ((err = snd_ctl_add(card, kctl)) < 0)
3109 trident->spdif_pcm_ctl = kctl;
3125 * gameport interface
3128 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
3130 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3132 struct snd_trident *chip = gameport_get_port_data(gameport);
3134 snd_assert(chip, return 0);
3135 return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3138 static void snd_trident_gameport_trigger(struct gameport *gameport)
3140 struct snd_trident *chip = gameport_get_port_data(gameport);
3142 snd_assert(chip, return);
3143 outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3146 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3148 struct snd_trident *chip = gameport_get_port_data(gameport);
3151 snd_assert(chip, return 0);
3153 *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3155 for (i = 0; i < 4; i++) {
3156 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3157 if (axes[i] == 0xffff) axes[i] = -1;
3163 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3165 struct snd_trident *chip = gameport_get_port_data(gameport);
3167 snd_assert(chip, return 0);
3170 case GAMEPORT_MODE_COOKED:
3171 outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3174 case GAMEPORT_MODE_RAW:
3175 outb(0, TRID_REG(chip, GAMEPORT_GCR));
3182 int __devinit snd_trident_create_gameport(struct snd_trident *chip)
3184 struct gameport *gp;
3186 chip->gameport = gp = gameport_allocate_port();
3188 printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
3192 gameport_set_name(gp, "Trident 4DWave");
3193 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3194 gameport_set_dev_parent(gp, &chip->pci->dev);
3196 gameport_set_port_data(gp, chip);
3198 gp->read = snd_trident_gameport_read;
3199 gp->trigger = snd_trident_gameport_trigger;
3200 gp->cooked_read = snd_trident_gameport_cooked_read;
3201 gp->open = snd_trident_gameport_open;
3203 gameport_register_port(gp);
3208 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3210 if (chip->gameport) {
3211 gameport_unregister_port(chip->gameport);
3212 chip->gameport = NULL;
3216 int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3217 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3218 #endif /* CONFIG_GAMEPORT */
3223 static inline void do_delay(struct snd_trident *chip)
3225 schedule_timeout_uninterruptible(1);
3232 static int snd_trident_sis_reset(struct snd_trident *trident)
3234 unsigned long end_time;
3238 r = trident->in_suspend ? 0 : 2; /* count of retries */
3240 pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
3242 pci_write_config_byte(trident->pci, 0x46, 0x00);
3244 /* disable AC97 GPIO interrupt */
3245 outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3246 /* initialize serial interface, force cold reset */
3247 i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3248 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3250 /* remove cold reset */
3252 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3254 /* wait, until the codec is ready */
3255 end_time = (jiffies + (HZ * 3) / 4) + 1;
3257 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3260 } while (time_after_eq(end_time, jiffies));
3261 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3263 end_time = jiffies + HZ;
3266 } while (time_after_eq(end_time, jiffies));
3267 goto __si7018_retry;
3270 /* wait for the second codec */
3272 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3275 } while (time_after_eq(end_time, jiffies));
3276 /* enable 64 channel mode */
3277 outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3285 static void snd_trident_proc_read(struct snd_info_entry *entry,
3286 struct snd_info_buffer *buffer)
3288 struct snd_trident *trident = entry->private_data;
3291 switch (trident->device) {
3292 case TRIDENT_DEVICE_ID_SI7018:
3293 s = "SiS 7018 Audio";
3295 case TRIDENT_DEVICE_ID_DX:
3296 s = "Trident 4DWave PCI DX";
3298 case TRIDENT_DEVICE_ID_NX:
3299 s = "Trident 4DWave PCI NX";
3304 snd_iprintf(buffer, "%s\n\n", s);
3305 snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
3306 snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3307 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3308 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3309 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3310 snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3311 if (trident->tlb.entries) {
3312 snd_iprintf(buffer,"\nVirtual Memory\n");
3313 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3314 snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
3315 snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3318 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3319 snd_iprintf(buffer,"\nWavetable Synth\n");
3320 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
3321 snd_iprintf(buffer, "Memory Used : %d\n", trident->synth.current_size);
3322 snd_iprintf(buffer, "Memory Free : %d\n", (trident->synth.max_size-trident->synth.current_size));
3326 static void __devinit snd_trident_proc_init(struct snd_trident * trident)
3328 struct snd_info_entry *entry;
3329 const char *s = "trident";
3331 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3333 if (! snd_card_proc_new(trident->card, s, &entry))
3334 snd_info_set_text_ops(entry, trident, 1024, snd_trident_proc_read);
3337 static int snd_trident_dev_free(struct snd_device *device)
3339 struct snd_trident *trident = device->device_data;
3340 return snd_trident_free(trident);
3343 /*---------------------------------------------------------------------------
3344 snd_trident_tlb_alloc
3346 Description: Allocate and set up the TLB page table on 4D NX.
3347 Each entry has 4 bytes (physical PCI address).
3349 Paramters: trident - pointer to target device class for 4DWave.
3351 Returns: 0 or negative error code
3353 ---------------------------------------------------------------------------*/
3355 static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
3359 /* TLB array must be aligned to 16kB !!! so we allocate
3360 32kB region and correct offset when necessary */
3362 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3363 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3364 snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
3367 trident->tlb.entries = (unsigned int*)(((unsigned long)trident->tlb.buffer.area + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1));
3368 trident->tlb.entries_dmaaddr = (trident->tlb.buffer.addr + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1);
3369 /* allocate shadow TLB page table (virtual addresses) */
3370 trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
3371 if (trident->tlb.shadow_entries == NULL) {
3372 snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
3375 /* allocate and setup silent page and initialise TLB entries */
3376 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3377 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3378 snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
3381 memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3382 for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3383 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3384 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3387 /* use emu memory block manager code to manage tlb page allocation */
3388 trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3389 if (trident->tlb.memhdr == NULL)
3392 trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3397 * initialize 4D DX chip
3400 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3402 outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3403 outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3404 outl(0, TRID_REG(trident, T4D_AINTEN_A));
3405 outl(0, TRID_REG(trident, T4D_AINTEN_B));
3408 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3410 struct pci_dev *pci = trident->pci;
3411 unsigned long end_time;
3413 /* reset the legacy configuration and whole audio/wavetable block */
3414 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3415 pci_write_config_byte(pci, 0x44, 0); /* ports */
3416 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3417 pci_write_config_byte(pci, 0x46, 4); /* reset */
3419 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3422 /* warm reset of the AC'97 codec */
3423 outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3425 outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3426 /* DAC on, disable SB IRQ and try to force ADC valid signal */
3427 trident->ac97_ctrl = 0x0000004a;
3428 outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3429 /* wait, until the codec is ready */
3430 end_time = (jiffies + (HZ * 3) / 4) + 1;
3432 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3435 } while (time_after_eq(end_time, jiffies));
3436 snd_printk(KERN_ERR "AC'97 codec ready error\n");
3440 snd_trident_stop_all_voices(trident);
3446 * initialize 4D NX chip
3448 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3450 struct pci_dev *pci = trident->pci;
3451 unsigned long end_time;
3453 /* reset the legacy configuration and whole audio/wavetable block */
3454 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3455 pci_write_config_byte(pci, 0x44, 0); /* ports */
3456 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3458 pci_write_config_byte(pci, 0x46, 1); /* reset */
3460 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3463 /* warm reset of the AC'97 codec */
3464 outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3466 outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3467 /* wait, until the codec is ready */
3468 end_time = (jiffies + (HZ * 3) / 4) + 1;
3470 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3473 } while (time_after_eq(end_time, jiffies));
3474 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3479 trident->ac97_ctrl = 0x00000002;
3480 outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3481 /* disable SB IRQ */
3482 outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3484 snd_trident_stop_all_voices(trident);
3486 if (trident->tlb.entries != NULL) {
3488 /* enable virtual addressing via TLB */
3489 i = trident->tlb.entries_dmaaddr;
3491 outl(i, TRID_REG(trident, NX_TLBC));
3493 outl(0, TRID_REG(trident, NX_TLBC));
3495 /* initialize S/PDIF */
3496 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3497 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3503 * initialize sis7018 chip
3505 static int snd_trident_sis_init(struct snd_trident *trident)
3509 if ((err = snd_trident_sis_reset(trident)) < 0)
3512 snd_trident_stop_all_voices(trident);
3514 /* initialize S/PDIF */
3515 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3520 /*---------------------------------------------------------------------------
3523 Description: This routine will create the device specific class for
3524 the 4DWave card. It will also perform basic initialization.
3526 Paramters: card - which card to create
3527 pci - interface to PCI bus resource info
3528 dma1ptr - playback dma buffer
3529 dma2ptr - capture dma buffer
3530 irqptr - interrupt resource info
3532 Returns: 4DWave device class private data
3534 ---------------------------------------------------------------------------*/
3536 int __devinit snd_trident_create(struct snd_card *card,
3537 struct pci_dev *pci,
3539 int pcm_spdif_device,
3540 int max_wavetable_size,
3541 struct snd_trident ** rtrident)
3543 struct snd_trident *trident;
3545 struct snd_trident_voice *voice;
3546 struct snd_trident_pcm_mixer *tmix;
3547 static struct snd_device_ops ops = {
3548 .dev_free = snd_trident_dev_free,
3553 /* enable PCI device */
3554 if ((err = pci_enable_device(pci)) < 0)
3556 /* check, if we can restrict PCI DMA transfers to 30 bits */
3557 if (pci_set_dma_mask(pci, 0x3fffffff) < 0 ||
3558 pci_set_consistent_dma_mask(pci, 0x3fffffff) < 0) {
3559 snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
3560 pci_disable_device(pci);
3564 trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3565 if (trident == NULL) {
3566 pci_disable_device(pci);
3569 trident->device = (pci->vendor << 16) | pci->device;
3570 trident->card = card;
3572 spin_lock_init(&trident->reg_lock);
3573 spin_lock_init(&trident->event_lock);
3574 spin_lock_init(&trident->voice_alloc);
3575 if (pcm_streams < 1)
3577 if (pcm_streams > 32)
3579 trident->ChanPCM = pcm_streams;
3580 if (max_wavetable_size < 0 )
3581 max_wavetable_size = 0;
3582 trident->synth.max_size = max_wavetable_size * 1024;
3585 trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3586 pci_set_master(pci);
3588 if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3590 pci_disable_device(pci);
3593 trident->port = pci_resource_start(pci, 0);
3595 if (request_irq(pci->irq, snd_trident_interrupt, SA_INTERRUPT|SA_SHIRQ,
3596 "Trident Audio", trident)) {
3597 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
3598 snd_trident_free(trident);
3601 trident->irq = pci->irq;
3603 /* allocate 16k-aligned TLB for NX cards */
3604 trident->tlb.entries = NULL;
3605 trident->tlb.buffer.area = NULL;
3606 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3607 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3608 snd_trident_free(trident);
3613 trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3615 /* initialize chip */
3616 switch (trident->device) {
3617 case TRIDENT_DEVICE_ID_DX:
3618 err = snd_trident_4d_dx_init(trident);
3620 case TRIDENT_DEVICE_ID_NX:
3621 err = snd_trident_4d_nx_init(trident);
3623 case TRIDENT_DEVICE_ID_SI7018:
3624 err = snd_trident_sis_init(trident);
3631 snd_trident_free(trident);
3635 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3636 snd_trident_free(trident);
3640 if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3643 /* initialise synth voices */
3644 for (i = 0; i < 64; i++) {
3645 voice = &trident->synth.voices[i];
3647 voice->trident = trident;
3649 /* initialize pcm mixer entries */
3650 for (i = 0; i < 32; i++) {
3651 tmix = &trident->pcm_mixer[i];
3652 tmix->vol = T4D_DEFAULT_PCM_VOL;
3653 tmix->pan = T4D_DEFAULT_PCM_PAN;
3654 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3655 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3658 snd_trident_enable_eso(trident);
3660 snd_trident_proc_init(trident);
3661 snd_card_set_dev(card, &pci->dev);
3662 *rtrident = trident;
3666 /*---------------------------------------------------------------------------
3669 Description: This routine will free the device specific class for
3672 Paramters: trident - device specific private data for 4DWave card
3676 ---------------------------------------------------------------------------*/
3678 static int snd_trident_free(struct snd_trident *trident)
3680 snd_trident_free_gameport(trident);
3681 snd_trident_disable_eso(trident);
3682 // Disable S/PDIF out
3683 if (trident->device == TRIDENT_DEVICE_ID_NX)
3684 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3685 else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3686 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3688 if (trident->tlb.buffer.area) {
3689 outl(0, TRID_REG(trident, NX_TLBC));
3690 if (trident->tlb.memhdr)
3691 snd_util_memhdr_free(trident->tlb.memhdr);
3692 if (trident->tlb.silent_page.area)
3693 snd_dma_free_pages(&trident->tlb.silent_page);
3694 vfree(trident->tlb.shadow_entries);
3695 snd_dma_free_pages(&trident->tlb.buffer);
3697 if (trident->irq >= 0)
3698 free_irq(trident->irq, trident);
3699 pci_release_regions(trident->pci);
3700 pci_disable_device(trident->pci);
3705 /*---------------------------------------------------------------------------
3706 snd_trident_interrupt
3708 Description: ISR for Trident 4DWave device
3710 Paramters: trident - device specific private data for 4DWave card
3712 Problems: It seems that Trident chips generates interrupts more than
3713 one time in special cases. The spurious interrupts are
3714 detected via sample timer (T4D_STIMER) and computing
3715 corresponding delta value. The limits are detected with
3716 the method try & fail so it is possible that it won't
3717 work on all computers. [jaroslav]
3721 ---------------------------------------------------------------------------*/
3723 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3725 struct snd_trident *trident = dev_id;
3726 unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3728 struct snd_trident_voice *voice;
3730 audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3731 if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3733 if (audio_int & ADDRESS_IRQ) {
3734 // get interrupt status for all channels
3735 spin_lock(&trident->reg_lock);
3736 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3737 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3740 outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
3742 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3745 for (channel = 63; channel >= 32; channel--) {
3746 mask = 1 << (channel&0x1f);
3747 if ((chn_int & mask) == 0)
3749 voice = &trident->synth.voices[channel];
3750 if (!voice->pcm || voice->substream == NULL) {
3751 outl(mask, TRID_REG(trident, T4D_STOP_B));
3754 delta = (int)stimer - (int)voice->stimer;
3757 if ((unsigned int)delta < voice->spurious_threshold) {
3758 /* do some statistics here */
3759 trident->spurious_irq_count++;
3760 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3761 trident->spurious_irq_max_delta = delta;
3764 voice->stimer = stimer;
3766 if (!voice->isync3) {
3767 tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3768 if (trident->bDMAStart & 0x40)
3771 tmp = voice->isync_max - tmp;
3773 tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3775 if (tmp < voice->isync_mark) {
3777 tmp = voice->isync_ESO - 7;
3779 tmp = voice->isync_ESO + 2;
3780 /* update ESO for IRQ voice to preserve sync */
3781 snd_trident_stop_voice(trident, voice->number);
3782 snd_trident_write_eso_reg(trident, voice, tmp);
3783 snd_trident_start_voice(trident, voice->number);
3785 } else if (voice->isync2) {
3787 /* write original ESO and update CSO for IRQ voice to preserve sync */
3788 snd_trident_stop_voice(trident, voice->number);
3789 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3790 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3791 snd_trident_start_voice(trident, voice->number);
3795 /* update CSO for extra voice to preserve sync */
3796 snd_trident_stop_voice(trident, voice->extra->number);
3797 snd_trident_write_cso_reg(trident, voice->extra, 0);
3798 snd_trident_start_voice(trident, voice->extra->number);
3801 spin_unlock(&trident->reg_lock);
3802 snd_pcm_period_elapsed(voice->substream);
3803 spin_lock(&trident->reg_lock);
3805 outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
3807 spin_unlock(&trident->reg_lock);
3809 if (audio_int & MPU401_IRQ) {
3810 if (trident->rmidi) {
3811 snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data, regs);
3813 inb(TRID_REG(trident, T4D_MPUR0));
3816 // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3820 /*---------------------------------------------------------------------------
3821 snd_trident_attach_synthesizer
3823 Description: Attach synthesizer hooks
3825 Paramters: trident - device specific private data for 4DWave card
3829 ---------------------------------------------------------------------------*/
3830 int snd_trident_attach_synthesizer(struct snd_trident *trident)
3832 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3833 if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
3834 sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
3835 strcpy(trident->seq_dev->name, "4DWave");
3836 *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
3842 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3844 struct snd_trident_voice *pvoice;
3845 unsigned long flags;
3848 spin_lock_irqsave(&trident->voice_alloc, flags);
3849 if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3850 idx = snd_trident_allocate_pcm_channel(trident);
3852 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3855 pvoice = &trident->synth.voices[idx];
3858 pvoice->capture = 0;
3860 pvoice->memblk = NULL;
3861 pvoice->substream = NULL;
3862 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3865 if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3866 idx = snd_trident_allocate_synth_channel(trident);
3868 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3871 pvoice = &trident->synth.voices[idx];
3874 pvoice->client = client;
3875 pvoice->port = port;
3876 pvoice->memblk = NULL;
3877 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3880 if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3882 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3886 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3888 unsigned long flags;
3889 void (*private_free)(struct snd_trident_voice *);
3892 if (voice == NULL || !voice->use)
3894 snd_trident_clear_voices(trident, voice->number, voice->number);
3895 spin_lock_irqsave(&trident->voice_alloc, flags);
3896 private_free = voice->private_free;
3897 private_data = voice->private_data;
3898 voice->private_free = NULL;
3899 voice->private_data = NULL;
3901 snd_trident_free_pcm_channel(trident, voice->number);
3903 snd_trident_free_synth_channel(trident, voice->number);
3904 voice->use = voice->pcm = voice->synth = voice->midi = 0;
3905 voice->capture = voice->spdif = 0;
3906 voice->sample_ops = NULL;
3907 voice->substream = NULL;
3908 voice->extra = NULL;
3909 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3911 private_free(voice);
3914 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3916 unsigned int i, val, mask[2] = { 0, 0 };
3918 snd_assert(v_min <= 63, return);
3919 snd_assert(v_max <= 63, return);
3920 for (i = v_min; i <= v_max; i++)
3921 mask[i >> 5] |= 1 << (i & 0x1f);
3923 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3924 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3925 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3928 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3929 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3930 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3935 int snd_trident_suspend(struct pci_dev *pci, pm_message_t state)
3937 struct snd_card *card = pci_get_drvdata(pci);
3938 struct snd_trident *trident = card->private_data;
3940 trident->in_suspend = 1;
3941 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3942 snd_pcm_suspend_all(trident->pcm);
3943 snd_pcm_suspend_all(trident->foldback);
3944 snd_pcm_suspend_all(trident->spdif);
3946 snd_ac97_suspend(trident->ac97);
3947 snd_ac97_suspend(trident->ac97_sec);
3949 switch (trident->device) {
3950 case TRIDENT_DEVICE_ID_DX:
3951 case TRIDENT_DEVICE_ID_NX:
3953 case TRIDENT_DEVICE_ID_SI7018:
3956 pci_disable_device(pci);
3957 pci_save_state(pci);
3961 int snd_trident_resume(struct pci_dev *pci)
3963 struct snd_card *card = pci_get_drvdata(pci);
3964 struct snd_trident *trident = card->private_data;
3966 pci_restore_state(pci);
3967 pci_enable_device(pci);
3968 pci_set_master(pci); /* to be sure */
3970 switch (trident->device) {
3971 case TRIDENT_DEVICE_ID_DX:
3972 snd_trident_4d_dx_init(trident);
3974 case TRIDENT_DEVICE_ID_NX:
3975 snd_trident_4d_nx_init(trident);
3977 case TRIDENT_DEVICE_ID_SI7018:
3978 snd_trident_sis_init(trident);
3982 snd_ac97_resume(trident->ac97);
3983 snd_ac97_resume(trident->ac97_sec);
3985 /* restore some registers */
3986 outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3988 snd_trident_enable_eso(trident);
3990 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3991 trident->in_suspend = 0;
3994 #endif /* CONFIG_PM */
3996 EXPORT_SYMBOL(snd_trident_alloc_voice);
3997 EXPORT_SYMBOL(snd_trident_free_voice);
3998 EXPORT_SYMBOL(snd_trident_start_voice);
3999 EXPORT_SYMBOL(snd_trident_stop_voice);
4000 EXPORT_SYMBOL(snd_trident_write_voice_regs);
4001 /* trident_memory.c symbols */
4002 EXPORT_SYMBOL(snd_trident_synth_alloc);
4003 EXPORT_SYMBOL(snd_trident_synth_free);
4004 EXPORT_SYMBOL(snd_trident_synth_copy_from_user);