1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 */
4
5#ifndef __SOUND_AU88X0_H
6#define __SOUND_AU88X0_H
7
8#include <linux/pci.h>
9#include <linux/io.h>
10#include <sound/core.h>
11#include <sound/pcm.h>
12#include <sound/rawmidi.h>
13#include <sound/mpu401.h>
14#include <sound/hwdep.h>
15#include <sound/ac97_codec.h>
16#include <sound/tlv.h>
17
18#ifndef CHIP_AU8820
19#include "au88x0_eq.h"
20#include "au88x0_a3d.h"
21#endif
22#ifndef CHIP_AU8810
23#include "au88x0_wt.h"
24#endif
25
26#define	hwread(x,y) readl((x)+(y))
27#define	hwwrite(x,y,z) writel((z),(x)+(y))
28
29/* Vortex MPU401 defines. */
30#define	MIDI_CLOCK_DIV		0x61
31/* Standart MPU401 defines. */
32#define	MPU401_RESET		0xff
33#define	MPU401_ENTER_UART	0x3f
34#define	MPU401_ACK		0xfe
35
36// Get src register value to convert from x to y.
37#define	SRC_RATIO(x,y)		((((x<<15)/y) + 1)/2)
38
39/* FIFO software state constants. */
40#define FIFO_STOP 0
41#define FIFO_START 1
42#define FIFO_PAUSE 2
43
44/* IRQ flags */
45#define IRQ_ERR_MASK	0x00ff
46#define IRQ_FATAL	0x0001
47#define IRQ_PARITY	0x0002
48#define IRQ_REG		0x0004
49#define IRQ_FIFO	0x0008
50#define IRQ_DMA		0x0010
51#define IRQ_PCMOUT	0x0020	/* PCM OUT page crossing */
52#define IRQ_TIMER	0x1000
53#define IRQ_MIDI	0x2000
54#define IRQ_MODEM	0x4000
55
56/* ADB Resource */
57#define VORTEX_RESOURCE_DMA	0x00000000
58#define VORTEX_RESOURCE_SRC	0x00000001
59#define VORTEX_RESOURCE_MIXIN	0x00000002
60#define VORTEX_RESOURCE_MIXOUT	0x00000003
61#define VORTEX_RESOURCE_A3D	0x00000004
62#define VORTEX_RESOURCE_LAST	0x00000005
63
64/* codec io: VORTEX_CODEC_IO bits */
65#define VORTEX_CODEC_ID_SHIFT	24
66#define VORTEX_CODEC_WRITE	0x00800000
67#define VORTEX_CODEC_ADDSHIFT 	16
68#define VORTEX_CODEC_ADDMASK	0x7f0000
69#define VORTEX_CODEC_DATSHIFT	0
70#define VORTEX_CODEC_DATMASK	0xffff
71
72/* Check for SDAC bit in "Extended audio ID" AC97 register */
73//#define VORTEX_IS_QUAD(x) (((x)->codec == NULL) ?  0 : ((x)->codec->ext_id&0x80))
74#define VORTEX_IS_QUAD(x) ((x)->isquad)
75/* Check if chip has bug. */
76#define IS_BAD_CHIP(x) (\
77	(x->rev == 0xfe && x->device == PCI_DEVICE_ID_AUREAL_VORTEX_2) || \
78	(x->rev == 0xfe && x->device == PCI_DEVICE_ID_AUREAL_ADVANTAGE))
79
80
81/* PCM devices */
82#define VORTEX_PCM_ADB		0
83#define VORTEX_PCM_SPDIF	1
84#define VORTEX_PCM_A3D		2
85#define VORTEX_PCM_WT		3
86#define VORTEX_PCM_I2S		4
87#define VORTEX_PCM_LAST		5
88
89#define MIX_CAPT(x) (vortex->mixcapt[x])
90#define MIX_PLAYB(x) (vortex->mixplayb[x])
91#define MIX_SPDIF(x) (vortex->mixspdif[x])
92
93#define NR_WTPB 0x20		/* WT channels per each bank. */
94#define NR_PCM	0x10
95
96struct pcm_vol {
97	struct snd_kcontrol *kctl;
98	int active;
99	int dma;
100	int mixin[4];
101	int vol[4];
102};
103
104/* Structs */
105typedef struct {
106	//int this_08;          /* Still unknown */
107	int fifo_enabled;	/* this_24 */
108	int fifo_status;	/* this_1c */
109	u32 dma_ctrl;		/* this_78 (ADB), this_7c (WT) */
110	int dma_unknown;	/* this_74 (ADB), this_78 (WT). WDM: +8 */
111	int cfg0;
112	int cfg1;
113
114	int nr_ch;		/* Nr of PCM channels in use */
115	int type;		/* Output type (ac97, a3d, spdif, i2s, dsp) */
116	int dma;		/* Hardware DMA index. */
117	int dir;		/* Stream Direction. */
118	u32 resources[5];
119
120	/* Virtual page extender stuff */
121	int nr_periods;
122	int period_bytes;
123	int period_real;
124	int period_virt;
125
126	struct snd_pcm_substream *substream;
127} stream_t;
128
129typedef struct snd_vortex vortex_t;
130struct snd_vortex {
131	/* ALSA structs. */
132	struct snd_card *card;
133	struct snd_pcm *pcm[VORTEX_PCM_LAST];
134
135	struct snd_rawmidi *rmidi;	/* Legacy Midi interface. */
136	struct snd_ac97 *codec;
137
138	/* Stream structs. */
139	stream_t dma_adb[NR_ADB];
140	int spdif_sr;
141#ifndef CHIP_AU8810
142	stream_t dma_wt[NR_WT];
143	wt_voice_t wt_voice[NR_WT];	/* WT register cache. */
144	s8 mixwt[(NR_WT / NR_WTPB) * 6];	/* WT mixin objects */
145#endif
146
147	/* Global resources */
148	s8 mixcapt[2];
149	s8 mixplayb[4];
150#ifndef CHIP_AU8820
151	s8 mixspdif[2];
152	s8 mixa3d[2];	/* mixers which collect all a3d streams. */
153	s8 mixxtlk[2];	/* crosstalk canceler mixer inputs. */
154#endif
155	u32 fixed_res[5];
156
157#ifndef CHIP_AU8820
158	/* Hardware equalizer structs */
159	eqlzr_t eq;
160	/* A3D structs */
161	a3dsrc_t a3d[NR_A3D];
162	/* Xtalk canceler */
163	int xt_mode;		/* 1: speakers, 0:headphones. */
164#endif
165	struct pcm_vol pcm_vol[NR_PCM];
166
167	int isquad;		/* cache of extended ID codec flag. */
168
169	/* Gameport stuff. */
170	struct gameport *gameport;
171
172	/* PCI hardware resources */
173	unsigned long io;
174	void __iomem *mmio;
175	unsigned int irq;
176	spinlock_t lock;
177
178	/* PCI device */
179	struct pci_dev *pci_dev;
180	u16 vendor;
181	u16 device;
182	u8 rev;
183};
184
185/* Functions. */
186
187/* SRC */
188static void vortex_adb_setsrc(vortex_t * vortex, int adbdma,
189			      unsigned int cvrt, int dir);
190
191/* DMA Engines. */
192static void vortex_adbdma_setbuffers(vortex_t * vortex, int adbdma,
193				     int size, int count);
194static void vortex_adbdma_setmode(vortex_t * vortex, int adbdma, int ie,
195				  int dir, int fmt, int d,
196				  u32 offset);
197static void vortex_adbdma_setstartbuffer(vortex_t * vortex, int adbdma, int sb);
198#ifndef CHIP_AU8810
199static void vortex_wtdma_setbuffers(vortex_t * vortex, int wtdma,
200				    int size, int count);
201static void vortex_wtdma_setmode(vortex_t * vortex, int wtdma, int ie, int fmt, int d,	/*int e, */
202				 u32 offset);
203static void vortex_wtdma_setstartbuffer(vortex_t * vortex, int wtdma, int sb);
204#endif
205
206static void vortex_adbdma_startfifo(vortex_t * vortex, int adbdma);
207//static void vortex_adbdma_stopfifo(vortex_t *vortex, int adbdma);
208static void vortex_adbdma_pausefifo(vortex_t * vortex, int adbdma);
209static void vortex_adbdma_resumefifo(vortex_t * vortex, int adbdma);
210static inline int vortex_adbdma_getlinearpos(vortex_t * vortex, int adbdma);
211static void vortex_adbdma_resetup(vortex_t *vortex, int adbdma);
212
213#ifndef CHIP_AU8810
214static void vortex_wtdma_startfifo(vortex_t * vortex, int wtdma);
215static void vortex_wtdma_stopfifo(vortex_t * vortex, int wtdma);
216static void vortex_wtdma_pausefifo(vortex_t * vortex, int wtdma);
217static void vortex_wtdma_resumefifo(vortex_t * vortex, int wtdma);
218static inline int vortex_wtdma_getlinearpos(vortex_t * vortex, int wtdma);
219#endif
220
221/* global stuff. */
222static void vortex_codec_init(vortex_t * vortex);
223static void vortex_codec_write(struct snd_ac97 * codec, unsigned short addr,
224			       unsigned short data);
225static unsigned short vortex_codec_read(struct snd_ac97 * codec, unsigned short addr);
226static void vortex_spdif_init(vortex_t * vortex, int spdif_sr, int spdif_mode);
227
228static int vortex_core_init(vortex_t * card);
229static int vortex_core_shutdown(vortex_t * card);
230static void vortex_enable_int(vortex_t * card);
231static irqreturn_t vortex_interrupt(int irq, void *dev_id);
232static int vortex_alsafmt_aspfmt(snd_pcm_format_t alsafmt, vortex_t *v);
233
234/* Connection  stuff. */
235static void vortex_connect_default(vortex_t * vortex, int en);
236static int vortex_adb_allocroute(vortex_t * vortex, int dma, int nr_ch,
237				 int dir, int type, int subdev);
238static int vortex_adb_checkinout(vortex_t * vortex, int resmap[], int out,
239				 int restype);
240#ifndef CHIP_AU8810
241static int vortex_wt_allocroute(vortex_t * vortex, int dma, int nr_ch);
242static void vortex_wt_connect(vortex_t * vortex, int en);
243static void vortex_wt_init(vortex_t * vortex);
244#endif
245
246static void vortex_route(vortex_t * vortex, int en, unsigned char channel,
247			 unsigned char source, unsigned char dest);
248#if 0
249static void vortex_routes(vortex_t * vortex, int en, unsigned char channel,
250			  unsigned char source, unsigned char dest0,
251			  unsigned char dest1);
252#endif
253static void vortex_connection_mixin_mix(vortex_t * vortex, int en,
254					unsigned char mixin,
255					unsigned char mix, int a);
256static void vortex_mix_setinputvolumebyte(vortex_t * vortex,
257					  unsigned char mix, int mixin,
258					  unsigned char vol);
259static void vortex_mix_setvolumebyte(vortex_t * vortex, unsigned char mix,
260				     unsigned char vol);
261
262/* A3D functions. */
263#ifndef CHIP_AU8820
264static void vortex_Vort3D_enable(vortex_t * v);
265static void vortex_Vort3D_disable(vortex_t * v);
266static void vortex_Vort3D_connect(vortex_t * vortex, int en);
267static void vortex_Vort3D_InitializeSource(a3dsrc_t *a, int en, vortex_t *v);
268#endif
269
270/* Driver stuff. */
271static int vortex_gameport_register(vortex_t * card);
272static void vortex_gameport_unregister(vortex_t * card);
273#ifndef CHIP_AU8820
274static int vortex_eq_init(vortex_t * vortex);
275static int vortex_eq_free(vortex_t * vortex);
276#endif
277/* ALSA stuff. */
278static int snd_vortex_new_pcm(vortex_t * vortex, int idx, int nr);
279static int snd_vortex_mixer(vortex_t * vortex);
280static int snd_vortex_midi(vortex_t * vortex);
281#endif
282