1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Maintained by Jaroslav Kysela <perex@perex.cz>
4 * Originated by audio@tridentmicro.com
5 * Fri Feb 19 15:55:28 MST 1999
6 * Routines for control of Trident 4DWave (DX and NX) chip
13 * SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/vmalloc.h>
22 #include <linux/gameport.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/export.h>
27 #include <sound/core.h>
28 #include <sound/info.h>
29 #include <sound/control.h>
30 #include <sound/tlv.h>
32 #include <sound/asoundef.h>
34 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
35 struct snd_trident_voice * voice,
36 struct snd_pcm_substream *substream);
37 static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
38 struct snd_trident_voice * voice,
39 struct snd_pcm_substream *substream);
40 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id);
41 static int snd_trident_sis_reset(struct snd_trident *trident);
43 static void snd_trident_clear_voices(struct snd_trident * trident,
44 unsigned short v_min, unsigned short v_max);
45 static int snd_trident_free(struct snd_trident *trident);
53 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
55 unsigned int val, tmp;
57 dev_dbg(trident->card->dev, "Trident voice %i:\n", voice);
58 outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
59 val = inl(TRID_REG(trident, CH_LBA));
60 dev_dbg(trident->card->dev, "LBA: 0x%x\n", val);
61 val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
62 dev_dbg(trident->card->dev, "GVSel: %i\n", val >> 31);
63 dev_dbg(trident->card->dev, "Pan: 0x%x\n", (val >> 24) & 0x7f);
64 dev_dbg(trident->card->dev, "Vol: 0x%x\n", (val >> 16) & 0xff);
65 dev_dbg(trident->card->dev, "CTRL: 0x%x\n", (val >> 12) & 0x0f);
66 dev_dbg(trident->card->dev, "EC: 0x%x\n", val & 0x0fff);
67 if (trident->device != TRIDENT_DEVICE_ID_NX) {
68 val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
69 dev_dbg(trident->card->dev, "CSO: 0x%x\n", val >> 16);
70 dev_dbg(trident->card->dev, "Alpha: 0x%x\n", (val >> 4) & 0x0fff);
71 dev_dbg(trident->card->dev, "FMS: 0x%x\n", val & 0x0f);
72 val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
73 dev_dbg(trident->card->dev, "ESO: 0x%x\n", val >> 16);
74 dev_dbg(trident->card->dev, "Delta: 0x%x\n", val & 0xffff);
75 val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
76 } else { // TRIDENT_DEVICE_ID_NX
77 val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
78 tmp = (val >> 24) & 0xff;
79 dev_dbg(trident->card->dev, "CSO: 0x%x\n", val & 0x00ffffff);
80 val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
81 tmp |= (val >> 16) & 0xff00;
82 dev_dbg(trident->card->dev, "Delta: 0x%x\n", tmp);
83 dev_dbg(trident->card->dev, "ESO: 0x%x\n", val & 0x00ffffff);
84 val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
85 dev_dbg(trident->card->dev, "Alpha: 0x%x\n", val >> 20);
86 dev_dbg(trident->card->dev, "FMS: 0x%x\n", (val >> 16) & 0x0f);
88 dev_dbg(trident->card->dev, "FMC: 0x%x\n", (val >> 14) & 3);
89 dev_dbg(trident->card->dev, "RVol: 0x%x\n", (val >> 7) & 0x7f);
90 dev_dbg(trident->card->dev, "CVol: 0x%x\n", val & 0x7f);
94 /*---------------------------------------------------------------------------
95 unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
97 Description: This routine will do all of the reading from the external
100 Parameters: ac97 - ac97 codec structure
101 reg - CODEC register index, from AC97 Hal.
103 returns: 16 bit value read from the AC97.
105 ---------------------------------------------------------------------------*/
106 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
108 unsigned int data = 0, treg;
109 unsigned short count = 0xffff;
111 struct snd_trident *trident = ac97->private_data;
113 spin_lock_irqsave(&trident->reg_lock, flags);
114 if (trident->device == TRIDENT_DEVICE_ID_DX) {
115 data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
116 outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
118 data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
119 if ((data & DX_AC97_BUSY_READ) == 0)
122 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
123 data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
124 treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
125 outl(data, TRID_REG(trident, treg));
127 data = inl(TRID_REG(trident, treg));
128 if ((data & 0x00000C00) == 0)
131 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
132 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
134 data |= SI_AC97_SECONDARY;
135 outl(data, TRID_REG(trident, SI_AC97_READ));
137 data = inl(TRID_REG(trident, SI_AC97_READ));
138 if ((data & (SI_AC97_BUSY_READ)) == 0)
143 if (count == 0 && !trident->ac97_detect) {
144 dev_err(trident->card->dev,
145 "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
150 spin_unlock_irqrestore(&trident->reg_lock, flags);
151 return ((unsigned short) (data >> 16));
154 /*---------------------------------------------------------------------------
155 void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
156 unsigned short wdata)
158 Description: This routine will do all of the writing to the external
161 Parameters: ac97 - ac97 codec structure
162 reg - CODEC register index, from AC97 Hal.
163 data - Lower 16 bits are the data to write to CODEC.
165 returns: TRUE if everything went ok, else FALSE.
167 ---------------------------------------------------------------------------*/
168 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
169 unsigned short wdata)
171 unsigned int address, data;
172 unsigned short count = 0xffff;
174 struct snd_trident *trident = ac97->private_data;
176 data = ((unsigned long) wdata) << 16;
178 spin_lock_irqsave(&trident->reg_lock, flags);
179 if (trident->device == TRIDENT_DEVICE_ID_DX) {
180 address = DX_ACR0_AC97_W;
182 /* read AC-97 write register status */
184 if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
188 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
189 } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
190 address = NX_ACR1_AC97_W;
192 /* read AC-97 write register status */
194 if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
198 data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
199 } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
200 address = SI_AC97_WRITE;
202 /* read AC-97 write register status */
204 if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
208 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
210 data |= SI_AC97_SECONDARY;
212 address = 0; /* keep GCC happy */
213 count = 0; /* return */
217 spin_unlock_irqrestore(&trident->reg_lock, flags);
220 outl(data, TRID_REG(trident, address));
221 spin_unlock_irqrestore(&trident->reg_lock, flags);
224 /*---------------------------------------------------------------------------
225 void snd_trident_enable_eso(struct snd_trident *trident)
227 Description: This routine will enable end of loop interrupts.
228 End of loop interrupts will occur when a running
230 Also enables middle of loop interrupts.
232 Parameters: trident - pointer to target device class for 4DWave.
234 ---------------------------------------------------------------------------*/
236 static void snd_trident_enable_eso(struct snd_trident * trident)
240 val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
243 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
245 outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
248 /*---------------------------------------------------------------------------
249 void snd_trident_disable_eso(struct snd_trident *trident)
251 Description: This routine will disable end of loop interrupts.
252 End of loop interrupts will occur when a running
254 Also disables middle of loop interrupts.
257 trident - pointer to target device class for 4DWave.
259 returns: TRUE if everything went ok, else FALSE.
261 ---------------------------------------------------------------------------*/
263 static void snd_trident_disable_eso(struct snd_trident * trident)
267 tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
270 outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
273 /*---------------------------------------------------------------------------
274 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
276 Description: Start a voice, any channel 0 thru 63.
277 This routine automatically handles the fact that there are
278 more than 32 channels available.
280 Parameters : voice - Voice number 0 thru n.
281 trident - pointer to target device class for 4DWave.
285 ---------------------------------------------------------------------------*/
287 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
289 unsigned int mask = 1 << (voice & 0x1f);
290 unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
292 outl(mask, TRID_REG(trident, reg));
295 EXPORT_SYMBOL(snd_trident_start_voice);
297 /*---------------------------------------------------------------------------
298 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
300 Description: Stop a voice, any channel 0 thru 63.
301 This routine automatically handles the fact that there are
302 more than 32 channels available.
304 Parameters : voice - Voice number 0 thru n.
305 trident - pointer to target device class for 4DWave.
309 ---------------------------------------------------------------------------*/
311 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
313 unsigned int mask = 1 << (voice & 0x1f);
314 unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
316 outl(mask, TRID_REG(trident, reg));
319 EXPORT_SYMBOL(snd_trident_stop_voice);
321 /*---------------------------------------------------------------------------
322 int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
324 Description: Allocate hardware channel in Bank B (32-63).
326 Parameters : trident - pointer to target device class for 4DWave.
328 Return Value: hardware channel - 32-63 or -1 when no channel is available
330 ---------------------------------------------------------------------------*/
332 static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
336 if (trident->ChanPCMcnt >= trident->ChanPCM)
338 for (idx = 31; idx >= 0; idx--) {
339 if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
340 trident->ChanMap[T4D_BANK_B] |= 1 << idx;
341 trident->ChanPCMcnt++;
348 /*---------------------------------------------------------------------------
349 void snd_trident_free_pcm_channel(int channel)
351 Description: Free hardware channel in Bank B (32-63)
353 Parameters : trident - pointer to target device class for 4DWave.
354 channel - hardware channel number 0-63
358 ---------------------------------------------------------------------------*/
360 static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
362 if (channel < 32 || channel > 63)
365 if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
366 trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
367 trident->ChanPCMcnt--;
371 /*---------------------------------------------------------------------------
372 unsigned int snd_trident_allocate_synth_channel(void)
374 Description: Allocate hardware channel in Bank A (0-31).
376 Parameters : trident - pointer to target device class for 4DWave.
378 Return Value: hardware channel - 0-31 or -1 when no channel is available
380 ---------------------------------------------------------------------------*/
382 static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
386 for (idx = 31; idx >= 0; idx--) {
387 if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
388 trident->ChanMap[T4D_BANK_A] |= 1 << idx;
389 trident->synth.ChanSynthCount++;
396 /*---------------------------------------------------------------------------
397 void snd_trident_free_synth_channel( int channel )
399 Description: Free hardware channel in Bank B (0-31).
401 Parameters : trident - pointer to target device class for 4DWave.
402 channel - hardware channel number 0-63
406 ---------------------------------------------------------------------------*/
408 static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
410 if (channel < 0 || channel > 31)
413 if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
414 trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
415 trident->synth.ChanSynthCount--;
419 /*---------------------------------------------------------------------------
420 snd_trident_write_voice_regs
422 Description: This routine will complete and write the 5 hardware channel
423 registers to hardware.
425 Parameters: trident - pointer to target device class for 4DWave.
426 voice - synthesizer voice structure
429 ---------------------------------------------------------------------------*/
431 void snd_trident_write_voice_regs(struct snd_trident * trident,
432 struct snd_trident_voice * voice)
434 unsigned int FmcRvolCvol;
435 unsigned int regs[5];
437 regs[1] = voice->LBA;
438 regs[4] = (voice->GVSel << 31) |
439 ((voice->Pan & 0x0000007f) << 24) |
440 ((voice->CTRL & 0x0000000f) << 12);
441 FmcRvolCvol = ((voice->FMC & 3) << 14) |
442 ((voice->RVol & 0x7f) << 7) |
443 (voice->CVol & 0x7f);
445 switch (trident->device) {
446 case TRIDENT_DEVICE_ID_SI7018:
447 regs[4] |= voice->number > 31 ?
448 (voice->Vol & 0x000003ff) :
449 ((voice->Vol & 0x00003fc) << (16-2)) |
450 (voice->EC & 0x00000fff);
451 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
452 (voice->FMS & 0x0000000f);
453 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
454 regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
456 case TRIDENT_DEVICE_ID_DX:
457 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
458 (voice->EC & 0x00000fff);
459 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
460 (voice->FMS & 0x0000000f);
461 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
462 regs[3] = FmcRvolCvol;
464 case TRIDENT_DEVICE_ID_NX:
465 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
466 (voice->EC & 0x00000fff);
467 regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
468 regs[2] = ((voice->Delta << 16) & 0xff000000) |
469 (voice->ESO & 0x00ffffff);
470 regs[3] = (voice->Alpha << 20) |
471 ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
478 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
479 outl(regs[0], TRID_REG(trident, CH_START + 0));
480 outl(regs[1], TRID_REG(trident, CH_START + 4));
481 outl(regs[2], TRID_REG(trident, CH_START + 8));
482 outl(regs[3], TRID_REG(trident, CH_START + 12));
483 outl(regs[4], TRID_REG(trident, CH_START + 16));
486 dev_dbg(trident->card->dev, "written %i channel:\n", voice->number);
487 dev_dbg(trident->card->dev, " regs[0] = 0x%x/0x%x\n",
488 regs[0], inl(TRID_REG(trident, CH_START + 0)));
489 dev_dbg(trident->card->dev, " regs[1] = 0x%x/0x%x\n",
490 regs[1], inl(TRID_REG(trident, CH_START + 4)));
491 dev_dbg(trident->card->dev, " regs[2] = 0x%x/0x%x\n",
492 regs[2], inl(TRID_REG(trident, CH_START + 8)));
493 dev_dbg(trident->card->dev, " regs[3] = 0x%x/0x%x\n",
494 regs[3], inl(TRID_REG(trident, CH_START + 12)));
495 dev_dbg(trident->card->dev, " regs[4] = 0x%x/0x%x\n",
496 regs[4], inl(TRID_REG(trident, CH_START + 16)));
500 EXPORT_SYMBOL(snd_trident_write_voice_regs);
502 /*---------------------------------------------------------------------------
503 snd_trident_write_cso_reg
505 Description: This routine will write the new CSO offset
506 register to hardware.
508 Parameters: trident - pointer to target device class for 4DWave.
509 voice - synthesizer voice structure
512 ---------------------------------------------------------------------------*/
514 static void snd_trident_write_cso_reg(struct snd_trident * trident,
515 struct snd_trident_voice * voice,
519 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
520 if (trident->device != TRIDENT_DEVICE_ID_NX) {
521 outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
523 outl((voice->Delta << 24) |
524 (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
528 /*---------------------------------------------------------------------------
529 snd_trident_write_eso_reg
531 Description: This routine will write the new ESO offset
532 register to hardware.
534 Parameters: trident - pointer to target device class for 4DWave.
535 voice - synthesizer voice structure
538 ---------------------------------------------------------------------------*/
540 static void snd_trident_write_eso_reg(struct snd_trident * trident,
541 struct snd_trident_voice * voice,
545 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
546 if (trident->device != TRIDENT_DEVICE_ID_NX) {
547 outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
549 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
550 TRID_REG(trident, CH_NX_DELTA_ESO));
554 /*---------------------------------------------------------------------------
555 snd_trident_write_vol_reg
557 Description: This routine will write the new voice volume
558 register to hardware.
560 Parameters: trident - pointer to target device class for 4DWave.
561 voice - synthesizer voice structure
562 Vol - new voice volume
564 ---------------------------------------------------------------------------*/
566 static void snd_trident_write_vol_reg(struct snd_trident * trident,
567 struct snd_trident_voice * voice,
571 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
572 switch (trident->device) {
573 case TRIDENT_DEVICE_ID_DX:
574 case TRIDENT_DEVICE_ID_NX:
575 outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
577 case TRIDENT_DEVICE_ID_SI7018:
578 /* dev_dbg(trident->card->dev, "voice->Vol = 0x%x\n", voice->Vol); */
579 outw((voice->CTRL << 12) | voice->Vol,
580 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
585 /*---------------------------------------------------------------------------
586 snd_trident_write_pan_reg
588 Description: This routine will write the new voice pan
589 register to hardware.
591 Parameters: trident - pointer to target device class for 4DWave.
592 voice - synthesizer voice structure
595 ---------------------------------------------------------------------------*/
597 static void snd_trident_write_pan_reg(struct snd_trident * trident,
598 struct snd_trident_voice * voice,
602 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
603 outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
604 TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
607 /*---------------------------------------------------------------------------
608 snd_trident_write_rvol_reg
610 Description: This routine will write the new reverb volume
611 register to hardware.
613 Parameters: trident - pointer to target device class for 4DWave.
614 voice - synthesizer voice structure
615 RVol - new reverb volume
617 ---------------------------------------------------------------------------*/
619 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
620 struct snd_trident_voice * voice,
624 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
625 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
626 (voice->CVol & 0x007f),
627 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
628 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
631 /*---------------------------------------------------------------------------
632 snd_trident_write_cvol_reg
634 Description: This routine will write the new chorus volume
635 register to hardware.
637 Parameters: trident - pointer to target device class for 4DWave.
638 voice - synthesizer voice structure
639 CVol - new chorus volume
641 ---------------------------------------------------------------------------*/
643 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
644 struct snd_trident_voice * voice,
648 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
649 outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
650 (voice->CVol & 0x007f),
651 TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
652 CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
655 /*---------------------------------------------------------------------------
656 snd_trident_convert_rate
658 Description: This routine converts rate in HZ to hardware delta value.
660 Parameters: trident - pointer to target device class for 4DWave.
661 rate - Real or Virtual channel number.
663 Returns: Delta value.
665 ---------------------------------------------------------------------------*/
666 static unsigned int snd_trident_convert_rate(unsigned int rate)
670 // We special case 44100 and 8000 since rounding with the equation
671 // does not give us an accurate enough value. For 11025 and 22050
672 // the equation gives us the best answer. All other frequencies will
673 // also use the equation. JDW
676 else if (rate == 8000)
678 else if (rate == 48000)
681 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
685 /*---------------------------------------------------------------------------
686 snd_trident_convert_adc_rate
688 Description: This routine converts rate in HZ to hardware delta value.
690 Parameters: trident - pointer to target device class for 4DWave.
691 rate - Real or Virtual channel number.
693 Returns: Delta value.
695 ---------------------------------------------------------------------------*/
696 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
700 // We special case 44100 and 8000 since rounding with the equation
701 // does not give us an accurate enough value. For 11025 and 22050
702 // the equation gives us the best answer. All other frequencies will
703 // also use the equation. JDW
706 else if (rate == 8000)
708 else if (rate == 48000)
711 delta = ((48000 << 12) / rate) & 0x0000ffff;
715 /*---------------------------------------------------------------------------
716 snd_trident_spurious_threshold
718 Description: This routine converts rate in HZ to spurious threshold.
720 Parameters: trident - pointer to target device class for 4DWave.
721 rate - Real or Virtual channel number.
723 Returns: Delta value.
725 ---------------------------------------------------------------------------*/
726 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
727 unsigned int period_size)
729 unsigned int res = (rate * period_size) / 48000;
737 /*---------------------------------------------------------------------------
738 snd_trident_control_mode
740 Description: This routine returns a control mode for a PCM channel.
742 Parameters: trident - pointer to target device class for 4DWave.
743 substream - PCM substream
745 Returns: Control value.
747 ---------------------------------------------------------------------------*/
748 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
751 struct snd_pcm_runtime *runtime = substream->runtime;
754 CTRL default: 8-bit (unsigned) mono, loop mode enabled
757 if (snd_pcm_format_width(runtime->format) == 16)
758 CTRL |= 0x00000008; // 16-bit data
759 if (snd_pcm_format_signed(runtime->format))
760 CTRL |= 0x00000002; // signed data
761 if (runtime->channels > 1)
762 CTRL |= 0x00000004; // stereo data
770 /*---------------------------------------------------------------------------
773 Description: Device I/O control handler for playback/capture parameters.
775 Parameters: substream - PCM substream class
776 cmd - what ioctl message to process
777 arg - additional message infoarg
779 Returns: Error status
781 ---------------------------------------------------------------------------*/
783 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
787 /* FIXME: it seems that with small periods the behaviour of
788 trident hardware is unpredictable and interrupt generator
790 return snd_pcm_lib_ioctl(substream, cmd, arg);
793 /*---------------------------------------------------------------------------
794 snd_trident_allocate_pcm_mem
796 Description: Allocate PCM ring buffer for given substream
798 Parameters: substream - PCM substream class
799 hw_params - hardware parameters
801 Returns: Error status
803 ---------------------------------------------------------------------------*/
805 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
806 struct snd_pcm_hw_params *hw_params)
808 struct snd_trident *trident = snd_pcm_substream_chip(substream);
809 struct snd_pcm_runtime *runtime = substream->runtime;
810 struct snd_trident_voice *voice = runtime->private_data;
813 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
815 if (trident->tlb.entries) {
816 if (err > 0) { /* change */
818 snd_trident_free_pages(trident, voice->memblk);
819 voice->memblk = snd_trident_alloc_pages(trident, substream);
820 if (voice->memblk == NULL)
827 /*---------------------------------------------------------------------------
828 snd_trident_allocate_evoice
830 Description: Allocate extra voice as interrupt generator
832 Parameters: substream - PCM substream class
833 hw_params - hardware parameters
835 Returns: Error status
837 ---------------------------------------------------------------------------*/
839 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
840 struct snd_pcm_hw_params *hw_params)
842 struct snd_trident *trident = snd_pcm_substream_chip(substream);
843 struct snd_pcm_runtime *runtime = substream->runtime;
844 struct snd_trident_voice *voice = runtime->private_data;
845 struct snd_trident_voice *evoice = voice->extra;
847 /* voice management */
849 if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
850 if (evoice == NULL) {
851 evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
854 voice->extra = evoice;
855 evoice->substream = substream;
858 if (evoice != NULL) {
859 snd_trident_free_voice(trident, evoice);
860 voice->extra = evoice = NULL;
867 /*---------------------------------------------------------------------------
868 snd_trident_hw_params
870 Description: Set the hardware parameters for the playback device.
872 Parameters: substream - PCM substream class
873 hw_params - hardware parameters
875 Returns: Error status
877 ---------------------------------------------------------------------------*/
879 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
880 struct snd_pcm_hw_params *hw_params)
884 err = snd_trident_allocate_pcm_mem(substream, hw_params);
886 err = snd_trident_allocate_evoice(substream, hw_params);
890 /*---------------------------------------------------------------------------
891 snd_trident_playback_hw_free
893 Description: Release the hardware resources for the playback device.
895 Parameters: substream - PCM substream class
897 Returns: Error status
899 ---------------------------------------------------------------------------*/
901 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
903 struct snd_trident *trident = snd_pcm_substream_chip(substream);
904 struct snd_pcm_runtime *runtime = substream->runtime;
905 struct snd_trident_voice *voice = runtime->private_data;
906 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
908 if (trident->tlb.entries) {
909 if (voice && voice->memblk) {
910 snd_trident_free_pages(trident, voice->memblk);
911 voice->memblk = NULL;
914 snd_pcm_lib_free_pages(substream);
915 if (evoice != NULL) {
916 snd_trident_free_voice(trident, evoice);
922 /*---------------------------------------------------------------------------
923 snd_trident_playback_prepare
925 Description: Prepare playback device for playback.
927 Parameters: substream - PCM substream class
929 Returns: Error status
931 ---------------------------------------------------------------------------*/
933 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
935 struct snd_trident *trident = snd_pcm_substream_chip(substream);
936 struct snd_pcm_runtime *runtime = substream->runtime;
937 struct snd_trident_voice *voice = runtime->private_data;
938 struct snd_trident_voice *evoice = voice->extra;
939 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
941 spin_lock_irq(&trident->reg_lock);
943 /* set delta (rate) value */
944 voice->Delta = snd_trident_convert_rate(runtime->rate);
945 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
947 /* set Loop Begin Address */
949 voice->LBA = voice->memblk->offset;
951 voice->LBA = runtime->dma_addr;
954 voice->ESO = runtime->buffer_size - 1; /* in samples */
955 voice->CTRL = snd_trident_control_mode(substream);
961 voice->Vol = mix->vol;
962 voice->RVol = mix->rvol;
963 voice->CVol = mix->cvol;
964 voice->Pan = mix->pan;
965 voice->Attribute = 0;
967 voice->Attribute = (1<<(30-16))|(2<<(26-16))|
968 (0<<(24-16))|(0x1f<<(19-16));
970 voice->Attribute = 0;
973 snd_trident_write_voice_regs(trident, voice);
975 if (evoice != NULL) {
976 evoice->Delta = voice->Delta;
977 evoice->spurious_threshold = voice->spurious_threshold;
978 evoice->LBA = voice->LBA;
980 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
981 evoice->CTRL = voice->CTRL;
983 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
987 evoice->Vol = 0x3ff; /* mute */
988 evoice->RVol = evoice->CVol = 0x7f; /* mute */
989 evoice->Pan = 0x7f; /* mute */
991 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
992 (0<<(24-16))|(0x1f<<(19-16));
994 evoice->Attribute = 0;
996 snd_trident_write_voice_regs(trident, evoice);
998 evoice->isync_mark = runtime->period_size;
999 evoice->ESO = (runtime->period_size * 2) - 1;
1002 spin_unlock_irq(&trident->reg_lock);
1007 /*---------------------------------------------------------------------------
1008 snd_trident_capture_hw_params
1010 Description: Set the hardware parameters for the capture device.
1012 Parameters: substream - PCM substream class
1013 hw_params - hardware parameters
1015 Returns: Error status
1017 ---------------------------------------------------------------------------*/
1019 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1020 struct snd_pcm_hw_params *hw_params)
1022 return snd_trident_allocate_pcm_mem(substream, hw_params);
1025 /*---------------------------------------------------------------------------
1026 snd_trident_capture_prepare
1028 Description: Prepare capture device for playback.
1030 Parameters: substream - PCM substream class
1032 Returns: Error status
1034 ---------------------------------------------------------------------------*/
1036 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1038 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1039 struct snd_pcm_runtime *runtime = substream->runtime;
1040 struct snd_trident_voice *voice = runtime->private_data;
1041 unsigned int val, ESO_bytes;
1043 spin_lock_irq(&trident->reg_lock);
1045 // Initialize the channel and set channel Mode
1046 outb(0, TRID_REG(trident, LEGACY_DMAR15));
1048 // Set DMA channel operation mode register
1049 outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1051 // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
1052 voice->LBA = runtime->dma_addr;
1053 outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
1055 voice->LBA = voice->memblk->offset;
1058 ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
1059 outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
1060 outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
1063 // Set channel sample rate, 4.12 format
1064 val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
1065 outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
1067 // Set channel interrupt blk length
1068 if (snd_pcm_format_width(runtime->format) == 16) {
1069 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1071 val = (unsigned short) (ESO_bytes - 1);
1074 outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1076 // Right now, set format and start to run captureing,
1077 // continuous run loop enable.
1078 trident->bDMAStart = 0x19; // 0001 1001b
1080 if (snd_pcm_format_width(runtime->format) == 16)
1081 trident->bDMAStart |= 0x80;
1082 if (snd_pcm_format_signed(runtime->format))
1083 trident->bDMAStart |= 0x20;
1084 if (runtime->channels > 1)
1085 trident->bDMAStart |= 0x40;
1087 // Prepare capture intr channel
1089 voice->Delta = snd_trident_convert_rate(runtime->rate);
1090 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1092 voice->isync_mark = runtime->period_size;
1093 voice->isync_max = runtime->buffer_size;
1095 // Set voice parameters
1097 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1098 voice->CTRL = snd_trident_control_mode(substream);
1103 voice->Pan = 0x7f; /* mute */
1104 voice->Vol = 0x3ff; /* mute */
1108 voice->Attribute = 0;
1110 snd_trident_write_voice_regs(trident, voice);
1112 spin_unlock_irq(&trident->reg_lock);
1116 /*---------------------------------------------------------------------------
1117 snd_trident_si7018_capture_hw_params
1119 Description: Set the hardware parameters for the capture device.
1121 Parameters: substream - PCM substream class
1122 hw_params - hardware parameters
1124 Returns: Error status
1126 ---------------------------------------------------------------------------*/
1128 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1129 struct snd_pcm_hw_params *hw_params)
1133 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1136 return snd_trident_allocate_evoice(substream, hw_params);
1139 /*---------------------------------------------------------------------------
1140 snd_trident_si7018_capture_hw_free
1142 Description: Release the hardware resources for the capture device.
1144 Parameters: substream - PCM substream class
1146 Returns: Error status
1148 ---------------------------------------------------------------------------*/
1150 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1152 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1153 struct snd_pcm_runtime *runtime = substream->runtime;
1154 struct snd_trident_voice *voice = runtime->private_data;
1155 struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1157 snd_pcm_lib_free_pages(substream);
1158 if (evoice != NULL) {
1159 snd_trident_free_voice(trident, evoice);
1160 voice->extra = NULL;
1165 /*---------------------------------------------------------------------------
1166 snd_trident_si7018_capture_prepare
1168 Description: Prepare capture device for playback.
1170 Parameters: substream - PCM substream class
1172 Returns: Error status
1174 ---------------------------------------------------------------------------*/
1176 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1178 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1179 struct snd_pcm_runtime *runtime = substream->runtime;
1180 struct snd_trident_voice *voice = runtime->private_data;
1181 struct snd_trident_voice *evoice = voice->extra;
1183 spin_lock_irq(&trident->reg_lock);
1185 voice->LBA = runtime->dma_addr;
1186 voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
1187 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1189 // Set voice parameters
1191 voice->ESO = runtime->buffer_size - 1; /* in samples */
1192 voice->CTRL = snd_trident_control_mode(substream);
1197 voice->Pan = T4D_DEFAULT_PCM_PAN;
1203 voice->Attribute = (2 << (30-16)) |
1208 snd_trident_write_voice_regs(trident, voice);
1210 if (evoice != NULL) {
1211 evoice->Delta = snd_trident_convert_rate(runtime->rate);
1212 evoice->spurious_threshold = voice->spurious_threshold;
1213 evoice->LBA = voice->LBA;
1215 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1216 evoice->CTRL = voice->CTRL;
1222 evoice->Vol = 0x3ff; /* mute */
1223 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1224 evoice->Pan = 0x7f; /* mute */
1225 evoice->Attribute = 0;
1226 snd_trident_write_voice_regs(trident, evoice);
1228 evoice->isync_mark = runtime->period_size;
1229 evoice->ESO = (runtime->period_size * 2) - 1;
1232 spin_unlock_irq(&trident->reg_lock);
1236 /*---------------------------------------------------------------------------
1237 snd_trident_foldback_prepare
1239 Description: Prepare foldback capture device for playback.
1241 Parameters: substream - PCM substream class
1243 Returns: Error status
1245 ---------------------------------------------------------------------------*/
1247 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1249 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1250 struct snd_pcm_runtime *runtime = substream->runtime;
1251 struct snd_trident_voice *voice = runtime->private_data;
1252 struct snd_trident_voice *evoice = voice->extra;
1254 spin_lock_irq(&trident->reg_lock);
1256 /* Set channel buffer Address */
1258 voice->LBA = voice->memblk->offset;
1260 voice->LBA = runtime->dma_addr;
1262 /* set target ESO for channel */
1263 voice->ESO = runtime->buffer_size - 1; /* in samples */
1265 /* set sample rate */
1266 voice->Delta = 0x1000;
1267 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1270 voice->CTRL = snd_trident_control_mode(substream);
1275 voice->Pan = 0x7f; /* mute */
1276 voice->Vol = 0x3ff; /* mute */
1280 voice->Attribute = 0;
1282 /* set up capture channel */
1283 outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1285 snd_trident_write_voice_regs(trident, voice);
1287 if (evoice != NULL) {
1288 evoice->Delta = voice->Delta;
1289 evoice->spurious_threshold = voice->spurious_threshold;
1290 evoice->LBA = voice->LBA;
1292 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1293 evoice->CTRL = voice->CTRL;
1295 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1299 evoice->Vol = 0x3ff; /* mute */
1300 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1301 evoice->Pan = 0x7f; /* mute */
1302 evoice->Attribute = 0;
1303 snd_trident_write_voice_regs(trident, evoice);
1305 evoice->isync_mark = runtime->period_size;
1306 evoice->ESO = (runtime->period_size * 2) - 1;
1309 spin_unlock_irq(&trident->reg_lock);
1313 /*---------------------------------------------------------------------------
1314 snd_trident_spdif_hw_params
1316 Description: Set the hardware parameters for the spdif device.
1318 Parameters: substream - PCM substream class
1319 hw_params - hardware parameters
1321 Returns: Error status
1323 ---------------------------------------------------------------------------*/
1325 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1326 struct snd_pcm_hw_params *hw_params)
1328 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1329 unsigned int old_bits = 0, change = 0;
1332 err = snd_trident_allocate_pcm_mem(substream, hw_params);
1336 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1337 err = snd_trident_allocate_evoice(substream, hw_params);
1342 /* prepare SPDIF channel */
1343 spin_lock_irq(&trident->reg_lock);
1344 old_bits = trident->spdif_pcm_bits;
1345 if (old_bits & IEC958_AES0_PROFESSIONAL)
1346 trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
1348 trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
1349 if (params_rate(hw_params) >= 48000) {
1350 trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
1351 trident->spdif_pcm_bits |=
1352 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1353 IEC958_AES0_PRO_FS_48000 :
1354 (IEC958_AES3_CON_FS_48000 << 24);
1356 else if (params_rate(hw_params) >= 44100) {
1357 trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
1358 trident->spdif_pcm_bits |=
1359 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1360 IEC958_AES0_PRO_FS_44100 :
1361 (IEC958_AES3_CON_FS_44100 << 24);
1364 trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
1365 trident->spdif_pcm_bits |=
1366 trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1367 IEC958_AES0_PRO_FS_32000 :
1368 (IEC958_AES3_CON_FS_32000 << 24);
1370 change = old_bits != trident->spdif_pcm_bits;
1371 spin_unlock_irq(&trident->reg_lock);
1374 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1379 /*---------------------------------------------------------------------------
1380 snd_trident_spdif_prepare
1382 Description: Prepare SPDIF device for playback.
1384 Parameters: substream - PCM substream class
1386 Returns: Error status
1388 ---------------------------------------------------------------------------*/
1390 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1392 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1393 struct snd_pcm_runtime *runtime = substream->runtime;
1394 struct snd_trident_voice *voice = runtime->private_data;
1395 struct snd_trident_voice *evoice = voice->extra;
1396 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1397 unsigned int RESO, LBAO;
1400 spin_lock_irq(&trident->reg_lock);
1402 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1404 /* set delta (rate) value */
1405 voice->Delta = snd_trident_convert_rate(runtime->rate);
1406 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1408 /* set Loop Back Address */
1409 LBAO = runtime->dma_addr;
1411 voice->LBA = voice->memblk->offset;
1417 voice->isync_mark = runtime->period_size;
1418 voice->isync_max = runtime->buffer_size;
1420 /* set target ESO for channel */
1421 RESO = runtime->buffer_size - 1;
1422 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1425 voice->CTRL = snd_trident_control_mode(substream);
1437 voice->Attribute = 0;
1439 /* prepare surrogate IRQ channel */
1440 snd_trident_write_voice_regs(trident, voice);
1442 outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
1443 outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
1444 outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
1445 outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
1446 outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
1448 /* set SPDIF setting */
1449 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1450 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1454 /* set delta (rate) value */
1455 voice->Delta = 0x800;
1456 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1458 /* set Loop Begin Address */
1460 voice->LBA = voice->memblk->offset;
1462 voice->LBA = runtime->dma_addr;
1465 voice->ESO = runtime->buffer_size - 1; /* in samples */
1466 voice->CTRL = snd_trident_control_mode(substream);
1472 voice->Vol = mix->vol;
1473 voice->RVol = mix->rvol;
1474 voice->CVol = mix->cvol;
1475 voice->Pan = mix->pan;
1476 voice->Attribute = (1<<(30-16))|(7<<(26-16))|
1477 (0<<(24-16))|(0<<(19-16));
1479 snd_trident_write_voice_regs(trident, voice);
1481 if (evoice != NULL) {
1482 evoice->Delta = voice->Delta;
1483 evoice->spurious_threshold = voice->spurious_threshold;
1484 evoice->LBA = voice->LBA;
1486 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1487 evoice->CTRL = voice->CTRL;
1489 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1493 evoice->Vol = 0x3ff; /* mute */
1494 evoice->RVol = evoice->CVol = 0x7f; /* mute */
1495 evoice->Pan = 0x7f; /* mute */
1496 evoice->Attribute = 0;
1497 snd_trident_write_voice_regs(trident, evoice);
1499 evoice->isync_mark = runtime->period_size;
1500 evoice->ESO = (runtime->period_size * 2) - 1;
1503 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1504 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1506 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1507 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1509 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1512 spin_unlock_irq(&trident->reg_lock);
1517 /*---------------------------------------------------------------------------
1520 Description: Start/stop devices
1522 Parameters: substream - PCM substream class
1523 cmd - trigger command (STOP, GO)
1525 Returns: Error status
1527 ---------------------------------------------------------------------------*/
1529 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1533 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1534 struct snd_pcm_substream *s;
1535 unsigned int what, whati, capture_flag, spdif_flag;
1536 struct snd_trident_voice *voice, *evoice;
1537 unsigned int val, go;
1540 case SNDRV_PCM_TRIGGER_START:
1541 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1542 case SNDRV_PCM_TRIGGER_RESUME:
1545 case SNDRV_PCM_TRIGGER_STOP:
1546 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1547 case SNDRV_PCM_TRIGGER_SUSPEND:
1553 what = whati = capture_flag = spdif_flag = 0;
1554 spin_lock(&trident->reg_lock);
1555 val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
1556 snd_pcm_group_for_each_entry(s, substream) {
1557 if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
1558 voice = s->runtime->private_data;
1559 evoice = voice->extra;
1560 what |= 1 << (voice->number & 0x1f);
1561 if (evoice == NULL) {
1562 whati |= 1 << (voice->number & 0x1f);
1564 what |= 1 << (evoice->number & 0x1f);
1565 whati |= 1 << (evoice->number & 0x1f);
1567 evoice->stimer = val;
1571 voice->stimer = val;
1575 snd_pcm_trigger_done(s, substream);
1583 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1584 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1585 val = trident->spdif_pcm_ctrl;
1588 outb(val, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1590 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1591 val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
1592 outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1596 outl(what, TRID_REG(trident, T4D_STOP_B));
1597 val = inl(TRID_REG(trident, T4D_AINTEN_B));
1603 outl(val, TRID_REG(trident, T4D_AINTEN_B));
1605 outl(what, TRID_REG(trident, T4D_START_B));
1607 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1608 outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1610 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1611 outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1613 spin_unlock(&trident->reg_lock);
1617 /*---------------------------------------------------------------------------
1618 snd_trident_playback_pointer
1620 Description: This routine return the playback position
1622 Parameters: substream - PCM substream class
1624 Returns: position of buffer
1626 ---------------------------------------------------------------------------*/
1628 static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1630 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1631 struct snd_pcm_runtime *runtime = substream->runtime;
1632 struct snd_trident_voice *voice = runtime->private_data;
1635 if (!voice->running)
1638 spin_lock(&trident->reg_lock);
1640 outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
1642 if (trident->device != TRIDENT_DEVICE_ID_NX) {
1643 cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
1644 } else { // ID_4DWAVE_NX
1645 cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
1648 spin_unlock(&trident->reg_lock);
1650 if (cso >= runtime->buffer_size)
1656 /*---------------------------------------------------------------------------
1657 snd_trident_capture_pointer
1659 Description: This routine return the capture position
1661 Parameters: pcm1 - PCM device class
1663 Returns: position of buffer
1665 ---------------------------------------------------------------------------*/
1667 static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1669 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1670 struct snd_pcm_runtime *runtime = substream->runtime;
1671 struct snd_trident_voice *voice = runtime->private_data;
1672 unsigned int result;
1674 if (!voice->running)
1677 result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
1678 if (runtime->channels > 1)
1681 result = runtime->buffer_size - result;
1686 /*---------------------------------------------------------------------------
1687 snd_trident_spdif_pointer
1689 Description: This routine return the SPDIF playback position
1691 Parameters: substream - PCM substream class
1693 Returns: position of buffer
1695 ---------------------------------------------------------------------------*/
1697 static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1699 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1700 struct snd_pcm_runtime *runtime = substream->runtime;
1701 struct snd_trident_voice *voice = runtime->private_data;
1702 unsigned int result;
1704 if (!voice->running)
1707 result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
1713 * Playback support device description
1716 static const struct snd_pcm_hardware snd_trident_playback =
1718 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1719 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1720 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1721 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1722 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1723 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1724 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1729 .buffer_bytes_max = (256*1024),
1730 .period_bytes_min = 64,
1731 .period_bytes_max = (256*1024),
1733 .periods_max = 1024,
1738 * Capture support device description
1741 static const struct snd_pcm_hardware snd_trident_capture =
1743 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1744 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1745 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1746 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1747 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1748 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1749 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1754 .buffer_bytes_max = (128*1024),
1755 .period_bytes_min = 64,
1756 .period_bytes_max = (128*1024),
1758 .periods_max = 1024,
1763 * Foldback capture support device description
1766 static const struct snd_pcm_hardware snd_trident_foldback =
1768 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1769 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1770 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1771 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1772 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1773 .rates = SNDRV_PCM_RATE_48000,
1778 .buffer_bytes_max = (128*1024),
1779 .period_bytes_min = 64,
1780 .period_bytes_max = (128*1024),
1782 .periods_max = 1024,
1787 * SPDIF playback support device description
1790 static const struct snd_pcm_hardware snd_trident_spdif =
1792 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1793 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1794 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1795 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1796 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1797 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1798 SNDRV_PCM_RATE_48000),
1803 .buffer_bytes_max = (128*1024),
1804 .period_bytes_min = 64,
1805 .period_bytes_max = (128*1024),
1807 .periods_max = 1024,
1811 static const struct snd_pcm_hardware snd_trident_spdif_7018 =
1813 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1814 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1815 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1816 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1817 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1818 .rates = SNDRV_PCM_RATE_48000,
1823 .buffer_bytes_max = (128*1024),
1824 .period_bytes_min = 64,
1825 .period_bytes_max = (128*1024),
1827 .periods_max = 1024,
1831 static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1833 struct snd_trident_voice *voice = runtime->private_data;
1834 struct snd_trident *trident;
1837 trident = voice->trident;
1838 snd_trident_free_voice(trident, voice);
1842 static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1844 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1845 struct snd_pcm_runtime *runtime = substream->runtime;
1846 struct snd_trident_voice *voice;
1848 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1851 snd_trident_pcm_mixer_build(trident, voice, substream);
1852 voice->substream = substream;
1853 runtime->private_data = voice;
1854 runtime->private_free = snd_trident_pcm_free_substream;
1855 runtime->hw = snd_trident_playback;
1856 snd_pcm_set_sync(substream);
1857 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1861 /*---------------------------------------------------------------------------
1862 snd_trident_playback_close
1864 Description: This routine will close the 4DWave playback device. For now
1865 we will simply free the dma transfer buffer.
1867 Parameters: substream - PCM substream class
1869 ---------------------------------------------------------------------------*/
1870 static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1872 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1873 struct snd_pcm_runtime *runtime = substream->runtime;
1874 struct snd_trident_voice *voice = runtime->private_data;
1876 snd_trident_pcm_mixer_free(trident, voice, substream);
1880 /*---------------------------------------------------------------------------
1881 snd_trident_spdif_open
1883 Description: This routine will open the 4DWave SPDIF device.
1885 Parameters: substream - PCM substream class
1887 Returns: status - success or failure flag
1889 ---------------------------------------------------------------------------*/
1891 static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1893 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1894 struct snd_trident_voice *voice;
1895 struct snd_pcm_runtime *runtime = substream->runtime;
1897 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1901 voice->substream = substream;
1902 spin_lock_irq(&trident->reg_lock);
1903 trident->spdif_pcm_bits = trident->spdif_bits;
1904 spin_unlock_irq(&trident->reg_lock);
1906 runtime->private_data = voice;
1907 runtime->private_free = snd_trident_pcm_free_substream;
1908 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1909 runtime->hw = snd_trident_spdif;
1911 runtime->hw = snd_trident_spdif_7018;
1914 trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1915 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1916 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1918 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1923 /*---------------------------------------------------------------------------
1924 snd_trident_spdif_close
1926 Description: This routine will close the 4DWave SPDIF device.
1928 Parameters: substream - PCM substream class
1930 ---------------------------------------------------------------------------*/
1932 static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1934 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1937 spin_lock_irq(&trident->reg_lock);
1938 // restore default SPDIF setting
1939 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1940 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1941 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
1943 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
1944 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1945 if (trident->spdif_ctrl) {
1950 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1952 spin_unlock_irq(&trident->reg_lock);
1953 trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1954 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1955 SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1959 /*---------------------------------------------------------------------------
1960 snd_trident_capture_open
1962 Description: This routine will open the 4DWave capture device.
1964 Parameters: substream - PCM substream class
1966 Returns: status - success or failure flag
1968 ---------------------------------------------------------------------------*/
1970 static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1972 struct snd_trident *trident = snd_pcm_substream_chip(substream);
1973 struct snd_trident_voice *voice;
1974 struct snd_pcm_runtime *runtime = substream->runtime;
1976 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1980 voice->substream = substream;
1981 runtime->private_data = voice;
1982 runtime->private_free = snd_trident_pcm_free_substream;
1983 runtime->hw = snd_trident_capture;
1984 snd_pcm_set_sync(substream);
1985 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1989 /*---------------------------------------------------------------------------
1990 snd_trident_capture_close
1992 Description: This routine will close the 4DWave capture device. For now
1993 we will simply free the dma transfer buffer.
1995 Parameters: substream - PCM substream class
1997 ---------------------------------------------------------------------------*/
1998 static int snd_trident_capture_close(struct snd_pcm_substream *substream)
2003 /*---------------------------------------------------------------------------
2004 snd_trident_foldback_open
2006 Description: This routine will open the 4DWave foldback capture device.
2008 Parameters: substream - PCM substream class
2010 Returns: status - success or failure flag
2012 ---------------------------------------------------------------------------*/
2014 static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
2016 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2017 struct snd_trident_voice *voice;
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2020 voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
2023 voice->foldback_chan = substream->number;
2024 voice->substream = substream;
2025 runtime->private_data = voice;
2026 runtime->private_free = snd_trident_pcm_free_substream;
2027 runtime->hw = snd_trident_foldback;
2028 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
2032 /*---------------------------------------------------------------------------
2033 snd_trident_foldback_close
2035 Description: This routine will close the 4DWave foldback capture device.
2036 For now we will simply free the dma transfer buffer.
2038 Parameters: substream - PCM substream class
2040 ---------------------------------------------------------------------------*/
2041 static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
2043 struct snd_trident *trident = snd_pcm_substream_chip(substream);
2044 struct snd_trident_voice *voice;
2045 struct snd_pcm_runtime *runtime = substream->runtime;
2046 voice = runtime->private_data;
2048 /* stop capture channel */
2049 spin_lock_irq(&trident->reg_lock);
2050 outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
2051 spin_unlock_irq(&trident->reg_lock);
2055 /*---------------------------------------------------------------------------
2057 ---------------------------------------------------------------------------*/
2059 static const struct snd_pcm_ops snd_trident_playback_ops = {
2060 .open = snd_trident_playback_open,
2061 .close = snd_trident_playback_close,
2062 .ioctl = snd_trident_ioctl,
2063 .hw_params = snd_trident_hw_params,
2064 .hw_free = snd_trident_hw_free,
2065 .prepare = snd_trident_playback_prepare,
2066 .trigger = snd_trident_trigger,
2067 .pointer = snd_trident_playback_pointer,
2070 static const struct snd_pcm_ops snd_trident_nx_playback_ops = {
2071 .open = snd_trident_playback_open,
2072 .close = snd_trident_playback_close,
2073 .ioctl = snd_trident_ioctl,
2074 .hw_params = snd_trident_hw_params,
2075 .hw_free = snd_trident_hw_free,
2076 .prepare = snd_trident_playback_prepare,
2077 .trigger = snd_trident_trigger,
2078 .pointer = snd_trident_playback_pointer,
2079 .page = snd_pcm_sgbuf_ops_page,
2082 static const struct snd_pcm_ops snd_trident_capture_ops = {
2083 .open = snd_trident_capture_open,
2084 .close = snd_trident_capture_close,
2085 .ioctl = snd_trident_ioctl,
2086 .hw_params = snd_trident_capture_hw_params,
2087 .hw_free = snd_trident_hw_free,
2088 .prepare = snd_trident_capture_prepare,
2089 .trigger = snd_trident_trigger,
2090 .pointer = snd_trident_capture_pointer,
2093 static const struct snd_pcm_ops snd_trident_si7018_capture_ops = {
2094 .open = snd_trident_capture_open,
2095 .close = snd_trident_capture_close,
2096 .ioctl = snd_trident_ioctl,
2097 .hw_params = snd_trident_si7018_capture_hw_params,
2098 .hw_free = snd_trident_si7018_capture_hw_free,
2099 .prepare = snd_trident_si7018_capture_prepare,
2100 .trigger = snd_trident_trigger,
2101 .pointer = snd_trident_playback_pointer,
2104 static const struct snd_pcm_ops snd_trident_foldback_ops = {
2105 .open = snd_trident_foldback_open,
2106 .close = snd_trident_foldback_close,
2107 .ioctl = snd_trident_ioctl,
2108 .hw_params = snd_trident_hw_params,
2109 .hw_free = snd_trident_hw_free,
2110 .prepare = snd_trident_foldback_prepare,
2111 .trigger = snd_trident_trigger,
2112 .pointer = snd_trident_playback_pointer,
2115 static const struct snd_pcm_ops snd_trident_nx_foldback_ops = {
2116 .open = snd_trident_foldback_open,
2117 .close = snd_trident_foldback_close,
2118 .ioctl = snd_trident_ioctl,
2119 .hw_params = snd_trident_hw_params,
2120 .hw_free = snd_trident_hw_free,
2121 .prepare = snd_trident_foldback_prepare,
2122 .trigger = snd_trident_trigger,
2123 .pointer = snd_trident_playback_pointer,
2124 .page = snd_pcm_sgbuf_ops_page,
2127 static const struct snd_pcm_ops snd_trident_spdif_ops = {
2128 .open = snd_trident_spdif_open,
2129 .close = snd_trident_spdif_close,
2130 .ioctl = snd_trident_ioctl,
2131 .hw_params = snd_trident_spdif_hw_params,
2132 .hw_free = snd_trident_hw_free,
2133 .prepare = snd_trident_spdif_prepare,
2134 .trigger = snd_trident_trigger,
2135 .pointer = snd_trident_spdif_pointer,
2138 static const struct snd_pcm_ops snd_trident_spdif_7018_ops = {
2139 .open = snd_trident_spdif_open,
2140 .close = snd_trident_spdif_close,
2141 .ioctl = snd_trident_ioctl,
2142 .hw_params = snd_trident_spdif_hw_params,
2143 .hw_free = snd_trident_hw_free,
2144 .prepare = snd_trident_spdif_prepare,
2145 .trigger = snd_trident_trigger,
2146 .pointer = snd_trident_playback_pointer,
2149 /*---------------------------------------------------------------------------
2152 Description: This routine registers the 4DWave device for PCM support.
2154 Parameters: trident - pointer to target device class for 4DWave.
2158 ---------------------------------------------------------------------------*/
2160 int snd_trident_pcm(struct snd_trident *trident, int device)
2162 struct snd_pcm *pcm;
2165 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
2168 pcm->private_data = trident;
2170 if (trident->tlb.entries) {
2171 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
2173 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
2175 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2176 trident->device != TRIDENT_DEVICE_ID_SI7018 ?
2177 &snd_trident_capture_ops :
2178 &snd_trident_si7018_capture_ops);
2180 pcm->info_flags = 0;
2181 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
2182 strcpy(pcm->name, "Trident 4DWave");
2185 if (trident->tlb.entries) {
2186 struct snd_pcm_substream *substream;
2187 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
2188 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
2189 snd_dma_pci_data(trident->pci),
2191 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2192 SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
2195 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2196 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2202 /*---------------------------------------------------------------------------
2203 snd_trident_foldback_pcm
2205 Description: This routine registers the 4DWave device for foldback PCM support.
2207 Parameters: trident - pointer to target device class for 4DWave.
2211 ---------------------------------------------------------------------------*/
2213 int snd_trident_foldback_pcm(struct snd_trident *trident, int device)
2215 struct snd_pcm *foldback;
2218 struct snd_pcm_substream *substream;
2220 if (trident->device == TRIDENT_DEVICE_ID_NX)
2222 if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
2225 foldback->private_data = trident;
2226 if (trident->tlb.entries)
2227 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
2229 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
2230 foldback->info_flags = 0;
2231 strcpy(foldback->name, "Trident 4DWave");
2232 substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2233 strcpy(substream->name, "Front Mixer");
2234 substream = substream->next;
2235 strcpy(substream->name, "Reverb Mixer");
2236 substream = substream->next;
2237 strcpy(substream->name, "Chorus Mixer");
2238 if (num_chan == 4) {
2239 substream = substream->next;
2240 strcpy(substream->name, "Second AC'97 ADC");
2242 trident->foldback = foldback;
2244 if (trident->tlb.entries)
2245 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
2246 snd_dma_pci_data(trident->pci), 0, 128*1024);
2248 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
2249 snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2254 /*---------------------------------------------------------------------------
2257 Description: This routine registers the 4DWave-NX device for SPDIF support.
2259 Parameters: trident - pointer to target device class for 4DWave-NX.
2263 ---------------------------------------------------------------------------*/
2265 int snd_trident_spdif_pcm(struct snd_trident *trident, int device)
2267 struct snd_pcm *spdif;
2270 if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
2273 spdif->private_data = trident;
2274 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2275 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
2277 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
2279 spdif->info_flags = 0;
2280 strcpy(spdif->name, "Trident 4DWave IEC958");
2281 trident->spdif = spdif;
2283 snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2293 /*---------------------------------------------------------------------------
2294 snd_trident_spdif_control
2296 Description: enable/disable S/PDIF out from ac97 mixer
2297 ---------------------------------------------------------------------------*/
2299 #define snd_trident_spdif_control_info snd_ctl_boolean_mono_info
2301 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2304 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2307 spin_lock_irq(&trident->reg_lock);
2308 val = trident->spdif_ctrl;
2309 ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2310 spin_unlock_irq(&trident->reg_lock);
2314 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2315 struct snd_ctl_elem_value *ucontrol)
2317 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2321 val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2322 spin_lock_irq(&trident->reg_lock);
2323 /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2324 change = trident->spdif_ctrl != val;
2325 trident->spdif_ctrl = val;
2326 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2327 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2328 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2329 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2332 if (trident->spdif == NULL) {
2334 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2335 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2338 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2341 spin_unlock_irq(&trident->reg_lock);
2345 static const struct snd_kcontrol_new snd_trident_spdif_control =
2347 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2348 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2349 .info = snd_trident_spdif_control_info,
2350 .get = snd_trident_spdif_control_get,
2351 .put = snd_trident_spdif_control_put,
2352 .private_value = 0x28,
2355 /*---------------------------------------------------------------------------
2356 snd_trident_spdif_default
2358 Description: put/get the S/PDIF default settings
2359 ---------------------------------------------------------------------------*/
2361 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_info *uinfo)
2364 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2369 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2372 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2374 spin_lock_irq(&trident->reg_lock);
2375 ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2376 ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2377 ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2378 ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2379 spin_unlock_irq(&trident->reg_lock);
2383 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2384 struct snd_ctl_elem_value *ucontrol)
2386 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2390 val = (ucontrol->value.iec958.status[0] << 0) |
2391 (ucontrol->value.iec958.status[1] << 8) |
2392 (ucontrol->value.iec958.status[2] << 16) |
2393 (ucontrol->value.iec958.status[3] << 24);
2394 spin_lock_irq(&trident->reg_lock);
2395 change = trident->spdif_bits != val;
2396 trident->spdif_bits = val;
2397 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2398 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2399 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2401 if (trident->spdif == NULL)
2402 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2404 spin_unlock_irq(&trident->reg_lock);
2408 static const struct snd_kcontrol_new snd_trident_spdif_default =
2410 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2411 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2412 .info = snd_trident_spdif_default_info,
2413 .get = snd_trident_spdif_default_get,
2414 .put = snd_trident_spdif_default_put
2417 /*---------------------------------------------------------------------------
2418 snd_trident_spdif_mask
2420 Description: put/get the S/PDIF mask
2421 ---------------------------------------------------------------------------*/
2423 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_info *uinfo)
2426 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2431 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_value *ucontrol)
2434 ucontrol->value.iec958.status[0] = 0xff;
2435 ucontrol->value.iec958.status[1] = 0xff;
2436 ucontrol->value.iec958.status[2] = 0xff;
2437 ucontrol->value.iec958.status[3] = 0xff;
2441 static const struct snd_kcontrol_new snd_trident_spdif_mask =
2443 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2444 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2445 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2446 .info = snd_trident_spdif_mask_info,
2447 .get = snd_trident_spdif_mask_get,
2450 /*---------------------------------------------------------------------------
2451 snd_trident_spdif_stream
2453 Description: put/get the S/PDIF stream settings
2454 ---------------------------------------------------------------------------*/
2456 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2459 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2464 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_value *ucontrol)
2467 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2469 spin_lock_irq(&trident->reg_lock);
2470 ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2471 ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2472 ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2473 ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2474 spin_unlock_irq(&trident->reg_lock);
2478 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2479 struct snd_ctl_elem_value *ucontrol)
2481 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2485 val = (ucontrol->value.iec958.status[0] << 0) |
2486 (ucontrol->value.iec958.status[1] << 8) |
2487 (ucontrol->value.iec958.status[2] << 16) |
2488 (ucontrol->value.iec958.status[3] << 24);
2489 spin_lock_irq(&trident->reg_lock);
2490 change = trident->spdif_pcm_bits != val;
2491 trident->spdif_pcm_bits = val;
2492 if (trident->spdif != NULL) {
2493 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2494 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2496 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2499 spin_unlock_irq(&trident->reg_lock);
2503 static const struct snd_kcontrol_new snd_trident_spdif_stream =
2505 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2506 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2507 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2508 .info = snd_trident_spdif_stream_info,
2509 .get = snd_trident_spdif_stream_get,
2510 .put = snd_trident_spdif_stream_put
2513 /*---------------------------------------------------------------------------
2514 snd_trident_ac97_control
2516 Description: enable/disable rear path for ac97
2517 ---------------------------------------------------------------------------*/
2519 #define snd_trident_ac97_control_info snd_ctl_boolean_mono_info
2521 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_value *ucontrol)
2524 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2527 spin_lock_irq(&trident->reg_lock);
2528 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2529 ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2530 spin_unlock_irq(&trident->reg_lock);
2534 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2535 struct snd_ctl_elem_value *ucontrol)
2537 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2541 spin_lock_irq(&trident->reg_lock);
2542 val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2543 val &= ~(1 << kcontrol->private_value);
2544 if (ucontrol->value.integer.value[0])
2545 val |= 1 << kcontrol->private_value;
2546 change = val != trident->ac97_ctrl;
2547 trident->ac97_ctrl = val;
2548 outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2549 spin_unlock_irq(&trident->reg_lock);
2553 static const struct snd_kcontrol_new snd_trident_ac97_rear_control =
2555 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2556 .name = "Rear Path",
2557 .info = snd_trident_ac97_control_info,
2558 .get = snd_trident_ac97_control_get,
2559 .put = snd_trident_ac97_control_put,
2563 /*---------------------------------------------------------------------------
2564 snd_trident_vol_control
2566 Description: wave & music volume control
2567 ---------------------------------------------------------------------------*/
2569 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_info *uinfo)
2572 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2574 uinfo->value.integer.min = 0;
2575 uinfo->value.integer.max = 255;
2579 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_value *ucontrol)
2582 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2585 val = trident->musicvol_wavevol;
2586 ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2587 ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2591 static const DECLARE_TLV_DB_SCALE(db_scale_gvol, -6375, 25, 0);
2593 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2596 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2600 spin_lock_irq(&trident->reg_lock);
2601 val = trident->musicvol_wavevol;
2602 val &= ~(0xffff << kcontrol->private_value);
2603 val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2604 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2605 change = val != trident->musicvol_wavevol;
2606 outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2607 spin_unlock_irq(&trident->reg_lock);
2611 static const struct snd_kcontrol_new snd_trident_vol_music_control =
2613 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2614 .name = "Music Playback Volume",
2615 .info = snd_trident_vol_control_info,
2616 .get = snd_trident_vol_control_get,
2617 .put = snd_trident_vol_control_put,
2618 .private_value = 16,
2619 .tlv = { .p = db_scale_gvol },
2622 static const struct snd_kcontrol_new snd_trident_vol_wave_control =
2624 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2625 .name = "Wave Playback Volume",
2626 .info = snd_trident_vol_control_info,
2627 .get = snd_trident_vol_control_get,
2628 .put = snd_trident_vol_control_put,
2630 .tlv = { .p = db_scale_gvol },
2633 /*---------------------------------------------------------------------------
2634 snd_trident_pcm_vol_control
2636 Description: PCM front volume control
2637 ---------------------------------------------------------------------------*/
2639 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2640 struct snd_ctl_elem_info *uinfo)
2642 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2644 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2646 uinfo->value.integer.min = 0;
2647 uinfo->value.integer.max = 255;
2648 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2649 uinfo->value.integer.max = 1023;
2653 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2654 struct snd_ctl_elem_value *ucontrol)
2656 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2657 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2659 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2660 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2662 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2667 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
2670 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2671 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2675 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2676 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2678 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2680 spin_lock_irq(&trident->reg_lock);
2681 change = val != mix->vol;
2683 if (mix->voice != NULL)
2684 snd_trident_write_vol_reg(trident, mix->voice, val);
2685 spin_unlock_irq(&trident->reg_lock);
2689 static const struct snd_kcontrol_new snd_trident_pcm_vol_control =
2691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2692 .name = "PCM Front Playback Volume",
2693 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2695 .info = snd_trident_pcm_vol_control_info,
2696 .get = snd_trident_pcm_vol_control_get,
2697 .put = snd_trident_pcm_vol_control_put,
2698 /* FIXME: no tlv yet */
2701 /*---------------------------------------------------------------------------
2702 snd_trident_pcm_pan_control
2704 Description: PCM front pan control
2705 ---------------------------------------------------------------------------*/
2707 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_info *uinfo)
2710 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2712 uinfo->value.integer.min = 0;
2713 uinfo->value.integer.max = 127;
2717 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_value *ucontrol)
2720 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2721 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2723 ucontrol->value.integer.value[0] = mix->pan;
2724 if (ucontrol->value.integer.value[0] & 0x40) {
2725 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2727 ucontrol->value.integer.value[0] |= 0x40;
2732 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_value *ucontrol)
2735 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2736 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2740 if (ucontrol->value.integer.value[0] & 0x40)
2741 val = ucontrol->value.integer.value[0] & 0x3f;
2743 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2744 spin_lock_irq(&trident->reg_lock);
2745 change = val != mix->pan;
2747 if (mix->voice != NULL)
2748 snd_trident_write_pan_reg(trident, mix->voice, val);
2749 spin_unlock_irq(&trident->reg_lock);
2753 static const struct snd_kcontrol_new snd_trident_pcm_pan_control =
2755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2756 .name = "PCM Pan Playback Control",
2757 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2759 .info = snd_trident_pcm_pan_control_info,
2760 .get = snd_trident_pcm_pan_control_get,
2761 .put = snd_trident_pcm_pan_control_put,
2764 /*---------------------------------------------------------------------------
2765 snd_trident_pcm_rvol_control
2767 Description: PCM reverb volume control
2768 ---------------------------------------------------------------------------*/
2770 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2771 struct snd_ctl_elem_info *uinfo)
2773 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2775 uinfo->value.integer.min = 0;
2776 uinfo->value.integer.max = 127;
2780 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_value *ucontrol)
2783 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2784 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2786 ucontrol->value.integer.value[0] = 127 - mix->rvol;
2790 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_value *ucontrol)
2793 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2794 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2798 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2799 spin_lock_irq(&trident->reg_lock);
2800 change = val != mix->rvol;
2802 if (mix->voice != NULL)
2803 snd_trident_write_rvol_reg(trident, mix->voice, val);
2804 spin_unlock_irq(&trident->reg_lock);
2808 static const DECLARE_TLV_DB_SCALE(db_scale_crvol, -3175, 25, 1);
2810 static const struct snd_kcontrol_new snd_trident_pcm_rvol_control =
2812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2813 .name = "PCM Reverb Playback Volume",
2814 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2816 .info = snd_trident_pcm_rvol_control_info,
2817 .get = snd_trident_pcm_rvol_control_get,
2818 .put = snd_trident_pcm_rvol_control_put,
2819 .tlv = { .p = db_scale_crvol },
2822 /*---------------------------------------------------------------------------
2823 snd_trident_pcm_cvol_control
2825 Description: PCM chorus volume control
2826 ---------------------------------------------------------------------------*/
2828 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2829 struct snd_ctl_elem_info *uinfo)
2831 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2833 uinfo->value.integer.min = 0;
2834 uinfo->value.integer.max = 127;
2838 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_value *ucontrol)
2841 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2842 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2844 ucontrol->value.integer.value[0] = 127 - mix->cvol;
2848 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2849 struct snd_ctl_elem_value *ucontrol)
2851 struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2852 struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2856 val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2857 spin_lock_irq(&trident->reg_lock);
2858 change = val != mix->cvol;
2860 if (mix->voice != NULL)
2861 snd_trident_write_cvol_reg(trident, mix->voice, val);
2862 spin_unlock_irq(&trident->reg_lock);
2866 static const struct snd_kcontrol_new snd_trident_pcm_cvol_control =
2868 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2869 .name = "PCM Chorus Playback Volume",
2870 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2872 .info = snd_trident_pcm_cvol_control_info,
2873 .get = snd_trident_pcm_cvol_control_get,
2874 .put = snd_trident_pcm_cvol_control_put,
2875 .tlv = { .p = db_scale_crvol },
2878 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2879 struct snd_kcontrol *kctl,
2880 int num, int activate)
2882 struct snd_ctl_elem_id id;
2887 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2889 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2890 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2891 SNDRV_CTL_EVENT_MASK_INFO,
2892 snd_ctl_build_ioff(&id, kctl, num));
2895 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2896 struct snd_trident_pcm_mixer *tmix,
2897 int num, int activate)
2899 snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2900 snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2901 snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2902 snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2905 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2906 struct snd_trident_voice *voice,
2907 struct snd_pcm_substream *substream)
2909 struct snd_trident_pcm_mixer *tmix;
2911 if (snd_BUG_ON(!trident || !voice || !substream))
2913 tmix = &trident->pcm_mixer[substream->number];
2914 tmix->voice = voice;
2915 tmix->vol = T4D_DEFAULT_PCM_VOL;
2916 tmix->pan = T4D_DEFAULT_PCM_PAN;
2917 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2918 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2919 snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2923 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2925 struct snd_trident_pcm_mixer *tmix;
2927 if (snd_BUG_ON(!trident || !substream))
2929 tmix = &trident->pcm_mixer[substream->number];
2931 snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2935 /*---------------------------------------------------------------------------
2938 Description: This routine registers the 4DWave device for mixer support.
2940 Parameters: trident - pointer to target device class for 4DWave.
2944 ---------------------------------------------------------------------------*/
2946 static int snd_trident_mixer(struct snd_trident *trident, int pcm_spdif_device)
2948 struct snd_ac97_template _ac97;
2949 struct snd_card *card = trident->card;
2950 struct snd_kcontrol *kctl;
2951 struct snd_ctl_elem_value *uctl;
2952 int idx, err, retries = 2;
2953 static struct snd_ac97_bus_ops ops = {
2954 .write = snd_trident_codec_write,
2955 .read = snd_trident_codec_read,
2958 uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
2962 if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
2965 memset(&_ac97, 0, sizeof(_ac97));
2966 _ac97.private_data = trident;
2967 trident->ac97_detect = 1;
2970 if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
2971 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2972 if ((err = snd_trident_sis_reset(trident)) < 0)
2981 /* secondary codec? */
2982 if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
2983 (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
2985 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
2987 dev_err(trident->card->dev,
2988 "SI7018: the secondary codec - invalid access\n");
2989 #if 0 // only for my testing purpose --jk
2991 struct snd_ac97 *mc97;
2992 err = snd_ac97_modem(trident->card, &_ac97, &mc97);
2994 dev_err(trident->card->dev,
2995 "snd_ac97_modem returned error %i\n", err);
3000 trident->ac97_detect = 0;
3002 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3003 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3005 kctl->put(kctl, uctl);
3006 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3008 kctl->put(kctl, uctl);
3009 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3011 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3014 for (idx = 0; idx < 32; idx++) {
3015 struct snd_trident_pcm_mixer *tmix;
3017 tmix = &trident->pcm_mixer[idx];
3020 if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3022 if ((err = snd_ctl_add(card, trident->ctl_vol)))
3025 if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3027 if ((err = snd_ctl_add(card, trident->ctl_pan)))
3030 if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3032 if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3035 if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3037 if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3040 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3041 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3043 kctl->put(kctl, uctl);
3045 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3047 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3052 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3054 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3056 idx = kctl->id.index;
3057 if ((err = snd_ctl_add(card, kctl)) < 0)
3059 kctl->put(kctl, uctl);
3061 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3066 kctl->id.index = idx;
3067 kctl->id.device = pcm_spdif_device;
3068 if ((err = snd_ctl_add(card, kctl)) < 0)
3071 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3076 kctl->id.index = idx;
3077 kctl->id.device = pcm_spdif_device;
3078 if ((err = snd_ctl_add(card, kctl)) < 0)
3081 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3086 kctl->id.index = idx;
3087 kctl->id.device = pcm_spdif_device;
3088 if ((err = snd_ctl_add(card, kctl)) < 0)
3090 trident->spdif_pcm_ctl = kctl;
3106 * gameport interface
3109 #if IS_REACHABLE(CONFIG_GAMEPORT)
3111 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3113 struct snd_trident *chip = gameport_get_port_data(gameport);
3115 if (snd_BUG_ON(!chip))
3117 return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3120 static void snd_trident_gameport_trigger(struct gameport *gameport)
3122 struct snd_trident *chip = gameport_get_port_data(gameport);
3124 if (snd_BUG_ON(!chip))
3126 outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3129 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3131 struct snd_trident *chip = gameport_get_port_data(gameport);
3134 if (snd_BUG_ON(!chip))
3137 *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3139 for (i = 0; i < 4; i++) {
3140 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3141 if (axes[i] == 0xffff) axes[i] = -1;
3147 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3149 struct snd_trident *chip = gameport_get_port_data(gameport);
3151 if (snd_BUG_ON(!chip))
3155 case GAMEPORT_MODE_COOKED:
3156 outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3159 case GAMEPORT_MODE_RAW:
3160 outb(0, TRID_REG(chip, GAMEPORT_GCR));
3167 int snd_trident_create_gameport(struct snd_trident *chip)
3169 struct gameport *gp;
3171 chip->gameport = gp = gameport_allocate_port();
3173 dev_err(chip->card->dev,
3174 "cannot allocate memory for gameport\n");
3178 gameport_set_name(gp, "Trident 4DWave");
3179 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3180 gameport_set_dev_parent(gp, &chip->pci->dev);
3182 gameport_set_port_data(gp, chip);
3184 gp->read = snd_trident_gameport_read;
3185 gp->trigger = snd_trident_gameport_trigger;
3186 gp->cooked_read = snd_trident_gameport_cooked_read;
3187 gp->open = snd_trident_gameport_open;
3189 gameport_register_port(gp);
3194 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3196 if (chip->gameport) {
3197 gameport_unregister_port(chip->gameport);
3198 chip->gameport = NULL;
3202 int snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3203 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3204 #endif /* CONFIG_GAMEPORT */
3209 static inline void do_delay(struct snd_trident *chip)
3211 schedule_timeout_uninterruptible(1);
3218 static int snd_trident_sis_reset(struct snd_trident *trident)
3220 unsigned long end_time;
3224 r = trident->in_suspend ? 0 : 2; /* count of retries */
3226 pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
3228 pci_write_config_byte(trident->pci, 0x46, 0x00);
3230 /* disable AC97 GPIO interrupt */
3231 outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3232 /* initialize serial interface, force cold reset */
3233 i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3234 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3236 /* remove cold reset */
3238 outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3240 /* wait, until the codec is ready */
3241 end_time = (jiffies + (HZ * 3) / 4) + 1;
3243 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3246 } while (time_after_eq(end_time, jiffies));
3247 dev_err(trident->card->dev, "AC'97 codec ready error [0x%x]\n",
3248 inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3250 end_time = jiffies + HZ;
3253 } while (time_after_eq(end_time, jiffies));
3254 goto __si7018_retry;
3257 /* wait for the second codec */
3259 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3262 } while (time_after_eq(end_time, jiffies));
3263 /* enable 64 channel mode */
3264 outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3272 static void snd_trident_proc_read(struct snd_info_entry *entry,
3273 struct snd_info_buffer *buffer)
3275 struct snd_trident *trident = entry->private_data;
3278 switch (trident->device) {
3279 case TRIDENT_DEVICE_ID_SI7018:
3280 s = "SiS 7018 Audio";
3282 case TRIDENT_DEVICE_ID_DX:
3283 s = "Trident 4DWave PCI DX";
3285 case TRIDENT_DEVICE_ID_NX:
3286 s = "Trident 4DWave PCI NX";
3291 snd_iprintf(buffer, "%s\n\n", s);
3292 snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
3293 snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3294 if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3295 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3296 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3297 snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3298 if (trident->tlb.entries) {
3299 snd_iprintf(buffer,"\nVirtual Memory\n");
3300 snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3301 snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
3302 snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3307 static void snd_trident_proc_init(struct snd_trident *trident)
3309 const char *s = "trident";
3311 if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3313 snd_card_ro_proc_new(trident->card, s, trident, snd_trident_proc_read);
3316 static int snd_trident_dev_free(struct snd_device *device)
3318 struct snd_trident *trident = device->device_data;
3319 return snd_trident_free(trident);
3322 /*---------------------------------------------------------------------------
3323 snd_trident_tlb_alloc
3325 Description: Allocate and set up the TLB page table on 4D NX.
3326 Each entry has 4 bytes (physical PCI address).
3328 Parameters: trident - pointer to target device class for 4DWave.
3330 Returns: 0 or negative error code
3332 ---------------------------------------------------------------------------*/
3334 static int snd_trident_tlb_alloc(struct snd_trident *trident)
3338 /* TLB array must be aligned to 16kB !!! so we allocate
3339 32kB region and correct offset when necessary */
3341 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3342 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3343 dev_err(trident->card->dev, "unable to allocate TLB buffer\n");
3346 trident->tlb.entries = (__le32 *)ALIGN((unsigned long)trident->tlb.buffer.area, SNDRV_TRIDENT_MAX_PAGES * 4);
3347 trident->tlb.entries_dmaaddr = ALIGN(trident->tlb.buffer.addr, SNDRV_TRIDENT_MAX_PAGES * 4);
3348 /* allocate shadow TLB page table (virtual addresses) */
3349 trident->tlb.shadow_entries =
3350 vmalloc(array_size(SNDRV_TRIDENT_MAX_PAGES,
3351 sizeof(unsigned long)));
3352 if (!trident->tlb.shadow_entries)
3355 /* allocate and setup silent page and initialise TLB entries */
3356 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3357 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3358 dev_err(trident->card->dev, "unable to allocate silent page\n");
3361 memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3362 for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3363 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3364 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3367 /* use emu memory block manager code to manage tlb page allocation */
3368 trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3369 if (trident->tlb.memhdr == NULL)
3372 trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3377 * initialize 4D DX chip
3380 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3382 outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3383 outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3384 outl(0, TRID_REG(trident, T4D_AINTEN_A));
3385 outl(0, TRID_REG(trident, T4D_AINTEN_B));
3388 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3390 struct pci_dev *pci = trident->pci;
3391 unsigned long end_time;
3393 /* reset the legacy configuration and whole audio/wavetable block */
3394 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3395 pci_write_config_byte(pci, 0x44, 0); /* ports */
3396 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3397 pci_write_config_byte(pci, 0x46, 4); /* reset */
3399 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3402 /* warm reset of the AC'97 codec */
3403 outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3405 outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3406 /* DAC on, disable SB IRQ and try to force ADC valid signal */
3407 trident->ac97_ctrl = 0x0000004a;
3408 outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3409 /* wait, until the codec is ready */
3410 end_time = (jiffies + (HZ * 3) / 4) + 1;
3412 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3415 } while (time_after_eq(end_time, jiffies));
3416 dev_err(trident->card->dev, "AC'97 codec ready error\n");
3420 snd_trident_stop_all_voices(trident);
3426 * initialize 4D NX chip
3428 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3430 struct pci_dev *pci = trident->pci;
3431 unsigned long end_time;
3433 /* reset the legacy configuration and whole audio/wavetable block */
3434 pci_write_config_dword(pci, 0x40, 0); /* DDMA */
3435 pci_write_config_byte(pci, 0x44, 0); /* ports */
3436 pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
3438 pci_write_config_byte(pci, 0x46, 1); /* reset */
3440 pci_write_config_byte(pci, 0x46, 0); /* release reset */
3443 /* warm reset of the AC'97 codec */
3444 outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3446 outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3447 /* wait, until the codec is ready */
3448 end_time = (jiffies + (HZ * 3) / 4) + 1;
3450 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3453 } while (time_after_eq(end_time, jiffies));
3454 dev_err(trident->card->dev, "AC'97 codec ready error [0x%x]\n",
3455 inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3460 trident->ac97_ctrl = 0x00000002;
3461 outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3462 /* disable SB IRQ */
3463 outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3465 snd_trident_stop_all_voices(trident);
3467 if (trident->tlb.entries != NULL) {
3469 /* enable virtual addressing via TLB */
3470 i = trident->tlb.entries_dmaaddr;
3472 outl(i, TRID_REG(trident, NX_TLBC));
3474 outl(0, TRID_REG(trident, NX_TLBC));
3476 /* initialize S/PDIF */
3477 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3478 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3484 * initialize sis7018 chip
3486 static int snd_trident_sis_init(struct snd_trident *trident)
3490 if ((err = snd_trident_sis_reset(trident)) < 0)
3493 snd_trident_stop_all_voices(trident);
3495 /* initialize S/PDIF */
3496 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3501 /*---------------------------------------------------------------------------
3504 Description: This routine will create the device specific class for
3505 the 4DWave card. It will also perform basic initialization.
3507 Parameters: card - which card to create
3508 pci - interface to PCI bus resource info
3509 dma1ptr - playback dma buffer
3510 dma2ptr - capture dma buffer
3511 irqptr - interrupt resource info
3513 Returns: 4DWave device class private data
3515 ---------------------------------------------------------------------------*/
3517 int snd_trident_create(struct snd_card *card,
3518 struct pci_dev *pci,
3520 int pcm_spdif_device,
3521 int max_wavetable_size,
3522 struct snd_trident ** rtrident)
3524 struct snd_trident *trident;
3526 struct snd_trident_voice *voice;
3527 struct snd_trident_pcm_mixer *tmix;
3528 static struct snd_device_ops ops = {
3529 .dev_free = snd_trident_dev_free,
3534 /* enable PCI device */
3535 if ((err = pci_enable_device(pci)) < 0)
3537 /* check, if we can restrict PCI DMA transfers to 30 bits */
3538 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(30)) < 0 ||
3539 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(30)) < 0) {
3541 "architecture does not support 30bit PCI busmaster DMA\n");
3542 pci_disable_device(pci);
3546 trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3547 if (trident == NULL) {
3548 pci_disable_device(pci);
3551 trident->device = (pci->vendor << 16) | pci->device;
3552 trident->card = card;
3554 spin_lock_init(&trident->reg_lock);
3555 spin_lock_init(&trident->event_lock);
3556 spin_lock_init(&trident->voice_alloc);
3557 if (pcm_streams < 1)
3559 if (pcm_streams > 32)
3561 trident->ChanPCM = pcm_streams;
3562 if (max_wavetable_size < 0 )
3563 max_wavetable_size = 0;
3564 trident->synth.max_size = max_wavetable_size * 1024;
3567 trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3568 pci_set_master(pci);
3570 if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3572 pci_disable_device(pci);
3575 trident->port = pci_resource_start(pci, 0);
3577 if (request_irq(pci->irq, snd_trident_interrupt, IRQF_SHARED,
3578 KBUILD_MODNAME, trident)) {
3579 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
3580 snd_trident_free(trident);
3583 trident->irq = pci->irq;
3585 /* allocate 16k-aligned TLB for NX cards */
3586 trident->tlb.entries = NULL;
3587 trident->tlb.buffer.area = NULL;
3588 if (trident->device == TRIDENT_DEVICE_ID_NX) {
3589 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3590 snd_trident_free(trident);
3595 trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3597 /* initialize chip */
3598 switch (trident->device) {
3599 case TRIDENT_DEVICE_ID_DX:
3600 err = snd_trident_4d_dx_init(trident);
3602 case TRIDENT_DEVICE_ID_NX:
3603 err = snd_trident_4d_nx_init(trident);
3605 case TRIDENT_DEVICE_ID_SI7018:
3606 err = snd_trident_sis_init(trident);
3613 snd_trident_free(trident);
3617 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3618 snd_trident_free(trident);
3622 if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3625 /* initialise synth voices */
3626 for (i = 0; i < 64; i++) {
3627 voice = &trident->synth.voices[i];
3629 voice->trident = trident;
3631 /* initialize pcm mixer entries */
3632 for (i = 0; i < 32; i++) {
3633 tmix = &trident->pcm_mixer[i];
3634 tmix->vol = T4D_DEFAULT_PCM_VOL;
3635 tmix->pan = T4D_DEFAULT_PCM_PAN;
3636 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3637 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3640 snd_trident_enable_eso(trident);
3642 snd_trident_proc_init(trident);
3643 *rtrident = trident;
3647 /*---------------------------------------------------------------------------
3650 Description: This routine will free the device specific class for
3653 Parameters: trident - device specific private data for 4DWave card
3657 ---------------------------------------------------------------------------*/
3659 static int snd_trident_free(struct snd_trident *trident)
3661 snd_trident_free_gameport(trident);
3662 snd_trident_disable_eso(trident);
3663 // Disable S/PDIF out
3664 if (trident->device == TRIDENT_DEVICE_ID_NX)
3665 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3666 else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3667 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3669 if (trident->irq >= 0)
3670 free_irq(trident->irq, trident);
3671 if (trident->tlb.buffer.area) {
3672 outl(0, TRID_REG(trident, NX_TLBC));
3673 snd_util_memhdr_free(trident->tlb.memhdr);
3674 if (trident->tlb.silent_page.area)
3675 snd_dma_free_pages(&trident->tlb.silent_page);
3676 vfree(trident->tlb.shadow_entries);
3677 snd_dma_free_pages(&trident->tlb.buffer);
3679 pci_release_regions(trident->pci);
3680 pci_disable_device(trident->pci);
3685 /*---------------------------------------------------------------------------
3686 snd_trident_interrupt
3688 Description: ISR for Trident 4DWave device
3690 Parameters: trident - device specific private data for 4DWave card
3692 Problems: It seems that Trident chips generates interrupts more than
3693 one time in special cases. The spurious interrupts are
3694 detected via sample timer (T4D_STIMER) and computing
3695 corresponding delta value. The limits are detected with
3696 the method try & fail so it is possible that it won't
3697 work on all computers. [jaroslav]
3701 ---------------------------------------------------------------------------*/
3703 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id)
3705 struct snd_trident *trident = dev_id;
3706 unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3708 struct snd_trident_voice *voice;
3710 audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3711 if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3713 if (audio_int & ADDRESS_IRQ) {
3714 // get interrupt status for all channels
3715 spin_lock(&trident->reg_lock);
3716 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3717 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3720 outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
3722 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3725 for (channel = 63; channel >= 32; channel--) {
3726 mask = 1 << (channel&0x1f);
3727 if ((chn_int & mask) == 0)
3729 voice = &trident->synth.voices[channel];
3730 if (!voice->pcm || voice->substream == NULL) {
3731 outl(mask, TRID_REG(trident, T4D_STOP_B));
3734 delta = (int)stimer - (int)voice->stimer;
3737 if ((unsigned int)delta < voice->spurious_threshold) {
3738 /* do some statistics here */
3739 trident->spurious_irq_count++;
3740 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3741 trident->spurious_irq_max_delta = delta;
3744 voice->stimer = stimer;
3746 if (!voice->isync3) {
3747 tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3748 if (trident->bDMAStart & 0x40)
3751 tmp = voice->isync_max - tmp;
3753 tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3755 if (tmp < voice->isync_mark) {
3757 tmp = voice->isync_ESO - 7;
3759 tmp = voice->isync_ESO + 2;
3760 /* update ESO for IRQ voice to preserve sync */
3761 snd_trident_stop_voice(trident, voice->number);
3762 snd_trident_write_eso_reg(trident, voice, tmp);
3763 snd_trident_start_voice(trident, voice->number);
3765 } else if (voice->isync2) {
3767 /* write original ESO and update CSO for IRQ voice to preserve sync */
3768 snd_trident_stop_voice(trident, voice->number);
3769 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3770 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3771 snd_trident_start_voice(trident, voice->number);
3775 /* update CSO for extra voice to preserve sync */
3776 snd_trident_stop_voice(trident, voice->extra->number);
3777 snd_trident_write_cso_reg(trident, voice->extra, 0);
3778 snd_trident_start_voice(trident, voice->extra->number);
3781 spin_unlock(&trident->reg_lock);
3782 snd_pcm_period_elapsed(voice->substream);
3783 spin_lock(&trident->reg_lock);
3785 outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
3787 spin_unlock(&trident->reg_lock);
3789 if (audio_int & MPU401_IRQ) {
3790 if (trident->rmidi) {
3791 snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data);
3793 inb(TRID_REG(trident, T4D_MPUR0));
3796 // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3800 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3802 struct snd_trident_voice *pvoice;
3803 unsigned long flags;
3806 spin_lock_irqsave(&trident->voice_alloc, flags);
3807 if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3808 idx = snd_trident_allocate_pcm_channel(trident);
3810 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3813 pvoice = &trident->synth.voices[idx];
3816 pvoice->capture = 0;
3818 pvoice->memblk = NULL;
3819 pvoice->substream = NULL;
3820 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3823 if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3824 idx = snd_trident_allocate_synth_channel(trident);
3826 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3829 pvoice = &trident->synth.voices[idx];
3832 pvoice->client = client;
3833 pvoice->port = port;
3834 pvoice->memblk = NULL;
3835 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3838 if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3840 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3844 EXPORT_SYMBOL(snd_trident_alloc_voice);
3846 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3848 unsigned long flags;
3849 void (*private_free)(struct snd_trident_voice *);
3851 if (voice == NULL || !voice->use)
3853 snd_trident_clear_voices(trident, voice->number, voice->number);
3854 spin_lock_irqsave(&trident->voice_alloc, flags);
3855 private_free = voice->private_free;
3856 voice->private_free = NULL;
3857 voice->private_data = NULL;
3859 snd_trident_free_pcm_channel(trident, voice->number);
3861 snd_trident_free_synth_channel(trident, voice->number);
3862 voice->use = voice->pcm = voice->synth = voice->midi = 0;
3863 voice->capture = voice->spdif = 0;
3864 voice->sample_ops = NULL;
3865 voice->substream = NULL;
3866 voice->extra = NULL;
3867 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3869 private_free(voice);
3872 EXPORT_SYMBOL(snd_trident_free_voice);
3874 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3876 unsigned int i, val, mask[2] = { 0, 0 };
3878 if (snd_BUG_ON(v_min > 63 || v_max > 63))
3880 for (i = v_min; i <= v_max; i++)
3881 mask[i >> 5] |= 1 << (i & 0x1f);
3883 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3884 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3885 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3888 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3889 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3890 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3894 #ifdef CONFIG_PM_SLEEP
3895 static int snd_trident_suspend(struct device *dev)
3897 struct snd_card *card = dev_get_drvdata(dev);
3898 struct snd_trident *trident = card->private_data;
3900 trident->in_suspend = 1;
3901 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3902 snd_ac97_suspend(trident->ac97);
3903 snd_ac97_suspend(trident->ac97_sec);
3907 static int snd_trident_resume(struct device *dev)
3909 struct snd_card *card = dev_get_drvdata(dev);
3910 struct snd_trident *trident = card->private_data;
3912 switch (trident->device) {
3913 case TRIDENT_DEVICE_ID_DX:
3914 snd_trident_4d_dx_init(trident);
3916 case TRIDENT_DEVICE_ID_NX:
3917 snd_trident_4d_nx_init(trident);
3919 case TRIDENT_DEVICE_ID_SI7018:
3920 snd_trident_sis_init(trident);
3924 snd_ac97_resume(trident->ac97);
3925 snd_ac97_resume(trident->ac97_sec);
3927 /* restore some registers */
3928 outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3930 snd_trident_enable_eso(trident);
3932 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3933 trident->in_suspend = 0;
3937 SIMPLE_DEV_PM_OPS(snd_trident_pm, snd_trident_suspend, snd_trident_resume);
3938 #endif /* CONFIG_PM_SLEEP */