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