xref: /linux/sound/pci/via82xx.c (revision 01d25d460a3b28aab537fab9a0038d1b5832ce28)
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.
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <sound/driver.h>
471da177e4SLinus Torvalds #include <asm/io.h>
481da177e4SLinus Torvalds #include <linux/delay.h>
491da177e4SLinus Torvalds #include <linux/interrupt.h>
501da177e4SLinus Torvalds #include <linux/init.h>
511da177e4SLinus Torvalds #include <linux/pci.h>
521da177e4SLinus Torvalds #include <linux/slab.h>
531da177e4SLinus Torvalds #include <linux/gameport.h>
541da177e4SLinus Torvalds #include <linux/moduleparam.h>
551da177e4SLinus Torvalds #include <sound/core.h>
561da177e4SLinus Torvalds #include <sound/pcm.h>
571da177e4SLinus Torvalds #include <sound/pcm_params.h>
581da177e4SLinus Torvalds #include <sound/info.h>
591da177e4SLinus Torvalds #include <sound/ac97_codec.h>
601da177e4SLinus Torvalds #include <sound/mpu401.h>
611da177e4SLinus Torvalds #include <sound/initval.h>
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #if 0
641da177e4SLinus Torvalds #define POINTER_DEBUG
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
681da177e4SLinus Torvalds MODULE_DESCRIPTION("VIA VT82xx audio");
691da177e4SLinus Torvalds MODULE_LICENSE("GPL");
701da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
731da177e4SLinus Torvalds #define SUPPORT_JOYSTICK 1
741da177e4SLinus Torvalds #endif
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
771da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
781da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
791da177e4SLinus Torvalds static long mpu_port[SNDRV_CARDS];
801da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
811da177e4SLinus Torvalds static int joystick[SNDRV_CARDS];
821da177e4SLinus Torvalds #endif
831da177e4SLinus Torvalds static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 48000};
841da177e4SLinus Torvalds static char *ac97_quirk[SNDRV_CARDS];
851da177e4SLinus Torvalds static int dxs_support[SNDRV_CARDS];
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
881da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
891da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
901da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
911da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
921da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable audio part of VIA 82xx bridge.");
931da177e4SLinus Torvalds module_param_array(mpu_port, long, NULL, 0444);
941da177e4SLinus Torvalds MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
951da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
961da177e4SLinus Torvalds module_param_array(joystick, bool, NULL, 0444);
971da177e4SLinus Torvalds MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
981da177e4SLinus Torvalds #endif
991da177e4SLinus Torvalds module_param_array(ac97_clock, int, NULL, 0444);
1001da177e4SLinus Torvalds MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
1011da177e4SLinus Torvalds module_param_array(ac97_quirk, charp, NULL, 0444);
1021da177e4SLinus Torvalds MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
1031da177e4SLinus Torvalds module_param_array(dxs_support, int, NULL, 0444);
1042d7eb7cbSSergey 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)");
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /* pci ids */
1081da177e4SLinus Torvalds #ifndef PCI_DEVICE_ID_VIA_82C686_5
1091da177e4SLinus Torvalds #define PCI_DEVICE_ID_VIA_82C686_5	0x3058
1101da177e4SLinus Torvalds #endif
1111da177e4SLinus Torvalds #ifndef PCI_DEVICE_ID_VIA_8233_5
1121da177e4SLinus Torvalds #define PCI_DEVICE_ID_VIA_8233_5	0x3059
1131da177e4SLinus Torvalds #endif
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* revision numbers for via686 */
1161da177e4SLinus Torvalds #define VIA_REV_686_A		0x10
1171da177e4SLinus Torvalds #define VIA_REV_686_B		0x11
1181da177e4SLinus Torvalds #define VIA_REV_686_C		0x12
1191da177e4SLinus Torvalds #define VIA_REV_686_D		0x13
1201da177e4SLinus Torvalds #define VIA_REV_686_E		0x14
1211da177e4SLinus Torvalds #define VIA_REV_686_H		0x20
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* revision numbers for via8233 */
1241da177e4SLinus Torvalds #define VIA_REV_PRE_8233	0x10	/* not in market */
1251da177e4SLinus Torvalds #define VIA_REV_8233C		0x20	/* 2 rec, 4 pb, 1 multi-pb */
1261da177e4SLinus Torvalds #define VIA_REV_8233		0x30	/* 2 rec, 4 pb, 1 multi-pb, spdif */
1271da177e4SLinus Torvalds #define VIA_REV_8233A		0x40	/* 1 rec, 1 multi-pb, spdf */
1281da177e4SLinus Torvalds #define VIA_REV_8235		0x50	/* 2 rec, 4 pb, 1 multi-pb, spdif */
1291da177e4SLinus Torvalds #define VIA_REV_8237		0x60
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /*
1321da177e4SLinus Torvalds  *  Direct registers
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
1361da177e4SLinus Torvalds #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* common offsets */
1391da177e4SLinus Torvalds #define VIA_REG_OFFSET_STATUS		0x00	/* byte - channel status */
1401da177e4SLinus Torvalds #define   VIA_REG_STAT_ACTIVE		0x80	/* RO */
1411da177e4SLinus Torvalds #define   VIA_REG_STAT_PAUSED		0x40	/* RO */
1421da177e4SLinus Torvalds #define   VIA_REG_STAT_TRIGGER_QUEUED	0x08	/* RO */
1431da177e4SLinus Torvalds #define   VIA_REG_STAT_STOPPED		0x04	/* RWC */
1441da177e4SLinus Torvalds #define   VIA_REG_STAT_EOL		0x02	/* RWC */
1451da177e4SLinus Torvalds #define   VIA_REG_STAT_FLAG		0x01	/* RWC */
1461da177e4SLinus Torvalds #define VIA_REG_OFFSET_CONTROL		0x01	/* byte - channel control */
1471da177e4SLinus Torvalds #define   VIA_REG_CTRL_START		0x80	/* WO */
1481da177e4SLinus Torvalds #define   VIA_REG_CTRL_TERMINATE	0x40	/* WO */
1491da177e4SLinus Torvalds #define   VIA_REG_CTRL_AUTOSTART	0x20
1501da177e4SLinus Torvalds #define   VIA_REG_CTRL_PAUSE		0x08	/* RW */
1511da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT_STOP		0x04
1521da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT_EOL		0x02
1531da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT_FLAG		0x01
1541da177e4SLinus Torvalds #define   VIA_REG_CTRL_RESET		0x01	/* RW - probably reset? undocumented */
1551da177e4SLinus Torvalds #define   VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
1561da177e4SLinus Torvalds #define VIA_REG_OFFSET_TYPE		0x02	/* byte - channel type (686 only) */
1571da177e4SLinus Torvalds #define   VIA_REG_TYPE_AUTOSTART	0x80	/* RW - autostart at EOL */
1581da177e4SLinus Torvalds #define   VIA_REG_TYPE_16BIT		0x20	/* RW */
1591da177e4SLinus Torvalds #define   VIA_REG_TYPE_STEREO		0x10	/* RW */
1601da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_LLINE	0x00
1611da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_LSAMPLE	0x04
1621da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_LESSONE	0x08
1631da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_MASK		0x0c
1641da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_EOL		0x02
1651da177e4SLinus Torvalds #define   VIA_REG_TYPE_INT_FLAG		0x01
1661da177e4SLinus Torvalds #define VIA_REG_OFFSET_TABLE_PTR	0x04	/* dword - channel table pointer */
1671da177e4SLinus Torvalds #define VIA_REG_OFFSET_CURR_PTR		0x04	/* dword - channel current pointer */
1681da177e4SLinus Torvalds #define VIA_REG_OFFSET_STOP_IDX		0x08	/* dword - stop index, channel type, sample rate */
1691da177e4SLinus Torvalds #define   VIA8233_REG_TYPE_16BIT	0x00200000	/* RW */
1701da177e4SLinus Torvalds #define   VIA8233_REG_TYPE_STEREO	0x00100000	/* RW */
1711da177e4SLinus Torvalds #define VIA_REG_OFFSET_CURR_COUNT	0x0c	/* dword - channel current count (24 bit) */
1721da177e4SLinus Torvalds #define VIA_REG_OFFSET_CURR_INDEX	0x0f	/* byte - channel current index (for via8233 only) */
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds #define DEFINE_VIA_REGSET(name,val) \
1751da177e4SLinus Torvalds enum {\
1761da177e4SLinus Torvalds 	VIA_REG_##name##_STATUS		= (val),\
1771da177e4SLinus Torvalds 	VIA_REG_##name##_CONTROL	= (val) + 0x01,\
1781da177e4SLinus Torvalds 	VIA_REG_##name##_TYPE		= (val) + 0x02,\
1791da177e4SLinus Torvalds 	VIA_REG_##name##_TABLE_PTR	= (val) + 0x04,\
1801da177e4SLinus Torvalds 	VIA_REG_##name##_CURR_PTR	= (val) + 0x04,\
1811da177e4SLinus Torvalds 	VIA_REG_##name##_STOP_IDX	= (val) + 0x08,\
1821da177e4SLinus Torvalds 	VIA_REG_##name##_CURR_COUNT	= (val) + 0x0c,\
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds /* playback block */
1861da177e4SLinus Torvalds DEFINE_VIA_REGSET(PLAYBACK, 0x00);
1871da177e4SLinus Torvalds DEFINE_VIA_REGSET(CAPTURE, 0x10);
1881da177e4SLinus Torvalds DEFINE_VIA_REGSET(FM, 0x20);
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /* AC'97 */
1911da177e4SLinus Torvalds #define VIA_REG_AC97			0x80	/* dword */
1921da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_MASK	(3<<30)
1931da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_SHIFT	30
1941da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_PRIMARY	0x00
1951da177e4SLinus Torvalds #define   VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
1961da177e4SLinus Torvalds #define   VIA_REG_AC97_SECONDARY_VALID	(1<<27)
1971da177e4SLinus Torvalds #define   VIA_REG_AC97_PRIMARY_VALID	(1<<25)
1981da177e4SLinus Torvalds #define   VIA_REG_AC97_BUSY		(1<<24)
1991da177e4SLinus Torvalds #define   VIA_REG_AC97_READ		(1<<23)
2001da177e4SLinus Torvalds #define   VIA_REG_AC97_CMD_SHIFT	16
2011da177e4SLinus Torvalds #define   VIA_REG_AC97_CMD_MASK		0x7e
2021da177e4SLinus Torvalds #define   VIA_REG_AC97_DATA_SHIFT	0
2031da177e4SLinus Torvalds #define   VIA_REG_AC97_DATA_MASK	0xffff
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds #define VIA_REG_SGD_SHADOW		0x84	/* dword */
2061da177e4SLinus Torvalds /* via686 */
2071da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_FLAG	(1<<0)
2081da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_FLAG	(1<<1)
2091da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_FLAG	(1<<2)
2101da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_EOL	(1<<4)
2111da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_EOL	(1<<5)
2121da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_EOL	(1<<6)
2131da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_STOP	(1<<8)
2141da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_STOP	(1<<9)
2151da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_STOP	(1<<10)
2161da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_PB_ACTIVE	(1<<12)
2171da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_CP_ACTIVE	(1<<13)
2181da177e4SLinus Torvalds #define   VIA_REG_SGD_STAT_FM_ACTIVE	(1<<14)
2191da177e4SLinus Torvalds /* via8233 */
2201da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_FLAG	(1<<0)
2211da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_EOL	(1<<1)
2221da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_STOP	(1<<2)
2231da177e4SLinus Torvalds #define   VIA8233_REG_SGD_STAT_ACTIVE	(1<<3)
2241da177e4SLinus Torvalds #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
2251da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_SDX	0
2261da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_MULTI	4
2271da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_REC	6
2281da177e4SLinus Torvalds #define   VIA8233_REG_SGD_CHAN_REC1	7
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds #define VIA_REG_GPI_STATUS		0x88
2311da177e4SLinus Torvalds #define VIA_REG_GPI_INTR		0x8c
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /* multi-channel and capture registers for via8233 */
2341da177e4SLinus Torvalds DEFINE_VIA_REGSET(MULTPLAY, 0x40);
2351da177e4SLinus Torvalds DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds /* via8233-specific registers */
2381da177e4SLinus Torvalds #define VIA_REG_OFS_PLAYBACK_VOLUME_L	0x02	/* byte */
2391da177e4SLinus Torvalds #define VIA_REG_OFS_PLAYBACK_VOLUME_R	0x03	/* byte */
2401da177e4SLinus Torvalds #define VIA_REG_OFS_MULTPLAY_FORMAT	0x02	/* byte - format and channels */
2411da177e4SLinus Torvalds #define   VIA_REG_MULTPLAY_FMT_8BIT	0x00
2421da177e4SLinus Torvalds #define   VIA_REG_MULTPLAY_FMT_16BIT	0x80
2431da177e4SLinus Torvalds #define   VIA_REG_MULTPLAY_FMT_CH_MASK	0x70	/* # channels << 4 (valid = 1,2,4,6) */
2441da177e4SLinus Torvalds #define VIA_REG_OFS_CAPTURE_FIFO	0x02	/* byte - bit 6 = fifo  enable */
2451da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_FIFO_ENABLE	0x40
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds #define VIA_DXS_MAX_VOLUME		31	/* max. volume (attenuation) of reg 0x32/33 */
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds #define VIA_REG_CAPTURE_CHANNEL		0x63	/* byte - input select */
2501da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_CHANNEL_MIC	0x4
2511da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_CHANNEL_LINE	0
2521da177e4SLinus Torvalds #define   VIA_REG_CAPTURE_SELECT_CODEC	0x03	/* recording source codec (0 = primary) */
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds #define VIA_TBL_BIT_FLAG	0x40000000
2551da177e4SLinus Torvalds #define VIA_TBL_BIT_EOL		0x80000000
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /* pci space */
2581da177e4SLinus Torvalds #define VIA_ACLINK_STAT		0x40
2591da177e4SLinus Torvalds #define  VIA_ACLINK_C11_READY	0x20
2601da177e4SLinus Torvalds #define  VIA_ACLINK_C10_READY	0x10
2611da177e4SLinus Torvalds #define  VIA_ACLINK_C01_READY	0x04 /* secondary codec ready */
2621da177e4SLinus Torvalds #define  VIA_ACLINK_LOWPOWER	0x02 /* low-power state */
2631da177e4SLinus Torvalds #define  VIA_ACLINK_C00_READY	0x01 /* primary codec ready */
2641da177e4SLinus Torvalds #define VIA_ACLINK_CTRL		0x41
2651da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_ENABLE	0x80 /* 0: disable, 1: enable */
2661da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_RESET	0x40 /* 0: assert, 1: de-assert */
2671da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_SYNC	0x20 /* 0: release SYNC, 1: force SYNC hi */
2681da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_SDO	0x10 /* 0: release SDO, 1: force SDO hi */
2691da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_VRA	0x08 /* 0: disable VRA, 1: enable VRA */
2701da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_PCM	0x04 /* 0: disable PCM, 1: enable PCM */
2711da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_FM	0x02 /* via686 only */
2721da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_SB	0x01 /* via686 only */
2731da177e4SLinus Torvalds #define  VIA_ACLINK_CTRL_INIT	(VIA_ACLINK_CTRL_ENABLE|\
2741da177e4SLinus Torvalds 				 VIA_ACLINK_CTRL_RESET|\
2751da177e4SLinus Torvalds 				 VIA_ACLINK_CTRL_PCM|\
2761da177e4SLinus Torvalds 				 VIA_ACLINK_CTRL_VRA)
2771da177e4SLinus Torvalds #define VIA_FUNC_ENABLE		0x42
2781da177e4SLinus Torvalds #define  VIA_FUNC_MIDI_PNP	0x80 /* FIXME: it's 0x40 in the datasheet! */
2791da177e4SLinus Torvalds #define  VIA_FUNC_MIDI_IRQMASK	0x40 /* FIXME: not documented! */
2801da177e4SLinus Torvalds #define  VIA_FUNC_RX2C_WRITE	0x20
2811da177e4SLinus Torvalds #define  VIA_FUNC_SB_FIFO_EMPTY	0x10
2821da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_GAME	0x08
2831da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_FM	0x04
2841da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_MIDI	0x02
2851da177e4SLinus Torvalds #define  VIA_FUNC_ENABLE_SB	0x01
2861da177e4SLinus Torvalds #define VIA_PNP_CONTROL		0x43
2871da177e4SLinus Torvalds #define VIA_FM_NMI_CTRL		0x48
2881da177e4SLinus Torvalds #define VIA8233_VOLCHG_CTRL	0x48
2891da177e4SLinus Torvalds #define VIA8233_SPDIF_CTRL	0x49
2901da177e4SLinus Torvalds #define  VIA8233_SPDIF_DX3	0x08
2911da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_MASK	0x03
2921da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_1011	0x00
2931da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_34		0x01
2941da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_78		0x02
2951da177e4SLinus Torvalds #define  VIA8233_SPDIF_SLOT_69		0x03
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds /*
2981da177e4SLinus Torvalds  */
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds #define VIA_DXS_AUTO	0
3011da177e4SLinus Torvalds #define VIA_DXS_ENABLE	1
3021da177e4SLinus Torvalds #define VIA_DXS_DISABLE	2
3031da177e4SLinus Torvalds #define VIA_DXS_48K	3
3041da177e4SLinus Torvalds #define VIA_DXS_NO_VRA	4
3052d7eb7cbSSergey Vlasov #define VIA_DXS_SRC	5
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /*
3091da177e4SLinus Torvalds  */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds typedef struct _snd_via82xx via82xx_t;
3121da177e4SLinus Torvalds typedef struct via_dev viadev_t;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds /*
3151da177e4SLinus Torvalds  * pcm stream
3161da177e4SLinus Torvalds  */
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds struct snd_via_sg_table {
3191da177e4SLinus Torvalds 	unsigned int offset;
3201da177e4SLinus Torvalds 	unsigned int size;
3211da177e4SLinus Torvalds } ;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds #define VIA_TABLE_SIZE	255
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds struct via_dev {
3261da177e4SLinus Torvalds 	unsigned int reg_offset;
3271da177e4SLinus Torvalds 	unsigned long port;
3281da177e4SLinus Torvalds 	int direction;	/* playback = 0, capture = 1 */
3291da177e4SLinus Torvalds         snd_pcm_substream_t *substream;
3301da177e4SLinus Torvalds 	int running;
3311da177e4SLinus Torvalds 	unsigned int tbl_entries; /* # descriptors */
3321da177e4SLinus Torvalds 	struct snd_dma_buffer table;
3331da177e4SLinus Torvalds 	struct snd_via_sg_table *idx_table;
3341da177e4SLinus Torvalds 	/* for recovery from the unexpected pointer */
3351da177e4SLinus Torvalds 	unsigned int lastpos;
3361da177e4SLinus Torvalds 	unsigned int fragsize;
3371da177e4SLinus Torvalds 	unsigned int bufsize;
3381da177e4SLinus Torvalds 	unsigned int bufsize2;
3391da177e4SLinus Torvalds };
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
3431da177e4SLinus Torvalds enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds #define VIA_MAX_DEVS	7	/* 4 playback, 1 multi, 2 capture */
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds struct via_rate_lock {
3481da177e4SLinus Torvalds 	spinlock_t lock;
3491da177e4SLinus Torvalds 	int rate;
3501da177e4SLinus Torvalds 	int used;
3511da177e4SLinus Torvalds };
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds struct _snd_via82xx {
3541da177e4SLinus Torvalds 	int irq;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	unsigned long port;
3571da177e4SLinus Torvalds 	struct resource *mpu_res;
3581da177e4SLinus Torvalds 	int chip_type;
3591da177e4SLinus Torvalds 	unsigned char revision;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	unsigned char old_legacy;
3621da177e4SLinus Torvalds 	unsigned char old_legacy_cfg;
3631da177e4SLinus Torvalds #ifdef CONFIG_PM
3641da177e4SLinus Torvalds 	unsigned char legacy_saved;
3651da177e4SLinus Torvalds 	unsigned char legacy_cfg_saved;
3661da177e4SLinus Torvalds 	unsigned char spdif_ctrl_saved;
3671da177e4SLinus Torvalds 	unsigned char capture_src_saved[2];
3681da177e4SLinus Torvalds 	unsigned int mpu_port_saved;
3691da177e4SLinus Torvalds #endif
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	unsigned char playback_volume[2]; /* for VIA8233/C/8235; default = 0 */
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	struct pci_dev *pci;
3761da177e4SLinus Torvalds 	snd_card_t *card;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	unsigned int num_devs;
3791da177e4SLinus Torvalds 	unsigned int playback_devno, multi_devno, capture_devno;
3801da177e4SLinus Torvalds 	viadev_t devs[VIA_MAX_DEVS];
3811da177e4SLinus Torvalds 	struct via_rate_lock rates[2]; /* playback and capture */
3821da177e4SLinus Torvalds 	unsigned int dxs_fixed: 1;	/* DXS channel accepts only 48kHz */
3831da177e4SLinus Torvalds 	unsigned int no_vra: 1;		/* no need to set VRA on DXS channels */
3842d7eb7cbSSergey Vlasov 	unsigned int dxs_src: 1;	/* use full SRC capabilities of DXS */
3851da177e4SLinus Torvalds 	unsigned int spdif_on: 1;	/* only spdif rates work to external DACs */
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	snd_pcm_t *pcms[2];
3881da177e4SLinus Torvalds 	snd_rawmidi_t *rmidi;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	ac97_bus_t *ac97_bus;
3911da177e4SLinus Torvalds 	ac97_t *ac97;
3921da177e4SLinus Torvalds 	unsigned int ac97_clock;
3931da177e4SLinus Torvalds 	unsigned int ac97_secondary;	/* secondary AC'97 codec is present */
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	spinlock_t reg_lock;
3961da177e4SLinus Torvalds 	snd_info_entry_t *proc_entry;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
3991da177e4SLinus Torvalds 	struct gameport *gameport;
4001da177e4SLinus Torvalds #endif
4011da177e4SLinus Torvalds };
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static struct pci_device_id snd_via82xx_ids[] = {
4041da177e4SLinus Torvalds 	{ 0x1106, 0x3058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, },	/* 686A */
4051da177e4SLinus Torvalds 	{ 0x1106, 0x3059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, },	/* VT8233 */
4061da177e4SLinus Torvalds 	{ 0, }
4071da177e4SLinus Torvalds };
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /*
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * allocate and initialize the descriptor buffers
4161da177e4SLinus Torvalds  * periods = number of periods
4171da177e4SLinus Torvalds  * fragsize = period size in bytes
4181da177e4SLinus Torvalds  */
4191da177e4SLinus Torvalds static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
4201da177e4SLinus Torvalds 			   struct pci_dev *pci,
4211da177e4SLinus Torvalds 			   unsigned int periods, unsigned int fragsize)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	unsigned int i, idx, ofs, rest;
4241da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
4251da177e4SLinus Torvalds 	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	if (dev->table.area == NULL) {
4281da177e4SLinus Torvalds 		/* the start of each lists must be aligned to 8 bytes,
4291da177e4SLinus Torvalds 		 * but the kernel pages are much bigger, so we don't care
4301da177e4SLinus Torvalds 		 */
4311da177e4SLinus Torvalds 		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
4321da177e4SLinus Torvalds 					PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
4331da177e4SLinus Torvalds 					&dev->table) < 0)
4341da177e4SLinus Torvalds 			return -ENOMEM;
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 	if (! dev->idx_table) {
4371da177e4SLinus Torvalds 		dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
4381da177e4SLinus Torvalds 		if (! dev->idx_table)
4391da177e4SLinus Torvalds 			return -ENOMEM;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	/* fill the entries */
4431da177e4SLinus Torvalds 	idx = 0;
4441da177e4SLinus Torvalds 	ofs = 0;
4451da177e4SLinus Torvalds 	for (i = 0; i < periods; i++) {
4461da177e4SLinus Torvalds 		rest = fragsize;
4471da177e4SLinus Torvalds 		/* fill descriptors for a period.
4481da177e4SLinus Torvalds 		 * a period can be split to several descriptors if it's
4491da177e4SLinus Torvalds 		 * over page boundary.
4501da177e4SLinus Torvalds 		 */
4511da177e4SLinus Torvalds 		do {
4521da177e4SLinus Torvalds 			unsigned int r;
4531da177e4SLinus Torvalds 			unsigned int flag;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 			if (idx >= VIA_TABLE_SIZE) {
4561da177e4SLinus Torvalds 				snd_printk(KERN_ERR "via82xx: too much table size!\n");
4571da177e4SLinus Torvalds 				return -EINVAL;
4581da177e4SLinus Torvalds 			}
4591da177e4SLinus Torvalds 			((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
4601da177e4SLinus Torvalds 			r = PAGE_SIZE - (ofs % PAGE_SIZE);
4611da177e4SLinus Torvalds 			if (rest < r)
4621da177e4SLinus Torvalds 				r = rest;
4631da177e4SLinus Torvalds 			rest -= r;
4641da177e4SLinus Torvalds 			if (! rest) {
4651da177e4SLinus Torvalds 				if (i == periods - 1)
4661da177e4SLinus Torvalds 					flag = VIA_TBL_BIT_EOL; /* buffer boundary */
4671da177e4SLinus Torvalds 				else
4681da177e4SLinus Torvalds 					flag = VIA_TBL_BIT_FLAG; /* period boundary */
4691da177e4SLinus Torvalds 			} else
4701da177e4SLinus Torvalds 				flag = 0; /* period continues to the next */
4711da177e4SLinus Torvalds 			// printk("via: tbl %d: at %d  size %d (rest %d)\n", idx, ofs, r, rest);
4721da177e4SLinus Torvalds 			((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
4731da177e4SLinus Torvalds 			dev->idx_table[idx].offset = ofs;
4741da177e4SLinus Torvalds 			dev->idx_table[idx].size = r;
4751da177e4SLinus Torvalds 			ofs += r;
4761da177e4SLinus Torvalds 			idx++;
4771da177e4SLinus Torvalds 		} while (rest > 0);
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 	dev->tbl_entries = idx;
4801da177e4SLinus Torvalds 	dev->bufsize = periods * fragsize;
4811da177e4SLinus Torvalds 	dev->bufsize2 = dev->bufsize / 2;
4821da177e4SLinus Torvalds 	dev->fragsize = fragsize;
4831da177e4SLinus Torvalds 	return 0;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
4881da177e4SLinus Torvalds 			   struct pci_dev *pci)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	if (dev->table.area) {
4911da177e4SLinus Torvalds 		snd_dma_free_pages(&dev->table);
4921da177e4SLinus Torvalds 		dev->table.area = NULL;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 	if (dev->idx_table) {
4951da177e4SLinus Torvalds 		kfree(dev->idx_table);
4961da177e4SLinus Torvalds 		dev->idx_table = NULL;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 	return 0;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /*
5021da177e4SLinus Torvalds  *  Basic I/O
5031da177e4SLinus Torvalds  */
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	return inl(VIAREG(chip, AC97));
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	outl(val, VIAREG(chip, AC97));
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	unsigned int timeout = 1000;	/* 1ms */
5181da177e4SLinus Torvalds 	unsigned int val;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	while (timeout-- > 0) {
5211da177e4SLinus Torvalds 		udelay(1);
5221da177e4SLinus Torvalds 		if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
5231da177e4SLinus Torvalds 			return val & 0xffff;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 	snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
5261da177e4SLinus Torvalds 	return -EIO;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	unsigned int timeout = 1000;	/* 1ms */
5321da177e4SLinus Torvalds 	unsigned int val, val1;
5331da177e4SLinus Torvalds 	unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
5341da177e4SLinus Torvalds 					 VIA_REG_AC97_SECONDARY_VALID;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	while (timeout-- > 0) {
5371da177e4SLinus Torvalds 		val = snd_via82xx_codec_xread(chip);
5381da177e4SLinus Torvalds 		val1 = val & (VIA_REG_AC97_BUSY | stat);
5391da177e4SLinus Torvalds 		if (val1 == stat)
5401da177e4SLinus Torvalds 			return val & 0xffff;
5411da177e4SLinus Torvalds 		udelay(1);
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 	return -EIO;
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds static void snd_via82xx_codec_wait(ac97_t *ac97)
5471da177e4SLinus Torvalds {
5481da177e4SLinus Torvalds 	via82xx_t *chip = ac97->private_data;
5491da177e4SLinus Torvalds 	int err;
5501da177e4SLinus Torvalds 	err = snd_via82xx_codec_ready(chip, ac97->num);
5511da177e4SLinus Torvalds 	/* here we need to wait fairly for long time.. */
5521da177e4SLinus Torvalds 	set_current_state(TASK_UNINTERRUPTIBLE);
5531da177e4SLinus Torvalds 	schedule_timeout(HZ/2);
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds static void snd_via82xx_codec_write(ac97_t *ac97,
5571da177e4SLinus Torvalds 				    unsigned short reg,
5581da177e4SLinus Torvalds 				    unsigned short val)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	via82xx_t *chip = ac97->private_data;
5611da177e4SLinus Torvalds 	unsigned int xval;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
5641da177e4SLinus Torvalds 	xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
5651da177e4SLinus Torvalds 	xval |= reg << VIA_REG_AC97_CMD_SHIFT;
5661da177e4SLinus Torvalds 	xval |= val << VIA_REG_AC97_DATA_SHIFT;
5671da177e4SLinus Torvalds 	snd_via82xx_codec_xwrite(chip, xval);
5681da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, ac97->num);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	via82xx_t *chip = ac97->private_data;
5741da177e4SLinus Torvalds 	unsigned int xval, val = 0xffff;
5751da177e4SLinus Torvalds 	int again = 0;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
5781da177e4SLinus Torvalds 	xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
5791da177e4SLinus Torvalds 	xval |= VIA_REG_AC97_READ;
5801da177e4SLinus Torvalds 	xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
5811da177e4SLinus Torvalds       	while (1) {
5821da177e4SLinus Torvalds       		if (again++ > 3) {
5831da177e4SLinus Torvalds 			snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
5841da177e4SLinus Torvalds 		      	return 0xffff;
5851da177e4SLinus Torvalds 		}
5861da177e4SLinus Torvalds 		snd_via82xx_codec_xwrite(chip, xval);
5871da177e4SLinus Torvalds 		udelay (20);
5881da177e4SLinus Torvalds 		if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
5891da177e4SLinus Torvalds 			udelay(25);
5901da177e4SLinus Torvalds 			val = snd_via82xx_codec_xread(chip);
5911da177e4SLinus Torvalds 			break;
5921da177e4SLinus Torvalds 		}
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 	return val & 0xffff;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
6001da177e4SLinus Torvalds 	     VIADEV_REG(viadev, OFFSET_CONTROL));
6011da177e4SLinus Torvalds 	inb(VIADEV_REG(viadev, OFFSET_CONTROL));
6021da177e4SLinus Torvalds 	udelay(50);
6031da177e4SLinus Torvalds 	/* disable interrupts */
6041da177e4SLinus Torvalds 	outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
6051da177e4SLinus Torvalds 	/* clear interrupts */
6061da177e4SLinus Torvalds 	outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
6071da177e4SLinus Torvalds 	outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
6081da177e4SLinus Torvalds 	// outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
6091da177e4SLinus Torvalds 	viadev->lastpos = 0;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds /*
6141da177e4SLinus Torvalds  *  Interrupt handler
6151da177e4SLinus Torvalds  */
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	via82xx_t *chip = dev_id;
6201da177e4SLinus Torvalds 	unsigned int status;
6211da177e4SLinus Torvalds 	unsigned int i;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	status = inl(VIAREG(chip, SGD_SHADOW));
6241da177e4SLinus Torvalds 	if (! (status & chip->intr_mask)) {
6251da177e4SLinus Torvalds 		if (chip->rmidi)
6261da177e4SLinus Torvalds 			/* check mpu401 interrupt */
6271da177e4SLinus Torvalds 			return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
6281da177e4SLinus Torvalds 		return IRQ_NONE;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	/* check status for each stream */
6321da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
6331da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++) {
6341da177e4SLinus Torvalds 		viadev_t *viadev = &chip->devs[i];
6351da177e4SLinus Torvalds 		unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
6361da177e4SLinus Torvalds 		c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
6371da177e4SLinus Torvalds 		if (! c_status)
6381da177e4SLinus Torvalds 			continue;
6391da177e4SLinus Torvalds 		if (viadev->substream && viadev->running) {
6401da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
6411da177e4SLinus Torvalds 			snd_pcm_period_elapsed(viadev->substream);
6421da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
6431da177e4SLinus Torvalds 		}
6441da177e4SLinus Torvalds 		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
6471da177e4SLinus Torvalds 	return IRQ_HANDLED;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds /*
6511da177e4SLinus Torvalds  *  PCM callbacks
6521da177e4SLinus Torvalds  */
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds /*
6551da177e4SLinus Torvalds  * trigger callback
6561da177e4SLinus Torvalds  */
6571da177e4SLinus Torvalds static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
6581da177e4SLinus Torvalds {
6591da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
6601da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
6611da177e4SLinus Torvalds 	unsigned char val;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686)
6641da177e4SLinus Torvalds 		val = VIA_REG_CTRL_INT;
6651da177e4SLinus Torvalds 	else
6661da177e4SLinus Torvalds 		val = 0;
6671da177e4SLinus Torvalds 	switch (cmd) {
6681da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
6691da177e4SLinus Torvalds 		val |= VIA_REG_CTRL_START;
6701da177e4SLinus Torvalds 		viadev->running = 1;
6711da177e4SLinus Torvalds 		break;
6721da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
6731da177e4SLinus Torvalds 		val = VIA_REG_CTRL_TERMINATE;
6741da177e4SLinus Torvalds 		viadev->running = 0;
6751da177e4SLinus Torvalds 		break;
6761da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
6771da177e4SLinus Torvalds 		val |= VIA_REG_CTRL_PAUSE;
6781da177e4SLinus Torvalds 		viadev->running = 0;
6791da177e4SLinus Torvalds 		break;
6801da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
6811da177e4SLinus Torvalds 		viadev->running = 1;
6821da177e4SLinus Torvalds 		break;
6831da177e4SLinus Torvalds 	default:
6841da177e4SLinus Torvalds 		return -EINVAL;
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 	outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
6871da177e4SLinus Torvalds 	if (cmd == SNDRV_PCM_TRIGGER_STOP)
6881da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, viadev);
6891da177e4SLinus Torvalds 	return 0;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds /*
6941da177e4SLinus Torvalds  * pointer callbacks
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds /*
6981da177e4SLinus Torvalds  * calculate the linear position at the given sg-buffer index and the rest count
6991da177e4SLinus Torvalds  */
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds #define check_invalid_pos(viadev,pos) \
7021da177e4SLinus Torvalds 	((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	unsigned int size, base, res;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	size = viadev->idx_table[idx].size;
7091da177e4SLinus Torvalds 	base = viadev->idx_table[idx].offset;
7101da177e4SLinus Torvalds 	res = base + size - count;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* check the validity of the calculated position */
7131da177e4SLinus Torvalds 	if (size < count) {
7141da177e4SLinus Torvalds 		snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n", (int)size, (int)count);
7151da177e4SLinus Torvalds 		res = viadev->lastpos;
7161da177e4SLinus Torvalds 	} else {
7171da177e4SLinus Torvalds 		if (! count) {
7181da177e4SLinus Torvalds 			/* Some mobos report count = 0 on the DMA boundary,
7191da177e4SLinus Torvalds 			 * i.e. count = size indeed.
7201da177e4SLinus Torvalds 			 * Let's check whether this step is above the expected size.
7211da177e4SLinus Torvalds 			 */
7221da177e4SLinus Torvalds 			int delta = res - viadev->lastpos;
7231da177e4SLinus Torvalds 			if (delta < 0)
7241da177e4SLinus Torvalds 				delta += viadev->bufsize;
7251da177e4SLinus Torvalds 			if ((unsigned int)delta > viadev->fragsize)
7261da177e4SLinus Torvalds 				res = base;
7271da177e4SLinus Torvalds 		}
7281da177e4SLinus Torvalds 		if (check_invalid_pos(viadev, res)) {
7291da177e4SLinus Torvalds #ifdef POINTER_DEBUG
7301da177e4SLinus Torvalds 			printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos, viadev->bufsize2, viadev->idx_table[idx].offset, viadev->idx_table[idx].size, count);
7311da177e4SLinus Torvalds #endif
7321da177e4SLinus Torvalds 			/* count register returns full size when end of buffer is reached */
7331da177e4SLinus Torvalds 			res = base + size;
7341da177e4SLinus Torvalds 			if (check_invalid_pos(viadev, res)) {
7351da177e4SLinus Torvalds 				snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
7361da177e4SLinus Torvalds 				res = viadev->lastpos;
7371da177e4SLinus Torvalds 			}
7381da177e4SLinus Torvalds 		}
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 	viadev->lastpos = res; /* remember the last position */
7411da177e4SLinus Torvalds 	if (res >= viadev->bufsize)
7421da177e4SLinus Torvalds 		res -= viadev->bufsize;
7431da177e4SLinus Torvalds 	return res;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /*
7471da177e4SLinus Torvalds  * get the current pointer on via686
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
7521da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
7531da177e4SLinus Torvalds 	unsigned int idx, ptr, count, res;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	snd_assert(viadev->tbl_entries, return 0);
7561da177e4SLinus Torvalds 	if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
7571da177e4SLinus Torvalds 		return 0;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
7601da177e4SLinus Torvalds 	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
7611da177e4SLinus Torvalds 	/* The via686a does not have the current index register,
7621da177e4SLinus Torvalds 	 * so we need to calculate the index from CURR_PTR.
7631da177e4SLinus Torvalds 	 */
7641da177e4SLinus Torvalds 	ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
7651da177e4SLinus Torvalds 	if (ptr <= (unsigned int)viadev->table.addr)
7661da177e4SLinus Torvalds 		idx = 0;
7671da177e4SLinus Torvalds 	else /* CURR_PTR holds the address + 8 */
7681da177e4SLinus Torvalds 		idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
7691da177e4SLinus Torvalds 	res = calc_linear_pos(viadev, idx, count);
7701da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, res);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /*
7761da177e4SLinus Torvalds  * get the current pointer on via823x
7771da177e4SLinus Torvalds  */
7781da177e4SLinus Torvalds static snd_pcm_uframes_t snd_via8233_pcm_pointer(snd_pcm_substream_t *substream)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
7811da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
7821da177e4SLinus Torvalds 	unsigned int idx, count, res;
7831da177e4SLinus Torvalds 	int timeout = 5000;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	snd_assert(viadev->tbl_entries, return 0);
7861da177e4SLinus Torvalds 	if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
7871da177e4SLinus Torvalds 		return 0;
7881da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
7891da177e4SLinus Torvalds 	do {
7901da177e4SLinus Torvalds 		count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
7911da177e4SLinus Torvalds 		/* some mobos read 0 count */
7921da177e4SLinus Torvalds 		if ((count & 0xffffff) || ! viadev->running)
7931da177e4SLinus Torvalds 			break;
7941da177e4SLinus Torvalds 	} while (--timeout);
7951da177e4SLinus Torvalds 	if (! timeout)
7961da177e4SLinus Torvalds 		snd_printd(KERN_ERR "zero position is read\n");
7971da177e4SLinus Torvalds 	idx = count >> 24;
7981da177e4SLinus Torvalds 	if (idx >= viadev->tbl_entries) {
7991da177e4SLinus Torvalds #ifdef POINTER_DEBUG
8001da177e4SLinus Torvalds 		printk("fail: invalid idx = %i/%i\n", idx, viadev->tbl_entries);
8011da177e4SLinus Torvalds #endif
8021da177e4SLinus Torvalds 		res = viadev->lastpos;
8031da177e4SLinus Torvalds 	} else {
8041da177e4SLinus Torvalds 		count &= 0xffffff;
8051da177e4SLinus Torvalds 		res = calc_linear_pos(viadev, idx, count);
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, res);
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds /*
8141da177e4SLinus Torvalds  * hw_params callback:
8151da177e4SLinus Torvalds  * allocate the buffer and build up the buffer description table
8161da177e4SLinus Torvalds  */
8171da177e4SLinus Torvalds static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
8181da177e4SLinus Torvalds 				 snd_pcm_hw_params_t * hw_params)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
8211da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
8221da177e4SLinus Torvalds 	int err;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
8251da177e4SLinus Torvalds 	if (err < 0)
8261da177e4SLinus Torvalds 		return err;
8271da177e4SLinus Torvalds 	err = build_via_table(viadev, substream, chip->pci,
8281da177e4SLinus Torvalds 			      params_periods(hw_params),
8291da177e4SLinus Torvalds 			      params_period_bytes(hw_params));
8301da177e4SLinus Torvalds 	if (err < 0)
8311da177e4SLinus Torvalds 		return err;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	return 0;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds /*
8371da177e4SLinus Torvalds  * hw_free callback:
8381da177e4SLinus Torvalds  * clean up the buffer description table and release the buffer
8391da177e4SLinus Torvalds  */
8401da177e4SLinus Torvalds static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
8431da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	clean_via_table(viadev, substream, chip->pci);
8461da177e4SLinus Torvalds 	snd_pcm_lib_free_pages(substream);
8471da177e4SLinus Torvalds 	return 0;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds /*
8521da177e4SLinus Torvalds  * set up the table pointer
8531da177e4SLinus Torvalds  */
8541da177e4SLinus Torvalds static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
8571da177e4SLinus Torvalds 	outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
8581da177e4SLinus Torvalds 	udelay(20);
8591da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds /*
8631da177e4SLinus Torvalds  * prepare callback for playback and capture on via686
8641da177e4SLinus Torvalds  */
8651da177e4SLinus Torvalds static void via686_setup_format(via82xx_t *chip, viadev_t *viadev, snd_pcm_runtime_t *runtime)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
8681da177e4SLinus Torvalds 	/* this must be set after channel_reset */
8691da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
8701da177e4SLinus Torvalds 	outb(VIA_REG_TYPE_AUTOSTART |
8711da177e4SLinus Torvalds 	     (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
8721da177e4SLinus Torvalds 	     (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
8731da177e4SLinus Torvalds 	     ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
8741da177e4SLinus Torvalds 	     VIA_REG_TYPE_INT_EOL |
8751da177e4SLinus Torvalds 	     VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds static int snd_via686_playback_prepare(snd_pcm_substream_t *substream)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
8811da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
8821da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
8851da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
8861da177e4SLinus Torvalds 	via686_setup_format(chip, viadev, runtime);
8871da177e4SLinus Torvalds 	return 0;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds static int snd_via686_capture_prepare(snd_pcm_substream_t *substream)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
8931da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
8941da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
8971da177e4SLinus Torvalds 	via686_setup_format(chip, viadev, runtime);
8981da177e4SLinus Torvalds 	return 0;
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds /*
9021da177e4SLinus Torvalds  * lock the current rate
9031da177e4SLinus Torvalds  */
9041da177e4SLinus Torvalds static int via_lock_rate(struct via_rate_lock *rec, int rate)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	int changed = 0;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	spin_lock_irq(&rec->lock);
9091da177e4SLinus Torvalds 	if (rec->rate != rate) {
9101da177e4SLinus Torvalds 		if (rec->rate && rec->used > 1) /* already set */
9111da177e4SLinus Torvalds 			changed = -EINVAL;
9121da177e4SLinus Torvalds 		else {
9131da177e4SLinus Torvalds 			rec->rate = rate;
9141da177e4SLinus Torvalds 			changed = 1;
9151da177e4SLinus Torvalds 		}
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 	spin_unlock_irq(&rec->lock);
9181da177e4SLinus Torvalds 	return changed;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds /*
9221da177e4SLinus Torvalds  * prepare callback for DSX playback on via823x
9231da177e4SLinus Torvalds  */
9241da177e4SLinus Torvalds static int snd_via8233_playback_prepare(snd_pcm_substream_t *substream)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
9271da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
9281da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
9292d7eb7cbSSergey Vlasov 	int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
9301da177e4SLinus Torvalds 	int rate_changed;
9311da177e4SLinus Torvalds 	u32 rbits;
9321da177e4SLinus Torvalds 
9332d7eb7cbSSergey Vlasov 	if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
9341da177e4SLinus Torvalds 		return rate_changed;
9351da177e4SLinus Torvalds 	if (rate_changed) {
9361da177e4SLinus Torvalds 		snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
9371da177e4SLinus Torvalds 				  chip->no_vra ? 48000 : runtime->rate);
93801ef355fSTakashi Iwai 		snd_ac97_set_rate(chip->ac97, AC97_SPDIF,
93901ef355fSTakashi Iwai 				  chip->no_vra ? 48000 : runtime->rate);
9401da177e4SLinus Torvalds 	}
9411da177e4SLinus Torvalds 	if (runtime->rate == 48000)
9421da177e4SLinus Torvalds 		rbits = 0xfffff;
9431da177e4SLinus Torvalds 	else
9441da177e4SLinus Torvalds 		rbits = (0x100000 / 48000) * runtime->rate + ((0x100000 % 48000) * runtime->rate) / 48000;
9451da177e4SLinus Torvalds 	snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
9461da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
9471da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
9481da177e4SLinus Torvalds 	outb(chip->playback_volume[0], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
9491da177e4SLinus Torvalds 	outb(chip->playback_volume[1], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
9501da177e4SLinus Torvalds 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
9511da177e4SLinus Torvalds 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
9521da177e4SLinus Torvalds 	     rbits | /* rate */
9531da177e4SLinus Torvalds 	     0xff000000,    /* STOP index is never reached */
9541da177e4SLinus Torvalds 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
9551da177e4SLinus Torvalds 	udelay(20);
9561da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
9571da177e4SLinus Torvalds 	return 0;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds /*
9611da177e4SLinus Torvalds  * prepare callback for multi-channel playback on via823x
9621da177e4SLinus Torvalds  */
9631da177e4SLinus Torvalds static int snd_via8233_multi_prepare(snd_pcm_substream_t *substream)
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
9661da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
9671da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
9681da177e4SLinus Torvalds 	unsigned int slots;
9691da177e4SLinus Torvalds 	int fmt;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
9721da177e4SLinus Torvalds 		return -EINVAL;
9731da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
9741da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
9751da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
9761da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
9771da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
9781da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ? VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
9811da177e4SLinus Torvalds 	fmt |= runtime->channels << 4;
9821da177e4SLinus Torvalds 	outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
9831da177e4SLinus Torvalds #if 0
9841da177e4SLinus Torvalds 	if (chip->revision == VIA_REV_8233A)
9851da177e4SLinus Torvalds 		slots = 0;
9861da177e4SLinus Torvalds 	else
9871da177e4SLinus Torvalds #endif
9881da177e4SLinus Torvalds 	{
9891da177e4SLinus Torvalds 		/* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
9901da177e4SLinus Torvalds 		/* corresponding to FL, FR, RL, RR, C, LFE ?? */
9911da177e4SLinus Torvalds 		switch (runtime->channels) {
9921da177e4SLinus Torvalds 		case 1: slots = (1<<0) | (1<<4); break;
9931da177e4SLinus Torvalds 		case 2: slots = (1<<0) | (2<<4); break;
9941da177e4SLinus Torvalds 		case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
9951da177e4SLinus Torvalds 		case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
9961da177e4SLinus Torvalds 		case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
9971da177e4SLinus Torvalds 		case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
9981da177e4SLinus Torvalds 		default: slots = 0; break;
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 	/* STOP index is never reached */
10021da177e4SLinus Torvalds 	outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
10031da177e4SLinus Torvalds 	udelay(20);
10041da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
10051da177e4SLinus Torvalds 	return 0;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds /*
10091da177e4SLinus Torvalds  * prepare callback for capture on via823x
10101da177e4SLinus Torvalds  */
10111da177e4SLinus Torvalds static int snd_via8233_capture_prepare(snd_pcm_substream_t *substream)
10121da177e4SLinus Torvalds {
10131da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
10141da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
10151da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
10181da177e4SLinus Torvalds 		return -EINVAL;
10191da177e4SLinus Torvalds 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
10201da177e4SLinus Torvalds 	snd_via82xx_channel_reset(chip, viadev);
10211da177e4SLinus Torvalds 	snd_via82xx_set_table_ptr(chip, viadev);
10221da177e4SLinus Torvalds 	outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
10231da177e4SLinus Torvalds 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
10241da177e4SLinus Torvalds 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
10251da177e4SLinus Torvalds 	     0xff000000,    /* STOP index is never reached */
10261da177e4SLinus Torvalds 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
10271da177e4SLinus Torvalds 	udelay(20);
10281da177e4SLinus Torvalds 	snd_via82xx_codec_ready(chip, 0);
10291da177e4SLinus Torvalds 	return 0;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds /*
10341da177e4SLinus Torvalds  * pcm hardware definition, identical for both playback and capture
10351da177e4SLinus Torvalds  */
10361da177e4SLinus Torvalds static snd_pcm_hardware_t snd_via82xx_hw =
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
10391da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
10401da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
10411da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME |
10421da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE),
10431da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
10441da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_48000,
10451da177e4SLinus Torvalds 	.rate_min =		48000,
10461da177e4SLinus Torvalds 	.rate_max =		48000,
10471da177e4SLinus Torvalds 	.channels_min =		1,
10481da177e4SLinus Torvalds 	.channels_max =		2,
10491da177e4SLinus Torvalds 	.buffer_bytes_max =	128 * 1024,
10501da177e4SLinus Torvalds 	.period_bytes_min =	32,
10511da177e4SLinus Torvalds 	.period_bytes_max =	128 * 1024,
10521da177e4SLinus Torvalds 	.periods_min =		2,
10531da177e4SLinus Torvalds 	.periods_max =		VIA_TABLE_SIZE / 2,
10541da177e4SLinus Torvalds 	.fifo_size =		0,
10551da177e4SLinus Torvalds };
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /*
10591da177e4SLinus Torvalds  * open callback skeleton
10601da177e4SLinus Torvalds  */
10611da177e4SLinus Torvalds static int snd_via82xx_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
10621da177e4SLinus Torvalds {
10631da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
10641da177e4SLinus Torvalds 	int err;
10651da177e4SLinus Torvalds 	struct via_rate_lock *ratep;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	runtime->hw = snd_via82xx_hw;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	/* set the hw rate condition */
10701da177e4SLinus Torvalds 	ratep = &chip->rates[viadev->direction];
10711da177e4SLinus Torvalds 	spin_lock_irq(&ratep->lock);
10721da177e4SLinus Torvalds 	ratep->used++;
10731da177e4SLinus Torvalds 	if (chip->spdif_on && viadev->reg_offset == 0x30) {
10741da177e4SLinus Torvalds 		/* DXS#3 and spdif is on */
10751da177e4SLinus Torvalds 		runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
10761da177e4SLinus Torvalds 		snd_pcm_limit_hw_rates(runtime);
10771da177e4SLinus Torvalds 	} else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
10781da177e4SLinus Torvalds 		/* fixed DXS playback rate */
10791da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_48000;
10801da177e4SLinus Torvalds 		runtime->hw.rate_min = runtime->hw.rate_max = 48000;
10812d7eb7cbSSergey Vlasov 	} else if (chip->dxs_src && viadev->reg_offset < 0x40) {
10822d7eb7cbSSergey Vlasov 		/* use full SRC capabilities of DXS */
10832d7eb7cbSSergey Vlasov 		runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
10842d7eb7cbSSergey Vlasov 				     SNDRV_PCM_RATE_8000_48000);
10852d7eb7cbSSergey Vlasov 		runtime->hw.rate_min = 8000;
10862d7eb7cbSSergey Vlasov 		runtime->hw.rate_max = 48000;
10871da177e4SLinus Torvalds 	} else if (! ratep->rate) {
10881da177e4SLinus Torvalds 		int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
10891da177e4SLinus Torvalds 		runtime->hw.rates = chip->ac97->rates[idx];
10901da177e4SLinus Torvalds 		snd_pcm_limit_hw_rates(runtime);
10911da177e4SLinus Torvalds 	} else {
10921da177e4SLinus Torvalds 		/* a fixed rate */
10931da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
10941da177e4SLinus Torvalds 		runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 	spin_unlock_irq(&ratep->lock);
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	/* we may remove following constaint when we modify table entries
10991da177e4SLinus Torvalds 	   in interrupt */
11001da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
11011da177e4SLinus Torvalds 		return err;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	runtime->private_data = viadev;
11041da177e4SLinus Torvalds 	viadev->substream = substream;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	return 0;
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds /*
11111da177e4SLinus Torvalds  * open callback for playback on via686 and via823x DSX
11121da177e4SLinus Torvalds  */
11131da177e4SLinus Torvalds static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
11161da177e4SLinus Torvalds 	viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
11171da177e4SLinus Torvalds 	int err;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
11201da177e4SLinus Torvalds 		return err;
11211da177e4SLinus Torvalds 	return 0;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds /*
11251da177e4SLinus Torvalds  * open callback for playback on via823x multi-channel
11261da177e4SLinus Torvalds  */
11271da177e4SLinus Torvalds static int snd_via8233_multi_open(snd_pcm_substream_t * substream)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
11301da177e4SLinus Torvalds 	viadev_t *viadev = &chip->devs[chip->multi_devno];
11311da177e4SLinus Torvalds 	int err;
11321da177e4SLinus Torvalds 	/* channels constraint for VIA8233A
11331da177e4SLinus Torvalds 	 * 3 and 5 channels are not supported
11341da177e4SLinus Torvalds 	 */
11351da177e4SLinus Torvalds 	static unsigned int channels[] = {
11361da177e4SLinus Torvalds 		1, 2, 4, 6
11371da177e4SLinus Torvalds 	};
11381da177e4SLinus Torvalds 	static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
11391da177e4SLinus Torvalds 		.count = ARRAY_SIZE(channels),
11401da177e4SLinus Torvalds 		.list = channels,
11411da177e4SLinus Torvalds 		.mask = 0,
11421da177e4SLinus Torvalds 	};
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
11451da177e4SLinus Torvalds 		return err;
11461da177e4SLinus Torvalds 	substream->runtime->hw.channels_max = 6;
11471da177e4SLinus Torvalds 	if (chip->revision == VIA_REV_8233A)
11481da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
11491da177e4SLinus Torvalds 	return 0;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds /*
11531da177e4SLinus Torvalds  * open callback for capture on via686 and via823x
11541da177e4SLinus Torvalds  */
11551da177e4SLinus Torvalds static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
11581da177e4SLinus Torvalds 	viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	return snd_via82xx_pcm_open(chip, viadev, substream);
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds /*
11641da177e4SLinus Torvalds  * close callback
11651da177e4SLinus Torvalds  */
11661da177e4SLinus Torvalds static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	via82xx_t *chip = snd_pcm_substream_chip(substream);
11691da177e4SLinus Torvalds 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
11701da177e4SLinus Torvalds 	struct via_rate_lock *ratep;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/* release the rate lock */
11731da177e4SLinus Torvalds 	ratep = &chip->rates[viadev->direction];
11741da177e4SLinus Torvalds 	spin_lock_irq(&ratep->lock);
11751da177e4SLinus Torvalds 	ratep->used--;
11761da177e4SLinus Torvalds 	if (! ratep->used)
11771da177e4SLinus Torvalds 		ratep->rate = 0;
11781da177e4SLinus Torvalds 	spin_unlock_irq(&ratep->lock);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	viadev->substream = NULL;
11811da177e4SLinus Torvalds 	return 0;
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds /* via686 playback callbacks */
11861da177e4SLinus Torvalds static snd_pcm_ops_t snd_via686_playback_ops = {
11871da177e4SLinus Torvalds 	.open =		snd_via82xx_playback_open,
11881da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
11891da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
11901da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
11911da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
11921da177e4SLinus Torvalds 	.prepare =	snd_via686_playback_prepare,
11931da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
11941da177e4SLinus Torvalds 	.pointer =	snd_via686_pcm_pointer,
11951da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
11961da177e4SLinus Torvalds };
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds /* via686 capture callbacks */
11991da177e4SLinus Torvalds static snd_pcm_ops_t snd_via686_capture_ops = {
12001da177e4SLinus Torvalds 	.open =		snd_via82xx_capture_open,
12011da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
12021da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
12031da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
12041da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
12051da177e4SLinus Torvalds 	.prepare =	snd_via686_capture_prepare,
12061da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
12071da177e4SLinus Torvalds 	.pointer =	snd_via686_pcm_pointer,
12081da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
12091da177e4SLinus Torvalds };
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds /* via823x DSX playback callbacks */
12121da177e4SLinus Torvalds static snd_pcm_ops_t snd_via8233_playback_ops = {
12131da177e4SLinus Torvalds 	.open =		snd_via82xx_playback_open,
12141da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
12151da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
12161da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
12171da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
12181da177e4SLinus Torvalds 	.prepare =	snd_via8233_playback_prepare,
12191da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
12201da177e4SLinus Torvalds 	.pointer =	snd_via8233_pcm_pointer,
12211da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
12221da177e4SLinus Torvalds };
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds /* via823x multi-channel playback callbacks */
12251da177e4SLinus Torvalds static snd_pcm_ops_t snd_via8233_multi_ops = {
12261da177e4SLinus Torvalds 	.open =		snd_via8233_multi_open,
12271da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
12281da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
12291da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
12301da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
12311da177e4SLinus Torvalds 	.prepare =	snd_via8233_multi_prepare,
12321da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
12331da177e4SLinus Torvalds 	.pointer =	snd_via8233_pcm_pointer,
12341da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
12351da177e4SLinus Torvalds };
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds /* via823x capture callbacks */
12381da177e4SLinus Torvalds static snd_pcm_ops_t snd_via8233_capture_ops = {
12391da177e4SLinus Torvalds 	.open =		snd_via82xx_capture_open,
12401da177e4SLinus Torvalds 	.close =	snd_via82xx_pcm_close,
12411da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
12421da177e4SLinus Torvalds 	.hw_params =	snd_via82xx_hw_params,
12431da177e4SLinus Torvalds 	.hw_free =	snd_via82xx_hw_free,
12441da177e4SLinus Torvalds 	.prepare =	snd_via8233_capture_prepare,
12451da177e4SLinus Torvalds 	.trigger =	snd_via82xx_pcm_trigger,
12461da177e4SLinus Torvalds 	.pointer =	snd_via8233_pcm_pointer,
12471da177e4SLinus Torvalds 	.page =		snd_pcm_sgbuf_ops_page,
12481da177e4SLinus Torvalds };
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	chip->devs[idx].reg_offset = reg_offset;
12541da177e4SLinus Torvalds 	chip->devs[idx].direction = direction;
12551da177e4SLinus Torvalds 	chip->devs[idx].port = chip->port + reg_offset;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds /*
12591da177e4SLinus Torvalds  * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
12601da177e4SLinus Torvalds  */
12611da177e4SLinus Torvalds static int __devinit snd_via8233_pcm_new(via82xx_t *chip)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds 	snd_pcm_t *pcm;
12641da177e4SLinus Torvalds 	int i, err;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	chip->playback_devno = 0;	/* x 4 */
12671da177e4SLinus Torvalds 	chip->multi_devno = 4;		/* x 1 */
12681da177e4SLinus Torvalds 	chip->capture_devno = 5;	/* x 2 */
12691da177e4SLinus Torvalds 	chip->num_devs = 7;
12701da177e4SLinus Torvalds 	chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	/* PCM #0:  4 DSX playbacks and 1 capture */
12731da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
12741da177e4SLinus Torvalds 	if (err < 0)
12751da177e4SLinus Torvalds 		return err;
12761da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
12771da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
12781da177e4SLinus Torvalds 	pcm->private_data = chip;
12791da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
12801da177e4SLinus Torvalds 	chip->pcms[0] = pcm;
12811da177e4SLinus Torvalds 	/* set up playbacks */
12821da177e4SLinus Torvalds 	for (i = 0; i < 4; i++)
12831da177e4SLinus Torvalds 		init_viadev(chip, i, 0x10 * i, 0);
12841da177e4SLinus Torvalds 	/* capture */
12851da177e4SLinus Torvalds 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
12881da177e4SLinus Torvalds 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
12891da177e4SLinus Torvalds 		return err;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	/* PCM #1:  multi-channel playback and 2nd capture */
12921da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
12931da177e4SLinus Torvalds 	if (err < 0)
12941da177e4SLinus Torvalds 		return err;
12951da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
12961da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
12971da177e4SLinus Torvalds 	pcm->private_data = chip;
12981da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
12991da177e4SLinus Torvalds 	chip->pcms[1] = pcm;
13001da177e4SLinus Torvalds 	/* set up playback */
13011da177e4SLinus Torvalds 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
13021da177e4SLinus Torvalds 	/* set up capture */
13031da177e4SLinus Torvalds 	init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 1);
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
13061da177e4SLinus Torvalds 						         snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
13071da177e4SLinus Torvalds 		return err;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	return 0;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds /*
13131da177e4SLinus Torvalds  * create pcm instances for VIA8233A
13141da177e4SLinus Torvalds  */
13151da177e4SLinus Torvalds static int __devinit snd_via8233a_pcm_new(via82xx_t *chip)
13161da177e4SLinus Torvalds {
13171da177e4SLinus Torvalds 	snd_pcm_t *pcm;
13181da177e4SLinus Torvalds 	int err;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	chip->multi_devno = 0;
13211da177e4SLinus Torvalds 	chip->playback_devno = 1;
13221da177e4SLinus Torvalds 	chip->capture_devno = 2;
13231da177e4SLinus Torvalds 	chip->num_devs = 3;
13241da177e4SLinus Torvalds 	chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	/* PCM #0:  multi-channel playback and capture */
13271da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
13281da177e4SLinus Torvalds 	if (err < 0)
13291da177e4SLinus Torvalds 		return err;
13301da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
13311da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
13321da177e4SLinus Torvalds 	pcm->private_data = chip;
13331da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
13341da177e4SLinus Torvalds 	chip->pcms[0] = pcm;
13351da177e4SLinus Torvalds 	/* set up playback */
13361da177e4SLinus Torvalds 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
13371da177e4SLinus Torvalds 	/* capture */
13381da177e4SLinus Torvalds 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
13411da177e4SLinus Torvalds 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
13421da177e4SLinus Torvalds 		return err;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	/* SPDIF supported? */
13451da177e4SLinus Torvalds 	if (! ac97_can_spdif(chip->ac97))
13461da177e4SLinus Torvalds 		return 0;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	/* PCM #1:  DXS3 playback (for spdif) */
13491da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
13501da177e4SLinus Torvalds 	if (err < 0)
13511da177e4SLinus Torvalds 		return err;
13521da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
13531da177e4SLinus Torvalds 	pcm->private_data = chip;
13541da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
13551da177e4SLinus Torvalds 	chip->pcms[1] = pcm;
13561da177e4SLinus Torvalds 	/* set up playback */
13571da177e4SLinus Torvalds 	init_viadev(chip, chip->playback_devno, 0x30, 0);
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
13601da177e4SLinus Torvalds 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
13611da177e4SLinus Torvalds 		return err;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	return 0;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds /*
13671da177e4SLinus Torvalds  * create a pcm instance for via686a/b
13681da177e4SLinus Torvalds  */
13691da177e4SLinus Torvalds static int __devinit snd_via686_pcm_new(via82xx_t *chip)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	snd_pcm_t *pcm;
13721da177e4SLinus Torvalds 	int err;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	chip->playback_devno = 0;
13751da177e4SLinus Torvalds 	chip->capture_devno = 1;
13761da177e4SLinus Torvalds 	chip->num_devs = 2;
13771da177e4SLinus Torvalds 	chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
13801da177e4SLinus Torvalds 	if (err < 0)
13811da177e4SLinus Torvalds 		return err;
13821da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
13831da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
13841da177e4SLinus Torvalds 	pcm->private_data = chip;
13851da177e4SLinus Torvalds 	strcpy(pcm->name, chip->card->shortname);
13861da177e4SLinus Torvalds 	chip->pcms[0] = pcm;
13871da177e4SLinus Torvalds 	init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0);
13881da177e4SLinus Torvalds 	init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 1);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
13911da177e4SLinus Torvalds 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
13921da177e4SLinus Torvalds 		return err;
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	return 0;
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds /*
13991da177e4SLinus Torvalds  *  Mixer part
14001da177e4SLinus Torvalds  */
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds static int snd_via8233_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
14031da177e4SLinus Torvalds {
14041da177e4SLinus Torvalds 	/* formerly they were "Line" and "Mic", but it looks like that they
14051da177e4SLinus Torvalds 	 * have nothing to do with the actual physical connections...
14061da177e4SLinus Torvalds 	 */
14071da177e4SLinus Torvalds 	static char *texts[2] = {
14081da177e4SLinus Torvalds 		"Input1", "Input2"
14091da177e4SLinus Torvalds 	};
14101da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
14111da177e4SLinus Torvalds 	uinfo->count = 1;
14121da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 2;
14131da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item >= 2)
14141da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 1;
14151da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
14161da177e4SLinus Torvalds 	return 0;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds static int snd_via8233_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
14221da177e4SLinus Torvalds 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
14231da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
14241da177e4SLinus Torvalds 	return 0;
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds static int snd_via8233_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
14281da177e4SLinus Torvalds {
14291da177e4SLinus Torvalds 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
14301da177e4SLinus Torvalds 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
14311da177e4SLinus Torvalds 	u8 val, oval;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14341da177e4SLinus Torvalds 	oval = inb(port);
14351da177e4SLinus Torvalds 	val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
14361da177e4SLinus Torvalds 	if (ucontrol->value.enumerated.item[0])
14371da177e4SLinus Torvalds 		val |= VIA_REG_CAPTURE_CHANNEL_MIC;
14381da177e4SLinus Torvalds 	if (val != oval)
14391da177e4SLinus Torvalds 		outb(val, port);
14401da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14411da177e4SLinus Torvalds 	return val != oval;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds static snd_kcontrol_new_t snd_via8233_capture_source __devinitdata = {
14451da177e4SLinus Torvalds 	.name = "Input Source Select",
14461da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
14471da177e4SLinus Torvalds 	.info = snd_via8233_capture_source_info,
14481da177e4SLinus Torvalds 	.get = snd_via8233_capture_source_get,
14491da177e4SLinus Torvalds 	.put = snd_via8233_capture_source_put,
14501da177e4SLinus Torvalds };
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds static int snd_via8233_dxs3_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
14531da177e4SLinus Torvalds {
14541da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
14551da177e4SLinus Torvalds 	uinfo->count = 1;
14561da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
14571da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
14581da177e4SLinus Torvalds 	return 0;
14591da177e4SLinus Torvalds }
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds static int snd_via8233_dxs3_spdif_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
14621da177e4SLinus Torvalds {
14631da177e4SLinus Torvalds 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
14641da177e4SLinus Torvalds 	u8 val;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
14671da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
14681da177e4SLinus Torvalds 	return 0;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds static int snd_via8233_dxs3_spdif_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
14721da177e4SLinus Torvalds {
14731da177e4SLinus Torvalds 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
14741da177e4SLinus Torvalds 	u8 val, oval;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
14771da177e4SLinus Torvalds 	val = oval & ~VIA8233_SPDIF_DX3;
14781da177e4SLinus Torvalds 	if (ucontrol->value.integer.value[0])
14791da177e4SLinus Torvalds 		val |= VIA8233_SPDIF_DX3;
14801da177e4SLinus Torvalds 	/* save the spdif flag for rate filtering */
14811da177e4SLinus Torvalds 	chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
14821da177e4SLinus Torvalds 	if (val != oval) {
14831da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
14841da177e4SLinus Torvalds 		return 1;
14851da177e4SLinus Torvalds 	}
14861da177e4SLinus Torvalds 	return 0;
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds static snd_kcontrol_new_t snd_via8233_dxs3_spdif_control __devinitdata = {
14901da177e4SLinus Torvalds 	.name = "IEC958 Output Switch",
14911da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
14921da177e4SLinus Torvalds 	.info = snd_via8233_dxs3_spdif_info,
14931da177e4SLinus Torvalds 	.get = snd_via8233_dxs3_spdif_get,
14941da177e4SLinus Torvalds 	.put = snd_via8233_dxs3_spdif_put,
14951da177e4SLinus Torvalds };
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds static int snd_via8233_dxs_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
14981da177e4SLinus Torvalds {
14991da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
15001da177e4SLinus Torvalds 	uinfo->count = 2;
15011da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
15021da177e4SLinus Torvalds 	uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
15031da177e4SLinus Torvalds 	return 0;
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds static int snd_via8233_dxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
15071da177e4SLinus Torvalds {
15081da177e4SLinus Torvalds 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
15091da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[0];
15101da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[1];
15111da177e4SLinus Torvalds 	return 0;
15121da177e4SLinus Torvalds }
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds static int snd_via8233_dxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
15171da177e4SLinus Torvalds 	unsigned int idx;
15181da177e4SLinus Torvalds 	unsigned char val;
15191da177e4SLinus Torvalds 	int i, change = 0;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
15221da177e4SLinus Torvalds 		val = ucontrol->value.integer.value[i];
15231da177e4SLinus Torvalds 		if (val > VIA_DXS_MAX_VOLUME)
15241da177e4SLinus Torvalds 			val = VIA_DXS_MAX_VOLUME;
15251da177e4SLinus Torvalds 		val = VIA_DXS_MAX_VOLUME - val;
15261da177e4SLinus Torvalds 		if (val != chip->playback_volume[i]) {
15271da177e4SLinus Torvalds 			change = 1;
15281da177e4SLinus Torvalds 			chip->playback_volume[i] = val;
15291da177e4SLinus Torvalds 			for (idx = 0; idx < 4; idx++) {
15301da177e4SLinus Torvalds 				unsigned long port = chip->port + 0x10 * idx;
15311da177e4SLinus Torvalds 				outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
15321da177e4SLinus Torvalds 			}
15331da177e4SLinus Torvalds 		}
15341da177e4SLinus Torvalds 	}
15351da177e4SLinus Torvalds 	return change;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds static snd_kcontrol_new_t snd_via8233_dxs_volume_control __devinitdata = {
15391da177e4SLinus Torvalds 	.name = "PCM Playback Volume",
15401da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
15411da177e4SLinus Torvalds 	.info = snd_via8233_dxs_volume_info,
15421da177e4SLinus Torvalds 	.get = snd_via8233_dxs_volume_get,
15431da177e4SLinus Torvalds 	.put = snd_via8233_dxs_volume_put,
15441da177e4SLinus Torvalds };
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds /*
15471da177e4SLinus Torvalds  */
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
15501da177e4SLinus Torvalds {
15511da177e4SLinus Torvalds 	via82xx_t *chip = bus->private_data;
15521da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	via82xx_t *chip = ac97->private_data;
15581da177e4SLinus Torvalds 	chip->ac97 = NULL;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds static struct ac97_quirk ac97_quirks[] = {
15621da177e4SLinus Torvalds 	{
15631da177e4SLinus Torvalds 		.vendor = 0x1106,
15641da177e4SLinus Torvalds 		.device = 0x4161,
15651da177e4SLinus Torvalds 		.codec_id = 0x56494161, /* VT1612A */
15661da177e4SLinus Torvalds 		.name = "Soltek SL-75DRV5",
15671da177e4SLinus Torvalds 		.type = AC97_TUNE_NONE
15681da177e4SLinus Torvalds 	},
15691da177e4SLinus Torvalds 	{	/* FIXME: which codec? */
15701da177e4SLinus Torvalds 		.vendor = 0x1106,
15711da177e4SLinus Torvalds 		.device = 0x4161,
15721da177e4SLinus Torvalds 		.name = "ASRock K7VT2",
15731da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY
15741da177e4SLinus Torvalds 	},
15751da177e4SLinus Torvalds 	{
15761da177e4SLinus Torvalds 		.vendor = 0x1019,
15771da177e4SLinus Torvalds 		.device = 0x0a81,
15781da177e4SLinus Torvalds 		.name = "ECS K7VTA3",
15791da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY
15801da177e4SLinus Torvalds 	},
15811da177e4SLinus Torvalds 	{
15821da177e4SLinus Torvalds 		.vendor = 0x1019,
15831da177e4SLinus Torvalds 		.device = 0x0a85,
15841da177e4SLinus Torvalds 		.name = "ECS L7VMM2",
15851da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY
15861da177e4SLinus Torvalds 	},
15871da177e4SLinus Torvalds 	{
15881da177e4SLinus Torvalds 		.vendor = 0x1849,
15891da177e4SLinus Torvalds 		.device = 0x3059,
15901da177e4SLinus Torvalds 		.name = "ASRock K7VM2",
15911da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY	/* VT1616 */
15921da177e4SLinus Torvalds 	},
15931da177e4SLinus Torvalds 	{
15941da177e4SLinus Torvalds 		.vendor = 0x14cd,
15951da177e4SLinus Torvalds 		.device = 0x7002,
15961da177e4SLinus Torvalds 		.name = "Unknown",
15971da177e4SLinus Torvalds 		.type = AC97_TUNE_ALC_JACK
15981da177e4SLinus Torvalds 	},
15991da177e4SLinus Torvalds 	{
16001da177e4SLinus Torvalds 		.vendor = 0x1071,
16011da177e4SLinus Torvalds 		.device = 0x8590,
16021da177e4SLinus Torvalds 		.name = "Mitac Mobo",
16031da177e4SLinus Torvalds 		.type = AC97_TUNE_ALC_JACK
16041da177e4SLinus Torvalds 	},
16051da177e4SLinus Torvalds 	{
16061da177e4SLinus Torvalds 		.vendor = 0x161f,
16071da177e4SLinus Torvalds 		.device = 0x202b,
16081da177e4SLinus Torvalds 		.name = "Arima Notebook",
16091da177e4SLinus Torvalds 		.type = AC97_TUNE_HP_ONLY,
16101da177e4SLinus Torvalds 	},
16111da177e4SLinus Torvalds 	{ } /* terminator */
16121da177e4SLinus Torvalds };
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds static int __devinit snd_via82xx_mixer_new(via82xx_t *chip, const char *quirk_override)
16151da177e4SLinus Torvalds {
16161da177e4SLinus Torvalds 	ac97_template_t ac97;
16171da177e4SLinus Torvalds 	int err;
16181da177e4SLinus Torvalds 	static ac97_bus_ops_t ops = {
16191da177e4SLinus Torvalds 		.write = snd_via82xx_codec_write,
16201da177e4SLinus Torvalds 		.read = snd_via82xx_codec_read,
16211da177e4SLinus Torvalds 		.wait = snd_via82xx_codec_wait,
16221da177e4SLinus Torvalds 	};
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
16251da177e4SLinus Torvalds 		return err;
16261da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
16271da177e4SLinus Torvalds 	chip->ac97_bus->clock = chip->ac97_clock;
16281da177e4SLinus Torvalds 	chip->ac97_bus->shared_type = AC97_SHARED_TYPE_VIA;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
16311da177e4SLinus Torvalds 	ac97.private_data = chip;
16321da177e4SLinus Torvalds 	ac97.private_free = snd_via82xx_mixer_free_ac97;
16331da177e4SLinus Torvalds 	ac97.pci = chip->pci;
16341da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
16351da177e4SLinus Torvalds 		return err;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686) {
16401da177e4SLinus Torvalds 		/* use slot 10/11 */
16411da177e4SLinus Torvalds 		snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
16421da177e4SLinus Torvalds 	}
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	return 0;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
16481da177e4SLinus Torvalds #define JOYSTICK_ADDR	0x200
16491da177e4SLinus Torvalds static int __devinit snd_via686_create_gameport(via82xx_t *chip, int dev, unsigned char *legacy)
16501da177e4SLinus Torvalds {
16511da177e4SLinus Torvalds 	struct gameport *gp;
16521da177e4SLinus Torvalds 	struct resource *r;
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	if (!joystick[dev])
16551da177e4SLinus Torvalds 		return -ENODEV;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
16581da177e4SLinus Torvalds 	if (!r) {
16591da177e4SLinus Torvalds 		printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n", JOYSTICK_ADDR);
16601da177e4SLinus Torvalds 		return -EBUSY;
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	chip->gameport = gp = gameport_allocate_port();
16641da177e4SLinus Torvalds 	if (!gp) {
16651da177e4SLinus Torvalds 		printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
16661da177e4SLinus Torvalds 		release_resource(r);
16671da177e4SLinus Torvalds 		kfree_nocheck(r);
16681da177e4SLinus Torvalds 		return -ENOMEM;
16691da177e4SLinus Torvalds 	}
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	gameport_set_name(gp, "VIA686 Gameport");
16721da177e4SLinus Torvalds 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
16731da177e4SLinus Torvalds 	gameport_set_dev_parent(gp, &chip->pci->dev);
16741da177e4SLinus Torvalds 	gp->io = JOYSTICK_ADDR;
16751da177e4SLinus Torvalds 	gameport_set_port_data(gp, r);
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	/* Enable legacy joystick port */
16781da177e4SLinus Torvalds 	*legacy |= VIA_FUNC_ENABLE_GAME;
16791da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	gameport_register_port(chip->gameport);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	return 0;
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds static void snd_via686_free_gameport(via82xx_t *chip)
16871da177e4SLinus Torvalds {
16881da177e4SLinus Torvalds 	if (chip->gameport) {
16891da177e4SLinus Torvalds 		struct resource *r = gameport_get_port_data(chip->gameport);
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 		gameport_unregister_port(chip->gameport);
16921da177e4SLinus Torvalds 		chip->gameport = NULL;
16931da177e4SLinus Torvalds 		release_resource(r);
16941da177e4SLinus Torvalds 		kfree_nocheck(r);
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds #else
16981da177e4SLinus Torvalds static inline int snd_via686_create_gameport(via82xx_t *chip, int dev, unsigned char *legacy)
16991da177e4SLinus Torvalds {
17001da177e4SLinus Torvalds 	return -ENOSYS;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds static inline void snd_via686_free_gameport(via82xx_t *chip) { }
17031da177e4SLinus Torvalds #endif
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds /*
17071da177e4SLinus Torvalds  *
17081da177e4SLinus Torvalds  */
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds static int __devinit snd_via8233_init_misc(via82xx_t *chip, int dev)
17111da177e4SLinus Torvalds {
17121da177e4SLinus Torvalds 	int i, err, caps;
17131da177e4SLinus Torvalds 	unsigned char val;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
17161da177e4SLinus Torvalds 	for (i = 0; i < caps; i++) {
17171da177e4SLinus Torvalds 		snd_via8233_capture_source.index = i;
17181da177e4SLinus Torvalds 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
17191da177e4SLinus Torvalds 		if (err < 0)
17201da177e4SLinus Torvalds 			return err;
17211da177e4SLinus Torvalds 	}
17221da177e4SLinus Torvalds 	if (ac97_can_spdif(chip->ac97)) {
17231da177e4SLinus Torvalds 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
17241da177e4SLinus Torvalds 		if (err < 0)
17251da177e4SLinus Torvalds 			return err;
17261da177e4SLinus Torvalds 	}
17271da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA8233A) {
17281da177e4SLinus Torvalds 		/* when no h/w PCM volume control is found, use DXS volume control
17291da177e4SLinus Torvalds 		 * as the PCM vol control
17301da177e4SLinus Torvalds 		 */
17311da177e4SLinus Torvalds 		snd_ctl_elem_id_t sid;
17321da177e4SLinus Torvalds 		memset(&sid, 0, sizeof(sid));
17331da177e4SLinus Torvalds 		strcpy(sid.name, "PCM Playback Volume");
17341da177e4SLinus Torvalds 		sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
17351da177e4SLinus Torvalds 		if (! snd_ctl_find_id(chip->card, &sid)) {
17361da177e4SLinus Torvalds 			err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
17371da177e4SLinus Torvalds 			if (err < 0)
17381da177e4SLinus Torvalds 				return err;
17391da177e4SLinus Torvalds 		}
17401da177e4SLinus Torvalds 	}
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 	/* select spdif data slot 10/11 */
17431da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
17441da177e4SLinus Torvalds 	val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
17451da177e4SLinus Torvalds 	val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
17461da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	return 0;
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds static int __devinit snd_via686_init_misc(via82xx_t *chip, int dev)
17521da177e4SLinus Torvalds {
17531da177e4SLinus Torvalds 	unsigned char legacy, legacy_cfg;
17541da177e4SLinus Torvalds 	int rev_h = 0;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	legacy = chip->old_legacy;
17571da177e4SLinus Torvalds 	legacy_cfg = chip->old_legacy_cfg;
17581da177e4SLinus Torvalds 	legacy |= VIA_FUNC_MIDI_IRQMASK;	/* FIXME: correct? (disable MIDI) */
17591da177e4SLinus Torvalds 	legacy &= ~VIA_FUNC_ENABLE_GAME;	/* disable joystick */
17601da177e4SLinus Torvalds 	if (chip->revision >= VIA_REV_686_H) {
17611da177e4SLinus Torvalds 		rev_h = 1;
17621da177e4SLinus Torvalds 		if (mpu_port[dev] >= 0x200) {	/* force MIDI */
17631da177e4SLinus Torvalds 			mpu_port[dev] &= 0xfffc;
17641da177e4SLinus Torvalds 			pci_write_config_dword(chip->pci, 0x18, mpu_port[dev] | 0x01);
17651da177e4SLinus Torvalds #ifdef CONFIG_PM
17661da177e4SLinus Torvalds 			chip->mpu_port_saved = mpu_port[dev];
17671da177e4SLinus Torvalds #endif
17681da177e4SLinus Torvalds 		} else {
17691da177e4SLinus Torvalds 			mpu_port[dev] = pci_resource_start(chip->pci, 2);
17701da177e4SLinus Torvalds 		}
17711da177e4SLinus Torvalds 	} else {
17721da177e4SLinus Torvalds 		switch (mpu_port[dev]) {	/* force MIDI */
17731da177e4SLinus Torvalds 		case 0x300:
17741da177e4SLinus Torvalds 		case 0x310:
17751da177e4SLinus Torvalds 		case 0x320:
17761da177e4SLinus Torvalds 		case 0x330:
17771da177e4SLinus Torvalds 			legacy_cfg &= ~(3 << 2);
17781da177e4SLinus Torvalds 			legacy_cfg |= (mpu_port[dev] & 0x0030) >> 2;
17791da177e4SLinus Torvalds 			break;
17801da177e4SLinus Torvalds 		default:			/* no, use BIOS settings */
17811da177e4SLinus Torvalds 			if (legacy & VIA_FUNC_ENABLE_MIDI)
17821da177e4SLinus Torvalds 				mpu_port[dev] = 0x300 + ((legacy_cfg & 0x000c) << 2);
17831da177e4SLinus Torvalds 			break;
17841da177e4SLinus Torvalds 		}
17851da177e4SLinus Torvalds 	}
17861da177e4SLinus Torvalds 	if (mpu_port[dev] >= 0x200 &&
17871da177e4SLinus Torvalds 	    (chip->mpu_res = request_region(mpu_port[dev], 2, "VIA82xx MPU401")) != NULL) {
17881da177e4SLinus Torvalds 		if (rev_h)
17891da177e4SLinus Torvalds 			legacy |= VIA_FUNC_MIDI_PNP;	/* enable PCI I/O 2 */
17901da177e4SLinus Torvalds 		legacy |= VIA_FUNC_ENABLE_MIDI;
17911da177e4SLinus Torvalds 	} else {
17921da177e4SLinus Torvalds 		if (rev_h)
17931da177e4SLinus Torvalds 			legacy &= ~VIA_FUNC_MIDI_PNP;	/* disable PCI I/O 2 */
17941da177e4SLinus Torvalds 		legacy &= ~VIA_FUNC_ENABLE_MIDI;
17951da177e4SLinus Torvalds 		mpu_port[dev] = 0;
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
17991da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
18001da177e4SLinus Torvalds 	if (chip->mpu_res) {
18011da177e4SLinus Torvalds 		if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
18021da177e4SLinus Torvalds 					mpu_port[dev], 1,
18031da177e4SLinus Torvalds 					chip->irq, 0, &chip->rmidi) < 0) {
18041da177e4SLinus Torvalds 			printk(KERN_WARNING "unable to initialize MPU-401 at 0x%lx, skipping\n", mpu_port[dev]);
18051da177e4SLinus Torvalds 			legacy &= ~VIA_FUNC_ENABLE_MIDI;
18061da177e4SLinus Torvalds 		} else {
18071da177e4SLinus Torvalds 			legacy &= ~VIA_FUNC_MIDI_IRQMASK;	/* enable MIDI interrupt */
18081da177e4SLinus Torvalds 		}
18091da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	snd_via686_create_gameport(chip, dev, &legacy);
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds #ifdef CONFIG_PM
18151da177e4SLinus Torvalds 	chip->legacy_saved = legacy;
18161da177e4SLinus Torvalds 	chip->legacy_cfg_saved = legacy_cfg;
18171da177e4SLinus Torvalds #endif
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	return 0;
18201da177e4SLinus Torvalds }
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds /*
18241da177e4SLinus Torvalds  * proc interface
18251da177e4SLinus Torvalds  */
18261da177e4SLinus Torvalds static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
18271da177e4SLinus Torvalds {
18281da177e4SLinus Torvalds 	via82xx_t *chip = entry->private_data;
18291da177e4SLinus Torvalds 	int i;
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
18321da177e4SLinus Torvalds 	for (i = 0; i < 0xa0; i += 4) {
18331da177e4SLinus Torvalds 		snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
18341da177e4SLinus Torvalds 	}
18351da177e4SLinus Torvalds }
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
18381da177e4SLinus Torvalds {
18391da177e4SLinus Torvalds 	snd_info_entry_t *entry;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "via82xx", &entry))
18421da177e4SLinus Torvalds 		snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds /*
18461da177e4SLinus Torvalds  *
18471da177e4SLinus Torvalds  */
18481da177e4SLinus Torvalds 
1849b3214970SJens Axboe static int snd_via82xx_chip_init(via82xx_t *chip)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds 	unsigned int val;
18521da177e4SLinus Torvalds 	int max_count;
18531da177e4SLinus Torvalds 	unsigned char pval;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds #if 0 /* broken on K7M? */
18561da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686)
18571da177e4SLinus Torvalds 		/* disable all legacy ports */
18581da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
18591da177e4SLinus Torvalds #endif
18601da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
18611da177e4SLinus Torvalds 	if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
18621da177e4SLinus Torvalds 		/* deassert ACLink reset, force SYNC */
18631da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
18641da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_ENABLE |
18651da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_RESET |
18661da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_SYNC);
18671da177e4SLinus Torvalds 		udelay(100);
18681da177e4SLinus Torvalds #if 1 /* FIXME: should we do full reset here for all chip models? */
18691da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
18701da177e4SLinus Torvalds 		udelay(100);
18711da177e4SLinus Torvalds #else
18721da177e4SLinus Torvalds 		/* deassert ACLink reset, force SYNC (warm AC'97 reset) */
18731da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
18741da177e4SLinus Torvalds 				      VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
18751da177e4SLinus Torvalds 		udelay(2);
18761da177e4SLinus Torvalds #endif
18771da177e4SLinus Torvalds 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
18781da177e4SLinus Torvalds 		/* note - FM data out has trouble with non VRA codecs !! */
18791da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
18801da177e4SLinus Torvalds 		udelay(100);
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	/* Make sure VRA is enabled, in case we didn't do a
18841da177e4SLinus Torvalds 	 * complete codec reset, above */
18851da177e4SLinus Torvalds 	pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
18861da177e4SLinus Torvalds 	if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
18871da177e4SLinus Torvalds 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
18881da177e4SLinus Torvalds 		/* note - FM data out has trouble with non VRA codecs !! */
18891da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
18901da177e4SLinus Torvalds 		udelay(100);
18911da177e4SLinus Torvalds 	}
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	/* wait until codec ready */
18941da177e4SLinus Torvalds 	max_count = ((3 * HZ) / 4) + 1;
18951da177e4SLinus Torvalds 	do {
18961da177e4SLinus Torvalds 		pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
18971da177e4SLinus Torvalds 		if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
18981da177e4SLinus Torvalds 			break;
18991da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
19001da177e4SLinus Torvalds 		schedule_timeout(1);
19011da177e4SLinus Torvalds 	} while (--max_count > 0);
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
19041da177e4SLinus Torvalds 		snd_printk("AC'97 codec is not ready [0x%x]\n", val);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
19071da177e4SLinus Torvalds 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
19081da177e4SLinus Torvalds 				 VIA_REG_AC97_SECONDARY_VALID |
19091da177e4SLinus Torvalds 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
19101da177e4SLinus Torvalds 	max_count = ((3 * HZ) / 4) + 1;
19111da177e4SLinus Torvalds 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
19121da177e4SLinus Torvalds 				 VIA_REG_AC97_SECONDARY_VALID |
19131da177e4SLinus Torvalds 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
19141da177e4SLinus Torvalds 	do {
19151da177e4SLinus Torvalds 		if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
19161da177e4SLinus Torvalds 			chip->ac97_secondary = 1;
19171da177e4SLinus Torvalds 			goto __ac97_ok2;
19181da177e4SLinus Torvalds 		}
19191da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
19201da177e4SLinus Torvalds 		schedule_timeout(1);
19211da177e4SLinus Torvalds 	} while (--max_count > 0);
19221da177e4SLinus Torvalds 	/* This is ok, the most of motherboards have only one codec */
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds       __ac97_ok2:
19251da177e4SLinus Torvalds #endif
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686) {
19281da177e4SLinus Torvalds 		/* route FM trap to IRQ, disable FM trap */
19291da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
19301da177e4SLinus Torvalds 		/* disable all GPI interrupts */
19311da177e4SLinus Torvalds 		outl(0, VIAREG(chip, GPI_INTR));
19321da177e4SLinus Torvalds 	}
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686) {
19351da177e4SLinus Torvalds 		/* Workaround for Award BIOS bug:
19361da177e4SLinus Torvalds 		 * DXS channels don't work properly with VRA if MC97 is disabled.
19371da177e4SLinus Torvalds 		 */
19381da177e4SLinus Torvalds 		struct pci_dev *pci;
19391da177e4SLinus Torvalds 		pci = pci_find_device(0x1106, 0x3068, NULL); /* MC97 */
19401da177e4SLinus Torvalds 		if (pci) {
19411da177e4SLinus Torvalds 			unsigned char data;
19421da177e4SLinus Torvalds 			pci_read_config_byte(pci, 0x44, &data);
19431da177e4SLinus Torvalds 			pci_write_config_byte(pci, 0x44, data | 0x40);
19441da177e4SLinus Torvalds 		}
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA8233A) {
19481da177e4SLinus Torvalds 		int i, idx;
19491da177e4SLinus Torvalds 		for (idx = 0; idx < 4; idx++) {
19501da177e4SLinus Torvalds 			unsigned long port = chip->port + 0x10 * idx;
19511da177e4SLinus Torvalds 			for (i = 0; i < 2; i++)
19521da177e4SLinus Torvalds 				outb(chip->playback_volume[i], port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
19531da177e4SLinus Torvalds 		}
19541da177e4SLinus Torvalds 	}
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	return 0;
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds #ifdef CONFIG_PM
19601da177e4SLinus Torvalds /*
19611da177e4SLinus Torvalds  * power management
19621da177e4SLinus Torvalds  */
19631da177e4SLinus Torvalds static int snd_via82xx_suspend(snd_card_t *card, pm_message_t state)
19641da177e4SLinus Torvalds {
19651da177e4SLinus Torvalds 	via82xx_t *chip = card->pm_private_data;
19661da177e4SLinus Torvalds 	int i;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
19691da177e4SLinus Torvalds 		if (chip->pcms[i])
19701da177e4SLinus Torvalds 			snd_pcm_suspend_all(chip->pcms[i]);
19711da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
19721da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
19731da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
19741da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	/* save misc values */
19771da177e4SLinus Torvalds 	if (chip->chip_type != TYPE_VIA686) {
19781da177e4SLinus Torvalds 		pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
19791da177e4SLinus Torvalds 		chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
19801da177e4SLinus Torvalds 		chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	pci_set_power_state(chip->pci, 3);
19841da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
19851da177e4SLinus Torvalds 	return 0;
19861da177e4SLinus Torvalds }
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds static int snd_via82xx_resume(snd_card_t *card)
19891da177e4SLinus Torvalds {
19901da177e4SLinus Torvalds 	via82xx_t *chip = card->pm_private_data;
19911da177e4SLinus Torvalds 	int i;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	pci_enable_device(chip->pci);
19941da177e4SLinus Torvalds 	pci_set_power_state(chip->pci, 0);
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	snd_via82xx_chip_init(chip);
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686) {
19991da177e4SLinus Torvalds 		if (chip->mpu_port_saved)
20001da177e4SLinus Torvalds 			pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
20011da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
20021da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
20031da177e4SLinus Torvalds 	} else {
20041da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
20051da177e4SLinus Torvalds 		outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
20061da177e4SLinus Torvalds 		outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
20071da177e4SLinus Torvalds 	}
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
20121da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	return 0;
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds #endif /* CONFIG_PM */
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds static int snd_via82xx_free(via82xx_t *chip)
20191da177e4SLinus Torvalds {
20201da177e4SLinus Torvalds 	unsigned int i;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	if (chip->irq < 0)
20231da177e4SLinus Torvalds 		goto __end_hw;
20241da177e4SLinus Torvalds 	/* disable interrupts */
20251da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
20261da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
20271da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
20281da177e4SLinus Torvalds       __end_hw:
20291da177e4SLinus Torvalds 	if (chip->irq >= 0)
20301da177e4SLinus Torvalds 		free_irq(chip->irq, (void *)chip);
20311da177e4SLinus Torvalds 	if (chip->mpu_res) {
20321da177e4SLinus Torvalds 		release_resource(chip->mpu_res);
20331da177e4SLinus Torvalds 		kfree_nocheck(chip->mpu_res);
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	if (chip->chip_type == TYPE_VIA686) {
20381da177e4SLinus Torvalds 		snd_via686_free_gameport(chip);
20391da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
20401da177e4SLinus Torvalds 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
20411da177e4SLinus Torvalds 	}
20421da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
20431da177e4SLinus Torvalds 	kfree(chip);
20441da177e4SLinus Torvalds 	return 0;
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds static int snd_via82xx_dev_free(snd_device_t *device)
20481da177e4SLinus Torvalds {
20491da177e4SLinus Torvalds 	via82xx_t *chip = device->device_data;
20501da177e4SLinus Torvalds 	return snd_via82xx_free(chip);
20511da177e4SLinus Torvalds }
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds static int __devinit snd_via82xx_create(snd_card_t * card,
20541da177e4SLinus Torvalds 					struct pci_dev *pci,
20551da177e4SLinus Torvalds 					int chip_type,
20561da177e4SLinus Torvalds 					int revision,
20571da177e4SLinus Torvalds 					unsigned int ac97_clock,
20581da177e4SLinus Torvalds 					via82xx_t ** r_via)
20591da177e4SLinus Torvalds {
20601da177e4SLinus Torvalds 	via82xx_t *chip;
20611da177e4SLinus Torvalds 	int err;
20621da177e4SLinus Torvalds         static snd_device_ops_t ops = {
20631da177e4SLinus Torvalds 		.dev_free =	snd_via82xx_dev_free,
20641da177e4SLinus Torvalds         };
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
20671da177e4SLinus Torvalds 		return err;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	if ((chip = kcalloc(1, sizeof(*chip), GFP_KERNEL)) == NULL) {
20701da177e4SLinus Torvalds 		pci_disable_device(pci);
20711da177e4SLinus Torvalds 		return -ENOMEM;
20721da177e4SLinus Torvalds 	}
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	chip->chip_type = chip_type;
20751da177e4SLinus Torvalds 	chip->revision = revision;
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
20781da177e4SLinus Torvalds 	spin_lock_init(&chip->rates[0].lock);
20791da177e4SLinus Torvalds 	spin_lock_init(&chip->rates[1].lock);
20801da177e4SLinus Torvalds 	chip->card = card;
20811da177e4SLinus Torvalds 	chip->pci = pci;
20821da177e4SLinus Torvalds 	chip->irq = -1;
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
20851da177e4SLinus Torvalds 	pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
20861da177e4SLinus Torvalds 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
20871da177e4SLinus Torvalds 			      chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
20901da177e4SLinus Torvalds 		kfree(chip);
20911da177e4SLinus Torvalds 		pci_disable_device(pci);
20921da177e4SLinus Torvalds 		return err;
20931da177e4SLinus Torvalds 	}
20941da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
20951da177e4SLinus Torvalds 	if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
20961da177e4SLinus Torvalds 			card->driver, (void *)chip)) {
20971da177e4SLinus Torvalds 		snd_printk("unable to grab IRQ %d\n", pci->irq);
20981da177e4SLinus Torvalds 		snd_via82xx_free(chip);
20991da177e4SLinus Torvalds 		return -EBUSY;
21001da177e4SLinus Torvalds 	}
21011da177e4SLinus Torvalds 	chip->irq = pci->irq;
21021da177e4SLinus Torvalds 	if (ac97_clock >= 8000 && ac97_clock <= 48000)
21031da177e4SLinus Torvalds 		chip->ac97_clock = ac97_clock;
21041da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	if ((err = snd_via82xx_chip_init(chip)) < 0) {
21071da177e4SLinus Torvalds 		snd_via82xx_free(chip);
21081da177e4SLinus Torvalds 		return err;
21091da177e4SLinus Torvalds 	}
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
21121da177e4SLinus Torvalds 		snd_via82xx_free(chip);
21131da177e4SLinus Torvalds 		return err;
21141da177e4SLinus Torvalds 	}
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	/* The 8233 ac97 controller does not implement the master bit
21171da177e4SLinus Torvalds 	 * in the pci command register. IMHO this is a violation of the PCI spec.
21181da177e4SLinus Torvalds 	 * We call pci_set_master here because it does not hurt. */
21191da177e4SLinus Torvalds 	pci_set_master(pci);
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	*r_via = chip;
21241da177e4SLinus Torvalds 	return 0;
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds struct via823x_info {
21281da177e4SLinus Torvalds 	int revision;
21291da177e4SLinus Torvalds 	char *name;
21301da177e4SLinus Torvalds 	int type;
21311da177e4SLinus Torvalds };
21321da177e4SLinus Torvalds static struct via823x_info via823x_cards[] __devinitdata = {
21331da177e4SLinus Torvalds 	{ VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
21341da177e4SLinus Torvalds 	{ VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
21351da177e4SLinus Torvalds 	{ VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
21361da177e4SLinus Torvalds 	{ VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
21371da177e4SLinus Torvalds 	{ VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
21381da177e4SLinus Torvalds 	{ VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
21391da177e4SLinus Torvalds };
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds /*
21421da177e4SLinus Torvalds  * auto detection of DXS channel supports.
21431da177e4SLinus Torvalds  */
21441da177e4SLinus Torvalds struct dxs_whitelist {
21451da177e4SLinus Torvalds 	unsigned short vendor;
21461da177e4SLinus Torvalds 	unsigned short device;
21471da177e4SLinus Torvalds 	unsigned short mask;
21481da177e4SLinus Torvalds 	short action;	/* new dxs_support value */
21491da177e4SLinus Torvalds };
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds static int __devinit check_dxs_list(struct pci_dev *pci)
21521da177e4SLinus Torvalds {
21531da177e4SLinus Torvalds 	static struct dxs_whitelist whitelist[] = {
21541da177e4SLinus Torvalds 		{ .vendor = 0x1005, .device = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
21551da177e4SLinus Torvalds 		{ .vendor = 0x1019, .device = 0x0996, .action = VIA_DXS_48K },
21561da177e4SLinus Torvalds 		{ .vendor = 0x1019, .device = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
21571da177e4SLinus Torvalds 		{ .vendor = 0x1019, .device = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
21581da177e4SLinus Torvalds 		{ .vendor = 0x1025, .device = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
21591da177e4SLinus Torvalds 		{ .vendor = 0x1043, .device = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
21601da177e4SLinus Torvalds 		{ .vendor = 0x1043, .device = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
21611da177e4SLinus Torvalds 		{ .vendor = 0x1043, .device = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
21622d7eb7cbSSergey Vlasov 		{ .vendor = 0x1043, .device = 0x812a, .action = VIA_DXS_SRC    }, /* ASUS A8V Deluxe */
21631da177e4SLinus Torvalds 		{ .vendor = 0x1071, .device = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
21641da177e4SLinus Torvalds 		{ .vendor = 0x10cf, .device = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
21651da177e4SLinus Torvalds 		{ .vendor = 0x1106, .device = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
21661da177e4SLinus Torvalds 		{ .vendor = 0x1106, .device = 0x4552, .action = VIA_DXS_NO_VRA }, /* QDI Kudoz 7X/600-6AL */
21671da177e4SLinus Torvalds 		{ .vendor = 0x1106, .device = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
21681da177e4SLinus Torvalds 		{ .vendor = 0x1297, .device = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
21691da177e4SLinus Torvalds 		{ .vendor = 0x1297, .device = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
21701da177e4SLinus Torvalds 		{ .vendor = 0x1458, .device = 0xa002, .action = VIA_DXS_ENABLE }, /* Gigabyte GA-7VAXP */
21711da177e4SLinus Torvalds 		{ .vendor = 0x1462, .device = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
21721da177e4SLinus Torvalds 		{ .vendor = 0x1462, .device = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
21731da177e4SLinus Torvalds 		{ .vendor = 0x1462, .device = 0x7023, .action = VIA_DXS_NO_VRA }, /* MSI K8T Neo2-FI */
21741da177e4SLinus Torvalds 		{ .vendor = 0x1462, .device = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
21751da177e4SLinus Torvalds 		{ .vendor = 0x147b, .device = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
21761da177e4SLinus Torvalds 		{ .vendor = 0x147b, .device = 0x1411, .action = VIA_DXS_ENABLE }, /* ABIT VA-20 */
21771da177e4SLinus Torvalds 		{ .vendor = 0x147b, .device = 0x1413, .action = VIA_DXS_ENABLE }, /* ABIT KV8 Pro */
21781da177e4SLinus Torvalds 		{ .vendor = 0x147b, .device = 0x1415, .action = VIA_DXS_NO_VRA }, /* Abit AV8 */
21791da177e4SLinus Torvalds 		{ .vendor = 0x14ff, .device = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
21801da177e4SLinus Torvalds 		{ .vendor = 0x1584, .device = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
21811da177e4SLinus Torvalds 		{ .vendor = 0x1584, .device = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
21821da177e4SLinus Torvalds 		{ .vendor = 0x161f, .device = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
21831da177e4SLinus Torvalds 		{ .vendor = 0x161f, .device = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
21841da177e4SLinus Torvalds 		{ .vendor = 0x1631, .device = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
21851da177e4SLinus Torvalds 		{ .vendor = 0x1695, .device = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
21861da177e4SLinus Torvalds 		{ .vendor = 0x1849, .device = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
21871da177e4SLinus Torvalds 		{ } /* terminator */
21881da177e4SLinus Torvalds 	};
21891da177e4SLinus Torvalds 	struct dxs_whitelist *w;
21901da177e4SLinus Torvalds 	unsigned short subsystem_vendor;
21911da177e4SLinus Torvalds 	unsigned short subsystem_device;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
21941da177e4SLinus Torvalds 	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	for (w = whitelist; w->vendor; w++) {
21971da177e4SLinus Torvalds 		if (w->vendor != subsystem_vendor)
21981da177e4SLinus Torvalds 			continue;
21991da177e4SLinus Torvalds 		if (w->mask) {
22001da177e4SLinus Torvalds 			if ((w->mask & subsystem_device) == w->device)
22011da177e4SLinus Torvalds 				return w->action;
22021da177e4SLinus Torvalds 		} else {
22031da177e4SLinus Torvalds 			if (subsystem_device == w->device)
22041da177e4SLinus Torvalds 				return w->action;
22051da177e4SLinus Torvalds 		}
22061da177e4SLinus Torvalds 	}
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	/*
22091da177e4SLinus Torvalds 	 * not detected, try 48k rate only to be sure.
22101da177e4SLinus Torvalds 	 */
22111da177e4SLinus Torvalds 	printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
22121da177e4SLinus Torvalds 	printk(KERN_INFO "         Please try dxs_support=1 or dxs_support=4 option\n");
22131da177e4SLinus Torvalds 	printk(KERN_INFO "         and report if it works on your machine.\n");
22141da177e4SLinus Torvalds 	return VIA_DXS_48K;
22151da177e4SLinus Torvalds };
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds static int __devinit snd_via82xx_probe(struct pci_dev *pci,
22181da177e4SLinus Torvalds 				       const struct pci_device_id *pci_id)
22191da177e4SLinus Torvalds {
22201da177e4SLinus Torvalds 	static int dev;
22211da177e4SLinus Torvalds 	snd_card_t *card;
22221da177e4SLinus Torvalds 	via82xx_t *chip;
22231da177e4SLinus Torvalds 	unsigned char revision;
22241da177e4SLinus Torvalds 	int chip_type = 0, card_type;
22251da177e4SLinus Torvalds 	unsigned int i;
22261da177e4SLinus Torvalds 	int err;
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS)
22291da177e4SLinus Torvalds 		return -ENODEV;
22301da177e4SLinus Torvalds 	if (!enable[dev]) {
22311da177e4SLinus Torvalds 		dev++;
22321da177e4SLinus Torvalds 		return -ENOENT;
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds 	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
22361da177e4SLinus Torvalds 	if (card == NULL)
22371da177e4SLinus Torvalds 		return -ENOMEM;
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	card_type = pci_id->driver_data;
22401da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
22411da177e4SLinus Torvalds 	switch (card_type) {
22421da177e4SLinus Torvalds 	case TYPE_CARD_VIA686:
22431da177e4SLinus Torvalds 		strcpy(card->driver, "VIA686A");
22441da177e4SLinus Torvalds 		sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
22451da177e4SLinus Torvalds 		chip_type = TYPE_VIA686;
22461da177e4SLinus Torvalds 		break;
22471da177e4SLinus Torvalds 	case TYPE_CARD_VIA8233:
22481da177e4SLinus Torvalds 		chip_type = TYPE_VIA8233;
22491da177e4SLinus Torvalds 		sprintf(card->shortname, "VIA 823x rev%x", revision);
22501da177e4SLinus Torvalds 		for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
22511da177e4SLinus Torvalds 			if (revision == via823x_cards[i].revision) {
22521da177e4SLinus Torvalds 				chip_type = via823x_cards[i].type;
22531da177e4SLinus Torvalds 				strcpy(card->shortname, via823x_cards[i].name);
22541da177e4SLinus Torvalds 				break;
22551da177e4SLinus Torvalds 			}
22561da177e4SLinus Torvalds 		}
22571da177e4SLinus Torvalds 		if (chip_type != TYPE_VIA8233A) {
22581da177e4SLinus Torvalds 			if (dxs_support[dev] == VIA_DXS_AUTO)
22591da177e4SLinus Torvalds 				dxs_support[dev] = check_dxs_list(pci);
22601da177e4SLinus Torvalds 			/* force to use VIA8233 or 8233A model according to
22611da177e4SLinus Torvalds 			 * dxs_support module option
22621da177e4SLinus Torvalds 			 */
22631da177e4SLinus Torvalds 			if (dxs_support[dev] == VIA_DXS_DISABLE)
22641da177e4SLinus Torvalds 				chip_type = TYPE_VIA8233A;
22651da177e4SLinus Torvalds 			else
22661da177e4SLinus Torvalds 				chip_type = TYPE_VIA8233;
22671da177e4SLinus Torvalds 		}
22681da177e4SLinus Torvalds 		if (chip_type == TYPE_VIA8233A)
22691da177e4SLinus Torvalds 			strcpy(card->driver, "VIA8233A");
22701da177e4SLinus Torvalds 		else if (revision >= VIA_REV_8237)
22711da177e4SLinus Torvalds 			strcpy(card->driver, "VIA8237"); /* no slog assignment */
22721da177e4SLinus Torvalds 		else
22731da177e4SLinus Torvalds 			strcpy(card->driver, "VIA8233");
22741da177e4SLinus Torvalds 		break;
22751da177e4SLinus Torvalds 	default:
22761da177e4SLinus Torvalds 		snd_printk(KERN_ERR "invalid card type %d\n", card_type);
22771da177e4SLinus Torvalds 		err = -EINVAL;
22781da177e4SLinus Torvalds 		goto __error;
22791da177e4SLinus Torvalds 	}
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds 	if ((err = snd_via82xx_create(card, pci, chip_type, revision, ac97_clock[dev], &chip)) < 0)
22821da177e4SLinus Torvalds 		goto __error;
22831da177e4SLinus Torvalds 	if ((err = snd_via82xx_mixer_new(chip, ac97_quirk[dev])) < 0)
22841da177e4SLinus Torvalds 		goto __error;
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	if (chip_type == TYPE_VIA686) {
22871da177e4SLinus Torvalds 		if ((err = snd_via686_pcm_new(chip)) < 0 ||
22881da177e4SLinus Torvalds 		    (err = snd_via686_init_misc(chip, dev)) < 0)
22891da177e4SLinus Torvalds 			goto __error;
22901da177e4SLinus Torvalds 	} else {
22911da177e4SLinus Torvalds 		if (chip_type == TYPE_VIA8233A) {
22921da177e4SLinus Torvalds 			if ((err = snd_via8233a_pcm_new(chip)) < 0)
22931da177e4SLinus Torvalds 				goto __error;
22941da177e4SLinus Torvalds 			// chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
22951da177e4SLinus Torvalds 		} else {
22961da177e4SLinus Torvalds 			if ((err = snd_via8233_pcm_new(chip)) < 0)
22971da177e4SLinus Torvalds 				goto __error;
22981da177e4SLinus Torvalds 			if (dxs_support[dev] == VIA_DXS_48K)
22991da177e4SLinus Torvalds 				chip->dxs_fixed = 1;
23001da177e4SLinus Torvalds 			else if (dxs_support[dev] == VIA_DXS_NO_VRA)
23011da177e4SLinus Torvalds 				chip->no_vra = 1;
23022d7eb7cbSSergey Vlasov 			else if (dxs_support[dev] == VIA_DXS_SRC) {
23032d7eb7cbSSergey Vlasov 				chip->no_vra = 1;
23042d7eb7cbSSergey Vlasov 				chip->dxs_src = 1;
23052d7eb7cbSSergey Vlasov 			}
23061da177e4SLinus Torvalds 		}
23071da177e4SLinus Torvalds 		if ((err = snd_via8233_init_misc(chip, dev)) < 0)
23081da177e4SLinus Torvalds 			goto __error;
23091da177e4SLinus Torvalds 	}
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds 	snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	/* disable interrupts */
23141da177e4SLinus Torvalds 	for (i = 0; i < chip->num_devs; i++)
23151da177e4SLinus Torvalds 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 	snprintf(card->longname, sizeof(card->longname),
23181da177e4SLinus Torvalds 		 "%s with %s at %#lx, irq %d", card->shortname,
23191da177e4SLinus Torvalds 		 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	snd_via82xx_proc_init(chip);
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
23241da177e4SLinus Torvalds 		snd_card_free(card);
23251da177e4SLinus Torvalds 		return err;
23261da177e4SLinus Torvalds 	}
23271da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
23281da177e4SLinus Torvalds 	dev++;
23291da177e4SLinus Torvalds 	return 0;
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds  __error:
23321da177e4SLinus Torvalds 	snd_card_free(card);
23331da177e4SLinus Torvalds 	return err;
23341da177e4SLinus Torvalds }
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds static void __devexit snd_via82xx_remove(struct pci_dev *pci)
23371da177e4SLinus Torvalds {
23381da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
23391da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds static struct pci_driver driver = {
23431da177e4SLinus Torvalds 	.name = "VIA 82xx Audio",
23441da177e4SLinus Torvalds 	.id_table = snd_via82xx_ids,
23451da177e4SLinus Torvalds 	.probe = snd_via82xx_probe,
23461da177e4SLinus Torvalds 	.remove = __devexit_p(snd_via82xx_remove),
23471da177e4SLinus Torvalds 	SND_PCI_PM_CALLBACKS
23481da177e4SLinus Torvalds };
23491da177e4SLinus Torvalds 
23501da177e4SLinus Torvalds static int __init alsa_card_via82xx_init(void)
23511da177e4SLinus Torvalds {
2352*01d25d46STakashi Iwai 	return pci_register_driver(&driver);
23531da177e4SLinus Torvalds }
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds static void __exit alsa_card_via82xx_exit(void)
23561da177e4SLinus Torvalds {
23571da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds module_init(alsa_card_via82xx_init)
23611da177e4SLinus Torvalds module_exit(alsa_card_via82xx_exit)
2362