xref: /linux/sound/pci/via82xx.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   ALSA driver for VIA VT82xx (South Bridge)
4  *
5  *   VT82C686A/B/C, VT8233A/C, VT8235
6  *
7  *	Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
8  *	                   Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
9  *                    2002 Takashi Iwai <tiwai@suse.de>
10  */
11 
12 /*
13  * Changes:
14  *
15  * Dec. 19, 2002	Takashi Iwai <tiwai@suse.de>
16  *	- use the DSX channels for the first pcm playback.
17  *	  (on VIA8233, 8233C and 8235 only)
18  *	  this will allow you play simultaneously up to 4 streams.
19  *	  multi-channel playback is assigned to the second device
20  *	  on these chips.
21  *	- support the secondary capture (on VIA8233/C,8235)
22  *	- SPDIF support
23  *	  the DSX3 channel can be used for SPDIF output.
24  *	  on VIA8233A, this channel is assigned to the second pcm
25  *	  playback.
26  *	  the card config of alsa-lib will assign the correct
27  *	  device for applications.
28  *	- clean up the code, separate low-level initialization
29  *	  routines for each chipset.
30  *
31  * Sep. 26, 2005	Karsten Wiese <annabellesgarden@yahoo.de>
32  *	- Optimize position calculation for the 823x chips.
33  */
34 
35 #include <linux/io.h>
36 #include <linux/delay.h>
37 #include <linux/interrupt.h>
38 #include <linux/init.h>
39 #include <linux/pci.h>
40 #include <linux/slab.h>
41 #include <linux/gameport.h>
42 #include <linux/module.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/info.h>
47 #include <sound/tlv.h>
48 #include <sound/ac97_codec.h>
49 #include <sound/mpu401.h>
50 #include <sound/initval.h>
51 
52 #if 0
53 #define POINTER_DEBUG
54 #endif
55 
56 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
57 MODULE_DESCRIPTION("VIA VT82xx audio");
58 MODULE_LICENSE("GPL");
59 
60 #if IS_REACHABLE(CONFIG_GAMEPORT)
61 #define SUPPORT_JOYSTICK 1
62 #endif
63 
64 static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
65 static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
66 static long mpu_port;
67 #ifdef SUPPORT_JOYSTICK
68 static bool joystick;
69 #endif
70 static int ac97_clock = 48000;
71 static char *ac97_quirk;
72 static int dxs_support;
73 static int dxs_init_volume = 31;
74 static int nodelay;
75 
76 module_param(index, int, 0444);
77 MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
78 module_param(id, charp, 0444);
79 MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
80 module_param_hw(mpu_port, long, ioport, 0444);
81 MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
82 #ifdef SUPPORT_JOYSTICK
83 module_param(joystick, bool, 0444);
84 MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
85 #endif
86 module_param(ac97_clock, int, 0444);
87 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
88 module_param(ac97_quirk, charp, 0444);
89 MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
90 module_param(dxs_support, int, 0444);
91 MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA, 5 = enable any sample rate)");
92 module_param(dxs_init_volume, int, 0644);
93 MODULE_PARM_DESC(dxs_init_volume, "initial DXS volume (0-31)");
94 module_param(nodelay, int, 0444);
95 MODULE_PARM_DESC(nodelay, "Disable 500ms init delay");
96 
97 /* just for backward compatibility */
98 static bool enable;
99 module_param(enable, bool, 0444);
100 
101 
102 /* revision numbers for via686 */
103 #define VIA_REV_686_A		0x10
104 #define VIA_REV_686_B		0x11
105 #define VIA_REV_686_C		0x12
106 #define VIA_REV_686_D		0x13
107 #define VIA_REV_686_E		0x14
108 #define VIA_REV_686_H		0x20
109 
110 /* revision numbers for via8233 */
111 #define VIA_REV_PRE_8233	0x10	/* not in market */
112 #define VIA_REV_8233C		0x20	/* 2 rec, 4 pb, 1 multi-pb */
113 #define VIA_REV_8233		0x30	/* 2 rec, 4 pb, 1 multi-pb, spdif */
114 #define VIA_REV_8233A		0x40	/* 1 rec, 1 multi-pb, spdf */
115 #define VIA_REV_8235		0x50	/* 2 rec, 4 pb, 1 multi-pb, spdif */
116 #define VIA_REV_8237		0x60
117 #define VIA_REV_8251		0x70
118 
119 /*
120  *  Direct registers
121  */
122 
123 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
124 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
125 
126 /* common offsets */
127 #define VIA_REG_OFFSET_STATUS		0x00	/* byte - channel status */
128 #define   VIA_REG_STAT_ACTIVE		0x80	/* RO */
129 #define   VIA8233_SHADOW_STAT_ACTIVE	0x08	/* RO */
130 #define   VIA_REG_STAT_PAUSED		0x40	/* RO */
131 #define   VIA_REG_STAT_TRIGGER_QUEUED	0x08	/* RO */
132 #define   VIA_REG_STAT_STOPPED		0x04	/* RWC */
133 #define   VIA_REG_STAT_EOL		0x02	/* RWC */
134 #define   VIA_REG_STAT_FLAG		0x01	/* RWC */
135 #define VIA_REG_OFFSET_CONTROL		0x01	/* byte - channel control */
136 #define   VIA_REG_CTRL_START		0x80	/* WO */
137 #define   VIA_REG_CTRL_TERMINATE	0x40	/* WO */
138 #define   VIA_REG_CTRL_AUTOSTART	0x20
139 #define   VIA_REG_CTRL_PAUSE		0x08	/* RW */
140 #define   VIA_REG_CTRL_INT_STOP		0x04
141 #define   VIA_REG_CTRL_INT_EOL		0x02
142 #define   VIA_REG_CTRL_INT_FLAG		0x01
143 #define   VIA_REG_CTRL_RESET		0x01	/* RW - probably reset? undocumented */
144 #define   VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
145 #define VIA_REG_OFFSET_TYPE		0x02	/* byte - channel type (686 only) */
146 #define   VIA_REG_TYPE_AUTOSTART	0x80	/* RW - autostart at EOL */
147 #define   VIA_REG_TYPE_16BIT		0x20	/* RW */
148 #define   VIA_REG_TYPE_STEREO		0x10	/* RW */
149 #define   VIA_REG_TYPE_INT_LLINE	0x00
150 #define   VIA_REG_TYPE_INT_LSAMPLE	0x04
151 #define   VIA_REG_TYPE_INT_LESSONE	0x08
152 #define   VIA_REG_TYPE_INT_MASK		0x0c
153 #define   VIA_REG_TYPE_INT_EOL		0x02
154 #define   VIA_REG_TYPE_INT_FLAG		0x01
155 #define VIA_REG_OFFSET_TABLE_PTR	0x04	/* dword - channel table pointer */
156 #define VIA_REG_OFFSET_CURR_PTR		0x04	/* dword - channel current pointer */
157 #define VIA_REG_OFFSET_STOP_IDX		0x08	/* dword - stop index, channel type, sample rate */
158 #define   VIA8233_REG_TYPE_16BIT	0x00200000	/* RW */
159 #define   VIA8233_REG_TYPE_STEREO	0x00100000	/* RW */
160 #define VIA_REG_OFFSET_CURR_COUNT	0x0c	/* dword - channel current count (24 bit) */
161 #define VIA_REG_OFFSET_CURR_INDEX	0x0f	/* byte - channel current index (for via8233 only) */
162 
163 #define DEFINE_VIA_REGSET(name,val) \
164 enum {\
165 	VIA_REG_##name##_STATUS		= (val),\
166 	VIA_REG_##name##_CONTROL	= (val) + 0x01,\
167 	VIA_REG_##name##_TYPE		= (val) + 0x02,\
168 	VIA_REG_##name##_TABLE_PTR	= (val) + 0x04,\
169 	VIA_REG_##name##_CURR_PTR	= (val) + 0x04,\
170 	VIA_REG_##name##_STOP_IDX	= (val) + 0x08,\
171 	VIA_REG_##name##_CURR_COUNT	= (val) + 0x0c,\
172 }
173 
174 /* playback block */
175 DEFINE_VIA_REGSET(PLAYBACK, 0x00);
176 DEFINE_VIA_REGSET(CAPTURE, 0x10);
177 DEFINE_VIA_REGSET(FM, 0x20);
178 
179 /* AC'97 */
180 #define VIA_REG_AC97			0x80	/* dword */
181 #define   VIA_REG_AC97_CODEC_ID_MASK	(3<<30)
182 #define   VIA_REG_AC97_CODEC_ID_SHIFT	30
183 #define   VIA_REG_AC97_CODEC_ID_PRIMARY	0x00
184 #define   VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
185 #define   VIA_REG_AC97_SECONDARY_VALID	(1<<27)
186 #define   VIA_REG_AC97_PRIMARY_VALID	(1<<25)
187 #define   VIA_REG_AC97_BUSY		(1<<24)
188 #define   VIA_REG_AC97_READ		(1<<23)
189 #define   VIA_REG_AC97_CMD_SHIFT	16
190 #define   VIA_REG_AC97_CMD_MASK		0x7e
191 #define   VIA_REG_AC97_DATA_SHIFT	0
192 #define   VIA_REG_AC97_DATA_MASK	0xffff
193 
194 #define VIA_REG_SGD_SHADOW		0x84	/* dword */
195 /* via686 */
196 #define   VIA_REG_SGD_STAT_PB_FLAG	(1<<0)
197 #define   VIA_REG_SGD_STAT_CP_FLAG	(1<<1)
198 #define   VIA_REG_SGD_STAT_FM_FLAG	(1<<2)
199 #define   VIA_REG_SGD_STAT_PB_EOL	(1<<4)
200 #define   VIA_REG_SGD_STAT_CP_EOL	(1<<5)
201 #define   VIA_REG_SGD_STAT_FM_EOL	(1<<6)
202 #define   VIA_REG_SGD_STAT_PB_STOP	(1<<8)
203 #define   VIA_REG_SGD_STAT_CP_STOP	(1<<9)
204 #define   VIA_REG_SGD_STAT_FM_STOP	(1<<10)
205 #define   VIA_REG_SGD_STAT_PB_ACTIVE	(1<<12)
206 #define   VIA_REG_SGD_STAT_CP_ACTIVE	(1<<13)
207 #define   VIA_REG_SGD_STAT_FM_ACTIVE	(1<<14)
208 /* via8233 */
209 #define   VIA8233_REG_SGD_STAT_FLAG	(1<<0)
210 #define   VIA8233_REG_SGD_STAT_EOL	(1<<1)
211 #define   VIA8233_REG_SGD_STAT_STOP	(1<<2)
212 #define   VIA8233_REG_SGD_STAT_ACTIVE	(1<<3)
213 #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
214 #define   VIA8233_REG_SGD_CHAN_SDX	0
215 #define   VIA8233_REG_SGD_CHAN_MULTI	4
216 #define   VIA8233_REG_SGD_CHAN_REC	6
217 #define   VIA8233_REG_SGD_CHAN_REC1	7
218 
219 #define VIA_REG_GPI_STATUS		0x88
220 #define VIA_REG_GPI_INTR		0x8c
221 
222 /* multi-channel and capture registers for via8233 */
223 DEFINE_VIA_REGSET(MULTPLAY, 0x40);
224 DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
225 
226 /* via8233-specific registers */
227 #define VIA_REG_OFS_PLAYBACK_VOLUME_L	0x02	/* byte */
228 #define VIA_REG_OFS_PLAYBACK_VOLUME_R	0x03	/* byte */
229 #define VIA_REG_OFS_MULTPLAY_FORMAT	0x02	/* byte - format and channels */
230 #define   VIA_REG_MULTPLAY_FMT_8BIT	0x00
231 #define   VIA_REG_MULTPLAY_FMT_16BIT	0x80
232 #define   VIA_REG_MULTPLAY_FMT_CH_MASK	0x70	/* # channels << 4 (valid = 1,2,4,6) */
233 #define VIA_REG_OFS_CAPTURE_FIFO	0x02	/* byte - bit 6 = fifo  enable */
234 #define   VIA_REG_CAPTURE_FIFO_ENABLE	0x40
235 
236 #define VIA_DXS_MAX_VOLUME		31	/* max. volume (attenuation) of reg 0x32/33 */
237 
238 #define VIA_REG_CAPTURE_CHANNEL		0x63	/* byte - input select */
239 #define   VIA_REG_CAPTURE_CHANNEL_MIC	0x4
240 #define   VIA_REG_CAPTURE_CHANNEL_LINE	0
241 #define   VIA_REG_CAPTURE_SELECT_CODEC	0x03	/* recording source codec (0 = primary) */
242 
243 #define VIA_TBL_BIT_FLAG	0x40000000
244 #define VIA_TBL_BIT_EOL		0x80000000
245 
246 /* pci space */
247 #define VIA_ACLINK_STAT		0x40
248 #define  VIA_ACLINK_C11_READY	0x20
249 #define  VIA_ACLINK_C10_READY	0x10
250 #define  VIA_ACLINK_C01_READY	0x04 /* secondary codec ready */
251 #define  VIA_ACLINK_LOWPOWER	0x02 /* low-power state */
252 #define  VIA_ACLINK_C00_READY	0x01 /* primary codec ready */
253 #define VIA_ACLINK_CTRL		0x41
254 #define  VIA_ACLINK_CTRL_ENABLE	0x80 /* 0: disable, 1: enable */
255 #define  VIA_ACLINK_CTRL_RESET	0x40 /* 0: assert, 1: de-assert */
256 #define  VIA_ACLINK_CTRL_SYNC	0x20 /* 0: release SYNC, 1: force SYNC hi */
257 #define  VIA_ACLINK_CTRL_SDO	0x10 /* 0: release SDO, 1: force SDO hi */
258 #define  VIA_ACLINK_CTRL_VRA	0x08 /* 0: disable VRA, 1: enable VRA */
259 #define  VIA_ACLINK_CTRL_PCM	0x04 /* 0: disable PCM, 1: enable PCM */
260 #define  VIA_ACLINK_CTRL_FM	0x02 /* via686 only */
261 #define  VIA_ACLINK_CTRL_SB	0x01 /* via686 only */
262 #define  VIA_ACLINK_CTRL_INIT	(VIA_ACLINK_CTRL_ENABLE|\
263 				 VIA_ACLINK_CTRL_RESET|\
264 				 VIA_ACLINK_CTRL_PCM|\
265 				 VIA_ACLINK_CTRL_VRA)
266 #define VIA_FUNC_ENABLE		0x42
267 #define  VIA_FUNC_MIDI_PNP	0x80 /* FIXME: it's 0x40 in the datasheet! */
268 #define  VIA_FUNC_MIDI_IRQMASK	0x40 /* FIXME: not documented! */
269 #define  VIA_FUNC_RX2C_WRITE	0x20
270 #define  VIA_FUNC_SB_FIFO_EMPTY	0x10
271 #define  VIA_FUNC_ENABLE_GAME	0x08
272 #define  VIA_FUNC_ENABLE_FM	0x04
273 #define  VIA_FUNC_ENABLE_MIDI	0x02
274 #define  VIA_FUNC_ENABLE_SB	0x01
275 #define VIA_PNP_CONTROL		0x43
276 #define VIA_FM_NMI_CTRL		0x48
277 #define VIA8233_VOLCHG_CTRL	0x48
278 #define VIA8233_SPDIF_CTRL	0x49
279 #define  VIA8233_SPDIF_DX3	0x08
280 #define  VIA8233_SPDIF_SLOT_MASK	0x03
281 #define  VIA8233_SPDIF_SLOT_1011	0x00
282 #define  VIA8233_SPDIF_SLOT_34		0x01
283 #define  VIA8233_SPDIF_SLOT_78		0x02
284 #define  VIA8233_SPDIF_SLOT_69		0x03
285 
286 /*
287  */
288 
289 #define VIA_DXS_AUTO	0
290 #define VIA_DXS_ENABLE	1
291 #define VIA_DXS_DISABLE	2
292 #define VIA_DXS_48K	3
293 #define VIA_DXS_NO_VRA	4
294 #define VIA_DXS_SRC	5
295 
296 
297 /*
298  * pcm stream
299  */
300 
301 struct snd_via_sg_table {
302 	unsigned int offset;
303 	unsigned int size;
304 } ;
305 
306 #define VIA_TABLE_SIZE	255
307 #define VIA_MAX_BUFSIZE	(1<<24)
308 
309 struct viadev {
310 	unsigned int reg_offset;
311 	unsigned long port;
312 	int direction;	/* playback = 0, capture = 1 */
313         struct snd_pcm_substream *substream;
314 	int running;
315 	unsigned int tbl_entries; /* # descriptors */
316 	struct snd_dma_buffer table;
317 	struct snd_via_sg_table *idx_table;
318 	/* for recovery from the unexpected pointer */
319 	unsigned int lastpos;
320 	unsigned int fragsize;
321 	unsigned int bufsize;
322 	unsigned int bufsize2;
323 	int hwptr_done;		/* processed frame position in the buffer */
324 	int in_interrupt;
325 	int shadow_shift;
326 };
327 
328 
329 enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
330 enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
331 
332 #define VIA_MAX_DEVS	7	/* 4 playback, 1 multi, 2 capture */
333 
334 struct via_rate_lock {
335 	spinlock_t lock;
336 	int rate;
337 	int used;
338 };
339 
340 struct via82xx {
341 	int irq;
342 
343 	unsigned long port;
344 	struct resource *mpu_res;
345 	int chip_type;
346 	unsigned char revision;
347 
348 	unsigned char old_legacy;
349 	unsigned char old_legacy_cfg;
350 	unsigned char legacy_saved;
351 	unsigned char legacy_cfg_saved;
352 	unsigned char spdif_ctrl_saved;
353 	unsigned char capture_src_saved[2];
354 	unsigned int mpu_port_saved;
355 
356 	unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
357 	unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
358 
359 	unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
360 
361 	struct pci_dev *pci;
362 	struct snd_card *card;
363 
364 	unsigned int num_devs;
365 	unsigned int playback_devno, multi_devno, capture_devno;
366 	struct viadev devs[VIA_MAX_DEVS];
367 	struct via_rate_lock rates[2]; /* playback and capture */
368 	unsigned int dxs_fixed: 1;	/* DXS channel accepts only 48kHz */
369 	unsigned int no_vra: 1;		/* no need to set VRA on DXS channels */
370 	unsigned int dxs_src: 1;	/* use full SRC capabilities of DXS */
371 	unsigned int spdif_on: 1;	/* only spdif rates work to external DACs */
372 
373 	struct snd_pcm *pcms[2];
374 	struct snd_rawmidi *rmidi;
375 	struct snd_kcontrol *dxs_controls[4];
376 
377 	struct snd_ac97_bus *ac97_bus;
378 	struct snd_ac97 *ac97;
379 	unsigned int ac97_clock;
380 	unsigned int ac97_secondary;	/* secondary AC'97 codec is present */
381 
382 	spinlock_t reg_lock;
383 	struct snd_info_entry *proc_entry;
384 
385 #ifdef SUPPORT_JOYSTICK
386 	struct gameport *gameport;
387 #endif
388 };
389 
390 static const struct pci_device_id snd_via82xx_ids[] = {
391 	/* 0x1106, 0x3058 */
392 	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C686_5), TYPE_CARD_VIA686, },	/* 686A */
393 	/* 0x1106, 0x3059 */
394 	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_8233_5), TYPE_CARD_VIA8233, },	/* VT8233 */
395 	{ 0, }
396 };
397 
398 MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
399 
400 /*
401  */
402 
403 /*
404  * allocate and initialize the descriptor buffers
405  * periods = number of periods
406  * fragsize = period size in bytes
407  */
408 static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
409 			   struct pci_dev *pci,
410 			   unsigned int periods, unsigned int fragsize)
411 {
412 	unsigned int i, idx, ofs, rest;
413 	struct via82xx *chip = snd_pcm_substream_chip(substream);
414 	__le32 *pgtbl;
415 
416 	if (dev->table.area == NULL) {
417 		/* the start of each lists must be aligned to 8 bytes,
418 		 * but the kernel pages are much bigger, so we don't care
419 		 */
420 		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
421 					PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
422 					&dev->table) < 0)
423 			return -ENOMEM;
424 	}
425 	if (! dev->idx_table) {
426 		dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
427 					       sizeof(*dev->idx_table),
428 					       GFP_KERNEL);
429 		if (! dev->idx_table)
430 			return -ENOMEM;
431 	}
432 
433 	/* fill the entries */
434 	idx = 0;
435 	ofs = 0;
436 	pgtbl = (__le32 *)dev->table.area;
437 	for (i = 0; i < periods; i++) {
438 		rest = fragsize;
439 		/* fill descriptors for a period.
440 		 * a period can be split to several descriptors if it's
441 		 * over page boundary.
442 		 */
443 		do {
444 			unsigned int r;
445 			unsigned int flag;
446 			unsigned int addr;
447 
448 			if (idx >= VIA_TABLE_SIZE) {
449 				dev_err(&pci->dev, "too much table size!\n");
450 				return -EINVAL;
451 			}
452 			addr = snd_pcm_sgbuf_get_addr(substream, ofs);
453 			pgtbl[idx << 1] = cpu_to_le32(addr);
454 			r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
455 			rest -= r;
456 			if (! rest) {
457 				if (i == periods - 1)
458 					flag = VIA_TBL_BIT_EOL; /* buffer boundary */
459 				else
460 					flag = VIA_TBL_BIT_FLAG; /* period boundary */
461 			} else
462 				flag = 0; /* period continues to the next */
463 			/*
464 			dev_dbg(&pci->dev,
465 				"tbl %d: at %d  size %d (rest %d)\n",
466 				idx, ofs, r, rest);
467 			*/
468 			pgtbl[(idx<<1) + 1] = cpu_to_le32(r | flag);
469 			dev->idx_table[idx].offset = ofs;
470 			dev->idx_table[idx].size = r;
471 			ofs += r;
472 			idx++;
473 		} while (rest > 0);
474 	}
475 	dev->tbl_entries = idx;
476 	dev->bufsize = periods * fragsize;
477 	dev->bufsize2 = dev->bufsize / 2;
478 	dev->fragsize = fragsize;
479 	return 0;
480 }
481 
482 
483 static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
484 			   struct pci_dev *pci)
485 {
486 	if (dev->table.area) {
487 		snd_dma_free_pages(&dev->table);
488 		dev->table.area = NULL;
489 	}
490 	kfree(dev->idx_table);
491 	dev->idx_table = NULL;
492 	return 0;
493 }
494 
495 /*
496  *  Basic I/O
497  */
498 
499 static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
500 {
501 	return inl(VIAREG(chip, AC97));
502 }
503 
504 static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
505 {
506 	outl(val, VIAREG(chip, AC97));
507 }
508 
509 static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
510 {
511 	unsigned int timeout = 1000;	/* 1ms */
512 	unsigned int val;
513 
514 	while (timeout-- > 0) {
515 		udelay(1);
516 		val = snd_via82xx_codec_xread(chip);
517 		if (!(val & VIA_REG_AC97_BUSY))
518 			return val & 0xffff;
519 	}
520 	dev_err(chip->card->dev, "codec_ready: codec %i is not ready [0x%x]\n",
521 		   secondary, snd_via82xx_codec_xread(chip));
522 	return -EIO;
523 }
524 
525 static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
526 {
527 	unsigned int timeout = 1000;	/* 1ms */
528 	unsigned int val, val1;
529 	unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
530 					 VIA_REG_AC97_SECONDARY_VALID;
531 
532 	while (timeout-- > 0) {
533 		val = snd_via82xx_codec_xread(chip);
534 		val1 = val & (VIA_REG_AC97_BUSY | stat);
535 		if (val1 == stat)
536 			return val & 0xffff;
537 		udelay(1);
538 	}
539 	return -EIO;
540 }
541 
542 static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
543 {
544 	struct via82xx *chip = ac97->private_data;
545 	__always_unused int err;
546 	err = snd_via82xx_codec_ready(chip, ac97->num);
547 	/* here we need to wait fairly for long time.. */
548 	if (!nodelay)
549 		msleep(500);
550 }
551 
552 static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
553 				    unsigned short reg,
554 				    unsigned short val)
555 {
556 	struct via82xx *chip = ac97->private_data;
557 	unsigned int xval;
558 
559 	xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
560 	xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
561 	xval |= reg << VIA_REG_AC97_CMD_SHIFT;
562 	xval |= val << VIA_REG_AC97_DATA_SHIFT;
563 	snd_via82xx_codec_xwrite(chip, xval);
564 	snd_via82xx_codec_ready(chip, ac97->num);
565 }
566 
567 static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
568 {
569 	struct via82xx *chip = ac97->private_data;
570 	unsigned int xval, val = 0xffff;
571 	int again = 0;
572 
573 	xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
574 	xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
575 	xval |= VIA_REG_AC97_READ;
576 	xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
577       	while (1) {
578       		if (again++ > 3) {
579 			dev_err(chip->card->dev,
580 				"codec_read: codec %i is not valid [0x%x]\n",
581 				   ac97->num, snd_via82xx_codec_xread(chip));
582 		      	return 0xffff;
583 		}
584 		snd_via82xx_codec_xwrite(chip, xval);
585 		udelay (20);
586 		if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
587 			udelay(25);
588 			val = snd_via82xx_codec_xread(chip);
589 			break;
590 		}
591 	}
592 	return val & 0xffff;
593 }
594 
595 static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
596 {
597 	outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
598 	     VIADEV_REG(viadev, OFFSET_CONTROL));
599 	inb(VIADEV_REG(viadev, OFFSET_CONTROL));
600 	udelay(50);
601 	/* disable interrupts */
602 	outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
603 	/* clear interrupts */
604 	outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
605 	outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
606 	// outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
607 	viadev->lastpos = 0;
608 	viadev->hwptr_done = 0;
609 }
610 
611 
612 /*
613  *  Interrupt handler
614  *  Used for 686 and 8233A
615  */
616 static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
617 {
618 	struct via82xx *chip = dev_id;
619 	unsigned int status;
620 	unsigned int i;
621 
622 	status = inl(VIAREG(chip, SGD_SHADOW));
623 	if (! (status & chip->intr_mask)) {
624 		if (chip->rmidi)
625 			/* check mpu401 interrupt */
626 			return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
627 		return IRQ_NONE;
628 	}
629 
630 	/* check status for each stream */
631 	guard(spinlock)(&chip->reg_lock);
632 	for (i = 0; i < chip->num_devs; i++) {
633 		struct viadev *viadev = &chip->devs[i];
634 		unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
635 		if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
636 			continue;
637 		if (viadev->substream && viadev->running) {
638 			/*
639 			 * Update hwptr_done based on 'period elapsed'
640 			 * interrupts. We'll use it, when the chip returns 0
641 			 * for OFFSET_CURR_COUNT.
642 			 */
643 			if (c_status & VIA_REG_STAT_EOL)
644 				viadev->hwptr_done = 0;
645 			else
646 				viadev->hwptr_done += viadev->fragsize;
647 			viadev->in_interrupt = c_status;
648 			spin_unlock(&chip->reg_lock);
649 			snd_pcm_period_elapsed(viadev->substream);
650 			spin_lock(&chip->reg_lock);
651 			viadev->in_interrupt = 0;
652 		}
653 		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
654 	}
655 	return IRQ_HANDLED;
656 }
657 
658 /*
659  *  Interrupt handler
660  */
661 static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
662 {
663 	struct via82xx *chip = dev_id;
664 	unsigned int status;
665 	unsigned int i;
666 	int irqreturn = 0;
667 
668 	/* check status for each stream */
669 	guard(spinlock)(&chip->reg_lock);
670 	status = inl(VIAREG(chip, SGD_SHADOW));
671 
672 	for (i = 0; i < chip->num_devs; i++) {
673 		struct viadev *viadev = &chip->devs[i];
674 		struct snd_pcm_substream *substream;
675 		unsigned char c_status, shadow_status;
676 
677 		shadow_status = (status >> viadev->shadow_shift) &
678 			(VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
679 			 VIA_REG_STAT_FLAG);
680 		c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
681 		if (!c_status)
682 			continue;
683 
684 		substream = viadev->substream;
685 		if (substream && viadev->running) {
686 			/*
687 			 * Update hwptr_done based on 'period elapsed'
688 			 * interrupts. We'll use it, when the chip returns 0
689 			 * for OFFSET_CURR_COUNT.
690 			 */
691 			if (c_status & VIA_REG_STAT_EOL)
692 				viadev->hwptr_done = 0;
693 			else
694 				viadev->hwptr_done += viadev->fragsize;
695 			viadev->in_interrupt = c_status;
696 			if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
697 				viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
698 			spin_unlock(&chip->reg_lock);
699 
700 			snd_pcm_period_elapsed(substream);
701 
702 			spin_lock(&chip->reg_lock);
703 			viadev->in_interrupt = 0;
704 		}
705 		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
706 		irqreturn = 1;
707 	}
708 	return IRQ_RETVAL(irqreturn);
709 }
710 
711 /*
712  *  PCM callbacks
713  */
714 
715 /*
716  * trigger callback
717  */
718 static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
719 {
720 	struct via82xx *chip = snd_pcm_substream_chip(substream);
721 	struct viadev *viadev = substream->runtime->private_data;
722 	unsigned char val;
723 
724 	if (chip->chip_type != TYPE_VIA686)
725 		val = VIA_REG_CTRL_INT;
726 	else
727 		val = 0;
728 	switch (cmd) {
729 	case SNDRV_PCM_TRIGGER_START:
730 	case SNDRV_PCM_TRIGGER_RESUME:
731 		val |= VIA_REG_CTRL_START;
732 		viadev->running = 1;
733 		break;
734 	case SNDRV_PCM_TRIGGER_STOP:
735 	case SNDRV_PCM_TRIGGER_SUSPEND:
736 		val = VIA_REG_CTRL_TERMINATE;
737 		viadev->running = 0;
738 		break;
739 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
740 		val |= VIA_REG_CTRL_PAUSE;
741 		viadev->running = 0;
742 		break;
743 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
744 		viadev->running = 1;
745 		break;
746 	default:
747 		return -EINVAL;
748 	}
749 	outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
750 	if (cmd == SNDRV_PCM_TRIGGER_STOP)
751 		snd_via82xx_channel_reset(chip, viadev);
752 	return 0;
753 }
754 
755 
756 /*
757  * pointer callbacks
758  */
759 
760 /*
761  * calculate the linear position at the given sg-buffer index and the rest count
762  */
763 
764 #define check_invalid_pos(viadev,pos) \
765 	((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
766 				     viadev->lastpos < viadev->bufsize2))
767 
768 static inline unsigned int calc_linear_pos(struct via82xx *chip,
769 					   struct viadev *viadev,
770 					   unsigned int idx,
771 					   unsigned int count)
772 {
773 	unsigned int size, base, res;
774 
775 	size = viadev->idx_table[idx].size;
776 	base = viadev->idx_table[idx].offset;
777 	res = base + size - count;
778 	if (res >= viadev->bufsize)
779 		res -= viadev->bufsize;
780 
781 	/* check the validity of the calculated position */
782 	if (size < count) {
783 		dev_dbg(chip->card->dev,
784 			"invalid via82xx_cur_ptr (size = %d, count = %d)\n",
785 			   (int)size, (int)count);
786 		res = viadev->lastpos;
787 	} else {
788 		if (! count) {
789 			/* Some mobos report count = 0 on the DMA boundary,
790 			 * i.e. count = size indeed.
791 			 * Let's check whether this step is above the expected size.
792 			 */
793 			int delta = res - viadev->lastpos;
794 			if (delta < 0)
795 				delta += viadev->bufsize;
796 			if ((unsigned int)delta > viadev->fragsize)
797 				res = base;
798 		}
799 		if (check_invalid_pos(viadev, res)) {
800 #ifdef POINTER_DEBUG
801 			dev_dbg(chip->card->dev,
802 				"fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n",
803 				idx, viadev->tbl_entries,
804 			       viadev->lastpos, viadev->bufsize2,
805 			       viadev->idx_table[idx].offset,
806 			       viadev->idx_table[idx].size, count);
807 #endif
808 			/* count register returns full size when end of buffer is reached */
809 			res = base + size;
810 			if (check_invalid_pos(viadev, res)) {
811 				dev_dbg(chip->card->dev,
812 					"invalid via82xx_cur_ptr (2), using last valid pointer\n");
813 				res = viadev->lastpos;
814 			}
815 		}
816 	}
817 	return res;
818 }
819 
820 /*
821  * get the current pointer on via686
822  */
823 static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
824 {
825 	struct via82xx *chip = snd_pcm_substream_chip(substream);
826 	struct viadev *viadev = substream->runtime->private_data;
827 	unsigned int idx, ptr, count, res;
828 
829 	if (snd_BUG_ON(!viadev->tbl_entries))
830 		return 0;
831 	if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
832 		return 0;
833 
834 	guard(spinlock)(&chip->reg_lock);
835 	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
836 	/* The via686a does not have the current index register,
837 	 * so we need to calculate the index from CURR_PTR.
838 	 */
839 	ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
840 	if (ptr <= (unsigned int)viadev->table.addr)
841 		idx = 0;
842 	else /* CURR_PTR holds the address + 8 */
843 		idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
844 	res = calc_linear_pos(chip, viadev, idx, count);
845 	viadev->lastpos = res; /* remember the last position */
846 
847 	return bytes_to_frames(substream->runtime, res);
848 }
849 
850 /*
851  * get the current pointer on via823x
852  */
853 static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
854 {
855 	struct via82xx *chip = snd_pcm_substream_chip(substream);
856 	struct viadev *viadev = substream->runtime->private_data;
857 	unsigned int idx, count, res;
858 	int status;
859 
860 	if (snd_BUG_ON(!viadev->tbl_entries))
861 		return 0;
862 
863 	guard(spinlock)(&chip->reg_lock);
864 	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
865 	status = viadev->in_interrupt;
866 	if (!status)
867 		status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
868 
869 	/* An apparent bug in the 8251 is worked around by sending a
870 	 * REG_CTRL_START. */
871 	if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
872 		snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
873 
874 	if (!(status & VIA_REG_STAT_ACTIVE)) {
875 		res = 0;
876 		goto unlock;
877 	}
878 	if (count & 0xffffff) {
879 		idx = count >> 24;
880 		if (idx >= viadev->tbl_entries) {
881 #ifdef POINTER_DEBUG
882 			dev_dbg(chip->card->dev,
883 				"fail: invalid idx = %i/%i\n", idx,
884 			       viadev->tbl_entries);
885 #endif
886 			res = viadev->lastpos;
887 		} else {
888 			count &= 0xffffff;
889 			res = calc_linear_pos(chip, viadev, idx, count);
890 		}
891 	} else {
892 		res = viadev->hwptr_done;
893 		if (!viadev->in_interrupt) {
894 			if (status & VIA_REG_STAT_EOL) {
895 				res = 0;
896 			} else
897 				if (status & VIA_REG_STAT_FLAG) {
898 					res += viadev->fragsize;
899 				}
900 		}
901 	}
902 unlock:
903 	viadev->lastpos = res;
904 
905 	return bytes_to_frames(substream->runtime, res);
906 }
907 
908 
909 /*
910  * hw_params callback:
911  * allocate the buffer and build up the buffer description table
912  */
913 static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
914 				 struct snd_pcm_hw_params *hw_params)
915 {
916 	struct via82xx *chip = snd_pcm_substream_chip(substream);
917 	struct viadev *viadev = substream->runtime->private_data;
918 
919 	return build_via_table(viadev, substream, chip->pci,
920 			       params_periods(hw_params),
921 			       params_period_bytes(hw_params));
922 }
923 
924 /*
925  * hw_free callback:
926  * clean up the buffer description table and release the buffer
927  */
928 static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
929 {
930 	struct via82xx *chip = snd_pcm_substream_chip(substream);
931 	struct viadev *viadev = substream->runtime->private_data;
932 
933 	clean_via_table(viadev, substream, chip->pci);
934 	return 0;
935 }
936 
937 
938 /*
939  * set up the table pointer
940  */
941 static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
942 {
943 	snd_via82xx_codec_ready(chip, 0);
944 	outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
945 	udelay(20);
946 	snd_via82xx_codec_ready(chip, 0);
947 }
948 
949 /*
950  * prepare callback for playback and capture on via686
951  */
952 static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
953 				struct snd_pcm_runtime *runtime)
954 {
955 	snd_via82xx_channel_reset(chip, viadev);
956 	/* this must be set after channel_reset */
957 	snd_via82xx_set_table_ptr(chip, viadev);
958 	outb(VIA_REG_TYPE_AUTOSTART |
959 	     (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
960 	     (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
961 	     ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
962 	     VIA_REG_TYPE_INT_EOL |
963 	     VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
964 }
965 
966 static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
967 {
968 	struct via82xx *chip = snd_pcm_substream_chip(substream);
969 	struct viadev *viadev = substream->runtime->private_data;
970 	struct snd_pcm_runtime *runtime = substream->runtime;
971 
972 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
973 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
974 	via686_setup_format(chip, viadev, runtime);
975 	return 0;
976 }
977 
978 static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
979 {
980 	struct via82xx *chip = snd_pcm_substream_chip(substream);
981 	struct viadev *viadev = substream->runtime->private_data;
982 	struct snd_pcm_runtime *runtime = substream->runtime;
983 
984 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
985 	via686_setup_format(chip, viadev, runtime);
986 	return 0;
987 }
988 
989 /*
990  * lock the current rate
991  */
992 static int via_lock_rate(struct via_rate_lock *rec, int rate)
993 {
994 	int changed = 0;
995 
996 	guard(spinlock_irq)(&rec->lock);
997 	if (rec->rate != rate) {
998 		if (rec->rate && rec->used > 1) /* already set */
999 			changed = -EINVAL;
1000 		else {
1001 			rec->rate = rate;
1002 			changed = 1;
1003 		}
1004 	}
1005 	return changed;
1006 }
1007 
1008 /*
1009  * prepare callback for DSX playback on via823x
1010  */
1011 static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
1012 {
1013 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1014 	struct viadev *viadev = substream->runtime->private_data;
1015 	struct snd_pcm_runtime *runtime = substream->runtime;
1016 	int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
1017 	int rate_changed;
1018 	u32 rbits;
1019 
1020 	rate_changed = via_lock_rate(&chip->rates[0], ac97_rate);
1021 	if (rate_changed < 0)
1022 		return rate_changed;
1023 	if (rate_changed)
1024 		snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1025 				  chip->no_vra ? 48000 : runtime->rate);
1026 	if (chip->spdif_on && viadev->reg_offset == 0x30)
1027 		snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1028 
1029 	if (runtime->rate == 48000)
1030 		rbits = 0xfffff;
1031 	else
1032 		rbits = (0x100000 / 48000) * runtime->rate +
1033 			((0x100000 % 48000) * runtime->rate) / 48000;
1034 	snd_BUG_ON(rbits & ~0xfffff);
1035 	snd_via82xx_channel_reset(chip, viadev);
1036 	snd_via82xx_set_table_ptr(chip, viadev);
1037 	outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1038 	     VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1039 	outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1040 	     VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
1041 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1042 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1043 	     rbits | /* rate */
1044 	     0xff000000,    /* STOP index is never reached */
1045 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
1046 	udelay(20);
1047 	snd_via82xx_codec_ready(chip, 0);
1048 	return 0;
1049 }
1050 
1051 /*
1052  * prepare callback for multi-channel playback on via823x
1053  */
1054 static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
1055 {
1056 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1057 	struct viadev *viadev = substream->runtime->private_data;
1058 	struct snd_pcm_runtime *runtime = substream->runtime;
1059 	unsigned int slots;
1060 	int fmt;
1061 
1062 	if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1063 		return -EINVAL;
1064 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1065 	snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1066 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1067 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1068 	snd_via82xx_channel_reset(chip, viadev);
1069 	snd_via82xx_set_table_ptr(chip, viadev);
1070 
1071 	fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1072 		VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
1073 	fmt |= runtime->channels << 4;
1074 	outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1075 #if 0
1076 	if (chip->revision == VIA_REV_8233A)
1077 		slots = 0;
1078 	else
1079 #endif
1080 	{
1081 		/* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1082 		/* corresponding to FL, FR, RL, RR, C, LFE ?? */
1083 		switch (runtime->channels) {
1084 		case 1: slots = (1<<0) | (1<<4); break;
1085 		case 2: slots = (1<<0) | (2<<4); break;
1086 		case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1087 		case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1088 		case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1089 		case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1090 		default: slots = 0; break;
1091 		}
1092 	}
1093 	/* STOP index is never reached */
1094 	outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1095 	udelay(20);
1096 	snd_via82xx_codec_ready(chip, 0);
1097 	return 0;
1098 }
1099 
1100 /*
1101  * prepare callback for capture on via823x
1102  */
1103 static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
1104 {
1105 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1106 	struct viadev *viadev = substream->runtime->private_data;
1107 	struct snd_pcm_runtime *runtime = substream->runtime;
1108 
1109 	if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1110 		return -EINVAL;
1111 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1112 	snd_via82xx_channel_reset(chip, viadev);
1113 	snd_via82xx_set_table_ptr(chip, viadev);
1114 	outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1115 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1116 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1117 	     0xff000000,    /* STOP index is never reached */
1118 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
1119 	udelay(20);
1120 	snd_via82xx_codec_ready(chip, 0);
1121 	return 0;
1122 }
1123 
1124 
1125 /*
1126  * pcm hardware definition, identical for both playback and capture
1127  */
1128 static const struct snd_pcm_hardware snd_via82xx_hw =
1129 {
1130 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1131 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1132 				 SNDRV_PCM_INFO_MMAP_VALID |
1133 				 /* SNDRV_PCM_INFO_RESUME | */
1134 				 SNDRV_PCM_INFO_PAUSE),
1135 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1136 	.rates =		SNDRV_PCM_RATE_48000,
1137 	.rate_min =		48000,
1138 	.rate_max =		48000,
1139 	.channels_min =		1,
1140 	.channels_max =		2,
1141 	.buffer_bytes_max =	VIA_MAX_BUFSIZE,
1142 	.period_bytes_min =	32,
1143 	.period_bytes_max =	VIA_MAX_BUFSIZE / 2,
1144 	.periods_min =		2,
1145 	.periods_max =		VIA_TABLE_SIZE / 2,
1146 	.fifo_size =		0,
1147 };
1148 
1149 
1150 /*
1151  * open callback skeleton
1152  */
1153 static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1154 				struct snd_pcm_substream *substream)
1155 {
1156 	struct snd_pcm_runtime *runtime = substream->runtime;
1157 	int err;
1158 	struct via_rate_lock *ratep;
1159 	bool use_src = false;
1160 
1161 	runtime->hw = snd_via82xx_hw;
1162 
1163 	/* set the hw rate condition */
1164 	ratep = &chip->rates[viadev->direction];
1165 	scoped_guard(spinlock_irq, &ratep->lock) {
1166 		ratep->used++;
1167 		if (chip->spdif_on && viadev->reg_offset == 0x30) {
1168 			/* DXS#3 and spdif is on */
1169 			runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1170 			snd_pcm_limit_hw_rates(runtime);
1171 		} else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1172 			/* fixed DXS playback rate */
1173 			runtime->hw.rates = SNDRV_PCM_RATE_48000;
1174 			runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1175 		} else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1176 			/* use full SRC capabilities of DXS */
1177 			runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1178 					     SNDRV_PCM_RATE_8000_48000);
1179 			runtime->hw.rate_min = 8000;
1180 			runtime->hw.rate_max = 48000;
1181 			use_src = true;
1182 		} else if (!ratep->rate) {
1183 			int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1184 			runtime->hw.rates = chip->ac97->rates[idx];
1185 			snd_pcm_limit_hw_rates(runtime);
1186 		} else {
1187 			/* a fixed rate */
1188 			runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1189 			runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1190 		}
1191 	}
1192 
1193 	/* we may remove following constaint when we modify table entries
1194 	   in interrupt */
1195 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1196 	if (err < 0)
1197 		return err;
1198 
1199 	if (use_src) {
1200 		err = snd_pcm_hw_rule_noresample(runtime, 48000);
1201 		if (err < 0)
1202 			return err;
1203 	}
1204 
1205 	runtime->private_data = viadev;
1206 	viadev->substream = substream;
1207 
1208 	return 0;
1209 }
1210 
1211 
1212 /*
1213  * open callback for playback on via686
1214  */
1215 static int snd_via686_playback_open(struct snd_pcm_substream *substream)
1216 {
1217 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1218 	struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
1219 	int err;
1220 
1221 	err = snd_via82xx_pcm_open(chip, viadev, substream);
1222 	if (err < 0)
1223 		return err;
1224 	return 0;
1225 }
1226 
1227 /*
1228  * open callback for playback on via823x DXS
1229  */
1230 static int snd_via8233_playback_open(struct snd_pcm_substream *substream)
1231 {
1232 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1233 	struct viadev *viadev;
1234 	unsigned int stream;
1235 	int err;
1236 
1237 	viadev = &chip->devs[chip->playback_devno + substream->number];
1238 	err = snd_via82xx_pcm_open(chip, viadev, substream);
1239 	if (err < 0)
1240 		return err;
1241 	stream = viadev->reg_offset / 0x10;
1242 	if (chip->dxs_controls[stream]) {
1243 		chip->playback_volume[stream][0] =
1244 				VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1245 		chip->playback_volume[stream][1] =
1246 				VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1247 		chip->dxs_controls[stream]->vd[0].access &=
1248 			~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1249 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1250 			       SNDRV_CTL_EVENT_MASK_INFO,
1251 			       &chip->dxs_controls[stream]->id);
1252 	}
1253 	return 0;
1254 }
1255 
1256 /*
1257  * open callback for playback on via823x multi-channel
1258  */
1259 static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
1260 {
1261 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1262 	struct viadev *viadev = &chip->devs[chip->multi_devno];
1263 	int err;
1264 	/* channels constraint for VIA8233A
1265 	 * 3 and 5 channels are not supported
1266 	 */
1267 	static const unsigned int channels[] = {
1268 		1, 2, 4, 6
1269 	};
1270 	static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
1271 		.count = ARRAY_SIZE(channels),
1272 		.list = channels,
1273 		.mask = 0,
1274 	};
1275 
1276 	err = snd_via82xx_pcm_open(chip, viadev, substream);
1277 	if (err < 0)
1278 		return err;
1279 	substream->runtime->hw.channels_max = 6;
1280 	if (chip->revision == VIA_REV_8233A)
1281 		snd_pcm_hw_constraint_list(substream->runtime, 0,
1282 					   SNDRV_PCM_HW_PARAM_CHANNELS,
1283 					   &hw_constraints_channels);
1284 	return 0;
1285 }
1286 
1287 /*
1288  * open callback for capture on via686 and via823x
1289  */
1290 static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
1291 {
1292 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1293 	struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1294 
1295 	return snd_via82xx_pcm_open(chip, viadev, substream);
1296 }
1297 
1298 /*
1299  * close callback
1300  */
1301 static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
1302 {
1303 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1304 	struct viadev *viadev = substream->runtime->private_data;
1305 	struct via_rate_lock *ratep;
1306 
1307 	/* release the rate lock */
1308 	ratep = &chip->rates[viadev->direction];
1309 	scoped_guard(spinlock_irq, &ratep->lock) {
1310 		ratep->used--;
1311 		if (!ratep->used)
1312 			ratep->rate = 0;
1313 	}
1314 	if (! ratep->rate) {
1315 		if (! viadev->direction) {
1316 			snd_ac97_update_power(chip->ac97,
1317 					      AC97_PCM_FRONT_DAC_RATE, 0);
1318 			snd_ac97_update_power(chip->ac97,
1319 					      AC97_PCM_SURR_DAC_RATE, 0);
1320 			snd_ac97_update_power(chip->ac97,
1321 					      AC97_PCM_LFE_DAC_RATE, 0);
1322 		} else
1323 			snd_ac97_update_power(chip->ac97,
1324 					      AC97_PCM_LR_ADC_RATE, 0);
1325 	}
1326 	viadev->substream = NULL;
1327 	return 0;
1328 }
1329 
1330 static int snd_via8233_playback_close(struct snd_pcm_substream *substream)
1331 {
1332 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1333 	struct viadev *viadev = substream->runtime->private_data;
1334 	unsigned int stream;
1335 
1336 	stream = viadev->reg_offset / 0x10;
1337 	if (chip->dxs_controls[stream]) {
1338 		chip->dxs_controls[stream]->vd[0].access |=
1339 			SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1340 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1341 			       &chip->dxs_controls[stream]->id);
1342 	}
1343 	return snd_via82xx_pcm_close(substream);
1344 }
1345 
1346 
1347 /* via686 playback callbacks */
1348 static const struct snd_pcm_ops snd_via686_playback_ops = {
1349 	.open =		snd_via686_playback_open,
1350 	.close =	snd_via82xx_pcm_close,
1351 	.hw_params =	snd_via82xx_hw_params,
1352 	.hw_free =	snd_via82xx_hw_free,
1353 	.prepare =	snd_via686_playback_prepare,
1354 	.trigger =	snd_via82xx_pcm_trigger,
1355 	.pointer =	snd_via686_pcm_pointer,
1356 };
1357 
1358 /* via686 capture callbacks */
1359 static const struct snd_pcm_ops snd_via686_capture_ops = {
1360 	.open =		snd_via82xx_capture_open,
1361 	.close =	snd_via82xx_pcm_close,
1362 	.hw_params =	snd_via82xx_hw_params,
1363 	.hw_free =	snd_via82xx_hw_free,
1364 	.prepare =	snd_via686_capture_prepare,
1365 	.trigger =	snd_via82xx_pcm_trigger,
1366 	.pointer =	snd_via686_pcm_pointer,
1367 };
1368 
1369 /* via823x DSX playback callbacks */
1370 static const struct snd_pcm_ops snd_via8233_playback_ops = {
1371 	.open =		snd_via8233_playback_open,
1372 	.close =	snd_via8233_playback_close,
1373 	.hw_params =	snd_via82xx_hw_params,
1374 	.hw_free =	snd_via82xx_hw_free,
1375 	.prepare =	snd_via8233_playback_prepare,
1376 	.trigger =	snd_via82xx_pcm_trigger,
1377 	.pointer =	snd_via8233_pcm_pointer,
1378 };
1379 
1380 /* via823x multi-channel playback callbacks */
1381 static const struct snd_pcm_ops snd_via8233_multi_ops = {
1382 	.open =		snd_via8233_multi_open,
1383 	.close =	snd_via82xx_pcm_close,
1384 	.hw_params =	snd_via82xx_hw_params,
1385 	.hw_free =	snd_via82xx_hw_free,
1386 	.prepare =	snd_via8233_multi_prepare,
1387 	.trigger =	snd_via82xx_pcm_trigger,
1388 	.pointer =	snd_via8233_pcm_pointer,
1389 };
1390 
1391 /* via823x capture callbacks */
1392 static const struct snd_pcm_ops snd_via8233_capture_ops = {
1393 	.open =		snd_via82xx_capture_open,
1394 	.close =	snd_via82xx_pcm_close,
1395 	.hw_params =	snd_via82xx_hw_params,
1396 	.hw_free =	snd_via82xx_hw_free,
1397 	.prepare =	snd_via8233_capture_prepare,
1398 	.trigger =	snd_via82xx_pcm_trigger,
1399 	.pointer =	snd_via8233_pcm_pointer,
1400 };
1401 
1402 
1403 static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1404 			int shadow_pos, int direction)
1405 {
1406 	chip->devs[idx].reg_offset = reg_offset;
1407 	chip->devs[idx].shadow_shift = shadow_pos * 4;
1408 	chip->devs[idx].direction = direction;
1409 	chip->devs[idx].port = chip->port + reg_offset;
1410 }
1411 
1412 /*
1413  * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1414  */
1415 static int snd_via8233_pcm_new(struct via82xx *chip)
1416 {
1417 	struct snd_pcm *pcm;
1418 	struct snd_pcm_chmap *chmap;
1419 	int i, err;
1420 
1421 	chip->playback_devno = 0;	/* x 4 */
1422 	chip->multi_devno = 4;		/* x 1 */
1423 	chip->capture_devno = 5;	/* x 2 */
1424 	chip->num_devs = 7;
1425 	chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1426 
1427 	/* PCM #0:  4 DSX playbacks and 1 capture */
1428 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1429 	if (err < 0)
1430 		return err;
1431 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1432 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1433 	pcm->private_data = chip;
1434 	strscpy(pcm->name, chip->card->shortname);
1435 	chip->pcms[0] = pcm;
1436 	/* set up playbacks */
1437 	for (i = 0; i < 4; i++)
1438 		init_viadev(chip, i, 0x10 * i, i, 0);
1439 	/* capture */
1440 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1441 
1442 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1443 				       &chip->pci->dev,
1444 				       64*1024, VIA_MAX_BUFSIZE);
1445 
1446 	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1447 				     snd_pcm_std_chmaps, 2, 0,
1448 				     &chmap);
1449 	if (err < 0)
1450 		return err;
1451 
1452 	/* PCM #1:  multi-channel playback and 2nd capture */
1453 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1454 	if (err < 0)
1455 		return err;
1456 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1457 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1458 	pcm->private_data = chip;
1459 	strscpy(pcm->name, chip->card->shortname);
1460 	chip->pcms[1] = pcm;
1461 	/* set up playback */
1462 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1463 	/* set up capture */
1464 	init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
1465 
1466 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1467 				       &chip->pci->dev,
1468 				       64*1024, VIA_MAX_BUFSIZE);
1469 
1470 	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1471 				     snd_pcm_alt_chmaps, 6, 0,
1472 				     &chmap);
1473 	if (err < 0)
1474 		return err;
1475 	chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1476 
1477 	return 0;
1478 }
1479 
1480 /*
1481  * create pcm instances for VIA8233A
1482  */
1483 static int snd_via8233a_pcm_new(struct via82xx *chip)
1484 {
1485 	struct snd_pcm *pcm;
1486 	struct snd_pcm_chmap *chmap;
1487 	int err;
1488 
1489 	chip->multi_devno = 0;
1490 	chip->playback_devno = 1;
1491 	chip->capture_devno = 2;
1492 	chip->num_devs = 3;
1493 	chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1494 
1495 	/* PCM #0:  multi-channel playback and capture */
1496 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1497 	if (err < 0)
1498 		return err;
1499 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1500 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1501 	pcm->private_data = chip;
1502 	strscpy(pcm->name, chip->card->shortname);
1503 	chip->pcms[0] = pcm;
1504 	/* set up playback */
1505 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1506 	/* capture */
1507 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1508 
1509 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1510 				       &chip->pci->dev,
1511 				       64*1024, VIA_MAX_BUFSIZE);
1512 
1513 	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1514 				     snd_pcm_alt_chmaps, 6, 0,
1515 				     &chmap);
1516 	if (err < 0)
1517 		return err;
1518 	chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1519 
1520 	/* SPDIF supported? */
1521 	if (! ac97_can_spdif(chip->ac97))
1522 		return 0;
1523 
1524 	/* PCM #1:  DXS3 playback (for spdif) */
1525 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1526 	if (err < 0)
1527 		return err;
1528 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1529 	pcm->private_data = chip;
1530 	strscpy(pcm->name, chip->card->shortname);
1531 	chip->pcms[1] = pcm;
1532 	/* set up playback */
1533 	init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
1534 
1535 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1536 				       &chip->pci->dev,
1537 				       64*1024, VIA_MAX_BUFSIZE);
1538 	return 0;
1539 }
1540 
1541 /*
1542  * create a pcm instance for via686a/b
1543  */
1544 static int snd_via686_pcm_new(struct via82xx *chip)
1545 {
1546 	struct snd_pcm *pcm;
1547 	int err;
1548 
1549 	chip->playback_devno = 0;
1550 	chip->capture_devno = 1;
1551 	chip->num_devs = 2;
1552 	chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1553 
1554 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1555 	if (err < 0)
1556 		return err;
1557 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1558 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1559 	pcm->private_data = chip;
1560 	strscpy(pcm->name, chip->card->shortname);
1561 	chip->pcms[0] = pcm;
1562 	init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1563 	init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
1564 
1565 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1566 				       &chip->pci->dev,
1567 				       64*1024, VIA_MAX_BUFSIZE);
1568 	return 0;
1569 }
1570 
1571 
1572 /*
1573  *  Mixer part
1574  */
1575 
1576 static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1577 					   struct snd_ctl_elem_info *uinfo)
1578 {
1579 	/* formerly they were "Line" and "Mic", but it looks like that they
1580 	 * have nothing to do with the actual physical connections...
1581 	 */
1582 	static const char * const texts[2] = {
1583 		"Input1", "Input2"
1584 	};
1585 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
1586 }
1587 
1588 static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1589 					  struct snd_ctl_elem_value *ucontrol)
1590 {
1591 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1592 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1593 	ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1594 	return 0;
1595 }
1596 
1597 static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1598 					  struct snd_ctl_elem_value *ucontrol)
1599 {
1600 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1601 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1602 	u8 val, oval;
1603 
1604 	guard(spinlock_irq)(&chip->reg_lock);
1605 	oval = inb(port);
1606 	val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1607 	if (ucontrol->value.enumerated.item[0])
1608 		val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1609 	if (val != oval)
1610 		outb(val, port);
1611 	return val != oval;
1612 }
1613 
1614 static struct snd_kcontrol_new snd_via8233_capture_source = {
1615 	.name = "Input Source Select",
1616 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1617 	.info = snd_via8233_capture_source_info,
1618 	.get = snd_via8233_capture_source_get,
1619 	.put = snd_via8233_capture_source_put,
1620 };
1621 
1622 #define snd_via8233_dxs3_spdif_info	snd_ctl_boolean_mono_info
1623 
1624 static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1625 				      struct snd_ctl_elem_value *ucontrol)
1626 {
1627 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1628 	u8 val;
1629 
1630 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1631 	ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1632 	return 0;
1633 }
1634 
1635 static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1636 				      struct snd_ctl_elem_value *ucontrol)
1637 {
1638 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1639 	u8 val, oval;
1640 
1641 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1642 	val = oval & ~VIA8233_SPDIF_DX3;
1643 	if (ucontrol->value.integer.value[0])
1644 		val |= VIA8233_SPDIF_DX3;
1645 	/* save the spdif flag for rate filtering */
1646 	chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1647 	if (val != oval) {
1648 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1649 		return 1;
1650 	}
1651 	return 0;
1652 }
1653 
1654 static const struct snd_kcontrol_new snd_via8233_dxs3_spdif_control = {
1655 	.name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
1656 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1657 	.info = snd_via8233_dxs3_spdif_info,
1658 	.get = snd_via8233_dxs3_spdif_get,
1659 	.put = snd_via8233_dxs3_spdif_put,
1660 };
1661 
1662 static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1663 				       struct snd_ctl_elem_info *uinfo)
1664 {
1665 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1666 	uinfo->count = 2;
1667 	uinfo->value.integer.min = 0;
1668 	uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1669 	return 0;
1670 }
1671 
1672 static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1673 				      struct snd_ctl_elem_value *ucontrol)
1674 {
1675 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1676 	unsigned int idx = kcontrol->id.subdevice;
1677 
1678 	ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1679 	ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1680 	return 0;
1681 }
1682 
1683 static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1684 					 struct snd_ctl_elem_value *ucontrol)
1685 {
1686 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1687 	ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1688 	ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
1689 	return 0;
1690 }
1691 
1692 static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1693 				      struct snd_ctl_elem_value *ucontrol)
1694 {
1695 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1696 	unsigned int idx = kcontrol->id.subdevice;
1697 	unsigned long port = chip->port + 0x10 * idx;
1698 	unsigned char val;
1699 	int i, change = 0;
1700 
1701 	for (i = 0; i < 2; i++) {
1702 		val = ucontrol->value.integer.value[i];
1703 		if (val > VIA_DXS_MAX_VOLUME)
1704 			val = VIA_DXS_MAX_VOLUME;
1705 		val = VIA_DXS_MAX_VOLUME - val;
1706 		change |= val != chip->playback_volume[idx][i];
1707 		if (change) {
1708 			chip->playback_volume[idx][i] = val;
1709 			outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1710 		}
1711 	}
1712 	return change;
1713 }
1714 
1715 static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1716 					 struct snd_ctl_elem_value *ucontrol)
1717 {
1718 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1719 	unsigned int idx;
1720 	unsigned char val;
1721 	int i, change = 0;
1722 
1723 	for (i = 0; i < 2; i++) {
1724 		val = ucontrol->value.integer.value[i];
1725 		if (val > VIA_DXS_MAX_VOLUME)
1726 			val = VIA_DXS_MAX_VOLUME;
1727 		val = VIA_DXS_MAX_VOLUME - val;
1728 		if (val != chip->playback_volume_c[i]) {
1729 			change = 1;
1730 			chip->playback_volume_c[i] = val;
1731 			for (idx = 0; idx < 4; idx++) {
1732 				unsigned long port = chip->port + 0x10 * idx;
1733 				chip->playback_volume[idx][i] = val;
1734 				outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1735 			}
1736 		}
1737 	}
1738 	return change;
1739 }
1740 
1741 static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -4650, 150, 1);
1742 
1743 static const struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control = {
1744 	.name = "PCM Playback Volume",
1745 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1746 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1747 		   SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1748 	.info = snd_via8233_dxs_volume_info,
1749 	.get = snd_via8233_pcmdxs_volume_get,
1750 	.put = snd_via8233_pcmdxs_volume_put,
1751 	.tlv = { .p = db_scale_dxs }
1752 };
1753 
1754 static const struct snd_kcontrol_new snd_via8233_dxs_volume_control = {
1755 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1756 	.device = 0,
1757 	/* .subdevice set later */
1758 	.name = "PCM Playback Volume",
1759 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1760 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1761 		  SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1762 	.info = snd_via8233_dxs_volume_info,
1763 	.get = snd_via8233_dxs_volume_get,
1764 	.put = snd_via8233_dxs_volume_put,
1765 	.tlv = { .p = db_scale_dxs }
1766 };
1767 
1768 /*
1769  */
1770 
1771 static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1772 {
1773 	struct via82xx *chip = bus->private_data;
1774 	chip->ac97_bus = NULL;
1775 }
1776 
1777 static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
1778 {
1779 	struct via82xx *chip = ac97->private_data;
1780 	chip->ac97 = NULL;
1781 }
1782 
1783 static const struct ac97_quirk ac97_quirks[] = {
1784 	{
1785 		.subvendor = 0x1106,
1786 		.subdevice = 0x4161,
1787 		.codec_id = 0x56494161, /* VT1612A */
1788 		.name = "Soltek SL-75DRV5",
1789 		.type = AC97_TUNE_NONE
1790 	},
1791 	{	/* FIXME: which codec? */
1792 		.subvendor = 0x1106,
1793 		.subdevice = 0x4161,
1794 		.name = "ASRock K7VT2",
1795 		.type = AC97_TUNE_HP_ONLY
1796 	},
1797 	{
1798 		.subvendor = 0x110a,
1799 		.subdevice = 0x0079,
1800 		.name = "Fujitsu Siemens D1289",
1801 		.type = AC97_TUNE_HP_ONLY
1802 	},
1803 	{
1804 		.subvendor = 0x1019,
1805 		.subdevice = 0x0a81,
1806 		.name = "ECS K7VTA3",
1807 		.type = AC97_TUNE_HP_ONLY
1808 	},
1809 	{
1810 		.subvendor = 0x1019,
1811 		.subdevice = 0x0a85,
1812 		.name = "ECS L7VMM2",
1813 		.type = AC97_TUNE_HP_ONLY
1814 	},
1815 	{
1816 		.subvendor = 0x1019,
1817 		.subdevice = 0x1841,
1818 		.name = "ECS K7VTA3",
1819 		.type = AC97_TUNE_HP_ONLY
1820 	},
1821 	{
1822 		.subvendor = 0x1849,
1823 		.subdevice = 0x3059,
1824 		.name = "ASRock K7VM2",
1825 		.type = AC97_TUNE_HP_ONLY	/* VT1616 */
1826 	},
1827 	{
1828 		.subvendor = 0x14cd,
1829 		.subdevice = 0x7002,
1830 		.name = "Unknown",
1831 		.type = AC97_TUNE_ALC_JACK
1832 	},
1833 	{
1834 		.subvendor = 0x1071,
1835 		.subdevice = 0x8590,
1836 		.name = "Mitac Mobo",
1837 		.type = AC97_TUNE_ALC_JACK
1838 	},
1839 	{
1840 		.subvendor = 0x161f,
1841 		.subdevice = 0x202b,
1842 		.name = "Arima Notebook",
1843 		.type = AC97_TUNE_HP_ONLY,
1844 	},
1845 	{
1846 		.subvendor = 0x161f,
1847 		.subdevice = 0x2032,
1848 		.name = "Targa Traveller 811",
1849 		.type = AC97_TUNE_HP_ONLY,
1850 	},
1851 	{
1852 		.subvendor = 0x161f,
1853 		.subdevice = 0x2032,
1854 		.name = "m680x",
1855 		.type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1856 	},
1857 	{
1858 		.subvendor = 0x1297,
1859 		.subdevice = 0xa232,
1860 		.name = "Shuttle AK32VN",
1861 		.type = AC97_TUNE_HP_ONLY
1862 	},
1863 	{ } /* terminator */
1864 };
1865 
1866 static int snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
1867 {
1868 	struct snd_ac97_template ac97;
1869 	int err;
1870 	static const struct snd_ac97_bus_ops ops = {
1871 		.write = snd_via82xx_codec_write,
1872 		.read = snd_via82xx_codec_read,
1873 		.wait = snd_via82xx_codec_wait,
1874 	};
1875 
1876 	err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1877 	if (err < 0)
1878 		return err;
1879 	chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1880 	chip->ac97_bus->clock = chip->ac97_clock;
1881 
1882 	memset(&ac97, 0, sizeof(ac97));
1883 	ac97.private_data = chip;
1884 	ac97.private_free = snd_via82xx_mixer_free_ac97;
1885 	ac97.pci = chip->pci;
1886 	ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
1887 	err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1888 	if (err < 0)
1889 		return err;
1890 
1891 	snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1892 
1893 	if (chip->chip_type != TYPE_VIA686) {
1894 		/* use slot 10/11 */
1895 		snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1896 	}
1897 
1898 	return 0;
1899 }
1900 
1901 #ifdef SUPPORT_JOYSTICK
1902 #define JOYSTICK_ADDR	0x200
1903 static int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1904 {
1905 	struct gameport *gp;
1906 
1907 	if (!joystick)
1908 		return -ENODEV;
1909 
1910 	if (!devm_request_region(chip->card->dev, JOYSTICK_ADDR, 8,
1911 				 "VIA686 gameport")) {
1912 		dev_warn(chip->card->dev, "cannot reserve joystick port %#x\n",
1913 		       JOYSTICK_ADDR);
1914 		return -EBUSY;
1915 	}
1916 
1917 	chip->gameport = gp = gameport_allocate_port();
1918 	if (!gp) {
1919 		dev_err(chip->card->dev,
1920 			"cannot allocate memory for gameport\n");
1921 		return -ENOMEM;
1922 	}
1923 
1924 	gameport_set_name(gp, "VIA686 Gameport");
1925 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1926 	gameport_set_dev_parent(gp, &chip->pci->dev);
1927 	gp->io = JOYSTICK_ADDR;
1928 
1929 	/* Enable legacy joystick port */
1930 	*legacy |= VIA_FUNC_ENABLE_GAME;
1931 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1932 
1933 	gameport_register_port(chip->gameport);
1934 
1935 	return 0;
1936 }
1937 
1938 static void snd_via686_free_gameport(struct via82xx *chip)
1939 {
1940 	if (chip->gameport) {
1941 		gameport_unregister_port(chip->gameport);
1942 		chip->gameport = NULL;
1943 	}
1944 }
1945 #else
1946 static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1947 {
1948 	return -ENOSYS;
1949 }
1950 static inline void snd_via686_free_gameport(struct via82xx *chip) { }
1951 #endif
1952 
1953 
1954 /*
1955  *
1956  */
1957 
1958 static int snd_via8233_init_misc(struct via82xx *chip)
1959 {
1960 	int i, err, caps;
1961 	unsigned char val;
1962 
1963 	caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1964 	for (i = 0; i < caps; i++) {
1965 		snd_via8233_capture_source.index = i;
1966 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1967 		if (err < 0)
1968 			return err;
1969 	}
1970 	if (ac97_can_spdif(chip->ac97)) {
1971 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1972 		if (err < 0)
1973 			return err;
1974 	}
1975 	if (chip->chip_type != TYPE_VIA8233A) {
1976 		/* when no h/w PCM volume control is found, use DXS volume control
1977 		 * as the PCM vol control
1978 		 */
1979 		if (!snd_ctl_find_id_mixer(chip->card, "PCM Playback Volume")) {
1980 			dev_info(chip->card->dev,
1981 				 "Using DXS as PCM Playback\n");
1982 			err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1983 			if (err < 0)
1984 				return err;
1985 		}
1986 		else /* Using DXS when PCM emulation is enabled is really weird */
1987 		{
1988 			for (i = 0; i < 4; ++i) {
1989 				struct snd_kcontrol *kctl;
1990 
1991 				kctl = snd_ctl_new1(
1992 					&snd_via8233_dxs_volume_control, chip);
1993 				if (!kctl)
1994 					return -ENOMEM;
1995 				kctl->id.subdevice = i;
1996 				err = snd_ctl_add(chip->card, kctl);
1997 				if (err < 0)
1998 					return err;
1999 				chip->dxs_controls[i] = kctl;
2000 			}
2001 		}
2002 	}
2003 	/* select spdif data slot 10/11 */
2004 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
2005 	val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
2006 	val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
2007 	pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
2008 
2009 	return 0;
2010 }
2011 
2012 static int snd_via686_init_misc(struct via82xx *chip)
2013 {
2014 	unsigned char legacy, legacy_cfg;
2015 	int rev_h = 0;
2016 
2017 	legacy = chip->old_legacy;
2018 	legacy_cfg = chip->old_legacy_cfg;
2019 	legacy |= VIA_FUNC_MIDI_IRQMASK;	/* FIXME: correct? (disable MIDI) */
2020 	legacy &= ~VIA_FUNC_ENABLE_GAME;	/* disable joystick */
2021 	if (chip->revision >= VIA_REV_686_H) {
2022 		rev_h = 1;
2023 		if (mpu_port >= 0x200) {	/* force MIDI */
2024 			mpu_port &= 0xfffc;
2025 			pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
2026 			chip->mpu_port_saved = mpu_port;
2027 		} else {
2028 			mpu_port = pci_resource_start(chip->pci, 2);
2029 		}
2030 	} else {
2031 		switch (mpu_port) {	/* force MIDI */
2032 		case 0x300:
2033 		case 0x310:
2034 		case 0x320:
2035 		case 0x330:
2036 			legacy_cfg &= ~(3 << 2);
2037 			legacy_cfg |= (mpu_port & 0x0030) >> 2;
2038 			break;
2039 		default:			/* no, use BIOS settings */
2040 			if (legacy & VIA_FUNC_ENABLE_MIDI)
2041 				mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
2042 			break;
2043 		}
2044 	}
2045 	if (mpu_port >= 0x200)
2046 		chip->mpu_res = devm_request_region(&chip->pci->dev, mpu_port,
2047 						    2, "VIA82xx MPU401");
2048 	if (chip->mpu_res) {
2049 		if (rev_h)
2050 			legacy |= VIA_FUNC_MIDI_PNP;	/* enable PCI I/O 2 */
2051 		legacy |= VIA_FUNC_ENABLE_MIDI;
2052 	} else {
2053 		if (rev_h)
2054 			legacy &= ~VIA_FUNC_MIDI_PNP;	/* disable PCI I/O 2 */
2055 		legacy &= ~VIA_FUNC_ENABLE_MIDI;
2056 		mpu_port = 0;
2057 	}
2058 
2059 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2060 	pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
2061 	if (chip->mpu_res) {
2062 		if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
2063 					mpu_port, MPU401_INFO_INTEGRATED |
2064 					MPU401_INFO_IRQ_HOOK, -1,
2065 					&chip->rmidi) < 0) {
2066 			dev_warn(chip->card->dev,
2067 				 "unable to initialize MPU-401 at 0x%lx, skipping\n",
2068 				 mpu_port);
2069 			legacy &= ~VIA_FUNC_ENABLE_MIDI;
2070 		} else {
2071 			legacy &= ~VIA_FUNC_MIDI_IRQMASK;	/* enable MIDI interrupt */
2072 		}
2073 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2074 	}
2075 
2076 	snd_via686_create_gameport(chip, &legacy);
2077 
2078 	chip->legacy_saved = legacy;
2079 	chip->legacy_cfg_saved = legacy_cfg;
2080 
2081 	return 0;
2082 }
2083 
2084 
2085 /*
2086  * proc interface
2087  */
2088 static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2089 				  struct snd_info_buffer *buffer)
2090 {
2091 	struct via82xx *chip = entry->private_data;
2092 	int i;
2093 
2094 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2095 	for (i = 0; i < 0xa0; i += 4) {
2096 		snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2097 	}
2098 }
2099 
2100 static void snd_via82xx_proc_init(struct via82xx *chip)
2101 {
2102 	snd_card_ro_proc_new(chip->card, "via82xx", chip,
2103 			     snd_via82xx_proc_read);
2104 }
2105 
2106 /*
2107  *
2108  */
2109 
2110 static int snd_via82xx_chip_init(struct via82xx *chip)
2111 {
2112 	unsigned int val;
2113 	unsigned long end_time;
2114 	unsigned char pval;
2115 
2116 #if 0 /* broken on K7M? */
2117 	if (chip->chip_type == TYPE_VIA686)
2118 		/* disable all legacy ports */
2119 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2120 #endif
2121 	pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2122 	if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2123 		/* deassert ACLink reset, force SYNC */
2124 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2125 				      VIA_ACLINK_CTRL_ENABLE |
2126 				      VIA_ACLINK_CTRL_RESET |
2127 				      VIA_ACLINK_CTRL_SYNC);
2128 		udelay(100);
2129 #if 1 /* FIXME: should we do full reset here for all chip models? */
2130 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2131 		udelay(100);
2132 #else
2133 		/* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2134 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2135 				      VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2136 		udelay(2);
2137 #endif
2138 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
2139 		/* note - FM data out has trouble with non VRA codecs !! */
2140 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2141 		udelay(100);
2142 	}
2143 
2144 	/* Make sure VRA is enabled, in case we didn't do a
2145 	 * complete codec reset, above */
2146 	pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2147 	if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2148 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
2149 		/* note - FM data out has trouble with non VRA codecs !! */
2150 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2151 		udelay(100);
2152 	}
2153 
2154 	/* wait until codec ready */
2155 	end_time = jiffies + msecs_to_jiffies(750);
2156 	do {
2157 		pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2158 		if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2159 			break;
2160 		schedule_timeout_uninterruptible(1);
2161 	} while (time_before(jiffies, end_time));
2162 
2163 	val = snd_via82xx_codec_xread(chip);
2164 	if (val & VIA_REG_AC97_BUSY)
2165 		dev_err(chip->card->dev,
2166 			"AC'97 codec is not ready [0x%x]\n", val);
2167 
2168 #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2169 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2170 				 VIA_REG_AC97_SECONDARY_VALID |
2171 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2172 	end_time = jiffies + msecs_to_jiffies(750);
2173 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2174 				 VIA_REG_AC97_SECONDARY_VALID |
2175 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2176 	do {
2177 		val = snd_via82xx_codec_xread(chip);
2178 		if (val & VIA_REG_AC97_SECONDARY_VALID) {
2179 			chip->ac97_secondary = 1;
2180 			goto __ac97_ok2;
2181 		}
2182 		schedule_timeout_uninterruptible(1);
2183 	} while (time_before(jiffies, end_time));
2184 	/* This is ok, the most of motherboards have only one codec */
2185 
2186       __ac97_ok2:
2187 #endif
2188 
2189 	if (chip->chip_type == TYPE_VIA686) {
2190 		/* route FM trap to IRQ, disable FM trap */
2191 		pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2192 		/* disable all GPI interrupts */
2193 		outl(0, VIAREG(chip, GPI_INTR));
2194 	}
2195 
2196 	if (chip->chip_type != TYPE_VIA686) {
2197 		/* Workaround for Award BIOS bug:
2198 		 * DXS channels don't work properly with VRA if MC97 is disabled.
2199 		 */
2200 		struct pci_dev *pci;
2201 		pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
2202 		if (pci) {
2203 			unsigned char data;
2204 			pci_read_config_byte(pci, 0x44, &data);
2205 			pci_write_config_byte(pci, 0x44, data | 0x40);
2206 			pci_dev_put(pci);
2207 		}
2208 	}
2209 
2210 	if (chip->chip_type != TYPE_VIA8233A) {
2211 		int i, idx;
2212 		for (idx = 0; idx < 4; idx++) {
2213 			unsigned long port = chip->port + 0x10 * idx;
2214 			for (i = 0; i < 2; i++) {
2215 				chip->playback_volume[idx][i]=chip->playback_volume_c[i];
2216 				outb(chip->playback_volume_c[i],
2217 				     port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
2218 			}
2219 		}
2220 	}
2221 
2222 	return 0;
2223 }
2224 
2225 /*
2226  * power management
2227  */
2228 static int snd_via82xx_suspend(struct device *dev)
2229 {
2230 	struct snd_card *card = dev_get_drvdata(dev);
2231 	struct via82xx *chip = card->private_data;
2232 	int i;
2233 
2234 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2235 	for (i = 0; i < chip->num_devs; i++)
2236 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2237 	snd_ac97_suspend(chip->ac97);
2238 
2239 	/* save misc values */
2240 	if (chip->chip_type != TYPE_VIA686) {
2241 		pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2242 		chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2243 		chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2244 	}
2245 
2246 	return 0;
2247 }
2248 
2249 static int snd_via82xx_resume(struct device *dev)
2250 {
2251 	struct snd_card *card = dev_get_drvdata(dev);
2252 	struct via82xx *chip = card->private_data;
2253 	int i;
2254 
2255 	snd_via82xx_chip_init(chip);
2256 
2257 	if (chip->chip_type == TYPE_VIA686) {
2258 		if (chip->mpu_port_saved)
2259 			pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2260 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2261 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2262 	} else {
2263 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2264 		outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2265 		outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2266 	}
2267 
2268 	snd_ac97_resume(chip->ac97);
2269 
2270 	for (i = 0; i < chip->num_devs; i++)
2271 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2272 
2273 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2274 	return 0;
2275 }
2276 
2277 static DEFINE_SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
2278 
2279 static void snd_via82xx_free(struct snd_card *card)
2280 {
2281 	struct via82xx *chip = card->private_data;
2282 	unsigned int i;
2283 
2284 	/* disable interrupts */
2285 	for (i = 0; i < chip->num_devs; i++)
2286 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2287 
2288 	if (chip->chip_type == TYPE_VIA686) {
2289 		snd_via686_free_gameport(chip);
2290 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2291 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2292 	}
2293 }
2294 
2295 static int snd_via82xx_create(struct snd_card *card,
2296 			      struct pci_dev *pci,
2297 			      int chip_type,
2298 			      int revision,
2299 			      unsigned int ac97_clock)
2300 {
2301 	struct via82xx *chip = card->private_data;
2302 	int err;
2303 
2304 	err = pcim_enable_device(pci);
2305 	if (err < 0)
2306 		return err;
2307 
2308 	chip->chip_type = chip_type;
2309 	chip->revision = revision;
2310 
2311 	spin_lock_init(&chip->reg_lock);
2312 	spin_lock_init(&chip->rates[0].lock);
2313 	spin_lock_init(&chip->rates[1].lock);
2314 	chip->card = card;
2315 	chip->pci = pci;
2316 	chip->irq = -1;
2317 
2318 	pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2319 	pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2320 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2321 			      chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2322 
2323 	err = pcim_request_all_regions(pci, card->driver);
2324 	if (err < 0)
2325 		return err;
2326 	chip->port = pci_resource_start(pci, 0);
2327 	if (devm_request_irq(&pci->dev, pci->irq,
2328 			     chip_type == TYPE_VIA8233 ?
2329 			     snd_via8233_interrupt : snd_via686_interrupt,
2330 			     IRQF_SHARED,
2331 			     KBUILD_MODNAME, chip)) {
2332 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2333 		return -EBUSY;
2334 	}
2335 	chip->irq = pci->irq;
2336 	card->sync_irq = chip->irq;
2337 	card->private_free = snd_via82xx_free;
2338 	if (ac97_clock >= 8000 && ac97_clock <= 48000)
2339 		chip->ac97_clock = ac97_clock;
2340 
2341 	err = snd_via82xx_chip_init(chip);
2342 	if (err < 0)
2343 		return err;
2344 
2345 	/* The 8233 ac97 controller does not implement the master bit
2346 	 * in the pci command register. IMHO this is a violation of the PCI spec.
2347 	 * We call pci_set_master here because it does not hurt. */
2348 	pci_set_master(pci);
2349 	return 0;
2350 }
2351 
2352 struct via823x_info {
2353 	int revision;
2354 	char *name;
2355 	int type;
2356 };
2357 static const struct via823x_info via823x_cards[] = {
2358 	{ VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2359 	{ VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2360 	{ VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2361 	{ VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2362 	{ VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2363 	{ VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
2364 	{ VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
2365 };
2366 
2367 /*
2368  * auto detection of DXS channel supports.
2369  */
2370 
2371 static const struct snd_pci_quirk dxs_allowlist[] = {
2372 	SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2373 	SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2374 	SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2375 	SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
2376 	SND_PCI_QUIRK_VENDOR(0x1019, "ESC K8", VIA_DXS_SRC),
2377 	SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2378 	SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2379 	SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
2380 	SND_PCI_QUIRK_VENDOR(0x1043, "ASUS A7/A8", VIA_DXS_NO_VRA),
2381 	SND_PCI_QUIRK_VENDOR(0x1071, "Diverse Notebook", VIA_DXS_NO_VRA),
2382 	SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
2383 	SND_PCI_QUIRK_VENDOR(0x1106, "ASRock", VIA_DXS_SRC),
2384 	SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
2385 	SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2386 	SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
2387 	SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2388 	SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2389 	SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2390 	SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
2391 	SND_PCI_QUIRK_VENDOR(0x1462, "MSI Mobo", VIA_DXS_SRC),
2392 	SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2393 	SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2394 	SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2395 	SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2396 	SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2397 	SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2398 	SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2399 	SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2400 	SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2401 	SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2402 	SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2403 	SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2404 	SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
2405 	SND_PCI_QUIRK_VENDOR(0x1695, "EPoX mobo", VIA_DXS_SRC),
2406 	SND_PCI_QUIRK_VENDOR(0x16f3, "Jetway K8", VIA_DXS_SRC),
2407 	SND_PCI_QUIRK_VENDOR(0x1734, "FSC Laptop", VIA_DXS_SRC),
2408 	SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
2409 	SND_PCI_QUIRK_VENDOR(0x1849, "ASRock mobo", VIA_DXS_SRC),
2410 	SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8",  VIA_DXS_NO_VRA),
2411 	SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2412 	{ } /* terminator */
2413 };
2414 
2415 static int check_dxs_list(struct pci_dev *pci, int revision)
2416 {
2417 	const struct snd_pci_quirk *w;
2418 
2419 	w = snd_pci_quirk_lookup(pci, dxs_allowlist);
2420 	if (w) {
2421 		dev_dbg(&pci->dev, "DXS allow list for %s found\n",
2422 			    snd_pci_quirk_name(w));
2423 		return w->value;
2424 	}
2425 
2426 	/* for newer revision, default to DXS_SRC */
2427 	if (revision >= VIA_REV_8235)
2428 		return VIA_DXS_SRC;
2429 
2430 	/*
2431 	 * not detected, try 48k rate only to be sure.
2432 	 */
2433 	dev_info(&pci->dev, "Assuming DXS channels with 48k fixed sample rate.\n");
2434 	dev_info(&pci->dev, "         Please try dxs_support=5 option\n");
2435 	dev_info(&pci->dev, "         and report if it works on your machine.\n");
2436 	dev_info(&pci->dev, "         For more details, read ALSA-Configuration.txt.\n");
2437 	return VIA_DXS_48K;
2438 };
2439 
2440 static int __snd_via82xx_probe(struct pci_dev *pci,
2441 			       const struct pci_device_id *pci_id)
2442 {
2443 	struct snd_card *card;
2444 	struct via82xx *chip;
2445 	int chip_type = 0, card_type;
2446 	unsigned int i;
2447 	int err;
2448 
2449 	err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
2450 				sizeof(*chip), &card);
2451 	if (err < 0)
2452 		return err;
2453 	chip = card->private_data;
2454 
2455 	card_type = pci_id->driver_data;
2456 	switch (card_type) {
2457 	case TYPE_CARD_VIA686:
2458 		strscpy(card->driver, "VIA686A");
2459 		sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
2460 		chip_type = TYPE_VIA686;
2461 		break;
2462 	case TYPE_CARD_VIA8233:
2463 		chip_type = TYPE_VIA8233;
2464 		sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
2465 		for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2466 			if (pci->revision == via823x_cards[i].revision) {
2467 				chip_type = via823x_cards[i].type;
2468 				strscpy(card->shortname, via823x_cards[i].name);
2469 				break;
2470 			}
2471 		}
2472 		if (chip_type != TYPE_VIA8233A) {
2473 			if (dxs_support == VIA_DXS_AUTO)
2474 				dxs_support = check_dxs_list(pci, pci->revision);
2475 			/* force to use VIA8233 or 8233A model according to
2476 			 * dxs_support module option
2477 			 */
2478 			if (dxs_support == VIA_DXS_DISABLE)
2479 				chip_type = TYPE_VIA8233A;
2480 			else
2481 				chip_type = TYPE_VIA8233;
2482 		}
2483 		if (chip_type == TYPE_VIA8233A)
2484 			strscpy(card->driver, "VIA8233A");
2485 		else if (pci->revision >= VIA_REV_8237)
2486 			strscpy(card->driver, "VIA8237"); /* no slog assignment */
2487 		else
2488 			strscpy(card->driver, "VIA8233");
2489 		break;
2490 	default:
2491 		dev_err(card->dev, "invalid card type %d\n", card_type);
2492 		return -EINVAL;
2493 	}
2494 
2495 	err = snd_via82xx_create(card, pci, chip_type, pci->revision,
2496 				 ac97_clock);
2497 	if (err < 0)
2498 		return err;
2499 	err = snd_via82xx_mixer_new(chip, ac97_quirk);
2500 	if (err < 0)
2501 		return err;
2502 
2503 	if (chip_type == TYPE_VIA686) {
2504 		err = snd_via686_pcm_new(chip);
2505 		if (err < 0)
2506 			return err;
2507 		err = snd_via686_init_misc(chip);
2508 		if (err < 0)
2509 			return err;
2510 	} else {
2511 		if (chip_type == TYPE_VIA8233A) {
2512 			err = snd_via8233a_pcm_new(chip);
2513 			if (err < 0)
2514 				return err;
2515 			// chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2516 		} else {
2517 			err = snd_via8233_pcm_new(chip);
2518 			if (err < 0)
2519 				return err;
2520 			if (dxs_support == VIA_DXS_48K)
2521 				chip->dxs_fixed = 1;
2522 			else if (dxs_support == VIA_DXS_NO_VRA)
2523 				chip->no_vra = 1;
2524 			else if (dxs_support == VIA_DXS_SRC) {
2525 				chip->no_vra = 1;
2526 				chip->dxs_src = 1;
2527 			}
2528 		}
2529 		err = snd_via8233_init_misc(chip);
2530 		if (err < 0)
2531 			return err;
2532 	}
2533 
2534 	/* disable interrupts */
2535 	for (i = 0; i < chip->num_devs; i++)
2536 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2537 
2538 	snprintf(card->longname, sizeof(card->longname),
2539 		 "%s with %s at %#lx, irq %d", card->shortname,
2540 		 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2541 
2542 	snd_via82xx_proc_init(chip);
2543 
2544 	err = snd_card_register(card);
2545 	if (err < 0)
2546 		return err;
2547 	pci_set_drvdata(pci, card);
2548 	return 0;
2549 }
2550 
2551 static int snd_via82xx_probe(struct pci_dev *pci,
2552 			     const struct pci_device_id *pci_id)
2553 {
2554 	return snd_card_free_on_error(&pci->dev, __snd_via82xx_probe(pci, pci_id));
2555 }
2556 
2557 static struct pci_driver via82xx_driver = {
2558 	.name = KBUILD_MODNAME,
2559 	.id_table = snd_via82xx_ids,
2560 	.probe = snd_via82xx_probe,
2561 	.driver = {
2562 		.pm = &snd_via82xx_pm,
2563 	},
2564 };
2565 
2566 module_pci_driver(via82xx_driver);
2567