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 static int snd_trident_sis_reset(struct snd_trident *trident);
58 static void snd_trident_clear_voices(struct snd_trident * trident,
59 unsigned short v_min, unsigned short v_max);
60 static int snd_trident_free(struct snd_trident *trident);
68 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
70 unsigned int val, tmp;
72 printk("Trident voice %i:\n", voice);
73 outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
74 val = inl(TRID_REG(trident, CH_LBA));
75 printk("LBA: 0x%x\n", val);
76 val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
77 printk("GVSel: %i\n", val >> 31);
78 printk("Pan: 0x%x\n", (val >> 24) & 0x7f);
79 printk("Vol: 0x%x\n", (val >> 16) & 0xff);
80 printk("CTRL: 0x%x\n", (val >> 12) & 0x0f);
81 printk("EC: 0x%x\n", val & 0x0fff);
82 if (trident->device != TRIDENT_DEVICE_ID_NX) {
83 val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
84 printk("CSO: 0x%x\n", val >> 16);
85 printk("Alpha: 0x%x\n", (val >> 4) & 0x0fff);
86 printk("FMS: 0x%x\n", val & 0x0f);
87 val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
88 printk("ESO: 0x%x\n", val >> 16);
89 printk("Delta: 0x%x\n", val & 0xffff);
90 val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
91 } else { // TRIDENT_DEVICE_ID_NX
92 val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
93 tmp = (val >> 24) & 0xff;
94 printk("CSO: 0x%x\n", val & 0x00ffffff);
95 val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
96 tmp |= (val >> 16) & 0xff00;
97 printk("Delta: 0x%x\n", tmp);
98 printk("ESO: 0x%x\n", val & 0x00ffffff);
99 val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
100 printk("Alpha: 0x%x\n", val >> 20);
101 printk("FMS: 0x%x\n", (val >> 16) & 0x0f);
103 printk("FMC: 0x%x\n", (val >> 14) & 3);
104 printk("RVol: 0x%x\n", (val >> 7) & 0x7f);
105 printk("CVol: 0x%x\n", val & 0x7f);
109 /*---------------------------------------------------------------------------
110 unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
112 Description: This routine will do all of the reading from the external
115 Parameters: ac97 - ac97 codec structure
116 reg - CODEC register index, from AC97 Hal.
118 returns: 16 bit value read from the AC97.
120 ---------------------------------------------------------------------------*/
121 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
123 unsigned int data = 0, treg;
124 unsigned short count = 0xffff;
126 struct snd_trident *trident = ac97->private_data;
128 spin_lock_irqsave(&trident->reg_lock, flags);
129 if (trident->device == TRIDENT_DEVICE_ID_DX) {
130 data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
131 outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
133 data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
134 if ((data & DX_AC97_BUSY_READ) == 0)
137 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
138 data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
139 treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
140 outl(data, TRID_REG(trident, treg));
142 data = inl(TRID_REG(trident, treg));
143 if ((data & 0x00000C00) == 0)
146 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
147 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
149 data |= SI_AC97_SECONDARY;
150 outl(data, TRID_REG(trident, SI_AC97_READ));
152 data = inl(TRID_REG(trident, SI_AC97_READ));
153 if ((data & (SI_AC97_BUSY_READ)) == 0)
158 if (count == 0 && !trident->ac97_detect) {
159 snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
164 spin_unlock_irqrestore(&trident->reg_lock, flags);
165 return ((unsigned short) (data >> 16));
168 /*---------------------------------------------------------------------------
169 void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
170 unsigned short wdata)
172 Description: This routine will do all of the writing to the external
175 Parameters: ac97 - ac97 codec structure
176 reg - CODEC register index, from AC97 Hal.
177 data - Lower 16 bits are the data to write to CODEC.
179 returns: TRUE if everything went ok, else FALSE.
181 ---------------------------------------------------------------------------*/
182 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
183 unsigned short wdata)
185 unsigned int address, data;
186 unsigned short count = 0xffff;
188 struct snd_trident *trident = ac97->private_data;
190 data = ((unsigned long) wdata) << 16;
192 spin_lock_irqsave(&trident->reg_lock, flags);
193 if (trident->device == TRIDENT_DEVICE_ID_DX) {
194 address = DX_ACR0_AC97_W;
196 /* read AC-97 write register status */
198 if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
202 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
203 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
204 address = NX_ACR1_AC97_W;
206 /* read AC-97 write register status */
208 if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
212 data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
213 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
214 address = SI_AC97_WRITE;
216 /* read AC-97 write register status */
218 if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
222 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
224 data |= SI_AC97_SECONDARY;
226 address = 0; /* keep GCC happy */
227 count = 0; /* return */
231 spin_unlock_irqrestore(&trident->reg_lock, flags);
234 outl(data, TRID_REG(trident, address));
235 spin_unlock_irqrestore(&trident->reg_lock, flags);
238 /*---------------------------------------------------------------------------
239 void snd_trident_enable_eso(struct snd_trident *trident)
241 Description: This routine will enable end of loop interrupts.
242 End of loop interrupts will occur when a running
244 Also enables middle of loop interrupts.
246 Parameters: trident - pointer to target device class for 4DWave.
248 ---------------------------------------------------------------------------*/
250 static void snd_trident_enable_eso(struct snd_trident * trident)
254 val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
257 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
259 outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
262 /*---------------------------------------------------------------------------
263 void snd_trident_disable_eso(struct snd_trident *trident)
265 Description: This routine will disable end of loop interrupts.
266 End of loop interrupts will occur when a running
268 Also disables middle of loop interrupts.
271 trident - pointer to target device class for 4DWave.
273 returns: TRUE if everything went ok, else FALSE.
275 ---------------------------------------------------------------------------*/
277 static void snd_trident_disable_eso(struct snd_trident * trident)
281 tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
284 outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
287 /*---------------------------------------------------------------------------
288 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
290 Description: Start a voice, any channel 0 thru 63.
291 This routine automatically handles the fact that there are
292 more than 32 channels available.
294 Parameters : voice - Voice number 0 thru n.
295 trident - pointer to target device class for 4DWave.
299 ---------------------------------------------------------------------------*/
301 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
303 unsigned int mask = 1 << (voice & 0x1f);
304 unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
306 outl(mask, TRID_REG(trident, reg));
309 EXPORT_SYMBOL(snd_trident_start_voice);
311 /*---------------------------------------------------------------------------
312 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
314 Description: Stop a voice, any channel 0 thru 63.
315 This routine automatically handles the fact that there are
316 more than 32 channels available.
318 Parameters : voice - Voice number 0 thru n.
319 trident - pointer to target device class for 4DWave.
323 ---------------------------------------------------------------------------*/
325 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
327 unsigned int mask = 1 << (voice & 0x1f);
328 unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
330 outl(mask, TRID_REG(trident, reg));
333 EXPORT_SYMBOL(snd_trident_stop_voice);
335 /*---------------------------------------------------------------------------
336 int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
338 Description: Allocate hardware channel in Bank B (32-63).
340 Parameters : trident - pointer to target device class for 4DWave.
342 Return Value: hardware channel - 32-63 or -1 when no channel is available
344 ---------------------------------------------------------------------------*/
346 static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
350 if (trident->ChanPCMcnt >= trident->ChanPCM)
352 for (idx = 31; idx >= 0; idx--) {
353 if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
354 trident->ChanMap[T4D_BANK_B] |= 1 << idx;
355 trident->ChanPCMcnt++;
362 /*---------------------------------------------------------------------------
363 void snd_trident_free_pcm_channel(int channel)
365 Description: Free hardware channel in Bank B (32-63)
367 Parameters : trident - pointer to target device class for 4DWave.
368 channel - hardware channel number 0-63
372 ---------------------------------------------------------------------------*/
374 static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
376 if (channel < 32 || channel > 63)
379 if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
380 trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
381 trident->ChanPCMcnt--;
385 /*---------------------------------------------------------------------------
386 unsigned int snd_trident_allocate_synth_channel(void)
388 Description: Allocate hardware channel in Bank A (0-31).
390 Parameters : trident - pointer to target device class for 4DWave.
392 Return Value: hardware channel - 0-31 or -1 when no channel is available
394 ---------------------------------------------------------------------------*/
396 static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
400 for (idx = 31; idx >= 0; idx--) {
401 if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
402 trident->ChanMap[T4D_BANK_A] |= 1 << idx;
403 trident->synth.ChanSynthCount++;
410 /*---------------------------------------------------------------------------
411 void snd_trident_free_synth_channel( int channel )
413 Description: Free hardware channel in Bank B (0-31).
415 Parameters : trident - pointer to target device class for 4DWave.
416 channel - hardware channel number 0-63
420 ---------------------------------------------------------------------------*/
422 static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
424 if (channel < 0 || channel > 31)
427 if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
428 trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
429 trident->synth.ChanSynthCount--;
433 /*---------------------------------------------------------------------------
434 snd_trident_write_voice_regs
436 Description: This routine will complete and write the 5 hardware channel
437 registers to hardware.
439 Paramters: trident - pointer to target device class for 4DWave.
440 voice - synthesizer voice structure
443 ---------------------------------------------------------------------------*/
445 void snd_trident_write_voice_regs(struct snd_trident * trident,
446 struct snd_trident_voice * voice)
448 unsigned int FmcRvolCvol;
449 unsigned int regs[5];
451 regs[1] = voice->LBA;
452 regs[4] = (voice->GVSel << 31) |
453 ((voice->Pan & 0x0000007f) << 24) |
454 ((voice->CTRL & 0x0000000f) << 12);
455 FmcRvolCvol = ((voice->FMC & 3) << 14) |
456 ((voice->RVol & 0x7f) << 7) |
457 (voice->CVol & 0x7f);
459 switch (trident->device) {
460 case TRIDENT_DEVICE_ID_SI7018:
461 regs[4] |= voice->number > 31 ?
462 (voice->Vol & 0x000003ff) :
463 ((voice->Vol & 0x00003fc) << (16-2)) |
464 (voice->EC & 0x00000fff);
465 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
466 (voice->FMS & 0x0000000f);
467 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
468 regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
470 case TRIDENT_DEVICE_ID_DX:
471 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
472 (voice->EC & 0x00000fff);
473 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
474 (voice->FMS & 0x0000000f);
475 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
476 regs[3] = FmcRvolCvol;
478 case TRIDENT_DEVICE_ID_NX:
479 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
480 (voice->EC & 0x00000fff);
481 regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
482 regs[2] = ((voice->Delta << 16) & 0xff000000) |
483 (voice->ESO & 0x00ffffff);
484 regs[3] = (voice->Alpha << 20) |
485 ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
492 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
493 outl(regs[0], TRID_REG(trident, CH_START + 0));
494 outl(regs[1], TRID_REG(trident, CH_START + 4));
495 outl(regs[2], TRID_REG(trident, CH_START + 8));
496 outl(regs[3], TRID_REG(trident, CH_START + 12));
497 outl(regs[4], TRID_REG(trident, CH_START + 16));
500 printk("written %i channel:\n", voice->number);
501 printk(" regs[0] = 0x%x/0x%x\n", regs[0], inl(TRID_REG(trident, CH_START + 0)));
502 printk(" regs[1] = 0x%x/0x%x\n", regs[1], inl(TRID_REG(trident, CH_START + 4)));
503 printk(" regs[2] = 0x%x/0x%x\n", regs[2], inl(TRID_REG(trident, CH_START + 8)));
504 printk(" regs[3] = 0x%x/0x%x\n", regs[3], inl(TRID_REG(trident, CH_START + 12)));
505 printk(" regs[4] = 0x%x/0x%x\n", regs[4], inl(TRID_REG(trident, CH_START + 16)));
509 EXPORT_SYMBOL(snd_trident_write_voice_regs);
511 /*---------------------------------------------------------------------------
512 snd_trident_write_cso_reg
514 Description: This routine will write the new CSO offset
515 register to hardware.
517 Paramters: trident - pointer to target device class for 4DWave.
518 voice - synthesizer voice structure
521 ---------------------------------------------------------------------------*/
523 static void snd_trident_write_cso_reg(struct snd_trident * trident,
524 struct snd_trident_voice * voice,
528 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
529 if (trident->device != TRIDENT_DEVICE_ID_NX) {
530 outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
532 outl((voice->Delta << 24) |
533 (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
537 /*---------------------------------------------------------------------------
538 snd_trident_write_eso_reg
540 Description: This routine will write the new ESO offset
541 register to hardware.
543 Paramters: trident - pointer to target device class for 4DWave.
544 voice - synthesizer voice structure
547 ---------------------------------------------------------------------------*/
549 static void snd_trident_write_eso_reg(struct snd_trident * trident,
550 struct snd_trident_voice * voice,
554 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
555 if (trident->device != TRIDENT_DEVICE_ID_NX) {
556 outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
558 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
559 TRID_REG(trident, CH_NX_DELTA_ESO));
563 /*---------------------------------------------------------------------------
564 snd_trident_write_vol_reg
566 Description: This routine will write the new voice volume
567 register to hardware.
569 Paramters: trident - pointer to target device class for 4DWave.
570 voice - synthesizer voice structure
571 Vol - new voice volume
573 ---------------------------------------------------------------------------*/
575 static void snd_trident_write_vol_reg(struct snd_trident * trident,
576 struct snd_trident_voice * voice,
580 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
581 switch (trident->device) {
582 case TRIDENT_DEVICE_ID_DX:
583 case TRIDENT_DEVICE_ID_NX:
584 outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
586 case TRIDENT_DEVICE_ID_SI7018:
587 // printk("voice->Vol = 0x%x\n", voice->Vol);
588 outw((voice->CTRL << 12) | voice->Vol,
589 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
594 /*---------------------------------------------------------------------------
595 snd_trident_write_pan_reg
597 Description: This routine will write the new voice pan
598 register to hardware.
600 Paramters: trident - pointer to target device class for 4DWave.
601 voice - synthesizer voice structure
604 ---------------------------------------------------------------------------*/
606 static void snd_trident_write_pan_reg(struct snd_trident * trident,
607 struct snd_trident_voice * voice,
611 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
612 outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
613 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
616 /*---------------------------------------------------------------------------
617 snd_trident_write_rvol_reg
619 Description: This routine will write the new reverb volume
620 register to hardware.
622 Paramters: trident - pointer to target device class for 4DWave.
623 voice - synthesizer voice structure
624 RVol - new reverb volume
626 ---------------------------------------------------------------------------*/
628 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
629 struct snd_trident_voice * voice,
633 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
634 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
635 (voice->CVol & 0x007f),
636 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
637 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
640 /*---------------------------------------------------------------------------
641 snd_trident_write_cvol_reg
643 Description: This routine will write the new chorus volume
644 register to hardware.
646 Paramters: trident - pointer to target device class for 4DWave.
647 voice - synthesizer voice structure
648 CVol - new chorus volume
650 ---------------------------------------------------------------------------*/
652 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
653 struct snd_trident_voice * voice,
657 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
658 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
659 (voice->CVol & 0x007f),
660 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
661 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
664 /*---------------------------------------------------------------------------
665 snd_trident_convert_rate
667 Description: This routine converts rate in HZ to hardware delta value.
669 Paramters: trident - pointer to target device class for 4DWave.
670 rate - Real or Virtual channel number.
672 Returns: Delta value.
674 ---------------------------------------------------------------------------*/
675 static unsigned int snd_trident_convert_rate(unsigned int rate)
679 // We special case 44100 and 8000 since rounding with the equation
680 // does not give us an accurate enough value. For 11025 and 22050
681 // the equation gives us the best answer. All other frequencies will
682 // also use the equation. JDW
685 else if (rate == 8000)
687 else if (rate == 48000)
690 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
694 /*---------------------------------------------------------------------------
695 snd_trident_convert_adc_rate
697 Description: This routine converts rate in HZ to hardware delta value.
699 Paramters: trident - pointer to target device class for 4DWave.
700 rate - Real or Virtual channel number.
702 Returns: Delta value.
704 ---------------------------------------------------------------------------*/
705 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
709 // We special case 44100 and 8000 since rounding with the equation
710 // does not give us an accurate enough value. For 11025 and 22050
711 // the equation gives us the best answer. All other frequencies will
712 // also use the equation. JDW
715 else if (rate == 8000)
717 else if (rate == 48000)
720 delta = ((48000 << 12) / rate) & 0x0000ffff;
724 /*---------------------------------------------------------------------------
725 snd_trident_spurious_threshold
727 Description: This routine converts rate in HZ to spurious threshold.
729 Paramters: trident - pointer to target device class for 4DWave.
730 rate - Real or Virtual channel number.
732 Returns: Delta value.
734 ---------------------------------------------------------------------------*/
735 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
736 unsigned int period_size)
738 unsigned int res = (rate * period_size) / 48000;
746 /*---------------------------------------------------------------------------
747 snd_trident_control_mode
749 Description: This routine returns a control mode for a PCM channel.
751 Paramters: trident - pointer to target device class for 4DWave.
752 substream - PCM substream
754 Returns: Control value.
756 ---------------------------------------------------------------------------*/
757 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
760 struct snd_pcm_runtime *runtime = substream->runtime;
763 CTRL default: 8-bit (unsigned) mono, loop mode enabled
766 if (snd_pcm_format_width(runtime->format) == 16)
767 CTRL |= 0x00000008; // 16-bit data
768 if (snd_pcm_format_signed(runtime->format))
769 CTRL |= 0x00000002; // signed data
770 if (runtime->channels > 1)
771 CTRL |= 0x00000004; // stereo data
779 /*---------------------------------------------------------------------------
782 Description: Device I/O control handler for playback/capture parameters.
784 Paramters: substream - PCM substream class
785 cmd - what ioctl message to process
786 arg - additional message infoarg
788 Returns: Error status
790 ---------------------------------------------------------------------------*/
792 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
796 /* FIXME: it seems that with small periods the behaviour of
797 trident hardware is unpredictable and interrupt generator
799 return snd_pcm_lib_ioctl(substream, cmd, arg);
802 /*---------------------------------------------------------------------------
803 snd_trident_allocate_pcm_mem
805 Description: Allocate PCM ring buffer for given substream
807 Parameters: substream - PCM substream class
808 hw_params - hardware parameters
810 Returns: Error status
812 ---------------------------------------------------------------------------*/
814 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
815 struct snd_pcm_hw_params *hw_params)
817 struct snd_trident *trident = snd_pcm_substream_chip(substream);
818 struct snd_pcm_runtime *runtime = substream->runtime;
819 struct snd_trident_voice *voice = runtime->private_data;
822 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
824 if (trident->tlb.entries) {
825 if (err > 0) { /* change */
827 snd_trident_free_pages(trident, voice->memblk);
828 voice->memblk = snd_trident_alloc_pages(trident, substream);
829 if (voice->memblk == NULL)
836 /*---------------------------------------------------------------------------
837 snd_trident_allocate_evoice
839 Description: Allocate extra voice as interrupt generator
841 Parameters: substream - PCM substream class
842 hw_params - hardware parameters
844 Returns: Error status
846 ---------------------------------------------------------------------------*/
848 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
849 struct snd_pcm_hw_params *hw_params)
851 struct snd_trident *trident = snd_pcm_substream_chip(substream);
852 struct snd_pcm_runtime *runtime = substream->runtime;
853 struct snd_trident_voice *voice = runtime->private_data;
854 struct snd_trident_voice *evoice = voice->extra;
856 /* voice management */
858 if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
859 if (evoice == NULL) {
860 evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
863 voice->extra = evoice;
864 evoice->substream = substream;
867 if (evoice != NULL) {
868 snd_trident_free_voice(trident, evoice);
869 voice->extra = evoice = NULL;
876 /*---------------------------------------------------------------------------
877 snd_trident_hw_params
879 Description: Set the hardware parameters for the playback device.
881 Parameters: substream - PCM substream class
882 hw_params - hardware parameters
884 Returns: Error status
886 ---------------------------------------------------------------------------*/
888 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
889 struct snd_pcm_hw_params *hw_params)
893 err = snd_trident_allocate_pcm_mem(substream, hw_params);
895 err = snd_trident_allocate_evoice(substream, hw_params);
899 /*---------------------------------------------------------------------------
900 snd_trident_playback_hw_free
902 Description: Release the hardware resources for the playback device.
904 Parameters: substream - PCM substream class
906 Returns: Error status
908 ---------------------------------------------------------------------------*/
910 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
912 struct snd_trident *trident = snd_pcm_substream_chip(substream);
913 struct snd_pcm_runtime *runtime = substream->runtime;
914 struct snd_trident_voice *voice = runtime->private_data;
915 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
917 if (trident->tlb.entries) {
918 if (voice && voice->memblk) {
919 snd_trident_free_pages(trident, voice->memblk);
920 voice->memblk = NULL;
923 snd_pcm_lib_free_pages(substream);
924 if (evoice != NULL) {
925 snd_trident_free_voice(trident, evoice);
931 /*---------------------------------------------------------------------------
932 snd_trident_playback_prepare
934 Description: Prepare playback device for playback.
936 Parameters: substream - PCM substream class
938 Returns: Error status
940 ---------------------------------------------------------------------------*/
942 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
944 struct snd_trident *trident = snd_pcm_substream_chip(substream);
945 struct snd_pcm_runtime *runtime = substream->runtime;
946 struct snd_trident_voice *voice = runtime->private_data;
947 struct snd_trident_voice *evoice = voice->extra;
948 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
950 spin_lock_irq(&trident->reg_lock);
952 /* set delta (rate) value */
953 voice->Delta = snd_trident_convert_rate(runtime->rate);
954 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
956 /* set Loop Begin Address */
958 voice->LBA = voice->memblk->offset;
960 voice->LBA = runtime->dma_addr;
963 voice->ESO = runtime->buffer_size - 1; /* in samples */
964 voice->CTRL = snd_trident_control_mode(substream);
970 voice->Vol = mix->vol;
971 voice->RVol = mix->rvol;
972 voice->CVol = mix->cvol;
973 voice->Pan = mix->pan;
974 voice->Attribute = 0;
976 voice->Attribute = (1<<(30-16))|(2<<(26-16))|
977 (0<<(24-16))|(0x1f<<(19-16));
979 voice->Attribute = 0;
982 snd_trident_write_voice_regs(trident, voice);
984 if (evoice != NULL) {
985 evoice->Delta = voice->Delta;
986 evoice->spurious_threshold = voice->spurious_threshold;
987 evoice->LBA = voice->LBA;
989 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
990 evoice->CTRL = voice->CTRL;
992 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
996 evoice->Vol = 0x3ff; /* mute */
997 evoice->RVol = evoice->CVol = 0x7f; /* mute */
998 evoice->Pan = 0x7f; /* mute */
1000 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
1001 (0<<(24-16))|(0x1f<<(19-16));
1003 evoice->Attribute = 0;
1005 snd_trident_write_voice_regs(trident, evoice);
1007 evoice->isync_mark = runtime->period_size;
1008 evoice->ESO = (runtime->period_size * 2) - 1;
1011 spin_unlock_irq(&trident->reg_lock);
1016 /*---------------------------------------------------------------------------
1017 snd_trident_capture_hw_params
1019 Description: Set the hardware parameters for the capture device.
1021 Parameters: substream - PCM substream class
1022 hw_params - hardware parameters
1024 Returns: Error status
1026 ---------------------------------------------------------------------------*/
1028 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1029 struct snd_pcm_hw_params *hw_params)
1031 return snd_trident_allocate_pcm_mem(substream, hw_params);
1034 /*---------------------------------------------------------------------------
1035 snd_trident_capture_prepare
1037 Description: Prepare capture device for playback.
1039 Parameters: substream - PCM substream class
1041 Returns: Error status
1043 ---------------------------------------------------------------------------*/
1045 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1047 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1048 struct snd_pcm_runtime *runtime = substream->runtime;
1049 struct snd_trident_voice *voice = runtime->private_data;
1050 unsigned int val, ESO_bytes;
1052 spin_lock_irq(&trident->reg_lock);
1054 // Initilize the channel and set channel Mode
1055 outb(0, TRID_REG(trident, LEGACY_DMAR15));
1057 // Set DMA channel operation mode register
1058 outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1060 // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
1061 voice->LBA = runtime->dma_addr;
1062 outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
1064 voice->LBA = voice->memblk->offset;
1067 ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
1068 outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
1069 outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
1072 // Set channel sample rate, 4.12 format
1073 val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
1074 outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
1076 // Set channel interrupt blk length
1077 if (snd_pcm_format_width(runtime->format) == 16) {
1078 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1080 val = (unsigned short) (ESO_bytes - 1);
1083 outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1085 // Right now, set format and start to run captureing,
1086 // continuous run loop enable.
1087 trident->bDMAStart = 0x19; // 0001 1001b
1089 if (snd_pcm_format_width(runtime->format) == 16)
1090 trident->bDMAStart |= 0x80;
1091 if (snd_pcm_format_signed(runtime->format))
1092 trident->bDMAStart |= 0x20;
1093 if (runtime->channels > 1)
1094 trident->bDMAStart |= 0x40;
1096 // Prepare capture intr channel
1098 voice->Delta = snd_trident_convert_rate(runtime->rate);
1099 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1101 voice->isync_mark = runtime->period_size;
1102 voice->isync_max = runtime->buffer_size;
1104 // Set voice parameters
1106 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1107 voice->CTRL = snd_trident_control_mode(substream);
1112 voice->Pan = 0x7f; /* mute */
1113 voice->Vol = 0x3ff; /* mute */
1117 voice->Attribute = 0;
1119 snd_trident_write_voice_regs(trident, voice);
1121 spin_unlock_irq(&trident->reg_lock);
1125 /*---------------------------------------------------------------------------
1126 snd_trident_si7018_capture_hw_params
1128 Description: Set the hardware parameters for the capture device.
1130 Parameters: substream - PCM substream class
1131 hw_params - hardware parameters
1133 Returns: Error status
1135 ---------------------------------------------------------------------------*/
1137 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1138 struct snd_pcm_hw_params *hw_params)
1142 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1145 return snd_trident_allocate_evoice(substream, hw_params);
1148 /*---------------------------------------------------------------------------
1149 snd_trident_si7018_capture_hw_free
1151 Description: Release the hardware resources for the capture device.
1153 Parameters: substream - PCM substream class
1155 Returns: Error status
1157 ---------------------------------------------------------------------------*/
1159 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1161 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1162 struct snd_pcm_runtime *runtime = substream->runtime;
1163 struct snd_trident_voice *voice = runtime->private_data;
1164 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1166 snd_pcm_lib_free_pages(substream);
1167 if (evoice != NULL) {
1168 snd_trident_free_voice(trident, evoice);
1169 voice->extra = NULL;
1174 /*---------------------------------------------------------------------------
1175 snd_trident_si7018_capture_prepare
1177 Description: Prepare capture device for playback.
1179 Parameters: substream - PCM substream class
1181 Returns: Error status
1183 ---------------------------------------------------------------------------*/
1185 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1187 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1188 struct snd_pcm_runtime *runtime = substream->runtime;
1189 struct snd_trident_voice *voice = runtime->private_data;
1190 struct snd_trident_voice *evoice = voice->extra;
1192 spin_lock_irq(&trident->reg_lock);
1194 voice->LBA = runtime->dma_addr;
1195 voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
1196 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1198 // Set voice parameters
1200 voice->ESO = runtime->buffer_size - 1; /* in samples */
1201 voice->CTRL = snd_trident_control_mode(substream);
1206 voice->Pan = T4D_DEFAULT_PCM_PAN;
1212 voice->Attribute = (2 << (30-16)) |
1217 snd_trident_write_voice_regs(trident, voice);
1219 if (evoice != NULL) {
1220 evoice->Delta = snd_trident_convert_rate(runtime->rate);
1221 evoice->spurious_threshold = voice->spurious_threshold;
1222 evoice->LBA = voice->LBA;
1224 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1225 evoice->CTRL = voice->CTRL;
1231 evoice->Vol = 0x3ff; /* mute */
1232 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1233 evoice->Pan = 0x7f; /* mute */
1234 evoice->Attribute = 0;
1235 snd_trident_write_voice_regs(trident, evoice);
1237 evoice->isync_mark = runtime->period_size;
1238 evoice->ESO = (runtime->period_size * 2) - 1;
1241 spin_unlock_irq(&trident->reg_lock);
1245 /*---------------------------------------------------------------------------
1246 snd_trident_foldback_prepare
1248 Description: Prepare foldback capture device for playback.
1250 Parameters: substream - PCM substream class
1252 Returns: Error status
1254 ---------------------------------------------------------------------------*/
1256 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1258 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1259 struct snd_pcm_runtime *runtime = substream->runtime;
1260 struct snd_trident_voice *voice = runtime->private_data;
1261 struct snd_trident_voice *evoice = voice->extra;
1263 spin_lock_irq(&trident->reg_lock);
1265 /* Set channel buffer Address */
1267 voice->LBA = voice->memblk->offset;
1269 voice->LBA = runtime->dma_addr;
1271 /* set target ESO for channel */
1272 voice->ESO = runtime->buffer_size - 1; /* in samples */
1274 /* set sample rate */
1275 voice->Delta = 0x1000;
1276 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1279 voice->CTRL = snd_trident_control_mode(substream);
1284 voice->Pan = 0x7f; /* mute */
1285 voice->Vol = 0x3ff; /* mute */
1289 voice->Attribute = 0;
1291 /* set up capture channel */
1292 outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1294 snd_trident_write_voice_regs(trident, voice);
1296 if (evoice != NULL) {
1297 evoice->Delta = voice->Delta;
1298 evoice->spurious_threshold = voice->spurious_threshold;
1299 evoice->LBA = voice->LBA;
1301 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1302 evoice->CTRL = voice->CTRL;
1304 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1308 evoice->Vol = 0x3ff; /* mute */
1309 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1310 evoice->Pan = 0x7f; /* mute */
1311 evoice->Attribute = 0;
1312 snd_trident_write_voice_regs(trident, evoice);
1314 evoice->isync_mark = runtime->period_size;
1315 evoice->ESO = (runtime->period_size * 2) - 1;
1318 spin_unlock_irq(&trident->reg_lock);
1322 /*---------------------------------------------------------------------------
1323 snd_trident_spdif_hw_params
1325 Description: Set the hardware parameters for the spdif device.
1327 Parameters: substream - PCM substream class
1328 hw_params - hardware parameters
1330 Returns: Error status
1332 ---------------------------------------------------------------------------*/
1334 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1335 struct snd_pcm_hw_params *hw_params)
1337 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1338 unsigned int old_bits = 0, change = 0;
1341 err = snd_trident_allocate_pcm_mem(substream, hw_params);
1345 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1346 err = snd_trident_allocate_evoice(substream, hw_params);
1351 /* prepare SPDIF channel */
1352 spin_lock_irq(&trident->reg_lock);
1353 old_bits = trident->spdif_pcm_bits;
1354 if (old_bits & IEC958_AES0_PROFESSIONAL)
1355 trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
1357 trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
1358 if (params_rate(hw_params) >= 48000) {
1359 trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
1360 trident->spdif_pcm_bits |=
1361 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1362 IEC958_AES0_PRO_FS_48000 :
1363 (IEC958_AES3_CON_FS_48000 << 24);
1365 else if (params_rate(hw_params) >= 44100) {
1366 trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
1367 trident->spdif_pcm_bits |=
1368 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1369 IEC958_AES0_PRO_FS_44100 :
1370 (IEC958_AES3_CON_FS_44100 << 24);
1373 trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
1374 trident->spdif_pcm_bits |=
1375 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1376 IEC958_AES0_PRO_FS_32000 :
1377 (IEC958_AES3_CON_FS_32000 << 24);
1379 change = old_bits != trident->spdif_pcm_bits;
1380 spin_unlock_irq(&trident->reg_lock);
1383 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1388 /*---------------------------------------------------------------------------
1389 snd_trident_spdif_prepare
1391 Description: Prepare SPDIF device for playback.
1393 Parameters: substream - PCM substream class
1395 Returns: Error status
1397 ---------------------------------------------------------------------------*/
1399 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1401 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1402 struct snd_pcm_runtime *runtime = substream->runtime;
1403 struct snd_trident_voice *voice = runtime->private_data;
1404 struct snd_trident_voice *evoice = voice->extra;
1405 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1406 unsigned int RESO, LBAO;
1409 spin_lock_irq(&trident->reg_lock);
1411 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1413 /* set delta (rate) value */
1414 voice->Delta = snd_trident_convert_rate(runtime->rate);
1415 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1417 /* set Loop Back Address */
1418 LBAO = runtime->dma_addr;
1420 voice->LBA = voice->memblk->offset;
1426 voice->isync_mark = runtime->period_size;
1427 voice->isync_max = runtime->buffer_size;
1429 /* set target ESO for channel */
1430 RESO = runtime->buffer_size - 1;
1431 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1434 voice->CTRL = snd_trident_control_mode(substream);
1446 voice->Attribute = 0;
1448 /* prepare surrogate IRQ channel */
1449 snd_trident_write_voice_regs(trident, voice);
1451 outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
1452 outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
1453 outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
1454 outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
1455 outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
1457 /* set SPDIF setting */
1458 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1459 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1463 /* set delta (rate) value */
1464 voice->Delta = 0x800;
1465 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1467 /* set Loop Begin Address */
1469 voice->LBA = voice->memblk->offset;
1471 voice->LBA = runtime->dma_addr;
1474 voice->ESO = runtime->buffer_size - 1; /* in samples */
1475 voice->CTRL = snd_trident_control_mode(substream);
1481 voice->Vol = mix->vol;
1482 voice->RVol = mix->rvol;
1483 voice->CVol = mix->cvol;
1484 voice->Pan = mix->pan;
1485 voice->Attribute = (1<<(30-16))|(7<<(26-16))|
1486 (0<<(24-16))|(0<<(19-16));
1488 snd_trident_write_voice_regs(trident, voice);
1490 if (evoice != NULL) {
1491 evoice->Delta = voice->Delta;
1492 evoice->spurious_threshold = voice->spurious_threshold;
1493 evoice->LBA = voice->LBA;
1495 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1496 evoice->CTRL = voice->CTRL;
1498 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1502 evoice->Vol = 0x3ff; /* mute */
1503 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1504 evoice->Pan = 0x7f; /* mute */
1505 evoice->Attribute = 0;
1506 snd_trident_write_voice_regs(trident, evoice);
1508 evoice->isync_mark = runtime->period_size;
1509 evoice->ESO = (runtime->period_size * 2) - 1;
1512 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1513 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1515 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1516 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1518 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1521 spin_unlock_irq(&trident->reg_lock);
1526 /*---------------------------------------------------------------------------
1529 Description: Start/stop devices
1531 Parameters: substream - PCM substream class
1532 cmd - trigger command (STOP, GO)
1534 Returns: Error status
1536 ---------------------------------------------------------------------------*/
1538 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1542 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1543 struct snd_pcm_substream *s;
1544 unsigned int what, whati, capture_flag, spdif_flag;
1545 struct snd_trident_voice *voice, *evoice;
1546 unsigned int val, go;
1549 case SNDRV_PCM_TRIGGER_START:
1550 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1551 case SNDRV_PCM_TRIGGER_RESUME:
1554 case SNDRV_PCM_TRIGGER_STOP:
1555 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1556 case SNDRV_PCM_TRIGGER_SUSPEND:
1562 what = whati = capture_flag = spdif_flag = 0;
1563 spin_lock(&trident->reg_lock);
1564 val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
1565 snd_pcm_group_for_each_entry(s, substream) {
1566 if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
1567 voice = s->runtime->private_data;
1568 evoice = voice->extra;
1569 what |= 1 << (voice->number & 0x1f);
1570 if (evoice == NULL) {
1571 whati |= 1 << (voice->number & 0x1f);
1573 what |= 1 << (evoice->number & 0x1f);
1574 whati |= 1 << (evoice->number & 0x1f);
1576 evoice->stimer = val;
1580 voice->stimer = val;
1584 snd_pcm_trigger_done(s, substream);
1592 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1593 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1594 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1596 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1597 val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
1598 outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1602 outl(what, TRID_REG(trident, T4D_STOP_B));
1603 val = inl(TRID_REG(trident, T4D_AINTEN_B));
1609 outl(val, TRID_REG(trident, T4D_AINTEN_B));
1611 outl(what, TRID_REG(trident, T4D_START_B));
1613 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1614 outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1616 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1617 outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1619 spin_unlock(&trident->reg_lock);
1623 /*---------------------------------------------------------------------------
1624 snd_trident_playback_pointer
1626 Description: This routine return the playback position
1628 Parameters: substream - PCM substream class
1630 Returns: position of buffer
1632 ---------------------------------------------------------------------------*/
1634 static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1636 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1637 struct snd_pcm_runtime *runtime = substream->runtime;
1638 struct snd_trident_voice *voice = runtime->private_data;
1641 if (!voice->running)
1644 spin_lock(&trident->reg_lock);
1646 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
1648 if (trident->device != TRIDENT_DEVICE_ID_NX) {
1649 cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
1650 } else { // ID_4DWAVE_NX
1651 cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
1654 spin_unlock(&trident->reg_lock);
1656 if (cso >= runtime->buffer_size)
1662 /*---------------------------------------------------------------------------
1663 snd_trident_capture_pointer
1665 Description: This routine return the capture position
1667 Paramters: pcm1 - PCM device class
1669 Returns: position of buffer
1671 ---------------------------------------------------------------------------*/
1673 static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1675 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1676 struct snd_pcm_runtime *runtime = substream->runtime;
1677 struct snd_trident_voice *voice = runtime->private_data;
1678 unsigned int result;
1680 if (!voice->running)
1683 result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
1684 if (runtime->channels > 1)
1687 result = runtime->buffer_size - result;
1692 /*---------------------------------------------------------------------------
1693 snd_trident_spdif_pointer
1695 Description: This routine return the SPDIF playback position
1697 Parameters: substream - PCM substream class
1699 Returns: position of buffer
1701 ---------------------------------------------------------------------------*/
1703 static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1705 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1706 struct snd_pcm_runtime *runtime = substream->runtime;
1707 struct snd_trident_voice *voice = runtime->private_data;
1708 unsigned int result;
1710 if (!voice->running)
1713 result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
1719 * Playback support device description
1722 static struct snd_pcm_hardware snd_trident_playback =
1724 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1725 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1726 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1727 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1728 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1729 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1730 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1735 .buffer_bytes_max = (256*1024),
1736 .period_bytes_min = 64,
1737 .period_bytes_max = (256*1024),
1739 .periods_max = 1024,
1744 * Capture support device description
1747 static struct snd_pcm_hardware snd_trident_capture =
1749 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1750 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1751 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1752 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1753 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1754 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1755 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1760 .buffer_bytes_max = (128*1024),
1761 .period_bytes_min = 64,
1762 .period_bytes_max = (128*1024),
1764 .periods_max = 1024,
1769 * Foldback capture support device description
1772 static struct snd_pcm_hardware snd_trident_foldback =
1774 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1775 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1776 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1777 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1778 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1779 .rates = SNDRV_PCM_RATE_48000,
1784 .buffer_bytes_max = (128*1024),
1785 .period_bytes_min = 64,
1786 .period_bytes_max = (128*1024),
1788 .periods_max = 1024,
1793 * SPDIF playback support device description
1796 static struct snd_pcm_hardware snd_trident_spdif =
1798 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1799 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1800 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1801 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1802 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1803 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1804 SNDRV_PCM_RATE_48000),
1809 .buffer_bytes_max = (128*1024),
1810 .period_bytes_min = 64,
1811 .period_bytes_max = (128*1024),
1813 .periods_max = 1024,
1817 static struct snd_pcm_hardware snd_trident_spdif_7018 =
1819 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1820 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1821 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1822 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1823 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1824 .rates = SNDRV_PCM_RATE_48000,
1829 .buffer_bytes_max = (128*1024),
1830 .period_bytes_min = 64,
1831 .period_bytes_max = (128*1024),
1833 .periods_max = 1024,
1837 static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1839 struct snd_trident_voice *voice = runtime->private_data;
1840 struct snd_trident *trident;
1843 trident = voice->trident;
1844 snd_trident_free_voice(trident, voice);
1848 static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1850 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1851 struct snd_pcm_runtime *runtime = substream->runtime;
1852 struct snd_trident_voice *voice;
1854 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1857 snd_trident_pcm_mixer_build(trident, voice, substream);
1858 voice->substream = substream;
1859 runtime->private_data = voice;
1860 runtime->private_free = snd_trident_pcm_free_substream;
1861 runtime->hw = snd_trident_playback;
1862 snd_pcm_set_sync(substream);
1863 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1867 /*---------------------------------------------------------------------------
1868 snd_trident_playback_close
1870 Description: This routine will close the 4DWave playback device. For now
1871 we will simply free the dma transfer buffer.
1873 Parameters: substream - PCM substream class
1875 ---------------------------------------------------------------------------*/
1876 static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1878 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1879 struct snd_pcm_runtime *runtime = substream->runtime;
1880 struct snd_trident_voice *voice = runtime->private_data;
1882 snd_trident_pcm_mixer_free(trident, voice, substream);
1886 /*---------------------------------------------------------------------------
1887 snd_trident_spdif_open
1889 Description: This routine will open the 4DWave SPDIF device.
1891 Parameters: substream - PCM substream class
1893 Returns: status - success or failure flag
1895 ---------------------------------------------------------------------------*/
1897 static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1899 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1900 struct snd_trident_voice *voice;
1901 struct snd_pcm_runtime *runtime = substream->runtime;
1903 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1907 voice->substream = substream;
1908 spin_lock_irq(&trident->reg_lock);
1909 trident->spdif_pcm_bits = trident->spdif_bits;
1910 spin_unlock_irq(&trident->reg_lock);
1912 runtime->private_data = voice;
1913 runtime->private_free = snd_trident_pcm_free_substream;
1914 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1915 runtime->hw = snd_trident_spdif;
1917 runtime->hw = snd_trident_spdif_7018;
1920 trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1921 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1922 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1924 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1929 /*---------------------------------------------------------------------------
1930 snd_trident_spdif_close
1932 Description: This routine will close the 4DWave SPDIF device.
1934 Parameters: substream - PCM substream class
1936 ---------------------------------------------------------------------------*/
1938 static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1940 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1943 spin_lock_irq(&trident->reg_lock);
1944 // restore default SPDIF setting
1945 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1946 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1947 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
1949 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
1950 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1951 if (trident->spdif_ctrl) {
1956 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1958 spin_unlock_irq(&trident->reg_lock);
1959 trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1960 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1961 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1965 /*---------------------------------------------------------------------------
1966 snd_trident_capture_open
1968 Description: This routine will open the 4DWave capture device.
1970 Parameters: substream - PCM substream class
1972 Returns: status - success or failure flag
1974 ---------------------------------------------------------------------------*/
1976 static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1978 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1979 struct snd_trident_voice *voice;
1980 struct snd_pcm_runtime *runtime = substream->runtime;
1982 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1986 voice->substream = substream;
1987 runtime->private_data = voice;
1988 runtime->private_free = snd_trident_pcm_free_substream;
1989 runtime->hw = snd_trident_capture;
1990 snd_pcm_set_sync(substream);
1991 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1995 /*---------------------------------------------------------------------------
1996 snd_trident_capture_close
1998 Description: This routine will close the 4DWave capture device. For now
1999 we will simply free the dma transfer buffer.
2001 Parameters: substream - PCM substream class
2003 ---------------------------------------------------------------------------*/
2004 static int snd_trident_capture_close(struct snd_pcm_substream *substream)
2009 /*---------------------------------------------------------------------------
2010 snd_trident_foldback_open
2012 Description: This routine will open the 4DWave foldback capture device.
2014 Parameters: substream - PCM substream class
2016 Returns: status - success or failure flag
2018 ---------------------------------------------------------------------------*/
2020 static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
2022 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2023 struct snd_trident_voice *voice;
2024 struct snd_pcm_runtime *runtime = substream->runtime;
2026 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
2029 voice->foldback_chan = substream->number;
2030 voice->substream = substream;
2031 runtime->private_data = voice;
2032 runtime->private_free = snd_trident_pcm_free_substream;
2033 runtime->hw = snd_trident_foldback;
2034 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
2038 /*---------------------------------------------------------------------------
2039 snd_trident_foldback_close
2041 Description: This routine will close the 4DWave foldback capture device.
2042 For now we will simply free the dma transfer buffer.
2044 Parameters: substream - PCM substream class
2046 ---------------------------------------------------------------------------*/
2047 static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
2049 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2050 struct snd_trident_voice *voice;
2051 struct snd_pcm_runtime *runtime = substream->runtime;
2052 voice = runtime->private_data;
2054 /* stop capture channel */
2055 spin_lock_irq(&trident->reg_lock);
2056 outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
2057 spin_unlock_irq(&trident->reg_lock);
2061 /*---------------------------------------------------------------------------
2063 ---------------------------------------------------------------------------*/
2065 static struct snd_pcm_ops snd_trident_playback_ops = {
2066 .open = snd_trident_playback_open,
2067 .close = snd_trident_playback_close,
2068 .ioctl = snd_trident_ioctl,
2069 .hw_params = snd_trident_hw_params,
2070 .hw_free = snd_trident_hw_free,
2071 .prepare = snd_trident_playback_prepare,
2072 .trigger = snd_trident_trigger,
2073 .pointer = snd_trident_playback_pointer,
2076 static struct snd_pcm_ops snd_trident_nx_playback_ops = {
2077 .open = snd_trident_playback_open,
2078 .close = snd_trident_playback_close,
2079 .ioctl = snd_trident_ioctl,
2080 .hw_params = snd_trident_hw_params,
2081 .hw_free = snd_trident_hw_free,
2082 .prepare = snd_trident_playback_prepare,
2083 .trigger = snd_trident_trigger,
2084 .pointer = snd_trident_playback_pointer,
2085 .page = snd_pcm_sgbuf_ops_page,
2088 static struct snd_pcm_ops snd_trident_capture_ops = {
2089 .open = snd_trident_capture_open,
2090 .close = snd_trident_capture_close,
2091 .ioctl = snd_trident_ioctl,
2092 .hw_params = snd_trident_capture_hw_params,
2093 .hw_free = snd_trident_hw_free,
2094 .prepare = snd_trident_capture_prepare,
2095 .trigger = snd_trident_trigger,
2096 .pointer = snd_trident_capture_pointer,
2099 static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
2100 .open = snd_trident_capture_open,
2101 .close = snd_trident_capture_close,
2102 .ioctl = snd_trident_ioctl,
2103 .hw_params = snd_trident_si7018_capture_hw_params,
2104 .hw_free = snd_trident_si7018_capture_hw_free,
2105 .prepare = snd_trident_si7018_capture_prepare,
2106 .trigger = snd_trident_trigger,
2107 .pointer = snd_trident_playback_pointer,
2110 static struct snd_pcm_ops snd_trident_foldback_ops = {
2111 .open = snd_trident_foldback_open,
2112 .close = snd_trident_foldback_close,
2113 .ioctl = snd_trident_ioctl,
2114 .hw_params = snd_trident_hw_params,
2115 .hw_free = snd_trident_hw_free,
2116 .prepare = snd_trident_foldback_prepare,
2117 .trigger = snd_trident_trigger,
2118 .pointer = snd_trident_playback_pointer,
2121 static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
2122 .open = snd_trident_foldback_open,
2123 .close = snd_trident_foldback_close,
2124 .ioctl = snd_trident_ioctl,
2125 .hw_params = snd_trident_hw_params,
2126 .hw_free = snd_trident_hw_free,
2127 .prepare = snd_trident_foldback_prepare,
2128 .trigger = snd_trident_trigger,
2129 .pointer = snd_trident_playback_pointer,
2130 .page = snd_pcm_sgbuf_ops_page,
2133 static struct snd_pcm_ops snd_trident_spdif_ops = {
2134 .open = snd_trident_spdif_open,
2135 .close = snd_trident_spdif_close,
2136 .ioctl = snd_trident_ioctl,
2137 .hw_params = snd_trident_spdif_hw_params,
2138 .hw_free = snd_trident_hw_free,
2139 .prepare = snd_trident_spdif_prepare,
2140 .trigger = snd_trident_trigger,
2141 .pointer = snd_trident_spdif_pointer,
2144 static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
2145 .open = snd_trident_spdif_open,
2146 .close = snd_trident_spdif_close,
2147 .ioctl = snd_trident_ioctl,
2148 .hw_params = snd_trident_spdif_hw_params,
2149 .hw_free = snd_trident_hw_free,
2150 .prepare = snd_trident_spdif_prepare,
2151 .trigger = snd_trident_trigger,
2152 .pointer = snd_trident_playback_pointer,
2155 /*---------------------------------------------------------------------------
2158 Description: This routine registers the 4DWave device for PCM support.
2160 Paramters: trident - pointer to target device class for 4DWave.
2164 ---------------------------------------------------------------------------*/
2166 int __devinit snd_trident_pcm(struct snd_trident * trident,
2167 int device, struct snd_pcm ** rpcm)
2169 struct snd_pcm *pcm;
2174 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
2177 pcm->private_data = trident;
2179 if (trident->tlb.entries) {
2180 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
2182 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
2184 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2185 trident->device != TRIDENT_DEVICE_ID_SI7018 ?
2186 &snd_trident_capture_ops :
2187 &snd_trident_si7018_capture_ops);
2189 pcm->info_flags = 0;
2190 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
2191 strcpy(pcm->name, "Trident 4DWave");
2194 if (trident->tlb.entries) {
2195 struct snd_pcm_substream *substream;
2196 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
2197 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
2198 snd_dma_pci_data(trident->pci),
2200 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2201 SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
2204 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2205 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2213 /*---------------------------------------------------------------------------
2214 snd_trident_foldback_pcm
2216 Description: This routine registers the 4DWave device for foldback PCM support.
2218 Paramters: trident - pointer to target device class for 4DWave.
2222 ---------------------------------------------------------------------------*/
2224 int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
2225 int device, struct snd_pcm ** rpcm)
2227 struct snd_pcm *foldback;
2230 struct snd_pcm_substream *substream;
2234 if (trident->device == TRIDENT_DEVICE_ID_NX)
2236 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
2239 foldback->private_data = trident;
2240 if (trident->tlb.entries)
2241 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
2243 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
2244 foldback->info_flags = 0;
2245 strcpy(foldback->name, "Trident 4DWave");
2246 substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2247 strcpy(substream->name, "Front Mixer");
2248 substream = substream->next;
2249 strcpy(substream->name, "Reverb Mixer");
2250 substream = substream->next;
2251 strcpy(substream->name, "Chorus Mixer");
2252 if (num_chan == 4) {
2253 substream = substream->next;
2254 strcpy(substream->name, "Second AC'97 ADC");
2256 trident->foldback = foldback;
2258 if (trident->tlb.entries)
2259 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
2260 snd_dma_pci_data(trident->pci), 0, 128*1024);
2262 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
2263 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2270 /*---------------------------------------------------------------------------
2273 Description: This routine registers the 4DWave-NX device for SPDIF support.
2275 Paramters: trident - pointer to target device class for 4DWave-NX.
2279 ---------------------------------------------------------------------------*/
2281 int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
2282 int device, struct snd_pcm ** rpcm)
2284 struct snd_pcm *spdif;
2289 if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
2292 spdif->private_data = trident;
2293 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2294 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
2296 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
2298 spdif->info_flags = 0;
2299 strcpy(spdif->name, "Trident 4DWave IEC958");
2300 trident->spdif = spdif;
2302 snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2314 /*---------------------------------------------------------------------------
2315 snd_trident_spdif_control
2317 Description: enable/disable S/PDIF out from ac97 mixer
2318 ---------------------------------------------------------------------------*/
2320 static int snd_trident_spdif_control_info(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_info *uinfo)
2323 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2325 uinfo->value.integer.min = 0;
2326 uinfo->value.integer.max = 1;
2330 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2333 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2336 spin_lock_irq(&trident->reg_lock);
2337 val = trident->spdif_ctrl;
2338 ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2339 spin_unlock_irq(&trident->reg_lock);
2343 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
2346 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2350 val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2351 spin_lock_irq(&trident->reg_lock);
2352 /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2353 change = trident->spdif_ctrl != val;
2354 trident->spdif_ctrl = val;
2355 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2356 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2357 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2358 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2361 if (trident->spdif == NULL) {
2363 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2364 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2367 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2370 spin_unlock_irq(&trident->reg_lock);
2374 static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
2376 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2377 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2378 .info = snd_trident_spdif_control_info,
2379 .get = snd_trident_spdif_control_get,
2380 .put = snd_trident_spdif_control_put,
2381 .private_value = 0x28,
2384 /*---------------------------------------------------------------------------
2385 snd_trident_spdif_default
2387 Description: put/get the S/PDIF default settings
2388 ---------------------------------------------------------------------------*/
2390 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_info *uinfo)
2393 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2398 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2399 struct snd_ctl_elem_value *ucontrol)
2401 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2403 spin_lock_irq(&trident->reg_lock);
2404 ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2405 ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2406 ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2407 ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2408 spin_unlock_irq(&trident->reg_lock);
2412 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_value *ucontrol)
2415 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2419 val = (ucontrol->value.iec958.status[0] << 0) |
2420 (ucontrol->value.iec958.status[1] << 8) |
2421 (ucontrol->value.iec958.status[2] << 16) |
2422 (ucontrol->value.iec958.status[3] << 24);
2423 spin_lock_irq(&trident->reg_lock);
2424 change = trident->spdif_bits != val;
2425 trident->spdif_bits = val;
2426 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2427 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2428 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2430 if (trident->spdif == NULL)
2431 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2433 spin_unlock_irq(&trident->reg_lock);
2437 static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
2439 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2440 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2441 .info = snd_trident_spdif_default_info,
2442 .get = snd_trident_spdif_default_get,
2443 .put = snd_trident_spdif_default_put
2446 /*---------------------------------------------------------------------------
2447 snd_trident_spdif_mask
2449 Description: put/get the S/PDIF mask
2450 ---------------------------------------------------------------------------*/
2452 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2453 struct snd_ctl_elem_info *uinfo)
2455 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2460 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_value *ucontrol)
2463 ucontrol->value.iec958.status[0] = 0xff;
2464 ucontrol->value.iec958.status[1] = 0xff;
2465 ucontrol->value.iec958.status[2] = 0xff;
2466 ucontrol->value.iec958.status[3] = 0xff;
2470 static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
2472 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2473 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2474 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2475 .info = snd_trident_spdif_mask_info,
2476 .get = snd_trident_spdif_mask_get,
2479 /*---------------------------------------------------------------------------
2480 snd_trident_spdif_stream
2482 Description: put/get the S/PDIF stream settings
2483 ---------------------------------------------------------------------------*/
2485 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_info *uinfo)
2488 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2493 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_value *ucontrol)
2496 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2498 spin_lock_irq(&trident->reg_lock);
2499 ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2500 ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2501 ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2502 ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2503 spin_unlock_irq(&trident->reg_lock);
2507 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2508 struct snd_ctl_elem_value *ucontrol)
2510 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2514 val = (ucontrol->value.iec958.status[0] << 0) |
2515 (ucontrol->value.iec958.status[1] << 8) |
2516 (ucontrol->value.iec958.status[2] << 16) |
2517 (ucontrol->value.iec958.status[3] << 24);
2518 spin_lock_irq(&trident->reg_lock);
2519 change = trident->spdif_pcm_bits != val;
2520 trident->spdif_pcm_bits = val;
2521 if (trident->spdif != NULL) {
2522 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2523 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2525 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2528 spin_unlock_irq(&trident->reg_lock);
2532 static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
2534 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2535 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2536 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2537 .info = snd_trident_spdif_stream_info,
2538 .get = snd_trident_spdif_stream_get,
2539 .put = snd_trident_spdif_stream_put
2542 /*---------------------------------------------------------------------------
2543 snd_trident_ac97_control
2545 Description: enable/disable rear path for ac97
2546 ---------------------------------------------------------------------------*/
2548 static int snd_trident_ac97_control_info(struct snd_kcontrol *kcontrol,
2549 struct snd_ctl_elem_info *uinfo)
2551 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2553 uinfo->value.integer.min = 0;
2554 uinfo->value.integer.max = 1;
2558 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_value *ucontrol)
2561 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2564 spin_lock_irq(&trident->reg_lock);
2565 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2566 ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2567 spin_unlock_irq(&trident->reg_lock);
2571 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2572 struct snd_ctl_elem_value *ucontrol)
2574 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2578 spin_lock_irq(&trident->reg_lock);
2579 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2580 val &= ~(1 << kcontrol->private_value);
2581 if (ucontrol->value.integer.value[0])
2582 val |= 1 << kcontrol->private_value;
2583 change = val != trident->ac97_ctrl;
2584 trident->ac97_ctrl = val;
2585 outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2586 spin_unlock_irq(&trident->reg_lock);
2590 static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
2592 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2593 .name = "Rear Path",
2594 .info = snd_trident_ac97_control_info,
2595 .get = snd_trident_ac97_control_get,
2596 .put = snd_trident_ac97_control_put,
2600 /*---------------------------------------------------------------------------
2601 snd_trident_vol_control
2603 Description: wave & music volume control
2604 ---------------------------------------------------------------------------*/
2606 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_info *uinfo)
2609 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2611 uinfo->value.integer.min = 0;
2612 uinfo->value.integer.max = 255;
2616 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2617 struct snd_ctl_elem_value *ucontrol)
2619 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2622 val = trident->musicvol_wavevol;
2623 ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2624 ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2628 static const DECLARE_TLV_DB_SCALE(db_scale_gvol, -6375, 25, 0);
2630 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_value *ucontrol)
2633 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2637 spin_lock_irq(&trident->reg_lock);
2638 val = trident->musicvol_wavevol;
2639 val &= ~(0xffff << kcontrol->private_value);
2640 val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2641 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2642 change = val != trident->musicvol_wavevol;
2643 outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2644 spin_unlock_irq(&trident->reg_lock);
2648 static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
2650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651 .name = "Music Playback Volume",
2652 .info = snd_trident_vol_control_info,
2653 .get = snd_trident_vol_control_get,
2654 .put = snd_trident_vol_control_put,
2655 .private_value = 16,
2656 .tlv = { .p = db_scale_gvol },
2659 static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
2661 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2662 .name = "Wave Playback Volume",
2663 .info = snd_trident_vol_control_info,
2664 .get = snd_trident_vol_control_get,
2665 .put = snd_trident_vol_control_put,
2667 .tlv = { .p = db_scale_gvol },
2670 /*---------------------------------------------------------------------------
2671 snd_trident_pcm_vol_control
2673 Description: PCM front volume control
2674 ---------------------------------------------------------------------------*/
2676 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_info *uinfo)
2679 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2681 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2683 uinfo->value.integer.min = 0;
2684 uinfo->value.integer.max = 255;
2685 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2686 uinfo->value.integer.max = 1023;
2690 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2693 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2694 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2696 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2697 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2699 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2704 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2705 struct snd_ctl_elem_value *ucontrol)
2707 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2708 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2712 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2713 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2715 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2717 spin_lock_irq(&trident->reg_lock);
2718 change = val != mix->vol;
2720 if (mix->voice != NULL)
2721 snd_trident_write_vol_reg(trident, mix->voice, val);
2722 spin_unlock_irq(&trident->reg_lock);
2726 static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
2728 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2729 .name = "PCM Front Playback Volume",
2730 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2732 .info = snd_trident_pcm_vol_control_info,
2733 .get = snd_trident_pcm_vol_control_get,
2734 .put = snd_trident_pcm_vol_control_put,
2735 /* FIXME: no tlv yet */
2738 /*---------------------------------------------------------------------------
2739 snd_trident_pcm_pan_control
2741 Description: PCM front pan control
2742 ---------------------------------------------------------------------------*/
2744 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_info *uinfo)
2747 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2749 uinfo->value.integer.min = 0;
2750 uinfo->value.integer.max = 127;
2754 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2757 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2758 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2760 ucontrol->value.integer.value[0] = mix->pan;
2761 if (ucontrol->value.integer.value[0] & 0x40) {
2762 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2764 ucontrol->value.integer.value[0] |= 0x40;
2769 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2770 struct snd_ctl_elem_value *ucontrol)
2772 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2773 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2777 if (ucontrol->value.integer.value[0] & 0x40)
2778 val = ucontrol->value.integer.value[0] & 0x3f;
2780 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2781 spin_lock_irq(&trident->reg_lock);
2782 change = val != mix->pan;
2784 if (mix->voice != NULL)
2785 snd_trident_write_pan_reg(trident, mix->voice, val);
2786 spin_unlock_irq(&trident->reg_lock);
2790 static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
2792 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2793 .name = "PCM Pan Playback Control",
2794 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2796 .info = snd_trident_pcm_pan_control_info,
2797 .get = snd_trident_pcm_pan_control_get,
2798 .put = snd_trident_pcm_pan_control_put,
2801 /*---------------------------------------------------------------------------
2802 snd_trident_pcm_rvol_control
2804 Description: PCM reverb volume control
2805 ---------------------------------------------------------------------------*/
2807 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_info *uinfo)
2810 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2812 uinfo->value.integer.min = 0;
2813 uinfo->value.integer.max = 127;
2817 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_value *ucontrol)
2820 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2821 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2823 ucontrol->value.integer.value[0] = 127 - mix->rvol;
2827 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2828 struct snd_ctl_elem_value *ucontrol)
2830 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2831 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2835 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2836 spin_lock_irq(&trident->reg_lock);
2837 change = val != mix->rvol;
2839 if (mix->voice != NULL)
2840 snd_trident_write_rvol_reg(trident, mix->voice, val);
2841 spin_unlock_irq(&trident->reg_lock);
2845 static const DECLARE_TLV_DB_SCALE(db_scale_crvol, -3175, 25, 1);
2847 static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
2849 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2850 .name = "PCM Reverb Playback Volume",
2851 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2853 .info = snd_trident_pcm_rvol_control_info,
2854 .get = snd_trident_pcm_rvol_control_get,
2855 .put = snd_trident_pcm_rvol_control_put,
2856 .tlv = { .p = db_scale_crvol },
2859 /*---------------------------------------------------------------------------
2860 snd_trident_pcm_cvol_control
2862 Description: PCM chorus volume control
2863 ---------------------------------------------------------------------------*/
2865 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2866 struct snd_ctl_elem_info *uinfo)
2868 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2870 uinfo->value.integer.min = 0;
2871 uinfo->value.integer.max = 127;
2875 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_value *ucontrol)
2878 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2879 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2881 ucontrol->value.integer.value[0] = 127 - mix->cvol;
2885 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2886 struct snd_ctl_elem_value *ucontrol)
2888 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2889 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2893 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2894 spin_lock_irq(&trident->reg_lock);
2895 change = val != mix->cvol;
2897 if (mix->voice != NULL)
2898 snd_trident_write_cvol_reg(trident, mix->voice, val);
2899 spin_unlock_irq(&trident->reg_lock);
2903 static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
2905 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2906 .name = "PCM Chorus Playback Volume",
2907 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2909 .info = snd_trident_pcm_cvol_control_info,
2910 .get = snd_trident_pcm_cvol_control_get,
2911 .put = snd_trident_pcm_cvol_control_put,
2912 .tlv = { .p = db_scale_crvol },
2915 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2916 struct snd_kcontrol *kctl,
2917 int num, int activate)
2919 struct snd_ctl_elem_id id;
2924 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2926 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2927 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2928 SNDRV_CTL_EVENT_MASK_INFO,
2929 snd_ctl_build_ioff(&id, kctl, num));
2932 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2933 struct snd_trident_pcm_mixer *tmix,
2934 int num, int activate)
2936 snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2937 snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2938 snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2939 snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2942 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2943 struct snd_trident_voice *voice,
2944 struct snd_pcm_substream *substream)
2946 struct snd_trident_pcm_mixer *tmix;
2948 snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
2949 tmix = &trident->pcm_mixer[substream->number];
2950 tmix->voice = voice;
2951 tmix->vol = T4D_DEFAULT_PCM_VOL;
2952 tmix->pan = T4D_DEFAULT_PCM_PAN;
2953 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2954 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2955 snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2959 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2961 struct snd_trident_pcm_mixer *tmix;
2963 snd_assert(trident != NULL && substream != NULL, return -EINVAL);
2964 tmix = &trident->pcm_mixer[substream->number];
2966 snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2970 /*---------------------------------------------------------------------------
2973 Description: This routine registers the 4DWave device for mixer support.
2975 Paramters: trident - pointer to target device class for 4DWave.
2979 ---------------------------------------------------------------------------*/
2981 static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
2983 struct snd_ac97_template _ac97;
2984 struct snd_card *card = trident->card;
2985 struct snd_kcontrol *kctl;
2986 struct snd_ctl_elem_value *uctl;
2987 int idx, err, retries = 2;
2988 static struct snd_ac97_bus_ops ops = {
2989 .write = snd_trident_codec_write,
2990 .read = snd_trident_codec_read,
2993 uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
2997 if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
3000 memset(&_ac97, 0, sizeof(_ac97));
3001 _ac97.private_data = trident;
3002 trident->ac97_detect = 1;
3005 if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
3006 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3007 if ((err = snd_trident_sis_reset(trident)) < 0)
3016 /* secondary codec? */
3017 if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
3018 (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
3020 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
3022 snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
3023 #if 0 // only for my testing purpose --jk
3025 struct snd_ac97 *mc97;
3026 err = snd_ac97_modem(trident->card, &_ac97, &mc97);
3028 snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
3033 trident->ac97_detect = 0;
3035 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3036 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3038 kctl->put(kctl, uctl);
3039 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3041 kctl->put(kctl, uctl);
3042 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3044 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3047 for (idx = 0; idx < 32; idx++) {
3048 struct snd_trident_pcm_mixer *tmix;
3050 tmix = &trident->pcm_mixer[idx];
3053 if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3055 if ((err = snd_ctl_add(card, trident->ctl_vol)))
3058 if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3060 if ((err = snd_ctl_add(card, trident->ctl_pan)))
3063 if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3065 if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3068 if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3070 if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3073 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3074 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3076 kctl->put(kctl, uctl);
3078 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3080 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3085 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3087 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3089 idx = kctl->id.index;
3090 if ((err = snd_ctl_add(card, kctl)) < 0)
3092 kctl->put(kctl, uctl);
3094 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3099 kctl->id.index = idx;
3100 kctl->id.device = pcm_spdif_device;
3101 if ((err = snd_ctl_add(card, kctl)) < 0)
3104 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3109 kctl->id.index = idx;
3110 kctl->id.device = pcm_spdif_device;
3111 if ((err = snd_ctl_add(card, kctl)) < 0)
3114 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3119 kctl->id.index = idx;
3120 kctl->id.device = pcm_spdif_device;
3121 if ((err = snd_ctl_add(card, kctl)) < 0)
3123 trident->spdif_pcm_ctl = kctl;
3139 * gameport interface
3142 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
3144 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3146 struct snd_trident *chip = gameport_get_port_data(gameport);
3148 snd_assert(chip, return 0);
3149 return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3152 static void snd_trident_gameport_trigger(struct gameport *gameport)
3154 struct snd_trident *chip = gameport_get_port_data(gameport);
3156 snd_assert(chip, return);
3157 outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3160 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3162 struct snd_trident *chip = gameport_get_port_data(gameport);
3165 snd_assert(chip, return 0);
3167 *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3169 for (i = 0; i < 4; i++) {
3170 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3171 if (axes[i] == 0xffff) axes[i] = -1;
3177 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3179 struct snd_trident *chip = gameport_get_port_data(gameport);
3181 snd_assert(chip, return 0);
3184 case GAMEPORT_MODE_COOKED:
3185 outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3188 case GAMEPORT_MODE_RAW:
3189 outb(0, TRID_REG(chip, GAMEPORT_GCR));
3196 int __devinit snd_trident_create_gameport(struct snd_trident *chip)
3198 struct gameport *gp;
3200 chip->gameport = gp = gameport_allocate_port();
3202 printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
3206 gameport_set_name(gp, "Trident 4DWave");
3207 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3208 gameport_set_dev_parent(gp, &chip->pci->dev);
3210 gameport_set_port_data(gp, chip);
3212 gp->read = snd_trident_gameport_read;
3213 gp->trigger = snd_trident_gameport_trigger;
3214 gp->cooked_read = snd_trident_gameport_cooked_read;
3215 gp->open = snd_trident_gameport_open;
3217 gameport_register_port(gp);
3222 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3224 if (chip->gameport) {
3225 gameport_unregister_port(chip->gameport);
3226 chip->gameport = NULL;
3230 int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3231 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3232 #endif /* CONFIG_GAMEPORT */
3237 static inline void do_delay(struct snd_trident *chip)
3239 schedule_timeout_uninterruptible(1);
3246 static int snd_trident_sis_reset(struct snd_trident *trident)
3248 unsigned long end_time;
3252 r = trident->in_suspend ? 0 : 2; /* count of retries */
3254 pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
3256 pci_write_config_byte(trident->pci, 0x46, 0x00);
3258 /* disable AC97 GPIO interrupt */
3259 outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3260 /* initialize serial interface, force cold reset */
3261 i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3262 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3264 /* remove cold reset */
3266 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3268 /* wait, until the codec is ready */
3269 end_time = (jiffies + (HZ * 3) / 4) + 1;
3271 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3274 } while (time_after_eq(end_time, jiffies));
3275 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3277 end_time = jiffies + HZ;
3280 } while (time_after_eq(end_time, jiffies));
3281 goto __si7018_retry;
3284 /* wait for the second codec */
3286 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3289 } while (time_after_eq(end_time, jiffies));
3290 /* enable 64 channel mode */
3291 outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3299 static void snd_trident_proc_read(struct snd_info_entry *entry,
3300 struct snd_info_buffer *buffer)
3302 struct snd_trident *trident = entry->private_data;
3305 switch (trident->device) {
3306 case TRIDENT_DEVICE_ID_SI7018:
3307 s = "SiS 7018 Audio";
3309 case TRIDENT_DEVICE_ID_DX:
3310 s = "Trident 4DWave PCI DX";
3312 case TRIDENT_DEVICE_ID_NX:
3313 s = "Trident 4DWave PCI NX";
3318 snd_iprintf(buffer, "%s\n\n", s);
3319 snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
3320 snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3321 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3322 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3323 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3324 snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3325 if (trident->tlb.entries) {
3326 snd_iprintf(buffer,"\nVirtual Memory\n");
3327 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3328 snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
3329 snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3332 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3333 snd_iprintf(buffer,"\nWavetable Synth\n");
3334 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
3335 snd_iprintf(buffer, "Memory Used : %d\n", trident->synth.current_size);
3336 snd_iprintf(buffer, "Memory Free : %d\n", (trident->synth.max_size-trident->synth.current_size));
3340 static void __devinit snd_trident_proc_init(struct snd_trident * trident)
3342 struct snd_info_entry *entry;
3343 const char *s = "trident";
3345 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3347 if (! snd_card_proc_new(trident->card, s, &entry))
3348 snd_info_set_text_ops(entry, trident, snd_trident_proc_read);
3351 static int snd_trident_dev_free(struct snd_device *device)
3353 struct snd_trident *trident = device->device_data;
3354 return snd_trident_free(trident);
3357 /*---------------------------------------------------------------------------
3358 snd_trident_tlb_alloc
3360 Description: Allocate and set up the TLB page table on 4D NX.
3361 Each entry has 4 bytes (physical PCI address).
3363 Paramters: trident - pointer to target device class for 4DWave.
3365 Returns: 0 or negative error code
3367 ---------------------------------------------------------------------------*/
3369 static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
3373 /* TLB array must be aligned to 16kB !!! so we allocate
3374 32kB region and correct offset when necessary */
3376 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3377 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3378 snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
3381 trident->tlb.entries = (unsigned int*)ALIGN((unsigned long)trident->tlb.buffer.area, SNDRV_TRIDENT_MAX_PAGES * 4);
3382 trident->tlb.entries_dmaaddr = ALIGN(trident->tlb.buffer.addr, SNDRV_TRIDENT_MAX_PAGES * 4);
3383 /* allocate shadow TLB page table (virtual addresses) */
3384 trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
3385 if (trident->tlb.shadow_entries == NULL) {
3386 snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
3389 /* allocate and setup silent page and initialise TLB entries */
3390 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3391 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3392 snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
3395 memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3396 for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3397 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3398 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3401 /* use emu memory block manager code to manage tlb page allocation */
3402 trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3403 if (trident->tlb.memhdr == NULL)
3406 trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3411 * initialize 4D DX chip
3414 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3416 outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3417 outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3418 outl(0, TRID_REG(trident, T4D_AINTEN_A));
3419 outl(0, TRID_REG(trident, T4D_AINTEN_B));
3422 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3424 struct pci_dev *pci = trident->pci;
3425 unsigned long end_time;
3427 /* reset the legacy configuration and whole audio/wavetable block */
3428 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3429 pci_write_config_byte(pci, 0x44, 0); /* ports */
3430 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3431 pci_write_config_byte(pci, 0x46, 4); /* reset */
3433 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3436 /* warm reset of the AC'97 codec */
3437 outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3439 outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3440 /* DAC on, disable SB IRQ and try to force ADC valid signal */
3441 trident->ac97_ctrl = 0x0000004a;
3442 outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3443 /* wait, until the codec is ready */
3444 end_time = (jiffies + (HZ * 3) / 4) + 1;
3446 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3449 } while (time_after_eq(end_time, jiffies));
3450 snd_printk(KERN_ERR "AC'97 codec ready error\n");
3454 snd_trident_stop_all_voices(trident);
3460 * initialize 4D NX chip
3462 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3464 struct pci_dev *pci = trident->pci;
3465 unsigned long end_time;
3467 /* reset the legacy configuration and whole audio/wavetable block */
3468 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3469 pci_write_config_byte(pci, 0x44, 0); /* ports */
3470 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3472 pci_write_config_byte(pci, 0x46, 1); /* reset */
3474 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3477 /* warm reset of the AC'97 codec */
3478 outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3480 outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3481 /* wait, until the codec is ready */
3482 end_time = (jiffies + (HZ * 3) / 4) + 1;
3484 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3487 } while (time_after_eq(end_time, jiffies));
3488 snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3493 trident->ac97_ctrl = 0x00000002;
3494 outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3495 /* disable SB IRQ */
3496 outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3498 snd_trident_stop_all_voices(trident);
3500 if (trident->tlb.entries != NULL) {
3502 /* enable virtual addressing via TLB */
3503 i = trident->tlb.entries_dmaaddr;
3505 outl(i, TRID_REG(trident, NX_TLBC));
3507 outl(0, TRID_REG(trident, NX_TLBC));
3509 /* initialize S/PDIF */
3510 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3511 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3517 * initialize sis7018 chip
3519 static int snd_trident_sis_init(struct snd_trident *trident)
3523 if ((err = snd_trident_sis_reset(trident)) < 0)
3526 snd_trident_stop_all_voices(trident);
3528 /* initialize S/PDIF */
3529 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3534 /*---------------------------------------------------------------------------
3537 Description: This routine will create the device specific class for
3538 the 4DWave card. It will also perform basic initialization.
3540 Paramters: card - which card to create
3541 pci - interface to PCI bus resource info
3542 dma1ptr - playback dma buffer
3543 dma2ptr - capture dma buffer
3544 irqptr - interrupt resource info
3546 Returns: 4DWave device class private data
3548 ---------------------------------------------------------------------------*/
3550 int __devinit snd_trident_create(struct snd_card *card,
3551 struct pci_dev *pci,
3553 int pcm_spdif_device,
3554 int max_wavetable_size,
3555 struct snd_trident ** rtrident)
3557 struct snd_trident *trident;
3559 struct snd_trident_voice *voice;
3560 struct snd_trident_pcm_mixer *tmix;
3561 static struct snd_device_ops ops = {
3562 .dev_free = snd_trident_dev_free,
3567 /* enable PCI device */
3568 if ((err = pci_enable_device(pci)) < 0)
3570 /* check, if we can restrict PCI DMA transfers to 30 bits */
3571 if (pci_set_dma_mask(pci, DMA_30BIT_MASK) < 0 ||
3572 pci_set_consistent_dma_mask(pci, DMA_30BIT_MASK) < 0) {
3573 snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
3574 pci_disable_device(pci);
3578 trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3579 if (trident == NULL) {
3580 pci_disable_device(pci);
3583 trident->device = (pci->vendor << 16) | pci->device;
3584 trident->card = card;
3586 spin_lock_init(&trident->reg_lock);
3587 spin_lock_init(&trident->event_lock);
3588 spin_lock_init(&trident->voice_alloc);
3589 if (pcm_streams < 1)
3591 if (pcm_streams > 32)
3593 trident->ChanPCM = pcm_streams;
3594 if (max_wavetable_size < 0 )
3595 max_wavetable_size = 0;
3596 trident->synth.max_size = max_wavetable_size * 1024;
3599 trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3600 pci_set_master(pci);
3602 if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3604 pci_disable_device(pci);
3607 trident->port = pci_resource_start(pci, 0);
3609 if (request_irq(pci->irq, snd_trident_interrupt, IRQF_SHARED,
3610 "Trident Audio", trident)) {
3611 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
3612 snd_trident_free(trident);
3615 trident->irq = pci->irq;
3617 /* allocate 16k-aligned TLB for NX cards */
3618 trident->tlb.entries = NULL;
3619 trident->tlb.buffer.area = NULL;
3620 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3621 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3622 snd_trident_free(trident);
3627 trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3629 /* initialize chip */
3630 switch (trident->device) {
3631 case TRIDENT_DEVICE_ID_DX:
3632 err = snd_trident_4d_dx_init(trident);
3634 case TRIDENT_DEVICE_ID_NX:
3635 err = snd_trident_4d_nx_init(trident);
3637 case TRIDENT_DEVICE_ID_SI7018:
3638 err = snd_trident_sis_init(trident);
3645 snd_trident_free(trident);
3649 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3650 snd_trident_free(trident);
3654 if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3657 /* initialise synth voices */
3658 for (i = 0; i < 64; i++) {
3659 voice = &trident->synth.voices[i];
3661 voice->trident = trident;
3663 /* initialize pcm mixer entries */
3664 for (i = 0; i < 32; i++) {
3665 tmix = &trident->pcm_mixer[i];
3666 tmix->vol = T4D_DEFAULT_PCM_VOL;
3667 tmix->pan = T4D_DEFAULT_PCM_PAN;
3668 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3669 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3672 snd_trident_enable_eso(trident);
3674 snd_trident_proc_init(trident);
3675 snd_card_set_dev(card, &pci->dev);
3676 *rtrident = trident;
3680 /*---------------------------------------------------------------------------
3683 Description: This routine will free the device specific class for
3686 Paramters: trident - device specific private data for 4DWave card
3690 ---------------------------------------------------------------------------*/
3692 static int snd_trident_free(struct snd_trident *trident)
3694 snd_trident_free_gameport(trident);
3695 snd_trident_disable_eso(trident);
3696 // Disable S/PDIF out
3697 if (trident->device == TRIDENT_DEVICE_ID_NX)
3698 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3699 else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3700 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3702 if (trident->tlb.buffer.area) {
3703 outl(0, TRID_REG(trident, NX_TLBC));
3704 if (trident->tlb.memhdr)
3705 snd_util_memhdr_free(trident->tlb.memhdr);
3706 if (trident->tlb.silent_page.area)
3707 snd_dma_free_pages(&trident->tlb.silent_page);
3708 vfree(trident->tlb.shadow_entries);
3709 snd_dma_free_pages(&trident->tlb.buffer);
3711 if (trident->irq >= 0)
3712 free_irq(trident->irq, trident);
3713 pci_release_regions(trident->pci);
3714 pci_disable_device(trident->pci);
3719 /*---------------------------------------------------------------------------
3720 snd_trident_interrupt
3722 Description: ISR for Trident 4DWave device
3724 Paramters: trident - device specific private data for 4DWave card
3726 Problems: It seems that Trident chips generates interrupts more than
3727 one time in special cases. The spurious interrupts are
3728 detected via sample timer (T4D_STIMER) and computing
3729 corresponding delta value. The limits are detected with
3730 the method try & fail so it is possible that it won't
3731 work on all computers. [jaroslav]
3735 ---------------------------------------------------------------------------*/
3737 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id)
3739 struct snd_trident *trident = dev_id;
3740 unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3742 struct snd_trident_voice *voice;
3744 audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3745 if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3747 if (audio_int & ADDRESS_IRQ) {
3748 // get interrupt status for all channels
3749 spin_lock(&trident->reg_lock);
3750 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3751 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3754 outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
3756 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3759 for (channel = 63; channel >= 32; channel--) {
3760 mask = 1 << (channel&0x1f);
3761 if ((chn_int & mask) == 0)
3763 voice = &trident->synth.voices[channel];
3764 if (!voice->pcm || voice->substream == NULL) {
3765 outl(mask, TRID_REG(trident, T4D_STOP_B));
3768 delta = (int)stimer - (int)voice->stimer;
3771 if ((unsigned int)delta < voice->spurious_threshold) {
3772 /* do some statistics here */
3773 trident->spurious_irq_count++;
3774 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3775 trident->spurious_irq_max_delta = delta;
3778 voice->stimer = stimer;
3780 if (!voice->isync3) {
3781 tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3782 if (trident->bDMAStart & 0x40)
3785 tmp = voice->isync_max - tmp;
3787 tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3789 if (tmp < voice->isync_mark) {
3791 tmp = voice->isync_ESO - 7;
3793 tmp = voice->isync_ESO + 2;
3794 /* update ESO for IRQ voice to preserve sync */
3795 snd_trident_stop_voice(trident, voice->number);
3796 snd_trident_write_eso_reg(trident, voice, tmp);
3797 snd_trident_start_voice(trident, voice->number);
3799 } else if (voice->isync2) {
3801 /* write original ESO and update CSO for IRQ voice to preserve sync */
3802 snd_trident_stop_voice(trident, voice->number);
3803 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3804 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3805 snd_trident_start_voice(trident, voice->number);
3809 /* update CSO for extra voice to preserve sync */
3810 snd_trident_stop_voice(trident, voice->extra->number);
3811 snd_trident_write_cso_reg(trident, voice->extra, 0);
3812 snd_trident_start_voice(trident, voice->extra->number);
3815 spin_unlock(&trident->reg_lock);
3816 snd_pcm_period_elapsed(voice->substream);
3817 spin_lock(&trident->reg_lock);
3819 outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
3821 spin_unlock(&trident->reg_lock);
3823 if (audio_int & MPU401_IRQ) {
3824 if (trident->rmidi) {
3825 snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data);
3827 inb(TRID_REG(trident, T4D_MPUR0));
3830 // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3834 /*---------------------------------------------------------------------------
3835 snd_trident_attach_synthesizer
3837 Description: Attach synthesizer hooks
3839 Paramters: trident - device specific private data for 4DWave card
3843 ---------------------------------------------------------------------------*/
3844 int snd_trident_attach_synthesizer(struct snd_trident *trident)
3846 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3847 if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
3848 sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
3849 strcpy(trident->seq_dev->name, "4DWave");
3850 *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
3856 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3858 struct snd_trident_voice *pvoice;
3859 unsigned long flags;
3862 spin_lock_irqsave(&trident->voice_alloc, flags);
3863 if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3864 idx = snd_trident_allocate_pcm_channel(trident);
3866 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3869 pvoice = &trident->synth.voices[idx];
3872 pvoice->capture = 0;
3874 pvoice->memblk = NULL;
3875 pvoice->substream = NULL;
3876 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3879 if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3880 idx = snd_trident_allocate_synth_channel(trident);
3882 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3885 pvoice = &trident->synth.voices[idx];
3888 pvoice->client = client;
3889 pvoice->port = port;
3890 pvoice->memblk = NULL;
3891 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3894 if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3896 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3900 EXPORT_SYMBOL(snd_trident_alloc_voice);
3902 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3904 unsigned long flags;
3905 void (*private_free)(struct snd_trident_voice *);
3908 if (voice == NULL || !voice->use)
3910 snd_trident_clear_voices(trident, voice->number, voice->number);
3911 spin_lock_irqsave(&trident->voice_alloc, flags);
3912 private_free = voice->private_free;
3913 private_data = voice->private_data;
3914 voice->private_free = NULL;
3915 voice->private_data = NULL;
3917 snd_trident_free_pcm_channel(trident, voice->number);
3919 snd_trident_free_synth_channel(trident, voice->number);
3920 voice->use = voice->pcm = voice->synth = voice->midi = 0;
3921 voice->capture = voice->spdif = 0;
3922 voice->sample_ops = NULL;
3923 voice->substream = NULL;
3924 voice->extra = NULL;
3925 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3927 private_free(voice);
3930 EXPORT_SYMBOL(snd_trident_free_voice);
3932 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3934 unsigned int i, val, mask[2] = { 0, 0 };
3936 snd_assert(v_min <= 63, return);
3937 snd_assert(v_max <= 63, return);
3938 for (i = v_min; i <= v_max; i++)
3939 mask[i >> 5] |= 1 << (i & 0x1f);
3941 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3942 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3943 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3946 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3947 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3948 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3953 int snd_trident_suspend(struct pci_dev *pci, pm_message_t state)
3955 struct snd_card *card = pci_get_drvdata(pci);
3956 struct snd_trident *trident = card->private_data;
3958 trident->in_suspend = 1;
3959 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3960 snd_pcm_suspend_all(trident->pcm);
3961 snd_pcm_suspend_all(trident->foldback);
3962 snd_pcm_suspend_all(trident->spdif);
3964 snd_ac97_suspend(trident->ac97);
3965 snd_ac97_suspend(trident->ac97_sec);
3967 pci_disable_device(pci);
3968 pci_save_state(pci);
3969 pci_set_power_state(pci, pci_choose_state(pci, state));
3973 int snd_trident_resume(struct pci_dev *pci)
3975 struct snd_card *card = pci_get_drvdata(pci);
3976 struct snd_trident *trident = card->private_data;
3978 pci_set_power_state(pci, PCI_D0);
3979 pci_restore_state(pci);
3980 if (pci_enable_device(pci) < 0) {
3981 printk(KERN_ERR "trident: pci_enable_device failed, "
3982 "disabling device\n");
3983 snd_card_disconnect(card);
3986 pci_set_master(pci);
3988 switch (trident->device) {
3989 case TRIDENT_DEVICE_ID_DX:
3990 snd_trident_4d_dx_init(trident);
3992 case TRIDENT_DEVICE_ID_NX:
3993 snd_trident_4d_nx_init(trident);
3995 case TRIDENT_DEVICE_ID_SI7018:
3996 snd_trident_sis_init(trident);
4000 snd_ac97_resume(trident->ac97);
4001 snd_ac97_resume(trident->ac97_sec);
4003 /* restore some registers */
4004 outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
4006 snd_trident_enable_eso(trident);
4008 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
4009 trident->in_suspend = 0;
4012 #endif /* CONFIG_PM */