blob: f03fd620a2a0760b1f028529dd5ad17c654dbc27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for VIA VT82xx (South Bridge)
3 *
4 * VT82C686A/B/C, VT8233A/C, VT8235
5 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02006 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
8 * 2002 Takashi Iwai <tiwai@suse.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26/*
27 * Changes:
28 *
29 * Dec. 19, 2002 Takashi Iwai <tiwai@suse.de>
30 * - use the DSX channels for the first pcm playback.
31 * (on VIA8233, 8233C and 8235 only)
32 * this will allow you play simultaneously up to 4 streams.
33 * multi-channel playback is assigned to the second device
34 * on these chips.
35 * - support the secondary capture (on VIA8233/C,8235)
36 * - SPDIF support
37 * the DSX3 channel can be used for SPDIF output.
38 * on VIA8233A, this channel is assigned to the second pcm
39 * playback.
40 * the card config of alsa-lib will assign the correct
41 * device for applications.
42 * - clean up the code, separate low-level initialization
43 * routines for each chipset.
Karsten Wiese4f550df2005-10-18 14:31:07 +020044 *
45 * Sep. 26, 2005 Karsten Wiese <annabellesgarden@yahoo.de>
46 * - Optimize position calculation for the 823x chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50#include <linux/delay.h>
51#include <linux/interrupt.h>
52#include <linux/init.h>
53#include <linux/pci.h>
54#include <linux/slab.h>
55#include <linux/gameport.h>
56#include <linux/moduleparam.h>
57#include <sound/core.h>
58#include <sound/pcm.h>
59#include <sound/pcm_params.h>
60#include <sound/info.h>
Takashi Iwai7058c042006-08-21 18:44:54 +020061#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <sound/ac97_codec.h>
63#include <sound/mpu401.h>
64#include <sound/initval.h>
65
66#if 0
67#define POINTER_DEBUG
68#endif
69
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020070MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071MODULE_DESCRIPTION("VIA VT82xx audio");
72MODULE_LICENSE("GPL");
73MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
74
75#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
76#define SUPPORT_JOYSTICK 1
77#endif
78
Clemens Ladischb7fe4622005-10-04 08:46:51 +020079static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
80static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
81static long mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef SUPPORT_JOYSTICK
Clemens Ladischb7fe4622005-10-04 08:46:51 +020083static int joystick;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +020085static int ac97_clock = 48000;
86static char *ac97_quirk;
87static int dxs_support;
Clemens Ladisch395c61d2010-07-12 16:27:24 +020088static int dxs_init_volume = 31;
Simon Arlott115551d2009-06-21 19:50:48 +010089static int nodelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Clemens Ladischb7fe4622005-10-04 08:46:51 +020091module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020093module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020095module_param(mpu_port, long, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
97#ifdef SUPPORT_JOYSTICK
Clemens Ladischb7fe4622005-10-04 08:46:51 +020098module_param(joystick, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
100#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200101module_param(ac97_clock, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200103module_param(ac97_quirk, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200105module_param(dxs_support, int, 0444);
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200106MODULE_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)");
Clemens Ladisch395c61d2010-07-12 16:27:24 +0200107module_param(dxs_init_volume, int, 0644);
108MODULE_PARM_DESC(dxs_init_volume, "initial DXS volume (0-31)");
Simon Arlott115551d2009-06-21 19:50:48 +0100109module_param(nodelay, int, 0444);
110MODULE_PARM_DESC(nodelay, "Disable 500ms init delay");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200112/* just for backward compatibility */
113static int enable;
Takashi Iwai698444f2005-10-20 16:53:49 +0200114module_param(enable, bool, 0444);
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* revision numbers for via686 */
118#define VIA_REV_686_A 0x10
119#define VIA_REV_686_B 0x11
120#define VIA_REV_686_C 0x12
121#define VIA_REV_686_D 0x13
122#define VIA_REV_686_E 0x14
123#define VIA_REV_686_H 0x20
124
125/* revision numbers for via8233 */
126#define VIA_REV_PRE_8233 0x10 /* not in market */
127#define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
128#define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
129#define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
130#define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
131#define VIA_REV_8237 0x60
Bastiaan Jacques8263c652006-04-18 17:04:04 +0200132#define VIA_REV_8251 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * Direct registers
136 */
137
138#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
139#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
140
141/* common offsets */
142#define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
143#define VIA_REG_STAT_ACTIVE 0x80 /* RO */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200144#define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define VIA_REG_STAT_PAUSED 0x40 /* RO */
146#define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
147#define VIA_REG_STAT_STOPPED 0x04 /* RWC */
148#define VIA_REG_STAT_EOL 0x02 /* RWC */
149#define VIA_REG_STAT_FLAG 0x01 /* RWC */
150#define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
151#define VIA_REG_CTRL_START 0x80 /* WO */
152#define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
153#define VIA_REG_CTRL_AUTOSTART 0x20
154#define VIA_REG_CTRL_PAUSE 0x08 /* RW */
155#define VIA_REG_CTRL_INT_STOP 0x04
156#define VIA_REG_CTRL_INT_EOL 0x02
157#define VIA_REG_CTRL_INT_FLAG 0x01
158#define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
159#define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
160#define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
161#define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
162#define VIA_REG_TYPE_16BIT 0x20 /* RW */
163#define VIA_REG_TYPE_STEREO 0x10 /* RW */
164#define VIA_REG_TYPE_INT_LLINE 0x00
165#define VIA_REG_TYPE_INT_LSAMPLE 0x04
166#define VIA_REG_TYPE_INT_LESSONE 0x08
167#define VIA_REG_TYPE_INT_MASK 0x0c
168#define VIA_REG_TYPE_INT_EOL 0x02
169#define VIA_REG_TYPE_INT_FLAG 0x01
170#define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
171#define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
172#define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
173#define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
174#define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
175#define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
176#define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
177
178#define DEFINE_VIA_REGSET(name,val) \
179enum {\
180 VIA_REG_##name##_STATUS = (val),\
181 VIA_REG_##name##_CONTROL = (val) + 0x01,\
182 VIA_REG_##name##_TYPE = (val) + 0x02,\
183 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
184 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
185 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
186 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
187}
188
189/* playback block */
190DEFINE_VIA_REGSET(PLAYBACK, 0x00);
191DEFINE_VIA_REGSET(CAPTURE, 0x10);
192DEFINE_VIA_REGSET(FM, 0x20);
193
194/* AC'97 */
195#define VIA_REG_AC97 0x80 /* dword */
196#define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
197#define VIA_REG_AC97_CODEC_ID_SHIFT 30
198#define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
199#define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
200#define VIA_REG_AC97_SECONDARY_VALID (1<<27)
201#define VIA_REG_AC97_PRIMARY_VALID (1<<25)
202#define VIA_REG_AC97_BUSY (1<<24)
203#define VIA_REG_AC97_READ (1<<23)
204#define VIA_REG_AC97_CMD_SHIFT 16
205#define VIA_REG_AC97_CMD_MASK 0x7e
206#define VIA_REG_AC97_DATA_SHIFT 0
207#define VIA_REG_AC97_DATA_MASK 0xffff
208
209#define VIA_REG_SGD_SHADOW 0x84 /* dword */
210/* via686 */
211#define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
212#define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
213#define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
214#define VIA_REG_SGD_STAT_PB_EOL (1<<4)
215#define VIA_REG_SGD_STAT_CP_EOL (1<<5)
216#define VIA_REG_SGD_STAT_FM_EOL (1<<6)
217#define VIA_REG_SGD_STAT_PB_STOP (1<<8)
218#define VIA_REG_SGD_STAT_CP_STOP (1<<9)
219#define VIA_REG_SGD_STAT_FM_STOP (1<<10)
220#define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
221#define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
222#define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
223/* via8233 */
224#define VIA8233_REG_SGD_STAT_FLAG (1<<0)
225#define VIA8233_REG_SGD_STAT_EOL (1<<1)
226#define VIA8233_REG_SGD_STAT_STOP (1<<2)
227#define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
228#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
229#define VIA8233_REG_SGD_CHAN_SDX 0
230#define VIA8233_REG_SGD_CHAN_MULTI 4
231#define VIA8233_REG_SGD_CHAN_REC 6
232#define VIA8233_REG_SGD_CHAN_REC1 7
233
234#define VIA_REG_GPI_STATUS 0x88
235#define VIA_REG_GPI_INTR 0x8c
236
237/* multi-channel and capture registers for via8233 */
238DEFINE_VIA_REGSET(MULTPLAY, 0x40);
239DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
240
241/* via8233-specific registers */
242#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
243#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
244#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
245#define VIA_REG_MULTPLAY_FMT_8BIT 0x00
246#define VIA_REG_MULTPLAY_FMT_16BIT 0x80
247#define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
248#define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
249#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
250
251#define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
252
253#define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
254#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
255#define VIA_REG_CAPTURE_CHANNEL_LINE 0
256#define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
257
258#define VIA_TBL_BIT_FLAG 0x40000000
259#define VIA_TBL_BIT_EOL 0x80000000
260
261/* pci space */
262#define VIA_ACLINK_STAT 0x40
263#define VIA_ACLINK_C11_READY 0x20
264#define VIA_ACLINK_C10_READY 0x10
265#define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
266#define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
267#define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
268#define VIA_ACLINK_CTRL 0x41
269#define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
270#define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
271#define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
272#define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
273#define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
274#define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
275#define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
276#define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
277#define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
278 VIA_ACLINK_CTRL_RESET|\
279 VIA_ACLINK_CTRL_PCM|\
280 VIA_ACLINK_CTRL_VRA)
281#define VIA_FUNC_ENABLE 0x42
282#define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
283#define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
284#define VIA_FUNC_RX2C_WRITE 0x20
285#define VIA_FUNC_SB_FIFO_EMPTY 0x10
286#define VIA_FUNC_ENABLE_GAME 0x08
287#define VIA_FUNC_ENABLE_FM 0x04
288#define VIA_FUNC_ENABLE_MIDI 0x02
289#define VIA_FUNC_ENABLE_SB 0x01
290#define VIA_PNP_CONTROL 0x43
291#define VIA_FM_NMI_CTRL 0x48
292#define VIA8233_VOLCHG_CTRL 0x48
293#define VIA8233_SPDIF_CTRL 0x49
294#define VIA8233_SPDIF_DX3 0x08
295#define VIA8233_SPDIF_SLOT_MASK 0x03
296#define VIA8233_SPDIF_SLOT_1011 0x00
297#define VIA8233_SPDIF_SLOT_34 0x01
298#define VIA8233_SPDIF_SLOT_78 0x02
299#define VIA8233_SPDIF_SLOT_69 0x03
300
301/*
302 */
303
304#define VIA_DXS_AUTO 0
305#define VIA_DXS_ENABLE 1
306#define VIA_DXS_DISABLE 2
307#define VIA_DXS_48K 3
308#define VIA_DXS_NO_VRA 4
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200309#define VIA_DXS_SRC 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311
312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * pcm stream
314 */
315
316struct snd_via_sg_table {
317 unsigned int offset;
318 unsigned int size;
319} ;
320
321#define VIA_TABLE_SIZE 255
Takashi Iwai55036002008-07-30 15:23:06 +0200322#define VIA_MAX_BUFSIZE (1<<24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100324struct viadev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 unsigned int reg_offset;
326 unsigned long port;
327 int direction; /* playback = 0, capture = 1 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100328 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int running;
330 unsigned int tbl_entries; /* # descriptors */
331 struct snd_dma_buffer table;
332 struct snd_via_sg_table *idx_table;
333 /* for recovery from the unexpected pointer */
334 unsigned int lastpos;
335 unsigned int fragsize;
336 unsigned int bufsize;
337 unsigned int bufsize2;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200338 int hwptr_done; /* processed frame position in the buffer */
339 int in_interrupt;
340 int shadow_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341};
342
343
344enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
345enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
346
347#define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
348
349struct via_rate_lock {
350 spinlock_t lock;
351 int rate;
352 int used;
353};
354
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100355struct via82xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int irq;
357
358 unsigned long port;
359 struct resource *mpu_res;
360 int chip_type;
361 unsigned char revision;
362
363 unsigned char old_legacy;
364 unsigned char old_legacy_cfg;
365#ifdef CONFIG_PM
366 unsigned char legacy_saved;
367 unsigned char legacy_cfg_saved;
368 unsigned char spdif_ctrl_saved;
369 unsigned char capture_src_saved[2];
370 unsigned int mpu_port_saved;
371#endif
372
Honza Maly00f226d2005-10-14 18:18:01 +0200373 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
374 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
377
378 struct pci_dev *pci;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100379 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 unsigned int num_devs;
382 unsigned int playback_devno, multi_devno, capture_devno;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100383 struct viadev devs[VIA_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct via_rate_lock rates[2]; /* playback and capture */
385 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
386 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200387 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
389
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100390 struct snd_pcm *pcms[2];
391 struct snd_rawmidi *rmidi;
Clemens Ladisch3d009412009-10-22 09:04:09 +0200392 struct snd_kcontrol *dxs_controls[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100394 struct snd_ac97_bus *ac97_bus;
395 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 unsigned int ac97_clock;
397 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
398
399 spinlock_t reg_lock;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100400 struct snd_info_entry *proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402#ifdef SUPPORT_JOYSTICK
403 struct gameport *gameport;
404#endif
405};
406
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200407static DEFINE_PCI_DEVICE_TABLE(snd_via82xx_ids) = {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200408 /* 0x1106, 0x3058 */
Joe Perches28d27aa2009-06-24 22:13:35 -0700409 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C686_5), TYPE_CARD_VIA686, }, /* 686A */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200410 /* 0x1106, 0x3059 */
Joe Perches28d27aa2009-06-24 22:13:35 -0700411 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_8233_5), TYPE_CARD_VIA8233, }, /* VT8233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 { 0, }
413};
414
415MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
416
417/*
418 */
419
420/*
421 * allocate and initialize the descriptor buffers
422 * periods = number of periods
423 * fragsize = period size in bytes
424 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100425static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct pci_dev *pci,
427 unsigned int periods, unsigned int fragsize)
428{
429 unsigned int i, idx, ofs, rest;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100430 struct via82xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (dev->table.area == NULL) {
433 /* the start of each lists must be aligned to 8 bytes,
434 * but the kernel pages are much bigger, so we don't care
435 */
436 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
437 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
438 &dev->table) < 0)
439 return -ENOMEM;
440 }
441 if (! dev->idx_table) {
442 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
443 if (! dev->idx_table)
444 return -ENOMEM;
445 }
446
447 /* fill the entries */
448 idx = 0;
449 ofs = 0;
450 for (i = 0; i < periods; i++) {
451 rest = fragsize;
452 /* fill descriptors for a period.
453 * a period can be split to several descriptors if it's
454 * over page boundary.
455 */
456 do {
457 unsigned int r;
458 unsigned int flag;
Takashi Iwai77a23f22008-08-21 13:00:13 +0200459 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (idx >= VIA_TABLE_SIZE) {
462 snd_printk(KERN_ERR "via82xx: too much table size!\n");
463 return -EINVAL;
464 }
Takashi Iwai77a23f22008-08-21 13:00:13 +0200465 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
466 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
Takashi Iwai55036002008-07-30 15:23:06 +0200467 r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 rest -= r;
469 if (! rest) {
470 if (i == periods - 1)
471 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
472 else
473 flag = VIA_TBL_BIT_FLAG; /* period boundary */
474 } else
475 flag = 0; /* period continues to the next */
Takashi Iwaiee419652009-02-05 16:11:31 +0100476 /*
477 printk(KERN_DEBUG "via: tbl %d: at %d size %d "
478 "(rest %d)\n", idx, ofs, r, rest);
479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
481 dev->idx_table[idx].offset = ofs;
482 dev->idx_table[idx].size = r;
483 ofs += r;
484 idx++;
485 } while (rest > 0);
486 }
487 dev->tbl_entries = idx;
488 dev->bufsize = periods * fragsize;
489 dev->bufsize2 = dev->bufsize / 2;
490 dev->fragsize = fragsize;
491 return 0;
492}
493
494
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100495static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct pci_dev *pci)
497{
498 if (dev->table.area) {
499 snd_dma_free_pages(&dev->table);
500 dev->table.area = NULL;
501 }
Jesper Juhl4d572772005-05-30 17:30:32 +0200502 kfree(dev->idx_table);
503 dev->idx_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505}
506
507/*
508 * Basic I/O
509 */
510
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100511static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 return inl(VIAREG(chip, AC97));
514}
515
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100516static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 outl(val, VIAREG(chip, AC97));
519}
520
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100521static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 unsigned int timeout = 1000; /* 1ms */
524 unsigned int val;
525
526 while (timeout-- > 0) {
527 udelay(1);
528 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
529 return val & 0xffff;
530 }
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100531 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
532 secondary, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EIO;
534}
535
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100536static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 unsigned int timeout = 1000; /* 1ms */
539 unsigned int val, val1;
540 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
541 VIA_REG_AC97_SECONDARY_VALID;
542
543 while (timeout-- > 0) {
544 val = snd_via82xx_codec_xread(chip);
545 val1 = val & (VIA_REG_AC97_BUSY | stat);
546 if (val1 == stat)
547 return val & 0xffff;
548 udelay(1);
549 }
550 return -EIO;
551}
552
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100553static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100555 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int err;
557 err = snd_via82xx_codec_ready(chip, ac97->num);
558 /* here we need to wait fairly for long time.. */
Simon Arlott115551d2009-06-21 19:50:48 +0100559 if (!nodelay)
560 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100563static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 unsigned short reg,
565 unsigned short val)
566{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100567 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned int xval;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
571 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
572 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
573 xval |= val << VIA_REG_AC97_DATA_SHIFT;
574 snd_via82xx_codec_xwrite(chip, xval);
575 snd_via82xx_codec_ready(chip, ac97->num);
576}
577
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100578static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100580 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 unsigned int xval, val = 0xffff;
582 int again = 0;
583
584 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
585 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
586 xval |= VIA_REG_AC97_READ;
587 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
588 while (1) {
589 if (again++ > 3) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100590 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
591 ac97->num, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0xffff;
593 }
594 snd_via82xx_codec_xwrite(chip, xval);
595 udelay (20);
596 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
597 udelay(25);
598 val = snd_via82xx_codec_xread(chip);
599 break;
600 }
601 }
602 return val & 0xffff;
603}
604
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100605static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
608 VIADEV_REG(viadev, OFFSET_CONTROL));
609 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
610 udelay(50);
611 /* disable interrupts */
612 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
613 /* clear interrupts */
614 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
615 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
616 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
617 viadev->lastpos = 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200618 viadev->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621
622/*
623 * Interrupt handler
Karsten Wiese4f550df2005-10-18 14:31:07 +0200624 * Used for 686 and 8233A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
David Howells7d12e782006-10-05 14:55:46 +0100626static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100628 struct via82xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 unsigned int status;
630 unsigned int i;
631
632 status = inl(VIAREG(chip, SGD_SHADOW));
633 if (! (status & chip->intr_mask)) {
634 if (chip->rmidi)
635 /* check mpu401 interrupt */
David Howells7d12e782006-10-05 14:55:46 +0100636 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return IRQ_NONE;
638 }
639
640 /* check status for each stream */
641 spin_lock(&chip->reg_lock);
642 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100643 struct viadev *viadev = &chip->devs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
Karsten Wiese4f550df2005-10-18 14:31:07 +0200645 if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 continue;
647 if (viadev->substream && viadev->running) {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200648 /*
649 * Update hwptr_done based on 'period elapsed'
650 * interrupts. We'll use it, when the chip returns 0
651 * for OFFSET_CURR_COUNT.
652 */
653 if (c_status & VIA_REG_STAT_EOL)
654 viadev->hwptr_done = 0;
655 else
656 viadev->hwptr_done += viadev->fragsize;
657 viadev->in_interrupt = c_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 spin_unlock(&chip->reg_lock);
659 snd_pcm_period_elapsed(viadev->substream);
660 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200661 viadev->in_interrupt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
664 }
665 spin_unlock(&chip->reg_lock);
666 return IRQ_HANDLED;
667}
668
669/*
Karsten Wiese4f550df2005-10-18 14:31:07 +0200670 * Interrupt handler
671 */
David Howells7d12e782006-10-05 14:55:46 +0100672static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
Karsten Wiese4f550df2005-10-18 14:31:07 +0200673{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100674 struct via82xx *chip = dev_id;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200675 unsigned int status;
676 unsigned int i;
677 int irqreturn = 0;
678
679 /* check status for each stream */
680 spin_lock(&chip->reg_lock);
681 status = inl(VIAREG(chip, SGD_SHADOW));
682
683 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100684 struct viadev *viadev = &chip->devs[i];
685 struct snd_pcm_substream *substream;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200686 unsigned char c_status, shadow_status;
687
688 shadow_status = (status >> viadev->shadow_shift) &
689 (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
690 VIA_REG_STAT_FLAG);
691 c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
692 if (!c_status)
693 continue;
694
695 substream = viadev->substream;
696 if (substream && viadev->running) {
697 /*
698 * Update hwptr_done based on 'period elapsed'
699 * interrupts. We'll use it, when the chip returns 0
700 * for OFFSET_CURR_COUNT.
701 */
702 if (c_status & VIA_REG_STAT_EOL)
703 viadev->hwptr_done = 0;
704 else
705 viadev->hwptr_done += viadev->fragsize;
706 viadev->in_interrupt = c_status;
707 if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
708 viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
709 spin_unlock(&chip->reg_lock);
710
711 snd_pcm_period_elapsed(substream);
712
713 spin_lock(&chip->reg_lock);
714 viadev->in_interrupt = 0;
715 }
716 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
717 irqreturn = 1;
718 }
719 spin_unlock(&chip->reg_lock);
720 return IRQ_RETVAL(irqreturn);
721}
722
723/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * PCM callbacks
725 */
726
727/*
728 * trigger callback
729 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100730static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100732 struct via82xx *chip = snd_pcm_substream_chip(substream);
733 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned char val;
735
736 if (chip->chip_type != TYPE_VIA686)
737 val = VIA_REG_CTRL_INT;
738 else
739 val = 0;
740 switch (cmd) {
741 case SNDRV_PCM_TRIGGER_START:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200742 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 val |= VIA_REG_CTRL_START;
744 viadev->running = 1;
745 break;
746 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200747 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 val = VIA_REG_CTRL_TERMINATE;
749 viadev->running = 0;
750 break;
751 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
752 val |= VIA_REG_CTRL_PAUSE;
753 viadev->running = 0;
754 break;
755 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
756 viadev->running = 1;
757 break;
758 default:
759 return -EINVAL;
760 }
761 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
762 if (cmd == SNDRV_PCM_TRIGGER_STOP)
763 snd_via82xx_channel_reset(chip, viadev);
764 return 0;
765}
766
767
768/*
769 * pointer callbacks
770 */
771
772/*
773 * calculate the linear position at the given sg-buffer index and the rest count
774 */
775
776#define check_invalid_pos(viadev,pos) \
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100777 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
778 viadev->lastpos < viadev->bufsize2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100780static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
781 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 unsigned int size, base, res;
784
785 size = viadev->idx_table[idx].size;
786 base = viadev->idx_table[idx].offset;
787 res = base + size - count;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200788 if (res >= viadev->bufsize)
789 res -= viadev->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* check the validity of the calculated position */
792 if (size < count) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100793 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
794 (int)size, (int)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 res = viadev->lastpos;
796 } else {
797 if (! count) {
798 /* Some mobos report count = 0 on the DMA boundary,
799 * i.e. count = size indeed.
800 * Let's check whether this step is above the expected size.
801 */
802 int delta = res - viadev->lastpos;
803 if (delta < 0)
804 delta += viadev->bufsize;
805 if ((unsigned int)delta > viadev->fragsize)
806 res = base;
807 }
808 if (check_invalid_pos(viadev, res)) {
809#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100810 printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
811 "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
812 "count = 0x%x\n", idx, viadev->tbl_entries,
813 viadev->lastpos, viadev->bufsize2,
814 viadev->idx_table[idx].offset,
815 viadev->idx_table[idx].size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#endif
817 /* count register returns full size when end of buffer is reached */
818 res = base + size;
819 if (check_invalid_pos(viadev, res)) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100820 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
821 "using last valid pointer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 res = viadev->lastpos;
823 }
824 }
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return res;
827}
828
829/*
830 * get the current pointer on via686
831 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100832static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100834 struct via82xx *chip = snd_pcm_substream_chip(substream);
835 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 unsigned int idx, ptr, count, res;
837
Takashi Iwaida3cec32008-08-08 17:12:14 +0200838 if (snd_BUG_ON(!viadev->tbl_entries))
839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
841 return 0;
842
843 spin_lock(&chip->reg_lock);
844 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
845 /* The via686a does not have the current index register,
846 * so we need to calculate the index from CURR_PTR.
847 */
848 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
849 if (ptr <= (unsigned int)viadev->table.addr)
850 idx = 0;
851 else /* CURR_PTR holds the address + 8 */
852 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
853 res = calc_linear_pos(viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200854 viadev->lastpos = res; /* remember the last position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 spin_unlock(&chip->reg_lock);
856
857 return bytes_to_frames(substream->runtime, res);
858}
859
860/*
861 * get the current pointer on via823x
862 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100863static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100865 struct via82xx *chip = snd_pcm_substream_chip(substream);
866 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned int idx, count, res;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200868 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Takashi Iwaida3cec32008-08-08 17:12:14 +0200870 if (snd_BUG_ON(!viadev->tbl_entries))
871 return 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200874 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
875 status = viadev->in_interrupt;
876 if (!status)
877 status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
878
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200879 /* An apparent bug in the 8251 is worked around by sending a
880 * REG_CTRL_START. */
881 if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
882 snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
883
Karsten Wiese4f550df2005-10-18 14:31:07 +0200884 if (!(status & VIA_REG_STAT_ACTIVE)) {
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200885 res = 0;
886 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Karsten Wiese4f550df2005-10-18 14:31:07 +0200888 if (count & 0xffffff) {
889 idx = count >> 24;
890 if (idx >= viadev->tbl_entries) {
891#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100892 printk(KERN_DEBUG "fail: invalid idx = %i/%i\n", idx,
893 viadev->tbl_entries);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200894#endif
895 res = viadev->lastpos;
896 } else {
897 count &= 0xffffff;
898 res = calc_linear_pos(viadev, idx, count);
899 }
900 } else {
901 res = viadev->hwptr_done;
902 if (!viadev->in_interrupt) {
903 if (status & VIA_REG_STAT_EOL) {
904 res = 0;
905 } else
906 if (status & VIA_REG_STAT_FLAG) {
907 res += viadev->fragsize;
908 }
909 }
910 }
911unlock:
912 viadev->lastpos = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 spin_unlock(&chip->reg_lock);
914
915 return bytes_to_frames(substream->runtime, res);
916}
917
918
919/*
920 * hw_params callback:
921 * allocate the buffer and build up the buffer description table
922 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100923static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
924 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100926 struct via82xx *chip = snd_pcm_substream_chip(substream);
927 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int err;
929
930 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
931 if (err < 0)
932 return err;
933 err = build_via_table(viadev, substream, chip->pci,
934 params_periods(hw_params),
935 params_period_bytes(hw_params));
936 if (err < 0)
937 return err;
938
939 return 0;
940}
941
942/*
943 * hw_free callback:
944 * clean up the buffer description table and release the buffer
945 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100946static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100948 struct via82xx *chip = snd_pcm_substream_chip(substream);
949 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 clean_via_table(viadev, substream, chip->pci);
952 snd_pcm_lib_free_pages(substream);
953 return 0;
954}
955
956
957/*
958 * set up the table pointer
959 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100960static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 snd_via82xx_codec_ready(chip, 0);
963 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
964 udelay(20);
965 snd_via82xx_codec_ready(chip, 0);
966}
967
968/*
969 * prepare callback for playback and capture on via686
970 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100971static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
972 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 snd_via82xx_channel_reset(chip, viadev);
975 /* this must be set after channel_reset */
976 snd_via82xx_set_table_ptr(chip, viadev);
977 outb(VIA_REG_TYPE_AUTOSTART |
978 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
979 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
980 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
981 VIA_REG_TYPE_INT_EOL |
982 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
983}
984
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100985static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100987 struct via82xx *chip = snd_pcm_substream_chip(substream);
988 struct viadev *viadev = substream->runtime->private_data;
989 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
992 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
993 via686_setup_format(chip, viadev, runtime);
994 return 0;
995}
996
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100997static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100999 struct via82xx *chip = snd_pcm_substream_chip(substream);
1000 struct viadev *viadev = substream->runtime->private_data;
1001 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1004 via686_setup_format(chip, viadev, runtime);
1005 return 0;
1006}
1007
1008/*
1009 * lock the current rate
1010 */
1011static int via_lock_rate(struct via_rate_lock *rec, int rate)
1012{
1013 int changed = 0;
1014
1015 spin_lock_irq(&rec->lock);
1016 if (rec->rate != rate) {
1017 if (rec->rate && rec->used > 1) /* already set */
1018 changed = -EINVAL;
1019 else {
1020 rec->rate = rate;
1021 changed = 1;
1022 }
1023 }
1024 spin_unlock_irq(&rec->lock);
1025 return changed;
1026}
1027
1028/*
1029 * prepare callback for DSX playback on via823x
1030 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001031static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001033 struct via82xx *chip = snd_pcm_substream_chip(substream);
1034 struct viadev *viadev = substream->runtime->private_data;
1035 struct snd_pcm_runtime *runtime = substream->runtime;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001036 int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 int rate_changed;
1038 u32 rbits;
1039
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001040 if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return rate_changed;
Takashi Iwai16d3f142005-08-15 15:02:28 +02001042 if (rate_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1044 chip->no_vra ? 48000 : runtime->rate);
Takashi Iwai16d3f142005-08-15 15:02:28 +02001045 if (chip->spdif_on && viadev->reg_offset == 0x30)
1046 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (runtime->rate == 48000)
1049 rbits = 0xfffff;
1050 else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001051 rbits = (0x100000 / 48000) * runtime->rate +
1052 ((0x100000 % 48000) * runtime->rate) / 48000;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001053 snd_BUG_ON(rbits & ~0xfffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 snd_via82xx_channel_reset(chip, viadev);
1055 snd_via82xx_set_table_ptr(chip, viadev);
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001056 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1057 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1058 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1059 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1061 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1062 rbits | /* rate */
1063 0xff000000, /* STOP index is never reached */
1064 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1065 udelay(20);
1066 snd_via82xx_codec_ready(chip, 0);
1067 return 0;
1068}
1069
1070/*
1071 * prepare callback for multi-channel playback on via823x
1072 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001073static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001075 struct via82xx *chip = snd_pcm_substream_chip(substream);
1076 struct viadev *viadev = substream->runtime->private_data;
1077 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 unsigned int slots;
1079 int fmt;
1080
1081 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1082 return -EINVAL;
1083 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1084 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1085 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1086 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1087 snd_via82xx_channel_reset(chip, viadev);
1088 snd_via82xx_set_table_ptr(chip, viadev);
1089
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001090 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1091 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 fmt |= runtime->channels << 4;
1093 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1094#if 0
1095 if (chip->revision == VIA_REV_8233A)
1096 slots = 0;
1097 else
1098#endif
1099 {
1100 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1101 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1102 switch (runtime->channels) {
1103 case 1: slots = (1<<0) | (1<<4); break;
1104 case 2: slots = (1<<0) | (2<<4); break;
1105 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1106 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1107 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1108 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1109 default: slots = 0; break;
1110 }
1111 }
1112 /* STOP index is never reached */
1113 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1114 udelay(20);
1115 snd_via82xx_codec_ready(chip, 0);
1116 return 0;
1117}
1118
1119/*
1120 * prepare callback for capture on via823x
1121 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001122static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001124 struct via82xx *chip = snd_pcm_substream_chip(substream);
1125 struct viadev *viadev = substream->runtime->private_data;
1126 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1129 return -EINVAL;
1130 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1131 snd_via82xx_channel_reset(chip, viadev);
1132 snd_via82xx_set_table_ptr(chip, viadev);
1133 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1134 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1135 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1136 0xff000000, /* STOP index is never reached */
1137 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1138 udelay(20);
1139 snd_via82xx_codec_ready(chip, 0);
1140 return 0;
1141}
1142
1143
1144/*
1145 * pcm hardware definition, identical for both playback and capture
1146 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001147static struct snd_pcm_hardware snd_via82xx_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1150 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1151 SNDRV_PCM_INFO_MMAP_VALID |
Jaroslav Kysela41e48452005-08-18 13:43:12 +02001152 /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 SNDRV_PCM_INFO_PAUSE),
1154 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1155 .rates = SNDRV_PCM_RATE_48000,
1156 .rate_min = 48000,
1157 .rate_max = 48000,
1158 .channels_min = 1,
1159 .channels_max = 2,
Takashi Iwai55036002008-07-30 15:23:06 +02001160 .buffer_bytes_max = VIA_MAX_BUFSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 .period_bytes_min = 32,
Takashi Iwai55036002008-07-30 15:23:06 +02001162 .period_bytes_max = VIA_MAX_BUFSIZE / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 .periods_min = 2,
1164 .periods_max = VIA_TABLE_SIZE / 2,
1165 .fifo_size = 0,
1166};
1167
1168
1169/*
1170 * open callback skeleton
1171 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001172static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1173 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001175 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int err;
1177 struct via_rate_lock *ratep;
1178
1179 runtime->hw = snd_via82xx_hw;
1180
1181 /* set the hw rate condition */
1182 ratep = &chip->rates[viadev->direction];
1183 spin_lock_irq(&ratep->lock);
1184 ratep->used++;
1185 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1186 /* DXS#3 and spdif is on */
1187 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1188 snd_pcm_limit_hw_rates(runtime);
1189 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1190 /* fixed DXS playback rate */
1191 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1192 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001193 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1194 /* use full SRC capabilities of DXS */
1195 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1196 SNDRV_PCM_RATE_8000_48000);
1197 runtime->hw.rate_min = 8000;
1198 runtime->hw.rate_max = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else if (! ratep->rate) {
1200 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1201 runtime->hw.rates = chip->ac97->rates[idx];
1202 snd_pcm_limit_hw_rates(runtime);
1203 } else {
1204 /* a fixed rate */
1205 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1206 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1207 }
1208 spin_unlock_irq(&ratep->lock);
1209
1210 /* we may remove following constaint when we modify table entries
1211 in interrupt */
1212 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1213 return err;
1214
1215 runtime->private_data = viadev;
1216 viadev->substream = substream;
1217
1218 return 0;
1219}
1220
1221
1222/*
Clemens Ladisch3d009412009-10-22 09:04:09 +02001223 * open callback for playback on via686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
Clemens Ladisch3d009412009-10-22 09:04:09 +02001225static int snd_via686_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001227 struct via82xx *chip = snd_pcm_substream_chip(substream);
1228 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
1230
1231 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1232 return err;
1233 return 0;
1234}
1235
1236/*
Clemens Ladisch3d009412009-10-22 09:04:09 +02001237 * open callback for playback on via823x DXS
1238 */
1239static int snd_via8233_playback_open(struct snd_pcm_substream *substream)
1240{
1241 struct via82xx *chip = snd_pcm_substream_chip(substream);
1242 struct viadev *viadev;
1243 unsigned int stream;
1244 int err;
1245
1246 viadev = &chip->devs[chip->playback_devno + substream->number];
1247 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1248 return err;
1249 stream = viadev->reg_offset / 0x10;
1250 if (chip->dxs_controls[stream]) {
Clemens Ladisch395c61d2010-07-12 16:27:24 +02001251 chip->playback_volume[stream][0] =
1252 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1253 chip->playback_volume[stream][1] =
1254 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
Clemens Ladisch3d009412009-10-22 09:04:09 +02001255 chip->dxs_controls[stream]->vd[0].access &=
1256 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1257 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1258 SNDRV_CTL_EVENT_MASK_INFO,
1259 &chip->dxs_controls[stream]->id);
1260 }
1261 return 0;
1262}
1263
1264/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * open callback for playback on via823x multi-channel
1266 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001267static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001269 struct via82xx *chip = snd_pcm_substream_chip(substream);
1270 struct viadev *viadev = &chip->devs[chip->multi_devno];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int err;
1272 /* channels constraint for VIA8233A
1273 * 3 and 5 channels are not supported
1274 */
1275 static unsigned int channels[] = {
1276 1, 2, 4, 6
1277 };
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001278 static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 .count = ARRAY_SIZE(channels),
1280 .list = channels,
1281 .mask = 0,
1282 };
1283
1284 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1285 return err;
1286 substream->runtime->hw.channels_max = 6;
1287 if (chip->revision == VIA_REV_8233A)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001288 snd_pcm_hw_constraint_list(substream->runtime, 0,
1289 SNDRV_PCM_HW_PARAM_CHANNELS,
1290 &hw_constraints_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
1292}
1293
1294/*
1295 * open callback for capture on via686 and via823x
1296 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001297static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001299 struct via82xx *chip = snd_pcm_substream_chip(substream);
1300 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 return snd_via82xx_pcm_open(chip, viadev, substream);
1303}
1304
1305/*
1306 * close callback
1307 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001308static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001310 struct via82xx *chip = snd_pcm_substream_chip(substream);
1311 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct via_rate_lock *ratep;
1313
1314 /* release the rate lock */
1315 ratep = &chip->rates[viadev->direction];
1316 spin_lock_irq(&ratep->lock);
1317 ratep->used--;
1318 if (! ratep->used)
1319 ratep->rate = 0;
1320 spin_unlock_irq(&ratep->lock);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001321 if (! ratep->rate) {
1322 if (! viadev->direction) {
1323 snd_ac97_update_power(chip->ac97,
1324 AC97_PCM_FRONT_DAC_RATE, 0);
1325 snd_ac97_update_power(chip->ac97,
1326 AC97_PCM_SURR_DAC_RATE, 0);
1327 snd_ac97_update_power(chip->ac97,
1328 AC97_PCM_LFE_DAC_RATE, 0);
1329 } else
1330 snd_ac97_update_power(chip->ac97,
1331 AC97_PCM_LR_ADC_RATE, 0);
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 viadev->substream = NULL;
1334 return 0;
1335}
1336
Clemens Ladisch3d009412009-10-22 09:04:09 +02001337static int snd_via8233_playback_close(struct snd_pcm_substream *substream)
1338{
1339 struct via82xx *chip = snd_pcm_substream_chip(substream);
1340 struct viadev *viadev = substream->runtime->private_data;
1341 unsigned int stream;
1342
1343 stream = viadev->reg_offset / 0x10;
1344 if (chip->dxs_controls[stream]) {
1345 chip->dxs_controls[stream]->vd[0].access |=
1346 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1347 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1348 &chip->dxs_controls[stream]->id);
1349 }
1350 return snd_via82xx_pcm_close(substream);
1351}
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354/* via686 playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001355static struct snd_pcm_ops snd_via686_playback_ops = {
Clemens Ladisch3d009412009-10-22 09:04:09 +02001356 .open = snd_via686_playback_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 .close = snd_via82xx_pcm_close,
1358 .ioctl = snd_pcm_lib_ioctl,
1359 .hw_params = snd_via82xx_hw_params,
1360 .hw_free = snd_via82xx_hw_free,
1361 .prepare = snd_via686_playback_prepare,
1362 .trigger = snd_via82xx_pcm_trigger,
1363 .pointer = snd_via686_pcm_pointer,
1364 .page = snd_pcm_sgbuf_ops_page,
1365};
1366
1367/* via686 capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001368static struct snd_pcm_ops snd_via686_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 .open = snd_via82xx_capture_open,
1370 .close = snd_via82xx_pcm_close,
1371 .ioctl = snd_pcm_lib_ioctl,
1372 .hw_params = snd_via82xx_hw_params,
1373 .hw_free = snd_via82xx_hw_free,
1374 .prepare = snd_via686_capture_prepare,
1375 .trigger = snd_via82xx_pcm_trigger,
1376 .pointer = snd_via686_pcm_pointer,
1377 .page = snd_pcm_sgbuf_ops_page,
1378};
1379
1380/* via823x DSX playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001381static struct snd_pcm_ops snd_via8233_playback_ops = {
Clemens Ladisch3d009412009-10-22 09:04:09 +02001382 .open = snd_via8233_playback_open,
1383 .close = snd_via8233_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .ioctl = snd_pcm_lib_ioctl,
1385 .hw_params = snd_via82xx_hw_params,
1386 .hw_free = snd_via82xx_hw_free,
1387 .prepare = snd_via8233_playback_prepare,
1388 .trigger = snd_via82xx_pcm_trigger,
1389 .pointer = snd_via8233_pcm_pointer,
1390 .page = snd_pcm_sgbuf_ops_page,
1391};
1392
1393/* via823x multi-channel playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001394static struct snd_pcm_ops snd_via8233_multi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 .open = snd_via8233_multi_open,
1396 .close = snd_via82xx_pcm_close,
1397 .ioctl = snd_pcm_lib_ioctl,
1398 .hw_params = snd_via82xx_hw_params,
1399 .hw_free = snd_via82xx_hw_free,
1400 .prepare = snd_via8233_multi_prepare,
1401 .trigger = snd_via82xx_pcm_trigger,
1402 .pointer = snd_via8233_pcm_pointer,
1403 .page = snd_pcm_sgbuf_ops_page,
1404};
1405
1406/* via823x capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001407static struct snd_pcm_ops snd_via8233_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 .open = snd_via82xx_capture_open,
1409 .close = snd_via82xx_pcm_close,
1410 .ioctl = snd_pcm_lib_ioctl,
1411 .hw_params = snd_via82xx_hw_params,
1412 .hw_free = snd_via82xx_hw_free,
1413 .prepare = snd_via8233_capture_prepare,
1414 .trigger = snd_via82xx_pcm_trigger,
1415 .pointer = snd_via8233_pcm_pointer,
1416 .page = snd_pcm_sgbuf_ops_page,
1417};
1418
1419
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001420static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1421 int shadow_pos, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 chip->devs[idx].reg_offset = reg_offset;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001424 chip->devs[idx].shadow_shift = shadow_pos * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 chip->devs[idx].direction = direction;
1426 chip->devs[idx].port = chip->port + reg_offset;
1427}
1428
1429/*
1430 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1431 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001432static int __devinit snd_via8233_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001434 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int i, err;
1436
1437 chip->playback_devno = 0; /* x 4 */
1438 chip->multi_devno = 4; /* x 1 */
1439 chip->capture_devno = 5; /* x 2 */
1440 chip->num_devs = 7;
1441 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1442
1443 /* PCM #0: 4 DSX playbacks and 1 capture */
1444 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1445 if (err < 0)
1446 return err;
1447 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1448 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1449 pcm->private_data = chip;
1450 strcpy(pcm->name, chip->card->shortname);
1451 chip->pcms[0] = pcm;
1452 /* set up playbacks */
1453 for (i = 0; i < 4; i++)
Karsten Wiese4f550df2005-10-18 14:31:07 +02001454 init_viadev(chip, i, 0x10 * i, i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001456 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Takashi Iwaia2e39612008-08-21 14:17:38 +02001458 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1459 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001460 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 /* PCM #1: multi-channel playback and 2nd capture */
1463 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1464 if (err < 0)
1465 return err;
1466 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1467 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1468 pcm->private_data = chip;
1469 strcpy(pcm->name, chip->card->shortname);
1470 chip->pcms[1] = pcm;
1471 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001472 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* set up capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001474 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Takashi Iwaia2e39612008-08-21 14:17:38 +02001476 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1477 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001478 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480}
1481
1482/*
1483 * create pcm instances for VIA8233A
1484 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001485static int __devinit snd_via8233a_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001487 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 int err;
1489
1490 chip->multi_devno = 0;
1491 chip->playback_devno = 1;
1492 chip->capture_devno = 2;
1493 chip->num_devs = 3;
1494 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1495
1496 /* PCM #0: multi-channel playback and capture */
1497 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1498 if (err < 0)
1499 return err;
1500 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1501 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1502 pcm->private_data = chip;
1503 strcpy(pcm->name, chip->card->shortname);
1504 chip->pcms[0] = pcm;
1505 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001506 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001508 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Takashi Iwaia2e39612008-08-21 14:17:38 +02001510 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1511 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001512 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /* SPDIF supported? */
1515 if (! ac97_can_spdif(chip->ac97))
1516 return 0;
1517
1518 /* PCM #1: DXS3 playback (for spdif) */
1519 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1520 if (err < 0)
1521 return err;
1522 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1523 pcm->private_data = chip;
1524 strcpy(pcm->name, chip->card->shortname);
1525 chip->pcms[1] = pcm;
1526 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001527 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Takashi Iwaia2e39612008-08-21 14:17:38 +02001529 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1530 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001531 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return 0;
1533}
1534
1535/*
1536 * create a pcm instance for via686a/b
1537 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001538static int __devinit snd_via686_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001540 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 int err;
1542
1543 chip->playback_devno = 0;
1544 chip->capture_devno = 1;
1545 chip->num_devs = 2;
1546 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1547
1548 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1549 if (err < 0)
1550 return err;
1551 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1552 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1553 pcm->private_data = chip;
1554 strcpy(pcm->name, chip->card->shortname);
1555 chip->pcms[0] = pcm;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001556 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1557 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Takashi Iwaia2e39612008-08-21 14:17:38 +02001559 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1560 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001561 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return 0;
1563}
1564
1565
1566/*
1567 * Mixer part
1568 */
1569
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001570static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 /* formerly they were "Line" and "Mic", but it looks like that they
1574 * have nothing to do with the actual physical connections...
1575 */
1576 static char *texts[2] = {
1577 "Input1", "Input2"
1578 };
1579 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1580 uinfo->count = 1;
1581 uinfo->value.enumerated.items = 2;
1582 if (uinfo->value.enumerated.item >= 2)
1583 uinfo->value.enumerated.item = 1;
1584 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1585 return 0;
1586}
1587
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001588static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001591 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1593 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1594 return 0;
1595}
1596
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001597static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1598 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001600 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1602 u8 val, oval;
1603
1604 spin_lock_irq(&chip->reg_lock);
1605 oval = inb(port);
1606 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1607 if (ucontrol->value.enumerated.item[0])
1608 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1609 if (val != oval)
1610 outb(val, port);
1611 spin_unlock_irq(&chip->reg_lock);
1612 return val != oval;
1613}
1614
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001615static struct snd_kcontrol_new snd_via8233_capture_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 .name = "Input Source Select",
1617 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1618 .info = snd_via8233_capture_source_info,
1619 .get = snd_via8233_capture_source_get,
1620 .put = snd_via8233_capture_source_put,
1621};
1622
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001623#define snd_via8233_dxs3_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001625static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1626 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001628 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 u8 val;
1630
1631 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1632 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1633 return 0;
1634}
1635
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001636static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1637 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001639 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 u8 val, oval;
1641
1642 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1643 val = oval & ~VIA8233_SPDIF_DX3;
1644 if (ucontrol->value.integer.value[0])
1645 val |= VIA8233_SPDIF_DX3;
1646 /* save the spdif flag for rate filtering */
1647 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1648 if (val != oval) {
1649 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1650 return 1;
1651 }
1652 return 0;
1653}
1654
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001655static struct snd_kcontrol_new snd_via8233_dxs3_spdif_control __devinitdata = {
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001656 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1658 .info = snd_via8233_dxs3_spdif_info,
1659 .get = snd_via8233_dxs3_spdif_get,
1660 .put = snd_via8233_dxs3_spdif_put,
1661};
1662
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001663static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1667 uinfo->count = 2;
1668 uinfo->value.integer.min = 0;
1669 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1670 return 0;
1671}
1672
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001673static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1674 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001676 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001677 unsigned int idx = kcontrol->id.subdevice;
Honza Maly00f226d2005-10-14 18:18:01 +02001678
1679 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1680 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1681 return 0;
1682}
1683
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001684static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1685 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001686{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001687 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001688 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1689 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691}
1692
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001693static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1694 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001696 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001697 unsigned int idx = kcontrol->id.subdevice;
Honza Maly00f226d2005-10-14 18:18:01 +02001698 unsigned long port = chip->port + 0x10 * idx;
1699 unsigned char val;
1700 int i, change = 0;
1701
1702 for (i = 0; i < 2; i++) {
1703 val = ucontrol->value.integer.value[i];
1704 if (val > VIA_DXS_MAX_VOLUME)
1705 val = VIA_DXS_MAX_VOLUME;
1706 val = VIA_DXS_MAX_VOLUME - val;
1707 change |= val != chip->playback_volume[idx][i];
1708 if (change) {
1709 chip->playback_volume[idx][i] = val;
1710 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1711 }
1712 }
1713 return change;
1714}
1715
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001716static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001718{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001719 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 unsigned int idx;
1721 unsigned char val;
1722 int i, change = 0;
1723
1724 for (i = 0; i < 2; i++) {
1725 val = ucontrol->value.integer.value[i];
1726 if (val > VIA_DXS_MAX_VOLUME)
1727 val = VIA_DXS_MAX_VOLUME;
1728 val = VIA_DXS_MAX_VOLUME - val;
Honza Maly00f226d2005-10-14 18:18:01 +02001729 if (val != chip->playback_volume_c[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 change = 1;
Honza Maly00f226d2005-10-14 18:18:01 +02001731 chip->playback_volume_c[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 for (idx = 0; idx < 4; idx++) {
1733 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02001734 chip->playback_volume[idx][i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1736 }
1737 }
1738 }
1739 return change;
1740}
1741
Clemens Ladischb452e082009-05-05 15:40:12 +02001742static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -4650, 150, 1);
Takashi Iwai7058c042006-08-21 18:44:54 +02001743
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001744static struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .name = "PCM Playback Volume",
1746 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwai7058c042006-08-21 18:44:54 +02001747 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1748 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 .info = snd_via8233_dxs_volume_info,
Honza Maly00f226d2005-10-14 18:18:01 +02001750 .get = snd_via8233_pcmdxs_volume_get,
1751 .put = snd_via8233_pcmdxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001752 .tlv = { .p = db_scale_dxs }
Honza Maly00f226d2005-10-14 18:18:01 +02001753};
1754
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001755static struct snd_kcontrol_new snd_via8233_dxs_volume_control __devinitdata = {
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001756 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1757 .device = 0,
1758 /* .subdevice set later */
1759 .name = "PCM Playback Volume",
Clemens Ladisch3d009412009-10-22 09:04:09 +02001760 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1761 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1762 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Honza Maly00f226d2005-10-14 18:18:01 +02001763 .info = snd_via8233_dxs_volume_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 .get = snd_via8233_dxs_volume_get,
1765 .put = snd_via8233_dxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001766 .tlv = { .p = db_scale_dxs }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767};
1768
1769/*
1770 */
1771
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001772static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001774 struct via82xx *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 chip->ac97_bus = NULL;
1776}
1777
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001778static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001780 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 chip->ac97 = NULL;
1782}
1783
1784static struct ac97_quirk ac97_quirks[] = {
1785 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001786 .subvendor = 0x1106,
1787 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .codec_id = 0x56494161, /* VT1612A */
1789 .name = "Soltek SL-75DRV5",
1790 .type = AC97_TUNE_NONE
1791 },
1792 { /* FIXME: which codec? */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001793 .subvendor = 0x1106,
1794 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 .name = "ASRock K7VT2",
1796 .type = AC97_TUNE_HP_ONLY
1797 },
1798 {
Clemens Ladischbf30a432010-02-22 10:33:13 +01001799 .subvendor = 0x110a,
1800 .subdevice = 0x0079,
1801 .name = "Fujitsu Siemens D1289",
1802 .type = AC97_TUNE_HP_ONLY
1803 },
1804 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001805 .subvendor = 0x1019,
1806 .subdevice = 0x0a81,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 .name = "ECS K7VTA3",
1808 .type = AC97_TUNE_HP_ONLY
1809 },
1810 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001811 .subvendor = 0x1019,
1812 .subdevice = 0x0a85,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 .name = "ECS L7VMM2",
1814 .type = AC97_TUNE_HP_ONLY
1815 },
1816 {
Walter Sheets942fd1e2008-06-27 11:53:31 +02001817 .subvendor = 0x1019,
1818 .subdevice = 0x1841,
1819 .name = "ECS K7VTA3",
1820 .type = AC97_TUNE_HP_ONLY
1821 },
1822 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001823 .subvendor = 0x1849,
1824 .subdevice = 0x3059,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 .name = "ASRock K7VM2",
1826 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1827 },
1828 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001829 .subvendor = 0x14cd,
1830 .subdevice = 0x7002,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 .name = "Unknown",
1832 .type = AC97_TUNE_ALC_JACK
1833 },
1834 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001835 .subvendor = 0x1071,
1836 .subdevice = 0x8590,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 .name = "Mitac Mobo",
1838 .type = AC97_TUNE_ALC_JACK
1839 },
1840 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001841 .subvendor = 0x161f,
1842 .subdevice = 0x202b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 .name = "Arima Notebook",
1844 .type = AC97_TUNE_HP_ONLY,
1845 },
Takashi Iwaidac8ddd2005-12-14 12:00:46 +01001846 {
1847 .subvendor = 0x161f,
1848 .subdevice = 0x2032,
1849 .name = "Targa Traveller 811",
1850 .type = AC97_TUNE_HP_ONLY,
1851 },
Daniel T Chend4199f02006-06-16 16:21:54 +02001852 {
1853 .subvendor = 0x161f,
1854 .subdevice = 0x2032,
1855 .name = "m680x",
1856 .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1857 },
Takashi Iwai96745132007-10-25 11:46:24 +02001858 {
1859 .subvendor = 0x1297,
1860 .subdevice = 0xa232,
1861 .name = "Shuttle AK32VN",
1862 .type = AC97_TUNE_HP_ONLY
1863 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 { } /* terminator */
1865};
1866
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001867static int __devinit snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001869 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001871 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 .write = snd_via82xx_codec_write,
1873 .read = snd_via82xx_codec_read,
1874 .wait = snd_via82xx_codec_wait,
1875 };
1876
1877 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1878 return err;
1879 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1880 chip->ac97_bus->clock = chip->ac97_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 memset(&ac97, 0, sizeof(ac97));
1883 ac97.private_data = chip;
1884 ac97.private_free = snd_via82xx_mixer_free_ac97;
1885 ac97.pci = chip->pci;
Takashi Iwaif1a63a32006-10-24 18:25:29 +02001886 ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1888 return err;
1889
1890 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1891
1892 if (chip->chip_type != TYPE_VIA686) {
1893 /* use slot 10/11 */
1894 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1895 }
1896
1897 return 0;
1898}
1899
1900#ifdef SUPPORT_JOYSTICK
1901#define JOYSTICK_ADDR 0x200
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001902static int __devinit snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct gameport *gp;
1905 struct resource *r;
1906
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001907 if (!joystick)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return -ENODEV;
1909
1910 r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1911 if (!r) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001912 printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n",
1913 JOYSTICK_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return -EBUSY;
1915 }
1916
1917 chip->gameport = gp = gameport_allocate_port();
1918 if (!gp) {
1919 printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02001920 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return -ENOMEM;
1922 }
1923
1924 gameport_set_name(gp, "VIA686 Gameport");
1925 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1926 gameport_set_dev_parent(gp, &chip->pci->dev);
1927 gp->io = JOYSTICK_ADDR;
1928 gameport_set_port_data(gp, r);
1929
1930 /* Enable legacy joystick port */
1931 *legacy |= VIA_FUNC_ENABLE_GAME;
1932 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1933
1934 gameport_register_port(chip->gameport);
1935
1936 return 0;
1937}
1938
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001939static void snd_via686_free_gameport(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 if (chip->gameport) {
1942 struct resource *r = gameport_get_port_data(chip->gameport);
1943
1944 gameport_unregister_port(chip->gameport);
1945 chip->gameport = NULL;
Takashi Iwaib1d57762005-10-10 11:56:31 +02001946 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948}
1949#else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001950static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 return -ENOSYS;
1953}
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001954static inline void snd_via686_free_gameport(struct via82xx *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955#endif
1956
1957
1958/*
1959 *
1960 */
1961
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001962static int __devinit snd_via8233_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 int i, err, caps;
1965 unsigned char val;
1966
1967 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1968 for (i = 0; i < caps; i++) {
1969 snd_via8233_capture_source.index = i;
1970 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1971 if (err < 0)
1972 return err;
1973 }
1974 if (ac97_can_spdif(chip->ac97)) {
1975 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1976 if (err < 0)
1977 return err;
1978 }
1979 if (chip->chip_type != TYPE_VIA8233A) {
1980 /* when no h/w PCM volume control is found, use DXS volume control
1981 * as the PCM vol control
1982 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001983 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 memset(&sid, 0, sizeof(sid));
1985 strcpy(sid.name, "PCM Playback Volume");
1986 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1987 if (! snd_ctl_find_id(chip->card, &sid)) {
Honza Maly00f226d2005-10-14 18:18:01 +02001988 snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
1989 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1990 if (err < 0)
1991 return err;
1992 }
1993 else /* Using DXS when PCM emulation is enabled is really weird */
1994 {
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001995 for (i = 0; i < 4; ++i) {
1996 struct snd_kcontrol *kctl;
1997
1998 kctl = snd_ctl_new1(
1999 &snd_via8233_dxs_volume_control, chip);
2000 if (!kctl)
2001 return -ENOMEM;
2002 kctl->id.subdevice = i;
2003 err = snd_ctl_add(chip->card, kctl);
2004 if (err < 0)
2005 return err;
Clemens Ladisch3d009412009-10-22 09:04:09 +02002006 chip->dxs_controls[i] = kctl;
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02002007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /* select spdif data slot 10/11 */
2011 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
2012 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
2013 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
2014 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
2015
2016 return 0;
2017}
2018
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002019static int __devinit snd_via686_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 unsigned char legacy, legacy_cfg;
2022 int rev_h = 0;
2023
2024 legacy = chip->old_legacy;
2025 legacy_cfg = chip->old_legacy_cfg;
2026 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
2027 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
2028 if (chip->revision >= VIA_REV_686_H) {
2029 rev_h = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002030 if (mpu_port >= 0x200) { /* force MIDI */
2031 mpu_port &= 0xfffc;
2032 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033#ifdef CONFIG_PM
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002034 chip->mpu_port_saved = mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035#endif
2036 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002037 mpu_port = pci_resource_start(chip->pci, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002040 switch (mpu_port) { /* force MIDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 case 0x300:
2042 case 0x310:
2043 case 0x320:
2044 case 0x330:
2045 legacy_cfg &= ~(3 << 2);
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002046 legacy_cfg |= (mpu_port & 0x0030) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 break;
2048 default: /* no, use BIOS settings */
2049 if (legacy & VIA_FUNC_ENABLE_MIDI)
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002050 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 break;
2052 }
2053 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002054 if (mpu_port >= 0x200 &&
2055 (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
2056 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (rev_h)
2058 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
2059 legacy |= VIA_FUNC_ENABLE_MIDI;
2060 } else {
2061 if (rev_h)
2062 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
2063 legacy &= ~VIA_FUNC_ENABLE_MIDI;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002064 mpu_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
2067 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2068 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
2069 if (chip->mpu_res) {
2070 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
Takashi Iwai302e4c22006-05-23 13:24:30 +02002071 mpu_port, MPU401_INFO_INTEGRATED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 chip->irq, 0, &chip->rmidi) < 0) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002073 printk(KERN_WARNING "unable to initialize MPU-401"
2074 " at 0x%lx, skipping\n", mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 legacy &= ~VIA_FUNC_ENABLE_MIDI;
2076 } else {
2077 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
2078 }
2079 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2080 }
2081
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002082 snd_via686_create_gameport(chip, &legacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084#ifdef CONFIG_PM
2085 chip->legacy_saved = legacy;
2086 chip->legacy_cfg_saved = legacy_cfg;
2087#endif
2088
2089 return 0;
2090}
2091
2092
2093/*
2094 * proc interface
2095 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002096static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2097 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002099 struct via82xx *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 int i;
2101
2102 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2103 for (i = 0; i < 0xa0; i += 4) {
2104 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2105 }
2106}
2107
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002108static void __devinit snd_via82xx_proc_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002110 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002113 snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116/*
2117 *
2118 */
2119
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002120static int snd_via82xx_chip_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 unsigned int val;
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002123 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 unsigned char pval;
2125
2126#if 0 /* broken on K7M? */
2127 if (chip->chip_type == TYPE_VIA686)
2128 /* disable all legacy ports */
2129 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2130#endif
2131 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2132 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2133 /* deassert ACLink reset, force SYNC */
2134 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2135 VIA_ACLINK_CTRL_ENABLE |
2136 VIA_ACLINK_CTRL_RESET |
2137 VIA_ACLINK_CTRL_SYNC);
2138 udelay(100);
2139#if 1 /* FIXME: should we do full reset here for all chip models? */
2140 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2141 udelay(100);
2142#else
2143 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2144 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2145 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2146 udelay(2);
2147#endif
2148 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2149 /* note - FM data out has trouble with non VRA codecs !! */
2150 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2151 udelay(100);
2152 }
2153
2154 /* Make sure VRA is enabled, in case we didn't do a
2155 * complete codec reset, above */
2156 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2157 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2158 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2159 /* note - FM data out has trouble with non VRA codecs !! */
2160 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2161 udelay(100);
2162 }
2163
2164 /* wait until codec ready */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002165 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 do {
2167 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2168 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2169 break;
Rene Hermand86d0192007-09-18 18:10:49 +02002170 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002171 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
Takashi Iwai99b359b2005-10-20 18:26:44 +02002174 snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2177 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2178 VIA_REG_AC97_SECONDARY_VALID |
2179 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002180 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2182 VIA_REG_AC97_SECONDARY_VALID |
2183 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2184 do {
2185 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2186 chip->ac97_secondary = 1;
2187 goto __ac97_ok2;
2188 }
Rene Hermand86d0192007-09-18 18:10:49 +02002189 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002190 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* This is ok, the most of motherboards have only one codec */
2192
2193 __ac97_ok2:
2194#endif
2195
2196 if (chip->chip_type == TYPE_VIA686) {
2197 /* route FM trap to IRQ, disable FM trap */
2198 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2199 /* disable all GPI interrupts */
2200 outl(0, VIAREG(chip, GPI_INTR));
2201 }
2202
2203 if (chip->chip_type != TYPE_VIA686) {
2204 /* Workaround for Award BIOS bug:
2205 * DXS channels don't work properly with VRA if MC97 is disabled.
2206 */
2207 struct pci_dev *pci;
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002208 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (pci) {
2210 unsigned char data;
2211 pci_read_config_byte(pci, 0x44, &data);
2212 pci_write_config_byte(pci, 0x44, data | 0x40);
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002213 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 }
2216
2217 if (chip->chip_type != TYPE_VIA8233A) {
2218 int i, idx;
2219 for (idx = 0; idx < 4; idx++) {
2220 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02002221 for (i = 0; i < 2; i++) {
2222 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002223 outb(chip->playback_volume_c[i],
2224 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
Honza Maly00f226d2005-10-14 18:18:01 +02002225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227 }
2228
2229 return 0;
2230}
2231
2232#ifdef CONFIG_PM
2233/*
2234 * power management
2235 */
Takashi Iwai57feb832005-11-17 16:10:35 +01002236static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Takashi Iwai57feb832005-11-17 16:10:35 +01002238 struct snd_card *card = pci_get_drvdata(pci);
2239 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 int i;
2241
Takashi Iwai57feb832005-11-17 16:10:35 +01002242 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 for (i = 0; i < 2; i++)
Takashi Iwai57feb832005-11-17 16:10:35 +01002244 snd_pcm_suspend_all(chip->pcms[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 for (i = 0; i < chip->num_devs; i++)
2246 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2247 synchronize_irq(chip->irq);
2248 snd_ac97_suspend(chip->ac97);
2249
2250 /* save misc values */
2251 if (chip->chip_type != TYPE_VIA686) {
2252 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2253 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2254 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2255 }
2256
Takashi Iwai57feb832005-11-17 16:10:35 +01002257 pci_disable_device(pci);
2258 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002259 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return 0;
2261}
2262
Takashi Iwai57feb832005-11-17 16:10:35 +01002263static int snd_via82xx_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Takashi Iwai57feb832005-11-17 16:10:35 +01002265 struct snd_card *card = pci_get_drvdata(pci);
2266 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 int i;
2268
Takashi Iwai57feb832005-11-17 16:10:35 +01002269 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02002270 pci_restore_state(pci);
2271 if (pci_enable_device(pci) < 0) {
2272 printk(KERN_ERR "via82xx: pci_enable_device failed, "
2273 "disabling device\n");
2274 snd_card_disconnect(card);
2275 return -EIO;
2276 }
2277 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 snd_via82xx_chip_init(chip);
2280
2281 if (chip->chip_type == TYPE_VIA686) {
2282 if (chip->mpu_port_saved)
2283 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2284 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2285 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2286 } else {
2287 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2288 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2289 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2290 }
2291
2292 snd_ac97_resume(chip->ac97);
2293
2294 for (i = 0; i < chip->num_devs; i++)
2295 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2296
Takashi Iwai57feb832005-11-17 16:10:35 +01002297 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return 0;
2299}
2300#endif /* CONFIG_PM */
2301
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002302static int snd_via82xx_free(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 unsigned int i;
2305
2306 if (chip->irq < 0)
2307 goto __end_hw;
2308 /* disable interrupts */
2309 for (i = 0; i < chip->num_devs; i++)
2310 snd_via82xx_channel_reset(chip, &chip->devs[i]);
Jeff Garzikf000fd82008-04-22 13:50:34 +02002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (chip->irq >= 0)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002313 free_irq(chip->irq, chip);
Marcin Åšlusarz757d5a72007-12-14 12:51:24 +01002314 __end_hw:
Takashi Iwaib1d57762005-10-10 11:56:31 +02002315 release_and_free_resource(chip->mpu_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 pci_release_regions(chip->pci);
2317
2318 if (chip->chip_type == TYPE_VIA686) {
2319 snd_via686_free_gameport(chip);
2320 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2321 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2322 }
2323 pci_disable_device(chip->pci);
2324 kfree(chip);
2325 return 0;
2326}
2327
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002328static int snd_via82xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002330 struct via82xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return snd_via82xx_free(chip);
2332}
2333
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002334static int __devinit snd_via82xx_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct pci_dev *pci,
2336 int chip_type,
2337 int revision,
2338 unsigned int ac97_clock,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002339 struct via82xx ** r_via)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002341 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002343 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 .dev_free = snd_via82xx_dev_free,
2345 };
2346
2347 if ((err = pci_enable_device(pci)) < 0)
2348 return err;
2349
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002350 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 pci_disable_device(pci);
2352 return -ENOMEM;
2353 }
2354
2355 chip->chip_type = chip_type;
2356 chip->revision = revision;
2357
2358 spin_lock_init(&chip->reg_lock);
2359 spin_lock_init(&chip->rates[0].lock);
2360 spin_lock_init(&chip->rates[1].lock);
2361 chip->card = card;
2362 chip->pci = pci;
2363 chip->irq = -1;
2364
2365 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2366 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2367 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2368 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2369
2370 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2371 kfree(chip);
2372 pci_disable_device(pci);
2373 return err;
2374 }
2375 chip->port = pci_resource_start(pci, 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +02002376 if (request_irq(pci->irq,
2377 chip_type == TYPE_VIA8233 ?
2378 snd_via8233_interrupt : snd_via686_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002379 IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002380 KBUILD_MODNAME, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002381 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 snd_via82xx_free(chip);
2383 return -EBUSY;
2384 }
2385 chip->irq = pci->irq;
2386 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2387 chip->ac97_clock = ac97_clock;
2388 synchronize_irq(chip->irq);
2389
2390 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2391 snd_via82xx_free(chip);
2392 return err;
2393 }
2394
2395 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2396 snd_via82xx_free(chip);
2397 return err;
2398 }
2399
2400 /* The 8233 ac97 controller does not implement the master bit
2401 * in the pci command register. IMHO this is a violation of the PCI spec.
2402 * We call pci_set_master here because it does not hurt. */
2403 pci_set_master(pci);
2404
2405 snd_card_set_dev(card, &pci->dev);
2406
2407 *r_via = chip;
2408 return 0;
2409}
2410
2411struct via823x_info {
2412 int revision;
2413 char *name;
2414 int type;
2415};
2416static struct via823x_info via823x_cards[] __devinitdata = {
2417 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2418 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2419 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2420 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2421 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2422 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
Bastiaan Jacques8263c652006-04-18 17:04:04 +02002423 { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424};
2425
2426/*
2427 * auto detection of DXS channel supports.
2428 */
Takashi Iwai9d749582006-11-24 15:37:18 +01002429
2430static struct snd_pci_quirk dxs_whitelist[] __devinitdata = {
2431 SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2432 SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2433 SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2434 SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002435 SND_PCI_QUIRK_VENDOR(0x1019, "ESC K8", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002436 SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2437 SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2438 SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002439 SND_PCI_QUIRK_VENDOR(0x1043, "ASUS A7/A8", VIA_DXS_NO_VRA),
2440 SND_PCI_QUIRK_VENDOR(0x1071, "Diverse Notebook", VIA_DXS_NO_VRA),
Takashi Iwai9d749582006-11-24 15:37:18 +01002441 SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002442 SND_PCI_QUIRK_VENDOR(0x1106, "ASRock", VIA_DXS_SRC),
Takashi Iwai11be2652007-10-11 14:35:00 +02002443 SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
Takashi Iwai96745132007-10-25 11:46:24 +02002444 SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2445 SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002446 SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2447 SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2448 SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2449 SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002450 SND_PCI_QUIRK_VENDOR(0x1462, "MSI Mobo", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002451 SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2452 SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2453 SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2454 SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2455 SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2456 SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2457 SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2458 SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2459 SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2460 SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2461 SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2462 SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2463 SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002464 SND_PCI_QUIRK_VENDOR(0x1695, "EPoX mobo", VIA_DXS_SRC),
2465 SND_PCI_QUIRK_VENDOR(0x16f3, "Jetway K8", VIA_DXS_SRC),
2466 SND_PCI_QUIRK_VENDOR(0x1734, "FSC Laptop", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002467 SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002468 SND_PCI_QUIRK_VENDOR(0x1849, "ASRock mobo", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002469 SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8", VIA_DXS_NO_VRA),
2470 SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2471 { } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472};
2473
Takashi Iwaia7695772006-04-27 16:56:07 +02002474static int __devinit check_dxs_list(struct pci_dev *pci, int revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Takashi Iwai9d749582006-11-24 15:37:18 +01002476 const struct snd_pci_quirk *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Takashi Iwai9d749582006-11-24 15:37:18 +01002478 w = snd_pci_quirk_lookup(pci, dxs_whitelist);
2479 if (w) {
2480 snd_printdd(KERN_INFO "via82xx: DXS white list for %s found\n",
2481 w->name);
2482 return w->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
Takashi Iwaia7695772006-04-27 16:56:07 +02002485 /* for newer revision, default to DXS_SRC */
2486 if (revision >= VIA_REV_8235)
2487 return VIA_DXS_SRC;
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /*
2490 * not detected, try 48k rate only to be sure.
2491 */
2492 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002493 printk(KERN_INFO " Please try dxs_support=5 option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 printk(KERN_INFO " and report if it works on your machine.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002495 printk(KERN_INFO " For more details, read ALSA-Configuration.txt.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return VIA_DXS_48K;
2497};
2498
2499static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2500 const struct pci_device_id *pci_id)
2501{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002502 struct snd_card *card;
2503 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 int chip_type = 0, card_type;
2505 unsigned int i;
2506 int err;
2507
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002508 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
2509 if (err < 0)
2510 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 card_type = pci_id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 switch (card_type) {
2514 case TYPE_CARD_VIA686:
2515 strcpy(card->driver, "VIA686A");
Auke Kok44c10132007-06-08 15:46:36 -07002516 sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 chip_type = TYPE_VIA686;
2518 break;
2519 case TYPE_CARD_VIA8233:
2520 chip_type = TYPE_VIA8233;
Auke Kok44c10132007-06-08 15:46:36 -07002521 sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
Auke Kok44c10132007-06-08 15:46:36 -07002523 if (pci->revision == via823x_cards[i].revision) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 chip_type = via823x_cards[i].type;
2525 strcpy(card->shortname, via823x_cards[i].name);
2526 break;
2527 }
2528 }
2529 if (chip_type != TYPE_VIA8233A) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002530 if (dxs_support == VIA_DXS_AUTO)
Auke Kok44c10132007-06-08 15:46:36 -07002531 dxs_support = check_dxs_list(pci, pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /* force to use VIA8233 or 8233A model according to
2533 * dxs_support module option
2534 */
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002535 if (dxs_support == VIA_DXS_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 chip_type = TYPE_VIA8233A;
2537 else
2538 chip_type = TYPE_VIA8233;
2539 }
2540 if (chip_type == TYPE_VIA8233A)
2541 strcpy(card->driver, "VIA8233A");
Auke Kok44c10132007-06-08 15:46:36 -07002542 else if (pci->revision >= VIA_REV_8237)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2544 else
2545 strcpy(card->driver, "VIA8233");
2546 break;
2547 default:
2548 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2549 err = -EINVAL;
2550 goto __error;
2551 }
2552
Auke Kok44c10132007-06-08 15:46:36 -07002553 if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002554 ac97_clock, &chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 goto __error;
Takashi Iwai57feb832005-11-17 16:10:35 +01002556 card->private_data = chip;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002557 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 goto __error;
2559
2560 if (chip_type == TYPE_VIA686) {
2561 if ((err = snd_via686_pcm_new(chip)) < 0 ||
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002562 (err = snd_via686_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 goto __error;
2564 } else {
2565 if (chip_type == TYPE_VIA8233A) {
2566 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2567 goto __error;
2568 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2569 } else {
2570 if ((err = snd_via8233_pcm_new(chip)) < 0)
2571 goto __error;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002572 if (dxs_support == VIA_DXS_48K)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 chip->dxs_fixed = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002574 else if (dxs_support == VIA_DXS_NO_VRA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 chip->no_vra = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002576 else if (dxs_support == VIA_DXS_SRC) {
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02002577 chip->no_vra = 1;
2578 chip->dxs_src = 1;
2579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002581 if ((err = snd_via8233_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 goto __error;
2583 }
2584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /* disable interrupts */
2586 for (i = 0; i < chip->num_devs; i++)
2587 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2588
2589 snprintf(card->longname, sizeof(card->longname),
2590 "%s with %s at %#lx, irq %d", card->shortname,
2591 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2592
2593 snd_via82xx_proc_init(chip);
2594
2595 if ((err = snd_card_register(card)) < 0) {
2596 snd_card_free(card);
2597 return err;
2598 }
2599 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
2601
2602 __error:
2603 snd_card_free(card);
2604 return err;
2605}
2606
2607static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2608{
2609 snd_card_free(pci_get_drvdata(pci));
2610 pci_set_drvdata(pci, NULL);
2611}
2612
2613static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002614 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 .id_table = snd_via82xx_ids,
2616 .probe = snd_via82xx_probe,
2617 .remove = __devexit_p(snd_via82xx_remove),
Takashi Iwai57feb832005-11-17 16:10:35 +01002618#ifdef CONFIG_PM
2619 .suspend = snd_via82xx_suspend,
2620 .resume = snd_via82xx_resume,
2621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622};
2623
2624static int __init alsa_card_via82xx_init(void)
2625{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002626 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
2629static void __exit alsa_card_via82xx_exit(void)
2630{
2631 pci_unregister_driver(&driver);
2632}
2633
2634module_init(alsa_card_via82xx_init)
2635module_exit(alsa_card_via82xx_exit)