8ac6f541b008304a6dfa45377e69465d283bf0a3
[linux-2.6-block.git] / sound / pci / trident / trident_main.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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
7  *
8  *  BUGS:
9  *
10  *  TODO:
11  *    ---
12  *
13  *  SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
14  */
15
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>
25 #include <linux/io.h>
26
27 #include <sound/core.h>
28 #include <sound/info.h>
29 #include <sound/control.h>
30 #include <sound/tlv.h>
31 #include "trident.h"
32 #include <sound/asoundef.h>
33
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);
42
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);
46
47 /*
48  *  common I/O routines
49  */
50
51
52 #if 0
53 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
54 {
55         unsigned int val, tmp;
56
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);
87         }
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);
91 }
92 #endif
93
94 /*---------------------------------------------------------------------------
95    unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
96   
97    Description: This routine will do all of the reading from the external
98                 CODEC (AC97).
99   
100    Parameters:  ac97 - ac97 codec structure
101                 reg - CODEC register index, from AC97 Hal.
102  
103    returns:     16 bit value read from the AC97.
104   
105   ---------------------------------------------------------------------------*/
106 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
107 {
108         unsigned int data = 0, treg;
109         unsigned short count = 0xffff;
110         unsigned long flags;
111         struct snd_trident *trident = ac97->private_data;
112
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));
117                 do {
118                         data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
119                         if ((data & DX_AC97_BUSY_READ) == 0)
120                                 break;
121                 } while (--count);
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));
126                 do {
127                         data = inl(TRID_REG(trident, treg));
128                         if ((data & 0x00000C00) == 0)
129                                 break;
130                 } while (--count);
131         } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
132                 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
133                 if (ac97->num == 1)
134                         data |= SI_AC97_SECONDARY;
135                 outl(data, TRID_REG(trident, SI_AC97_READ));
136                 do {
137                         data = inl(TRID_REG(trident, SI_AC97_READ));
138                         if ((data & (SI_AC97_BUSY_READ)) == 0)
139                                 break;
140                 } while (--count);
141         }
142
143         if (count == 0 && !trident->ac97_detect) {
144                 dev_err(trident->card->dev,
145                         "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
146                            reg, data);
147                 data = 0;
148         }
149
150         spin_unlock_irqrestore(&trident->reg_lock, flags);
151         return ((unsigned short) (data >> 16));
152 }
153
154 /*---------------------------------------------------------------------------
155    void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
156    unsigned short wdata)
157   
158    Description: This routine will do all of the writing to the external
159                 CODEC (AC97).
160   
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.
164   
165    returns:     TRUE if everything went ok, else FALSE.
166   
167   ---------------------------------------------------------------------------*/
168 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
169                                     unsigned short wdata)
170 {
171         unsigned int address, data;
172         unsigned short count = 0xffff;
173         unsigned long flags;
174         struct snd_trident *trident = ac97->private_data;
175
176         data = ((unsigned long) wdata) << 16;
177
178         spin_lock_irqsave(&trident->reg_lock, flags);
179         if (trident->device == TRIDENT_DEVICE_ID_DX) {
180                 address = DX_ACR0_AC97_W;
181
182                 /* read AC-97 write register status */
183                 do {
184                         if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
185                                 break;
186                 } while (--count);
187
188                 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
189         } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
190                 address = NX_ACR1_AC97_W;
191
192                 /* read AC-97 write register status */
193                 do {
194                         if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
195                                 break;
196                 } while (--count);
197
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;
201
202                 /* read AC-97 write register status */
203                 do {
204                         if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
205                                 break;
206                 } while (--count);
207
208                 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
209                 if (ac97->num == 1)
210                         data |= SI_AC97_SECONDARY;
211         } else {
212                 address = 0;    /* keep GCC happy */
213                 count = 0;      /* return */
214         }
215
216         if (count == 0) {
217                 spin_unlock_irqrestore(&trident->reg_lock, flags);
218                 return;
219         }
220         outl(data, TRID_REG(trident, address));
221         spin_unlock_irqrestore(&trident->reg_lock, flags);
222 }
223
224 /*---------------------------------------------------------------------------
225    void snd_trident_enable_eso(struct snd_trident *trident)
226   
227    Description: This routine will enable end of loop interrupts.
228                 End of loop interrupts will occur when a running
229                 channel reaches ESO.
230                 Also enables middle of loop interrupts.
231   
232    Parameters:  trident - pointer to target device class for 4DWave.
233   
234   ---------------------------------------------------------------------------*/
235
236 static void snd_trident_enable_eso(struct snd_trident * trident)
237 {
238         unsigned int val;
239
240         val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
241         val |= ENDLP_IE;
242         val |= MIDLP_IE;
243         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
244                 val |= BANK_B_EN;
245         outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
246 }
247
248 /*---------------------------------------------------------------------------
249    void snd_trident_disable_eso(struct snd_trident *trident)
250   
251    Description: This routine will disable end of loop interrupts.
252                 End of loop interrupts will occur when a running
253                 channel reaches ESO.
254                 Also disables middle of loop interrupts.
255   
256    Parameters:  
257                 trident - pointer to target device class for 4DWave.
258   
259    returns:     TRUE if everything went ok, else FALSE.
260   
261   ---------------------------------------------------------------------------*/
262
263 static void snd_trident_disable_eso(struct snd_trident * trident)
264 {
265         unsigned int tmp;
266
267         tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
268         tmp &= ~ENDLP_IE;
269         tmp &= ~MIDLP_IE;
270         outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
271 }
272
273 /*---------------------------------------------------------------------------
274    void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
275
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.
279
280     Parameters : voice - Voice number 0 thru n.
281                  trident - pointer to target device class for 4DWave.
282
283     Return Value: None.
284
285   ---------------------------------------------------------------------------*/
286
287 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
288 {
289         unsigned int mask = 1 << (voice & 0x1f);
290         unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
291
292         outl(mask, TRID_REG(trident, reg));
293 }
294
295 EXPORT_SYMBOL(snd_trident_start_voice);
296
297 /*---------------------------------------------------------------------------
298    void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
299
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.
303
304     Parameters : voice - Voice number 0 thru n.
305                  trident - pointer to target device class for 4DWave.
306
307     Return Value: None.
308
309   ---------------------------------------------------------------------------*/
310
311 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
312 {
313         unsigned int mask = 1 << (voice & 0x1f);
314         unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
315
316         outl(mask, TRID_REG(trident, reg));
317 }
318
319 EXPORT_SYMBOL(snd_trident_stop_voice);
320
321 /*---------------------------------------------------------------------------
322     int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
323   
324     Description: Allocate hardware channel in Bank B (32-63).
325   
326     Parameters :  trident - pointer to target device class for 4DWave.
327   
328     Return Value: hardware channel - 32-63 or -1 when no channel is available
329   
330   ---------------------------------------------------------------------------*/
331
332 static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
333 {
334         int idx;
335
336         if (trident->ChanPCMcnt >= trident->ChanPCM)
337                 return -1;
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++;
342                         return idx + 32;
343                 }
344         }
345         return -1;
346 }
347
348 /*---------------------------------------------------------------------------
349     void snd_trident_free_pcm_channel(int channel)
350   
351     Description: Free hardware channel in Bank B (32-63)
352   
353     Parameters :  trident - pointer to target device class for 4DWave.
354                   channel - hardware channel number 0-63
355   
356     Return Value: none
357   
358   ---------------------------------------------------------------------------*/
359
360 static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
361 {
362         if (channel < 32 || channel > 63)
363                 return;
364         channel &= 0x1f;
365         if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
366                 trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
367                 trident->ChanPCMcnt--;
368         }
369 }
370
371 /*---------------------------------------------------------------------------
372     unsigned int snd_trident_allocate_synth_channel(void)
373   
374     Description: Allocate hardware channel in Bank A (0-31).
375   
376     Parameters :  trident - pointer to target device class for 4DWave.
377   
378     Return Value: hardware channel - 0-31 or -1 when no channel is available
379   
380   ---------------------------------------------------------------------------*/
381
382 static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
383 {
384         int idx;
385
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++;
390                         return idx;
391                 }
392         }
393         return -1;
394 }
395
396 /*---------------------------------------------------------------------------
397     void snd_trident_free_synth_channel( int channel )
398   
399     Description: Free hardware channel in Bank B (0-31).
400   
401     Parameters :  trident - pointer to target device class for 4DWave.
402                   channel - hardware channel number 0-63
403   
404     Return Value: none
405   
406   ---------------------------------------------------------------------------*/
407
408 static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
409 {
410         if (channel < 0 || channel > 31)
411                 return;
412         channel &= 0x1f;
413         if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
414                 trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
415                 trident->synth.ChanSynthCount--;
416         }
417 }
418
419 /*---------------------------------------------------------------------------
420    snd_trident_write_voice_regs
421   
422    Description: This routine will complete and write the 5 hardware channel
423                 registers to hardware.
424   
425    Parameters:  trident - pointer to target device class for 4DWave.
426                 voice - synthesizer voice structure
427                 Each register field.
428   
429   ---------------------------------------------------------------------------*/
430
431 void snd_trident_write_voice_regs(struct snd_trident * trident,
432                                   struct snd_trident_voice * voice)
433 {
434         unsigned int FmcRvolCvol;
435         unsigned int regs[5];
436
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);
444
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;
455                 break;
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;
463                 break;
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;
472                 break;
473         default:
474                 snd_BUG();
475                 return;
476         }
477
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));
484
485 #if 0
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)));
497 #endif
498 }
499
500 EXPORT_SYMBOL(snd_trident_write_voice_regs);
501
502 /*---------------------------------------------------------------------------
503    snd_trident_write_cso_reg
504   
505    Description: This routine will write the new CSO offset
506                 register to hardware.
507   
508    Parameters:  trident - pointer to target device class for 4DWave.
509                 voice - synthesizer voice structure
510                 CSO - new CSO value
511   
512   ---------------------------------------------------------------------------*/
513
514 static void snd_trident_write_cso_reg(struct snd_trident * trident,
515                                       struct snd_trident_voice * voice,
516                                       unsigned int CSO)
517 {
518         voice->CSO = CSO;
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);
522         } else {
523                 outl((voice->Delta << 24) |
524                      (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
525         }
526 }
527
528 /*---------------------------------------------------------------------------
529    snd_trident_write_eso_reg
530   
531    Description: This routine will write the new ESO offset
532                 register to hardware.
533   
534    Parameters:  trident - pointer to target device class for 4DWave.
535                 voice - synthesizer voice structure
536                 ESO - new ESO value
537   
538   ---------------------------------------------------------------------------*/
539
540 static void snd_trident_write_eso_reg(struct snd_trident * trident,
541                                       struct snd_trident_voice * voice,
542                                       unsigned int ESO)
543 {
544         voice->ESO = ESO;
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);
548         } else {
549                 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
550                      TRID_REG(trident, CH_NX_DELTA_ESO));
551         }
552 }
553
554 /*---------------------------------------------------------------------------
555    snd_trident_write_vol_reg
556   
557    Description: This routine will write the new voice volume
558                 register to hardware.
559   
560    Parameters:  trident - pointer to target device class for 4DWave.
561                 voice - synthesizer voice structure
562                 Vol - new voice volume
563   
564   ---------------------------------------------------------------------------*/
565
566 static void snd_trident_write_vol_reg(struct snd_trident * trident,
567                                       struct snd_trident_voice * voice,
568                                       unsigned int Vol)
569 {
570         voice->Vol = Vol;
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));
576                 break;
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));
581                 break;
582         }
583 }
584
585 /*---------------------------------------------------------------------------
586    snd_trident_write_pan_reg
587   
588    Description: This routine will write the new voice pan
589                 register to hardware.
590   
591    Parameters:  trident - pointer to target device class for 4DWave.
592                 voice - synthesizer voice structure
593                 Pan - new pan value
594   
595   ---------------------------------------------------------------------------*/
596
597 static void snd_trident_write_pan_reg(struct snd_trident * trident,
598                                       struct snd_trident_voice * voice,
599                                       unsigned int Pan)
600 {
601         voice->Pan = Pan;
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));
605 }
606
607 /*---------------------------------------------------------------------------
608    snd_trident_write_rvol_reg
609   
610    Description: This routine will write the new reverb volume
611                 register to hardware.
612   
613    Parameters:  trident - pointer to target device class for 4DWave.
614                 voice - synthesizer voice structure
615                 RVol - new reverb volume
616   
617   ---------------------------------------------------------------------------*/
618
619 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
620                                        struct snd_trident_voice * voice,
621                                        unsigned int RVol)
622 {
623         voice->RVol = RVol;
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));
629 }
630
631 /*---------------------------------------------------------------------------
632    snd_trident_write_cvol_reg
633   
634    Description: This routine will write the new chorus volume
635                 register to hardware.
636   
637    Parameters:  trident - pointer to target device class for 4DWave.
638                 voice - synthesizer voice structure
639                 CVol - new chorus volume
640   
641   ---------------------------------------------------------------------------*/
642
643 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
644                                        struct snd_trident_voice * voice,
645                                        unsigned int CVol)
646 {
647         voice->CVol = CVol;
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));
653 }
654
655 /*---------------------------------------------------------------------------
656    snd_trident_convert_rate
657
658    Description: This routine converts rate in HZ to hardware delta value.
659   
660    Parameters:  trident - pointer to target device class for 4DWave.
661                 rate - Real or Virtual channel number.
662   
663    Returns:     Delta value.
664   
665   ---------------------------------------------------------------------------*/
666 static unsigned int snd_trident_convert_rate(unsigned int rate)
667 {
668         unsigned int delta;
669
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
674         if (rate == 44100)
675                 delta = 0xeb3;
676         else if (rate == 8000)
677                 delta = 0x2ab;
678         else if (rate == 48000)
679                 delta = 0x1000;
680         else
681                 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
682         return delta;
683 }
684
685 /*---------------------------------------------------------------------------
686    snd_trident_convert_adc_rate
687
688    Description: This routine converts rate in HZ to hardware delta value.
689   
690    Parameters:  trident - pointer to target device class for 4DWave.
691                 rate - Real or Virtual channel number.
692   
693    Returns:     Delta value.
694   
695   ---------------------------------------------------------------------------*/
696 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
697 {
698         unsigned int delta;
699
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
704         if (rate == 44100)
705                 delta = 0x116a;
706         else if (rate == 8000)
707                 delta = 0x6000;
708         else if (rate == 48000)
709                 delta = 0x1000;
710         else
711                 delta = ((48000 << 12) / rate) & 0x0000ffff;
712         return delta;
713 }
714
715 /*---------------------------------------------------------------------------
716    snd_trident_spurious_threshold
717
718    Description: This routine converts rate in HZ to spurious threshold.
719   
720    Parameters:  trident - pointer to target device class for 4DWave.
721                 rate - Real or Virtual channel number.
722   
723    Returns:     Delta value.
724   
725   ---------------------------------------------------------------------------*/
726 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
727                                                    unsigned int period_size)
728 {
729         unsigned int res = (rate * period_size) / 48000;
730         if (res < 64)
731                 res = res / 2;
732         else
733                 res -= 32;
734         return res;
735 }
736
737 /*---------------------------------------------------------------------------
738    snd_trident_control_mode
739
740    Description: This routine returns a control mode for a PCM channel.
741   
742    Parameters:  trident - pointer to target device class for 4DWave.
743                 substream  - PCM substream
744   
745    Returns:     Control value.
746   
747   ---------------------------------------------------------------------------*/
748 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
749 {
750         unsigned int CTRL;
751         struct snd_pcm_runtime *runtime = substream->runtime;
752
753         /* set ctrl mode
754            CTRL default: 8-bit (unsigned) mono, loop mode enabled
755          */
756         CTRL = 0x00000001;
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
763         return CTRL;
764 }
765
766 /*
767  *  PCM part
768  */
769
770 /*---------------------------------------------------------------------------
771    snd_trident_ioctl
772   
773    Description: Device I/O control handler for playback/capture parameters.
774   
775    Parameters:   substream  - PCM substream class
776                 cmd     - what ioctl message to process
777                 arg     - additional message infoarg     
778   
779    Returns:     Error status
780   
781   ---------------------------------------------------------------------------*/
782
783 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
784                              unsigned int cmd,
785                              void *arg)
786 {
787         /* FIXME: it seems that with small periods the behaviour of
788                   trident hardware is unpredictable and interrupt generator
789                   is broken */
790         return snd_pcm_lib_ioctl(substream, cmd, arg);
791 }
792
793 /*---------------------------------------------------------------------------
794    snd_trident_allocate_pcm_mem
795   
796    Description: Allocate PCM ring buffer for given substream
797   
798    Parameters:  substream  - PCM substream class
799                 hw_params  - hardware parameters
800   
801    Returns:     Error status
802   
803   ---------------------------------------------------------------------------*/
804
805 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
806                                         struct snd_pcm_hw_params *hw_params)
807 {
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;
811         int err;
812
813         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
814                 return err;
815         if (trident->tlb.entries) {
816                 if (err > 0) { /* change */
817                         if (voice->memblk)
818                                 snd_trident_free_pages(trident, voice->memblk);
819                         voice->memblk = snd_trident_alloc_pages(trident, substream);
820                         if (voice->memblk == NULL)
821                                 return -ENOMEM;
822                 }
823         }
824         return 0;
825 }
826
827 /*---------------------------------------------------------------------------
828    snd_trident_allocate_evoice
829   
830    Description: Allocate extra voice as interrupt generator
831   
832    Parameters:  substream  - PCM substream class
833                 hw_params  - hardware parameters
834   
835    Returns:     Error status
836   
837   ---------------------------------------------------------------------------*/
838
839 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
840                                        struct snd_pcm_hw_params *hw_params)
841 {
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;
846
847         /* voice management */
848
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);
852                         if (evoice == NULL)
853                                 return -ENOMEM;
854                         voice->extra = evoice;
855                         evoice->substream = substream;
856                 }
857         } else {
858                 if (evoice != NULL) {
859                         snd_trident_free_voice(trident, evoice);
860                         voice->extra = evoice = NULL;
861                 }
862         }
863
864         return 0;
865 }
866
867 /*---------------------------------------------------------------------------
868    snd_trident_hw_params
869   
870    Description: Set the hardware parameters for the playback device.
871   
872    Parameters:  substream  - PCM substream class
873                 hw_params  - hardware parameters
874   
875    Returns:     Error status
876   
877   ---------------------------------------------------------------------------*/
878
879 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
880                                  struct snd_pcm_hw_params *hw_params)
881 {
882         int err;
883
884         err = snd_trident_allocate_pcm_mem(substream, hw_params);
885         if (err >= 0)
886                 err = snd_trident_allocate_evoice(substream, hw_params);
887         return err;
888 }
889
890 /*---------------------------------------------------------------------------
891    snd_trident_playback_hw_free
892   
893    Description: Release the hardware resources for the playback device.
894   
895    Parameters:  substream  - PCM substream class
896   
897    Returns:     Error status
898   
899   ---------------------------------------------------------------------------*/
900
901 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
902 {
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;
907
908         if (trident->tlb.entries) {
909                 if (voice && voice->memblk) {
910                         snd_trident_free_pages(trident, voice->memblk);
911                         voice->memblk = NULL;
912                 }
913         }
914         snd_pcm_lib_free_pages(substream);
915         if (evoice != NULL) {
916                 snd_trident_free_voice(trident, evoice);
917                 voice->extra = NULL;
918         }
919         return 0;
920 }
921
922 /*---------------------------------------------------------------------------
923    snd_trident_playback_prepare
924   
925    Description: Prepare playback device for playback.
926   
927    Parameters:  substream  - PCM substream class
928   
929    Returns:     Error status
930   
931   ---------------------------------------------------------------------------*/
932
933 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
934 {
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];
940
941         spin_lock_irq(&trident->reg_lock);      
942
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);
946
947         /* set Loop Begin Address */
948         if (voice->memblk)
949                 voice->LBA = voice->memblk->offset;
950         else
951                 voice->LBA = runtime->dma_addr;
952  
953         voice->CSO = 0;
954         voice->ESO = runtime->buffer_size - 1;  /* in samples */
955         voice->CTRL = snd_trident_control_mode(substream);
956         voice->FMC = 3;
957         voice->GVSel = 1;
958         voice->EC = 0;
959         voice->Alpha = 0;
960         voice->FMS = 0;
961         voice->Vol = mix->vol;
962         voice->RVol = mix->rvol;
963         voice->CVol = mix->cvol;
964         voice->Pan = mix->pan;
965         voice->Attribute = 0;
966 #if 0
967         voice->Attribute = (1<<(30-16))|(2<<(26-16))|
968                            (0<<(24-16))|(0x1f<<(19-16));
969 #else
970         voice->Attribute = 0;
971 #endif
972
973         snd_trident_write_voice_regs(trident, voice);
974
975         if (evoice != NULL) {
976                 evoice->Delta = voice->Delta;
977                 evoice->spurious_threshold = voice->spurious_threshold;
978                 evoice->LBA = voice->LBA;
979                 evoice->CSO = 0;
980                 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
981                 evoice->CTRL = voice->CTRL;
982                 evoice->FMC = 3;
983                 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
984                 evoice->EC = 0;
985                 evoice->Alpha = 0;
986                 evoice->FMS = 0;
987                 evoice->Vol = 0x3ff;                    /* mute */
988                 evoice->RVol = evoice->CVol = 0x7f;     /* mute */
989                 evoice->Pan = 0x7f;                     /* mute */
990 #if 0
991                 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
992                                     (0<<(24-16))|(0x1f<<(19-16));
993 #else
994                 evoice->Attribute = 0;
995 #endif
996                 snd_trident_write_voice_regs(trident, evoice);
997                 evoice->isync2 = 1;
998                 evoice->isync_mark = runtime->period_size;
999                 evoice->ESO = (runtime->period_size * 2) - 1;
1000         }
1001
1002         spin_unlock_irq(&trident->reg_lock);
1003
1004         return 0;
1005 }
1006
1007 /*---------------------------------------------------------------------------
1008    snd_trident_capture_hw_params
1009   
1010    Description: Set the hardware parameters for the capture device.
1011   
1012    Parameters:  substream  - PCM substream class
1013                 hw_params  - hardware parameters
1014   
1015    Returns:     Error status
1016   
1017   ---------------------------------------------------------------------------*/
1018
1019 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1020                                          struct snd_pcm_hw_params *hw_params)
1021 {
1022         return snd_trident_allocate_pcm_mem(substream, hw_params);
1023 }
1024
1025 /*---------------------------------------------------------------------------
1026    snd_trident_capture_prepare
1027   
1028    Description: Prepare capture device for playback.
1029   
1030    Parameters:  substream  - PCM substream class
1031   
1032    Returns:     Error status
1033   
1034   ---------------------------------------------------------------------------*/
1035
1036 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1037 {
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;
1042
1043         spin_lock_irq(&trident->reg_lock);
1044
1045         // Initialize the channel and set channel Mode
1046         outb(0, TRID_REG(trident, LEGACY_DMAR15));
1047
1048         // Set DMA channel operation mode register
1049         outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1050
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));
1054         if (voice->memblk)
1055                 voice->LBA = voice->memblk->offset;
1056
1057         // set ESO
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));
1061         ESO_bytes++;
1062
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));
1066
1067         // Set channel interrupt blk length
1068         if (snd_pcm_format_width(runtime->format) == 16) {
1069                 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1070         } else {
1071                 val = (unsigned short) (ESO_bytes - 1);
1072         }
1073
1074         outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1075
1076         // Right now, set format and start to run captureing, 
1077         // continuous run loop enable.
1078         trident->bDMAStart = 0x19;      // 0001 1001b
1079
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;
1086
1087         // Prepare capture intr channel
1088
1089         voice->Delta = snd_trident_convert_rate(runtime->rate);
1090         voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1091         voice->isync = 1;
1092         voice->isync_mark = runtime->period_size;
1093         voice->isync_max = runtime->buffer_size;
1094
1095         // Set voice parameters
1096         voice->CSO = 0;
1097         voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1098         voice->CTRL = snd_trident_control_mode(substream);
1099         voice->FMC = 3;
1100         voice->RVol = 0x7f;
1101         voice->CVol = 0x7f;
1102         voice->GVSel = 1;
1103         voice->Pan = 0x7f;              /* mute */
1104         voice->Vol = 0x3ff;             /* mute */
1105         voice->EC = 0;
1106         voice->Alpha = 0;
1107         voice->FMS = 0;
1108         voice->Attribute = 0;
1109
1110         snd_trident_write_voice_regs(trident, voice);
1111
1112         spin_unlock_irq(&trident->reg_lock);
1113         return 0;
1114 }
1115
1116 /*---------------------------------------------------------------------------
1117    snd_trident_si7018_capture_hw_params
1118   
1119    Description: Set the hardware parameters for the capture device.
1120   
1121    Parameters:  substream  - PCM substream class
1122                 hw_params  - hardware parameters
1123   
1124    Returns:     Error status
1125   
1126   ---------------------------------------------------------------------------*/
1127
1128 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1129                                                 struct snd_pcm_hw_params *hw_params)
1130 {
1131         int err;
1132
1133         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1134                 return err;
1135
1136         return snd_trident_allocate_evoice(substream, hw_params);
1137 }
1138
1139 /*---------------------------------------------------------------------------
1140    snd_trident_si7018_capture_hw_free
1141   
1142    Description: Release the hardware resources for the capture device.
1143   
1144    Parameters:  substream  - PCM substream class
1145   
1146    Returns:     Error status
1147   
1148   ---------------------------------------------------------------------------*/
1149
1150 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1151 {
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;
1156
1157         snd_pcm_lib_free_pages(substream);
1158         if (evoice != NULL) {
1159                 snd_trident_free_voice(trident, evoice);
1160                 voice->extra = NULL;
1161         }
1162         return 0;
1163 }
1164
1165 /*---------------------------------------------------------------------------
1166    snd_trident_si7018_capture_prepare
1167   
1168    Description: Prepare capture device for playback.
1169   
1170    Parameters:  substream  - PCM substream class
1171   
1172    Returns:     Error status
1173   
1174   ---------------------------------------------------------------------------*/
1175
1176 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1177 {
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;
1182
1183         spin_lock_irq(&trident->reg_lock);
1184
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);
1188
1189         // Set voice parameters
1190         voice->CSO = 0;
1191         voice->ESO = runtime->buffer_size - 1;          /* in samples */
1192         voice->CTRL = snd_trident_control_mode(substream);
1193         voice->FMC = 0;
1194         voice->RVol = 0;
1195         voice->CVol = 0;
1196         voice->GVSel = 1;
1197         voice->Pan = T4D_DEFAULT_PCM_PAN;
1198         voice->Vol = 0;
1199         voice->EC = 0;
1200         voice->Alpha = 0;
1201         voice->FMS = 0;
1202
1203         voice->Attribute = (2 << (30-16)) |
1204                            (2 << (26-16)) |
1205                            (2 << (24-16)) |
1206                            (1 << (23-16));
1207
1208         snd_trident_write_voice_regs(trident, voice);
1209
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;
1214                 evoice->CSO = 0;
1215                 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1216                 evoice->CTRL = voice->CTRL;
1217                 evoice->FMC = 3;
1218                 evoice->GVSel = 0;
1219                 evoice->EC = 0;
1220                 evoice->Alpha = 0;
1221                 evoice->FMS = 0;
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);
1227                 evoice->isync2 = 1;
1228                 evoice->isync_mark = runtime->period_size;
1229                 evoice->ESO = (runtime->period_size * 2) - 1;
1230         }
1231         
1232         spin_unlock_irq(&trident->reg_lock);
1233         return 0;
1234 }
1235
1236 /*---------------------------------------------------------------------------
1237    snd_trident_foldback_prepare
1238   
1239    Description: Prepare foldback capture device for playback.
1240   
1241    Parameters:  substream  - PCM substream class
1242   
1243    Returns:     Error status
1244   
1245   ---------------------------------------------------------------------------*/
1246
1247 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1248 {
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;
1253
1254         spin_lock_irq(&trident->reg_lock);
1255
1256         /* Set channel buffer Address */
1257         if (voice->memblk)
1258                 voice->LBA = voice->memblk->offset;
1259         else
1260                 voice->LBA = runtime->dma_addr;
1261
1262         /* set target ESO for channel */
1263         voice->ESO = runtime->buffer_size - 1;  /* in samples */
1264
1265         /* set sample rate */
1266         voice->Delta = 0x1000;
1267         voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1268
1269         voice->CSO = 0;
1270         voice->CTRL = snd_trident_control_mode(substream);
1271         voice->FMC = 3;
1272         voice->RVol = 0x7f;
1273         voice->CVol = 0x7f;
1274         voice->GVSel = 1;
1275         voice->Pan = 0x7f;      /* mute */
1276         voice->Vol = 0x3ff;     /* mute */
1277         voice->EC = 0;
1278         voice->Alpha = 0;
1279         voice->FMS = 0;
1280         voice->Attribute = 0;
1281
1282         /* set up capture channel */
1283         outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1284
1285         snd_trident_write_voice_regs(trident, voice);
1286
1287         if (evoice != NULL) {
1288                 evoice->Delta = voice->Delta;
1289                 evoice->spurious_threshold = voice->spurious_threshold;
1290                 evoice->LBA = voice->LBA;
1291                 evoice->CSO = 0;
1292                 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1293                 evoice->CTRL = voice->CTRL;
1294                 evoice->FMC = 3;
1295                 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1296                 evoice->EC = 0;
1297                 evoice->Alpha = 0;
1298                 evoice->FMS = 0;
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);
1304                 evoice->isync2 = 1;
1305                 evoice->isync_mark = runtime->period_size;
1306                 evoice->ESO = (runtime->period_size * 2) - 1;
1307         }
1308
1309         spin_unlock_irq(&trident->reg_lock);
1310         return 0;
1311 }
1312
1313 /*---------------------------------------------------------------------------
1314    snd_trident_spdif_hw_params
1315   
1316    Description: Set the hardware parameters for the spdif device.
1317   
1318    Parameters:  substream  - PCM substream class
1319                 hw_params  - hardware parameters
1320   
1321    Returns:     Error status
1322   
1323   ---------------------------------------------------------------------------*/
1324
1325 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1326                                        struct snd_pcm_hw_params *hw_params)
1327 {
1328         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1329         unsigned int old_bits = 0, change = 0;
1330         int err;
1331
1332         err = snd_trident_allocate_pcm_mem(substream, hw_params);
1333         if (err < 0)
1334                 return err;
1335
1336         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1337                 err = snd_trident_allocate_evoice(substream, hw_params);
1338                 if (err < 0)
1339                         return err;
1340         }
1341
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;
1347         else
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);
1355         }
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);
1362         }
1363         else {
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);
1369         }
1370         change = old_bits != trident->spdif_pcm_bits;
1371         spin_unlock_irq(&trident->reg_lock);
1372
1373         if (change)
1374                 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1375
1376         return 0;
1377 }
1378
1379 /*---------------------------------------------------------------------------
1380    snd_trident_spdif_prepare
1381   
1382    Description: Prepare SPDIF device for playback.
1383   
1384    Parameters:  substream  - PCM substream class
1385   
1386    Returns:     Error status
1387   
1388   ---------------------------------------------------------------------------*/
1389
1390 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1391 {
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;
1398         unsigned int temp;
1399
1400         spin_lock_irq(&trident->reg_lock);
1401
1402         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1403
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);
1407
1408                 /* set Loop Back Address */
1409                 LBAO = runtime->dma_addr;
1410                 if (voice->memblk)
1411                         voice->LBA = voice->memblk->offset;
1412                 else
1413                         voice->LBA = LBAO;
1414
1415                 voice->isync = 1;
1416                 voice->isync3 = 1;
1417                 voice->isync_mark = runtime->period_size;
1418                 voice->isync_max = runtime->buffer_size;
1419
1420                 /* set target ESO for channel */
1421                 RESO = runtime->buffer_size - 1;
1422                 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1423
1424                 /* set ctrl mode */
1425                 voice->CTRL = snd_trident_control_mode(substream);
1426
1427                 voice->FMC = 3;
1428                 voice->RVol = 0x7f;
1429                 voice->CVol = 0x7f;
1430                 voice->GVSel = 1;
1431                 voice->Pan = 0x7f;
1432                 voice->Vol = 0x3ff;
1433                 voice->EC = 0;
1434                 voice->CSO = 0;
1435                 voice->Alpha = 0;
1436                 voice->FMS = 0;
1437                 voice->Attribute = 0;
1438
1439                 /* prepare surrogate IRQ channel */
1440                 snd_trident_write_voice_regs(trident, voice);
1441
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));
1447
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));
1451
1452         } else {        /* SiS */
1453         
1454                 /* set delta (rate) value */
1455                 voice->Delta = 0x800;
1456                 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1457
1458                 /* set Loop Begin Address */
1459                 if (voice->memblk)
1460                         voice->LBA = voice->memblk->offset;
1461                 else
1462                         voice->LBA = runtime->dma_addr;
1463
1464                 voice->CSO = 0;
1465                 voice->ESO = runtime->buffer_size - 1;  /* in samples */
1466                 voice->CTRL = snd_trident_control_mode(substream);
1467                 voice->FMC = 3;
1468                 voice->GVSel = 1;
1469                 voice->EC = 0;
1470                 voice->Alpha = 0;
1471                 voice->FMS = 0;
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));
1478
1479                 snd_trident_write_voice_regs(trident, voice);
1480
1481                 if (evoice != NULL) {
1482                         evoice->Delta = voice->Delta;
1483                         evoice->spurious_threshold = voice->spurious_threshold;
1484                         evoice->LBA = voice->LBA;
1485                         evoice->CSO = 0;
1486                         evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1487                         evoice->CTRL = voice->CTRL;
1488                         evoice->FMC = 3;
1489                         evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1490                         evoice->EC = 0;
1491                         evoice->Alpha = 0;
1492                         evoice->FMS = 0;
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);
1498                         evoice->isync2 = 1;
1499                         evoice->isync_mark = runtime->period_size;
1500                         evoice->ESO = (runtime->period_size * 2) - 1;
1501                 }
1502
1503                 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1504                 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1505                 temp &= ~(1<<19);
1506                 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1507                 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1508                 temp |= SPDIF_EN;
1509                 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1510         }
1511
1512         spin_unlock_irq(&trident->reg_lock);
1513
1514         return 0;
1515 }
1516
1517 /*---------------------------------------------------------------------------
1518    snd_trident_trigger
1519   
1520    Description: Start/stop devices
1521   
1522    Parameters:  substream  - PCM substream class
1523                 cmd     - trigger command (STOP, GO)
1524   
1525    Returns:     Error status
1526   
1527   ---------------------------------------------------------------------------*/
1528
1529 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1530                                int cmd)
1531                                     
1532 {
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;
1538
1539         switch (cmd) {
1540         case SNDRV_PCM_TRIGGER_START:
1541         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1542         case SNDRV_PCM_TRIGGER_RESUME:
1543                 go = 1;
1544                 break;
1545         case SNDRV_PCM_TRIGGER_STOP:
1546         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1547         case SNDRV_PCM_TRIGGER_SUSPEND:
1548                 go = 0;
1549                 break;
1550         default:
1551                 return -EINVAL;
1552         }
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);
1563                         } else {
1564                                 what |= 1 << (evoice->number & 0x1f);
1565                                 whati |= 1 << (evoice->number & 0x1f);
1566                                 if (go)
1567                                         evoice->stimer = val;
1568                         }
1569                         if (go) {
1570                                 voice->running = 1;
1571                                 voice->stimer = val;
1572                         } else {
1573                                 voice->running = 0;
1574                         }
1575                         snd_pcm_trigger_done(s, substream);
1576                         if (voice->capture)
1577                                 capture_flag = 1;
1578                         if (voice->spdif)
1579                                 spdif_flag = 1;
1580                 }
1581         }
1582         if (spdif_flag) {
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;
1586                         if (!go)
1587                                 val &= ~(0x28);
1588                         outb(val, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1589                 } else {
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));
1593                 }
1594         }
1595         if (!go)
1596                 outl(what, TRID_REG(trident, T4D_STOP_B));
1597         val = inl(TRID_REG(trident, T4D_AINTEN_B));
1598         if (go) {
1599                 val |= whati;
1600         } else {
1601                 val &= ~whati;
1602         }
1603         outl(val, TRID_REG(trident, T4D_AINTEN_B));
1604         if (go) {
1605                 outl(what, TRID_REG(trident, T4D_START_B));
1606
1607                 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1608                         outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1609         } else {
1610                 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1611                         outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1612         }
1613         spin_unlock(&trident->reg_lock);
1614         return 0;
1615 }
1616
1617 /*---------------------------------------------------------------------------
1618    snd_trident_playback_pointer
1619   
1620    Description: This routine return the playback position
1621                 
1622    Parameters:  substream  - PCM substream class
1623
1624    Returns:     position of buffer
1625   
1626   ---------------------------------------------------------------------------*/
1627
1628 static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1629 {
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;
1633         unsigned int cso;
1634
1635         if (!voice->running)
1636                 return 0;
1637
1638         spin_lock(&trident->reg_lock);
1639
1640         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
1641
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;
1646         }
1647
1648         spin_unlock(&trident->reg_lock);
1649
1650         if (cso >= runtime->buffer_size)
1651                 cso = 0;
1652
1653         return cso;
1654 }
1655
1656 /*---------------------------------------------------------------------------
1657    snd_trident_capture_pointer
1658   
1659    Description: This routine return the capture position
1660                 
1661    Parameters:   pcm1    - PCM device class
1662
1663    Returns:     position of buffer
1664   
1665   ---------------------------------------------------------------------------*/
1666
1667 static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1668 {
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;
1673
1674         if (!voice->running)
1675                 return 0;
1676
1677         result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
1678         if (runtime->channels > 1)
1679                 result >>= 1;
1680         if (result > 0)
1681                 result = runtime->buffer_size - result;
1682
1683         return result;
1684 }
1685
1686 /*---------------------------------------------------------------------------
1687    snd_trident_spdif_pointer
1688   
1689    Description: This routine return the SPDIF playback position
1690                 
1691    Parameters:  substream  - PCM substream class
1692
1693    Returns:     position of buffer
1694   
1695   ---------------------------------------------------------------------------*/
1696
1697 static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1698 {
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;
1703
1704         if (!voice->running)
1705                 return 0;
1706
1707         result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
1708
1709         return result;
1710 }
1711
1712 /*
1713  *  Playback support device description
1714  */
1715
1716 static const struct snd_pcm_hardware snd_trident_playback =
1717 {
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,
1725         .rate_min =             4000,
1726         .rate_max =             48000,
1727         .channels_min =         1,
1728         .channels_max =         2,
1729         .buffer_bytes_max =     (256*1024),
1730         .period_bytes_min =     64,
1731         .period_bytes_max =     (256*1024),
1732         .periods_min =          1,
1733         .periods_max =          1024,
1734         .fifo_size =            0,
1735 };
1736
1737 /*
1738  *  Capture support device description
1739  */
1740
1741 static const struct snd_pcm_hardware snd_trident_capture =
1742 {
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,
1750         .rate_min =             4000,
1751         .rate_max =             48000,
1752         .channels_min =         1,
1753         .channels_max =         2,
1754         .buffer_bytes_max =     (128*1024),
1755         .period_bytes_min =     64,
1756         .period_bytes_max =     (128*1024),
1757         .periods_min =          1,
1758         .periods_max =          1024,
1759         .fifo_size =            0,
1760 };
1761
1762 /*
1763  *  Foldback capture support device description
1764  */
1765
1766 static const struct snd_pcm_hardware snd_trident_foldback =
1767 {
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,
1774         .rate_min =             48000,
1775         .rate_max =             48000,
1776         .channels_min =         2,
1777         .channels_max =         2,
1778         .buffer_bytes_max =     (128*1024),
1779         .period_bytes_min =     64,
1780         .period_bytes_max =     (128*1024),
1781         .periods_min =          1,
1782         .periods_max =          1024,
1783         .fifo_size =            0,
1784 };
1785
1786 /*
1787  *  SPDIF playback support device description
1788  */
1789
1790 static const struct snd_pcm_hardware snd_trident_spdif =
1791 {
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),
1799         .rate_min =             32000,
1800         .rate_max =             48000,
1801         .channels_min =         2,
1802         .channels_max =         2,
1803         .buffer_bytes_max =     (128*1024),
1804         .period_bytes_min =     64,
1805         .period_bytes_max =     (128*1024),
1806         .periods_min =          1,
1807         .periods_max =          1024,
1808         .fifo_size =            0,
1809 };
1810
1811 static const struct snd_pcm_hardware snd_trident_spdif_7018 =
1812 {
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,
1819         .rate_min =             48000,
1820         .rate_max =             48000,
1821         .channels_min =         2,
1822         .channels_max =         2,
1823         .buffer_bytes_max =     (128*1024),
1824         .period_bytes_min =     64,
1825         .period_bytes_max =     (128*1024),
1826         .periods_min =          1,
1827         .periods_max =          1024,
1828         .fifo_size =            0,
1829 };
1830
1831 static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1832 {
1833         struct snd_trident_voice *voice = runtime->private_data;
1834         struct snd_trident *trident;
1835
1836         if (voice) {
1837                 trident = voice->trident;
1838                 snd_trident_free_voice(trident, voice);
1839         }
1840 }
1841
1842 static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1843 {
1844         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1845         struct snd_pcm_runtime *runtime = substream->runtime;
1846         struct snd_trident_voice *voice;
1847
1848         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1849         if (voice == NULL)
1850                 return -EAGAIN;
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);
1858         return 0;
1859 }
1860
1861 /*---------------------------------------------------------------------------
1862    snd_trident_playback_close
1863   
1864    Description: This routine will close the 4DWave playback device. For now 
1865                 we will simply free the dma transfer buffer.
1866                 
1867    Parameters:  substream  - PCM substream class
1868
1869   ---------------------------------------------------------------------------*/
1870 static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1871 {
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;
1875
1876         snd_trident_pcm_mixer_free(trident, voice, substream);
1877         return 0;
1878 }
1879
1880 /*---------------------------------------------------------------------------
1881    snd_trident_spdif_open
1882   
1883    Description: This routine will open the 4DWave SPDIF device.
1884
1885    Parameters:  substream  - PCM substream class
1886
1887    Returns:     status  - success or failure flag
1888   
1889   ---------------------------------------------------------------------------*/
1890
1891 static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1892 {
1893         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1894         struct snd_trident_voice *voice;
1895         struct snd_pcm_runtime *runtime = substream->runtime;
1896         
1897         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1898         if (voice == NULL)
1899                 return -EAGAIN;
1900         voice->spdif = 1;
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);
1905
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;
1910         } else {
1911                 runtime->hw = snd_trident_spdif_7018;
1912         }
1913
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);
1917
1918         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1919         return 0;
1920 }
1921
1922
1923 /*---------------------------------------------------------------------------
1924    snd_trident_spdif_close
1925   
1926    Description: This routine will close the 4DWave SPDIF device.
1927                 
1928    Parameters:  substream  - PCM substream class
1929
1930   ---------------------------------------------------------------------------*/
1931
1932 static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1933 {
1934         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1935         unsigned int temp;
1936
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));
1942         } else {
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) {
1946                         temp |= SPDIF_EN;
1947                 } else {
1948                         temp &= ~SPDIF_EN;
1949                 }
1950                 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1951         }
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);
1956         return 0;
1957 }
1958
1959 /*---------------------------------------------------------------------------
1960    snd_trident_capture_open
1961   
1962    Description: This routine will open the 4DWave capture device.
1963
1964    Parameters:  substream  - PCM substream class
1965
1966    Returns:     status  - success or failure flag
1967
1968   ---------------------------------------------------------------------------*/
1969
1970 static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1971 {
1972         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1973         struct snd_trident_voice *voice;
1974         struct snd_pcm_runtime *runtime = substream->runtime;
1975
1976         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1977         if (voice == NULL)
1978                 return -EAGAIN;
1979         voice->capture = 1;
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);
1986         return 0;
1987 }
1988
1989 /*---------------------------------------------------------------------------
1990    snd_trident_capture_close
1991   
1992    Description: This routine will close the 4DWave capture device. For now 
1993                 we will simply free the dma transfer buffer.
1994                 
1995    Parameters:  substream  - PCM substream class
1996
1997   ---------------------------------------------------------------------------*/
1998 static int snd_trident_capture_close(struct snd_pcm_substream *substream)
1999 {
2000         return 0;
2001 }
2002
2003 /*---------------------------------------------------------------------------
2004    snd_trident_foldback_open
2005   
2006    Description: This routine will open the 4DWave foldback capture device.
2007
2008    Parameters:  substream  - PCM substream class
2009
2010    Returns:     status  - success or failure flag
2011
2012   ---------------------------------------------------------------------------*/
2013
2014 static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
2015 {
2016         struct snd_trident *trident = snd_pcm_substream_chip(substream);
2017         struct snd_trident_voice *voice;
2018         struct snd_pcm_runtime *runtime = substream->runtime;
2019
2020         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
2021         if (voice == NULL)
2022                 return -EAGAIN;
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);
2029         return 0;
2030 }
2031
2032 /*---------------------------------------------------------------------------
2033    snd_trident_foldback_close
2034   
2035    Description: This routine will close the 4DWave foldback capture device. 
2036                 For now we will simply free the dma transfer buffer.
2037                 
2038    Parameters:  substream  - PCM substream class
2039
2040   ---------------------------------------------------------------------------*/
2041 static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
2042 {
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;
2047         
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);
2052         return 0;
2053 }
2054
2055 /*---------------------------------------------------------------------------
2056    PCM operations
2057   ---------------------------------------------------------------------------*/
2058
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,
2068 };
2069
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 };
2080
2081 static const struct snd_pcm_ops snd_trident_capture_ops = {
2082         .open =         snd_trident_capture_open,
2083         .close =        snd_trident_capture_close,
2084         .ioctl =        snd_trident_ioctl,
2085         .hw_params =    snd_trident_capture_hw_params,
2086         .hw_free =      snd_trident_hw_free,
2087         .prepare =      snd_trident_capture_prepare,
2088         .trigger =      snd_trident_trigger,
2089         .pointer =      snd_trident_capture_pointer,
2090 };
2091
2092 static const struct snd_pcm_ops snd_trident_si7018_capture_ops = {
2093         .open =         snd_trident_capture_open,
2094         .close =        snd_trident_capture_close,
2095         .ioctl =        snd_trident_ioctl,
2096         .hw_params =    snd_trident_si7018_capture_hw_params,
2097         .hw_free =      snd_trident_si7018_capture_hw_free,
2098         .prepare =      snd_trident_si7018_capture_prepare,
2099         .trigger =      snd_trident_trigger,
2100         .pointer =      snd_trident_playback_pointer,
2101 };
2102
2103 static const struct snd_pcm_ops snd_trident_foldback_ops = {
2104         .open =         snd_trident_foldback_open,
2105         .close =        snd_trident_foldback_close,
2106         .ioctl =        snd_trident_ioctl,
2107         .hw_params =    snd_trident_hw_params,
2108         .hw_free =      snd_trident_hw_free,
2109         .prepare =      snd_trident_foldback_prepare,
2110         .trigger =      snd_trident_trigger,
2111         .pointer =      snd_trident_playback_pointer,
2112 };
2113
2114 static const struct snd_pcm_ops snd_trident_nx_foldback_ops = {
2115         .open =         snd_trident_foldback_open,
2116         .close =        snd_trident_foldback_close,
2117         .ioctl =        snd_trident_ioctl,
2118         .hw_params =    snd_trident_hw_params,
2119         .hw_free =      snd_trident_hw_free,
2120         .prepare =      snd_trident_foldback_prepare,
2121         .trigger =      snd_trident_trigger,
2122         .pointer =      snd_trident_playback_pointer,
2123 };
2124
2125 static const struct snd_pcm_ops snd_trident_spdif_ops = {
2126         .open =         snd_trident_spdif_open,
2127         .close =        snd_trident_spdif_close,
2128         .ioctl =        snd_trident_ioctl,
2129         .hw_params =    snd_trident_spdif_hw_params,
2130         .hw_free =      snd_trident_hw_free,
2131         .prepare =      snd_trident_spdif_prepare,
2132         .trigger =      snd_trident_trigger,
2133         .pointer =      snd_trident_spdif_pointer,
2134 };
2135
2136 static const struct snd_pcm_ops snd_trident_spdif_7018_ops = {
2137         .open =         snd_trident_spdif_open,
2138         .close =        snd_trident_spdif_close,
2139         .ioctl =        snd_trident_ioctl,
2140         .hw_params =    snd_trident_spdif_hw_params,
2141         .hw_free =      snd_trident_hw_free,
2142         .prepare =      snd_trident_spdif_prepare,
2143         .trigger =      snd_trident_trigger,
2144         .pointer =      snd_trident_playback_pointer,
2145 };
2146
2147 /*---------------------------------------------------------------------------
2148    snd_trident_pcm
2149   
2150    Description: This routine registers the 4DWave device for PCM support.
2151                 
2152    Parameters:  trident - pointer to target device class for 4DWave.
2153
2154    Returns:     None
2155   
2156   ---------------------------------------------------------------------------*/
2157
2158 int snd_trident_pcm(struct snd_trident *trident, int device)
2159 {
2160         struct snd_pcm *pcm;
2161         int err;
2162
2163         if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
2164                 return err;
2165
2166         pcm->private_data = trident;
2167
2168         if (trident->tlb.entries) {
2169                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
2170         } else {
2171                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
2172         }
2173         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2174                         trident->device != TRIDENT_DEVICE_ID_SI7018 ?
2175                         &snd_trident_capture_ops :
2176                         &snd_trident_si7018_capture_ops);
2177
2178         pcm->info_flags = 0;
2179         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
2180         strcpy(pcm->name, "Trident 4DWave");
2181         trident->pcm = pcm;
2182
2183         if (trident->tlb.entries) {
2184                 struct snd_pcm_substream *substream;
2185                 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
2186                         snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
2187                                                       snd_dma_pci_data(trident->pci),
2188                                                       64*1024, 128*1024);
2189                 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2190                                               SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
2191                                               64*1024, 128*1024);
2192         } else {
2193                 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2194                                                       snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2195         }
2196
2197         return 0;
2198 }
2199
2200 /*---------------------------------------------------------------------------
2201    snd_trident_foldback_pcm
2202   
2203    Description: This routine registers the 4DWave device for foldback PCM support.
2204                 
2205    Parameters:  trident - pointer to target device class for 4DWave.
2206
2207    Returns:     None
2208   
2209   ---------------------------------------------------------------------------*/
2210
2211 int snd_trident_foldback_pcm(struct snd_trident *trident, int device)
2212 {
2213         struct snd_pcm *foldback;
2214         int err;
2215         int num_chan = 3;
2216         struct snd_pcm_substream *substream;
2217
2218         if (trident->device == TRIDENT_DEVICE_ID_NX)
2219                 num_chan = 4;
2220         if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
2221                 return err;
2222
2223         foldback->private_data = trident;
2224         if (trident->tlb.entries)
2225                 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
2226         else
2227                 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
2228         foldback->info_flags = 0;
2229         strcpy(foldback->name, "Trident 4DWave");
2230         substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2231         strcpy(substream->name, "Front Mixer");
2232         substream = substream->next;
2233         strcpy(substream->name, "Reverb Mixer");
2234         substream = substream->next;
2235         strcpy(substream->name, "Chorus Mixer");
2236         if (num_chan == 4) {
2237                 substream = substream->next;
2238                 strcpy(substream->name, "Second AC'97 ADC");
2239         }
2240         trident->foldback = foldback;
2241
2242         if (trident->tlb.entries)
2243                 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
2244                                                       snd_dma_pci_data(trident->pci), 0, 128*1024);
2245         else
2246                 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
2247                                                       snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2248
2249         return 0;
2250 }
2251
2252 /*---------------------------------------------------------------------------
2253    snd_trident_spdif
2254   
2255    Description: This routine registers the 4DWave-NX device for SPDIF support.
2256                 
2257    Parameters:  trident - pointer to target device class for 4DWave-NX.
2258
2259    Returns:     None
2260   
2261   ---------------------------------------------------------------------------*/
2262
2263 int snd_trident_spdif_pcm(struct snd_trident *trident, int device)
2264 {
2265         struct snd_pcm *spdif;
2266         int err;
2267
2268         if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
2269                 return err;
2270
2271         spdif->private_data = trident;
2272         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2273                 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
2274         } else {
2275                 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
2276         }
2277         spdif->info_flags = 0;
2278         strcpy(spdif->name, "Trident 4DWave IEC958");
2279         trident->spdif = spdif;
2280
2281         snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2282
2283         return 0;
2284 }
2285
2286 /*
2287  *  Mixer part
2288  */
2289
2290
2291 /*---------------------------------------------------------------------------
2292     snd_trident_spdif_control
2293
2294     Description: enable/disable S/PDIF out from ac97 mixer
2295   ---------------------------------------------------------------------------*/
2296
2297 #define snd_trident_spdif_control_info  snd_ctl_boolean_mono_info
2298
2299 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2300                                          struct snd_ctl_elem_value *ucontrol)
2301 {
2302         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2303         unsigned char val;
2304
2305         spin_lock_irq(&trident->reg_lock);
2306         val = trident->spdif_ctrl;
2307         ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2308         spin_unlock_irq(&trident->reg_lock);
2309         return 0;
2310 }
2311
2312 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2313                                          struct snd_ctl_elem_value *ucontrol)
2314 {
2315         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2316         unsigned char val;
2317         int change;
2318
2319         val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2320         spin_lock_irq(&trident->reg_lock);
2321         /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2322         change = trident->spdif_ctrl != val;
2323         trident->spdif_ctrl = val;
2324         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2325                 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2326                         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2327                         outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2328                 }
2329         } else {
2330                 if (trident->spdif == NULL) {
2331                         unsigned int temp;
2332                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2333                         temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2334                         if (val)
2335                                 temp |= SPDIF_EN;
2336                         outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2337                 }
2338         }
2339         spin_unlock_irq(&trident->reg_lock);
2340         return change;
2341 }
2342
2343 static const struct snd_kcontrol_new snd_trident_spdif_control =
2344 {
2345         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2346         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2347         .info =         snd_trident_spdif_control_info,
2348         .get =          snd_trident_spdif_control_get,
2349         .put =          snd_trident_spdif_control_put,
2350         .private_value = 0x28,
2351 };
2352
2353 /*---------------------------------------------------------------------------
2354     snd_trident_spdif_default
2355
2356     Description: put/get the S/PDIF default settings
2357   ---------------------------------------------------------------------------*/
2358
2359 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2360                                           struct snd_ctl_elem_info *uinfo)
2361 {
2362         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2363         uinfo->count = 1;
2364         return 0;
2365 }
2366
2367 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2368                                          struct snd_ctl_elem_value *ucontrol)
2369 {
2370         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2371
2372         spin_lock_irq(&trident->reg_lock);
2373         ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2374         ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2375         ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2376         ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2377         spin_unlock_irq(&trident->reg_lock);
2378         return 0;
2379 }
2380
2381 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2382                                          struct snd_ctl_elem_value *ucontrol)
2383 {
2384         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2385         unsigned int val;
2386         int change;
2387
2388         val = (ucontrol->value.iec958.status[0] << 0) |
2389               (ucontrol->value.iec958.status[1] << 8) |
2390               (ucontrol->value.iec958.status[2] << 16) |
2391               (ucontrol->value.iec958.status[3] << 24);
2392         spin_lock_irq(&trident->reg_lock);
2393         change = trident->spdif_bits != val;
2394         trident->spdif_bits = val;
2395         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2396                 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2397                         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2398         } else {
2399                 if (trident->spdif == NULL)
2400                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2401         }
2402         spin_unlock_irq(&trident->reg_lock);
2403         return change;
2404 }
2405
2406 static const struct snd_kcontrol_new snd_trident_spdif_default =
2407 {
2408         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2409         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2410         .info =         snd_trident_spdif_default_info,
2411         .get =          snd_trident_spdif_default_get,
2412         .put =          snd_trident_spdif_default_put
2413 };
2414
2415 /*---------------------------------------------------------------------------
2416     snd_trident_spdif_mask
2417
2418     Description: put/get the S/PDIF mask
2419   ---------------------------------------------------------------------------*/
2420
2421 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2422                                        struct snd_ctl_elem_info *uinfo)
2423 {
2424         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2425         uinfo->count = 1;
2426         return 0;
2427 }
2428
2429 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2430                                       struct snd_ctl_elem_value *ucontrol)
2431 {
2432         ucontrol->value.iec958.status[0] = 0xff;
2433         ucontrol->value.iec958.status[1] = 0xff;
2434         ucontrol->value.iec958.status[2] = 0xff;
2435         ucontrol->value.iec958.status[3] = 0xff;
2436         return 0;
2437 }
2438
2439 static const struct snd_kcontrol_new snd_trident_spdif_mask =
2440 {
2441         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
2442         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2443         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2444         .info =         snd_trident_spdif_mask_info,
2445         .get =          snd_trident_spdif_mask_get,
2446 };
2447
2448 /*---------------------------------------------------------------------------
2449     snd_trident_spdif_stream
2450
2451     Description: put/get the S/PDIF stream settings
2452   ---------------------------------------------------------------------------*/
2453
2454 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2455                                          struct snd_ctl_elem_info *uinfo)
2456 {
2457         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2458         uinfo->count = 1;
2459         return 0;
2460 }
2461
2462 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2463                                         struct snd_ctl_elem_value *ucontrol)
2464 {
2465         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2466
2467         spin_lock_irq(&trident->reg_lock);
2468         ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2469         ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2470         ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2471         ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2472         spin_unlock_irq(&trident->reg_lock);
2473         return 0;
2474 }
2475
2476 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2477                                         struct snd_ctl_elem_value *ucontrol)
2478 {
2479         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2480         unsigned int val;
2481         int change;
2482
2483         val = (ucontrol->value.iec958.status[0] << 0) |
2484               (ucontrol->value.iec958.status[1] << 8) |
2485               (ucontrol->value.iec958.status[2] << 16) |
2486               (ucontrol->value.iec958.status[3] << 24);
2487         spin_lock_irq(&trident->reg_lock);
2488         change = trident->spdif_pcm_bits != val;
2489         trident->spdif_pcm_bits = val;
2490         if (trident->spdif != NULL) {
2491                 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2492                         outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2493                 } else {
2494                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2495                 }
2496         }
2497         spin_unlock_irq(&trident->reg_lock);
2498         return change;
2499 }
2500
2501 static const struct snd_kcontrol_new snd_trident_spdif_stream =
2502 {
2503         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2504         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2505         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2506         .info =         snd_trident_spdif_stream_info,
2507         .get =          snd_trident_spdif_stream_get,
2508         .put =          snd_trident_spdif_stream_put
2509 };
2510
2511 /*---------------------------------------------------------------------------
2512     snd_trident_ac97_control
2513
2514     Description: enable/disable rear path for ac97
2515   ---------------------------------------------------------------------------*/
2516
2517 #define snd_trident_ac97_control_info   snd_ctl_boolean_mono_info
2518
2519 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2520                                         struct snd_ctl_elem_value *ucontrol)
2521 {
2522         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2523         unsigned char val;
2524
2525         spin_lock_irq(&trident->reg_lock);
2526         val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2527         ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2528         spin_unlock_irq(&trident->reg_lock);
2529         return 0;
2530 }
2531
2532 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2533                                         struct snd_ctl_elem_value *ucontrol)
2534 {
2535         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2536         unsigned char val;
2537         int change = 0;
2538
2539         spin_lock_irq(&trident->reg_lock);
2540         val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2541         val &= ~(1 << kcontrol->private_value);
2542         if (ucontrol->value.integer.value[0])
2543                 val |= 1 << kcontrol->private_value;
2544         change = val != trident->ac97_ctrl;
2545         trident->ac97_ctrl = val;
2546         outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2547         spin_unlock_irq(&trident->reg_lock);
2548         return change;
2549 }
2550
2551 static const struct snd_kcontrol_new snd_trident_ac97_rear_control =
2552 {
2553         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2554         .name =         "Rear Path",
2555         .info =         snd_trident_ac97_control_info,
2556         .get =          snd_trident_ac97_control_get,
2557         .put =          snd_trident_ac97_control_put,
2558         .private_value = 4,
2559 };
2560
2561 /*---------------------------------------------------------------------------
2562     snd_trident_vol_control
2563
2564     Description: wave & music volume control
2565   ---------------------------------------------------------------------------*/
2566
2567 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2568                                         struct snd_ctl_elem_info *uinfo)
2569 {
2570         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2571         uinfo->count = 2;
2572         uinfo->value.integer.min = 0;
2573         uinfo->value.integer.max = 255;
2574         return 0;
2575 }
2576
2577 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2578                                        struct snd_ctl_elem_value *ucontrol)
2579 {
2580         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2581         unsigned int val;
2582
2583         val = trident->musicvol_wavevol;
2584         ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2585         ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2586         return 0;
2587 }
2588
2589 static const DECLARE_TLV_DB_SCALE(db_scale_gvol, -6375, 25, 0);
2590
2591 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2592                                        struct snd_ctl_elem_value *ucontrol)
2593 {
2594         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2595         unsigned int val;
2596         int change = 0;
2597
2598         spin_lock_irq(&trident->reg_lock);
2599         val = trident->musicvol_wavevol;
2600         val &= ~(0xffff << kcontrol->private_value);
2601         val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2602                 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2603         change = val != trident->musicvol_wavevol;
2604         outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2605         spin_unlock_irq(&trident->reg_lock);
2606         return change;
2607 }
2608
2609 static const struct snd_kcontrol_new snd_trident_vol_music_control =
2610 {
2611         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2612         .name =         "Music Playback Volume",
2613         .info =         snd_trident_vol_control_info,
2614         .get =          snd_trident_vol_control_get,
2615         .put =          snd_trident_vol_control_put,
2616         .private_value = 16,
2617         .tlv = { .p = db_scale_gvol },
2618 };
2619
2620 static const struct snd_kcontrol_new snd_trident_vol_wave_control =
2621 {
2622         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2623         .name =         "Wave Playback Volume",
2624         .info =         snd_trident_vol_control_info,
2625         .get =          snd_trident_vol_control_get,
2626         .put =          snd_trident_vol_control_put,
2627         .private_value = 0,
2628         .tlv = { .p = db_scale_gvol },
2629 };
2630
2631 /*---------------------------------------------------------------------------
2632     snd_trident_pcm_vol_control
2633
2634     Description: PCM front volume control
2635   ---------------------------------------------------------------------------*/
2636
2637 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2638                                             struct snd_ctl_elem_info *uinfo)
2639 {
2640         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2641
2642         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2643         uinfo->count = 1;
2644         uinfo->value.integer.min = 0;
2645         uinfo->value.integer.max = 255;
2646         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2647                 uinfo->value.integer.max = 1023;
2648         return 0;
2649 }
2650
2651 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2652                                            struct snd_ctl_elem_value *ucontrol)
2653 {
2654         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2655         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2656
2657         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2658                 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2659         } else {
2660                 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2661         }
2662         return 0;
2663 }
2664
2665 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2666                                            struct snd_ctl_elem_value *ucontrol)
2667 {
2668         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2669         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2670         unsigned int val;
2671         int change = 0;
2672
2673         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2674                 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2675         } else {
2676                 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2677         }
2678         spin_lock_irq(&trident->reg_lock);
2679         change = val != mix->vol;
2680         mix->vol = val;
2681         if (mix->voice != NULL)
2682                 snd_trident_write_vol_reg(trident, mix->voice, val);
2683         spin_unlock_irq(&trident->reg_lock);
2684         return change;
2685 }
2686
2687 static const struct snd_kcontrol_new snd_trident_pcm_vol_control =
2688 {
2689         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2690         .name =         "PCM Front Playback Volume",
2691         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2692         .count =        32,
2693         .info =         snd_trident_pcm_vol_control_info,
2694         .get =          snd_trident_pcm_vol_control_get,
2695         .put =          snd_trident_pcm_vol_control_put,
2696         /* FIXME: no tlv yet */
2697 };
2698
2699 /*---------------------------------------------------------------------------
2700     snd_trident_pcm_pan_control
2701
2702     Description: PCM front pan control
2703   ---------------------------------------------------------------------------*/
2704
2705 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2706                                             struct snd_ctl_elem_info *uinfo)
2707 {
2708         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2709         uinfo->count = 1;
2710         uinfo->value.integer.min = 0;
2711         uinfo->value.integer.max = 127;
2712         return 0;
2713 }
2714
2715 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2716                                            struct snd_ctl_elem_value *ucontrol)
2717 {
2718         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2719         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2720
2721         ucontrol->value.integer.value[0] = mix->pan;
2722         if (ucontrol->value.integer.value[0] & 0x40) {
2723                 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2724         } else {
2725                 ucontrol->value.integer.value[0] |= 0x40;
2726         }
2727         return 0;
2728 }
2729
2730 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2731                                            struct snd_ctl_elem_value *ucontrol)
2732 {
2733         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2734         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2735         unsigned char val;
2736         int change = 0;
2737
2738         if (ucontrol->value.integer.value[0] & 0x40)
2739                 val = ucontrol->value.integer.value[0] & 0x3f;
2740         else
2741                 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2742         spin_lock_irq(&trident->reg_lock);
2743         change = val != mix->pan;
2744         mix->pan = val;
2745         if (mix->voice != NULL)
2746                 snd_trident_write_pan_reg(trident, mix->voice, val);
2747         spin_unlock_irq(&trident->reg_lock);
2748         return change;
2749 }
2750
2751 static const struct snd_kcontrol_new snd_trident_pcm_pan_control =
2752 {
2753         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2754         .name =         "PCM Pan Playback Control",
2755         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2756         .count =        32,
2757         .info =         snd_trident_pcm_pan_control_info,
2758         .get =          snd_trident_pcm_pan_control_get,
2759         .put =          snd_trident_pcm_pan_control_put,
2760 };
2761
2762 /*---------------------------------------------------------------------------
2763     snd_trident_pcm_rvol_control
2764
2765     Description: PCM reverb volume control
2766   ---------------------------------------------------------------------------*/
2767
2768 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2769                                              struct snd_ctl_elem_info *uinfo)
2770 {
2771         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2772         uinfo->count = 1;
2773         uinfo->value.integer.min = 0;
2774         uinfo->value.integer.max = 127;
2775         return 0;
2776 }
2777
2778 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2779                                             struct snd_ctl_elem_value *ucontrol)
2780 {
2781         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2782         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2783
2784         ucontrol->value.integer.value[0] = 127 - mix->rvol;
2785         return 0;
2786 }
2787
2788 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2789                                             struct snd_ctl_elem_value *ucontrol)
2790 {
2791         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2792         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2793         unsigned short val;
2794         int change = 0;
2795
2796         val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2797         spin_lock_irq(&trident->reg_lock);
2798         change = val != mix->rvol;
2799         mix->rvol = val;
2800         if (mix->voice != NULL)
2801                 snd_trident_write_rvol_reg(trident, mix->voice, val);
2802         spin_unlock_irq(&trident->reg_lock);
2803         return change;
2804 }
2805
2806 static const DECLARE_TLV_DB_SCALE(db_scale_crvol, -3175, 25, 1);
2807
2808 static const struct snd_kcontrol_new snd_trident_pcm_rvol_control =
2809 {
2810         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2811         .name =         "PCM Reverb Playback Volume",
2812         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2813         .count =        32,
2814         .info =         snd_trident_pcm_rvol_control_info,
2815         .get =          snd_trident_pcm_rvol_control_get,
2816         .put =          snd_trident_pcm_rvol_control_put,
2817         .tlv = { .p = db_scale_crvol },
2818 };
2819
2820 /*---------------------------------------------------------------------------
2821     snd_trident_pcm_cvol_control
2822
2823     Description: PCM chorus volume control
2824   ---------------------------------------------------------------------------*/
2825
2826 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2827                                              struct snd_ctl_elem_info *uinfo)
2828 {
2829         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2830         uinfo->count = 1;
2831         uinfo->value.integer.min = 0;
2832         uinfo->value.integer.max = 127;
2833         return 0;
2834 }
2835
2836 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2837                                             struct snd_ctl_elem_value *ucontrol)
2838 {
2839         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2840         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2841
2842         ucontrol->value.integer.value[0] = 127 - mix->cvol;
2843         return 0;
2844 }
2845
2846 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2847                                             struct snd_ctl_elem_value *ucontrol)
2848 {
2849         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2850         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2851         unsigned short val;
2852         int change = 0;
2853
2854         val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2855         spin_lock_irq(&trident->reg_lock);
2856         change = val != mix->cvol;
2857         mix->cvol = val;
2858         if (mix->voice != NULL)
2859                 snd_trident_write_cvol_reg(trident, mix->voice, val);
2860         spin_unlock_irq(&trident->reg_lock);
2861         return change;
2862 }
2863
2864 static const struct snd_kcontrol_new snd_trident_pcm_cvol_control =
2865 {
2866         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2867         .name =         "PCM Chorus Playback Volume",
2868         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2869         .count =        32,
2870         .info =         snd_trident_pcm_cvol_control_info,
2871         .get =          snd_trident_pcm_cvol_control_get,
2872         .put =          snd_trident_pcm_cvol_control_put,
2873         .tlv = { .p = db_scale_crvol },
2874 };
2875
2876 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2877                                            struct snd_kcontrol *kctl,
2878                                            int num, int activate)
2879 {
2880         struct snd_ctl_elem_id id;
2881
2882         if (! kctl)
2883                 return;
2884         if (activate)
2885                 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2886         else
2887                 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2888         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2889                        SNDRV_CTL_EVENT_MASK_INFO,
2890                        snd_ctl_build_ioff(&id, kctl, num));
2891 }
2892
2893 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2894                                           struct snd_trident_pcm_mixer *tmix,
2895                                           int num, int activate)
2896 {
2897         snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2898         snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2899         snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2900         snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2901 }
2902
2903 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2904                                        struct snd_trident_voice *voice,
2905                                        struct snd_pcm_substream *substream)
2906 {
2907         struct snd_trident_pcm_mixer *tmix;
2908
2909         if (snd_BUG_ON(!trident || !voice || !substream))
2910                 return -EINVAL;
2911         tmix = &trident->pcm_mixer[substream->number];
2912         tmix->voice = voice;
2913         tmix->vol = T4D_DEFAULT_PCM_VOL;
2914         tmix->pan = T4D_DEFAULT_PCM_PAN;
2915         tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2916         tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2917         snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2918         return 0;
2919 }
2920
2921 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2922 {
2923         struct snd_trident_pcm_mixer *tmix;
2924
2925         if (snd_BUG_ON(!trident || !substream))
2926                 return -EINVAL;
2927         tmix = &trident->pcm_mixer[substream->number];
2928         tmix->voice = NULL;
2929         snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2930         return 0;
2931 }
2932
2933 /*---------------------------------------------------------------------------
2934    snd_trident_mixer
2935   
2936    Description: This routine registers the 4DWave device for mixer support.
2937                 
2938    Parameters:  trident - pointer to target device class for 4DWave.
2939
2940    Returns:     None
2941   
2942   ---------------------------------------------------------------------------*/
2943
2944 static int snd_trident_mixer(struct snd_trident *trident, int pcm_spdif_device)
2945 {
2946         struct snd_ac97_template _ac97;
2947         struct snd_card *card = trident->card;
2948         struct snd_kcontrol *kctl;
2949         struct snd_ctl_elem_value *uctl;
2950         int idx, err, retries = 2;
2951         static struct snd_ac97_bus_ops ops = {
2952                 .write = snd_trident_codec_write,
2953                 .read = snd_trident_codec_read,
2954         };
2955
2956         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
2957         if (!uctl)
2958                 return -ENOMEM;
2959
2960         if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
2961                 goto __out;
2962
2963         memset(&_ac97, 0, sizeof(_ac97));
2964         _ac97.private_data = trident;
2965         trident->ac97_detect = 1;
2966
2967       __again:
2968         if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
2969                 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2970                         if ((err = snd_trident_sis_reset(trident)) < 0)
2971                                 goto __out;
2972                         if (retries-- > 0)
2973                                 goto __again;
2974                         err = -EIO;
2975                 }
2976                 goto __out;
2977         }
2978         
2979         /* secondary codec? */
2980         if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
2981             (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
2982                 _ac97.num = 1;
2983                 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
2984                 if (err < 0)
2985                         dev_err(trident->card->dev,
2986                                 "SI7018: the secondary codec - invalid access\n");
2987 #if 0   // only for my testing purpose --jk
2988                 {
2989                         struct snd_ac97 *mc97;
2990                         err = snd_ac97_modem(trident->card, &_ac97, &mc97);
2991                         if (err < 0)
2992                                 dev_err(trident->card->dev,
2993                                         "snd_ac97_modem returned error %i\n", err);
2994                 }
2995 #endif
2996         }
2997         
2998         trident->ac97_detect = 0;
2999
3000         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3001                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3002                         goto __out;
3003                 kctl->put(kctl, uctl);
3004                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3005                         goto __out;
3006                 kctl->put(kctl, uctl);
3007                 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3008         } else {
3009                 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3010         }
3011
3012         for (idx = 0; idx < 32; idx++) {
3013                 struct snd_trident_pcm_mixer *tmix;
3014                 
3015                 tmix = &trident->pcm_mixer[idx];
3016                 tmix->voice = NULL;
3017         }
3018         if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3019                 goto __nomem;
3020         if ((err = snd_ctl_add(card, trident->ctl_vol)))
3021                 goto __out;
3022                 
3023         if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3024                 goto __nomem;
3025         if ((err = snd_ctl_add(card, trident->ctl_pan)))
3026                 goto __out;
3027
3028         if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3029                 goto __nomem;
3030         if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3031                 goto __out;
3032
3033         if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3034                 goto __nomem;
3035         if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3036                 goto __out;
3037
3038         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3039                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3040                         goto __out;
3041                 kctl->put(kctl, uctl);
3042         }
3043         if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3044
3045                 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3046                 if (kctl == NULL) {
3047                         err = -ENOMEM;
3048                         goto __out;
3049                 }
3050                 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3051                         kctl->id.index++;
3052                 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3053                         kctl->id.index++;
3054                 idx = kctl->id.index;
3055                 if ((err = snd_ctl_add(card, kctl)) < 0)
3056                         goto __out;
3057                 kctl->put(kctl, uctl);
3058
3059                 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3060                 if (kctl == NULL) {
3061                         err = -ENOMEM;
3062                         goto __out;
3063                 }
3064                 kctl->id.index = idx;
3065                 kctl->id.device = pcm_spdif_device;
3066                 if ((err = snd_ctl_add(card, kctl)) < 0)
3067                         goto __out;
3068
3069                 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3070                 if (kctl == NULL) {
3071                         err = -ENOMEM;
3072                         goto __out;
3073                 }
3074                 kctl->id.index = idx;
3075                 kctl->id.device = pcm_spdif_device;
3076                 if ((err = snd_ctl_add(card, kctl)) < 0)
3077                         goto __out;
3078
3079                 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3080                 if (kctl == NULL) {
3081                         err = -ENOMEM;
3082                         goto __out;
3083                 }
3084                 kctl->id.index = idx;
3085                 kctl->id.device = pcm_spdif_device;
3086                 if ((err = snd_ctl_add(card, kctl)) < 0)
3087                         goto __out;
3088                 trident->spdif_pcm_ctl = kctl;
3089         }
3090
3091         err = 0;
3092         goto __out;
3093
3094  __nomem:
3095         err = -ENOMEM;
3096
3097  __out:
3098         kfree(uctl);
3099
3100         return err;
3101 }
3102
3103 /*
3104  * gameport interface
3105  */
3106
3107 #if IS_REACHABLE(CONFIG_GAMEPORT)
3108
3109 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3110 {
3111         struct snd_trident *chip = gameport_get_port_data(gameport);
3112
3113         if (snd_BUG_ON(!chip))
3114                 return 0;
3115         return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3116 }
3117
3118 static void snd_trident_gameport_trigger(struct gameport *gameport)
3119 {
3120         struct snd_trident *chip = gameport_get_port_data(gameport);
3121
3122         if (snd_BUG_ON(!chip))
3123                 return;
3124         outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3125 }
3126
3127 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3128 {
3129         struct snd_trident *chip = gameport_get_port_data(gameport);
3130         int i;
3131
3132         if (snd_BUG_ON(!chip))
3133                 return 0;
3134
3135         *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3136
3137         for (i = 0; i < 4; i++) {
3138                 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3139                 if (axes[i] == 0xffff) axes[i] = -1;
3140         }
3141         
3142         return 0;
3143 }
3144
3145 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3146 {
3147         struct snd_trident *chip = gameport_get_port_data(gameport);
3148
3149         if (snd_BUG_ON(!chip))
3150                 return 0;
3151
3152         switch (mode) {
3153                 case GAMEPORT_MODE_COOKED:
3154                         outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3155                         msleep(20);
3156                         return 0;
3157                 case GAMEPORT_MODE_RAW:
3158                         outb(0, TRID_REG(chip, GAMEPORT_GCR));
3159                         return 0;
3160                 default:
3161                         return -1;
3162         }
3163 }
3164
3165 int snd_trident_create_gameport(struct snd_trident *chip)
3166 {
3167         struct gameport *gp;
3168
3169         chip->gameport = gp = gameport_allocate_port();
3170         if (!gp) {
3171                 dev_err(chip->card->dev,
3172                         "cannot allocate memory for gameport\n");
3173                 return -ENOMEM;
3174         }
3175
3176         gameport_set_name(gp, "Trident 4DWave");
3177         gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3178         gameport_set_dev_parent(gp, &chip->pci->dev);
3179
3180         gameport_set_port_data(gp, chip);
3181         gp->fuzz = 64;
3182         gp->read = snd_trident_gameport_read;
3183         gp->trigger = snd_trident_gameport_trigger;
3184         gp->cooked_read = snd_trident_gameport_cooked_read;
3185         gp->open = snd_trident_gameport_open;
3186
3187         gameport_register_port(gp);
3188
3189         return 0;
3190 }
3191
3192 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3193 {
3194         if (chip->gameport) {
3195                 gameport_unregister_port(chip->gameport);
3196                 chip->gameport = NULL;
3197         }
3198 }
3199 #else
3200 int snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3201 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3202 #endif /* CONFIG_GAMEPORT */
3203
3204 /*
3205  * delay for 1 tick
3206  */
3207 static inline void do_delay(struct snd_trident *chip)
3208 {
3209         schedule_timeout_uninterruptible(1);
3210 }
3211
3212 /*
3213  *  SiS reset routine
3214  */
3215
3216 static int snd_trident_sis_reset(struct snd_trident *trident)
3217 {
3218         unsigned long end_time;
3219         unsigned int i;
3220         int r;
3221
3222         r = trident->in_suspend ? 0 : 2;        /* count of retries */
3223       __si7018_retry:
3224         pci_write_config_byte(trident->pci, 0x46, 0x04);        /* SOFTWARE RESET */
3225         udelay(100);
3226         pci_write_config_byte(trident->pci, 0x46, 0x00);
3227         udelay(100);
3228         /* disable AC97 GPIO interrupt */
3229         outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3230         /* initialize serial interface, force cold reset */
3231         i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3232         outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3233         udelay(1000);
3234         /* remove cold reset */
3235         i &= ~COLD_RESET;
3236         outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3237         udelay(2000);
3238         /* wait, until the codec is ready */
3239         end_time = (jiffies + (HZ * 3) / 4) + 1;
3240         do {
3241                 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3242                         goto __si7018_ok;
3243                 do_delay(trident);
3244         } while (time_after_eq(end_time, jiffies));
3245         dev_err(trident->card->dev, "AC'97 codec ready error [0x%x]\n",
3246                 inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3247         if (r-- > 0) {
3248                 end_time = jiffies + HZ;
3249                 do {
3250                         do_delay(trident);
3251                 } while (time_after_eq(end_time, jiffies));
3252                 goto __si7018_retry;
3253         }
3254       __si7018_ok:
3255         /* wait for the second codec */
3256         do {
3257                 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3258                         break;
3259                 do_delay(trident);
3260         } while (time_after_eq(end_time, jiffies));
3261         /* enable 64 channel mode */
3262         outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3263         return 0;
3264 }
3265
3266 /*  
3267  *  /proc interface
3268  */
3269
3270 static void snd_trident_proc_read(struct snd_info_entry *entry, 
3271                                   struct snd_info_buffer *buffer)
3272 {
3273         struct snd_trident *trident = entry->private_data;
3274         char *s;
3275
3276         switch (trident->device) {
3277         case TRIDENT_DEVICE_ID_SI7018:
3278                 s = "SiS 7018 Audio";
3279                 break;
3280         case TRIDENT_DEVICE_ID_DX:
3281                 s = "Trident 4DWave PCI DX";
3282                 break;
3283         case TRIDENT_DEVICE_ID_NX:
3284                 s = "Trident 4DWave PCI NX";
3285                 break;
3286         default:
3287                 s = "???";
3288         }
3289         snd_iprintf(buffer, "%s\n\n", s);
3290         snd_iprintf(buffer, "Spurious IRQs    : %d\n", trident->spurious_irq_count);
3291         snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3292         if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3293                 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3294         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3295                 snd_iprintf(buffer, "Rear Speakers    : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3296                 if (trident->tlb.entries) {
3297                         snd_iprintf(buffer,"\nVirtual Memory\n");
3298                         snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3299                         snd_iprintf(buffer, "Memory Used    : %d\n", trident->tlb.memhdr->used);
3300                         snd_iprintf(buffer, "Memory Free    : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3301                 }
3302         }
3303 }
3304
3305 static void snd_trident_proc_init(struct snd_trident *trident)
3306 {
3307         const char *s = "trident";
3308         
3309         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3310                 s = "sis7018";
3311         snd_card_ro_proc_new(trident->card, s, trident, snd_trident_proc_read);
3312 }
3313
3314 static int snd_trident_dev_free(struct snd_device *device)
3315 {
3316         struct snd_trident *trident = device->device_data;
3317         return snd_trident_free(trident);
3318 }
3319
3320 /*---------------------------------------------------------------------------
3321    snd_trident_tlb_alloc
3322   
3323    Description: Allocate and set up the TLB page table on 4D NX.
3324                 Each entry has 4 bytes (physical PCI address).
3325                 
3326    Parameters:  trident - pointer to target device class for 4DWave.
3327
3328    Returns:     0 or negative error code
3329   
3330   ---------------------------------------------------------------------------*/
3331
3332 static int snd_trident_tlb_alloc(struct snd_trident *trident)
3333 {
3334         int i;
3335
3336         /* TLB array must be aligned to 16kB !!! so we allocate
3337            32kB region and correct offset when necessary */
3338
3339         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3340                                 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3341                 dev_err(trident->card->dev, "unable to allocate TLB buffer\n");
3342                 return -ENOMEM;
3343         }
3344         trident->tlb.entries = (__le32 *)ALIGN((unsigned long)trident->tlb.buffer.area, SNDRV_TRIDENT_MAX_PAGES * 4);
3345         trident->tlb.entries_dmaaddr = ALIGN(trident->tlb.buffer.addr, SNDRV_TRIDENT_MAX_PAGES * 4);
3346         /* allocate shadow TLB page table (virtual addresses) */
3347         trident->tlb.shadow_entries =
3348                 vmalloc(array_size(SNDRV_TRIDENT_MAX_PAGES,
3349                                    sizeof(unsigned long)));
3350         if (!trident->tlb.shadow_entries)
3351                 return -ENOMEM;
3352
3353         /* allocate and setup silent page and initialise TLB entries */
3354         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3355                                 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3356                 dev_err(trident->card->dev, "unable to allocate silent page\n");
3357                 return -ENOMEM;
3358         }
3359         memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3360         for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3361                 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3362                 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3363         }
3364
3365         /* use emu memory block manager code to manage tlb page allocation */
3366         trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3367         if (trident->tlb.memhdr == NULL)
3368                 return -ENOMEM;
3369
3370         trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3371         return 0;
3372 }
3373
3374 /*
3375  * initialize 4D DX chip
3376  */
3377
3378 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3379 {
3380         outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3381         outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3382         outl(0, TRID_REG(trident, T4D_AINTEN_A));
3383         outl(0, TRID_REG(trident, T4D_AINTEN_B));
3384 }
3385
3386 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3387 {
3388         struct pci_dev *pci = trident->pci;
3389         unsigned long end_time;
3390
3391         /* reset the legacy configuration and whole audio/wavetable block */
3392         pci_write_config_dword(pci, 0x40, 0);   /* DDMA */
3393         pci_write_config_byte(pci, 0x44, 0);    /* ports */
3394         pci_write_config_byte(pci, 0x45, 0);    /* Legacy DMA */
3395         pci_write_config_byte(pci, 0x46, 4); /* reset */
3396         udelay(100);
3397         pci_write_config_byte(pci, 0x46, 0); /* release reset */
3398         udelay(100);
3399         
3400         /* warm reset of the AC'97 codec */
3401         outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3402         udelay(100);
3403         outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3404         /* DAC on, disable SB IRQ and try to force ADC valid signal */
3405         trident->ac97_ctrl = 0x0000004a;
3406         outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3407         /* wait, until the codec is ready */
3408         end_time = (jiffies + (HZ * 3) / 4) + 1;
3409         do {
3410                 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3411                         goto __dx_ok;
3412                 do_delay(trident);
3413         } while (time_after_eq(end_time, jiffies));
3414         dev_err(trident->card->dev, "AC'97 codec ready error\n");
3415         return -EIO;
3416
3417  __dx_ok:
3418         snd_trident_stop_all_voices(trident);
3419
3420         return 0;
3421 }
3422
3423 /*
3424  * initialize 4D NX chip
3425  */
3426 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3427 {
3428         struct pci_dev *pci = trident->pci;
3429         unsigned long end_time;
3430
3431         /* reset the legacy configuration and whole audio/wavetable block */
3432         pci_write_config_dword(pci, 0x40, 0);   /* DDMA */
3433         pci_write_config_byte(pci, 0x44, 0);    /* ports */
3434         pci_write_config_byte(pci, 0x45, 0);    /* Legacy DMA */
3435
3436         pci_write_config_byte(pci, 0x46, 1); /* reset */
3437         udelay(100);
3438         pci_write_config_byte(pci, 0x46, 0); /* release reset */
3439         udelay(100);
3440
3441         /* warm reset of the AC'97 codec */
3442         outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3443         udelay(100);
3444         outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3445         /* wait, until the codec is ready */
3446         end_time = (jiffies + (HZ * 3) / 4) + 1;
3447         do {
3448                 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3449                         goto __nx_ok;
3450                 do_delay(trident);
3451         } while (time_after_eq(end_time, jiffies));
3452         dev_err(trident->card->dev, "AC'97 codec ready error [0x%x]\n",
3453                 inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3454         return -EIO;
3455
3456  __nx_ok:
3457         /* DAC on */
3458         trident->ac97_ctrl = 0x00000002;
3459         outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3460         /* disable SB IRQ */
3461         outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3462
3463         snd_trident_stop_all_voices(trident);
3464
3465         if (trident->tlb.entries != NULL) {
3466                 unsigned int i;
3467                 /* enable virtual addressing via TLB */
3468                 i = trident->tlb.entries_dmaaddr;
3469                 i |= 0x00000001;
3470                 outl(i, TRID_REG(trident, NX_TLBC));
3471         } else {
3472                 outl(0, TRID_REG(trident, NX_TLBC));
3473         }
3474         /* initialize S/PDIF */
3475         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3476         outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3477
3478         return 0;
3479 }
3480
3481 /*
3482  * initialize sis7018 chip
3483  */
3484 static int snd_trident_sis_init(struct snd_trident *trident)
3485 {
3486         int err;
3487
3488         if ((err = snd_trident_sis_reset(trident)) < 0)
3489                 return err;
3490
3491         snd_trident_stop_all_voices(trident);
3492
3493         /* initialize S/PDIF */
3494         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3495
3496         return 0;
3497 }
3498
3499 /*---------------------------------------------------------------------------
3500    snd_trident_create
3501   
3502    Description: This routine will create the device specific class for
3503                 the 4DWave card. It will also perform basic initialization.
3504                 
3505    Parameters:  card  - which card to create
3506                 pci   - interface to PCI bus resource info
3507                 dma1ptr - playback dma buffer
3508                 dma2ptr - capture dma buffer
3509                 irqptr  -  interrupt resource info
3510
3511    Returns:     4DWave device class private data
3512   
3513   ---------------------------------------------------------------------------*/
3514
3515 int snd_trident_create(struct snd_card *card,
3516                        struct pci_dev *pci,
3517                        int pcm_streams,
3518                        int pcm_spdif_device,
3519                        int max_wavetable_size,
3520                        struct snd_trident ** rtrident)
3521 {
3522         struct snd_trident *trident;
3523         int i, err;
3524         struct snd_trident_voice *voice;
3525         struct snd_trident_pcm_mixer *tmix;
3526         static struct snd_device_ops ops = {
3527                 .dev_free =     snd_trident_dev_free,
3528         };
3529
3530         *rtrident = NULL;
3531
3532         /* enable PCI device */
3533         if ((err = pci_enable_device(pci)) < 0)
3534                 return err;
3535         /* check, if we can restrict PCI DMA transfers to 30 bits */
3536         if (dma_set_mask(&pci->dev, DMA_BIT_MASK(30)) < 0 ||
3537             dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(30)) < 0) {
3538                 dev_err(card->dev,
3539                         "architecture does not support 30bit PCI busmaster DMA\n");
3540                 pci_disable_device(pci);
3541                 return -ENXIO;
3542         }
3543         
3544         trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3545         if (trident == NULL) {
3546                 pci_disable_device(pci);
3547                 return -ENOMEM;
3548         }
3549         trident->device = (pci->vendor << 16) | pci->device;
3550         trident->card = card;
3551         trident->pci = pci;
3552         spin_lock_init(&trident->reg_lock);
3553         spin_lock_init(&trident->event_lock);
3554         spin_lock_init(&trident->voice_alloc);
3555         if (pcm_streams < 1)
3556                 pcm_streams = 1;
3557         if (pcm_streams > 32)
3558                 pcm_streams = 32;
3559         trident->ChanPCM = pcm_streams;
3560         if (max_wavetable_size < 0 )
3561                 max_wavetable_size = 0;
3562         trident->synth.max_size = max_wavetable_size * 1024;
3563         trident->irq = -1;
3564
3565         trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3566         pci_set_master(pci);
3567
3568         if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3569                 kfree(trident);
3570                 pci_disable_device(pci);
3571                 return err;
3572         }
3573         trident->port = pci_resource_start(pci, 0);
3574
3575         if (request_irq(pci->irq, snd_trident_interrupt, IRQF_SHARED,
3576                         KBUILD_MODNAME, trident)) {
3577                 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
3578                 snd_trident_free(trident);
3579                 return -EBUSY;
3580         }
3581         trident->irq = pci->irq;
3582
3583         /* allocate 16k-aligned TLB for NX cards */
3584         trident->tlb.entries = NULL;
3585         trident->tlb.buffer.area = NULL;
3586         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3587                 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3588                         snd_trident_free(trident);
3589                         return err;
3590                 }
3591         }
3592
3593         trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3594
3595         /* initialize chip */
3596         switch (trident->device) {
3597         case TRIDENT_DEVICE_ID_DX:
3598                 err = snd_trident_4d_dx_init(trident);
3599                 break;
3600         case TRIDENT_DEVICE_ID_NX:
3601                 err = snd_trident_4d_nx_init(trident);
3602                 break;
3603         case TRIDENT_DEVICE_ID_SI7018:
3604                 err = snd_trident_sis_init(trident);
3605                 break;
3606         default:
3607                 snd_BUG();
3608                 break;
3609         }
3610         if (err < 0) {
3611                 snd_trident_free(trident);
3612                 return err;
3613         }
3614
3615         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3616                 snd_trident_free(trident);
3617                 return err;
3618         }
3619
3620         if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3621                 return err;
3622         
3623         /* initialise synth voices */
3624         for (i = 0; i < 64; i++) {
3625                 voice = &trident->synth.voices[i];
3626                 voice->number = i;
3627                 voice->trident = trident;
3628         }
3629         /* initialize pcm mixer entries */
3630         for (i = 0; i < 32; i++) {
3631                 tmix = &trident->pcm_mixer[i];
3632                 tmix->vol = T4D_DEFAULT_PCM_VOL;
3633                 tmix->pan = T4D_DEFAULT_PCM_PAN;
3634                 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3635                 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3636         }
3637
3638         snd_trident_enable_eso(trident);
3639
3640         snd_trident_proc_init(trident);
3641         *rtrident = trident;
3642         return 0;
3643 }
3644
3645 /*---------------------------------------------------------------------------
3646    snd_trident_free
3647   
3648    Description: This routine will free the device specific class for
3649                 the 4DWave card. 
3650                 
3651    Parameters:  trident  - device specific private data for 4DWave card
3652
3653    Returns:     None.
3654   
3655   ---------------------------------------------------------------------------*/
3656
3657 static int snd_trident_free(struct snd_trident *trident)
3658 {
3659         snd_trident_free_gameport(trident);
3660         snd_trident_disable_eso(trident);
3661         // Disable S/PDIF out
3662         if (trident->device == TRIDENT_DEVICE_ID_NX)
3663                 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3664         else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3665                 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3666         }
3667         if (trident->irq >= 0)
3668                 free_irq(trident->irq, trident);
3669         if (trident->tlb.buffer.area) {
3670                 outl(0, TRID_REG(trident, NX_TLBC));
3671                 snd_util_memhdr_free(trident->tlb.memhdr);
3672                 if (trident->tlb.silent_page.area)
3673                         snd_dma_free_pages(&trident->tlb.silent_page);
3674                 vfree(trident->tlb.shadow_entries);
3675                 snd_dma_free_pages(&trident->tlb.buffer);
3676         }
3677         pci_release_regions(trident->pci);
3678         pci_disable_device(trident->pci);
3679         kfree(trident);
3680         return 0;
3681 }
3682
3683 /*---------------------------------------------------------------------------
3684    snd_trident_interrupt
3685   
3686    Description: ISR for Trident 4DWave device
3687                 
3688    Parameters:  trident  - device specific private data for 4DWave card
3689
3690    Problems:    It seems that Trident chips generates interrupts more than
3691                 one time in special cases. The spurious interrupts are
3692                 detected via sample timer (T4D_STIMER) and computing
3693                 corresponding delta value. The limits are detected with
3694                 the method try & fail so it is possible that it won't
3695                 work on all computers. [jaroslav]
3696
3697    Returns:     None.
3698   
3699   ---------------------------------------------------------------------------*/
3700
3701 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id)
3702 {
3703         struct snd_trident *trident = dev_id;
3704         unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3705         int delta;
3706         struct snd_trident_voice *voice;
3707
3708         audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3709         if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3710                 return IRQ_NONE;
3711         if (audio_int & ADDRESS_IRQ) {
3712                 // get interrupt status for all channels
3713                 spin_lock(&trident->reg_lock);
3714                 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3715                 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3716                 if (chn_int == 0)
3717                         goto __skip1;
3718                 outl(chn_int, TRID_REG(trident, T4D_AINT_A));   /* ack */
3719               __skip1:
3720                 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3721                 if (chn_int == 0)
3722                         goto __skip2;
3723                 for (channel = 63; channel >= 32; channel--) {
3724                         mask = 1 << (channel&0x1f);
3725                         if ((chn_int & mask) == 0)
3726                                 continue;
3727                         voice = &trident->synth.voices[channel];
3728                         if (!voice->pcm || voice->substream == NULL) {
3729                                 outl(mask, TRID_REG(trident, T4D_STOP_B));
3730                                 continue;
3731                         }
3732                         delta = (int)stimer - (int)voice->stimer;
3733                         if (delta < 0)
3734                                 delta = -delta;
3735                         if ((unsigned int)delta < voice->spurious_threshold) {
3736                                 /* do some statistics here */
3737                                 trident->spurious_irq_count++;
3738                                 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3739                                         trident->spurious_irq_max_delta = delta;
3740                                 continue;
3741                         }
3742                         voice->stimer = stimer;
3743                         if (voice->isync) {
3744                                 if (!voice->isync3) {
3745                                         tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3746                                         if (trident->bDMAStart & 0x40)
3747                                                 tmp >>= 1;
3748                                         if (tmp > 0)
3749                                                 tmp = voice->isync_max - tmp;
3750                                 } else {
3751                                         tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3752                                 }
3753                                 if (tmp < voice->isync_mark) {
3754                                         if (tmp > 0x10)
3755                                                 tmp = voice->isync_ESO - 7;
3756                                         else
3757                                                 tmp = voice->isync_ESO + 2;
3758                                         /* update ESO for IRQ voice to preserve sync */
3759                                         snd_trident_stop_voice(trident, voice->number);
3760                                         snd_trident_write_eso_reg(trident, voice, tmp);
3761                                         snd_trident_start_voice(trident, voice->number);
3762                                 }
3763                         } else if (voice->isync2) {
3764                                 voice->isync2 = 0;
3765                                 /* write original ESO and update CSO for IRQ voice to preserve sync */
3766                                 snd_trident_stop_voice(trident, voice->number);
3767                                 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3768                                 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3769                                 snd_trident_start_voice(trident, voice->number);
3770                         }
3771 #if 0
3772                         if (voice->extra) {
3773                                 /* update CSO for extra voice to preserve sync */
3774                                 snd_trident_stop_voice(trident, voice->extra->number);
3775                                 snd_trident_write_cso_reg(trident, voice->extra, 0);
3776                                 snd_trident_start_voice(trident, voice->extra->number);
3777                         }
3778 #endif
3779                         spin_unlock(&trident->reg_lock);
3780                         snd_pcm_period_elapsed(voice->substream);
3781                         spin_lock(&trident->reg_lock);
3782                 }
3783                 outl(chn_int, TRID_REG(trident, T4D_AINT_B));   /* ack */
3784               __skip2:
3785                 spin_unlock(&trident->reg_lock);
3786         }
3787         if (audio_int & MPU401_IRQ) {
3788                 if (trident->rmidi) {
3789                         snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data);
3790                 } else {
3791                         inb(TRID_REG(trident, T4D_MPUR0));
3792                 }
3793         }
3794         // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3795         return IRQ_HANDLED;
3796 }
3797
3798 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3799 {
3800         struct snd_trident_voice *pvoice;
3801         unsigned long flags;
3802         int idx;
3803
3804         spin_lock_irqsave(&trident->voice_alloc, flags);
3805         if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3806                 idx = snd_trident_allocate_pcm_channel(trident);
3807                 if(idx < 0) {
3808                         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3809                         return NULL;
3810                 }
3811                 pvoice = &trident->synth.voices[idx];
3812                 pvoice->use = 1;
3813                 pvoice->pcm = 1;
3814                 pvoice->capture = 0;
3815                 pvoice->spdif = 0;
3816                 pvoice->memblk = NULL;
3817                 pvoice->substream = NULL;
3818                 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3819                 return pvoice;
3820         }
3821         if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3822                 idx = snd_trident_allocate_synth_channel(trident);
3823                 if(idx < 0) {
3824                         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3825                         return NULL;
3826                 }
3827                 pvoice = &trident->synth.voices[idx];
3828                 pvoice->use = 1;
3829                 pvoice->synth = 1;
3830                 pvoice->client = client;
3831                 pvoice->port = port;
3832                 pvoice->memblk = NULL;
3833                 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3834                 return pvoice;
3835         }
3836         if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3837         }
3838         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3839         return NULL;
3840 }
3841
3842 EXPORT_SYMBOL(snd_trident_alloc_voice);
3843
3844 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3845 {
3846         unsigned long flags;
3847         void (*private_free)(struct snd_trident_voice *);
3848
3849         if (voice == NULL || !voice->use)
3850                 return;
3851         snd_trident_clear_voices(trident, voice->number, voice->number);
3852         spin_lock_irqsave(&trident->voice_alloc, flags);
3853         private_free = voice->private_free;
3854         voice->private_free = NULL;
3855         voice->private_data = NULL;
3856         if (voice->pcm)
3857                 snd_trident_free_pcm_channel(trident, voice->number);
3858         if (voice->synth)
3859                 snd_trident_free_synth_channel(trident, voice->number);
3860         voice->use = voice->pcm = voice->synth = voice->midi = 0;
3861         voice->capture = voice->spdif = 0;
3862         voice->sample_ops = NULL;
3863         voice->substream = NULL;
3864         voice->extra = NULL;
3865         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3866         if (private_free)
3867                 private_free(voice);
3868 }
3869
3870 EXPORT_SYMBOL(snd_trident_free_voice);
3871
3872 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3873 {
3874         unsigned int i, val, mask[2] = { 0, 0 };
3875
3876         if (snd_BUG_ON(v_min > 63 || v_max > 63))
3877                 return;
3878         for (i = v_min; i <= v_max; i++)
3879                 mask[i >> 5] |= 1 << (i & 0x1f);
3880         if (mask[0]) {
3881                 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3882                 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3883                 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3884         }
3885         if (mask[1]) {
3886                 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3887                 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3888                 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3889         }
3890 }
3891
3892 #ifdef CONFIG_PM_SLEEP
3893 static int snd_trident_suspend(struct device *dev)
3894 {
3895         struct snd_card *card = dev_get_drvdata(dev);
3896         struct snd_trident *trident = card->private_data;
3897
3898         trident->in_suspend = 1;
3899         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3900         snd_ac97_suspend(trident->ac97);
3901         snd_ac97_suspend(trident->ac97_sec);
3902         return 0;
3903 }
3904
3905 static int snd_trident_resume(struct device *dev)
3906 {
3907         struct snd_card *card = dev_get_drvdata(dev);
3908         struct snd_trident *trident = card->private_data;
3909
3910         switch (trident->device) {
3911         case TRIDENT_DEVICE_ID_DX:
3912                 snd_trident_4d_dx_init(trident);
3913                 break;
3914         case TRIDENT_DEVICE_ID_NX:
3915                 snd_trident_4d_nx_init(trident);
3916                 break;
3917         case TRIDENT_DEVICE_ID_SI7018:
3918                 snd_trident_sis_init(trident);
3919                 break;
3920         }
3921
3922         snd_ac97_resume(trident->ac97);
3923         snd_ac97_resume(trident->ac97_sec);
3924
3925         /* restore some registers */
3926         outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3927
3928         snd_trident_enable_eso(trident);
3929
3930         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3931         trident->in_suspend = 0;
3932         return 0;
3933 }
3934
3935 SIMPLE_DEV_PM_OPS(snd_trident_pm, snd_trident_suspend, snd_trident_resume);
3936 #endif /* CONFIG_PM_SLEEP */