ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / sound / trident.h
1 #ifndef __SOUND_TRIDENT_H
2 #define __SOUND_TRIDENT_H
3
4 /*
5  *  audio@tridentmicro.com
6  *  Fri Feb 19 15:55:28 MST 1999
7  *  Definitions for Trident 4DWave DX/NX chips
8  *
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  */
25
26 #include "pcm.h"
27 #include "mpu401.h"
28 #include "ac97_codec.h"
29 #include "seq_midi_emul.h"
30 #include "seq_device.h"
31 #include "util_mem.h"
32 //#include "ainstr_iw.h"
33 //#include "ainstr_gf1.h"
34 #include "ainstr_simple.h"
35
36 #ifndef PCI_VENDOR_ID_TRIDENT
37 #define PCI_VENDOR_ID_TRIDENT           0x1023
38 #endif
39 #ifndef PCI_DEVICE_ID_TRIDENT_4DWAVE_DX 
40 #define PCI_DEVICE_ID_TRIDENT_4DWAVE_DX 0x2000
41 #endif
42 #ifndef PCI_DEVICE_ID_TRIDENT_4DWAVE_NX 
43 #define PCI_DEVICE_ID_TRIDENT_4DWAVE_NX 0x2001
44 #endif
45
46 #ifndef PCI_VENDOR_ID_SI
47 #define PCI_VENDOR_ID_SI                0x1039
48 #endif
49 #ifndef PCI_DEVICE_ID_SI_7018
50 #define PCI_DEVICE_ID_SI_7018           0x7018
51 #endif
52
53 #define TRIDENT_DEVICE_ID_DX            ((PCI_VENDOR_ID_TRIDENT<<16)|PCI_DEVICE_ID_TRIDENT_4DWAVE_DX)
54 #define TRIDENT_DEVICE_ID_NX            ((PCI_VENDOR_ID_TRIDENT<<16)|PCI_DEVICE_ID_TRIDENT_4DWAVE_NX)
55 #define TRIDENT_DEVICE_ID_SI7018        ((PCI_VENDOR_ID_SI<<16)|PCI_DEVICE_ID_SI_7018)
56
57 /* Trident chipsets have 1GB memory limit */
58 #ifdef __alpha__
59 #define TRIDENT_DMA_TYPE        SNDRV_DMA_TYPE_PCI_16MB
60 #else
61 #define TRIDENT_DMA_TYPE        SNDRV_DMA_TYPE_PCI
62 #endif
63
64 #define SNDRV_SEQ_DEV_ID_TRIDENT                        "trident-synth"
65
66 #define SNDRV_TRIDENT_VOICE_TYPE_PCM            0
67 #define SNDRV_TRIDENT_VOICE_TYPE_SYNTH          1
68 #define SNDRV_TRIDENT_VOICE_TYPE_MIDI           2
69
70 #define SNDRV_TRIDENT_VFLG_RUNNING              (1<<0)
71
72 /* TLB code constants */
73 #define SNDRV_TRIDENT_PAGE_SIZE                 4096
74 #define SNDRV_TRIDENT_PAGE_SHIFT                        12
75 #define SNDRV_TRIDENT_PAGE_MASK                 ((1<<SNDRV_TRIDENT_PAGE_SHIFT)-1)
76 #define SNDRV_TRIDENT_MAX_PAGES                 4096
77
78 /*
79  * Direct registers
80  */
81
82 #define TRID_REG(trident, x) ((trident)->port + (x))
83
84 #define ID_4DWAVE_DX        0x2000
85 #define ID_4DWAVE_NX        0x2001
86
87 /* Bank definitions */
88
89 #define T4D_BANK_A      0
90 #define T4D_BANK_B      1
91 #define T4D_NUM_BANKS   2
92
93 /* Register definitions */
94
95 /* Global registers */
96
97 enum global_control_bits {
98         CHANNEL_IDX     = 0x0000003f,
99         OVERRUN_IE      = 0x00000400,   /* interrupt enable: capture overrun */
100         UNDERRUN_IE     = 0x00000800,   /* interrupt enable: playback underrun */
101         ENDLP_IE        = 0x00001000,   /* interrupt enable: end of buffer */
102         MIDLP_IE        = 0x00002000,   /* interrupt enable: middle buffer */
103         ETOG_IE         = 0x00004000,   /* interrupt enable: envelope toggling */
104         EDROP_IE        = 0x00008000,   /* interrupt enable: envelope drop */
105         BANK_B_EN       = 0x00010000,   /* SiS: enable bank B (64 channels) */
106         PCMIN_B_MIX     = 0x00020000,   /* SiS: PCM IN B mixing enable */
107         I2S_OUT_ASSIGN  = 0x00040000,   /* SiS: I2S Out contains surround PCM */
108         SPDIF_OUT_ASSIGN= 0x00080000,   /* SiS: 0=S/PDIF L/R | 1=PCM Out FIFO */
109         MAIN_OUT_ASSIGN = 0x00100000,   /* SiS: 0=PCM Out FIFO | 1=MMC Out buffer */
110 };
111
112 enum miscint_bits {
113         PB_UNDERRUN_IRQ = 0x00000001, REC_OVERRUN_IRQ = 0x00000002,
114         SB_IRQ          = 0x00000004, MPU401_IRQ      = 0x00000008,
115         OPL3_IRQ        = 0x00000010, ADDRESS_IRQ     = 0x00000020,
116         ENVELOPE_IRQ    = 0x00000040, PB_UNDERRUN     = 0x00000100,
117         REC_OVERRUN     = 0x00000200, MIXER_UNDERFLOW = 0x00000400,
118         MIXER_OVERFLOW  = 0x00000800, NX_SB_IRQ_DISABLE = 0x00001000,
119         ST_TARGET_REACHED = 0x00008000,
120         PB_24K_MODE     = 0x00010000, ST_IRQ_EN       = 0x00800000,
121         ACGPIO_IRQ      = 0x01000000
122 };
123
124 /* T2 legacy dma control registers. */
125 #define LEGACY_DMAR0                0x00  // ADR0
126 #define LEGACY_DMAR4                0x04  // CNT0
127 #define LEGACY_DMAR6                0x06  // CNT0 - High bits
128 #define LEGACY_DMAR11               0x0b  // MOD 
129 #define LEGACY_DMAR15               0x0f  // MMR 
130
131 #define T4D_START_A                  0x80
132 #define T4D_STOP_A                   0x84
133 #define T4D_DLY_A                    0x88
134 #define T4D_SIGN_CSO_A               0x8c
135 #define T4D_CSPF_A                   0x90
136 #define T4D_CSPF_B                   0xbc
137 #define T4D_CEBC_A                   0x94
138 #define T4D_AINT_A                   0x98
139 #define T4D_AINTEN_A                 0x9c
140 #define T4D_LFO_GC_CIR               0xa0
141 #define T4D_MUSICVOL_WAVEVOL         0xa8
142 #define T4D_SBDELTA_DELTA_R          0xac
143 #define T4D_MISCINT                  0xb0
144 #define T4D_START_B                  0xb4
145 #define T4D_STOP_B                   0xb8
146 #define T4D_SBBL_SBCL                0xc0
147 #define T4D_SBCTRL_SBE2R_SBDD        0xc4
148 #define T4D_STIMER                   0xc8
149 #define T4D_AINT_B                   0xd8
150 #define T4D_AINTEN_B                 0xdc
151 #define T4D_RCI                      0x70
152
153 /* MPU-401 UART */
154 #define T4D_MPU401_BASE             0x20
155 #define T4D_MPUR0                   0x20
156 #define T4D_MPUR1                   0x21
157 #define T4D_MPUR2                   0x22
158 #define T4D_MPUR3                   0x23
159
160 /* S/PDIF Registers */
161 #define NX_SPCTRL_SPCSO             0x24
162 #define NX_SPLBA                    0x28
163 #define NX_SPESO                    0x2c
164 #define NX_SPCSTATUS                0x64
165
166 /* Joystick */
167 #define GAMEPORT_GCR                0x30
168 #define GAMEPORT_MODE_ADC           0x80
169 #define GAMEPORT_LEGACY             0x31
170 #define GAMEPORT_AXES               0x34
171
172 /* NX Specific Registers */
173 #define NX_TLBC                     0x6c
174
175 /* Channel Registers */
176
177 #define CH_START                    0xe0
178
179 #define CH_DX_CSO_ALPHA_FMS         0xe0
180 #define CH_DX_ESO_DELTA             0xe8
181 #define CH_DX_FMC_RVOL_CVOL         0xec
182
183 #define CH_NX_DELTA_CSO             0xe0
184 #define CH_NX_DELTA_ESO             0xe8
185 #define CH_NX_ALPHA_FMS_FMC_RVOL_CVOL 0xec
186
187 #define CH_LBA                      0xe4
188 #define CH_GVSEL_PAN_VOL_CTRL_EC    0xf0
189 #define CH_EBUF1                    0xf4
190 #define CH_EBUF2                    0xf8
191
192 /* AC-97 Registers */
193
194 #define DX_ACR0_AC97_W              0x40
195 #define DX_ACR1_AC97_R              0x44
196 #define DX_ACR2_AC97_COM_STAT       0x48
197
198 #define NX_ACR0_AC97_COM_STAT       0x40
199 #define NX_ACR1_AC97_W              0x44
200 #define NX_ACR2_AC97_R_PRIMARY      0x48
201 #define NX_ACR3_AC97_R_SECONDARY    0x4c
202
203 #define SI_AC97_WRITE               0x40
204 #define SI_AC97_READ                0x44
205 #define SI_SERIAL_INTF_CTRL         0x48
206 #define SI_AC97_GPIO                0x4c
207 #define SI_ASR0                     0x50
208 #define SI_SPDIF_CS                 0x70
209 #define SI_GPIO                     0x7c
210
211 enum trident_nx_ac97_bits {
212         /* ACR1-3 */
213         NX_AC97_BUSY_WRITE      = 0x0800,
214         NX_AC97_BUSY_READ       = 0x0800,
215         NX_AC97_BUSY_DATA       = 0x0400,
216         NX_AC97_WRITE_SECONDARY = 0x0100,
217         /* ACR0 */
218         NX_AC97_SECONDARY_READY = 0x0040,
219         NX_AC97_SECONDARY_RECORD = 0x0020,
220         NX_AC97_SURROUND_OUTPUT = 0x0010,
221         NX_AC97_PRIMARY_READY   = 0x0008,
222         NX_AC97_PRIMARY_RECORD  = 0x0004,
223         NX_AC97_PCM_OUTPUT      = 0x0002,
224         NX_AC97_WARM_RESET      = 0x0001
225 };
226
227 enum trident_dx_ac97_bits {
228         DX_AC97_BUSY_WRITE      = 0x8000,
229         DX_AC97_BUSY_READ       = 0x8000,
230         DX_AC97_READY           = 0x0010,
231         DX_AC97_RECORD          = 0x0008,
232         DX_AC97_PLAYBACK        = 0x0002
233 };
234
235 enum sis7018_ac97_bits {
236         SI_AC97_BUSY_WRITE =    0x00008000,
237         SI_AC97_AUDIO_BUSY =    0x00004000,
238         SI_AC97_MODEM_BUSY =    0x00002000,
239         SI_AC97_BUSY_READ =     0x00008000,
240         SI_AC97_SECONDARY =     0x00000080,
241 };
242
243 enum serial_intf_ctrl_bits {
244         WARM_RESET      = 0x00000001,
245         COLD_RESET      = 0x00000002,
246         I2S_CLOCK       = 0x00000004,
247         PCM_SEC_AC97    = 0x00000008,
248         AC97_DBL_RATE   = 0x00000010,
249         SPDIF_EN        = 0x00000020,
250         I2S_OUTPUT_EN   = 0x00000040,
251         I2S_INPUT_EN    = 0x00000080,
252         PCMIN           = 0x00000100,
253         LINE1IN         = 0x00000200,
254         MICIN           = 0x00000400,
255         LINE2IN         = 0x00000800,
256         HEAD_SET_IN     = 0x00001000,
257         GPIOIN          = 0x00002000,
258         /* 7018 spec says id = 01 but the demo board routed to 10
259            SECONDARY_ID= 0x00004000, */
260         SECONDARY_ID    = 0x00004000,
261         PCMOUT          = 0x00010000,
262         SURROUT         = 0x00020000,
263         CENTEROUT       = 0x00040000,
264         LFEOUT          = 0x00080000,
265         LINE1OUT        = 0x00100000,
266         LINE2OUT        = 0x00200000,
267         GPIOOUT         = 0x00400000,
268         SI_AC97_PRIMARY_READY = 0x01000000,
269         SI_AC97_SECONDARY_READY = 0x02000000,
270         SI_AC97_POWERDOWN = 0x04000000,
271 };
272                                                                                                                                    
273 /* PCM defaults */
274
275 #define T4D_DEFAULT_PCM_VOL     10      /* 0 - 255 */
276 #define T4D_DEFAULT_PCM_PAN     0       /* 0 - 127 */
277 #define T4D_DEFAULT_PCM_RVOL    127     /* 0 - 127 */
278 #define T4D_DEFAULT_PCM_CVOL    127     /* 0 - 127 */
279
280 typedef struct _snd_trident trident_t;
281 typedef struct _snd_trident_voice snd_trident_voice_t;
282 typedef struct _snd_trident_pcm_mixer snd_trident_pcm_mixer_t;
283
284 typedef struct {
285         void (*sample_start)(trident_t *gus, snd_trident_voice_t *voice, snd_seq_position_t position);
286         void (*sample_stop)(trident_t *gus, snd_trident_voice_t *voice, snd_seq_stop_mode_t mode);
287         void (*sample_freq)(trident_t *gus, snd_trident_voice_t *voice, snd_seq_frequency_t freq);
288         void (*sample_volume)(trident_t *gus, snd_trident_voice_t *voice, snd_seq_ev_volume_t *volume);
289         void (*sample_loop)(trident_t *card, snd_trident_voice_t *voice, snd_seq_ev_loop_t *loop);
290         void (*sample_pos)(trident_t *card, snd_trident_voice_t *voice, snd_seq_position_t position);
291         void (*sample_private1)(trident_t *card, snd_trident_voice_t *voice, unsigned char *data);
292 } snd_trident_sample_ops_t;
293
294 typedef struct {
295         snd_midi_channel_set_t * chset;
296         trident_t * trident;
297         int mode;               /* operation mode */
298         int client;             /* sequencer client number */
299         int port;               /* sequencer port number */
300         int midi_has_voices: 1;
301 } snd_trident_port_t;
302
303 typedef struct snd_trident_memblk_arg {
304         short first_page, last_page;
305 } snd_trident_memblk_arg_t;
306
307 typedef struct {
308         unsigned int * entries;         /* 16k-aligned TLB table */
309         dma_addr_t entries_dmaaddr;     /* 16k-aligned PCI address to TLB table */
310         unsigned long * shadow_entries; /* shadow entries with virtual addresses */
311         struct snd_dma_buffer buffer;
312         snd_util_memhdr_t * memhdr;     /* page allocation list */
313         struct snd_dma_buffer silent_page;
314 } snd_trident_tlb_t;
315
316 struct _snd_trident_voice {
317         unsigned int number;
318         int use: 1,
319             pcm: 1,
320             synth:1,
321             midi: 1;
322         unsigned int flags;
323         unsigned char client;
324         unsigned char port;
325         unsigned char index;
326
327         snd_seq_instr_t instr;
328         snd_trident_sample_ops_t *sample_ops;
329
330         /* channel parameters */
331         unsigned int CSO;               /* 24 bits (16 on DX) */
332         unsigned int ESO;               /* 24 bits (16 on DX) */
333         unsigned int LBA;               /* 30 bits */
334         unsigned short EC;              /* 12 bits */
335         unsigned short Alpha;           /* 12 bits */
336         unsigned short Delta;           /* 16 bits */
337         unsigned short Attribute;       /* 16 bits - SiS 7018 */
338         unsigned short Vol;             /* 12 bits (6.6) */
339         unsigned char Pan;              /* 7 bits (1.4.2) */
340         unsigned char GVSel;            /* 1 bit */
341         unsigned char RVol;             /* 7 bits (5.2) */
342         unsigned char CVol;             /* 7 bits (5.2) */
343         unsigned char FMC;              /* 2 bits */
344         unsigned char CTRL;             /* 4 bits */
345         unsigned char FMS;              /* 4 bits */
346         unsigned char LFO;              /* 8 bits */
347
348         unsigned int negCSO;    /* nonzero - use negative CSO */
349
350         snd_util_memblk_t *memblk;      /* memory block if TLB enabled */
351
352         /* PCM data */
353
354         trident_t *trident;
355         snd_pcm_substream_t *substream;
356         snd_trident_voice_t *extra;     /* extra PCM voice (acts as interrupt generator) */
357         int running: 1,
358             capture: 1,
359             spdif: 1,
360             foldback: 1,
361             isync: 1,
362             isync2: 1,
363             isync3: 1;
364         int foldback_chan;              /* foldback subdevice number */
365         unsigned int stimer;            /* global sample timer (to detect spurious interrupts) */
366         unsigned int spurious_threshold; /* spurious threshold */
367         unsigned int isync_mark;
368         unsigned int isync_max;
369         unsigned int isync_ESO;
370
371         /* --- */
372
373         void *private_data;
374         void (*private_free)(snd_trident_voice_t *voice);
375 };
376
377 struct _snd_4dwave {
378         int seq_client;
379
380         snd_trident_port_t seq_ports[4];
381         snd_simple_ops_t simple_ops;
382         snd_seq_kinstr_list_t *ilist;
383
384         snd_trident_voice_t voices[64]; 
385
386         int ChanSynthCount;             /* number of allocated synth channels */
387         int max_size;                   /* maximum synth memory size in bytes */
388         int current_size;               /* current allocated synth mem in bytes */
389 };
390
391 struct _snd_trident_pcm_mixer {
392         snd_trident_voice_t *voice;     /* active voice */
393         unsigned short vol;             /* front volume */
394         unsigned char pan;              /* pan control */
395         unsigned char rvol;             /* rear volume */
396         unsigned char cvol;             /* center volume */
397         unsigned char pad;
398 };
399
400 struct _snd_trident {
401         int irq;
402
403         unsigned int device;    /* device ID */
404
405         unsigned char  bDMAStart;
406
407         unsigned long port;
408         struct resource *res_port;
409         unsigned long midi_port;
410
411         unsigned int spurious_irq_count;
412         unsigned int spurious_irq_max_delta;
413
414         snd_trident_tlb_t tlb;  /* TLB entries for NX cards */
415
416         unsigned char spdif_ctrl;
417         unsigned char spdif_pcm_ctrl;
418         unsigned int spdif_bits;
419         unsigned int spdif_pcm_bits;
420         snd_kcontrol_t *spdif_pcm_ctl;  /* S/PDIF settings */
421         unsigned int ac97_ctrl;
422         
423         unsigned int ChanMap[2];        /* allocation map for hardware channels */
424         
425         int ChanPCM;                    /* max number of PCM channels */
426         int ChanPCMcnt;                 /* actual number of PCM channels */
427
428         unsigned int ac97_detect: 1;    /* 1 = AC97 in detection phase */
429         unsigned int in_suspend: 1;     /* 1 during suspend/resume */
430
431         struct _snd_4dwave synth;       /* synth specific variables */
432
433         spinlock_t event_lock;
434         spinlock_t voice_alloc;
435
436         struct snd_dma_device dma_dev;
437
438         struct pci_dev *pci;
439         snd_card_t *card;
440         snd_pcm_t *pcm;         /* ADC/DAC PCM */
441         snd_pcm_t *foldback;    /* Foldback PCM */
442         snd_pcm_t *spdif;       /* SPDIF PCM */
443         snd_rawmidi_t *rmidi;
444         snd_seq_device_t *seq_dev;
445
446         ac97_bus_t *ac97_bus;
447         ac97_t *ac97;
448         ac97_t *ac97_sec;
449
450         unsigned int musicvol_wavevol;
451         snd_trident_pcm_mixer_t pcm_mixer[32];
452         snd_kcontrol_t *ctl_vol;        /* front volume */
453         snd_kcontrol_t *ctl_pan;        /* pan */
454         snd_kcontrol_t *ctl_rvol;       /* rear volume */
455         snd_kcontrol_t *ctl_cvol;       /* center volume */
456
457         spinlock_t reg_lock;
458
459         struct snd_trident_gameport *gameport;
460 };
461
462 int snd_trident_create(snd_card_t * card,
463                        struct pci_dev *pci,
464                        int pcm_streams,
465                        int pcm_spdif_device,
466                        int max_wavetable_size,
467                        trident_t ** rtrident);
468 int snd_trident_free(trident_t *trident);
469 void snd_trident_gameport(trident_t *trident);
470
471 int snd_trident_pcm(trident_t * trident, int device, snd_pcm_t **rpcm);
472 int snd_trident_foldback_pcm(trident_t * trident, int device, snd_pcm_t **rpcm);
473 int snd_trident_spdif_pcm(trident_t * trident, int device, snd_pcm_t **rpcm);
474 int snd_trident_attach_synthesizer(trident_t * trident);
475 int snd_trident_detach_synthesizer(trident_t * trident);
476 snd_trident_voice_t *snd_trident_alloc_voice(trident_t * trident, int type, int client, int port);
477 void snd_trident_free_voice(trident_t * trident, snd_trident_voice_t *voice);
478 void snd_trident_start_voice(trident_t * trident, unsigned int voice);
479 void snd_trident_stop_voice(trident_t * trident, unsigned int voice);
480 void snd_trident_write_voice_regs(trident_t * trident, snd_trident_voice_t *voice);
481 void snd_trident_clear_voices(trident_t * trident, unsigned short v_min, unsigned short v_max);
482
483 /* TLB memory allocation */
484 snd_util_memblk_t *snd_trident_alloc_pages(trident_t *trident, snd_pcm_substream_t *substream);
485 int snd_trident_free_pages(trident_t *trident, snd_util_memblk_t *blk);
486 snd_util_memblk_t *snd_trident_synth_alloc(trident_t *trident, unsigned int size);
487 int snd_trident_synth_free(trident_t *trident, snd_util_memblk_t *blk);
488 int snd_trident_synth_bzero(trident_t *trident, snd_util_memblk_t *blk, int offset, int size);
489 int snd_trident_synth_copy_from_user(trident_t *trident, snd_util_memblk_t *blk, int offset, const char *data, int size);
490
491 /* Power Management */
492 #ifdef CONFIG_PM
493 void snd_trident_suspend(trident_t *trident);
494 void snd_trident_resume(trident_t *trident);
495 #endif
496
497 #endif /* __SOUND_TRIDENT_H */