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