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>
38 #include <linux/dma-mapping.h>
40 #include <sound/core.h>
41 #include <sound/info.h>
42 #include <sound/control.h>
43 #include <sound/tlv.h>
44 #include <sound/trident.h>
45 #include <sound/asoundef.h>
49 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
50 struct snd_trident_voice * voice,
51 struct snd_pcm_substream *substream);
52 static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
53 struct snd_trident_voice * voice,
54 struct snd_pcm_substream *substream);
55 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id,
56 struct pt_regs *regs);
57 static int snd_trident_sis_reset(struct snd_trident *trident);
59 static void snd_trident_clear_voices(struct snd_trident * trident,
60 unsigned short v_min, unsigned short v_max);
61 static int snd_trident_free(struct snd_trident *trident);
69 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
71 unsigned int val, tmp;
73 printk("Trident voice %i:\n", voice);
74 outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
75 val = inl(TRID_REG(trident, CH_LBA));
76 printk("LBA: 0x%x\n", val);
77 val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
78 printk("GVSel: %i\n", val >> 31);
79 printk("Pan: 0x%x\n", (val >> 24) & 0x7f);
80 printk("Vol: 0x%x\n", (val >> 16) & 0xff);
81 printk("CTRL: 0x%x\n", (val >> 12) & 0x0f);
82 printk("EC: 0x%x\n", val & 0x0fff);
83 if (trident->device != TRIDENT_DEVICE_ID_NX) {
84 val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
85 printk("CSO: 0x%x\n", val >> 16);
86 printk("Alpha: 0x%x\n", (val >> 4) & 0x0fff);
87 printk("FMS: 0x%x\n", val & 0x0f);
88 val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
89 printk("ESO: 0x%x\n", val >> 16);
90 printk("Delta: 0x%x\n", val & 0xffff);
91 val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
92 } else { // TRIDENT_DEVICE_ID_NX
93 val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
94 tmp = (val >> 24) & 0xff;
95 printk("CSO: 0x%x\n", val & 0x00ffffff);
96 val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
97 tmp |= (val >> 16) & 0xff00;
98 printk("Delta: 0x%x\n", tmp);
99 printk("ESO: 0x%x\n", val & 0x00ffffff);
100 val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
101 printk("Alpha: 0x%x\n", val >> 20);
102 printk("FMS: 0x%x\n", (val >> 16) & 0x0f);
104 printk("FMC: 0x%x\n", (val >> 14) & 3);
105 printk("RVol: 0x%x\n", (val >> 7) & 0x7f);
106 printk("CVol: 0x%x\n", val & 0x7f);
110 /*---------------------------------------------------------------------------
111 unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
113 Description: This routine will do all of the reading from the external
116 Parameters: ac97 - ac97 codec structure
117 reg - CODEC register index, from AC97 Hal.
119 returns: 16 bit value read from the AC97.
121 ---------------------------------------------------------------------------*/
122 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
124 unsigned int data = 0, treg;
125 unsigned short count = 0xffff;
127 struct snd_trident *trident = ac97->private_data;
129 spin_lock_irqsave(&trident->reg_lock, flags);
130 if (trident->device == TRIDENT_DEVICE_ID_DX) {
131 data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
132 outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
134 data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
135 if ((data & DX_AC97_BUSY_READ) == 0)
138 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
139 data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
140 treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
141 outl(data, TRID_REG(trident, treg));
143 data = inl(TRID_REG(trident, treg));
144 if ((data & 0x00000C00) == 0)
147 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
148 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
150 data |= SI_AC97_SECONDARY;
151 outl(data, TRID_REG(trident, SI_AC97_READ));
153 data = inl(TRID_REG(trident, SI_AC97_READ));
154 if ((data & (SI_AC97_BUSY_READ)) == 0)
159 if (count == 0 && !trident->ac97_detect) {
160 snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
165 spin_unlock_irqrestore(&trident->reg_lock, flags);
166 return ((unsigned short) (data >> 16));
169 /*---------------------------------------------------------------------------
170 void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
171 unsigned short wdata)
173 Description: This routine will do all of the writing to the external
176 Parameters: ac97 - ac97 codec structure
177 reg - CODEC register index, from AC97 Hal.
178 data - Lower 16 bits are the data to write to CODEC.
180 returns: TRUE if everything went ok, else FALSE.
182 ---------------------------------------------------------------------------*/
183 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
184 unsigned short wdata)
186 unsigned int address, data;
187 unsigned short count = 0xffff;
189 struct snd_trident *trident = ac97->private_data;
191 data = ((unsigned long) wdata) << 16;
193 spin_lock_irqsave(&trident->reg_lock, flags);
194 if (trident->device == TRIDENT_DEVICE_ID_DX) {
195 address = DX_ACR0_AC97_W;
197 /* read AC-97 write register status */
199 if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
203 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
204 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
205 address = NX_ACR1_AC97_W;
207 /* read AC-97 write register status */
209 if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
213 data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
214 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
215 address = SI_AC97_WRITE;
217 /* read AC-97 write register status */
219 if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
223 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
225 data |= SI_AC97_SECONDARY;
227 address = 0; /* keep GCC happy */
228 count = 0; /* return */
232 spin_unlock_irqrestore(&trident->reg_lock, flags);
235 outl(data, TRID_REG(trident, address));
236 spin_unlock_irqrestore(&trident->reg_lock, flags);
239 /*---------------------------------------------------------------------------
240 void snd_trident_enable_eso(struct snd_trident *trident)
242 Description: This routine will enable end of loop interrupts.
243 End of loop interrupts will occur when a running
245 Also enables middle of loop interrupts.
247 Parameters: trident - pointer to target device class for 4DWave.
249 ---------------------------------------------------------------------------*/
251 static void snd_trident_enable_eso(struct snd_trident * trident)
255 val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
258 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
260 outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
263 /*---------------------------------------------------------------------------
264 void snd_trident_disable_eso(struct snd_trident *trident)
266 Description: This routine will disable end of loop interrupts.
267 End of loop interrupts will occur when a running
269 Also disables middle of loop interrupts.
272 trident - pointer to target device class for 4DWave.
274 returns: TRUE if everything went ok, else FALSE.
276 ---------------------------------------------------------------------------*/
278 static void snd_trident_disable_eso(struct snd_trident * trident)
282 tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
285 outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
288 /*---------------------------------------------------------------------------
289 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
291 Description: Start a voice, any channel 0 thru 63.
292 This routine automatically handles the fact that there are
293 more than 32 channels available.
295 Parameters : voice - Voice number 0 thru n.
296 trident - pointer to target device class for 4DWave.
300 ---------------------------------------------------------------------------*/
302 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
304 unsigned int mask = 1 << (voice & 0x1f);
305 unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
307 outl(mask, TRID_REG(trident, reg));
310 EXPORT_SYMBOL(snd_trident_start_voice);
312 /*---------------------------------------------------------------------------
313 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
315 Description: Stop a voice, any channel 0 thru 63.
316 This routine automatically handles the fact that there are
317 more than 32 channels available.
319 Parameters : voice - Voice number 0 thru n.
320 trident - pointer to target device class for 4DWave.
324 ---------------------------------------------------------------------------*/
326 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
328 unsigned int mask = 1 << (voice & 0x1f);
329 unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
331 outl(mask, TRID_REG(trident, reg));
334 EXPORT_SYMBOL(snd_trident_stop_voice);
336 /*---------------------------------------------------------------------------
337 int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
339 Description: Allocate hardware channel in Bank B (32-63).
341 Parameters : trident - pointer to target device class for 4DWave.
343 Return Value: hardware channel - 32-63 or -1 when no channel is available
345 ---------------------------------------------------------------------------*/
347 static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
351 if (trident->ChanPCMcnt >= trident->ChanPCM)
353 for (idx = 31; idx >= 0; idx--) {
354 if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
355 trident->ChanMap[T4D_BANK_B] |= 1 << idx;
356 trident->ChanPCMcnt++;
363 /*---------------------------------------------------------------------------
364 void snd_trident_free_pcm_channel(int channel)
366 Description: Free hardware channel in Bank B (32-63)
368 Parameters : trident - pointer to target device class for 4DWave.
369 channel - hardware channel number 0-63
373 ---------------------------------------------------------------------------*/
375 static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
377 if (channel < 32 || channel > 63)
380 if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
381 trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
382 trident->ChanPCMcnt--;
386 /*---------------------------------------------------------------------------
387 unsigned int snd_trident_allocate_synth_channel(void)
389 Description: Allocate hardware channel in Bank A (0-31).
391 Parameters : trident - pointer to target device class for 4DWave.
393 Return Value: hardware channel - 0-31 or -1 when no channel is available
395 ---------------------------------------------------------------------------*/
397 static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
401 for (idx = 31; idx >= 0; idx--) {
402 if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
403 trident->ChanMap[T4D_BANK_A] |= 1 << idx;
404 trident->synth.ChanSynthCount++;
411 /*---------------------------------------------------------------------------
412 void snd_trident_free_synth_channel( int channel )
414 Description: Free hardware channel in Bank B (0-31).
416 Parameters : trident - pointer to target device class for 4DWave.
417 channel - hardware channel number 0-63
421 ---------------------------------------------------------------------------*/
423 static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
425 if (channel < 0 || channel > 31)
428 if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
429 trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
430 trident->synth.ChanSynthCount--;
434 /*---------------------------------------------------------------------------
435 snd_trident_write_voice_regs
437 Description: This routine will complete and write the 5 hardware channel
438 registers to hardware.
440 Paramters: trident - pointer to target device class for 4DWave.
441 voice - synthesizer voice structure
444 ---------------------------------------------------------------------------*/
446 void snd_trident_write_voice_regs(struct snd_trident * trident,
447 struct snd_trident_voice * voice)
449 unsigned int FmcRvolCvol;
450 unsigned int regs[5];
452 regs[1] = voice->LBA;
453 regs[4] = (voice->GVSel << 31) |
454 ((voice->Pan & 0x0000007f) << 24) |
455 ((voice->CTRL & 0x0000000f) << 12);
456 FmcRvolCvol = ((voice->FMC & 3) << 14) |
457 ((voice->RVol & 0x7f) << 7) |
458 (voice->CVol & 0x7f);
460 switch (trident->device) {
461 case TRIDENT_DEVICE_ID_SI7018:
462 regs[4] |= voice->number > 31 ?
463 (voice->Vol & 0x000003ff) :
464 ((voice->Vol & 0x00003fc) << (16-2)) |
465 (voice->EC & 0x00000fff);
466 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
467 (voice->FMS & 0x0000000f);
468 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
469 regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
471 case TRIDENT_DEVICE_ID_DX:
472 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
473 (voice->EC & 0x00000fff);
474 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
475 (voice->FMS & 0x0000000f);
476 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
477 regs[3] = FmcRvolCvol;
479 case TRIDENT_DEVICE_ID_NX:
480 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
481 (voice->EC & 0x00000fff);
482 regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
483 regs[2] = ((voice->Delta << 16) & 0xff000000) |
484 (voice->ESO & 0x00ffffff);
485 regs[3] = (voice->Alpha << 20) |
486 ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
493 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
494 outl(regs[0], TRID_REG(trident, CH_START + 0));
495 outl(regs[1], TRID_REG(trident, CH_START + 4));
496 outl(regs[2], TRID_REG(trident, CH_START + 8));
497 outl(regs[3], TRID_REG(trident, CH_START + 12));
498 outl(regs[4], TRID_REG(trident, CH_START + 16));
501 printk("written %i channel:\n", voice->number);
502 printk(" regs[0] = 0x%x/0x%x\n", regs[0], inl(TRID_REG(trident, CH_START + 0)));
503 printk(" regs[1] = 0x%x/0x%x\n", regs[1], inl(TRID_REG(trident, CH_START + 4)));
504 printk(" regs[2] = 0x%x/0x%x\n", regs[2], inl(TRID_REG(trident, CH_START + 8)));
505 printk(" regs[3] = 0x%x/0x%x\n", regs[3], inl(TRID_REG(trident, CH_START + 12)));
506 printk(" regs[4] = 0x%x/0x%x\n", regs[4], inl(TRID_REG(trident, CH_START + 16)));
510 EXPORT_SYMBOL(snd_trident_write_voice_regs);
512 /*---------------------------------------------------------------------------
513 snd_trident_write_cso_reg
515 Description: This routine will write the new CSO offset
516 register to hardware.
518 Paramters: trident - pointer to target device class for 4DWave.
519 voice - synthesizer voice structure
522 ---------------------------------------------------------------------------*/
524 static void snd_trident_write_cso_reg(struct snd_trident * trident,
525 struct snd_trident_voice * voice,
529 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
530 if (trident->device != TRIDENT_DEVICE_ID_NX) {
531 outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
533 outl((voice->Delta << 24) |
534 (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
538 /*---------------------------------------------------------------------------
539 snd_trident_write_eso_reg
541 Description: This routine will write the new ESO offset
542 register to hardware.
544 Paramters: trident - pointer to target device class for 4DWave.
545 voice - synthesizer voice structure
548 ---------------------------------------------------------------------------*/
550 static void snd_trident_write_eso_reg(struct snd_trident * trident,
551 struct snd_trident_voice * voice,
555 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
556 if (trident->device != TRIDENT_DEVICE_ID_NX) {
557 outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
559 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
560 TRID_REG(trident, CH_NX_DELTA_ESO));
564 /*---------------------------------------------------------------------------
565 snd_trident_write_vol_reg
567 Description: This routine will write the new voice volume
568 register to hardware.
570 Paramters: trident - pointer to target device class for 4DWave.
571 voice - synthesizer voice structure
572 Vol - new voice volume
574 ---------------------------------------------------------------------------*/
576 static void snd_trident_write_vol_reg(struct snd_trident * trident,
577 struct snd_trident_voice * voice,
581 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
582 switch (trident->device) {
583 case TRIDENT_DEVICE_ID_DX:
584 case TRIDENT_DEVICE_ID_NX:
585 outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
587 case TRIDENT_DEVICE_ID_SI7018:
588 // printk("voice->Vol = 0x%x\n", voice->Vol);
589 outw((voice->CTRL << 12) | voice->Vol,
590 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
595 /*---------------------------------------------------------------------------
596 snd_trident_write_pan_reg
598 Description: This routine will write the new voice pan
599 register to hardware.
601 Paramters: trident - pointer to target device class for 4DWave.
602 voice - synthesizer voice structure
605 ---------------------------------------------------------------------------*/
607 static void snd_trident_write_pan_reg(struct snd_trident * trident,
608 struct snd_trident_voice * voice,
612 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
613 outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
614 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
617 /*---------------------------------------------------------------------------
618 snd_trident_write_rvol_reg
620 Description: This routine will write the new reverb volume
621 register to hardware.
623 Paramters: trident - pointer to target device class for 4DWave.
624 voice - synthesizer voice structure
625 RVol - new reverb volume
627 ---------------------------------------------------------------------------*/
629 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
630 struct snd_trident_voice * voice,
634 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
635 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
636 (voice->CVol & 0x007f),
637 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
638 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
641 /*---------------------------------------------------------------------------
642 snd_trident_write_cvol_reg
644 Description: This routine will write the new chorus volume
645 register to hardware.
647 Paramters: trident - pointer to target device class for 4DWave.
648 voice - synthesizer voice structure
649 CVol - new chorus volume
651 ---------------------------------------------------------------------------*/
653 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
654 struct snd_trident_voice * voice,
658 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
659 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
660 (voice->CVol & 0x007f),
661 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
662 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
665 /*---------------------------------------------------------------------------
666 snd_trident_convert_rate
668 Description: This routine converts rate in HZ to hardware delta value.
670 Paramters: trident - pointer to target device class for 4DWave.
671 rate - Real or Virtual channel number.
673 Returns: Delta value.
675 ---------------------------------------------------------------------------*/
676 static unsigned int snd_trident_convert_rate(unsigned int rate)
680 // We special case 44100 and 8000 since rounding with the equation
681 // does not give us an accurate enough value. For 11025 and 22050
682 // the equation gives us the best answer. All other frequencies will
683 // also use the equation. JDW
686 else if (rate == 8000)
688 else if (rate == 48000)
691 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
695 /*---------------------------------------------------------------------------
696 snd_trident_convert_adc_rate
698 Description: This routine converts rate in HZ to hardware delta value.
700 Paramters: trident - pointer to target device class for 4DWave.
701 rate - Real or Virtual channel number.
703 Returns: Delta value.
705 ---------------------------------------------------------------------------*/
706 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
710 // We special case 44100 and 8000 since rounding with the equation
711 // does not give us an accurate enough value. For 11025 and 22050
712 // the equation gives us the best answer. All other frequencies will
713 // also use the equation. JDW
716 else if (rate == 8000)
718 else if (rate == 48000)
721 delta = ((48000 << 12) / rate) & 0x0000ffff;
725 /*---------------------------------------------------------------------------
726 snd_trident_spurious_threshold
728 Description: This routine converts rate in HZ to spurious threshold.
730 Paramters: trident - pointer to target device class for 4DWave.
731 rate - Real or Virtual channel number.
733 Returns: Delta value.
735 ---------------------------------------------------------------------------*/
736 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
737 unsigned int period_size)
739 unsigned int res = (rate * period_size) / 48000;
747 /*---------------------------------------------------------------------------
748 snd_trident_control_mode
750 Description: This routine returns a control mode for a PCM channel.
752 Paramters: trident - pointer to target device class for 4DWave.
753 substream - PCM substream
755 Returns: Control value.
757 ---------------------------------------------------------------------------*/
758 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
761 struct snd_pcm_runtime *runtime = substream->runtime;
764 CTRL default: 8-bit (unsigned) mono, loop mode enabled
767 if (snd_pcm_format_width(runtime->format) == 16)
768 CTRL |= 0x00000008; // 16-bit data
769 if (snd_pcm_format_signed(runtime->format))
770 CTRL |= 0x00000002; // signed data
771 if (runtime->channels > 1)
772 CTRL |= 0x00000004; // stereo data
780 /*---------------------------------------------------------------------------
783 Description: Device I/O control handler for playback/capture parameters.
785 Paramters: substream - PCM substream class
786 cmd - what ioctl message to process
787 arg - additional message infoarg
789 Returns: Error status
791 ---------------------------------------------------------------------------*/
793 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
797 /* FIXME: it seems that with small periods the behaviour of
798 trident hardware is unpredictable and interrupt generator
800 return snd_pcm_lib_ioctl(substream, cmd, arg);
803 /*---------------------------------------------------------------------------
804 snd_trident_allocate_pcm_mem
806 Description: Allocate PCM ring buffer for given substream
808 Parameters: substream - PCM substream class
809 hw_params - hardware parameters
811 Returns: Error status
813 ---------------------------------------------------------------------------*/
815 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
816 struct snd_pcm_hw_params *hw_params)
818 struct snd_trident *trident = snd_pcm_substream_chip(substream);
819 struct snd_pcm_runtime *runtime = substream->runtime;
820 struct snd_trident_voice *voice = runtime->private_data;
823 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
825 if (trident->tlb.entries) {
826 if (err > 0) { /* change */
828 snd_trident_free_pages(trident, voice->memblk);
829 voice->memblk = snd_trident_alloc_pages(trident, substream);
830 if (voice->memblk == NULL)
837 /*---------------------------------------------------------------------------
838 snd_trident_allocate_evoice
840 Description: Allocate extra voice as interrupt generator
842 Parameters: substream - PCM substream class
843 hw_params - hardware parameters
845 Returns: Error status
847 ---------------------------------------------------------------------------*/
849 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
850 struct snd_pcm_hw_params *hw_params)
852 struct snd_trident *trident = snd_pcm_substream_chip(substream);
853 struct snd_pcm_runtime *runtime = substream->runtime;
854 struct snd_trident_voice *voice = runtime->private_data;
855 struct snd_trident_voice *evoice = voice->extra;
857 /* voice management */
859 if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
860 if (evoice == NULL) {
861 evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
864 voice->extra = evoice;
865 evoice->substream = substream;
868 if (evoice != NULL) {
869 snd_trident_free_voice(trident, evoice);
870 voice->extra = evoice = NULL;
877 /*---------------------------------------------------------------------------
878 snd_trident_hw_params
880 Description: Set the hardware parameters for the playback device.
882 Parameters: substream - PCM substream class
883 hw_params - hardware parameters
885 Returns: Error status
887 ---------------------------------------------------------------------------*/
889 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
890 struct snd_pcm_hw_params *hw_params)
894 err = snd_trident_allocate_pcm_mem(substream, hw_params);
896 err = snd_trident_allocate_evoice(substream, hw_params);
900 /*---------------------------------------------------------------------------
901 snd_trident_playback_hw_free
903 Description: Release the hardware resources for the playback device.
905 Parameters: substream - PCM substream class
907 Returns: Error status
909 ---------------------------------------------------------------------------*/
911 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
913 struct snd_trident *trident = snd_pcm_substream_chip(substream);
914 struct snd_pcm_runtime *runtime = substream->runtime;
915 struct snd_trident_voice *voice = runtime->private_data;
916 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
918 if (trident->tlb.entries) {
919 if (voice && voice->memblk) {
920 snd_trident_free_pages(trident, voice->memblk);
921 voice->memblk = NULL;
924 snd_pcm_lib_free_pages(substream);
925 if (evoice != NULL) {
926 snd_trident_free_voice(trident, evoice);
932 /*---------------------------------------------------------------------------
933 snd_trident_playback_prepare
935 Description: Prepare playback device for playback.
937 Parameters: substream - PCM substream class
939 Returns: Error status
941 ---------------------------------------------------------------------------*/
943 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
945 struct snd_trident *trident = snd_pcm_substream_chip(substream);
946 struct snd_pcm_runtime *runtime = substream->runtime;
947 struct snd_trident_voice *voice = runtime->private_data;
948 struct snd_trident_voice *evoice = voice->extra;
949 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
951 spin_lock_irq(&trident->reg_lock);
953 /* set delta (rate) value */
954 voice->Delta = snd_trident_convert_rate(runtime->rate);
955 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
957 /* set Loop Begin Address */
959 voice->LBA = voice->memblk->offset;
961 voice->LBA = runtime->dma_addr;
964 voice->ESO = runtime->buffer_size - 1; /* in samples */
965 voice->CTRL = snd_trident_control_mode(substream);
971 voice->Vol = mix->vol;
972 voice->RVol = mix->rvol;
973 voice->CVol = mix->cvol;
974 voice->Pan = mix->pan;
975 voice->Attribute = 0;
977 voice->Attribute = (1<<(30-16))|(2<<(26-16))|
978 (0<<(24-16))|(0x1f<<(19-16));
980 voice->Attribute = 0;
983 snd_trident_write_voice_regs(trident, voice);
985 if (evoice != NULL) {
986 evoice->Delta = voice->Delta;
987 evoice->spurious_threshold = voice->spurious_threshold;
988 evoice->LBA = voice->LBA;
990 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
991 evoice->CTRL = voice->CTRL;
993 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
997 evoice->Vol = 0x3ff; /* mute */
998 evoice->RVol = evoice->CVol = 0x7f; /* mute */
999 evoice->Pan = 0x7f; /* mute */
1001 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
1002 (0<<(24-16))|(0x1f<<(19-16));
1004 evoice->Attribute = 0;
1006 snd_trident_write_voice_regs(trident, evoice);
1008 evoice->isync_mark = runtime->period_size;
1009 evoice->ESO = (runtime->period_size * 2) - 1;
1012 spin_unlock_irq(&trident->reg_lock);
1017 /*---------------------------------------------------------------------------
1018 snd_trident_capture_hw_params
1020 Description: Set the hardware parameters for the capture device.
1022 Parameters: substream - PCM substream class
1023 hw_params - hardware parameters
1025 Returns: Error status
1027 ---------------------------------------------------------------------------*/
1029 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1030 struct snd_pcm_hw_params *hw_params)
1032 return snd_trident_allocate_pcm_mem(substream, hw_params);
1035 /*---------------------------------------------------------------------------
1036 snd_trident_capture_prepare
1038 Description: Prepare capture device for playback.
1040 Parameters: substream - PCM substream class
1042 Returns: Error status
1044 ---------------------------------------------------------------------------*/
1046 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1048 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1049 struct snd_pcm_runtime *runtime = substream->runtime;
1050 struct snd_trident_voice *voice = runtime->private_data;
1051 unsigned int val, ESO_bytes;
1053 spin_lock_irq(&trident->reg_lock);
1055 // Initilize the channel and set channel Mode
1056 outb(0, TRID_REG(trident, LEGACY_DMAR15));
1058 // Set DMA channel operation mode register
1059 outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1061 // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
1062 voice->LBA = runtime->dma_addr;
1063 outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
1065 voice->LBA = voice->memblk->offset;
1068 ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
1069 outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
1070 outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
1073 // Set channel sample rate, 4.12 format
1074 val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
1075 outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
1077 // Set channel interrupt blk length
1078 if (snd_pcm_format_width(runtime->format) == 16) {
1079 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1081 val = (unsigned short) (ESO_bytes - 1);
1084 outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1086 // Right now, set format and start to run captureing,
1087 // continuous run loop enable.
1088 trident->bDMAStart = 0x19; // 0001 1001b
1090 if (snd_pcm_format_width(runtime->format) == 16)
1091 trident->bDMAStart |= 0x80;
1092 if (snd_pcm_format_signed(runtime->format))
1093 trident->bDMAStart |= 0x20;
1094 if (runtime->channels > 1)
1095 trident->bDMAStart |= 0x40;
1097 // Prepare capture intr channel
1099 voice->Delta = snd_trident_convert_rate(runtime->rate);
1100 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1102 voice->isync_mark = runtime->period_size;
1103 voice->isync_max = runtime->buffer_size;
1105 // Set voice parameters
1107 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1108 voice->CTRL = snd_trident_control_mode(substream);
1113 voice->Pan = 0x7f; /* mute */
1114 voice->Vol = 0x3ff; /* mute */
1118 voice->Attribute = 0;
1120 snd_trident_write_voice_regs(trident, voice);
1122 spin_unlock_irq(&trident->reg_lock);
1126 /*---------------------------------------------------------------------------
1127 snd_trident_si7018_capture_hw_params
1129 Description: Set the hardware parameters for the capture device.
1131 Parameters: substream - PCM substream class
1132 hw_params - hardware parameters
1134 Returns: Error status
1136 ---------------------------------------------------------------------------*/
1138 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1139 struct snd_pcm_hw_params *hw_params)
1143 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1146 return snd_trident_allocate_evoice(substream, hw_params);
1149 /*---------------------------------------------------------------------------
1150 snd_trident_si7018_capture_hw_free
1152 Description: Release the hardware resources for the capture device.
1154 Parameters: substream - PCM substream class
1156 Returns: Error status
1158 ---------------------------------------------------------------------------*/
1160 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1162 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1163 struct snd_pcm_runtime *runtime = substream->runtime;
1164 struct snd_trident_voice *voice = runtime->private_data;
1165 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1167 snd_pcm_lib_free_pages(substream);
1168 if (evoice != NULL) {
1169 snd_trident_free_voice(trident, evoice);
1170 voice->extra = NULL;
1175 /*---------------------------------------------------------------------------
1176 snd_trident_si7018_capture_prepare
1178 Description: Prepare capture device for playback.
1180 Parameters: substream - PCM substream class
1182 Returns: Error status
1184 ---------------------------------------------------------------------------*/
1186 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1188 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1189 struct snd_pcm_runtime *runtime = substream->runtime;
1190 struct snd_trident_voice *voice = runtime->private_data;
1191 struct snd_trident_voice *evoice = voice->extra;
1193 spin_lock_irq(&trident->reg_lock);
1195 voice->LBA = runtime->dma_addr;
1196 voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
1197 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1199 // Set voice parameters
1201 voice->ESO = runtime->buffer_size - 1; /* in samples */
1202 voice->CTRL = snd_trident_control_mode(substream);
1207 voice->Pan = T4D_DEFAULT_PCM_PAN;
1213 voice->Attribute = (2 << (30-16)) |
1218 snd_trident_write_voice_regs(trident, voice);
1220 if (evoice != NULL) {
1221 evoice->Delta = snd_trident_convert_rate(runtime->rate);
1222 evoice->spurious_threshold = voice->spurious_threshold;
1223 evoice->LBA = voice->LBA;
1225 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1226 evoice->CTRL = voice->CTRL;
1232 evoice->Vol = 0x3ff; /* mute */
1233 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1234 evoice->Pan = 0x7f; /* mute */
1235 evoice->Attribute = 0;
1236 snd_trident_write_voice_regs(trident, evoice);
1238 evoice->isync_mark = runtime->period_size;
1239 evoice->ESO = (runtime->period_size * 2) - 1;
1242 spin_unlock_irq(&trident->reg_lock);
1246 /*---------------------------------------------------------------------------
1247 snd_trident_foldback_prepare
1249 Description: Prepare foldback capture device for playback.
1251 Parameters: substream - PCM substream class
1253 Returns: Error status
1255 ---------------------------------------------------------------------------*/
1257 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1259 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1260 struct snd_pcm_runtime *runtime = substream->runtime;
1261 struct snd_trident_voice *voice = runtime->private_data;
1262 struct snd_trident_voice *evoice = voice->extra;
1264 spin_lock_irq(&trident->reg_lock);
1266 /* Set channel buffer Address */
1268 voice->LBA = voice->memblk->offset;
1270 voice->LBA = runtime->dma_addr;
1272 /* set target ESO for channel */
1273 voice->ESO = runtime->buffer_size - 1; /* in samples */
1275 /* set sample rate */
1276 voice->Delta = 0x1000;
1277 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1280 voice->CTRL = snd_trident_control_mode(substream);
1285 voice->Pan = 0x7f; /* mute */
1286 voice->Vol = 0x3ff; /* mute */
1290 voice->Attribute = 0;
1292 /* set up capture channel */
1293 outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1295 snd_trident_write_voice_regs(trident, voice);
1297 if (evoice != NULL) {
1298 evoice->Delta = voice->Delta;
1299 evoice->spurious_threshold = voice->spurious_threshold;
1300 evoice->LBA = voice->LBA;
1302 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1303 evoice->CTRL = voice->CTRL;
1305 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1309 evoice->Vol = 0x3ff; /* mute */
1310 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1311 evoice->Pan = 0x7f; /* mute */
1312 evoice->Attribute = 0;
1313 snd_trident_write_voice_regs(trident, evoice);
1315 evoice->isync_mark = runtime->period_size;
1316 evoice->ESO = (runtime->period_size * 2) - 1;
1319 spin_unlock_irq(&trident->reg_lock);
1323 /*---------------------------------------------------------------------------
1324 snd_trident_spdif_hw_params
1326 Description: Set the hardware parameters for the spdif device.
1328 Parameters: substream - PCM substream class
1329 hw_params - hardware parameters
1331 Returns: Error status
1333 ---------------------------------------------------------------------------*/
1335 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1336 struct snd_pcm_hw_params *hw_params)
1338 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1339 unsigned int old_bits = 0, change = 0;
1342 err = snd_trident_allocate_pcm_mem(substream, hw_params);
1346 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1347 err = snd_trident_allocate_evoice(substream, hw_params);
1352 /* prepare SPDIF channel */
1353 spin_lock_irq(&trident->reg_lock);
1354 old_bits = trident->spdif_pcm_bits;
1355 if (old_bits & IEC958_AES0_PROFESSIONAL)
1356 trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
1358 trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
1359 if (params_rate(hw_params) >= 48000) {
1360 trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
1361 trident->spdif_pcm_bits |=
1362 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1363 IEC958_AES0_PRO_FS_48000 :
1364 (IEC958_AES3_CON_FS_48000 << 24);
1366 else if (params_rate(hw_params) >= 44100) {
1367 trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
1368 trident->spdif_pcm_bits |=
1369 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1370 IEC958_AES0_PRO_FS_44100 :
1371 (IEC958_AES3_CON_FS_44100 << 24);
1374 trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
1375 trident->spdif_pcm_bits |=
1376 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1377 IEC958_AES0_PRO_FS_32000 :
1378 (IEC958_AES3_CON_FS_32000 << 24);
1380 change = old_bits != trident->spdif_pcm_bits;
1381 spin_unlock_irq(&trident->reg_lock);
1384 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1389 /*---------------------------------------------------------------------------
1390 snd_trident_spdif_prepare
1392 Description: Prepare SPDIF device for playback.
1394 Parameters: substream - PCM substream class
1396 Returns: Error status
1398 ---------------------------------------------------------------------------*/
1400 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1402 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1403 struct snd_pcm_runtime *runtime = substream->runtime;
1404 struct snd_trident_voice *voice = runtime->private_data;
1405 struct snd_trident_voice *evoice = voice->extra;
1406 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1407 unsigned int RESO, LBAO;
1410 spin_lock_irq(&trident->reg_lock);
1412 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1414 /* set delta (rate) value */
1415 voice->Delta = snd_trident_convert_rate(runtime->rate);
1416 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1418 /* set Loop Back Address */
1419 LBAO = runtime->dma_addr;
1421 voice->LBA = voice->memblk->offset;
1427 voice->isync_mark = runtime->period_size;
1428 voice->isync_max = runtime->buffer_size;
1430 /* set target ESO for channel */
1431 RESO = runtime->buffer_size - 1;
1432 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1435 voice->CTRL = snd_trident_control_mode(substream);
1447 voice->Attribute = 0;
1449 /* prepare surrogate IRQ channel */
1450 snd_trident_write_voice_regs(trident, voice);
1452 outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
1453 outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
1454 outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
1455 outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
1456 outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
1458 /* set SPDIF setting */
1459 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1460 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1464 /* set delta (rate) value */
1465 voice->Delta = 0x800;
1466 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1468 /* set Loop Begin Address */
1470 voice->LBA = voice->memblk->offset;
1472 voice->LBA = runtime->dma_addr;
1475 voice->ESO = runtime->buffer_size - 1; /* in samples */
1476 voice->CTRL = snd_trident_control_mode(substream);
1482 voice->Vol = mix->vol;
1483 voice->RVol = mix->rvol;
1484 voice->CVol = mix->cvol;
1485 voice->Pan = mix->pan;
1486 voice->Attribute = (1<<(30-16))|(7<<(26-16))|
1487 (0<<(24-16))|(0<<(19-16));
1489 snd_trident_write_voice_regs(trident, voice);
1491 if (evoice != NULL) {
1492 evoice->Delta = voice->Delta;
1493 evoice->spurious_threshold = voice->spurious_threshold;
1494 evoice->LBA = voice->LBA;
1496 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1497 evoice->CTRL = voice->CTRL;
1499 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1503 evoice->Vol = 0x3ff; /* mute */
1504 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1505 evoice->Pan = 0x7f; /* mute */
1506 evoice->Attribute = 0;
1507 snd_trident_write_voice_regs(trident, evoice);
1509 evoice->isync_mark = runtime->period_size;
1510 evoice->ESO = (runtime->period_size * 2) - 1;
1513 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1514 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1516 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1517 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1519 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1522 spin_unlock_irq(&trident->reg_lock);
1527 /*---------------------------------------------------------------------------
1530 Description: Start/stop devices
1532 Parameters: substream - PCM substream class
1533 cmd - trigger command (STOP, GO)
1535 Returns: Error status
1537 ---------------------------------------------------------------------------*/
1539 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1543 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1544 struct list_head *pos;
1545 struct snd_pcm_substream *s;
1546 unsigned int what, whati, capture_flag, spdif_flag;
1547 struct snd_trident_voice *voice, *evoice;
1548 unsigned int val, go;
1551 case SNDRV_PCM_TRIGGER_START:
1552 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1553 case SNDRV_PCM_TRIGGER_RESUME:
1556 case SNDRV_PCM_TRIGGER_STOP:
1557 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1558 case SNDRV_PCM_TRIGGER_SUSPEND:
1564 what = whati = capture_flag = spdif_flag = 0;
1565 spin_lock(&trident->reg_lock);
1566 val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
1567 snd_pcm_group_for_each(pos, substream) {
1568 s = snd_pcm_group_substream_entry(pos);
1569 if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
1570 voice = s->runtime->private_data;
1571 evoice = voice->extra;
1572 what |= 1 << (voice->number & 0x1f);
1573 if (evoice == NULL) {
1574 whati |= 1 << (voice->number & 0x1f);
1576 what |= 1 << (evoice->number & 0x1f);
1577 whati |= 1 << (evoice->number & 0x1f);
1579 evoice->stimer = val;
1583 voice->stimer = val;
1587 snd_pcm_trigger_done(s, substream);
1595 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1596 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1597 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1599 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1600 val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
1601 outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1605 outl(what, TRID_REG(trident, T4D_STOP_B));
1606 val = inl(TRID_REG(trident, T4D_AINTEN_B));
1612 outl(val, TRID_REG(trident, T4D_AINTEN_B));
1614 outl(what, TRID_REG(trident, T4D_START_B));
1616 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1617 outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1619 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1620 outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1622 spin_unlock(&trident->reg_lock);
1626 /*---------------------------------------------------------------------------
1627 snd_trident_playback_pointer
1629 Description: This routine return the playback position
1631 Parameters: substream - PCM substream class
1633 Returns: position of buffer
1635 ---------------------------------------------------------------------------*/
1637 static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1639 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1640 struct snd_pcm_runtime *runtime = substream->runtime;
1641 struct snd_trident_voice *voice = runtime->private_data;
1644 if (!voice->running)
1647 spin_lock(&trident->reg_lock);
1649 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
1651 if (trident->device != TRIDENT_DEVICE_ID_NX) {
1652 cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
1653 } else { // ID_4DWAVE_NX
1654 cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
1657 spin_unlock(&trident->reg_lock);
1659 if (cso >= runtime->buffer_size)
1665 /*---------------------------------------------------------------------------
1666 snd_trident_capture_pointer
1668 Description: This routine return the capture position
1670 Paramters: pcm1 - PCM device class
1672 Returns: position of buffer
1674 ---------------------------------------------------------------------------*/
1676 static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1678 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1679 struct snd_pcm_runtime *runtime = substream->runtime;
1680 struct snd_trident_voice *voice = runtime->private_data;
1681 unsigned int result;
1683 if (!voice->running)
1686 result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
1687 if (runtime->channels > 1)
1690 result = runtime->buffer_size - result;
1695 /*---------------------------------------------------------------------------
1696 snd_trident_spdif_pointer
1698 Description: This routine return the SPDIF playback position
1700 Parameters: substream - PCM substream class
1702 Returns: position of buffer
1704 ---------------------------------------------------------------------------*/
1706 static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1708 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1709 struct snd_pcm_runtime *runtime = substream->runtime;
1710 struct snd_trident_voice *voice = runtime->private_data;
1711 unsigned int result;
1713 if (!voice->running)
1716 result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
1722 * Playback support device description
1725 static struct snd_pcm_hardware snd_trident_playback =
1727 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1728 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1729 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1730 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1731 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1732 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1733 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1738 .buffer_bytes_max = (256*1024),
1739 .period_bytes_min = 64,
1740 .period_bytes_max = (256*1024),
1742 .periods_max = 1024,
1747 * Capture support device description
1750 static struct snd_pcm_hardware snd_trident_capture =
1752 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1753 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1754 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1755 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1756 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1757 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1758 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1763 .buffer_bytes_max = (128*1024),
1764 .period_bytes_min = 64,
1765 .period_bytes_max = (128*1024),
1767 .periods_max = 1024,
1772 * Foldback capture support device description
1775 static struct snd_pcm_hardware snd_trident_foldback =
1777 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1778 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1779 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1780 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1781 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1782 .rates = SNDRV_PCM_RATE_48000,
1787 .buffer_bytes_max = (128*1024),
1788 .period_bytes_min = 64,
1789 .period_bytes_max = (128*1024),
1791 .periods_max = 1024,
1796 * SPDIF playback support device description
1799 static struct snd_pcm_hardware snd_trident_spdif =
1801 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1802 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1803 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1804 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1805 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1806 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1807 SNDRV_PCM_RATE_48000),
1812 .buffer_bytes_max = (128*1024),
1813 .period_bytes_min = 64,
1814 .period_bytes_max = (128*1024),
1816 .periods_max = 1024,
1820 static struct snd_pcm_hardware snd_trident_spdif_7018 =
1822 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1823 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1824 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1825 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1826 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1827 .rates = SNDRV_PCM_RATE_48000,
1832 .buffer_bytes_max = (128*1024),
1833 .period_bytes_min = 64,
1834 .period_bytes_max = (128*1024),
1836 .periods_max = 1024,
1840 static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1842 struct snd_trident_voice *voice = runtime->private_data;
1843 struct snd_trident *trident;
1846 trident = voice->trident;
1847 snd_trident_free_voice(trident, voice);
1851 static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1853 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1854 struct snd_pcm_runtime *runtime = substream->runtime;
1855 struct snd_trident_voice *voice;
1857 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1860 snd_trident_pcm_mixer_build(trident, voice, substream);
1861 voice->substream = substream;
1862 runtime->private_data = voice;
1863 runtime->private_free = snd_trident_pcm_free_substream;
1864 runtime->hw = snd_trident_playback;
1865 snd_pcm_set_sync(substream);
1866 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1870 /*---------------------------------------------------------------------------
1871 snd_trident_playback_close
1873 Description: This routine will close the 4DWave playback device. For now
1874 we will simply free the dma transfer buffer.
1876 Parameters: substream - PCM substream class
1878 ---------------------------------------------------------------------------*/
1879 static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1881 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1882 struct snd_pcm_runtime *runtime = substream->runtime;
1883 struct snd_trident_voice *voice = runtime->private_data;
1885 snd_trident_pcm_mixer_free(trident, voice, substream);
1889 /*---------------------------------------------------------------------------
1890 snd_trident_spdif_open
1892 Description: This routine will open the 4DWave SPDIF device.
1894 Parameters: substream - PCM substream class
1896 Returns: status - success or failure flag
1898 ---------------------------------------------------------------------------*/
1900 static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1902 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1903 struct snd_trident_voice *voice;
1904 struct snd_pcm_runtime *runtime = substream->runtime;
1906 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1910 voice->substream = substream;
1911 spin_lock_irq(&trident->reg_lock);
1912 trident->spdif_pcm_bits = trident->spdif_bits;
1913 spin_unlock_irq(&trident->reg_lock);
1915 runtime->private_data = voice;
1916 runtime->private_free = snd_trident_pcm_free_substream;
1917 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1918 runtime->hw = snd_trident_spdif;
1920 runtime->hw = snd_trident_spdif_7018;
1923 trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1924 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1925 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1927 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1932 /*---------------------------------------------------------------------------
1933 snd_trident_spdif_close
1935 Description: This routine will close the 4DWave SPDIF device.
1937 Parameters: substream - PCM substream class
1939 ---------------------------------------------------------------------------*/
1941 static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1943 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1946 spin_lock_irq(&trident->reg_lock);
1947 // restore default SPDIF setting
1948 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1949 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1950 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
1952 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
1953 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1954 if (trident->spdif_ctrl) {
1959 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1961 spin_unlock_irq(&trident->reg_lock);
1962 trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1963 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1964 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1968 /*---------------------------------------------------------------------------
1969 snd_trident_capture_open
1971 Description: This routine will open the 4DWave capture device.
1973 Parameters: substream - PCM substream class
1975 Returns: status - success or failure flag
1977 ---------------------------------------------------------------------------*/
1979 static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1981 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1982 struct snd_trident_voice *voice;
1983 struct snd_pcm_runtime *runtime = substream->runtime;
1985 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1989 voice->substream = substream;
1990 runtime->private_data = voice;
1991 runtime->private_free = snd_trident_pcm_free_substream;
1992 runtime->hw = snd_trident_capture;
1993 snd_pcm_set_sync(substream);
1994 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1998 /*---------------------------------------------------------------------------
1999 snd_trident_capture_close
2001 Description: This routine will close the 4DWave capture device. For now
2002 we will simply free the dma transfer buffer.
2004 Parameters: substream - PCM substream class
2006 ---------------------------------------------------------------------------*/
2007 static int snd_trident_capture_close(struct snd_pcm_substream *substream)
2012 /*---------------------------------------------------------------------------
2013 snd_trident_foldback_open
2015 Description: This routine will open the 4DWave foldback capture device.
2017 Parameters: substream - PCM substream class
2019 Returns: status - success or failure flag
2021 ---------------------------------------------------------------------------*/
2023 static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
2025 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2026 struct snd_trident_voice *voice;
2027 struct snd_pcm_runtime *runtime = substream->runtime;
2029 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
2032 voice->foldback_chan = substream->number;
2033 voice->substream = substream;
2034 runtime->private_data = voice;
2035 runtime->private_free = snd_trident_pcm_free_substream;
2036 runtime->hw = snd_trident_foldback;
2037 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
2041 /*---------------------------------------------------------------------------
2042 snd_trident_foldback_close
2044 Description: This routine will close the 4DWave foldback capture device.
2045 For now we will simply free the dma transfer buffer.
2047 Parameters: substream - PCM substream class
2049 ---------------------------------------------------------------------------*/
2050 static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
2052 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2053 struct snd_trident_voice *voice;
2054 struct snd_pcm_runtime *runtime = substream->runtime;
2055 voice = runtime->private_data;
2057 /* stop capture channel */
2058 spin_lock_irq(&trident->reg_lock);
2059 outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
2060 spin_unlock_irq(&trident->reg_lock);
2064 /*---------------------------------------------------------------------------
2066 ---------------------------------------------------------------------------*/
2068 static struct snd_pcm_ops snd_trident_playback_ops = {
2069 .open = snd_trident_playback_open,
2070 .close = snd_trident_playback_close,
2071 .ioctl = snd_trident_ioctl,
2072 .hw_params = snd_trident_hw_params,
2073 .hw_free = snd_trident_hw_free,
2074 .prepare = snd_trident_playback_prepare,
2075 .trigger = snd_trident_trigger,
2076 .pointer = snd_trident_playback_pointer,
2079 static struct snd_pcm_ops snd_trident_nx_playback_ops = {
2080 .open = snd_trident_playback_open,
2081 .close = snd_trident_playback_close,
2082 .ioctl = snd_trident_ioctl,
2083 .hw_params = snd_trident_hw_params,
2084 .hw_free = snd_trident_hw_free,
2085 .prepare = snd_trident_playback_prepare,
2086 .trigger = snd_trident_trigger,
2087 .pointer = snd_trident_playback_pointer,
2088 .page = snd_pcm_sgbuf_ops_page,
2091 static struct snd_pcm_ops snd_trident_capture_ops = {
2092 .open = snd_trident_capture_open,
2093 .close = snd_trident_capture_close,
2094 .ioctl = snd_trident_ioctl,
2095 .hw_params = snd_trident_capture_hw_params,
2096 .hw_free = snd_trident_hw_free,
2097 .prepare = snd_trident_capture_prepare,
2098 .trigger = snd_trident_trigger,
2099 .pointer = snd_trident_capture_pointer,
2102 static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
2103 .open = snd_trident_capture_open,
2104 .close = snd_trident_capture_close,
2105 .ioctl = snd_trident_ioctl,
2106 .hw_params = snd_trident_si7018_capture_hw_params,
2107 .hw_free = snd_trident_si7018_capture_hw_free,
2108 .prepare = snd_trident_si7018_capture_prepare,
2109 .trigger = snd_trident_trigger,
2110 .pointer = snd_trident_playback_pointer,
2113 static struct snd_pcm_ops snd_trident_foldback_ops = {
2114 .open = snd_trident_foldback_open,
2115 .close = snd_trident_foldback_close,
2116 .ioctl = snd_trident_ioctl,
2117 .hw_params = snd_trident_hw_params,
2118 .hw_free = snd_trident_hw_free,
2119 .prepare = snd_trident_foldback_prepare,
2120 .trigger = snd_trident_trigger,
2121 .pointer = snd_trident_playback_pointer,
2124 static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
2125 .open = snd_trident_foldback_open,
2126 .close = snd_trident_foldback_close,
2127 .ioctl = snd_trident_ioctl,
2128 .hw_params = snd_trident_hw_params,
2129 .hw_free = snd_trident_hw_free,
2130 .prepare = snd_trident_foldback_prepare,
2131 .trigger = snd_trident_trigger,
2132 .pointer = snd_trident_playback_pointer,
2133 .page = snd_pcm_sgbuf_ops_page,
2136 static struct snd_pcm_ops snd_trident_spdif_ops = {
2137 .open = snd_trident_spdif_open,
2138 .close = snd_trident_spdif_close,
2139 .ioctl = snd_trident_ioctl,
2140 .hw_params = snd_trident_spdif_hw_params,
2141 .hw_free = snd_trident_hw_free,
2142 .prepare = snd_trident_spdif_prepare,
2143 .trigger = snd_trident_trigger,
2144 .pointer = snd_trident_spdif_pointer,
2147 static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
2148 .open = snd_trident_spdif_open,
2149 .close = snd_trident_spdif_close,
2150 .ioctl = snd_trident_ioctl,
2151 .hw_params = snd_trident_spdif_hw_params,
2152 .hw_free = snd_trident_hw_free,
2153 .prepare = snd_trident_spdif_prepare,
2154 .trigger = snd_trident_trigger,
2155 .pointer = snd_trident_playback_pointer,
2158 /*---------------------------------------------------------------------------
2161 Description: This routine registers the 4DWave device for PCM support.
2163 Paramters: trident - pointer to target device class for 4DWave.
2167 ---------------------------------------------------------------------------*/
2169 int __devinit snd_trident_pcm(struct snd_trident * trident,
2170 int device, struct snd_pcm ** rpcm)
2172 struct snd_pcm *pcm;
2177 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
2180 pcm->private_data = trident;
2182 if (trident->tlb.entries) {
2183 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
2185 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
2187 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2188 trident->device != TRIDENT_DEVICE_ID_SI7018 ?
2189 &snd_trident_capture_ops :
2190 &snd_trident_si7018_capture_ops);
2192 pcm->info_flags = 0;
2193 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
2194 strcpy(pcm->name, "Trident 4DWave");
2197 if (trident->tlb.entries) {
2198 struct snd_pcm_substream *substream;
2199 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
2200 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
2201 snd_dma_pci_data(trident->pci),
2203 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2204 SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
2207 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2208 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2216 /*---------------------------------------------------------------------------
2217 snd_trident_foldback_pcm
2219 Description: This routine registers the 4DWave device for foldback PCM support.
2221 Paramters: trident - pointer to target device class for 4DWave.
2225 ---------------------------------------------------------------------------*/
2227 int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
2228 int device, struct snd_pcm ** rpcm)
2230 struct snd_pcm *foldback;
2233 struct snd_pcm_substream *substream;
2237 if (trident->device == TRIDENT_DEVICE_ID_NX)
2239 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
2242 foldback->private_data = trident;
2243 if (trident->tlb.entries)
2244 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
2246 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
2247 foldback->info_flags = 0;
2248 strcpy(foldback->name, "Trident 4DWave");
2249 substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2250 strcpy(substream->name, "Front Mixer");
2251 substream = substream->next;
2252 strcpy(substream->name, "Reverb Mixer");
2253 substream = substream->next;
2254 strcpy(substream->name, "Chorus Mixer");
2255 if (num_chan == 4) {
2256 substream = substream->next;
2257 strcpy(substream->name, "Second AC'97 ADC");
2259 trident->foldback = foldback;
2261 if (trident->tlb.entries)
2262 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
2263 snd_dma_pci_data(trident->pci), 0, 128*1024);
2265 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
2266 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2273 /*---------------------------------------------------------------------------
2276 Description: This routine registers the 4DWave-NX device for SPDIF support.
2278 Paramters: trident - pointer to target device class for 4DWave-NX.
2282 ---------------------------------------------------------------------------*/
2284 int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
2285 int device, struct snd_pcm ** rpcm)
2287 struct snd_pcm *spdif;
2292 if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
2295 spdif->private_data = trident;
2296 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2297 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
2299 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
2301 spdif->info_flags = 0;
2302 strcpy(spdif->name, "Trident 4DWave IEC958");
2303 trident->spdif = spdif;
2305 snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2317 /*---------------------------------------------------------------------------
2318 snd_trident_spdif_control
2320 Description: enable/disable S/PDIF out from ac97 mixer
2321 ---------------------------------------------------------------------------*/
2323 static int snd_trident_spdif_control_info(struct snd_kcontrol *kcontrol,
2324 struct snd_ctl_elem_info *uinfo)
2326 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2328 uinfo->value.integer.min = 0;
2329 uinfo->value.integer.max = 1;
2333 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2336 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2339 spin_lock_irq(&trident->reg_lock);
2340 val = trident->spdif_ctrl;
2341 ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2342 spin_unlock_irq(&trident->reg_lock);
2346 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2347 struct snd_ctl_elem_value *ucontrol)
2349 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2353 val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2354 spin_lock_irq(&trident->reg_lock);
2355 /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2356 change = trident->spdif_ctrl != val;
2357 trident->spdif_ctrl = val;
2358 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2359 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2360 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2361 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2364 if (trident->spdif == NULL) {
2366 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2367 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2370 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2373 spin_unlock_irq(&trident->reg_lock);
2377 static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
2379 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2380 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2381 .info = snd_trident_spdif_control_info,
2382 .get = snd_trident_spdif_control_get,
2383 .put = snd_trident_spdif_control_put,
2384 .private_value = 0x28,
2387 /*---------------------------------------------------------------------------
2388 snd_trident_spdif_default
2390 Description: put/get the S/PDIF default settings
2391 ---------------------------------------------------------------------------*/
2393 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_info *uinfo)
2396 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2401 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2404 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2406 spin_lock_irq(&trident->reg_lock);
2407 ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2408 ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2409 ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2410 ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2411 spin_unlock_irq(&trident->reg_lock);
2415 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
2418 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2422 val = (ucontrol->value.iec958.status[0] << 0) |
2423 (ucontrol->value.iec958.status[1] << 8) |
2424 (ucontrol->value.iec958.status[2] << 16) |
2425 (ucontrol->value.iec958.status[3] << 24);
2426 spin_lock_irq(&trident->reg_lock);
2427 change = trident->spdif_bits != val;
2428 trident->spdif_bits = val;
2429 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2430 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2431 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2433 if (trident->spdif == NULL)
2434 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2436 spin_unlock_irq(&trident->reg_lock);
2440 static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
2442 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2443 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2444 .info = snd_trident_spdif_default_info,
2445 .get = snd_trident_spdif_default_get,
2446 .put = snd_trident_spdif_default_put
2449 /*---------------------------------------------------------------------------
2450 snd_trident_spdif_mask
2452 Description: put/get the S/PDIF mask
2453 ---------------------------------------------------------------------------*/
2455 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_info *uinfo)
2458 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2463 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
2466 ucontrol->value.iec958.status[0] = 0xff;
2467 ucontrol->value.iec958.status[1] = 0xff;
2468 ucontrol->value.iec958.status[2] = 0xff;
2469 ucontrol->value.iec958.status[3] = 0xff;
2473 static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
2475 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2476 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2477 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2478 .info = snd_trident_spdif_mask_info,
2479 .get = snd_trident_spdif_mask_get,
2482 /*---------------------------------------------------------------------------
2483 snd_trident_spdif_stream
2485 Description: put/get the S/PDIF stream settings
2486 ---------------------------------------------------------------------------*/
2488 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_info *uinfo)
2491 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2496 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2497 struct snd_ctl_elem_value *ucontrol)
2499 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2501 spin_lock_irq(&trident->reg_lock);
2502 ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2503 ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2504 ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2505 ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2506 spin_unlock_irq(&trident->reg_lock);
2510 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_value *ucontrol)
2513 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2517 val = (ucontrol->value.iec958.status[0] << 0) |
2518 (ucontrol->value.iec958.status[1] << 8) |
2519 (ucontrol->value.iec958.status[2] << 16) |
2520 (ucontrol->value.iec958.status[3] << 24);
2521 spin_lock_irq(&trident->reg_lock);
2522 change = trident->spdif_pcm_bits != val;
2523 trident->spdif_pcm_bits = val;
2524 if (trident->spdif != NULL) {
2525 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2526 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2528 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2531 spin_unlock_irq(&trident->reg_lock);
2535 static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
2537 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2538 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2539 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2540 .info = snd_trident_spdif_stream_info,
2541 .get = snd_trident_spdif_stream_get,
2542 .put = snd_trident_spdif_stream_put
2545 /*---------------------------------------------------------------------------
2546 snd_trident_ac97_control
2548 Description: enable/disable rear path for ac97
2549 ---------------------------------------------------------------------------*/
2551 static int snd_trident_ac97_control_info(struct snd_kcontrol *kcontrol,
2552 struct snd_ctl_elem_info *uinfo)
2554 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2556 uinfo->value.integer.min = 0;
2557 uinfo->value.integer.max = 1;
2561 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2562 struct snd_ctl_elem_value *ucontrol)
2564 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2567 spin_lock_irq(&trident->reg_lock);
2568 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2569 ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2570 spin_unlock_irq(&trident->reg_lock);
2574 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2575 struct snd_ctl_elem_value *ucontrol)
2577 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2581 spin_lock_irq(&trident->reg_lock);
2582 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2583 val &= ~(1 << kcontrol->private_value);
2584 if (ucontrol->value.integer.value[0])
2585 val |= 1 << kcontrol->private_value;
2586 change = val != trident->ac97_ctrl;
2587 trident->ac97_ctrl = val;
2588 outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2589 spin_unlock_irq(&trident->reg_lock);
2593 static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
2595 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2596 .name = "Rear Path",
2597 .info = snd_trident_ac97_control_info,
2598 .get = snd_trident_ac97_control_get,
2599 .put = snd_trident_ac97_control_put,
2603 /*---------------------------------------------------------------------------
2604 snd_trident_vol_control
2606 Description: wave & music volume control
2607 ---------------------------------------------------------------------------*/
2609 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2610 struct snd_ctl_elem_info *uinfo)
2612 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2614 uinfo->value.integer.min = 0;
2615 uinfo->value.integer.max = 255;
2619 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2622 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2625 val = trident->musicvol_wavevol;
2626 ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2627 ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2631 static DECLARE_TLV_DB_SCALE(db_scale_gvol, -6375, 25, 0);
2633 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2636 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2640 spin_lock_irq(&trident->reg_lock);
2641 val = trident->musicvol_wavevol;
2642 val &= ~(0xffff << kcontrol->private_value);
2643 val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2644 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2645 change = val != trident->musicvol_wavevol;
2646 outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2647 spin_unlock_irq(&trident->reg_lock);
2651 static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
2653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2654 .name = "Music Playback Volume",
2655 .info = snd_trident_vol_control_info,
2656 .get = snd_trident_vol_control_get,
2657 .put = snd_trident_vol_control_put,
2658 .private_value = 16,
2659 .tlv = { .p = db_scale_gvol },
2662 static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
2664 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2665 .name = "Wave Playback Volume",
2666 .info = snd_trident_vol_control_info,
2667 .get = snd_trident_vol_control_get,
2668 .put = snd_trident_vol_control_put,
2670 .tlv = { .p = db_scale_gvol },
2673 /*---------------------------------------------------------------------------
2674 snd_trident_pcm_vol_control
2676 Description: PCM front volume control
2677 ---------------------------------------------------------------------------*/
2679 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_info *uinfo)
2682 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2684 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2686 uinfo->value.integer.min = 0;
2687 uinfo->value.integer.max = 255;
2688 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2689 uinfo->value.integer.max = 1023;
2693 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_value *ucontrol)
2696 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2697 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2699 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2700 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2702 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2707 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_value *ucontrol)
2710 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2711 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2715 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2716 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2718 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2720 spin_lock_irq(&trident->reg_lock);
2721 change = val != mix->vol;
2723 if (mix->voice != NULL)
2724 snd_trident_write_vol_reg(trident, mix->voice, val);
2725 spin_unlock_irq(&trident->reg_lock);
2729 static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
2731 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2732 .name = "PCM Front Playback Volume",
2733 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2735 .info = snd_trident_pcm_vol_control_info,
2736 .get = snd_trident_pcm_vol_control_get,
2737 .put = snd_trident_pcm_vol_control_put,
2738 /* FIXME: no tlv yet */
2741 /*---------------------------------------------------------------------------
2742 snd_trident_pcm_pan_control
2744 Description: PCM front pan control
2745 ---------------------------------------------------------------------------*/
2747 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_info *uinfo)
2750 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2752 uinfo->value.integer.min = 0;
2753 uinfo->value.integer.max = 127;
2757 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2760 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2761 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2763 ucontrol->value.integer.value[0] = mix->pan;
2764 if (ucontrol->value.integer.value[0] & 0x40) {
2765 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2767 ucontrol->value.integer.value[0] |= 0x40;
2772 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2773 struct snd_ctl_elem_value *ucontrol)
2775 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2776 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2780 if (ucontrol->value.integer.value[0] & 0x40)
2781 val = ucontrol->value.integer.value[0] & 0x3f;
2783 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2784 spin_lock_irq(&trident->reg_lock);
2785 change = val != mix->pan;
2787 if (mix->voice != NULL)
2788 snd_trident_write_pan_reg(trident, mix->voice, val);
2789 spin_unlock_irq(&trident->reg_lock);
2793 static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
2795 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2796 .name = "PCM Pan Playback Control",
2797 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2799 .info = snd_trident_pcm_pan_control_info,
2800 .get = snd_trident_pcm_pan_control_get,
2801 .put = snd_trident_pcm_pan_control_put,
2804 /*---------------------------------------------------------------------------
2805 snd_trident_pcm_rvol_control
2807 Description: PCM reverb volume control
2808 ---------------------------------------------------------------------------*/
2810 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_info *uinfo)
2813 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2815 uinfo->value.integer.min = 0;
2816 uinfo->value.integer.max = 127;
2820 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2821 struct snd_ctl_elem_value *ucontrol)
2823 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2824 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2826 ucontrol->value.integer.value[0] = 127 - mix->rvol;
2830 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
2833 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2834 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2838 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2839 spin_lock_irq(&trident->reg_lock);
2840 change = val != mix->rvol;
2842 if (mix->voice != NULL)
2843 snd_trident_write_rvol_reg(trident, mix->voice, val);
2844 spin_unlock_irq(&trident->reg_lock);
2848 static DECLARE_TLV_DB_SCALE(db_scale_crvol, -3175, 25, 1);
2850 static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
2852 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2853 .name = "PCM Reverb Playback Volume",
2854 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2856 .info = snd_trident_pcm_rvol_control_info,
2857 .get = snd_trident_pcm_rvol_control_get,
2858 .put = snd_trident_pcm_rvol_control_put,
2859 .tlv = { .p = db_scale_crvol },
2862 /*---------------------------------------------------------------------------
2863 snd_trident_pcm_cvol_control
2865 Description: PCM chorus volume control
2866 ---------------------------------------------------------------------------*/
2868 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_info *uinfo)
2871 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2873 uinfo->value.integer.min = 0;
2874 uinfo->value.integer.max = 127;
2878 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2879 struct snd_ctl_elem_value *ucontrol)
2881 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2882 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2884 ucontrol->value.integer.value[0] = 127 - mix->cvol;
2888 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2889 struct snd_ctl_elem_value *ucontrol)
2891 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2892 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2896 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2897 spin_lock_irq(&trident->reg_lock);
2898 change = val != mix->cvol;
2900 if (mix->voice != NULL)
2901 snd_trident_write_cvol_reg(trident, mix->voice, val);
2902 spin_unlock_irq(&trident->reg_lock);
2906 static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
2908 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2909 .name = "PCM Chorus Playback Volume",
2910 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2912 .info = snd_trident_pcm_cvol_control_info,
2913 .get = snd_trident_pcm_cvol_control_get,
2914 .put = snd_trident_pcm_cvol_control_put,
2915 .tlv = { .p = db_scale_crvol },
2918 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2919 struct snd_kcontrol *kctl,
2920 int num, int activate)
2922 struct snd_ctl_elem_id id;
2927 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2929 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2930 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2931 SNDRV_CTL_EVENT_MASK_INFO,
2932 snd_ctl_build_ioff(&id, kctl, num));
2935 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2936 struct snd_trident_pcm_mixer *tmix,
2937 int num, int activate)
2939 snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2940 snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2941 snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2942 snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2945 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2946 struct snd_trident_voice *voice,
2947 struct snd_pcm_substream *substream)
2949 struct snd_trident_pcm_mixer *tmix;
2951 snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
2952 tmix = &trident->pcm_mixer[substream->number];
2953 tmix->voice = voice;
2954 tmix->vol = T4D_DEFAULT_PCM_VOL;
2955 tmix->pan = T4D_DEFAULT_PCM_PAN;
2956 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2957 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2958 snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2962 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2964 struct snd_trident_pcm_mixer *tmix;
2966 snd_assert(trident != NULL && substream != NULL, return -EINVAL);
2967 tmix = &trident->pcm_mixer[substream->number];
2969 snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2973 /*---------------------------------------------------------------------------
2976 Description: This routine registers the 4DWave device for mixer support.
2978 Paramters: trident - pointer to target device class for 4DWave.
2982 ---------------------------------------------------------------------------*/
2984 static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
2986 struct snd_ac97_template _ac97;
2987 struct snd_card *card = trident->card;
2988 struct snd_kcontrol *kctl;
2989 struct snd_ctl_elem_value *uctl;
2990 int idx, err, retries = 2;
2991 static struct snd_ac97_bus_ops ops = {
2992 .write = snd_trident_codec_write,
2993 .read = snd_trident_codec_read,
2996 uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
3000 if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
3003 memset(&_ac97, 0, sizeof(_ac97));
3004 _ac97.private_data = trident;
3005 trident->ac97_detect = 1;
3008 if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
3009 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3010 if ((err = snd_trident_sis_reset(trident)) < 0)
3019 /* secondary codec? */
3020 if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
3021 (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
3023 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
3025 snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
3026 #if 0 // only for my testing purpose --jk
3028 struct snd_ac97 *mc97;
3029 err = snd_ac97_modem(trident->card, &_ac97, &mc97);
3031 snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
3036 trident->ac97_detect = 0;
3038 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3039 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3041 kctl->put(kctl, uctl);
3042 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3044 kctl->put(kctl, uctl);
3045 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3047 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3050 for (idx = 0; idx < 32; idx++) {
3051 struct snd_trident_pcm_mixer *tmix;
3053 tmix = &trident->pcm_mixer[idx];
3056 if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3058 if ((err = snd_ctl_add(card, trident->ctl_vol)))
3061 if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3063 if ((err = snd_ctl_add(card, trident->ctl_pan)))
3066 if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3068 if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3071 if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3073 if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3076 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3077 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3079 kctl->put(kctl, uctl);
3081 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3083 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3088 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3090 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3092 idx = kctl->id.index;
3093 if ((err = snd_ctl_add(card, kctl)) < 0)
3095 kctl->put(kctl, uctl);
3097 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3102 kctl->id.index = idx;
3103 kctl->id.device = pcm_spdif_device;
3104 if ((err = snd_ctl_add(card, kctl)) < 0)
3107 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3112 kctl->id.index = idx;
3113 kctl->id.device = pcm_spdif_device;
3114 if ((err = snd_ctl_add(card, kctl)) < 0)
3117 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3122 kctl->id.index = idx;
3123 kctl->id.device = pcm_spdif_device;
3124 if ((err = snd_ctl_add(card, kctl)) < 0)
3126 trident->spdif_pcm_ctl = kctl;
3142 * gameport interface
3145 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
3147 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3149 struct snd_trident *chip = gameport_get_port_data(gameport);
3151 snd_assert(chip, return 0);
3152 return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3155 static void snd_trident_gameport_trigger(struct gameport *gameport)
3157 struct snd_trident *chip = gameport_get_port_data(gameport);
3159 snd_assert(chip, return);
3160 outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3163 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3165 struct snd_trident *chip = gameport_get_port_data(gameport);
3168 snd_assert(chip, return 0);
3170 *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3172 for (i = 0; i < 4; i++) {
3173 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3174 if (axes[i] == 0xffff) axes[i] = -1;
3180 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3182 struct snd_trident *chip = gameport_get_port_data(gameport);
3184 snd_assert(chip, return 0);
3187 case GAMEPORT_MODE_COOKED:
3188 outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3191 case GAMEPORT_MODE_RAW:
3192 outb(0, TRID_REG(chip, GAMEPORT_GCR));
3199 int __devinit snd_trident_create_gameport(struct snd_trident *chip)
3201 struct gameport *gp;
3203 chip->gameport = gp = gameport_allocate_port();
3205 printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
3209 gameport_set_name(gp, "Trident 4DWave");
3210 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3211 gameport_set_dev_parent(gp, &chip->pci->dev);
3213 gameport_set_port_data(gp, chip);
3215 gp->read = snd_trident_gameport_read;
3216 gp->trigger = snd_trident_gameport_trigger;
3217 gp->cooked_read = snd_trident_gameport_cooked_read;
3218 gp->open = snd_trident_gameport_open;
3220 gameport_register_port(gp);
3225 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3227 if (chip->gameport) {
3228 gameport_unregister_port(chip->gameport);
3229 chip->gameport = NULL;
3233 int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3234 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3235 #endif /* CONFIG_GAMEPORT */
3240 static inline void do_delay(struct snd_trident *chip)
3242 schedule_timeout_uninterruptible(1);
3249 static int snd_trident_sis_reset(struct snd_trident *trident)
3251 unsigned long end_time;
3255 r = trident->in_suspend ? 0 : 2; /* count of retries */
3257 pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
3259 pci_write_config_byte(trident->pci, 0x46, 0x00);
3261 /* disable AC97 GPIO interrupt */
3262 outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3263 /* initialize serial interface, force cold reset */
3264 i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3265 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3267 /* remove cold reset */
3269 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3271 /* wait, until the codec is ready */
3272 end_time = (jiffies + (HZ * 3) / 4) + 1;
3274 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3277 } while (time_after_eq(end_time, jiffies));
3278 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3280 end_time = jiffies + HZ;
3283 } while (time_after_eq(end_time, jiffies));
3284 goto __si7018_retry;
3287 /* wait for the second codec */
3289 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3292 } while (time_after_eq(end_time, jiffies));
3293 /* enable 64 channel mode */
3294 outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3302 static void snd_trident_proc_read(struct snd_info_entry *entry,
3303 struct snd_info_buffer *buffer)
3305 struct snd_trident *trident = entry->private_data;
3308 switch (trident->device) {
3309 case TRIDENT_DEVICE_ID_SI7018:
3310 s = "SiS 7018 Audio";
3312 case TRIDENT_DEVICE_ID_DX:
3313 s = "Trident 4DWave PCI DX";
3315 case TRIDENT_DEVICE_ID_NX:
3316 s = "Trident 4DWave PCI NX";
3321 snd_iprintf(buffer, "%s\n\n", s);
3322 snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
3323 snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3324 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3325 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3326 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3327 snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3328 if (trident->tlb.entries) {
3329 snd_iprintf(buffer,"\nVirtual Memory\n");
3330 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3331 snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
3332 snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3335 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3336 snd_iprintf(buffer,"\nWavetable Synth\n");
3337 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
3338 snd_iprintf(buffer, "Memory Used : %d\n", trident->synth.current_size);
3339 snd_iprintf(buffer, "Memory Free : %d\n", (trident->synth.max_size-trident->synth.current_size));
3343 static void __devinit snd_trident_proc_init(struct snd_trident * trident)
3345 struct snd_info_entry *entry;
3346 const char *s = "trident";
3348 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3350 if (! snd_card_proc_new(trident->card, s, &entry))
3351 snd_info_set_text_ops(entry, trident, snd_trident_proc_read);
3354 static int snd_trident_dev_free(struct snd_device *device)
3356 struct snd_trident *trident = device->device_data;
3357 return snd_trident_free(trident);
3360 /*---------------------------------------------------------------------------
3361 snd_trident_tlb_alloc
3363 Description: Allocate and set up the TLB page table on 4D NX.
3364 Each entry has 4 bytes (physical PCI address).
3366 Paramters: trident - pointer to target device class for 4DWave.
3368 Returns: 0 or negative error code
3370 ---------------------------------------------------------------------------*/
3372 static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
3376 /* TLB array must be aligned to 16kB !!! so we allocate
3377 32kB region and correct offset when necessary */
3379 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3380 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3381 snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
3384 trident->tlb.entries = (unsigned int*)(((unsigned long)trident->tlb.buffer.area + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1));
3385 trident->tlb.entries_dmaaddr = (trident->tlb.buffer.addr + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1);
3386 /* allocate shadow TLB page table (virtual addresses) */
3387 trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
3388 if (trident->tlb.shadow_entries == NULL) {
3389 snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
3392 /* allocate and setup silent page and initialise TLB entries */
3393 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3394 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3395 snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
3398 memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3399 for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3400 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3401 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3404 /* use emu memory block manager code to manage tlb page allocation */
3405 trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3406 if (trident->tlb.memhdr == NULL)
3409 trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3414 * initialize 4D DX chip
3417 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3419 outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3420 outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3421 outl(0, TRID_REG(trident, T4D_AINTEN_A));
3422 outl(0, TRID_REG(trident, T4D_AINTEN_B));
3425 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3427 struct pci_dev *pci = trident->pci;
3428 unsigned long end_time;
3430 /* reset the legacy configuration and whole audio/wavetable block */
3431 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3432 pci_write_config_byte(pci, 0x44, 0); /* ports */
3433 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3434 pci_write_config_byte(pci, 0x46, 4); /* reset */
3436 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3439 /* warm reset of the AC'97 codec */
3440 outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3442 outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3443 /* DAC on, disable SB IRQ and try to force ADC valid signal */
3444 trident->ac97_ctrl = 0x0000004a;
3445 outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3446 /* wait, until the codec is ready */
3447 end_time = (jiffies + (HZ * 3) / 4) + 1;
3449 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3452 } while (time_after_eq(end_time, jiffies));
3453 snd_printk(KERN_ERR "AC'97 codec ready error\n");
3457 snd_trident_stop_all_voices(trident);
3463 * initialize 4D NX chip
3465 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3467 struct pci_dev *pci = trident->pci;
3468 unsigned long end_time;
3470 /* reset the legacy configuration and whole audio/wavetable block */
3471 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3472 pci_write_config_byte(pci, 0x44, 0); /* ports */
3473 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3475 pci_write_config_byte(pci, 0x46, 1); /* reset */
3477 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3480 /* warm reset of the AC'97 codec */
3481 outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3483 outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3484 /* wait, until the codec is ready */
3485 end_time = (jiffies + (HZ * 3) / 4) + 1;
3487 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3490 } while (time_after_eq(end_time, jiffies));
3491 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3496 trident->ac97_ctrl = 0x00000002;
3497 outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3498 /* disable SB IRQ */
3499 outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3501 snd_trident_stop_all_voices(trident);
3503 if (trident->tlb.entries != NULL) {
3505 /* enable virtual addressing via TLB */
3506 i = trident->tlb.entries_dmaaddr;
3508 outl(i, TRID_REG(trident, NX_TLBC));
3510 outl(0, TRID_REG(trident, NX_TLBC));
3512 /* initialize S/PDIF */
3513 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3514 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3520 * initialize sis7018 chip
3522 static int snd_trident_sis_init(struct snd_trident *trident)
3526 if ((err = snd_trident_sis_reset(trident)) < 0)
3529 snd_trident_stop_all_voices(trident);
3531 /* initialize S/PDIF */
3532 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3537 /*---------------------------------------------------------------------------
3540 Description: This routine will create the device specific class for
3541 the 4DWave card. It will also perform basic initialization.
3543 Paramters: card - which card to create
3544 pci - interface to PCI bus resource info
3545 dma1ptr - playback dma buffer
3546 dma2ptr - capture dma buffer
3547 irqptr - interrupt resource info
3549 Returns: 4DWave device class private data
3551 ---------------------------------------------------------------------------*/
3553 int __devinit snd_trident_create(struct snd_card *card,
3554 struct pci_dev *pci,
3556 int pcm_spdif_device,
3557 int max_wavetable_size,
3558 struct snd_trident ** rtrident)
3560 struct snd_trident *trident;
3562 struct snd_trident_voice *voice;
3563 struct snd_trident_pcm_mixer *tmix;
3564 static struct snd_device_ops ops = {
3565 .dev_free = snd_trident_dev_free,
3570 /* enable PCI device */
3571 if ((err = pci_enable_device(pci)) < 0)
3573 /* check, if we can restrict PCI DMA transfers to 30 bits */
3574 if (pci_set_dma_mask(pci, DMA_30BIT_MASK) < 0 ||
3575 pci_set_consistent_dma_mask(pci, DMA_30BIT_MASK) < 0) {
3576 snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
3577 pci_disable_device(pci);
3581 trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3582 if (trident == NULL) {
3583 pci_disable_device(pci);
3586 trident->device = (pci->vendor << 16) | pci->device;
3587 trident->card = card;
3589 spin_lock_init(&trident->reg_lock);
3590 spin_lock_init(&trident->event_lock);
3591 spin_lock_init(&trident->voice_alloc);
3592 if (pcm_streams < 1)
3594 if (pcm_streams > 32)
3596 trident->ChanPCM = pcm_streams;
3597 if (max_wavetable_size < 0 )
3598 max_wavetable_size = 0;
3599 trident->synth.max_size = max_wavetable_size * 1024;
3602 trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3603 pci_set_master(pci);
3605 if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3607 pci_disable_device(pci);
3610 trident->port = pci_resource_start(pci, 0);
3612 if (request_irq(pci->irq, snd_trident_interrupt, IRQF_DISABLED|IRQF_SHARED,
3613 "Trident Audio", trident)) {
3614 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
3615 snd_trident_free(trident);
3618 trident->irq = pci->irq;
3620 /* allocate 16k-aligned TLB for NX cards */
3621 trident->tlb.entries = NULL;
3622 trident->tlb.buffer.area = NULL;
3623 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3624 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3625 snd_trident_free(trident);
3630 trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3632 /* initialize chip */
3633 switch (trident->device) {
3634 case TRIDENT_DEVICE_ID_DX:
3635 err = snd_trident_4d_dx_init(trident);
3637 case TRIDENT_DEVICE_ID_NX:
3638 err = snd_trident_4d_nx_init(trident);
3640 case TRIDENT_DEVICE_ID_SI7018:
3641 err = snd_trident_sis_init(trident);
3648 snd_trident_free(trident);
3652 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3653 snd_trident_free(trident);
3657 if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3660 /* initialise synth voices */
3661 for (i = 0; i < 64; i++) {
3662 voice = &trident->synth.voices[i];
3664 voice->trident = trident;
3666 /* initialize pcm mixer entries */
3667 for (i = 0; i < 32; i++) {
3668 tmix = &trident->pcm_mixer[i];
3669 tmix->vol = T4D_DEFAULT_PCM_VOL;
3670 tmix->pan = T4D_DEFAULT_PCM_PAN;
3671 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3672 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3675 snd_trident_enable_eso(trident);
3677 snd_trident_proc_init(trident);
3678 snd_card_set_dev(card, &pci->dev);
3679 *rtrident = trident;
3683 /*---------------------------------------------------------------------------
3686 Description: This routine will free the device specific class for
3689 Paramters: trident - device specific private data for 4DWave card
3693 ---------------------------------------------------------------------------*/
3695 static int snd_trident_free(struct snd_trident *trident)
3697 snd_trident_free_gameport(trident);
3698 snd_trident_disable_eso(trident);
3699 // Disable S/PDIF out
3700 if (trident->device == TRIDENT_DEVICE_ID_NX)
3701 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3702 else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3703 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3705 if (trident->tlb.buffer.area) {
3706 outl(0, TRID_REG(trident, NX_TLBC));
3707 if (trident->tlb.memhdr)
3708 snd_util_memhdr_free(trident->tlb.memhdr);
3709 if (trident->tlb.silent_page.area)
3710 snd_dma_free_pages(&trident->tlb.silent_page);
3711 vfree(trident->tlb.shadow_entries);
3712 snd_dma_free_pages(&trident->tlb.buffer);
3714 if (trident->irq >= 0)
3715 free_irq(trident->irq, trident);
3716 pci_release_regions(trident->pci);
3717 pci_disable_device(trident->pci);
3722 /*---------------------------------------------------------------------------
3723 snd_trident_interrupt
3725 Description: ISR for Trident 4DWave device
3727 Paramters: trident - device specific private data for 4DWave card
3729 Problems: It seems that Trident chips generates interrupts more than
3730 one time in special cases. The spurious interrupts are
3731 detected via sample timer (T4D_STIMER) and computing
3732 corresponding delta value. The limits are detected with
3733 the method try & fail so it is possible that it won't
3734 work on all computers. [jaroslav]
3738 ---------------------------------------------------------------------------*/
3740 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3742 struct snd_trident *trident = dev_id;
3743 unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3745 struct snd_trident_voice *voice;
3747 audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3748 if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3750 if (audio_int & ADDRESS_IRQ) {
3751 // get interrupt status for all channels
3752 spin_lock(&trident->reg_lock);
3753 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3754 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3757 outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
3759 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3762 for (channel = 63; channel >= 32; channel--) {
3763 mask = 1 << (channel&0x1f);
3764 if ((chn_int & mask) == 0)
3766 voice = &trident->synth.voices[channel];
3767 if (!voice->pcm || voice->substream == NULL) {
3768 outl(mask, TRID_REG(trident, T4D_STOP_B));
3771 delta = (int)stimer - (int)voice->stimer;
3774 if ((unsigned int)delta < voice->spurious_threshold) {
3775 /* do some statistics here */
3776 trident->spurious_irq_count++;
3777 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3778 trident->spurious_irq_max_delta = delta;
3781 voice->stimer = stimer;
3783 if (!voice->isync3) {
3784 tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3785 if (trident->bDMAStart & 0x40)
3788 tmp = voice->isync_max - tmp;
3790 tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3792 if (tmp < voice->isync_mark) {
3794 tmp = voice->isync_ESO - 7;
3796 tmp = voice->isync_ESO + 2;
3797 /* update ESO for IRQ voice to preserve sync */
3798 snd_trident_stop_voice(trident, voice->number);
3799 snd_trident_write_eso_reg(trident, voice, tmp);
3800 snd_trident_start_voice(trident, voice->number);
3802 } else if (voice->isync2) {
3804 /* write original ESO and update CSO for IRQ voice to preserve sync */
3805 snd_trident_stop_voice(trident, voice->number);
3806 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3807 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3808 snd_trident_start_voice(trident, voice->number);
3812 /* update CSO for extra voice to preserve sync */
3813 snd_trident_stop_voice(trident, voice->extra->number);
3814 snd_trident_write_cso_reg(trident, voice->extra, 0);
3815 snd_trident_start_voice(trident, voice->extra->number);
3818 spin_unlock(&trident->reg_lock);
3819 snd_pcm_period_elapsed(voice->substream);
3820 spin_lock(&trident->reg_lock);
3822 outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
3824 spin_unlock(&trident->reg_lock);
3826 if (audio_int & MPU401_IRQ) {
3827 if (trident->rmidi) {
3828 snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data, regs);
3830 inb(TRID_REG(trident, T4D_MPUR0));
3833 // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3837 /*---------------------------------------------------------------------------
3838 snd_trident_attach_synthesizer
3840 Description: Attach synthesizer hooks
3842 Paramters: trident - device specific private data for 4DWave card
3846 ---------------------------------------------------------------------------*/
3847 int snd_trident_attach_synthesizer(struct snd_trident *trident)
3849 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3850 if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
3851 sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
3852 strcpy(trident->seq_dev->name, "4DWave");
3853 *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
3859 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3861 struct snd_trident_voice *pvoice;
3862 unsigned long flags;
3865 spin_lock_irqsave(&trident->voice_alloc, flags);
3866 if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3867 idx = snd_trident_allocate_pcm_channel(trident);
3869 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3872 pvoice = &trident->synth.voices[idx];
3875 pvoice->capture = 0;
3877 pvoice->memblk = NULL;
3878 pvoice->substream = NULL;
3879 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3882 if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3883 idx = snd_trident_allocate_synth_channel(trident);
3885 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3888 pvoice = &trident->synth.voices[idx];
3891 pvoice->client = client;
3892 pvoice->port = port;
3893 pvoice->memblk = NULL;
3894 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3897 if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3899 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3903 EXPORT_SYMBOL(snd_trident_alloc_voice);
3905 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3907 unsigned long flags;
3908 void (*private_free)(struct snd_trident_voice *);
3911 if (voice == NULL || !voice->use)
3913 snd_trident_clear_voices(trident, voice->number, voice->number);
3914 spin_lock_irqsave(&trident->voice_alloc, flags);
3915 private_free = voice->private_free;
3916 private_data = voice->private_data;
3917 voice->private_free = NULL;
3918 voice->private_data = NULL;
3920 snd_trident_free_pcm_channel(trident, voice->number);
3922 snd_trident_free_synth_channel(trident, voice->number);
3923 voice->use = voice->pcm = voice->synth = voice->midi = 0;
3924 voice->capture = voice->spdif = 0;
3925 voice->sample_ops = NULL;
3926 voice->substream = NULL;
3927 voice->extra = NULL;
3928 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3930 private_free(voice);
3933 EXPORT_SYMBOL(snd_trident_free_voice);
3935 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3937 unsigned int i, val, mask[2] = { 0, 0 };
3939 snd_assert(v_min <= 63, return);
3940 snd_assert(v_max <= 63, return);
3941 for (i = v_min; i <= v_max; i++)
3942 mask[i >> 5] |= 1 << (i & 0x1f);
3944 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3945 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3946 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3949 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3950 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3951 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3956 int snd_trident_suspend(struct pci_dev *pci, pm_message_t state)
3958 struct snd_card *card = pci_get_drvdata(pci);
3959 struct snd_trident *trident = card->private_data;
3961 trident->in_suspend = 1;
3962 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3963 snd_pcm_suspend_all(trident->pcm);
3964 snd_pcm_suspend_all(trident->foldback);
3965 snd_pcm_suspend_all(trident->spdif);
3967 snd_ac97_suspend(trident->ac97);
3968 snd_ac97_suspend(trident->ac97_sec);
3970 switch (trident->device) {
3971 case TRIDENT_DEVICE_ID_DX:
3972 case TRIDENT_DEVICE_ID_NX:
3974 case TRIDENT_DEVICE_ID_SI7018:
3977 pci_disable_device(pci);
3978 pci_save_state(pci);
3982 int snd_trident_resume(struct pci_dev *pci)
3984 struct snd_card *card = pci_get_drvdata(pci);
3985 struct snd_trident *trident = card->private_data;
3987 pci_restore_state(pci);
3988 pci_enable_device(pci);
3989 pci_set_master(pci); /* to be sure */
3991 switch (trident->device) {
3992 case TRIDENT_DEVICE_ID_DX:
3993 snd_trident_4d_dx_init(trident);
3995 case TRIDENT_DEVICE_ID_NX:
3996 snd_trident_4d_nx_init(trident);
3998 case TRIDENT_DEVICE_ID_SI7018:
3999 snd_trident_sis_init(trident);
4003 snd_ac97_resume(trident->ac97);
4004 snd_ac97_resume(trident->ac97_sec);
4006 /* restore some registers */
4007 outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
4009 snd_trident_enable_eso(trident);
4011 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
4012 trident->in_suspend = 0;
4015 #endif /* CONFIG_PM */