xref: /linux/sound/pci/via82xx.c (revision 30b35399ceb2398d05837863476dcb12f12f3a82)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *   ALSA driver for VIA VT82xx (South Bridge)
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *   VT82C686A/B/C, VT8233A/C, VT8235
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
71da177e4SLinus Torvalds  *	                   Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
81da177e4SLinus Torvalds  *                    2002 Takashi Iwai <tiwai@suse.de>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
121da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
131da177e4SLinus Torvalds  *   (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
161da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
171da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181da177e4SLinus Torvalds  *   GNU General Public License for more details.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
211da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
221da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  * Changes:
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * Dec. 19, 2002	Takashi Iwai <tiwai@suse.de>
301da177e4SLinus Torvalds  *	- use the DSX channels for the first pcm playback.
311da177e4SLinus Torvalds  *	  (on VIA8233, 8233C and 8235 only)
321da177e4SLinus Torvalds  *	  this will allow you play simultaneously up to 4 streams.
331da177e4SLinus Torvalds  *	  multi-channel playback is assigned to the second device
341da177e4SLinus Torvalds  *	  on these chips.
351da177e4SLinus Torvalds  *	- support the secondary capture (on VIA8233/C,8235)
361da177e4SLinus Torvalds  *	- SPDIF support
371da177e4SLinus Torvalds  *	  the DSX3 channel can be used for SPDIF output.
381da177e4SLinus Torvalds  *	  on VIA8233A, this channel is assigned to the second pcm
391da177e4SLinus Torvalds  *	  playback.
401da177e4SLinus Torvalds  *	  the card config of alsa-lib will assign the correct
411da177e4SLinus Torvalds  *	  device for applications.
421da177e4SLinus Torvalds  *	- clean up the code, separate low-level initialization
431da177e4SLinus Torvalds  *	  routines for each chipset.
444f550df5SKarsten Wiese  *
454f550df5SKarsten Wiese  * Sep. 26, 2005	Karsten Wiese <annabellesgarden@yahoo.de>
464f550df5SKarsten Wiese  *	- Optimize position calculation for the 823x chips.
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <sound/driver.h>
501da177e4SLinus Torvalds #include <asm/io.h>
511da177e4SLinus Torvalds #include <linux/delay.h>
521da177e4SLinus Torvalds #include <linux/interrupt.h>
531da177e4SLinus Torvalds #include <linux/init.h>
541da177e4SLinus Torvalds #include <linux/pci.h>
551da177e4SLinus Torvalds #include <linux/slab.h>
561da177e4SLinus Torvalds #include <linux/gameport.h>
571da177e4SLinus Torvalds #include <linux/moduleparam.h>
581da177e4SLinus Torvalds #include <sound/core.h>
591da177e4SLinus Torvalds #include <sound/pcm.h>
601da177e4SLinus Torvalds #include <sound/pcm_params.h>
611da177e4SLinus Torvalds #include <sound/info.h>
627058c042STakashi Iwai #include <sound/tlv.h>
631da177e4SLinus Torvalds #include <sound/ac97_codec.h>
641da177e4SLinus Torvalds #include <sound/mpu401.h>
651da177e4SLinus Torvalds #include <sound/initval.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #if 0
681da177e4SLinus Torvalds #define POINTER_DEBUG
691da177e4SLinus Torvalds #endif
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
721da177e4SLinus Torvalds MODULE_DESCRIPTION("VIA VT82xx audio");
731da177e4SLinus Torvalds MODULE_LICENSE("GPL");
741da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
771da177e4SLinus Torvalds #define SUPPORT_JOYSTICK 1
781da177e4SLinus Torvalds #endif
791da177e4SLinus Torvalds 
80b7fe4622SClemens Ladisch static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
81b7fe4622SClemens Ladisch static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
82b7fe4622SClemens Ladisch static long mpu_port;
831da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
84b7fe4622SClemens Ladisch static int joystick;
851da177e4SLinus Torvalds #endif
86b7fe4622SClemens Ladisch static int ac97_clock = 48000;
87b7fe4622SClemens Ladisch static char *ac97_quirk;
88b7fe4622SClemens Ladisch static int dxs_support;
891da177e4SLinus Torvalds 
90b7fe4622SClemens Ladisch module_param(index, int, 0444);
911da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
92b7fe4622SClemens Ladisch module_param(id, charp, 0444);
931da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
94b7fe4622SClemens Ladisch module_param(mpu_port, long, 0444);
951da177e4SLinus Torvalds MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
961da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
97b7fe4622SClemens Ladisch module_param(joystick, bool, 0444);
981da177e4SLinus Torvalds MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
991da177e4SLinus Torvalds #endif
100b7fe4622SClemens Ladisch module_param(ac97_clock, int, 0444);
1011da177e4SLinus Torvalds MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
102b7fe4622SClemens Ladisch module_param(ac97_quirk, charp, 0444);
1031da177e4SLinus Torvalds MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
104b7fe4622SClemens Ladisch module_param(dxs_support, int, 0444);
1052d7eb7cbSSergey Vlasov 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)");
1061da177e4SLinus Torvalds 
1072b3e584bSTakashi Iwai /* just for backward compatibility */
1082b3e584bSTakashi Iwai static int enable;
109698444f3STakashi Iwai module_param(enable, bool, 0444);
1102b3e584bSTakashi Iwai 
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* revision numbers for via686 */
1131da177e4SLinus Torvalds #define VIA_REV_686_A		0x10
1141da177e4SLinus Torvalds #define VIA_REV_686_B		0x11
1151da177e4SLinus Torvalds #define VIA_REV_686_C		0x12
1161da177e4SLinus Torvalds #define VIA_REV_686_D		0x13
1171da177e4SLinus Torvalds #define VIA_REV_686_E		0x14
1181da177e4SLinus Torvalds #define VIA_REV_686_H		0x20
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /* revision numbers for via8233 */
1211da177e4SLinus Torvalds #define VIA_REV_PRE_8233	0x10	/* not in market */
1221da177e4SLinus Torvalds #define VIA_REV_8233C		0x20	/* 2 rec, 4 pb, 1 multi-pb */
1231da177e4SLinus Torvalds #define VIA_REV_8233		0x30	/* 2 rec, 4 pb, 1 multi-pb, spdif */
1241da177e4SLinus Torvalds #define VIA_REV_8233A		0x40	/* 1 rec, 1 multi-pb, spdf */
1251da177e4SLinus Torvalds #define VIA_REV_8235		0x50	/* 2 rec, 4 pb, 1 multi-pb, spdif */
1261da177e4SLinus Torvalds #define VIA_REV_8237		0x60
1278263c65fSBastiaan Jacques #define VIA_REV_8251		0x70
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds  *  Direct registers
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
1341da177e4SLinus Torvalds #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /* common offsets */
1371da177e4SLinus Torvalds #define VIA_REG_OFFSET_STATUS		0x00	/* byte - channel status */
1381da177e4SLinus Torvalds #define   VIA_REG_STAT_ACTIVE		0x80	/* RO */
1394f550df5SKarsten Wiese #define   VIA8233_SHADOW_STAT_ACTIVE	0x08	/* RO */
1401da177e4SLinus Torvalds #define   VIA_REG_STAT_PAUSED		0x40	/* RO */
1411da177e4SLinus Torvalds #define   VIA_REG_STAT_TRIGGER_QUEUED	0x08	/* RO */
1421da177e4SLinus Torvalds #define   VIA_REG_STAT_STOPPED		0x04	/* RWC */
1431da177e4SLinus Torvalds #define   VIA_REG_STAT_EOL		0x02	/* RWC */
1441da177e4SLinus Torvalds #define   VIA_REG_STAT_FLAG		0x01	/* RWC */
1451da177e4SLinus Torvalds #define VIA_REG_OFFSET_CONTROL		0x01	/* byte - channel control */
1461da177e4SLinus Torvalds #define   VIA_REG_CTRL_START		0x80	/* WO */
1471da177e4SLinus Torvalds #define   VIA_REG_CTRL_TERMINATE	0x40	/* WO */
1481da177e4SLinus Torvalds #define   VIA_REG_CTRL_AUTOSTART	0x20
1491da177e4SLinus Torvalds #define   VIA_REG_CTRL_PAUSE		0x08	/* RW */
1501da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT_STOP		0x04
1511da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT_EOL		0x02
1521da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT_FLAG		0x01
1531da177e4SLinus Torvalds #define   VIA_REG_CTRL_RESET		0x01	/* RW - probably reset? undocumented */
1541da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
1551da177e4SLinus Torvalds #define VIA_REG_OFFSET_TYPE		0x02	/* byte - channel type (686 only) */
1561da177e4SLinus Torvalds #define   VIA_REG_TYPE_AUTOSTART	0x80	/* RW - autostart at EOL */
1571da177e4SLinus Torvalds #define   VIA_REG_TYPE_16BIT		0x20	/* RW */
1581da177e4SLinus Torvalds #define   VIA_REG_TYPE_STEREO		0x10	/* RW */
1591da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_LLINE	0x00
1601da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_LSAMPLE	0x04
1611da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_LESSONE	0x08
1621da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_MASK		0x0c
1631da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_EOL		0x02
1641da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_FLAG		0x01
1651da177e4SLinus Torvalds #define VIA_REG_OFFSET_TABLE_PTR	0x04	/* dword - channel table pointer */
1661da177e4SLinus Torvalds #define VIA_REG_OFFSET_CURR_PTR		0x04	/* dword - channel current pointer */
1671da177e4SLinus Torvalds #define VIA_REG_OFFSET_STOP_IDX		0x08	/* dword - stop index, channel type, sample rate */
1681da177e4SLinus Torvalds #define   VIA8233_REG_TYPE_16BIT	0x00200000	/* RW */
1691da177e4SLinus Torvalds #define   VIA8233_REG_TYPE_STEREO	0x00100000	/* RW */
1701da177e4SLinus Torvalds #define VIA_REG_OFFSET_CURR_COUNT	0x0c	/* dword - channel current count (24 bit) */
1711da177e4SLinus Torvalds #define VIA_REG_OFFSET_CURR_INDEX	0x0f	/* byte - channel current index (for via8233 only) */
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds #define DEFINE_VIA_REGSET(name,val) \
1741da177e4SLinus Torvalds enum {\
1751da177e4SLinus Torvalds 	VIA_REG_##name##_STATUS		= (val),\
1761da177e4SLinus Torvalds 	VIA_REG_##name##_CONTROL	= (val) + 0x01,\
1771da177e4SLinus Torvalds 	VIA_REG_##name##_TYPE		= (val) + 0x02,\
1781da177e4SLinus Torvalds 	VIA_REG_##name##_TABLE_PTR	= (val) + 0x04,\
1791da177e4SLinus Torvalds 	VIA_REG_##name##_CURR_PTR	= (val) + 0x04,\
1801da177e4SLinus Torvalds 	VIA_REG_##name##_STOP_IDX	= (val) + 0x08,\
1811da177e4SLinus Torvalds 	VIA_REG_##name##_CURR_COUNT	= (val) + 0x0c,\
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds /* playback block */
1851da177e4SLinus Torvalds DEFINE_VIA_REGSET(PLAYBACK, 0x00);
1861da177e4SLinus Torvalds DEFINE_VIA_REGSET(CAPTURE, 0x10);
1871da177e4SLinus Torvalds DEFINE_VIA_REGSET(FM, 0x20);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /* AC'97 */
1901da177e4SLinus Torvalds #define VIA_REG_AC97			0x80	/* dword */
1911da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_MASK	(3<<30)
1921da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_SHIFT	30
1931da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_PRIMARY	0x00
1941da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
1951da177e4SLinus Torvalds #define   VIA_REG_AC97_SECONDARY_VALID	(1<<27)
1961da177e4SLinus Torvalds #define   VIA_REG_AC97_PRIMARY_VALID	(1<<25)
1971da177e4SLinus Torvalds #define   VIA_REG_AC97_BUSY		(1<<24)
1981da177e4SLinus Torvalds #define   VIA_REG_AC97_READ		(1<<23)
1991da177e4SLinus Torvalds #define   VIA_REG_AC97_CMD_SHIFT	16
2001da177e4SLinus Torvalds #define   VIA_REG_AC97_CMD_MASK		0x7e
2011da177e4SLinus Torvalds #define   VIA_REG_AC97_DATA_SHIFT	0
2021da177e4SLinus Torvalds #define   VIA_REG_AC97_DATA_MASK	0xffff
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds #define VIA_REG_SGD_SHADOW		0x84	/* dword */
2051da177e4SLinus Torvalds /* via686 */
2061da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_FLAG	(1<<0)
2071da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_FLAG	(1<<1)
2081da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_FLAG	(1<<2)
2091da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_EOL	(1<<4)
2101da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_EOL	(1<<5)
2111da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_EOL	(1<<6)
2121da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_STOP	(1<<8)
2131da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_STOP	(1<<9)
2141da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_STOP	(1<<10)
2151da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_ACTIVE	(1<<12)
2161da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_ACTIVE	(1<<13)
2171da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_ACTIVE	(1<<14)
2181da177e4SLinus Torvalds /* via8233 */
2191da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_FLAG	(1<<0)
2201da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_EOL	(1<<1)
2211da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_STOP	(1<<2)
2221da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_ACTIVE	(1<<3)
2231da177e4SLinus Torvalds #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
2241da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_SDX	0
2251da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_MULTI	4
2261da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_REC	6
2271da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_REC1	7
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds #define VIA_REG_GPI_STATUS		0x88
2301da177e4SLinus Torvalds #define VIA_REG_GPI_INTR		0x8c
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds /* multi-channel and capture registers for via8233 */
2331da177e4SLinus Torvalds DEFINE_VIA_REGSET(MULTPLAY, 0x40);
2341da177e4SLinus Torvalds DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /* via8233-specific registers */
2371da177e4SLinus Torvalds #define VIA_REG_OFS_PLAYBACK_VOLUME_L	0x02	/* byte */
2381da177e4SLinus Torvalds #define VIA_REG_OFS_PLAYBACK_VOLUME_R	0x03	/* byte */
2391da177e4SLinus Torvalds #define VIA_REG_OFS_MULTPLAY_FORMAT	0x02	/* byte - format and channels */
2401da177e4SLinus Torvalds #define   VIA_REG_MULTPLAY_FMT_8BIT	0x00
2411da177e4SLinus Torvalds #define   VIA_REG_MULTPLAY_FMT_16BIT	0x80
2421da177e4SLinus Torvalds #define   VIA_REG_MULTPLAY_FMT_CH_MASK	0x70	/* # channels << 4 (valid = 1,2,4,6) */
2431da177e4SLinus Torvalds #define VIA_REG_OFS_CAPTURE_FIFO	0x02	/* byte - bit 6 = fifo  enable */
2441da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_FIFO_ENABLE	0x40
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds #define VIA_DXS_MAX_VOLUME		31	/* max. volume (attenuation) of reg 0x32/33 */
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds #define VIA_REG_CAPTURE_CHANNEL		0x63	/* byte - input select */
2491da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_CHANNEL_MIC	0x4
2501da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_CHANNEL_LINE	0
2511da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_SELECT_CODEC	0x03	/* recording source codec (0 = primary) */
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds #define VIA_TBL_BIT_FLAG	0x40000000
2541da177e4SLinus Torvalds #define VIA_TBL_BIT_EOL		0x80000000
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /* pci space */
2571da177e4SLinus Torvalds #define VIA_ACLINK_STAT		0x40
2581da177e4SLinus Torvalds #define  VIA_ACLINK_C11_READY	0x20
2591da177e4SLinus Torvalds #define  VIA_ACLINK_C10_READY	0x10
2601da177e4SLinus Torvalds #define  VIA_ACLINK_C01_READY	0x04 /* secondary codec ready */
2611da177e4SLinus Torvalds #define  VIA_ACLINK_LOWPOWER	0x02 /* low-power state */
2621da177e4SLinus Torvalds #define  VIA_ACLINK_C00_READY	0x01 /* primary codec ready */
2631da177e4SLinus Torvalds #define VIA_ACLINK_CTRL		0x41
2641da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_ENABLE	0x80 /* 0: disable, 1: enable */
2651da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_RESET	0x40 /* 0: assert, 1: de-assert */
2661da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_SYNC	0x20 /* 0: release SYNC, 1: force SYNC hi */
2671da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_SDO	0x10 /* 0: release SDO, 1: force SDO hi */
2681da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_VRA	0x08 /* 0: disable VRA, 1: enable VRA */
2691da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_PCM	0x04 /* 0: disable PCM, 1: enable PCM */
2701da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_FM	0x02 /* via686 only */
2711da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_SB	0x01 /* via686 only */
2721da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_INIT	(VIA_ACLINK_CTRL_ENABLE|\
2731da177e4SLinus Torvalds 				 VIA_ACLINK_CTRL_RESET|\
2741da177e4SLinus Torvalds 				 VIA_ACLINK_CTRL_PCM|\
2751da177e4SLinus Torvalds 				 VIA_ACLINK_CTRL_VRA)
2761da177e4SLinus Torvalds #define VIA_FUNC_ENABLE		0x42
2771da177e4SLinus Torvalds #define  VIA_FUNC_MIDI_PNP	0x80 /* FIXME: it's 0x40 in the datasheet! */
2781da177e4SLinus Torvalds #define  VIA_FUNC_MIDI_IRQMASK	0x40 /* FIXME: not documented! */
2791da177e4SLinus Torvalds #define  VIA_FUNC_RX2C_WRITE	0x20
2801da177e4SLinus Torvalds #define  VIA_FUNC_SB_FIFO_EMPTY	0x10
2811da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_GAME	0x08
2821da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_FM	0x04
2831da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_MIDI	0x02
2841da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_SB	0x01
2851da177e4SLinus Torvalds #define VIA_PNP_CONTROL		0x43
2861da177e4SLinus Torvalds #define VIA_FM_NMI_CTRL		0x48
2871da177e4SLinus Torvalds #define VIA8233_VOLCHG_CTRL	0x48
2881da177e4SLinus Torvalds #define VIA8233_SPDIF_CTRL	0x49
2891da177e4SLinus Torvalds #define  VIA8233_SPDIF_DX3	0x08
2901da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_MASK	0x03
2911da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_1011	0x00
2921da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_34		0x01
2931da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_78		0x02
2941da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_69		0x03
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  */
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds #define VIA_DXS_AUTO	0
3001da177e4SLinus Torvalds #define VIA_DXS_ENABLE	1
3011da177e4SLinus Torvalds #define VIA_DXS_DISABLE	2
3021da177e4SLinus Torvalds #define VIA_DXS_48K	3
3031da177e4SLinus Torvalds #define VIA_DXS_NO_VRA	4
3042d7eb7cbSSergey Vlasov #define VIA_DXS_SRC	5
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /*
3081da177e4SLinus Torvalds  * pcm stream
3091da177e4SLinus Torvalds  */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds struct snd_via_sg_table {
3121da177e4SLinus Torvalds 	unsigned int offset;
3131da177e4SLinus Torvalds 	unsigned int size;
3141da177e4SLinus Torvalds } ;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds #define VIA_TABLE_SIZE	255
3171da177e4SLinus Torvalds 
318e437e3d7STakashi Iwai struct viadev {
3191da177e4SLinus Torvalds 	unsigned int reg_offset;
3201da177e4SLinus Torvalds 	unsigned long port;
3211da177e4SLinus Torvalds 	int direction;	/* playback = 0, capture = 1 */
322e437e3d7STakashi Iwai         struct snd_pcm_substream *substream;
3231da177e4SLinus Torvalds 	int running;
3241da177e4SLinus Torvalds 	unsigned int tbl_entries; /* # descriptors */
3251da177e4SLinus Torvalds 	struct snd_dma_buffer table;
3261da177e4SLinus Torvalds 	struct snd_via_sg_table *idx_table;
3271da177e4SLinus Torvalds 	/* for recovery from the unexpected pointer */
3281da177e4SLinus Torvalds 	unsigned int lastpos;
3291da177e4SLinus Torvalds 	unsigned int fragsize;
3301da177e4SLinus Torvalds 	unsigned int bufsize;
3311da177e4SLinus Torvalds 	unsigned int bufsize2;
3324f550df5SKarsten Wiese 	int hwptr_done;		/* processed frame position in the buffer */
3334f550df5SKarsten Wiese 	int in_interrupt;
3344f550df5SKarsten Wiese 	int shadow_shift;
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
3391da177e4SLinus Torvalds enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds #define VIA_MAX_DEVS	7	/* 4 playback, 1 multi, 2 capture */
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds struct via_rate_lock {
3441da177e4SLinus Torvalds 	spinlock_t lock;
3451da177e4SLinus Torvalds 	int rate;
3461da177e4SLinus Torvalds 	int used;
3471da177e4SLinus Torvalds };
3481da177e4SLinus Torvalds 
349e437e3d7STakashi Iwai struct via82xx {
3501da177e4SLinus Torvalds 	int irq;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	unsigned long port;
3531da177e4SLinus Torvalds 	struct resource *mpu_res;
3541da177e4SLinus Torvalds 	int chip_type;
3551da177e4SLinus Torvalds 	unsigned char revision;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	unsigned char old_legacy;
3581da177e4SLinus Torvalds 	unsigned char old_legacy_cfg;
3591da177e4SLinus Torvalds #ifdef CONFIG_PM
3601da177e4SLinus Torvalds 	unsigned char legacy_saved;
3611da177e4SLinus Torvalds 	unsigned char legacy_cfg_saved;
3621da177e4SLinus Torvalds 	unsigned char spdif_ctrl_saved;
3631da177e4SLinus Torvalds 	unsigned char capture_src_saved[2];
3641da177e4SLinus Torvalds 	unsigned int mpu_port_saved;
3651da177e4SLinus Torvalds #endif
3661da177e4SLinus Torvalds 
36700f226d4SHonza Maly 	unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
36800f226d4SHonza Maly 	unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	struct pci_dev *pci;
373e437e3d7STakashi Iwai 	struct snd_card *card;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	unsigned int num_devs;
3761da177e4SLinus Torvalds 	unsigned int playback_devno, multi_devno, capture_devno;
377e437e3d7STakashi Iwai 	struct viadev devs[VIA_MAX_DEVS];
3781da177e4SLinus Torvalds 	struct via_rate_lock rates[2]; /* playback and capture */
3791da177e4SLinus Torvalds 	unsigned int dxs_fixed: 1;	/* DXS channel accepts only 48kHz */
3801da177e4SLinus Torvalds 	unsigned int no_vra: 1;		/* no need to set VRA on DXS channels */
3812d7eb7cbSSergey Vlasov 	unsigned int dxs_src: 1;	/* use full SRC capabilities of DXS */
3821da177e4SLinus Torvalds 	unsigned int spdif_on: 1;	/* only spdif rates work to external DACs */
3831da177e4SLinus Torvalds 
384e437e3d7STakashi Iwai 	struct snd_pcm *pcms[2];
385e437e3d7STakashi Iwai 	struct snd_rawmidi *rmidi;
3861da177e4SLinus Torvalds 
387e437e3d7STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
388e437e3d7STakashi Iwai 	struct snd_ac97 *ac97;
3891da177e4SLinus Torvalds 	unsigned int ac97_clock;
3901da177e4SLinus Torvalds 	unsigned int ac97_secondary;	/* secondary AC'97 codec is present */
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	spinlock_t reg_lock;
393e437e3d7STakashi Iwai 	struct snd_info_entry *proc_entry;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
3961da177e4SLinus Torvalds 	struct gameport *gameport;
3971da177e4SLinus Torvalds #endif
3981da177e4SLinus Torvalds };
3991da177e4SLinus Torvalds 
400f40b6890STakashi Iwai static struct pci_device_id snd_via82xx_ids[] = {
4014f550df5SKarsten Wiese 	/* 0x1106, 0x3058 */
4024f550df5SKarsten Wiese 	{ PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, },	/* 686A */
4034f550df5SKarsten Wiese 	/* 0x1106, 0x3059 */
4044f550df5SKarsten Wiese 	{ PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, },	/* VT8233 */
4051da177e4SLinus Torvalds 	{ 0, }
4061da177e4SLinus Torvalds };
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  */
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds /*
4141da177e4SLinus Torvalds  * allocate and initialize the descriptor buffers
4151da177e4SLinus Torvalds  * periods = number of periods
4161da177e4SLinus Torvalds  * fragsize = period size in bytes
4171da177e4SLinus Torvalds  */
418e437e3d7STakashi Iwai static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
4191da177e4SLinus Torvalds 			   struct pci_dev *pci,
4201da177e4SLinus Torvalds 			   unsigned int periods, unsigned int fragsize)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	unsigned int i, idx, ofs, rest;
423e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
4241da177e4SLinus Torvalds 	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	if (dev->table.area == NULL) {
4271da177e4SLinus Torvalds 		/* the start of each lists must be aligned to 8 bytes,
4281da177e4SLinus Torvalds 		 * but the kernel pages are much bigger, so we don't care
4291da177e4SLinus Torvalds 		 */
4301da177e4SLinus Torvalds 		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
4311da177e4SLinus Torvalds 					PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
4321da177e4SLinus Torvalds 					&dev->table) < 0)
4331da177e4SLinus Torvalds 			return -ENOMEM;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 	if (! dev->idx_table) {
4361da177e4SLinus Torvalds 		dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
4371da177e4SLinus Torvalds 		if (! dev->idx_table)
4381da177e4SLinus Torvalds 			return -ENOMEM;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	/* fill the entries */
4421da177e4SLinus Torvalds 	idx = 0;
4431da177e4SLinus Torvalds 	ofs = 0;
4441da177e4SLinus Torvalds 	for (i = 0; i < periods; i++) {
4451da177e4SLinus Torvalds 		rest = fragsize;
4461da177e4SLinus Torvalds 		/* fill descriptors for a period.
4471da177e4SLinus Torvalds 		 * a period can be split to several descriptors if it's
4481da177e4SLinus Torvalds 		 * over page boundary.
4491da177e4SLinus Torvalds 		 */
4501da177e4SLinus Torvalds 		do {
4511da177e4SLinus Torvalds 			unsigned int r;
4521da177e4SLinus Torvalds 			unsigned int flag;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 			if (idx >= VIA_TABLE_SIZE) {
4551da177e4SLinus Torvalds 				snd_printk(KERN_ERR "via82xx: too much table size!\n");
4561da177e4SLinus Torvalds 				return -EINVAL;
4571da177e4SLinus Torvalds 			}
4581da177e4SLinus Torvalds 			((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
4591da177e4SLinus Torvalds 			r = PAGE_SIZE - (ofs % PAGE_SIZE);
4601da177e4SLinus Torvalds 			if (rest < r)
4611da177e4SLinus Torvalds 				r = rest;
4621da177e4SLinus Torvalds 			rest -= r;
4631da177e4SLinus Torvalds 			if (! rest) {
4641da177e4SLinus Torvalds 				if (i == periods - 1)
4651da177e4SLinus Torvalds 					flag = VIA_TBL_BIT_EOL; /* buffer boundary */
4661da177e4SLinus Torvalds 				else
4671da177e4SLinus Torvalds 					flag = VIA_TBL_BIT_FLAG; /* period boundary */
4681da177e4SLinus Torvalds 			} else
4691da177e4SLinus Torvalds 				flag = 0; /* period continues to the next */
4701da177e4SLinus Torvalds 			// printk("via: tbl %d: at %d  size %d (rest %d)\n", idx, ofs, r, rest);
4711da177e4SLinus Torvalds 			((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
4721da177e4SLinus Torvalds 			dev->idx_table[idx].offset = ofs;
4731da177e4SLinus Torvalds 			dev->idx_table[idx].size = r;
4741da177e4SLinus Torvalds 			ofs += r;
4751da177e4SLinus Torvalds 			idx++;
4761da177e4SLinus Torvalds 		} while (rest > 0);
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 	dev->tbl_entries = idx;
4791da177e4SLinus Torvalds 	dev->bufsize = periods * fragsize;
4801da177e4SLinus Torvalds 	dev->bufsize2 = dev->bufsize / 2;
4811da177e4SLinus Torvalds 	dev->fragsize = fragsize;
4821da177e4SLinus Torvalds 	return 0;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 
486e437e3d7STakashi Iwai static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
4871da177e4SLinus Torvalds 			   struct pci_dev *pci)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	if (dev->table.area) {
4901da177e4SLinus Torvalds 		snd_dma_free_pages(&dev->table);
4911da177e4SLinus Torvalds 		dev->table.area = NULL;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 	kfree(dev->idx_table);
4941da177e4SLinus Torvalds 	dev->idx_table = NULL;
4951da177e4SLinus Torvalds 	return 0;
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /*
4991da177e4SLinus Torvalds  *  Basic I/O
5001da177e4SLinus Torvalds  */
5011da177e4SLinus Torvalds 
502e437e3d7STakashi Iwai static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	return inl(VIAREG(chip, AC97));
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
507e437e3d7STakashi Iwai static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	outl(val, VIAREG(chip, AC97));
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
512e437e3d7STakashi Iwai static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	unsigned int timeout = 1000;	/* 1ms */
5151da177e4SLinus Torvalds 	unsigned int val;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	while (timeout-- > 0) {
5181da177e4SLinus Torvalds 		udelay(1);
5191da177e4SLinus Torvalds 		if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
5201da177e4SLinus Torvalds 			return val & 0xffff;
5211da177e4SLinus Torvalds 	}
522e437e3d7STakashi Iwai 	snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
523e437e3d7STakashi Iwai 		   secondary, snd_via82xx_codec_xread(chip));
5241da177e4SLinus Torvalds 	return -EIO;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
527e437e3d7STakashi Iwai static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	unsigned int timeout = 1000;	/* 1ms */
5301da177e4SLinus Torvalds 	unsigned int val, val1;
5311da177e4SLinus Torvalds 	unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
5321da177e4SLinus Torvalds 					 VIA_REG_AC97_SECONDARY_VALID;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	while (timeout-- > 0) {
5351da177e4SLinus Torvalds 		val = snd_via82xx_codec_xread(chip);
5361da177e4SLinus Torvalds 		val1 = val & (VIA_REG_AC97_BUSY | stat);
5371da177e4SLinus Torvalds 		if (val1 == stat)
5381da177e4SLinus Torvalds 			return val & 0xffff;
5391da177e4SLinus Torvalds 		udelay(1);
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 	return -EIO;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
544e437e3d7STakashi Iwai static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
5451da177e4SLinus Torvalds {
546e437e3d7STakashi Iwai 	struct via82xx *chip = ac97->private_data;
5471da177e4SLinus Torvalds 	int err;
5481da177e4SLinus Torvalds 	err = snd_via82xx_codec_ready(chip, ac97->num);
5491da177e4SLinus Torvalds 	/* here we need to wait fairly for long time.. */
550ef21ca24SNishanth Aravamudan 	msleep(500);
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
553e437e3d7STakashi Iwai static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
5541da177e4SLinus Torvalds 				    unsigned short reg,
5551da177e4SLinus Torvalds 				    unsigned short val)
5561da177e4SLinus Torvalds {
557e437e3d7STakashi Iwai 	struct via82xx *chip = ac97->private_data;
5581da177e4SLinus Torvalds 	unsigned int xval;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
5611da177e4SLinus Torvalds 	xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
5621da177e4SLinus Torvalds 	xval |= reg << VIA_REG_AC97_CMD_SHIFT;
5631da177e4SLinus Torvalds 	xval |= val << VIA_REG_AC97_DATA_SHIFT;
5641da177e4SLinus Torvalds 	snd_via82xx_codec_xwrite(chip, xval);
5651da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, ac97->num);
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
568e437e3d7STakashi Iwai static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
5691da177e4SLinus Torvalds {
570e437e3d7STakashi Iwai 	struct via82xx *chip = ac97->private_data;
5711da177e4SLinus Torvalds 	unsigned int xval, val = 0xffff;
5721da177e4SLinus Torvalds 	int again = 0;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
5751da177e4SLinus Torvalds 	xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
5761da177e4SLinus Torvalds 	xval |= VIA_REG_AC97_READ;
5771da177e4SLinus Torvalds 	xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
5781da177e4SLinus Torvalds       	while (1) {
5791da177e4SLinus Torvalds       		if (again++ > 3) {
580e437e3d7STakashi Iwai 			snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
581e437e3d7STakashi Iwai 				   ac97->num, snd_via82xx_codec_xread(chip));
5821da177e4SLinus Torvalds 		      	return 0xffff;
5831da177e4SLinus Torvalds 		}
5841da177e4SLinus Torvalds 		snd_via82xx_codec_xwrite(chip, xval);
5851da177e4SLinus Torvalds 		udelay (20);
5861da177e4SLinus Torvalds 		if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
5871da177e4SLinus Torvalds 			udelay(25);
5881da177e4SLinus Torvalds 			val = snd_via82xx_codec_xread(chip);
5891da177e4SLinus Torvalds 			break;
5901da177e4SLinus Torvalds 		}
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 	return val & 0xffff;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
595e437e3d7STakashi Iwai static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
5981da177e4SLinus Torvalds 	     VIADEV_REG(viadev, OFFSET_CONTROL));
5991da177e4SLinus Torvalds 	inb(VIADEV_REG(viadev, OFFSET_CONTROL));
6001da177e4SLinus Torvalds 	udelay(50);
6011da177e4SLinus Torvalds 	/* disable interrupts */
6021da177e4SLinus Torvalds 	outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
6031da177e4SLinus Torvalds 	/* clear interrupts */
6041da177e4SLinus Torvalds 	outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
6051da177e4SLinus Torvalds 	outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
6061da177e4SLinus Torvalds 	// outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
6071da177e4SLinus Torvalds 	viadev->lastpos = 0;
6084f550df5SKarsten Wiese 	viadev->hwptr_done = 0;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /*
6131da177e4SLinus Torvalds  *  Interrupt handler
6144f550df5SKarsten Wiese  *  Used for 686 and 8233A
6151da177e4SLinus Torvalds  */
6167d12e780SDavid Howells static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
6171da177e4SLinus Torvalds {
618e437e3d7STakashi Iwai 	struct via82xx *chip = dev_id;
6191da177e4SLinus Torvalds 	unsigned int status;
6201da177e4SLinus Torvalds 	unsigned int i;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	status = inl(VIAREG(chip, SGD_SHADOW));
6231da177e4SLinus Torvalds 	if (! (status & chip->intr_mask)) {
6241da177e4SLinus Torvalds 		if (chip->rmidi)
6251da177e4SLinus Torvalds 			/* check mpu401 interrupt */
6267d12e780SDavid Howells 			return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
6271da177e4SLinus Torvalds 		return IRQ_NONE;
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	/* check status for each stream */
6311da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
6321da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++) {
633e437e3d7STakashi Iwai 		struct viadev *viadev = &chip->devs[i];
6341da177e4SLinus Torvalds 		unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
6354f550df5SKarsten Wiese 		if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
6361da177e4SLinus Torvalds 			continue;
6371da177e4SLinus Torvalds 		if (viadev->substream && viadev->running) {
6384f550df5SKarsten Wiese 			/*
6394f550df5SKarsten Wiese 			 * Update hwptr_done based on 'period elapsed'
6404f550df5SKarsten Wiese 			 * interrupts. We'll use it, when the chip returns 0
6414f550df5SKarsten Wiese 			 * for OFFSET_CURR_COUNT.
6424f550df5SKarsten Wiese 			 */
6434f550df5SKarsten Wiese 			if (c_status & VIA_REG_STAT_EOL)
6444f550df5SKarsten Wiese 				viadev->hwptr_done = 0;
6454f550df5SKarsten Wiese 			else
6464f550df5SKarsten Wiese 				viadev->hwptr_done += viadev->fragsize;
6474f550df5SKarsten Wiese 			viadev->in_interrupt = c_status;
6481da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
6491da177e4SLinus Torvalds 			snd_pcm_period_elapsed(viadev->substream);
6501da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
6514f550df5SKarsten Wiese 			viadev->in_interrupt = 0;
6521da177e4SLinus Torvalds 		}
6531da177e4SLinus Torvalds 		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
6561da177e4SLinus Torvalds 	return IRQ_HANDLED;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds /*
6604f550df5SKarsten Wiese  *  Interrupt handler
6614f550df5SKarsten Wiese  */
6627d12e780SDavid Howells static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
6634f550df5SKarsten Wiese {
664e437e3d7STakashi Iwai 	struct via82xx *chip = dev_id;
6654f550df5SKarsten Wiese 	unsigned int status;
6664f550df5SKarsten Wiese 	unsigned int i;
6674f550df5SKarsten Wiese 	int irqreturn = 0;
6684f550df5SKarsten Wiese 
6694f550df5SKarsten Wiese 	/* check status for each stream */
6704f550df5SKarsten Wiese 	spin_lock(&chip->reg_lock);
6714f550df5SKarsten Wiese 	status = inl(VIAREG(chip, SGD_SHADOW));
6724f550df5SKarsten Wiese 
6734f550df5SKarsten Wiese 	for (i = 0; i < chip->num_devs; i++) {
674e437e3d7STakashi Iwai 		struct viadev *viadev = &chip->devs[i];
675e437e3d7STakashi Iwai 		struct snd_pcm_substream *substream;
6764f550df5SKarsten Wiese 		unsigned char c_status, shadow_status;
6774f550df5SKarsten Wiese 
6784f550df5SKarsten Wiese 		shadow_status = (status >> viadev->shadow_shift) &
6794f550df5SKarsten Wiese 			(VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
6804f550df5SKarsten Wiese 			 VIA_REG_STAT_FLAG);
6814f550df5SKarsten Wiese 		c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
6824f550df5SKarsten Wiese 		if (!c_status)
6834f550df5SKarsten Wiese 			continue;
6844f550df5SKarsten Wiese 
6854f550df5SKarsten Wiese 		substream = viadev->substream;
6864f550df5SKarsten Wiese 		if (substream && viadev->running) {
6874f550df5SKarsten Wiese 			/*
6884f550df5SKarsten Wiese 			 * Update hwptr_done based on 'period elapsed'
6894f550df5SKarsten Wiese 			 * interrupts. We'll use it, when the chip returns 0
6904f550df5SKarsten Wiese 			 * for OFFSET_CURR_COUNT.
6914f550df5SKarsten Wiese 			 */
6924f550df5SKarsten Wiese 			if (c_status & VIA_REG_STAT_EOL)
6934f550df5SKarsten Wiese 				viadev->hwptr_done = 0;
6944f550df5SKarsten Wiese 			else
6954f550df5SKarsten Wiese 				viadev->hwptr_done += viadev->fragsize;
6964f550df5SKarsten Wiese 			viadev->in_interrupt = c_status;
6974f550df5SKarsten Wiese 			if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
6984f550df5SKarsten Wiese 				viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
6994f550df5SKarsten Wiese 			spin_unlock(&chip->reg_lock);
7004f550df5SKarsten Wiese 
7014f550df5SKarsten Wiese 			snd_pcm_period_elapsed(substream);
7024f550df5SKarsten Wiese 
7034f550df5SKarsten Wiese 			spin_lock(&chip->reg_lock);
7044f550df5SKarsten Wiese 			viadev->in_interrupt = 0;
7054f550df5SKarsten Wiese 		}
7064f550df5SKarsten Wiese 		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
7074f550df5SKarsten Wiese 		irqreturn = 1;
7084f550df5SKarsten Wiese 	}
7094f550df5SKarsten Wiese 	spin_unlock(&chip->reg_lock);
7104f550df5SKarsten Wiese 	return IRQ_RETVAL(irqreturn);
7114f550df5SKarsten Wiese }
7124f550df5SKarsten Wiese 
7134f550df5SKarsten Wiese /*
7141da177e4SLinus Torvalds  *  PCM callbacks
7151da177e4SLinus Torvalds  */
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /*
7181da177e4SLinus Torvalds  * trigger callback
7191da177e4SLinus Torvalds  */
720e437e3d7STakashi Iwai static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
7211da177e4SLinus Torvalds {
722e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
723e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
7241da177e4SLinus Torvalds 	unsigned char val;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686)
7271da177e4SLinus Torvalds 		val = VIA_REG_CTRL_INT;
7281da177e4SLinus Torvalds 	else
7291da177e4SLinus Torvalds 		val = 0;
7301da177e4SLinus Torvalds 	switch (cmd) {
7311da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
73241e4845cSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_RESUME:
7331da177e4SLinus Torvalds 		val |= VIA_REG_CTRL_START;
7341da177e4SLinus Torvalds 		viadev->running = 1;
7351da177e4SLinus Torvalds 		break;
7361da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
73741e4845cSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_SUSPEND:
7381da177e4SLinus Torvalds 		val = VIA_REG_CTRL_TERMINATE;
7391da177e4SLinus Torvalds 		viadev->running = 0;
7401da177e4SLinus Torvalds 		break;
7411da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
7421da177e4SLinus Torvalds 		val |= VIA_REG_CTRL_PAUSE;
7431da177e4SLinus Torvalds 		viadev->running = 0;
7441da177e4SLinus Torvalds 		break;
7451da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
7461da177e4SLinus Torvalds 		viadev->running = 1;
7471da177e4SLinus Torvalds 		break;
7481da177e4SLinus Torvalds 	default:
7491da177e4SLinus Torvalds 		return -EINVAL;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 	outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
7521da177e4SLinus Torvalds 	if (cmd == SNDRV_PCM_TRIGGER_STOP)
7531da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, viadev);
7541da177e4SLinus Torvalds 	return 0;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds /*
7591da177e4SLinus Torvalds  * pointer callbacks
7601da177e4SLinus Torvalds  */
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds /*
7631da177e4SLinus Torvalds  * calculate the linear position at the given sg-buffer index and the rest count
7641da177e4SLinus Torvalds  */
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds #define check_invalid_pos(viadev,pos) \
767e437e3d7STakashi Iwai 	((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
768e437e3d7STakashi Iwai 				     viadev->lastpos < viadev->bufsize2))
7691da177e4SLinus Torvalds 
770e437e3d7STakashi Iwai static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
771e437e3d7STakashi Iwai 					   unsigned int count)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	unsigned int size, base, res;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	size = viadev->idx_table[idx].size;
7761da177e4SLinus Torvalds 	base = viadev->idx_table[idx].offset;
7771da177e4SLinus Torvalds 	res = base + size - count;
7784f550df5SKarsten Wiese 	if (res >= viadev->bufsize)
7794f550df5SKarsten Wiese 		res -= viadev->bufsize;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/* check the validity of the calculated position */
7821da177e4SLinus Torvalds 	if (size < count) {
783e437e3d7STakashi Iwai 		snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
784e437e3d7STakashi Iwai 			   (int)size, (int)count);
7851da177e4SLinus Torvalds 		res = viadev->lastpos;
7861da177e4SLinus Torvalds 	} else {
7871da177e4SLinus Torvalds 		if (! count) {
7881da177e4SLinus Torvalds 			/* Some mobos report count = 0 on the DMA boundary,
7891da177e4SLinus Torvalds 			 * i.e. count = size indeed.
7901da177e4SLinus Torvalds 			 * Let's check whether this step is above the expected size.
7911da177e4SLinus Torvalds 			 */
7921da177e4SLinus Torvalds 			int delta = res - viadev->lastpos;
7931da177e4SLinus Torvalds 			if (delta < 0)
7941da177e4SLinus Torvalds 				delta += viadev->bufsize;
7951da177e4SLinus Torvalds 			if ((unsigned int)delta > viadev->fragsize)
7961da177e4SLinus Torvalds 				res = base;
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 		if (check_invalid_pos(viadev, res)) {
7991da177e4SLinus Torvalds #ifdef POINTER_DEBUG
800e437e3d7STakashi Iwai 			printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
801e437e3d7STakashi Iwai 			       "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
802e437e3d7STakashi Iwai 			       "count = 0x%x\n", idx, viadev->tbl_entries,
803e437e3d7STakashi Iwai 			       viadev->lastpos, viadev->bufsize2,
804e437e3d7STakashi Iwai 			       viadev->idx_table[idx].offset,
805e437e3d7STakashi Iwai 			       viadev->idx_table[idx].size, count);
8061da177e4SLinus Torvalds #endif
8071da177e4SLinus Torvalds 			/* count register returns full size when end of buffer is reached */
8081da177e4SLinus Torvalds 			res = base + size;
8091da177e4SLinus Torvalds 			if (check_invalid_pos(viadev, res)) {
810e437e3d7STakashi Iwai 				snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
811e437e3d7STakashi Iwai 					   "using last valid pointer\n");
8121da177e4SLinus Torvalds 				res = viadev->lastpos;
8131da177e4SLinus Torvalds 			}
8141da177e4SLinus Torvalds 		}
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 	return res;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds /*
8201da177e4SLinus Torvalds  * get the current pointer on via686
8211da177e4SLinus Torvalds  */
822e437e3d7STakashi Iwai static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
8231da177e4SLinus Torvalds {
824e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
825e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
8261da177e4SLinus Torvalds 	unsigned int idx, ptr, count, res;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	snd_assert(viadev->tbl_entries, return 0);
8291da177e4SLinus Torvalds 	if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
8301da177e4SLinus Torvalds 		return 0;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
8331da177e4SLinus Torvalds 	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
8341da177e4SLinus Torvalds 	/* The via686a does not have the current index register,
8351da177e4SLinus Torvalds 	 * so we need to calculate the index from CURR_PTR.
8361da177e4SLinus Torvalds 	 */
8371da177e4SLinus Torvalds 	ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
8381da177e4SLinus Torvalds 	if (ptr <= (unsigned int)viadev->table.addr)
8391da177e4SLinus Torvalds 		idx = 0;
8401da177e4SLinus Torvalds 	else /* CURR_PTR holds the address + 8 */
8411da177e4SLinus Torvalds 		idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
8421da177e4SLinus Torvalds 	res = calc_linear_pos(viadev, idx, count);
8434f550df5SKarsten Wiese 	viadev->lastpos = res; /* remember the last position */
8441da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, res);
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /*
8501da177e4SLinus Torvalds  * get the current pointer on via823x
8511da177e4SLinus Torvalds  */
852e437e3d7STakashi Iwai static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
8531da177e4SLinus Torvalds {
854e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
855e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
8561da177e4SLinus Torvalds 	unsigned int idx, count, res;
8574f550df5SKarsten Wiese 	int status;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	snd_assert(viadev->tbl_entries, return 0);
8604f550df5SKarsten Wiese 
8611da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
8621da177e4SLinus Torvalds 	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
8634f550df5SKarsten Wiese 	status = viadev->in_interrupt;
8644f550df5SKarsten Wiese 	if (!status)
8654f550df5SKarsten Wiese 		status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
8664f550df5SKarsten Wiese 
867c6cc0e3bSBastiaan Jacques 	/* An apparent bug in the 8251 is worked around by sending a
868c6cc0e3bSBastiaan Jacques 	 * REG_CTRL_START. */
869c6cc0e3bSBastiaan Jacques 	if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
870c6cc0e3bSBastiaan Jacques 		snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
871c6cc0e3bSBastiaan Jacques 
8724f550df5SKarsten Wiese 	if (!(status & VIA_REG_STAT_ACTIVE)) {
8734f550df5SKarsten Wiese 		res = 0;
8744f550df5SKarsten Wiese 		goto unlock;
8754f550df5SKarsten Wiese 	}
8764f550df5SKarsten Wiese 	if (count & 0xffffff) {
8771da177e4SLinus Torvalds 		idx = count >> 24;
8781da177e4SLinus Torvalds 		if (idx >= viadev->tbl_entries) {
8791da177e4SLinus Torvalds #ifdef POINTER_DEBUG
880e437e3d7STakashi Iwai 			printk(KERN_DEBUG "fail: invalid idx = %i/%i\n", idx,
881e437e3d7STakashi Iwai 			       viadev->tbl_entries);
8821da177e4SLinus Torvalds #endif
8831da177e4SLinus Torvalds 			res = viadev->lastpos;
8841da177e4SLinus Torvalds 		} else {
8851da177e4SLinus Torvalds 			count &= 0xffffff;
8861da177e4SLinus Torvalds 			res = calc_linear_pos(viadev, idx, count);
8871da177e4SLinus Torvalds 		}
8884f550df5SKarsten Wiese 	} else {
8894f550df5SKarsten Wiese 		res = viadev->hwptr_done;
8904f550df5SKarsten Wiese 		if (!viadev->in_interrupt) {
8914f550df5SKarsten Wiese 			if (status & VIA_REG_STAT_EOL) {
8924f550df5SKarsten Wiese 				res = 0;
8934f550df5SKarsten Wiese 			} else
8944f550df5SKarsten Wiese 				if (status & VIA_REG_STAT_FLAG) {
8954f550df5SKarsten Wiese 					res += viadev->fragsize;
8964f550df5SKarsten Wiese 				}
8974f550df5SKarsten Wiese 		}
8984f550df5SKarsten Wiese 	}
8994f550df5SKarsten Wiese unlock:
9004f550df5SKarsten Wiese 	viadev->lastpos = res;
9011da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, res);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds /*
9081da177e4SLinus Torvalds  * hw_params callback:
9091da177e4SLinus Torvalds  * allocate the buffer and build up the buffer description table
9101da177e4SLinus Torvalds  */
911e437e3d7STakashi Iwai static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
912e437e3d7STakashi Iwai 				 struct snd_pcm_hw_params *hw_params)
9131da177e4SLinus Torvalds {
914e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
915e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
9161da177e4SLinus Torvalds 	int err;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
9191da177e4SLinus Torvalds 	if (err < 0)
9201da177e4SLinus Torvalds 		return err;
9211da177e4SLinus Torvalds 	err = build_via_table(viadev, substream, chip->pci,
9221da177e4SLinus Torvalds 			      params_periods(hw_params),
9231da177e4SLinus Torvalds 			      params_period_bytes(hw_params));
9241da177e4SLinus Torvalds 	if (err < 0)
9251da177e4SLinus Torvalds 		return err;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	return 0;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds /*
9311da177e4SLinus Torvalds  * hw_free callback:
9321da177e4SLinus Torvalds  * clean up the buffer description table and release the buffer
9331da177e4SLinus Torvalds  */
934e437e3d7STakashi Iwai static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
9351da177e4SLinus Torvalds {
936e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
937e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	clean_via_table(viadev, substream, chip->pci);
9401da177e4SLinus Torvalds 	snd_pcm_lib_free_pages(substream);
9411da177e4SLinus Torvalds 	return 0;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds /*
9461da177e4SLinus Torvalds  * set up the table pointer
9471da177e4SLinus Torvalds  */
948e437e3d7STakashi Iwai static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
9511da177e4SLinus Torvalds 	outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
9521da177e4SLinus Torvalds 	udelay(20);
9531da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds /*
9571da177e4SLinus Torvalds  * prepare callback for playback and capture on via686
9581da177e4SLinus Torvalds  */
959e437e3d7STakashi Iwai static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
960e437e3d7STakashi Iwai 				struct snd_pcm_runtime *runtime)
9611da177e4SLinus Torvalds {
9621da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
9631da177e4SLinus Torvalds 	/* this must be set after channel_reset */
9641da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
9651da177e4SLinus Torvalds 	outb(VIA_REG_TYPE_AUTOSTART |
9661da177e4SLinus Torvalds 	     (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
9671da177e4SLinus Torvalds 	     (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
9681da177e4SLinus Torvalds 	     ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
9691da177e4SLinus Torvalds 	     VIA_REG_TYPE_INT_EOL |
9701da177e4SLinus Torvalds 	     VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
973e437e3d7STakashi Iwai static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
9741da177e4SLinus Torvalds {
975e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
976e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
977e437e3d7STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
9801da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
9811da177e4SLinus Torvalds 	via686_setup_format(chip, viadev, runtime);
9821da177e4SLinus Torvalds 	return 0;
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
985e437e3d7STakashi Iwai static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
9861da177e4SLinus Torvalds {
987e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
988e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
989e437e3d7STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
9921da177e4SLinus Torvalds 	via686_setup_format(chip, viadev, runtime);
9931da177e4SLinus Torvalds 	return 0;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds /*
9971da177e4SLinus Torvalds  * lock the current rate
9981da177e4SLinus Torvalds  */
9991da177e4SLinus Torvalds static int via_lock_rate(struct via_rate_lock *rec, int rate)
10001da177e4SLinus Torvalds {
10011da177e4SLinus Torvalds 	int changed = 0;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	spin_lock_irq(&rec->lock);
10041da177e4SLinus Torvalds 	if (rec->rate != rate) {
10051da177e4SLinus Torvalds 		if (rec->rate && rec->used > 1) /* already set */
10061da177e4SLinus Torvalds 			changed = -EINVAL;
10071da177e4SLinus Torvalds 		else {
10081da177e4SLinus Torvalds 			rec->rate = rate;
10091da177e4SLinus Torvalds 			changed = 1;
10101da177e4SLinus Torvalds 		}
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 	spin_unlock_irq(&rec->lock);
10131da177e4SLinus Torvalds 	return changed;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds /*
10171da177e4SLinus Torvalds  * prepare callback for DSX playback on via823x
10181da177e4SLinus Torvalds  */
1019e437e3d7STakashi Iwai static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
10201da177e4SLinus Torvalds {
1021e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1022e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
1023e437e3d7STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10242d7eb7cbSSergey Vlasov 	int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
10251da177e4SLinus Torvalds 	int rate_changed;
10261da177e4SLinus Torvalds 	u32 rbits;
10271da177e4SLinus Torvalds 
10282d7eb7cbSSergey Vlasov 	if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
10291da177e4SLinus Torvalds 		return rate_changed;
103016d3f140STakashi Iwai 	if (rate_changed)
10311da177e4SLinus Torvalds 		snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
10321da177e4SLinus Torvalds 				  chip->no_vra ? 48000 : runtime->rate);
103316d3f140STakashi Iwai 	if (chip->spdif_on && viadev->reg_offset == 0x30)
103416d3f140STakashi Iwai 		snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
103516d3f140STakashi Iwai 
10361da177e4SLinus Torvalds 	if (runtime->rate == 48000)
10371da177e4SLinus Torvalds 		rbits = 0xfffff;
10381da177e4SLinus Torvalds 	else
1039e437e3d7STakashi Iwai 		rbits = (0x100000 / 48000) * runtime->rate +
1040e437e3d7STakashi Iwai 			((0x100000 % 48000) * runtime->rate) / 48000;
10411da177e4SLinus Torvalds 	snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
10421da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
10431da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
1044e437e3d7STakashi Iwai 	outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1045e437e3d7STakashi Iwai 	     VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1046e437e3d7STakashi Iwai 	outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1047e437e3d7STakashi Iwai 	     VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
10481da177e4SLinus Torvalds 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
10491da177e4SLinus Torvalds 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
10501da177e4SLinus Torvalds 	     rbits | /* rate */
10511da177e4SLinus Torvalds 	     0xff000000,    /* STOP index is never reached */
10521da177e4SLinus Torvalds 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
10531da177e4SLinus Torvalds 	udelay(20);
10541da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
10551da177e4SLinus Torvalds 	return 0;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /*
10591da177e4SLinus Torvalds  * prepare callback for multi-channel playback on via823x
10601da177e4SLinus Torvalds  */
1061e437e3d7STakashi Iwai static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
10621da177e4SLinus Torvalds {
1063e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1064e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
1065e437e3d7STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10661da177e4SLinus Torvalds 	unsigned int slots;
10671da177e4SLinus Torvalds 	int fmt;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
10701da177e4SLinus Torvalds 		return -EINVAL;
10711da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
10721da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
10731da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
10741da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
10751da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
10761da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
10771da177e4SLinus Torvalds 
1078e437e3d7STakashi Iwai 	fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1079e437e3d7STakashi Iwai 		VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
10801da177e4SLinus Torvalds 	fmt |= runtime->channels << 4;
10811da177e4SLinus Torvalds 	outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
10821da177e4SLinus Torvalds #if 0
10831da177e4SLinus Torvalds 	if (chip->revision == VIA_REV_8233A)
10841da177e4SLinus Torvalds 		slots = 0;
10851da177e4SLinus Torvalds 	else
10861da177e4SLinus Torvalds #endif
10871da177e4SLinus Torvalds 	{
10881da177e4SLinus Torvalds 		/* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
10891da177e4SLinus Torvalds 		/* corresponding to FL, FR, RL, RR, C, LFE ?? */
10901da177e4SLinus Torvalds 		switch (runtime->channels) {
10911da177e4SLinus Torvalds 		case 1: slots = (1<<0) | (1<<4); break;
10921da177e4SLinus Torvalds 		case 2: slots = (1<<0) | (2<<4); break;
10931da177e4SLinus Torvalds 		case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
10941da177e4SLinus Torvalds 		case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
10951da177e4SLinus Torvalds 		case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
10961da177e4SLinus Torvalds 		case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
10971da177e4SLinus Torvalds 		default: slots = 0; break;
10981da177e4SLinus Torvalds 		}
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 	/* STOP index is never reached */
11011da177e4SLinus Torvalds 	outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
11021da177e4SLinus Torvalds 	udelay(20);
11031da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
11041da177e4SLinus Torvalds 	return 0;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds /*
11081da177e4SLinus Torvalds  * prepare callback for capture on via823x
11091da177e4SLinus Torvalds  */
1110e437e3d7STakashi Iwai static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
11111da177e4SLinus Torvalds {
1112e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1113e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
1114e437e3d7STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
11171da177e4SLinus Torvalds 		return -EINVAL;
11181da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
11191da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
11201da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
11211da177e4SLinus Torvalds 	outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
11221da177e4SLinus Torvalds 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
11231da177e4SLinus Torvalds 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
11241da177e4SLinus Torvalds 	     0xff000000,    /* STOP index is never reached */
11251da177e4SLinus Torvalds 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
11261da177e4SLinus Torvalds 	udelay(20);
11271da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
11281da177e4SLinus Torvalds 	return 0;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds /*
11331da177e4SLinus Torvalds  * pcm hardware definition, identical for both playback and capture
11341da177e4SLinus Torvalds  */
1135e437e3d7STakashi Iwai static struct snd_pcm_hardware snd_via82xx_hw =
11361da177e4SLinus Torvalds {
11371da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
11381da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
11391da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
114041e4845cSJaroslav Kysela 				 /* SNDRV_PCM_INFO_RESUME | */
11411da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE),
11421da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
11431da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_48000,
11441da177e4SLinus Torvalds 	.rate_min =		48000,
11451da177e4SLinus Torvalds 	.rate_max =		48000,
11461da177e4SLinus Torvalds 	.channels_min =		1,
11471da177e4SLinus Torvalds 	.channels_max =		2,
11481da177e4SLinus Torvalds 	.buffer_bytes_max =	128 * 1024,
11491da177e4SLinus Torvalds 	.period_bytes_min =	32,
11501da177e4SLinus Torvalds 	.period_bytes_max =	128 * 1024,
11511da177e4SLinus Torvalds 	.periods_min =		2,
11521da177e4SLinus Torvalds 	.periods_max =		VIA_TABLE_SIZE / 2,
11531da177e4SLinus Torvalds 	.fifo_size =		0,
11541da177e4SLinus Torvalds };
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds /*
11581da177e4SLinus Torvalds  * open callback skeleton
11591da177e4SLinus Torvalds  */
1160e437e3d7STakashi Iwai static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1161e437e3d7STakashi Iwai 				struct snd_pcm_substream *substream)
11621da177e4SLinus Torvalds {
1163e437e3d7STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
11641da177e4SLinus Torvalds 	int err;
11651da177e4SLinus Torvalds 	struct via_rate_lock *ratep;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	runtime->hw = snd_via82xx_hw;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	/* set the hw rate condition */
11701da177e4SLinus Torvalds 	ratep = &chip->rates[viadev->direction];
11711da177e4SLinus Torvalds 	spin_lock_irq(&ratep->lock);
11721da177e4SLinus Torvalds 	ratep->used++;
11731da177e4SLinus Torvalds 	if (chip->spdif_on && viadev->reg_offset == 0x30) {
11741da177e4SLinus Torvalds 		/* DXS#3 and spdif is on */
11751da177e4SLinus Torvalds 		runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
11761da177e4SLinus Torvalds 		snd_pcm_limit_hw_rates(runtime);
11771da177e4SLinus Torvalds 	} else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
11781da177e4SLinus Torvalds 		/* fixed DXS playback rate */
11791da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_48000;
11801da177e4SLinus Torvalds 		runtime->hw.rate_min = runtime->hw.rate_max = 48000;
11812d7eb7cbSSergey Vlasov 	} else if (chip->dxs_src && viadev->reg_offset < 0x40) {
11822d7eb7cbSSergey Vlasov 		/* use full SRC capabilities of DXS */
11832d7eb7cbSSergey Vlasov 		runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
11842d7eb7cbSSergey Vlasov 				     SNDRV_PCM_RATE_8000_48000);
11852d7eb7cbSSergey Vlasov 		runtime->hw.rate_min = 8000;
11862d7eb7cbSSergey Vlasov 		runtime->hw.rate_max = 48000;
11871da177e4SLinus Torvalds 	} else if (! ratep->rate) {
11881da177e4SLinus Torvalds 		int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
11891da177e4SLinus Torvalds 		runtime->hw.rates = chip->ac97->rates[idx];
11901da177e4SLinus Torvalds 		snd_pcm_limit_hw_rates(runtime);
11911da177e4SLinus Torvalds 	} else {
11921da177e4SLinus Torvalds 		/* a fixed rate */
11931da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
11941da177e4SLinus Torvalds 		runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
11951da177e4SLinus Torvalds 	}
11961da177e4SLinus Torvalds 	spin_unlock_irq(&ratep->lock);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	/* we may remove following constaint when we modify table entries
11991da177e4SLinus Torvalds 	   in interrupt */
12001da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
12011da177e4SLinus Torvalds 		return err;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	runtime->private_data = viadev;
12041da177e4SLinus Torvalds 	viadev->substream = substream;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	return 0;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds /*
12111da177e4SLinus Torvalds  * open callback for playback on via686 and via823x DSX
12121da177e4SLinus Torvalds  */
1213e437e3d7STakashi Iwai static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
12141da177e4SLinus Torvalds {
1215e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1216e437e3d7STakashi Iwai 	struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
12171da177e4SLinus Torvalds 	int err;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
12201da177e4SLinus Torvalds 		return err;
12211da177e4SLinus Torvalds 	return 0;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds /*
12251da177e4SLinus Torvalds  * open callback for playback on via823x multi-channel
12261da177e4SLinus Torvalds  */
1227e437e3d7STakashi Iwai static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
12281da177e4SLinus Torvalds {
1229e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1230e437e3d7STakashi Iwai 	struct viadev *viadev = &chip->devs[chip->multi_devno];
12311da177e4SLinus Torvalds 	int err;
12321da177e4SLinus Torvalds 	/* channels constraint for VIA8233A
12331da177e4SLinus Torvalds 	 * 3 and 5 channels are not supported
12341da177e4SLinus Torvalds 	 */
12351da177e4SLinus Torvalds 	static unsigned int channels[] = {
12361da177e4SLinus Torvalds 		1, 2, 4, 6
12371da177e4SLinus Torvalds 	};
1238e437e3d7STakashi Iwai 	static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
12391da177e4SLinus Torvalds 		.count = ARRAY_SIZE(channels),
12401da177e4SLinus Torvalds 		.list = channels,
12411da177e4SLinus Torvalds 		.mask = 0,
12421da177e4SLinus Torvalds 	};
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
12451da177e4SLinus Torvalds 		return err;
12461da177e4SLinus Torvalds 	substream->runtime->hw.channels_max = 6;
12471da177e4SLinus Torvalds 	if (chip->revision == VIA_REV_8233A)
1248e437e3d7STakashi Iwai 		snd_pcm_hw_constraint_list(substream->runtime, 0,
1249e437e3d7STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
1250e437e3d7STakashi Iwai 					   &hw_constraints_channels);
12511da177e4SLinus Torvalds 	return 0;
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds /*
12551da177e4SLinus Torvalds  * open callback for capture on via686 and via823x
12561da177e4SLinus Torvalds  */
1257e437e3d7STakashi Iwai static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
12581da177e4SLinus Torvalds {
1259e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1260e437e3d7STakashi Iwai 	struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	return snd_via82xx_pcm_open(chip, viadev, substream);
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds /*
12661da177e4SLinus Torvalds  * close callback
12671da177e4SLinus Torvalds  */
1268e437e3d7STakashi Iwai static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
12691da177e4SLinus Torvalds {
1270e437e3d7STakashi Iwai 	struct via82xx *chip = snd_pcm_substream_chip(substream);
1271e437e3d7STakashi Iwai 	struct viadev *viadev = substream->runtime->private_data;
12721da177e4SLinus Torvalds 	struct via_rate_lock *ratep;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	/* release the rate lock */
12751da177e4SLinus Torvalds 	ratep = &chip->rates[viadev->direction];
12761da177e4SLinus Torvalds 	spin_lock_irq(&ratep->lock);
12771da177e4SLinus Torvalds 	ratep->used--;
12781da177e4SLinus Torvalds 	if (! ratep->used)
12791da177e4SLinus Torvalds 		ratep->rate = 0;
12801da177e4SLinus Torvalds 	spin_unlock_irq(&ratep->lock);
12816dbe6628STakashi Iwai 	if (! ratep->rate) {
12826dbe6628STakashi Iwai 		if (! viadev->direction) {
12836dbe6628STakashi Iwai 			snd_ac97_update_power(chip->ac97,
12846dbe6628STakashi Iwai 					      AC97_PCM_FRONT_DAC_RATE, 0);
12856dbe6628STakashi Iwai 			snd_ac97_update_power(chip->ac97,
12866dbe6628STakashi Iwai 					      AC97_PCM_SURR_DAC_RATE, 0);
12876dbe6628STakashi Iwai 			snd_ac97_update_power(chip->ac97,
12886dbe6628STakashi Iwai 					      AC97_PCM_LFE_DAC_RATE, 0);
12896dbe6628STakashi Iwai 		} else
12906dbe6628STakashi Iwai 			snd_ac97_update_power(chip->ac97,
12916dbe6628STakashi Iwai 					      AC97_PCM_LR_ADC_RATE, 0);
12926dbe6628STakashi Iwai 	}
12931da177e4SLinus Torvalds 	viadev->substream = NULL;
12941da177e4SLinus Torvalds 	return 0;
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /* via686 playback callbacks */
1299e437e3d7STakashi Iwai static struct snd_pcm_ops snd_via686_playback_ops = {
13001da177e4SLinus Torvalds 	.open =		snd_via82xx_playback_open,
13011da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
13021da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13031da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
13041da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
13051da177e4SLinus Torvalds 	.prepare =	snd_via686_playback_prepare,
13061da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
13071da177e4SLinus Torvalds 	.pointer =	snd_via686_pcm_pointer,
13081da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
13091da177e4SLinus Torvalds };
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds /* via686 capture callbacks */
1312e437e3d7STakashi Iwai static struct snd_pcm_ops snd_via686_capture_ops = {
13131da177e4SLinus Torvalds 	.open =		snd_via82xx_capture_open,
13141da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
13151da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13161da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
13171da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
13181da177e4SLinus Torvalds 	.prepare =	snd_via686_capture_prepare,
13191da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
13201da177e4SLinus Torvalds 	.pointer =	snd_via686_pcm_pointer,
13211da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
13221da177e4SLinus Torvalds };
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds /* via823x DSX playback callbacks */
1325e437e3d7STakashi Iwai static struct snd_pcm_ops snd_via8233_playback_ops = {
13261da177e4SLinus Torvalds 	.open =		snd_via82xx_playback_open,
13271da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
13281da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13291da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
13301da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
13311da177e4SLinus Torvalds 	.prepare =	snd_via8233_playback_prepare,
13321da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
13331da177e4SLinus Torvalds 	.pointer =	snd_via8233_pcm_pointer,
13341da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
13351da177e4SLinus Torvalds };
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds /* via823x multi-channel playback callbacks */
1338e437e3d7STakashi Iwai static struct snd_pcm_ops snd_via8233_multi_ops = {
13391da177e4SLinus Torvalds 	.open =		snd_via8233_multi_open,
13401da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
13411da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13421da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
13431da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
13441da177e4SLinus Torvalds 	.prepare =	snd_via8233_multi_prepare,
13451da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
13461da177e4SLinus Torvalds 	.pointer =	snd_via8233_pcm_pointer,
13471da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
13481da177e4SLinus Torvalds };
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds /* via823x capture callbacks */
1351e437e3d7STakashi Iwai static struct snd_pcm_ops snd_via8233_capture_ops = {
13521da177e4SLinus Torvalds 	.open =		snd_via82xx_capture_open,
13531da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
13541da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13551da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
13561da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
13571da177e4SLinus Torvalds 	.prepare =	snd_via8233_capture_prepare,
13581da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
13591da177e4SLinus Torvalds 	.pointer =	snd_via8233_pcm_pointer,
13601da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
13611da177e4SLinus Torvalds };
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 
1364e437e3d7STakashi Iwai static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1365e437e3d7STakashi Iwai 			int shadow_pos, int direction)
13661da177e4SLinus Torvalds {
13671da177e4SLinus Torvalds 	chip->devs[idx].reg_offset = reg_offset;
13684f550df5SKarsten Wiese 	chip->devs[idx].shadow_shift = shadow_pos * 4;
13691da177e4SLinus Torvalds 	chip->devs[idx].direction = direction;
13701da177e4SLinus Torvalds 	chip->devs[idx].port = chip->port + reg_offset;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds /*
13741da177e4SLinus Torvalds  * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
13751da177e4SLinus Torvalds  */
1376e437e3d7STakashi Iwai static int __devinit snd_via8233_pcm_new(struct via82xx *chip)
13771da177e4SLinus Torvalds {
1378e437e3d7STakashi Iwai 	struct snd_pcm *pcm;
13791da177e4SLinus Torvalds 	int i, err;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	chip->playback_devno = 0;	/* x 4 */
13821da177e4SLinus Torvalds 	chip->multi_devno = 4;		/* x 1 */
13831da177e4SLinus Torvalds 	chip->capture_devno = 5;	/* x 2 */
13841da177e4SLinus Torvalds 	chip->num_devs = 7;
13851da177e4SLinus Torvalds 	chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	/* PCM #0:  4 DSX playbacks and 1 capture */
13881da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
13891da177e4SLinus Torvalds 	if (err < 0)
13901da177e4SLinus Torvalds 		return err;
13911da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
13921da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
13931da177e4SLinus Torvalds 	pcm->private_data = chip;
13941da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
13951da177e4SLinus Torvalds 	chip->pcms[0] = pcm;
13961da177e4SLinus Torvalds 	/* set up playbacks */
13971da177e4SLinus Torvalds 	for (i = 0; i < 4; i++)
13984f550df5SKarsten Wiese 		init_viadev(chip, i, 0x10 * i, i, 0);
13991da177e4SLinus Torvalds 	/* capture */
14004f550df5SKarsten Wiese 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1403e437e3d7STakashi Iwai 							 snd_dma_pci_data(chip->pci),
1404e437e3d7STakashi Iwai 							 64*1024, 128*1024)) < 0)
14051da177e4SLinus Torvalds 		return err;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	/* PCM #1:  multi-channel playback and 2nd capture */
14081da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
14091da177e4SLinus Torvalds 	if (err < 0)
14101da177e4SLinus Torvalds 		return err;
14111da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
14121da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
14131da177e4SLinus Torvalds 	pcm->private_data = chip;
14141da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
14151da177e4SLinus Torvalds 	chip->pcms[1] = pcm;
14161da177e4SLinus Torvalds 	/* set up playback */
14174f550df5SKarsten Wiese 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
14181da177e4SLinus Torvalds 	/* set up capture */
14194f550df5SKarsten Wiese 	init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1422e437e3d7STakashi Iwai 						         snd_dma_pci_data(chip->pci),
1423e437e3d7STakashi Iwai 							 64*1024, 128*1024)) < 0)
14241da177e4SLinus Torvalds 		return err;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	return 0;
14271da177e4SLinus Torvalds }
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds /*
14301da177e4SLinus Torvalds  * create pcm instances for VIA8233A
14311da177e4SLinus Torvalds  */
1432e437e3d7STakashi Iwai static int __devinit snd_via8233a_pcm_new(struct via82xx *chip)
14331da177e4SLinus Torvalds {
1434e437e3d7STakashi Iwai 	struct snd_pcm *pcm;
14351da177e4SLinus Torvalds 	int err;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	chip->multi_devno = 0;
14381da177e4SLinus Torvalds 	chip->playback_devno = 1;
14391da177e4SLinus Torvalds 	chip->capture_devno = 2;
14401da177e4SLinus Torvalds 	chip->num_devs = 3;
14411da177e4SLinus Torvalds 	chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	/* PCM #0:  multi-channel playback and capture */
14441da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
14451da177e4SLinus Torvalds 	if (err < 0)
14461da177e4SLinus Torvalds 		return err;
14471da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
14481da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
14491da177e4SLinus Torvalds 	pcm->private_data = chip;
14501da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
14511da177e4SLinus Torvalds 	chip->pcms[0] = pcm;
14521da177e4SLinus Torvalds 	/* set up playback */
14534f550df5SKarsten Wiese 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
14541da177e4SLinus Torvalds 	/* capture */
14554f550df5SKarsten Wiese 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1458e437e3d7STakashi Iwai 							 snd_dma_pci_data(chip->pci),
1459e437e3d7STakashi Iwai 							 64*1024, 128*1024)) < 0)
14601da177e4SLinus Torvalds 		return err;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	/* SPDIF supported? */
14631da177e4SLinus Torvalds 	if (! ac97_can_spdif(chip->ac97))
14641da177e4SLinus Torvalds 		return 0;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	/* PCM #1:  DXS3 playback (for spdif) */
14671da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
14681da177e4SLinus Torvalds 	if (err < 0)
14691da177e4SLinus Torvalds 		return err;
14701da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
14711da177e4SLinus Torvalds 	pcm->private_data = chip;
14721da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
14731da177e4SLinus Torvalds 	chip->pcms[1] = pcm;
14741da177e4SLinus Torvalds 	/* set up playback */
14754f550df5SKarsten Wiese 	init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1478e437e3d7STakashi Iwai 							 snd_dma_pci_data(chip->pci),
1479e437e3d7STakashi Iwai 							 64*1024, 128*1024)) < 0)
14801da177e4SLinus Torvalds 		return err;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	return 0;
14831da177e4SLinus Torvalds }
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds /*
14861da177e4SLinus Torvalds  * create a pcm instance for via686a/b
14871da177e4SLinus Torvalds  */
1488e437e3d7STakashi Iwai static int __devinit snd_via686_pcm_new(struct via82xx *chip)
14891da177e4SLinus Torvalds {
1490e437e3d7STakashi Iwai 	struct snd_pcm *pcm;
14911da177e4SLinus Torvalds 	int err;
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	chip->playback_devno = 0;
14941da177e4SLinus Torvalds 	chip->capture_devno = 1;
14951da177e4SLinus Torvalds 	chip->num_devs = 2;
14961da177e4SLinus Torvalds 	chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
14991da177e4SLinus Torvalds 	if (err < 0)
15001da177e4SLinus Torvalds 		return err;
15011da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
15021da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
15031da177e4SLinus Torvalds 	pcm->private_data = chip;
15041da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
15051da177e4SLinus Torvalds 	chip->pcms[0] = pcm;
15064f550df5SKarsten Wiese 	init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
15074f550df5SKarsten Wiese 	init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1510e437e3d7STakashi Iwai 							 snd_dma_pci_data(chip->pci),
1511e437e3d7STakashi Iwai 							 64*1024, 128*1024)) < 0)
15121da177e4SLinus Torvalds 		return err;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	return 0;
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds /*
15191da177e4SLinus Torvalds  *  Mixer part
15201da177e4SLinus Torvalds  */
15211da177e4SLinus Torvalds 
1522e437e3d7STakashi Iwai static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1523e437e3d7STakashi Iwai 					   struct snd_ctl_elem_info *uinfo)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	/* formerly they were "Line" and "Mic", but it looks like that they
15261da177e4SLinus Torvalds 	 * have nothing to do with the actual physical connections...
15271da177e4SLinus Torvalds 	 */
15281da177e4SLinus Torvalds 	static char *texts[2] = {
15291da177e4SLinus Torvalds 		"Input1", "Input2"
15301da177e4SLinus Torvalds 	};
15311da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
15321da177e4SLinus Torvalds 	uinfo->count = 1;
15331da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 2;
15341da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= 2)
15351da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 1;
15361da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
15371da177e4SLinus Torvalds 	return 0;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
1540e437e3d7STakashi Iwai static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1541e437e3d7STakashi Iwai 					  struct snd_ctl_elem_value *ucontrol)
15421da177e4SLinus Torvalds {
1543e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
15441da177e4SLinus Torvalds 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
15451da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
15461da177e4SLinus Torvalds 	return 0;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
1549e437e3d7STakashi Iwai static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1550e437e3d7STakashi Iwai 					  struct snd_ctl_elem_value *ucontrol)
15511da177e4SLinus Torvalds {
1552e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
15531da177e4SLinus Torvalds 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
15541da177e4SLinus Torvalds 	u8 val, oval;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15571da177e4SLinus Torvalds 	oval = inb(port);
15581da177e4SLinus Torvalds 	val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
15591da177e4SLinus Torvalds 	if (ucontrol->value.enumerated.item[0])
15601da177e4SLinus Torvalds 		val |= VIA_REG_CAPTURE_CHANNEL_MIC;
15611da177e4SLinus Torvalds 	if (val != oval)
15621da177e4SLinus Torvalds 		outb(val, port);
15631da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15641da177e4SLinus Torvalds 	return val != oval;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
1567e437e3d7STakashi Iwai static struct snd_kcontrol_new snd_via8233_capture_source __devinitdata = {
15681da177e4SLinus Torvalds 	.name = "Input Source Select",
15691da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
15701da177e4SLinus Torvalds 	.info = snd_via8233_capture_source_info,
15711da177e4SLinus Torvalds 	.get = snd_via8233_capture_source_get,
15721da177e4SLinus Torvalds 	.put = snd_via8233_capture_source_put,
15731da177e4SLinus Torvalds };
15741da177e4SLinus Torvalds 
1575e437e3d7STakashi Iwai static int snd_via8233_dxs3_spdif_info(struct snd_kcontrol *kcontrol,
1576e437e3d7STakashi Iwai 				       struct snd_ctl_elem_info *uinfo)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
15791da177e4SLinus Torvalds 	uinfo->count = 1;
15801da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
15811da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
15821da177e4SLinus Torvalds 	return 0;
15831da177e4SLinus Torvalds }
15841da177e4SLinus Torvalds 
1585e437e3d7STakashi Iwai static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1586e437e3d7STakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
15871da177e4SLinus Torvalds {
1588e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
15891da177e4SLinus Torvalds 	u8 val;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
15921da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
15931da177e4SLinus Torvalds 	return 0;
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
1596e437e3d7STakashi Iwai static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1597e437e3d7STakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
15981da177e4SLinus Torvalds {
1599e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
16001da177e4SLinus Torvalds 	u8 val, oval;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
16031da177e4SLinus Torvalds 	val = oval & ~VIA8233_SPDIF_DX3;
16041da177e4SLinus Torvalds 	if (ucontrol->value.integer.value[0])
16051da177e4SLinus Torvalds 		val |= VIA8233_SPDIF_DX3;
16061da177e4SLinus Torvalds 	/* save the spdif flag for rate filtering */
16071da177e4SLinus Torvalds 	chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
16081da177e4SLinus Torvalds 	if (val != oval) {
16091da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
16101da177e4SLinus Torvalds 		return 1;
16111da177e4SLinus Torvalds 	}
16121da177e4SLinus Torvalds 	return 0;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
1615e437e3d7STakashi Iwai static struct snd_kcontrol_new snd_via8233_dxs3_spdif_control __devinitdata = {
161610e8d78aSClemens Ladisch 	.name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
16171da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
16181da177e4SLinus Torvalds 	.info = snd_via8233_dxs3_spdif_info,
16191da177e4SLinus Torvalds 	.get = snd_via8233_dxs3_spdif_get,
16201da177e4SLinus Torvalds 	.put = snd_via8233_dxs3_spdif_put,
16211da177e4SLinus Torvalds };
16221da177e4SLinus Torvalds 
1623e437e3d7STakashi Iwai static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1624e437e3d7STakashi Iwai 				       struct snd_ctl_elem_info *uinfo)
16251da177e4SLinus Torvalds {
16261da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
16271da177e4SLinus Torvalds 	uinfo->count = 2;
16281da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
16291da177e4SLinus Torvalds 	uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
16301da177e4SLinus Torvalds 	return 0;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
1633e437e3d7STakashi Iwai static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1634e437e3d7STakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
16351da177e4SLinus Torvalds {
1636e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
163700f226d4SHonza Maly 	unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
163800f226d4SHonza Maly 
163900f226d4SHonza Maly 	ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
164000f226d4SHonza Maly 	ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
164100f226d4SHonza Maly 	return 0;
164200f226d4SHonza Maly }
164300f226d4SHonza Maly 
1644e437e3d7STakashi Iwai static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1645e437e3d7STakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
164600f226d4SHonza Maly {
1647e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
164800f226d4SHonza Maly 	ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
164900f226d4SHonza Maly 	ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
16501da177e4SLinus Torvalds 	return 0;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
1653e437e3d7STakashi Iwai static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1654e437e3d7STakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
16551da177e4SLinus Torvalds {
1656e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
165700f226d4SHonza Maly 	unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
165800f226d4SHonza Maly 	unsigned long port = chip->port + 0x10 * idx;
165900f226d4SHonza Maly 	unsigned char val;
166000f226d4SHonza Maly 	int i, change = 0;
166100f226d4SHonza Maly 
166200f226d4SHonza Maly 	for (i = 0; i < 2; i++) {
166300f226d4SHonza Maly 		val = ucontrol->value.integer.value[i];
166400f226d4SHonza Maly 		if (val > VIA_DXS_MAX_VOLUME)
166500f226d4SHonza Maly 			val = VIA_DXS_MAX_VOLUME;
166600f226d4SHonza Maly 		val = VIA_DXS_MAX_VOLUME - val;
166700f226d4SHonza Maly 		change |= val != chip->playback_volume[idx][i];
166800f226d4SHonza Maly 		if (change) {
166900f226d4SHonza Maly 			chip->playback_volume[idx][i] = val;
167000f226d4SHonza Maly 			outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
167100f226d4SHonza Maly 		}
167200f226d4SHonza Maly 	}
167300f226d4SHonza Maly 	return change;
167400f226d4SHonza Maly }
167500f226d4SHonza Maly 
1676e437e3d7STakashi Iwai static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1677e437e3d7STakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
167800f226d4SHonza Maly {
1679e437e3d7STakashi Iwai 	struct via82xx *chip = snd_kcontrol_chip(kcontrol);
16801da177e4SLinus Torvalds 	unsigned int idx;
16811da177e4SLinus Torvalds 	unsigned char val;
16821da177e4SLinus Torvalds 	int i, change = 0;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
16851da177e4SLinus Torvalds 		val = ucontrol->value.integer.value[i];
16861da177e4SLinus Torvalds 		if (val > VIA_DXS_MAX_VOLUME)
16871da177e4SLinus Torvalds 			val = VIA_DXS_MAX_VOLUME;
16881da177e4SLinus Torvalds 		val = VIA_DXS_MAX_VOLUME - val;
168900f226d4SHonza Maly 		if (val != chip->playback_volume_c[i]) {
16901da177e4SLinus Torvalds 			change = 1;
169100f226d4SHonza Maly 			chip->playback_volume_c[i] = val;
16921da177e4SLinus Torvalds 			for (idx = 0; idx < 4; idx++) {
16931da177e4SLinus Torvalds 				unsigned long port = chip->port + 0x10 * idx;
169400f226d4SHonza Maly 				chip->playback_volume[idx][i] = val;
16951da177e4SLinus Torvalds 				outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
16961da177e4SLinus Torvalds 			}
16971da177e4SLinus Torvalds 		}
16981da177e4SLinus Torvalds 	}
16991da177e4SLinus Torvalds 	return change;
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
17027058c042STakashi Iwai static DECLARE_TLV_DB_SCALE(db_scale_dxs, -9450, 150, 1);
17037058c042STakashi Iwai 
1704e437e3d7STakashi Iwai static struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control __devinitdata = {
17051da177e4SLinus Torvalds 	.name = "PCM Playback Volume",
17061da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
17077058c042STakashi Iwai 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
17087058c042STakashi Iwai 		   SNDRV_CTL_ELEM_ACCESS_TLV_READ),
17091da177e4SLinus Torvalds 	.info = snd_via8233_dxs_volume_info,
171000f226d4SHonza Maly 	.get = snd_via8233_pcmdxs_volume_get,
171100f226d4SHonza Maly 	.put = snd_via8233_pcmdxs_volume_put,
17127058c042STakashi Iwai 	.tlv = { .p = db_scale_dxs }
171300f226d4SHonza Maly };
171400f226d4SHonza Maly 
1715e437e3d7STakashi Iwai static struct snd_kcontrol_new snd_via8233_dxs_volume_control __devinitdata = {
171600f226d4SHonza Maly 	.name = "VIA DXS Playback Volume",
171700f226d4SHonza Maly 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
17187058c042STakashi Iwai 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
17197058c042STakashi Iwai 		   SNDRV_CTL_ELEM_ACCESS_TLV_READ),
172000f226d4SHonza Maly 	.count = 4,
172100f226d4SHonza Maly 	.info = snd_via8233_dxs_volume_info,
17221da177e4SLinus Torvalds 	.get = snd_via8233_dxs_volume_get,
17231da177e4SLinus Torvalds 	.put = snd_via8233_dxs_volume_put,
17247058c042STakashi Iwai 	.tlv = { .p = db_scale_dxs }
17251da177e4SLinus Torvalds };
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds /*
17281da177e4SLinus Torvalds  */
17291da177e4SLinus Torvalds 
1730e437e3d7STakashi Iwai static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
17311da177e4SLinus Torvalds {
1732e437e3d7STakashi Iwai 	struct via82xx *chip = bus->private_data;
17331da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
17341da177e4SLinus Torvalds }
17351da177e4SLinus Torvalds 
1736e437e3d7STakashi Iwai static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
17371da177e4SLinus Torvalds {
1738e437e3d7STakashi Iwai 	struct via82xx *chip = ac97->private_data;
17391da177e4SLinus Torvalds 	chip->ac97 = NULL;
17401da177e4SLinus Torvalds }
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds static struct ac97_quirk ac97_quirks[] = {
17431da177e4SLinus Torvalds 	{
174469ad07cfSJaroslav Kysela 		.subvendor = 0x1106,
174569ad07cfSJaroslav Kysela 		.subdevice = 0x4161,
17461da177e4SLinus Torvalds 		.codec_id = 0x56494161, /* VT1612A */
17471da177e4SLinus Torvalds 		.name = "Soltek SL-75DRV5",
17481da177e4SLinus Torvalds 		.type = AC97_TUNE_NONE
17491da177e4SLinus Torvalds 	},
17501da177e4SLinus Torvalds 	{	/* FIXME: which codec? */
175169ad07cfSJaroslav Kysela 		.subvendor = 0x1106,
175269ad07cfSJaroslav Kysela 		.subdevice = 0x4161,
17531da177e4SLinus Torvalds 		.name = "ASRock K7VT2",
17541da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY
17551da177e4SLinus Torvalds 	},
17561da177e4SLinus Torvalds 	{
175769ad07cfSJaroslav Kysela 		.subvendor = 0x1019,
175869ad07cfSJaroslav Kysela 		.subdevice = 0x0a81,
17591da177e4SLinus Torvalds 		.name = "ECS K7VTA3",
17601da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY
17611da177e4SLinus Torvalds 	},
17621da177e4SLinus Torvalds 	{
176369ad07cfSJaroslav Kysela 		.subvendor = 0x1019,
176469ad07cfSJaroslav Kysela 		.subdevice = 0x0a85,
17651da177e4SLinus Torvalds 		.name = "ECS L7VMM2",
17661da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY
17671da177e4SLinus Torvalds 	},
17681da177e4SLinus Torvalds 	{
176969ad07cfSJaroslav Kysela 		.subvendor = 0x1849,
177069ad07cfSJaroslav Kysela 		.subdevice = 0x3059,
17711da177e4SLinus Torvalds 		.name = "ASRock K7VM2",
17721da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY	/* VT1616 */
17731da177e4SLinus Torvalds 	},
17741da177e4SLinus Torvalds 	{
177569ad07cfSJaroslav Kysela 		.subvendor = 0x14cd,
177669ad07cfSJaroslav Kysela 		.subdevice = 0x7002,
17771da177e4SLinus Torvalds 		.name = "Unknown",
17781da177e4SLinus Torvalds 		.type = AC97_TUNE_ALC_JACK
17791da177e4SLinus Torvalds 	},
17801da177e4SLinus Torvalds 	{
178169ad07cfSJaroslav Kysela 		.subvendor = 0x1071,
178269ad07cfSJaroslav Kysela 		.subdevice = 0x8590,
17831da177e4SLinus Torvalds 		.name = "Mitac Mobo",
17841da177e4SLinus Torvalds 		.type = AC97_TUNE_ALC_JACK
17851da177e4SLinus Torvalds 	},
17861da177e4SLinus Torvalds 	{
178769ad07cfSJaroslav Kysela 		.subvendor = 0x161f,
178869ad07cfSJaroslav Kysela 		.subdevice = 0x202b,
17891da177e4SLinus Torvalds 		.name = "Arima Notebook",
17901da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY,
17911da177e4SLinus Torvalds 	},
1792dac8ddddSTakashi Iwai 	{
1793dac8ddddSTakashi Iwai 		.subvendor = 0x161f,
1794dac8ddddSTakashi Iwai 		.subdevice = 0x2032,
1795dac8ddddSTakashi Iwai 		.name = "Targa Traveller 811",
1796dac8ddddSTakashi Iwai 		.type = AC97_TUNE_HP_ONLY,
1797dac8ddddSTakashi Iwai 	},
1798d4199f01SDaniel T Chen 	{
1799d4199f01SDaniel T Chen 		.subvendor = 0x161f,
1800d4199f01SDaniel T Chen 		.subdevice = 0x2032,
1801d4199f01SDaniel T Chen 		.name = "m680x",
1802d4199f01SDaniel T Chen 		.type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1803d4199f01SDaniel T Chen 	},
18041da177e4SLinus Torvalds 	{ } /* terminator */
18051da177e4SLinus Torvalds };
18061da177e4SLinus Torvalds 
1807e437e3d7STakashi Iwai static int __devinit snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
18081da177e4SLinus Torvalds {
1809e437e3d7STakashi Iwai 	struct snd_ac97_template ac97;
18101da177e4SLinus Torvalds 	int err;
1811e437e3d7STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
18121da177e4SLinus Torvalds 		.write = snd_via82xx_codec_write,
18131da177e4SLinus Torvalds 		.read = snd_via82xx_codec_read,
18141da177e4SLinus Torvalds 		.wait = snd_via82xx_codec_wait,
18151da177e4SLinus Torvalds 	};
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
18181da177e4SLinus Torvalds 		return err;
18191da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
18201da177e4SLinus Torvalds 	chip->ac97_bus->clock = chip->ac97_clock;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
18231da177e4SLinus Torvalds 	ac97.private_data = chip;
18241da177e4SLinus Torvalds 	ac97.private_free = snd_via82xx_mixer_free_ac97;
18251da177e4SLinus Torvalds 	ac97.pci = chip->pci;
18262ba71978SSasha Khapyorsky 	ac97.scaps = AC97_SCAP_SKIP_MODEM;
18271da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
18281da177e4SLinus Torvalds 		return err;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686) {
18331da177e4SLinus Torvalds 		/* use slot 10/11 */
18341da177e4SLinus Torvalds 		snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	return 0;
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
18411da177e4SLinus Torvalds #define JOYSTICK_ADDR	0x200
1842e437e3d7STakashi Iwai static int __devinit snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
18431da177e4SLinus Torvalds {
18441da177e4SLinus Torvalds 	struct gameport *gp;
18451da177e4SLinus Torvalds 	struct resource *r;
18461da177e4SLinus Torvalds 
1847b7fe4622SClemens Ladisch 	if (!joystick)
18481da177e4SLinus Torvalds 		return -ENODEV;
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
18511da177e4SLinus Torvalds 	if (!r) {
1852e437e3d7STakashi Iwai 		printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n",
1853e437e3d7STakashi Iwai 		       JOYSTICK_ADDR);
18541da177e4SLinus Torvalds 		return -EBUSY;
18551da177e4SLinus Torvalds 	}
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	chip->gameport = gp = gameport_allocate_port();
18581da177e4SLinus Torvalds 	if (!gp) {
18591da177e4SLinus Torvalds 		printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
1860b1d5776dSTakashi Iwai 		release_and_free_resource(r);
18611da177e4SLinus Torvalds 		return -ENOMEM;
18621da177e4SLinus Torvalds 	}
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	gameport_set_name(gp, "VIA686 Gameport");
18651da177e4SLinus Torvalds 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
18661da177e4SLinus Torvalds 	gameport_set_dev_parent(gp, &chip->pci->dev);
18671da177e4SLinus Torvalds 	gp->io = JOYSTICK_ADDR;
18681da177e4SLinus Torvalds 	gameport_set_port_data(gp, r);
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	/* Enable legacy joystick port */
18711da177e4SLinus Torvalds 	*legacy |= VIA_FUNC_ENABLE_GAME;
18721da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	gameport_register_port(chip->gameport);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	return 0;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
1879e437e3d7STakashi Iwai static void snd_via686_free_gameport(struct via82xx *chip)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds 	if (chip->gameport) {
18821da177e4SLinus Torvalds 		struct resource *r = gameport_get_port_data(chip->gameport);
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 		gameport_unregister_port(chip->gameport);
18851da177e4SLinus Torvalds 		chip->gameport = NULL;
1886b1d5776dSTakashi Iwai 		release_and_free_resource(r);
18871da177e4SLinus Torvalds 	}
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds #else
1890e437e3d7STakashi Iwai static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	return -ENOSYS;
18931da177e4SLinus Torvalds }
1894e437e3d7STakashi Iwai static inline void snd_via686_free_gameport(struct via82xx *chip) { }
18951da177e4SLinus Torvalds #endif
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds /*
18991da177e4SLinus Torvalds  *
19001da177e4SLinus Torvalds  */
19011da177e4SLinus Torvalds 
1902e437e3d7STakashi Iwai static int __devinit snd_via8233_init_misc(struct via82xx *chip)
19031da177e4SLinus Torvalds {
19041da177e4SLinus Torvalds 	int i, err, caps;
19051da177e4SLinus Torvalds 	unsigned char val;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
19081da177e4SLinus Torvalds 	for (i = 0; i < caps; i++) {
19091da177e4SLinus Torvalds 		snd_via8233_capture_source.index = i;
19101da177e4SLinus Torvalds 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
19111da177e4SLinus Torvalds 		if (err < 0)
19121da177e4SLinus Torvalds 			return err;
19131da177e4SLinus Torvalds 	}
19141da177e4SLinus Torvalds 	if (ac97_can_spdif(chip->ac97)) {
19151da177e4SLinus Torvalds 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
19161da177e4SLinus Torvalds 		if (err < 0)
19171da177e4SLinus Torvalds 			return err;
19181da177e4SLinus Torvalds 	}
19191da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA8233A) {
19201da177e4SLinus Torvalds 		/* when no h/w PCM volume control is found, use DXS volume control
19211da177e4SLinus Torvalds 		 * as the PCM vol control
19221da177e4SLinus Torvalds 		 */
1923e437e3d7STakashi Iwai 		struct snd_ctl_elem_id sid;
19241da177e4SLinus Torvalds 		memset(&sid, 0, sizeof(sid));
19251da177e4SLinus Torvalds 		strcpy(sid.name, "PCM Playback Volume");
19261da177e4SLinus Torvalds 		sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
19271da177e4SLinus Torvalds 		if (! snd_ctl_find_id(chip->card, &sid)) {
192800f226d4SHonza Maly 			snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
192900f226d4SHonza Maly 			err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
193000f226d4SHonza Maly 			if (err < 0)
193100f226d4SHonza Maly 				return err;
193200f226d4SHonza Maly 		}
193300f226d4SHonza Maly 		else /* Using DXS when PCM emulation is enabled is really weird */
193400f226d4SHonza Maly 		{
193500f226d4SHonza Maly 			/* Standalone DXS controls */
19361da177e4SLinus Torvalds 			err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
19371da177e4SLinus Torvalds 			if (err < 0)
19381da177e4SLinus Torvalds 				return err;
19391da177e4SLinus Torvalds 		}
19401da177e4SLinus Torvalds 	}
19411da177e4SLinus Torvalds 	/* select spdif data slot 10/11 */
19421da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
19431da177e4SLinus Torvalds 	val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
19441da177e4SLinus Torvalds 	val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
19451da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	return 0;
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
1950e437e3d7STakashi Iwai static int __devinit snd_via686_init_misc(struct via82xx *chip)
19511da177e4SLinus Torvalds {
19521da177e4SLinus Torvalds 	unsigned char legacy, legacy_cfg;
19531da177e4SLinus Torvalds 	int rev_h = 0;
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	legacy = chip->old_legacy;
19561da177e4SLinus Torvalds 	legacy_cfg = chip->old_legacy_cfg;
19571da177e4SLinus Torvalds 	legacy |= VIA_FUNC_MIDI_IRQMASK;	/* FIXME: correct? (disable MIDI) */
19581da177e4SLinus Torvalds 	legacy &= ~VIA_FUNC_ENABLE_GAME;	/* disable joystick */
19591da177e4SLinus Torvalds 	if (chip->revision >= VIA_REV_686_H) {
19601da177e4SLinus Torvalds 		rev_h = 1;
1961b7fe4622SClemens Ladisch 		if (mpu_port >= 0x200) {	/* force MIDI */
1962b7fe4622SClemens Ladisch 			mpu_port &= 0xfffc;
1963b7fe4622SClemens Ladisch 			pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
19641da177e4SLinus Torvalds #ifdef CONFIG_PM
1965b7fe4622SClemens Ladisch 			chip->mpu_port_saved = mpu_port;
19661da177e4SLinus Torvalds #endif
19671da177e4SLinus Torvalds 		} else {
1968b7fe4622SClemens Ladisch 			mpu_port = pci_resource_start(chip->pci, 2);
19691da177e4SLinus Torvalds 		}
19701da177e4SLinus Torvalds 	} else {
1971b7fe4622SClemens Ladisch 		switch (mpu_port) {	/* force MIDI */
19721da177e4SLinus Torvalds 		case 0x300:
19731da177e4SLinus Torvalds 		case 0x310:
19741da177e4SLinus Torvalds 		case 0x320:
19751da177e4SLinus Torvalds 		case 0x330:
19761da177e4SLinus Torvalds 			legacy_cfg &= ~(3 << 2);
1977b7fe4622SClemens Ladisch 			legacy_cfg |= (mpu_port & 0x0030) >> 2;
19781da177e4SLinus Torvalds 			break;
19791da177e4SLinus Torvalds 		default:			/* no, use BIOS settings */
19801da177e4SLinus Torvalds 			if (legacy & VIA_FUNC_ENABLE_MIDI)
1981b7fe4622SClemens Ladisch 				mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
19821da177e4SLinus Torvalds 			break;
19831da177e4SLinus Torvalds 		}
19841da177e4SLinus Torvalds 	}
1985b7fe4622SClemens Ladisch 	if (mpu_port >= 0x200 &&
1986b7fe4622SClemens Ladisch 	    (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
1987b7fe4622SClemens Ladisch 	    != NULL) {
19881da177e4SLinus Torvalds 		if (rev_h)
19891da177e4SLinus Torvalds 			legacy |= VIA_FUNC_MIDI_PNP;	/* enable PCI I/O 2 */
19901da177e4SLinus Torvalds 		legacy |= VIA_FUNC_ENABLE_MIDI;
19911da177e4SLinus Torvalds 	} else {
19921da177e4SLinus Torvalds 		if (rev_h)
19931da177e4SLinus Torvalds 			legacy &= ~VIA_FUNC_MIDI_PNP;	/* disable PCI I/O 2 */
19941da177e4SLinus Torvalds 		legacy &= ~VIA_FUNC_ENABLE_MIDI;
1995b7fe4622SClemens Ladisch 		mpu_port = 0;
19961da177e4SLinus Torvalds 	}
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
19991da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
20001da177e4SLinus Torvalds 	if (chip->mpu_res) {
20011da177e4SLinus Torvalds 		if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
2002302e4c2fSTakashi Iwai 					mpu_port, MPU401_INFO_INTEGRATED,
20031da177e4SLinus Torvalds 					chip->irq, 0, &chip->rmidi) < 0) {
2004b7fe4622SClemens Ladisch 			printk(KERN_WARNING "unable to initialize MPU-401"
2005b7fe4622SClemens Ladisch 			       " at 0x%lx, skipping\n", mpu_port);
20061da177e4SLinus Torvalds 			legacy &= ~VIA_FUNC_ENABLE_MIDI;
20071da177e4SLinus Torvalds 		} else {
20081da177e4SLinus Torvalds 			legacy &= ~VIA_FUNC_MIDI_IRQMASK;	/* enable MIDI interrupt */
20091da177e4SLinus Torvalds 		}
20101da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
20111da177e4SLinus Torvalds 	}
20121da177e4SLinus Torvalds 
2013b7fe4622SClemens Ladisch 	snd_via686_create_gameport(chip, &legacy);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds #ifdef CONFIG_PM
20161da177e4SLinus Torvalds 	chip->legacy_saved = legacy;
20171da177e4SLinus Torvalds 	chip->legacy_cfg_saved = legacy_cfg;
20181da177e4SLinus Torvalds #endif
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	return 0;
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds /*
20251da177e4SLinus Torvalds  * proc interface
20261da177e4SLinus Torvalds  */
2027e437e3d7STakashi Iwai static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2028e437e3d7STakashi Iwai 				  struct snd_info_buffer *buffer)
20291da177e4SLinus Torvalds {
2030e437e3d7STakashi Iwai 	struct via82xx *chip = entry->private_data;
20311da177e4SLinus Torvalds 	int i;
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
20341da177e4SLinus Torvalds 	for (i = 0; i < 0xa0; i += 4) {
20351da177e4SLinus Torvalds 		snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
20361da177e4SLinus Torvalds 	}
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
2039e437e3d7STakashi Iwai static void __devinit snd_via82xx_proc_init(struct via82xx *chip)
20401da177e4SLinus Torvalds {
2041e437e3d7STakashi Iwai 	struct snd_info_entry *entry;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "via82xx", &entry))
2044bf850204STakashi Iwai 		snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds /*
20481da177e4SLinus Torvalds  *
20491da177e4SLinus Torvalds  */
20501da177e4SLinus Torvalds 
2051e437e3d7STakashi Iwai static int snd_via82xx_chip_init(struct via82xx *chip)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	unsigned int val;
2054ef21ca24SNishanth Aravamudan 	unsigned long end_time;
20551da177e4SLinus Torvalds 	unsigned char pval;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds #if 0 /* broken on K7M? */
20581da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686)
20591da177e4SLinus Torvalds 		/* disable all legacy ports */
20601da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
20611da177e4SLinus Torvalds #endif
20621da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
20631da177e4SLinus Torvalds 	if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
20641da177e4SLinus Torvalds 		/* deassert ACLink reset, force SYNC */
20651da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
20661da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_ENABLE |
20671da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_RESET |
20681da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_SYNC);
20691da177e4SLinus Torvalds 		udelay(100);
20701da177e4SLinus Torvalds #if 1 /* FIXME: should we do full reset here for all chip models? */
20711da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
20721da177e4SLinus Torvalds 		udelay(100);
20731da177e4SLinus Torvalds #else
20741da177e4SLinus Torvalds 		/* deassert ACLink reset, force SYNC (warm AC'97 reset) */
20751da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
20761da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
20771da177e4SLinus Torvalds 		udelay(2);
20781da177e4SLinus Torvalds #endif
20791da177e4SLinus Torvalds 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
20801da177e4SLinus Torvalds 		/* note - FM data out has trouble with non VRA codecs !! */
20811da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
20821da177e4SLinus Torvalds 		udelay(100);
20831da177e4SLinus Torvalds 	}
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* Make sure VRA is enabled, in case we didn't do a
20861da177e4SLinus Torvalds 	 * complete codec reset, above */
20871da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
20881da177e4SLinus Torvalds 	if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
20891da177e4SLinus Torvalds 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
20901da177e4SLinus Torvalds 		/* note - FM data out has trouble with non VRA codecs !! */
20911da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
20921da177e4SLinus Torvalds 		udelay(100);
20931da177e4SLinus Torvalds 	}
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	/* wait until codec ready */
2096ef21ca24SNishanth Aravamudan 	end_time = jiffies + msecs_to_jiffies(750);
20971da177e4SLinus Torvalds 	do {
20981da177e4SLinus Torvalds 		pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
20991da177e4SLinus Torvalds 		if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
21001da177e4SLinus Torvalds 			break;
21018433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
2102ef21ca24SNishanth Aravamudan 	} while (time_before(jiffies, end_time));
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
210599b359baSTakashi Iwai 		snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
21081da177e4SLinus Torvalds 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
21091da177e4SLinus Torvalds 				 VIA_REG_AC97_SECONDARY_VALID |
21101da177e4SLinus Torvalds 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2111ef21ca24SNishanth Aravamudan 	end_time = jiffies + msecs_to_jiffies(750);
21121da177e4SLinus Torvalds 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
21131da177e4SLinus Torvalds 				 VIA_REG_AC97_SECONDARY_VALID |
21141da177e4SLinus Torvalds 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
21151da177e4SLinus Torvalds 	do {
21161da177e4SLinus Torvalds 		if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
21171da177e4SLinus Torvalds 			chip->ac97_secondary = 1;
21181da177e4SLinus Torvalds 			goto __ac97_ok2;
21191da177e4SLinus Torvalds 		}
21208433a509SNishanth Aravamudan 		schedule_timeout_interruptible(1);
2121ef21ca24SNishanth Aravamudan 	} while (time_before(jiffies, end_time));
21221da177e4SLinus Torvalds 	/* This is ok, the most of motherboards have only one codec */
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds       __ac97_ok2:
21251da177e4SLinus Torvalds #endif
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686) {
21281da177e4SLinus Torvalds 		/* route FM trap to IRQ, disable FM trap */
21291da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
21301da177e4SLinus Torvalds 		/* disable all GPI interrupts */
21311da177e4SLinus Torvalds 		outl(0, VIAREG(chip, GPI_INTR));
21321da177e4SLinus Torvalds 	}
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686) {
21351da177e4SLinus Torvalds 		/* Workaround for Award BIOS bug:
21361da177e4SLinus Torvalds 		 * DXS channels don't work properly with VRA if MC97 is disabled.
21371da177e4SLinus Torvalds 		 */
21381da177e4SLinus Torvalds 		struct pci_dev *pci;
21390dd119f7SJiri Slaby 		pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
21401da177e4SLinus Torvalds 		if (pci) {
21411da177e4SLinus Torvalds 			unsigned char data;
21421da177e4SLinus Torvalds 			pci_read_config_byte(pci, 0x44, &data);
21431da177e4SLinus Torvalds 			pci_write_config_byte(pci, 0x44, data | 0x40);
21440dd119f7SJiri Slaby 			pci_dev_put(pci);
21451da177e4SLinus Torvalds 		}
21461da177e4SLinus Torvalds 	}
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA8233A) {
21491da177e4SLinus Torvalds 		int i, idx;
21501da177e4SLinus Torvalds 		for (idx = 0; idx < 4; idx++) {
21511da177e4SLinus Torvalds 			unsigned long port = chip->port + 0x10 * idx;
215200f226d4SHonza Maly 			for (i = 0; i < 2; i++) {
215300f226d4SHonza Maly 				chip->playback_volume[idx][i]=chip->playback_volume_c[i];
2154e437e3d7STakashi Iwai 				outb(chip->playback_volume_c[i],
2155e437e3d7STakashi Iwai 				     port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
215600f226d4SHonza Maly 			}
21571da177e4SLinus Torvalds 		}
21581da177e4SLinus Torvalds 	}
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	return 0;
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds #ifdef CONFIG_PM
21641da177e4SLinus Torvalds /*
21651da177e4SLinus Torvalds  * power management
21661da177e4SLinus Torvalds  */
216757feb835STakashi Iwai static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
21681da177e4SLinus Torvalds {
216957feb835STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
217057feb835STakashi Iwai 	struct via82xx *chip = card->private_data;
21711da177e4SLinus Torvalds 	int i;
21721da177e4SLinus Torvalds 
217357feb835STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
21741da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
21751da177e4SLinus Torvalds 		snd_pcm_suspend_all(chip->pcms[i]);
21761da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
21771da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
21781da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
21791da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	/* save misc values */
21821da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686) {
21831da177e4SLinus Torvalds 		pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
21841da177e4SLinus Torvalds 		chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
21851da177e4SLinus Torvalds 		chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
21861da177e4SLinus Torvalds 	}
21871da177e4SLinus Torvalds 
218857feb835STakashi Iwai 	pci_disable_device(pci);
218957feb835STakashi Iwai 	pci_save_state(pci);
2190*30b35399STakashi Iwai 	pci_set_power_state(pci, pci_choose_state(pci, state));
21911da177e4SLinus Torvalds 	return 0;
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
219457feb835STakashi Iwai static int snd_via82xx_resume(struct pci_dev *pci)
21951da177e4SLinus Torvalds {
219657feb835STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
219757feb835STakashi Iwai 	struct via82xx *chip = card->private_data;
21981da177e4SLinus Torvalds 	int i;
21991da177e4SLinus Torvalds 
220057feb835STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
2201*30b35399STakashi Iwai 	pci_restore_state(pci);
2202*30b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
2203*30b35399STakashi Iwai 		printk(KERN_ERR "via82xx: pci_enable_device failed, "
2204*30b35399STakashi Iwai 		       "disabling device\n");
2205*30b35399STakashi Iwai 		snd_card_disconnect(card);
2206*30b35399STakashi Iwai 		return -EIO;
2207*30b35399STakashi Iwai 	}
2208*30b35399STakashi Iwai 	pci_set_master(pci);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	snd_via82xx_chip_init(chip);
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686) {
22131da177e4SLinus Torvalds 		if (chip->mpu_port_saved)
22141da177e4SLinus Torvalds 			pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
22151da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
22161da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
22171da177e4SLinus Torvalds 	} else {
22181da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
22191da177e4SLinus Torvalds 		outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
22201da177e4SLinus Torvalds 		outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
22211da177e4SLinus Torvalds 	}
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
22261da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
22271da177e4SLinus Torvalds 
222857feb835STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
22291da177e4SLinus Torvalds 	return 0;
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds #endif /* CONFIG_PM */
22321da177e4SLinus Torvalds 
2233e437e3d7STakashi Iwai static int snd_via82xx_free(struct via82xx *chip)
22341da177e4SLinus Torvalds {
22351da177e4SLinus Torvalds 	unsigned int i;
22361da177e4SLinus Torvalds 
22371da177e4SLinus Torvalds 	if (chip->irq < 0)
22381da177e4SLinus Torvalds 		goto __end_hw;
22391da177e4SLinus Torvalds 	/* disable interrupts */
22401da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
22411da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
22421da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
22431da177e4SLinus Torvalds       __end_hw:
22441da177e4SLinus Torvalds 	if (chip->irq >= 0)
2245e437e3d7STakashi Iwai 		free_irq(chip->irq, chip);
2246b1d5776dSTakashi Iwai 	release_and_free_resource(chip->mpu_res);
22471da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686) {
22501da177e4SLinus Torvalds 		snd_via686_free_gameport(chip);
22511da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
22521da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
22551da177e4SLinus Torvalds 	kfree(chip);
22561da177e4SLinus Torvalds 	return 0;
22571da177e4SLinus Torvalds }
22581da177e4SLinus Torvalds 
2259e437e3d7STakashi Iwai static int snd_via82xx_dev_free(struct snd_device *device)
22601da177e4SLinus Torvalds {
2261e437e3d7STakashi Iwai 	struct via82xx *chip = device->device_data;
22621da177e4SLinus Torvalds 	return snd_via82xx_free(chip);
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
2265e437e3d7STakashi Iwai static int __devinit snd_via82xx_create(struct snd_card *card,
22661da177e4SLinus Torvalds 					struct pci_dev *pci,
22671da177e4SLinus Torvalds 					int chip_type,
22681da177e4SLinus Torvalds 					int revision,
22691da177e4SLinus Torvalds 					unsigned int ac97_clock,
2270e437e3d7STakashi Iwai 					struct via82xx ** r_via)
22711da177e4SLinus Torvalds {
2272e437e3d7STakashi Iwai 	struct via82xx *chip;
22731da177e4SLinus Torvalds 	int err;
2274e437e3d7STakashi Iwai         static struct snd_device_ops ops = {
22751da177e4SLinus Torvalds 		.dev_free =	snd_via82xx_dev_free,
22761da177e4SLinus Torvalds         };
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
22791da177e4SLinus Torvalds 		return err;
22801da177e4SLinus Torvalds 
2281e560d8d8STakashi Iwai 	if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
22821da177e4SLinus Torvalds 		pci_disable_device(pci);
22831da177e4SLinus Torvalds 		return -ENOMEM;
22841da177e4SLinus Torvalds 	}
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	chip->chip_type = chip_type;
22871da177e4SLinus Torvalds 	chip->revision = revision;
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
22901da177e4SLinus Torvalds 	spin_lock_init(&chip->rates[0].lock);
22911da177e4SLinus Torvalds 	spin_lock_init(&chip->rates[1].lock);
22921da177e4SLinus Torvalds 	chip->card = card;
22931da177e4SLinus Torvalds 	chip->pci = pci;
22941da177e4SLinus Torvalds 	chip->irq = -1;
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
22971da177e4SLinus Torvalds 	pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
22981da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
22991da177e4SLinus Torvalds 			      chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
23021da177e4SLinus Torvalds 		kfree(chip);
23031da177e4SLinus Torvalds 		pci_disable_device(pci);
23041da177e4SLinus Torvalds 		return err;
23051da177e4SLinus Torvalds 	}
23061da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
23074f550df5SKarsten Wiese 	if (request_irq(pci->irq,
23084f550df5SKarsten Wiese 			chip_type == TYPE_VIA8233 ?
23094f550df5SKarsten Wiese 			snd_via8233_interrupt :	snd_via686_interrupt,
231065ca68b3SThomas Gleixner 			IRQF_DISABLED|IRQF_SHARED,
2311e437e3d7STakashi Iwai 			card->driver, chip)) {
231299b359baSTakashi Iwai 		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
23131da177e4SLinus Torvalds 		snd_via82xx_free(chip);
23141da177e4SLinus Torvalds 		return -EBUSY;
23151da177e4SLinus Torvalds 	}
23161da177e4SLinus Torvalds 	chip->irq = pci->irq;
23171da177e4SLinus Torvalds 	if (ac97_clock >= 8000 && ac97_clock <= 48000)
23181da177e4SLinus Torvalds 		chip->ac97_clock = ac97_clock;
23191da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	if ((err = snd_via82xx_chip_init(chip)) < 0) {
23221da177e4SLinus Torvalds 		snd_via82xx_free(chip);
23231da177e4SLinus Torvalds 		return err;
23241da177e4SLinus Torvalds 	}
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
23271da177e4SLinus Torvalds 		snd_via82xx_free(chip);
23281da177e4SLinus Torvalds 		return err;
23291da177e4SLinus Torvalds 	}
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds 	/* The 8233 ac97 controller does not implement the master bit
23321da177e4SLinus Torvalds 	 * in the pci command register. IMHO this is a violation of the PCI spec.
23331da177e4SLinus Torvalds 	 * We call pci_set_master here because it does not hurt. */
23341da177e4SLinus Torvalds 	pci_set_master(pci);
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	*r_via = chip;
23391da177e4SLinus Torvalds 	return 0;
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds struct via823x_info {
23431da177e4SLinus Torvalds 	int revision;
23441da177e4SLinus Torvalds 	char *name;
23451da177e4SLinus Torvalds 	int type;
23461da177e4SLinus Torvalds };
23471da177e4SLinus Torvalds static struct via823x_info via823x_cards[] __devinitdata = {
23481da177e4SLinus Torvalds 	{ VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
23491da177e4SLinus Torvalds 	{ VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
23501da177e4SLinus Torvalds 	{ VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
23511da177e4SLinus Torvalds 	{ VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
23521da177e4SLinus Torvalds 	{ VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
23531da177e4SLinus Torvalds 	{ VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
23548263c65fSBastiaan Jacques 	{ VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
23551da177e4SLinus Torvalds };
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds /*
23581da177e4SLinus Torvalds  * auto detection of DXS channel supports.
23591da177e4SLinus Torvalds  */
23601da177e4SLinus Torvalds struct dxs_whitelist {
236169ad07cfSJaroslav Kysela 	unsigned short subvendor;
236269ad07cfSJaroslav Kysela 	unsigned short subdevice;
23631da177e4SLinus Torvalds 	unsigned short mask;
23641da177e4SLinus Torvalds 	short action;	/* new dxs_support value */
23651da177e4SLinus Torvalds };
23661da177e4SLinus Torvalds 
2367a769577bSTakashi Iwai static int __devinit check_dxs_list(struct pci_dev *pci, int revision)
23681da177e4SLinus Torvalds {
23691da177e4SLinus Torvalds 	static struct dxs_whitelist whitelist[] = {
237069ad07cfSJaroslav Kysela 		{ .subvendor = 0x1005, .subdevice = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
237169ad07cfSJaroslav Kysela 		{ .subvendor = 0x1019, .subdevice = 0x0996, .action = VIA_DXS_48K },
237269ad07cfSJaroslav Kysela 		{ .subvendor = 0x1019, .subdevice = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
237369ad07cfSJaroslav Kysela 		{ .subvendor = 0x1019, .subdevice = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
2374db99055fSTakashi Iwai 		{ .subvendor = 0x1019, .subdevice = 0xa101, .action = VIA_DXS_SRC },
2375a26e9d9dSTakashi Iwai 		{ .subvendor = 0x1019, .subdevice = 0xaa01, .action = VIA_DXS_SRC }, /* ECS K8T890-A */
237669ad07cfSJaroslav Kysela 		{ .subvendor = 0x1025, .subdevice = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
2377f347c774STakashi Iwai 		{ .subvendor = 0x1025, .subdevice = 0x0046, .action = VIA_DXS_SRC }, /* Acer Aspire 1524 WLMi */
237869ad07cfSJaroslav Kysela 		{ .subvendor = 0x1043, .subdevice = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
237969ad07cfSJaroslav Kysela 		{ .subvendor = 0x1043, .subdevice = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
238069ad07cfSJaroslav Kysela 		{ .subvendor = 0x1043, .subdevice = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
2381c66186e1STakashi Iwai 		{ .subvendor = 0x1043, .subdevice = 0x810d, .action = VIA_DXS_SRC }, /* ASUS */
238269ad07cfSJaroslav Kysela 		{ .subvendor = 0x1043, .subdevice = 0x812a, .action = VIA_DXS_SRC    }, /* ASUS A8V Deluxe */
23836c1080c1STakashi Iwai 		{ .subvendor = 0x1043, .subdevice = 0x8174, .action = VIA_DXS_SRC    }, /* ASUS */
23848263c65fSBastiaan Jacques 		{ .subvendor = 0x1043, .subdevice = 0x81b9, .action = VIA_DXS_SRC    }, /* ASUS A8V-MX */
238569ad07cfSJaroslav Kysela 		{ .subvendor = 0x1071, .subdevice = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
238696d07815SJaroslav Kysela 		{ .subvendor = 0x1071, .subdevice = 0x8399, .action = VIA_DXS_NO_VRA }, /* Umax AB 595T (VIA K8N800A - VT8237) */
238769ad07cfSJaroslav Kysela 		{ .subvendor = 0x10cf, .subdevice = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
238869ad07cfSJaroslav Kysela 		{ .subvendor = 0x1106, .subdevice = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
238969ad07cfSJaroslav Kysela 		{ .subvendor = 0x1106, .subdevice = 0x4552, .action = VIA_DXS_NO_VRA }, /* QDI Kudoz 7X/600-6AL */
239069ad07cfSJaroslav Kysela 		{ .subvendor = 0x1106, .subdevice = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
239169ad07cfSJaroslav Kysela 		{ .subvendor = 0x1106, .subdevice = 0xc001, .action = VIA_DXS_SRC }, /* Insight P4-ITX */
239269ad07cfSJaroslav Kysela 		{ .subvendor = 0x1297, .subdevice = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
239369ad07cfSJaroslav Kysela 		{ .subvendor = 0x1297, .subdevice = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
239469ad07cfSJaroslav Kysela 		{ .subvendor = 0x1458, .subdevice = 0xa002, .action = VIA_DXS_ENABLE }, /* Gigabyte GA-7VAXP */
239569ad07cfSJaroslav Kysela 		{ .subvendor = 0x1462, .subdevice = 0x0080, .action = VIA_DXS_SRC }, /* MSI K8T Neo-FIS2R */
2396352dbfd0STakashi Iwai 		{ .subvendor = 0x1462, .subdevice = 0x0430, .action = VIA_DXS_SRC }, /* MSI 7142 (K8MM-V) */
2397c1b8f5f0STakashi Iwai 		{ .subvendor = 0x1462, .subdevice = 0x0470, .action = VIA_DXS_SRC }, /* MSI KT880 Delta-FSR */
239869ad07cfSJaroslav Kysela 		{ .subvendor = 0x1462, .subdevice = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
239969ad07cfSJaroslav Kysela 		{ .subvendor = 0x1462, .subdevice = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
2400396f739eSKarsten Wiese 		{ .subvendor = 0x1462, .subdevice = 0x7023, .action = VIA_DXS_SRC }, /* MSI K8T Neo2-FI */
240169ad07cfSJaroslav Kysela 		{ .subvendor = 0x1462, .subdevice = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
2402f347c774STakashi Iwai 		{ .subvendor = 0x1462, .subdevice = 0x7142, .action = VIA_DXS_ENABLE }, /* MSI K8MM-V */
2403802c00f2STakashi Iwai 		{ .subvendor = 0x1462, .subdevice = 0xb012, .action = VIA_DXS_SRC }, /* P4M800/VIA8237R */
240469ad07cfSJaroslav Kysela 		{ .subvendor = 0x147b, .subdevice = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
240569ad07cfSJaroslav Kysela 		{ .subvendor = 0x147b, .subdevice = 0x1411, .action = VIA_DXS_ENABLE }, /* ABIT VA-20 */
240669ad07cfSJaroslav Kysela 		{ .subvendor = 0x147b, .subdevice = 0x1413, .action = VIA_DXS_ENABLE }, /* ABIT KV8 Pro */
240769ad07cfSJaroslav Kysela 		{ .subvendor = 0x147b, .subdevice = 0x1415, .action = VIA_DXS_NO_VRA }, /* Abit AV8 */
240869ad07cfSJaroslav Kysela 		{ .subvendor = 0x14ff, .subdevice = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
240969c3e5f8STakashi Iwai 		{ .subvendor = 0x14ff, .subdevice = 0x0408, .action = VIA_DXS_SRC }, /* Twinhead laptop */
241094651a5bSTakashi Iwai 		{ .subvendor = 0x1558, .subdevice = 0x4701, .action = VIA_DXS_SRC }, /* Clevo D470 */
241169ad07cfSJaroslav Kysela 		{ .subvendor = 0x1584, .subdevice = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
241269ad07cfSJaroslav Kysela 		{ .subvendor = 0x1584, .subdevice = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
241369ad07cfSJaroslav Kysela 		{ .subvendor = 0x161f, .subdevice = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
241469ad07cfSJaroslav Kysela 		{ .subvendor = 0x161f, .subdevice = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
241569ad07cfSJaroslav Kysela 		{ .subvendor = 0x1631, .subdevice = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
241669ad07cfSJaroslav Kysela 		{ .subvendor = 0x1695, .subdevice = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
2417c04d092bSTakashi Iwai 		{ .subvendor = 0x1695, .subdevice = 0x300c, .action = VIA_DXS_SRC }, /* EPoX EP-8KRAI */
2418cacd3347STakashi Iwai 		{ .subvendor = 0x1695, .subdevice = 0x300e, .action = VIA_DXS_SRC }, /* EPoX 9HEAI */
2419d0bd41e2STakashi Iwai 		{ .subvendor = 0x16f3, .subdevice = 0x6405, .action = VIA_DXS_SRC }, /* Jetway K8M8MS */
2420bd84b0ccSTakashi Iwai 		{ .subvendor = 0x1734, .subdevice = 0x1078, .action = VIA_DXS_SRC }, /* FSC Amilo L7300 */
242151c2bbfcSTakashi Iwai 		{ .subvendor = 0x1734, .subdevice = 0x1093, .action = VIA_DXS_SRC }, /* FSC */
2422b7c6b034STakashi Iwai 		{ .subvendor = 0x1734, .subdevice = 0x10ab, .action = VIA_DXS_SRC }, /* FSC */
242369ad07cfSJaroslav Kysela 		{ .subvendor = 0x1849, .subdevice = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
242442611c02STakashi Iwai 		{ .subvendor = 0x1849, .subdevice = 0x9739, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
242546a1736dSTakashi Iwai 		{ .subvendor = 0x1849, .subdevice = 0x9761, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
2426b3e28ce9STakashi Iwai 		{ .subvendor = 0x1919, .subdevice = 0x200a, .action = VIA_DXS_NO_VRA }, /* Soltek SL-K8Tpro-939 */
24272201987cSJaroslav Kysela 		{ .subvendor = 0x4005, .subdevice = 0x4710, .action = VIA_DXS_SRC },	/* MSI K7T266 Pro2 (MS-6380 V2.0) BIOS 3.7 */
24281da177e4SLinus Torvalds 		{ } /* terminator */
24291da177e4SLinus Torvalds 	};
24301da177e4SLinus Torvalds 	struct dxs_whitelist *w;
24311da177e4SLinus Torvalds 	unsigned short subsystem_vendor;
24321da177e4SLinus Torvalds 	unsigned short subsystem_device;
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 	pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
24351da177e4SLinus Torvalds 	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
24361da177e4SLinus Torvalds 
243769ad07cfSJaroslav Kysela 	for (w = whitelist; w->subvendor; w++) {
243869ad07cfSJaroslav Kysela 		if (w->subvendor != subsystem_vendor)
24391da177e4SLinus Torvalds 			continue;
24401da177e4SLinus Torvalds 		if (w->mask) {
244169ad07cfSJaroslav Kysela 			if ((w->mask & subsystem_device) == w->subdevice)
24421da177e4SLinus Torvalds 				return w->action;
24431da177e4SLinus Torvalds 		} else {
244469ad07cfSJaroslav Kysela 			if (subsystem_device == w->subdevice)
24451da177e4SLinus Torvalds 				return w->action;
24461da177e4SLinus Torvalds 		}
24471da177e4SLinus Torvalds 	}
24481da177e4SLinus Torvalds 
2449a769577bSTakashi Iwai 	/* for newer revision, default to DXS_SRC */
2450a769577bSTakashi Iwai 	if (revision >= VIA_REV_8235)
2451a769577bSTakashi Iwai 		return VIA_DXS_SRC;
2452a769577bSTakashi Iwai 
24531da177e4SLinus Torvalds 	/*
24541da177e4SLinus Torvalds 	 * not detected, try 48k rate only to be sure.
24551da177e4SLinus Torvalds 	 */
24561da177e4SLinus Torvalds 	printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
2457ee3b4c60STakashi Iwai 	printk(KERN_INFO "         Please try dxs_support=5 option\n");
24581da177e4SLinus Torvalds 	printk(KERN_INFO "         and report if it works on your machine.\n");
2459ee3b4c60STakashi Iwai 	printk(KERN_INFO "         For more details, read ALSA-Configuration.txt.\n");
24601da177e4SLinus Torvalds 	return VIA_DXS_48K;
24611da177e4SLinus Torvalds };
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds static int __devinit snd_via82xx_probe(struct pci_dev *pci,
24641da177e4SLinus Torvalds 				       const struct pci_device_id *pci_id)
24651da177e4SLinus Torvalds {
2466e437e3d7STakashi Iwai 	struct snd_card *card;
2467e437e3d7STakashi Iwai 	struct via82xx *chip;
24681da177e4SLinus Torvalds 	unsigned char revision;
24691da177e4SLinus Torvalds 	int chip_type = 0, card_type;
24701da177e4SLinus Torvalds 	unsigned int i;
24711da177e4SLinus Torvalds 	int err;
24721da177e4SLinus Torvalds 
2473b7fe4622SClemens Ladisch 	card = snd_card_new(index, id, THIS_MODULE, 0);
24741da177e4SLinus Torvalds 	if (card == NULL)
24751da177e4SLinus Torvalds 		return -ENOMEM;
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 	card_type = pci_id->driver_data;
24781da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
24791da177e4SLinus Torvalds 	switch (card_type) {
24801da177e4SLinus Torvalds 	case TYPE_CARD_VIA686:
24811da177e4SLinus Torvalds 		strcpy(card->driver, "VIA686A");
24821da177e4SLinus Torvalds 		sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
24831da177e4SLinus Torvalds 		chip_type = TYPE_VIA686;
24841da177e4SLinus Torvalds 		break;
24851da177e4SLinus Torvalds 	case TYPE_CARD_VIA8233:
24861da177e4SLinus Torvalds 		chip_type = TYPE_VIA8233;
24871da177e4SLinus Torvalds 		sprintf(card->shortname, "VIA 823x rev%x", revision);
24881da177e4SLinus Torvalds 		for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
24891da177e4SLinus Torvalds 			if (revision == via823x_cards[i].revision) {
24901da177e4SLinus Torvalds 				chip_type = via823x_cards[i].type;
24911da177e4SLinus Torvalds 				strcpy(card->shortname, via823x_cards[i].name);
24921da177e4SLinus Torvalds 				break;
24931da177e4SLinus Torvalds 			}
24941da177e4SLinus Torvalds 		}
24951da177e4SLinus Torvalds 		if (chip_type != TYPE_VIA8233A) {
2496b7fe4622SClemens Ladisch 			if (dxs_support == VIA_DXS_AUTO)
2497a769577bSTakashi Iwai 				dxs_support = check_dxs_list(pci, revision);
24981da177e4SLinus Torvalds 			/* force to use VIA8233 or 8233A model according to
24991da177e4SLinus Torvalds 			 * dxs_support module option
25001da177e4SLinus Torvalds 			 */
2501b7fe4622SClemens Ladisch 			if (dxs_support == VIA_DXS_DISABLE)
25021da177e4SLinus Torvalds 				chip_type = TYPE_VIA8233A;
25031da177e4SLinus Torvalds 			else
25041da177e4SLinus Torvalds 				chip_type = TYPE_VIA8233;
25051da177e4SLinus Torvalds 		}
25061da177e4SLinus Torvalds 		if (chip_type == TYPE_VIA8233A)
25071da177e4SLinus Torvalds 			strcpy(card->driver, "VIA8233A");
25081da177e4SLinus Torvalds 		else if (revision >= VIA_REV_8237)
25091da177e4SLinus Torvalds 			strcpy(card->driver, "VIA8237"); /* no slog assignment */
25101da177e4SLinus Torvalds 		else
25111da177e4SLinus Torvalds 			strcpy(card->driver, "VIA8233");
25121da177e4SLinus Torvalds 		break;
25131da177e4SLinus Torvalds 	default:
25141da177e4SLinus Torvalds 		snd_printk(KERN_ERR "invalid card type %d\n", card_type);
25151da177e4SLinus Torvalds 		err = -EINVAL;
25161da177e4SLinus Torvalds 		goto __error;
25171da177e4SLinus Torvalds 	}
25181da177e4SLinus Torvalds 
2519b7fe4622SClemens Ladisch 	if ((err = snd_via82xx_create(card, pci, chip_type, revision,
2520b7fe4622SClemens Ladisch 				      ac97_clock, &chip)) < 0)
25211da177e4SLinus Torvalds 		goto __error;
252257feb835STakashi Iwai 	card->private_data = chip;
2523b7fe4622SClemens Ladisch 	if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
25241da177e4SLinus Torvalds 		goto __error;
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	if (chip_type == TYPE_VIA686) {
25271da177e4SLinus Torvalds 		if ((err = snd_via686_pcm_new(chip)) < 0 ||
2528b7fe4622SClemens Ladisch 		    (err = snd_via686_init_misc(chip)) < 0)
25291da177e4SLinus Torvalds 			goto __error;
25301da177e4SLinus Torvalds 	} else {
25311da177e4SLinus Torvalds 		if (chip_type == TYPE_VIA8233A) {
25321da177e4SLinus Torvalds 			if ((err = snd_via8233a_pcm_new(chip)) < 0)
25331da177e4SLinus Torvalds 				goto __error;
25341da177e4SLinus Torvalds 			// chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
25351da177e4SLinus Torvalds 		} else {
25361da177e4SLinus Torvalds 			if ((err = snd_via8233_pcm_new(chip)) < 0)
25371da177e4SLinus Torvalds 				goto __error;
2538b7fe4622SClemens Ladisch 			if (dxs_support == VIA_DXS_48K)
25391da177e4SLinus Torvalds 				chip->dxs_fixed = 1;
2540b7fe4622SClemens Ladisch 			else if (dxs_support == VIA_DXS_NO_VRA)
25411da177e4SLinus Torvalds 				chip->no_vra = 1;
2542b7fe4622SClemens Ladisch 			else if (dxs_support == VIA_DXS_SRC) {
25432d7eb7cbSSergey Vlasov 				chip->no_vra = 1;
25442d7eb7cbSSergey Vlasov 				chip->dxs_src = 1;
25452d7eb7cbSSergey Vlasov 			}
25461da177e4SLinus Torvalds 		}
2547b7fe4622SClemens Ladisch 		if ((err = snd_via8233_init_misc(chip)) < 0)
25481da177e4SLinus Torvalds 			goto __error;
25491da177e4SLinus Torvalds 	}
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	/* disable interrupts */
25521da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
25531da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 	snprintf(card->longname, sizeof(card->longname),
25561da177e4SLinus Torvalds 		 "%s with %s at %#lx, irq %d", card->shortname,
25571da177e4SLinus Torvalds 		 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	snd_via82xx_proc_init(chip);
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
25621da177e4SLinus Torvalds 		snd_card_free(card);
25631da177e4SLinus Torvalds 		return err;
25641da177e4SLinus Torvalds 	}
25651da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
25661da177e4SLinus Torvalds 	return 0;
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds  __error:
25691da177e4SLinus Torvalds 	snd_card_free(card);
25701da177e4SLinus Torvalds 	return err;
25711da177e4SLinus Torvalds }
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds static void __devexit snd_via82xx_remove(struct pci_dev *pci)
25741da177e4SLinus Torvalds {
25751da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
25761da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
25771da177e4SLinus Torvalds }
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds static struct pci_driver driver = {
25801da177e4SLinus Torvalds 	.name = "VIA 82xx Audio",
25811da177e4SLinus Torvalds 	.id_table = snd_via82xx_ids,
25821da177e4SLinus Torvalds 	.probe = snd_via82xx_probe,
25831da177e4SLinus Torvalds 	.remove = __devexit_p(snd_via82xx_remove),
258457feb835STakashi Iwai #ifdef CONFIG_PM
258557feb835STakashi Iwai 	.suspend = snd_via82xx_suspend,
258657feb835STakashi Iwai 	.resume = snd_via82xx_resume,
258757feb835STakashi Iwai #endif
25881da177e4SLinus Torvalds };
25891da177e4SLinus Torvalds 
25901da177e4SLinus Torvalds static int __init alsa_card_via82xx_init(void)
25911da177e4SLinus Torvalds {
259201d25d46STakashi Iwai 	return pci_register_driver(&driver);
25931da177e4SLinus Torvalds }
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds static void __exit alsa_card_via82xx_exit(void)
25961da177e4SLinus Torvalds {
25971da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds module_init(alsa_card_via82xx_init)
26011da177e4SLinus Torvalds module_exit(alsa_card_via82xx_exit)
2602