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