blob: 08da9234efb38e9981c3183536403dc74f0a3058 [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 *
6 * Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
7 * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
8 * 2002 Takashi Iwai <tiwai@suse.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26/*
27 * Changes:
28 *
29 * Dec. 19, 2002 Takashi Iwai <tiwai@suse.de>
30 * - use the DSX channels for the first pcm playback.
31 * (on VIA8233, 8233C and 8235 only)
32 * this will allow you play simultaneously up to 4 streams.
33 * multi-channel playback is assigned to the second device
34 * on these chips.
35 * - support the secondary capture (on VIA8233/C,8235)
36 * - SPDIF support
37 * the DSX3 channel can be used for SPDIF output.
38 * on VIA8233A, this channel is assigned to the second pcm
39 * playback.
40 * the card config of alsa-lib will assign the correct
41 * device for applications.
42 * - clean up the code, separate low-level initialization
43 * routines for each chipset.
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
49#include <sound/driver.h>
50#include <asm/io.h>
51#include <linux/delay.h>
52#include <linux/interrupt.h>
53#include <linux/init.h>
54#include <linux/pci.h>
55#include <linux/slab.h>
56#include <linux/gameport.h>
57#include <linux/moduleparam.h>
58#include <sound/core.h>
59#include <sound/pcm.h>
60#include <sound/pcm_params.h>
61#include <sound/info.h>
62#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
70MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
71MODULE_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Clemens Ladischb7fe4622005-10-04 08:46:51 +020089module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020091module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020093module_param(mpu_port, long, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
95#ifdef SUPPORT_JOYSTICK
Clemens Ladischb7fe4622005-10-04 08:46:51 +020096module_param(joystick, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
98#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +020099module_param(ac97_clock, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200101module_param(ac97_quirk, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200103module_param(dxs_support, int, 0444);
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200104MODULE_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)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200106/* just for backward compatibility */
107static int enable;
Takashi Iwai698444f2005-10-20 16:53:49 +0200108module_param(enable, bool, 0444);
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* revision numbers for via686 */
112#define VIA_REV_686_A 0x10
113#define VIA_REV_686_B 0x11
114#define VIA_REV_686_C 0x12
115#define VIA_REV_686_D 0x13
116#define VIA_REV_686_E 0x14
117#define VIA_REV_686_H 0x20
118
119/* revision numbers for via8233 */
120#define VIA_REV_PRE_8233 0x10 /* not in market */
121#define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
122#define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
123#define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
124#define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
125#define VIA_REV_8237 0x60
Bastiaan Jacques8263c652006-04-18 17:04:04 +0200126#define VIA_REV_8251 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
129 * Direct registers
130 */
131
132#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
133#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
134
135/* common offsets */
136#define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
137#define VIA_REG_STAT_ACTIVE 0x80 /* RO */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200138#define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define VIA_REG_STAT_PAUSED 0x40 /* RO */
140#define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
141#define VIA_REG_STAT_STOPPED 0x04 /* RWC */
142#define VIA_REG_STAT_EOL 0x02 /* RWC */
143#define VIA_REG_STAT_FLAG 0x01 /* RWC */
144#define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
145#define VIA_REG_CTRL_START 0x80 /* WO */
146#define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
147#define VIA_REG_CTRL_AUTOSTART 0x20
148#define VIA_REG_CTRL_PAUSE 0x08 /* RW */
149#define VIA_REG_CTRL_INT_STOP 0x04
150#define VIA_REG_CTRL_INT_EOL 0x02
151#define VIA_REG_CTRL_INT_FLAG 0x01
152#define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
153#define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
154#define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
155#define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
156#define VIA_REG_TYPE_16BIT 0x20 /* RW */
157#define VIA_REG_TYPE_STEREO 0x10 /* RW */
158#define VIA_REG_TYPE_INT_LLINE 0x00
159#define VIA_REG_TYPE_INT_LSAMPLE 0x04
160#define VIA_REG_TYPE_INT_LESSONE 0x08
161#define VIA_REG_TYPE_INT_MASK 0x0c
162#define VIA_REG_TYPE_INT_EOL 0x02
163#define VIA_REG_TYPE_INT_FLAG 0x01
164#define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
165#define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
166#define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
167#define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
168#define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
169#define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
170#define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
171
172#define DEFINE_VIA_REGSET(name,val) \
173enum {\
174 VIA_REG_##name##_STATUS = (val),\
175 VIA_REG_##name##_CONTROL = (val) + 0x01,\
176 VIA_REG_##name##_TYPE = (val) + 0x02,\
177 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
178 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
179 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
180 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
181}
182
183/* playback block */
184DEFINE_VIA_REGSET(PLAYBACK, 0x00);
185DEFINE_VIA_REGSET(CAPTURE, 0x10);
186DEFINE_VIA_REGSET(FM, 0x20);
187
188/* AC'97 */
189#define VIA_REG_AC97 0x80 /* dword */
190#define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
191#define VIA_REG_AC97_CODEC_ID_SHIFT 30
192#define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
193#define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
194#define VIA_REG_AC97_SECONDARY_VALID (1<<27)
195#define VIA_REG_AC97_PRIMARY_VALID (1<<25)
196#define VIA_REG_AC97_BUSY (1<<24)
197#define VIA_REG_AC97_READ (1<<23)
198#define VIA_REG_AC97_CMD_SHIFT 16
199#define VIA_REG_AC97_CMD_MASK 0x7e
200#define VIA_REG_AC97_DATA_SHIFT 0
201#define VIA_REG_AC97_DATA_MASK 0xffff
202
203#define VIA_REG_SGD_SHADOW 0x84 /* dword */
204/* via686 */
205#define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
206#define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
207#define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
208#define VIA_REG_SGD_STAT_PB_EOL (1<<4)
209#define VIA_REG_SGD_STAT_CP_EOL (1<<5)
210#define VIA_REG_SGD_STAT_FM_EOL (1<<6)
211#define VIA_REG_SGD_STAT_PB_STOP (1<<8)
212#define VIA_REG_SGD_STAT_CP_STOP (1<<9)
213#define VIA_REG_SGD_STAT_FM_STOP (1<<10)
214#define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
215#define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
216#define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
217/* via8233 */
218#define VIA8233_REG_SGD_STAT_FLAG (1<<0)
219#define VIA8233_REG_SGD_STAT_EOL (1<<1)
220#define VIA8233_REG_SGD_STAT_STOP (1<<2)
221#define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
222#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
223#define VIA8233_REG_SGD_CHAN_SDX 0
224#define VIA8233_REG_SGD_CHAN_MULTI 4
225#define VIA8233_REG_SGD_CHAN_REC 6
226#define VIA8233_REG_SGD_CHAN_REC1 7
227
228#define VIA_REG_GPI_STATUS 0x88
229#define VIA_REG_GPI_INTR 0x8c
230
231/* multi-channel and capture registers for via8233 */
232DEFINE_VIA_REGSET(MULTPLAY, 0x40);
233DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
234
235/* via8233-specific registers */
236#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
237#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
238#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
239#define VIA_REG_MULTPLAY_FMT_8BIT 0x00
240#define VIA_REG_MULTPLAY_FMT_16BIT 0x80
241#define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
242#define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
243#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
244
245#define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
246
247#define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
248#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
249#define VIA_REG_CAPTURE_CHANNEL_LINE 0
250#define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
251
252#define VIA_TBL_BIT_FLAG 0x40000000
253#define VIA_TBL_BIT_EOL 0x80000000
254
255/* pci space */
256#define VIA_ACLINK_STAT 0x40
257#define VIA_ACLINK_C11_READY 0x20
258#define VIA_ACLINK_C10_READY 0x10
259#define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
260#define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
261#define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
262#define VIA_ACLINK_CTRL 0x41
263#define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
264#define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
265#define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
266#define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
267#define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
268#define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
269#define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
270#define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
271#define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
272 VIA_ACLINK_CTRL_RESET|\
273 VIA_ACLINK_CTRL_PCM|\
274 VIA_ACLINK_CTRL_VRA)
275#define VIA_FUNC_ENABLE 0x42
276#define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
277#define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
278#define VIA_FUNC_RX2C_WRITE 0x20
279#define VIA_FUNC_SB_FIFO_EMPTY 0x10
280#define VIA_FUNC_ENABLE_GAME 0x08
281#define VIA_FUNC_ENABLE_FM 0x04
282#define VIA_FUNC_ENABLE_MIDI 0x02
283#define VIA_FUNC_ENABLE_SB 0x01
284#define VIA_PNP_CONTROL 0x43
285#define VIA_FM_NMI_CTRL 0x48
286#define VIA8233_VOLCHG_CTRL 0x48
287#define VIA8233_SPDIF_CTRL 0x49
288#define VIA8233_SPDIF_DX3 0x08
289#define VIA8233_SPDIF_SLOT_MASK 0x03
290#define VIA8233_SPDIF_SLOT_1011 0x00
291#define VIA8233_SPDIF_SLOT_34 0x01
292#define VIA8233_SPDIF_SLOT_78 0x02
293#define VIA8233_SPDIF_SLOT_69 0x03
294
295/*
296 */
297
298#define VIA_DXS_AUTO 0
299#define VIA_DXS_ENABLE 1
300#define VIA_DXS_DISABLE 2
301#define VIA_DXS_48K 3
302#define VIA_DXS_NO_VRA 4
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200303#define VIA_DXS_SRC 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305
306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * pcm stream
308 */
309
310struct snd_via_sg_table {
311 unsigned int offset;
312 unsigned int size;
313} ;
314
315#define VIA_TABLE_SIZE 255
316
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100317struct viadev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned int reg_offset;
319 unsigned long port;
320 int direction; /* playback = 0, capture = 1 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100321 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int running;
323 unsigned int tbl_entries; /* # descriptors */
324 struct snd_dma_buffer table;
325 struct snd_via_sg_table *idx_table;
326 /* for recovery from the unexpected pointer */
327 unsigned int lastpos;
328 unsigned int fragsize;
329 unsigned int bufsize;
330 unsigned int bufsize2;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200331 int hwptr_done; /* processed frame position in the buffer */
332 int in_interrupt;
333 int shadow_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
336
337enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
338enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
339
340#define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
341
342struct via_rate_lock {
343 spinlock_t lock;
344 int rate;
345 int used;
346};
347
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100348struct via82xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int irq;
350
351 unsigned long port;
352 struct resource *mpu_res;
353 int chip_type;
354 unsigned char revision;
355
356 unsigned char old_legacy;
357 unsigned char old_legacy_cfg;
358#ifdef CONFIG_PM
359 unsigned char legacy_saved;
360 unsigned char legacy_cfg_saved;
361 unsigned char spdif_ctrl_saved;
362 unsigned char capture_src_saved[2];
363 unsigned int mpu_port_saved;
364#endif
365
Honza Maly00f226d2005-10-14 18:18:01 +0200366 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
367 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
370
371 struct pci_dev *pci;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100372 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 unsigned int num_devs;
375 unsigned int playback_devno, multi_devno, capture_devno;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100376 struct viadev devs[VIA_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct via_rate_lock rates[2]; /* playback and capture */
378 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
379 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200380 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
382
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100383 struct snd_pcm *pcms[2];
384 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100386 struct snd_ac97_bus *ac97_bus;
387 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned int ac97_clock;
389 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
390
391 spinlock_t reg_lock;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100392 struct snd_info_entry *proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394#ifdef SUPPORT_JOYSTICK
395 struct gameport *gameport;
396#endif
397};
398
Takashi Iwaif40b6892006-07-05 16:51:05 +0200399static struct pci_device_id snd_via82xx_ids[] = {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200400 /* 0x1106, 0x3058 */
401 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
402 /* 0x1106, 0x3059 */
403 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 { 0, }
405};
406
407MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
408
409/*
410 */
411
412/*
413 * allocate and initialize the descriptor buffers
414 * periods = number of periods
415 * fragsize = period size in bytes
416 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100417static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct pci_dev *pci,
419 unsigned int periods, unsigned int fragsize)
420{
421 unsigned int i, idx, ofs, rest;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100422 struct via82xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
424
425 if (dev->table.area == NULL) {
426 /* the start of each lists must be aligned to 8 bytes,
427 * but the kernel pages are much bigger, so we don't care
428 */
429 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
430 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
431 &dev->table) < 0)
432 return -ENOMEM;
433 }
434 if (! dev->idx_table) {
435 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
436 if (! dev->idx_table)
437 return -ENOMEM;
438 }
439
440 /* fill the entries */
441 idx = 0;
442 ofs = 0;
443 for (i = 0; i < periods; i++) {
444 rest = fragsize;
445 /* fill descriptors for a period.
446 * a period can be split to several descriptors if it's
447 * over page boundary.
448 */
449 do {
450 unsigned int r;
451 unsigned int flag;
452
453 if (idx >= VIA_TABLE_SIZE) {
454 snd_printk(KERN_ERR "via82xx: too much table size!\n");
455 return -EINVAL;
456 }
457 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
458 r = PAGE_SIZE - (ofs % PAGE_SIZE);
459 if (rest < r)
460 r = rest;
461 rest -= r;
462 if (! rest) {
463 if (i == periods - 1)
464 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
465 else
466 flag = VIA_TBL_BIT_FLAG; /* period boundary */
467 } else
468 flag = 0; /* period continues to the next */
469 // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
470 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
471 dev->idx_table[idx].offset = ofs;
472 dev->idx_table[idx].size = r;
473 ofs += r;
474 idx++;
475 } while (rest > 0);
476 }
477 dev->tbl_entries = idx;
478 dev->bufsize = periods * fragsize;
479 dev->bufsize2 = dev->bufsize / 2;
480 dev->fragsize = fragsize;
481 return 0;
482}
483
484
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100485static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct pci_dev *pci)
487{
488 if (dev->table.area) {
489 snd_dma_free_pages(&dev->table);
490 dev->table.area = NULL;
491 }
Jesper Juhl4d572772005-05-30 17:30:32 +0200492 kfree(dev->idx_table);
493 dev->idx_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495}
496
497/*
498 * Basic I/O
499 */
500
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100501static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 return inl(VIAREG(chip, AC97));
504}
505
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100506static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 outl(val, VIAREG(chip, AC97));
509}
510
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100511static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 unsigned int timeout = 1000; /* 1ms */
514 unsigned int val;
515
516 while (timeout-- > 0) {
517 udelay(1);
518 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
519 return val & 0xffff;
520 }
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100521 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
522 secondary, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EIO;
524}
525
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100526static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 unsigned int timeout = 1000; /* 1ms */
529 unsigned int val, val1;
530 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
531 VIA_REG_AC97_SECONDARY_VALID;
532
533 while (timeout-- > 0) {
534 val = snd_via82xx_codec_xread(chip);
535 val1 = val & (VIA_REG_AC97_BUSY | stat);
536 if (val1 == stat)
537 return val & 0xffff;
538 udelay(1);
539 }
540 return -EIO;
541}
542
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100543static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100545 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int err;
547 err = snd_via82xx_codec_ready(chip, ac97->num);
548 /* here we need to wait fairly for long time.. */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +0200549 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100552static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned short reg,
554 unsigned short val)
555{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100556 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned int xval;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
560 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
561 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
562 xval |= val << VIA_REG_AC97_DATA_SHIFT;
563 snd_via82xx_codec_xwrite(chip, xval);
564 snd_via82xx_codec_ready(chip, ac97->num);
565}
566
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100567static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100569 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned int xval, val = 0xffff;
571 int again = 0;
572
573 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
574 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
575 xval |= VIA_REG_AC97_READ;
576 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
577 while (1) {
578 if (again++ > 3) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100579 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
580 ac97->num, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0xffff;
582 }
583 snd_via82xx_codec_xwrite(chip, xval);
584 udelay (20);
585 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
586 udelay(25);
587 val = snd_via82xx_codec_xread(chip);
588 break;
589 }
590 }
591 return val & 0xffff;
592}
593
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100594static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
597 VIADEV_REG(viadev, OFFSET_CONTROL));
598 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
599 udelay(50);
600 /* disable interrupts */
601 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
602 /* clear interrupts */
603 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
604 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
605 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
606 viadev->lastpos = 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200607 viadev->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610
611/*
612 * Interrupt handler
Karsten Wiese4f550df2005-10-18 14:31:07 +0200613 * Used for 686 and 8233A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200615static irqreturn_t snd_via686_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100617 struct via82xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unsigned int status;
619 unsigned int i;
620
621 status = inl(VIAREG(chip, SGD_SHADOW));
622 if (! (status & chip->intr_mask)) {
623 if (chip->rmidi)
624 /* check mpu401 interrupt */
625 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
626 return IRQ_NONE;
627 }
628
629 /* check status for each stream */
630 spin_lock(&chip->reg_lock);
631 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100632 struct viadev *viadev = &chip->devs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
Karsten Wiese4f550df2005-10-18 14:31:07 +0200634 if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 continue;
636 if (viadev->substream && viadev->running) {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200637 /*
638 * Update hwptr_done based on 'period elapsed'
639 * interrupts. We'll use it, when the chip returns 0
640 * for OFFSET_CURR_COUNT.
641 */
642 if (c_status & VIA_REG_STAT_EOL)
643 viadev->hwptr_done = 0;
644 else
645 viadev->hwptr_done += viadev->fragsize;
646 viadev->in_interrupt = c_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 spin_unlock(&chip->reg_lock);
648 snd_pcm_period_elapsed(viadev->substream);
649 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200650 viadev->in_interrupt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
653 }
654 spin_unlock(&chip->reg_lock);
655 return IRQ_HANDLED;
656}
657
658/*
Karsten Wiese4f550df2005-10-18 14:31:07 +0200659 * Interrupt handler
660 */
661static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id, struct pt_regs *regs)
662{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100663 struct via82xx *chip = dev_id;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200664 unsigned int status;
665 unsigned int i;
666 int irqreturn = 0;
667
668 /* check status for each stream */
669 spin_lock(&chip->reg_lock);
670 status = inl(VIAREG(chip, SGD_SHADOW));
671
672 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100673 struct viadev *viadev = &chip->devs[i];
674 struct snd_pcm_substream *substream;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200675 unsigned char c_status, shadow_status;
676
677 shadow_status = (status >> viadev->shadow_shift) &
678 (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
679 VIA_REG_STAT_FLAG);
680 c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
681 if (!c_status)
682 continue;
683
684 substream = viadev->substream;
685 if (substream && viadev->running) {
686 /*
687 * Update hwptr_done based on 'period elapsed'
688 * interrupts. We'll use it, when the chip returns 0
689 * for OFFSET_CURR_COUNT.
690 */
691 if (c_status & VIA_REG_STAT_EOL)
692 viadev->hwptr_done = 0;
693 else
694 viadev->hwptr_done += viadev->fragsize;
695 viadev->in_interrupt = c_status;
696 if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
697 viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
698 spin_unlock(&chip->reg_lock);
699
700 snd_pcm_period_elapsed(substream);
701
702 spin_lock(&chip->reg_lock);
703 viadev->in_interrupt = 0;
704 }
705 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
706 irqreturn = 1;
707 }
708 spin_unlock(&chip->reg_lock);
709 return IRQ_RETVAL(irqreturn);
710}
711
712/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * PCM callbacks
714 */
715
716/*
717 * trigger callback
718 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100719static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100721 struct via82xx *chip = snd_pcm_substream_chip(substream);
722 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned char val;
724
725 if (chip->chip_type != TYPE_VIA686)
726 val = VIA_REG_CTRL_INT;
727 else
728 val = 0;
729 switch (cmd) {
730 case SNDRV_PCM_TRIGGER_START:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200731 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 val |= VIA_REG_CTRL_START;
733 viadev->running = 1;
734 break;
735 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200736 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 val = VIA_REG_CTRL_TERMINATE;
738 viadev->running = 0;
739 break;
740 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
741 val |= VIA_REG_CTRL_PAUSE;
742 viadev->running = 0;
743 break;
744 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
745 viadev->running = 1;
746 break;
747 default:
748 return -EINVAL;
749 }
750 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
751 if (cmd == SNDRV_PCM_TRIGGER_STOP)
752 snd_via82xx_channel_reset(chip, viadev);
753 return 0;
754}
755
756
757/*
758 * pointer callbacks
759 */
760
761/*
762 * calculate the linear position at the given sg-buffer index and the rest count
763 */
764
765#define check_invalid_pos(viadev,pos) \
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100766 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
767 viadev->lastpos < viadev->bufsize2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100769static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
770 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 unsigned int size, base, res;
773
774 size = viadev->idx_table[idx].size;
775 base = viadev->idx_table[idx].offset;
776 res = base + size - count;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200777 if (res >= viadev->bufsize)
778 res -= viadev->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* check the validity of the calculated position */
781 if (size < count) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100782 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
783 (int)size, (int)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 res = viadev->lastpos;
785 } else {
786 if (! count) {
787 /* Some mobos report count = 0 on the DMA boundary,
788 * i.e. count = size indeed.
789 * Let's check whether this step is above the expected size.
790 */
791 int delta = res - viadev->lastpos;
792 if (delta < 0)
793 delta += viadev->bufsize;
794 if ((unsigned int)delta > viadev->fragsize)
795 res = base;
796 }
797 if (check_invalid_pos(viadev, res)) {
798#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100799 printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
800 "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
801 "count = 0x%x\n", idx, viadev->tbl_entries,
802 viadev->lastpos, viadev->bufsize2,
803 viadev->idx_table[idx].offset,
804 viadev->idx_table[idx].size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#endif
806 /* count register returns full size when end of buffer is reached */
807 res = base + size;
808 if (check_invalid_pos(viadev, res)) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100809 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
810 "using last valid pointer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 res = viadev->lastpos;
812 }
813 }
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return res;
816}
817
818/*
819 * get the current pointer on via686
820 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100821static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100823 struct via82xx *chip = snd_pcm_substream_chip(substream);
824 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 unsigned int idx, ptr, count, res;
826
827 snd_assert(viadev->tbl_entries, return 0);
828 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
829 return 0;
830
831 spin_lock(&chip->reg_lock);
832 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
833 /* The via686a does not have the current index register,
834 * so we need to calculate the index from CURR_PTR.
835 */
836 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
837 if (ptr <= (unsigned int)viadev->table.addr)
838 idx = 0;
839 else /* CURR_PTR holds the address + 8 */
840 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
841 res = calc_linear_pos(viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200842 viadev->lastpos = res; /* remember the last position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 spin_unlock(&chip->reg_lock);
844
845 return bytes_to_frames(substream->runtime, res);
846}
847
848/*
849 * get the current pointer on via823x
850 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100851static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100853 struct via82xx *chip = snd_pcm_substream_chip(substream);
854 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned int idx, count, res;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200856 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 snd_assert(viadev->tbl_entries, return 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200861 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
862 status = viadev->in_interrupt;
863 if (!status)
864 status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
865
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200866 /* An apparent bug in the 8251 is worked around by sending a
867 * REG_CTRL_START. */
868 if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
869 snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
870
Karsten Wiese4f550df2005-10-18 14:31:07 +0200871 if (!(status & VIA_REG_STAT_ACTIVE)) {
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200872 res = 0;
873 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Karsten Wiese4f550df2005-10-18 14:31:07 +0200875 if (count & 0xffffff) {
876 idx = count >> 24;
877 if (idx >= viadev->tbl_entries) {
878#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100879 printk(KERN_DEBUG "fail: invalid idx = %i/%i\n", idx,
880 viadev->tbl_entries);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200881#endif
882 res = viadev->lastpos;
883 } else {
884 count &= 0xffffff;
885 res = calc_linear_pos(viadev, idx, count);
886 }
887 } else {
888 res = viadev->hwptr_done;
889 if (!viadev->in_interrupt) {
890 if (status & VIA_REG_STAT_EOL) {
891 res = 0;
892 } else
893 if (status & VIA_REG_STAT_FLAG) {
894 res += viadev->fragsize;
895 }
896 }
897 }
898unlock:
899 viadev->lastpos = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 spin_unlock(&chip->reg_lock);
901
902 return bytes_to_frames(substream->runtime, res);
903}
904
905
906/*
907 * hw_params callback:
908 * allocate the buffer and build up the buffer description table
909 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100910static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
911 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100913 struct via82xx *chip = snd_pcm_substream_chip(substream);
914 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int err;
916
917 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
918 if (err < 0)
919 return err;
920 err = build_via_table(viadev, substream, chip->pci,
921 params_periods(hw_params),
922 params_period_bytes(hw_params));
923 if (err < 0)
924 return err;
925
926 return 0;
927}
928
929/*
930 * hw_free callback:
931 * clean up the buffer description table and release the buffer
932 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100933static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100935 struct via82xx *chip = snd_pcm_substream_chip(substream);
936 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 clean_via_table(viadev, substream, chip->pci);
939 snd_pcm_lib_free_pages(substream);
940 return 0;
941}
942
943
944/*
945 * set up the table pointer
946 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100947static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 snd_via82xx_codec_ready(chip, 0);
950 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
951 udelay(20);
952 snd_via82xx_codec_ready(chip, 0);
953}
954
955/*
956 * prepare callback for playback and capture on via686
957 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100958static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
959 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 snd_via82xx_channel_reset(chip, viadev);
962 /* this must be set after channel_reset */
963 snd_via82xx_set_table_ptr(chip, viadev);
964 outb(VIA_REG_TYPE_AUTOSTART |
965 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
966 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
967 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
968 VIA_REG_TYPE_INT_EOL |
969 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
970}
971
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100972static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100974 struct via82xx *chip = snd_pcm_substream_chip(substream);
975 struct viadev *viadev = substream->runtime->private_data;
976 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
979 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
980 via686_setup_format(chip, viadev, runtime);
981 return 0;
982}
983
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100984static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100986 struct via82xx *chip = snd_pcm_substream_chip(substream);
987 struct viadev *viadev = substream->runtime->private_data;
988 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
991 via686_setup_format(chip, viadev, runtime);
992 return 0;
993}
994
995/*
996 * lock the current rate
997 */
998static int via_lock_rate(struct via_rate_lock *rec, int rate)
999{
1000 int changed = 0;
1001
1002 spin_lock_irq(&rec->lock);
1003 if (rec->rate != rate) {
1004 if (rec->rate && rec->used > 1) /* already set */
1005 changed = -EINVAL;
1006 else {
1007 rec->rate = rate;
1008 changed = 1;
1009 }
1010 }
1011 spin_unlock_irq(&rec->lock);
1012 return changed;
1013}
1014
1015/*
1016 * prepare callback for DSX playback on via823x
1017 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001018static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001020 struct via82xx *chip = snd_pcm_substream_chip(substream);
1021 struct viadev *viadev = substream->runtime->private_data;
1022 struct snd_pcm_runtime *runtime = substream->runtime;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001023 int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int rate_changed;
1025 u32 rbits;
1026
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001027 if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return rate_changed;
Takashi Iwai16d3f142005-08-15 15:02:28 +02001029 if (rate_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1031 chip->no_vra ? 48000 : runtime->rate);
Takashi Iwai16d3f142005-08-15 15:02:28 +02001032 if (chip->spdif_on && viadev->reg_offset == 0x30)
1033 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (runtime->rate == 48000)
1036 rbits = 0xfffff;
1037 else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001038 rbits = (0x100000 / 48000) * runtime->rate +
1039 ((0x100000 % 48000) * runtime->rate) / 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
1041 snd_via82xx_channel_reset(chip, viadev);
1042 snd_via82xx_set_table_ptr(chip, viadev);
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001043 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1044 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1045 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1046 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1048 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1049 rbits | /* rate */
1050 0xff000000, /* STOP index is never reached */
1051 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1052 udelay(20);
1053 snd_via82xx_codec_ready(chip, 0);
1054 return 0;
1055}
1056
1057/*
1058 * prepare callback for multi-channel playback on via823x
1059 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001060static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001062 struct via82xx *chip = snd_pcm_substream_chip(substream);
1063 struct viadev *viadev = substream->runtime->private_data;
1064 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 unsigned int slots;
1066 int fmt;
1067
1068 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1069 return -EINVAL;
1070 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1071 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1072 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1073 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1074 snd_via82xx_channel_reset(chip, viadev);
1075 snd_via82xx_set_table_ptr(chip, viadev);
1076
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001077 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1078 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 fmt |= runtime->channels << 4;
1080 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1081#if 0
1082 if (chip->revision == VIA_REV_8233A)
1083 slots = 0;
1084 else
1085#endif
1086 {
1087 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1088 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1089 switch (runtime->channels) {
1090 case 1: slots = (1<<0) | (1<<4); break;
1091 case 2: slots = (1<<0) | (2<<4); break;
1092 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1093 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1094 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1095 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1096 default: slots = 0; break;
1097 }
1098 }
1099 /* STOP index is never reached */
1100 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1101 udelay(20);
1102 snd_via82xx_codec_ready(chip, 0);
1103 return 0;
1104}
1105
1106/*
1107 * prepare callback for capture on via823x
1108 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001109static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001111 struct via82xx *chip = snd_pcm_substream_chip(substream);
1112 struct viadev *viadev = substream->runtime->private_data;
1113 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1116 return -EINVAL;
1117 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1118 snd_via82xx_channel_reset(chip, viadev);
1119 snd_via82xx_set_table_ptr(chip, viadev);
1120 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1121 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1122 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1123 0xff000000, /* STOP index is never reached */
1124 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1125 udelay(20);
1126 snd_via82xx_codec_ready(chip, 0);
1127 return 0;
1128}
1129
1130
1131/*
1132 * pcm hardware definition, identical for both playback and capture
1133 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001134static struct snd_pcm_hardware snd_via82xx_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1137 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1138 SNDRV_PCM_INFO_MMAP_VALID |
Jaroslav Kysela41e48452005-08-18 13:43:12 +02001139 /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 SNDRV_PCM_INFO_PAUSE),
1141 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1142 .rates = SNDRV_PCM_RATE_48000,
1143 .rate_min = 48000,
1144 .rate_max = 48000,
1145 .channels_min = 1,
1146 .channels_max = 2,
1147 .buffer_bytes_max = 128 * 1024,
1148 .period_bytes_min = 32,
1149 .period_bytes_max = 128 * 1024,
1150 .periods_min = 2,
1151 .periods_max = VIA_TABLE_SIZE / 2,
1152 .fifo_size = 0,
1153};
1154
1155
1156/*
1157 * open callback skeleton
1158 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001159static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1160 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001162 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int err;
1164 struct via_rate_lock *ratep;
1165
1166 runtime->hw = snd_via82xx_hw;
1167
1168 /* set the hw rate condition */
1169 ratep = &chip->rates[viadev->direction];
1170 spin_lock_irq(&ratep->lock);
1171 ratep->used++;
1172 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1173 /* DXS#3 and spdif is on */
1174 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1175 snd_pcm_limit_hw_rates(runtime);
1176 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1177 /* fixed DXS playback rate */
1178 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1179 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001180 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1181 /* use full SRC capabilities of DXS */
1182 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1183 SNDRV_PCM_RATE_8000_48000);
1184 runtime->hw.rate_min = 8000;
1185 runtime->hw.rate_max = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else if (! ratep->rate) {
1187 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1188 runtime->hw.rates = chip->ac97->rates[idx];
1189 snd_pcm_limit_hw_rates(runtime);
1190 } else {
1191 /* a fixed rate */
1192 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1193 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1194 }
1195 spin_unlock_irq(&ratep->lock);
1196
1197 /* we may remove following constaint when we modify table entries
1198 in interrupt */
1199 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1200 return err;
1201
1202 runtime->private_data = viadev;
1203 viadev->substream = substream;
1204
1205 return 0;
1206}
1207
1208
1209/*
1210 * open callback for playback on via686 and via823x DSX
1211 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001212static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001214 struct via82xx *chip = snd_pcm_substream_chip(substream);
1215 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int err;
1217
1218 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1219 return err;
1220 return 0;
1221}
1222
1223/*
1224 * open callback for playback on via823x multi-channel
1225 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001226static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001228 struct via82xx *chip = snd_pcm_substream_chip(substream);
1229 struct viadev *viadev = &chip->devs[chip->multi_devno];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err;
1231 /* channels constraint for VIA8233A
1232 * 3 and 5 channels are not supported
1233 */
1234 static unsigned int channels[] = {
1235 1, 2, 4, 6
1236 };
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001237 static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 .count = ARRAY_SIZE(channels),
1239 .list = channels,
1240 .mask = 0,
1241 };
1242
1243 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1244 return err;
1245 substream->runtime->hw.channels_max = 6;
1246 if (chip->revision == VIA_REV_8233A)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001247 snd_pcm_hw_constraint_list(substream->runtime, 0,
1248 SNDRV_PCM_HW_PARAM_CHANNELS,
1249 &hw_constraints_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251}
1252
1253/*
1254 * open callback for capture on via686 and via823x
1255 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001256static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001258 struct via82xx *chip = snd_pcm_substream_chip(substream);
1259 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 return snd_via82xx_pcm_open(chip, viadev, substream);
1262}
1263
1264/*
1265 * close callback
1266 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001267static int snd_via82xx_pcm_close(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 = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct via_rate_lock *ratep;
1272
1273 /* release the rate lock */
1274 ratep = &chip->rates[viadev->direction];
1275 spin_lock_irq(&ratep->lock);
1276 ratep->used--;
1277 if (! ratep->used)
1278 ratep->rate = 0;
1279 spin_unlock_irq(&ratep->lock);
1280
1281 viadev->substream = NULL;
1282 return 0;
1283}
1284
1285
1286/* via686 playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001287static struct snd_pcm_ops snd_via686_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 .open = snd_via82xx_playback_open,
1289 .close = snd_via82xx_pcm_close,
1290 .ioctl = snd_pcm_lib_ioctl,
1291 .hw_params = snd_via82xx_hw_params,
1292 .hw_free = snd_via82xx_hw_free,
1293 .prepare = snd_via686_playback_prepare,
1294 .trigger = snd_via82xx_pcm_trigger,
1295 .pointer = snd_via686_pcm_pointer,
1296 .page = snd_pcm_sgbuf_ops_page,
1297};
1298
1299/* via686 capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001300static struct snd_pcm_ops snd_via686_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 .open = snd_via82xx_capture_open,
1302 .close = snd_via82xx_pcm_close,
1303 .ioctl = snd_pcm_lib_ioctl,
1304 .hw_params = snd_via82xx_hw_params,
1305 .hw_free = snd_via82xx_hw_free,
1306 .prepare = snd_via686_capture_prepare,
1307 .trigger = snd_via82xx_pcm_trigger,
1308 .pointer = snd_via686_pcm_pointer,
1309 .page = snd_pcm_sgbuf_ops_page,
1310};
1311
1312/* via823x DSX playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001313static struct snd_pcm_ops snd_via8233_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 .open = snd_via82xx_playback_open,
1315 .close = snd_via82xx_pcm_close,
1316 .ioctl = snd_pcm_lib_ioctl,
1317 .hw_params = snd_via82xx_hw_params,
1318 .hw_free = snd_via82xx_hw_free,
1319 .prepare = snd_via8233_playback_prepare,
1320 .trigger = snd_via82xx_pcm_trigger,
1321 .pointer = snd_via8233_pcm_pointer,
1322 .page = snd_pcm_sgbuf_ops_page,
1323};
1324
1325/* via823x multi-channel playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001326static struct snd_pcm_ops snd_via8233_multi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 .open = snd_via8233_multi_open,
1328 .close = snd_via82xx_pcm_close,
1329 .ioctl = snd_pcm_lib_ioctl,
1330 .hw_params = snd_via82xx_hw_params,
1331 .hw_free = snd_via82xx_hw_free,
1332 .prepare = snd_via8233_multi_prepare,
1333 .trigger = snd_via82xx_pcm_trigger,
1334 .pointer = snd_via8233_pcm_pointer,
1335 .page = snd_pcm_sgbuf_ops_page,
1336};
1337
1338/* via823x capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001339static struct snd_pcm_ops snd_via8233_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 .open = snd_via82xx_capture_open,
1341 .close = snd_via82xx_pcm_close,
1342 .ioctl = snd_pcm_lib_ioctl,
1343 .hw_params = snd_via82xx_hw_params,
1344 .hw_free = snd_via82xx_hw_free,
1345 .prepare = snd_via8233_capture_prepare,
1346 .trigger = snd_via82xx_pcm_trigger,
1347 .pointer = snd_via8233_pcm_pointer,
1348 .page = snd_pcm_sgbuf_ops_page,
1349};
1350
1351
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001352static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1353 int shadow_pos, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 chip->devs[idx].reg_offset = reg_offset;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001356 chip->devs[idx].shadow_shift = shadow_pos * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 chip->devs[idx].direction = direction;
1358 chip->devs[idx].port = chip->port + reg_offset;
1359}
1360
1361/*
1362 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1363 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001364static int __devinit snd_via8233_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001366 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int i, err;
1368
1369 chip->playback_devno = 0; /* x 4 */
1370 chip->multi_devno = 4; /* x 1 */
1371 chip->capture_devno = 5; /* x 2 */
1372 chip->num_devs = 7;
1373 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1374
1375 /* PCM #0: 4 DSX playbacks and 1 capture */
1376 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1377 if (err < 0)
1378 return err;
1379 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1380 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1381 pcm->private_data = chip;
1382 strcpy(pcm->name, chip->card->shortname);
1383 chip->pcms[0] = pcm;
1384 /* set up playbacks */
1385 for (i = 0; i < 4; i++)
Karsten Wiese4f550df2005-10-18 14:31:07 +02001386 init_viadev(chip, i, 0x10 * i, i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001388 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001391 snd_dma_pci_data(chip->pci),
1392 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return err;
1394
1395 /* PCM #1: multi-channel playback and 2nd capture */
1396 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1397 if (err < 0)
1398 return err;
1399 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1400 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1401 pcm->private_data = chip;
1402 strcpy(pcm->name, chip->card->shortname);
1403 chip->pcms[1] = pcm;
1404 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001405 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* set up capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001407 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001410 snd_dma_pci_data(chip->pci),
1411 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return err;
1413
1414 return 0;
1415}
1416
1417/*
1418 * create pcm instances for VIA8233A
1419 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001420static int __devinit snd_via8233a_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001422 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int err;
1424
1425 chip->multi_devno = 0;
1426 chip->playback_devno = 1;
1427 chip->capture_devno = 2;
1428 chip->num_devs = 3;
1429 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1430
1431 /* PCM #0: multi-channel playback and capture */
1432 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1433 if (err < 0)
1434 return err;
1435 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1436 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1437 pcm->private_data = chip;
1438 strcpy(pcm->name, chip->card->shortname);
1439 chip->pcms[0] = pcm;
1440 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001441 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001443 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001446 snd_dma_pci_data(chip->pci),
1447 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return err;
1449
1450 /* SPDIF supported? */
1451 if (! ac97_can_spdif(chip->ac97))
1452 return 0;
1453
1454 /* PCM #1: DXS3 playback (for spdif) */
1455 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1456 if (err < 0)
1457 return err;
1458 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1459 pcm->private_data = chip;
1460 strcpy(pcm->name, chip->card->shortname);
1461 chip->pcms[1] = pcm;
1462 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001463 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001466 snd_dma_pci_data(chip->pci),
1467 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return err;
1469
1470 return 0;
1471}
1472
1473/*
1474 * create a pcm instance for via686a/b
1475 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001476static int __devinit snd_via686_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001478 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int err;
1480
1481 chip->playback_devno = 0;
1482 chip->capture_devno = 1;
1483 chip->num_devs = 2;
1484 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1485
1486 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1487 if (err < 0)
1488 return err;
1489 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1490 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1491 pcm->private_data = chip;
1492 strcpy(pcm->name, chip->card->shortname);
1493 chip->pcms[0] = pcm;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001494 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1495 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001498 snd_dma_pci_data(chip->pci),
1499 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return err;
1501
1502 return 0;
1503}
1504
1505
1506/*
1507 * Mixer part
1508 */
1509
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001510static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1511 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 /* formerly they were "Line" and "Mic", but it looks like that they
1514 * have nothing to do with the actual physical connections...
1515 */
1516 static char *texts[2] = {
1517 "Input1", "Input2"
1518 };
1519 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1520 uinfo->count = 1;
1521 uinfo->value.enumerated.items = 2;
1522 if (uinfo->value.enumerated.item >= 2)
1523 uinfo->value.enumerated.item = 1;
1524 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1525 return 0;
1526}
1527
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001528static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1529 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001531 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1533 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1534 return 0;
1535}
1536
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001537static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001540 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1542 u8 val, oval;
1543
1544 spin_lock_irq(&chip->reg_lock);
1545 oval = inb(port);
1546 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1547 if (ucontrol->value.enumerated.item[0])
1548 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1549 if (val != oval)
1550 outb(val, port);
1551 spin_unlock_irq(&chip->reg_lock);
1552 return val != oval;
1553}
1554
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001555static struct snd_kcontrol_new snd_via8233_capture_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 .name = "Input Source Select",
1557 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1558 .info = snd_via8233_capture_source_info,
1559 .get = snd_via8233_capture_source_get,
1560 .put = snd_via8233_capture_source_put,
1561};
1562
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001563static int snd_via8233_dxs3_spdif_info(struct snd_kcontrol *kcontrol,
1564 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1567 uinfo->count = 1;
1568 uinfo->value.integer.min = 0;
1569 uinfo->value.integer.max = 1;
1570 return 0;
1571}
1572
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001573static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1574 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001576 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 u8 val;
1578
1579 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1580 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1581 return 0;
1582}
1583
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001584static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1585 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001587 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 u8 val, oval;
1589
1590 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1591 val = oval & ~VIA8233_SPDIF_DX3;
1592 if (ucontrol->value.integer.value[0])
1593 val |= VIA8233_SPDIF_DX3;
1594 /* save the spdif flag for rate filtering */
1595 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1596 if (val != oval) {
1597 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1598 return 1;
1599 }
1600 return 0;
1601}
1602
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001603static struct snd_kcontrol_new snd_via8233_dxs3_spdif_control __devinitdata = {
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001604 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1606 .info = snd_via8233_dxs3_spdif_info,
1607 .get = snd_via8233_dxs3_spdif_get,
1608 .put = snd_via8233_dxs3_spdif_put,
1609};
1610
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001611static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1612 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1615 uinfo->count = 2;
1616 uinfo->value.integer.min = 0;
1617 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1618 return 0;
1619}
1620
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001621static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001624 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001625 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1626
1627 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1628 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1629 return 0;
1630}
1631
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001632static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001634{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001635 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001636 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1637 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return 0;
1639}
1640
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001641static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001644 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001645 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1646 unsigned long port = chip->port + 0x10 * idx;
1647 unsigned char val;
1648 int i, change = 0;
1649
1650 for (i = 0; i < 2; i++) {
1651 val = ucontrol->value.integer.value[i];
1652 if (val > VIA_DXS_MAX_VOLUME)
1653 val = VIA_DXS_MAX_VOLUME;
1654 val = VIA_DXS_MAX_VOLUME - val;
1655 change |= val != chip->playback_volume[idx][i];
1656 if (change) {
1657 chip->playback_volume[idx][i] = val;
1658 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1659 }
1660 }
1661 return change;
1662}
1663
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001664static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1665 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001666{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001667 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 unsigned int idx;
1669 unsigned char val;
1670 int i, change = 0;
1671
1672 for (i = 0; i < 2; i++) {
1673 val = ucontrol->value.integer.value[i];
1674 if (val > VIA_DXS_MAX_VOLUME)
1675 val = VIA_DXS_MAX_VOLUME;
1676 val = VIA_DXS_MAX_VOLUME - val;
Honza Maly00f226d2005-10-14 18:18:01 +02001677 if (val != chip->playback_volume_c[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 change = 1;
Honza Maly00f226d2005-10-14 18:18:01 +02001679 chip->playback_volume_c[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 for (idx = 0; idx < 4; idx++) {
1681 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02001682 chip->playback_volume[idx][i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1684 }
1685 }
1686 }
1687 return change;
1688}
1689
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001690static struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 .name = "PCM Playback Volume",
1692 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1693 .info = snd_via8233_dxs_volume_info,
Honza Maly00f226d2005-10-14 18:18:01 +02001694 .get = snd_via8233_pcmdxs_volume_get,
1695 .put = snd_via8233_pcmdxs_volume_put,
1696};
1697
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001698static struct snd_kcontrol_new snd_via8233_dxs_volume_control __devinitdata = {
Honza Maly00f226d2005-10-14 18:18:01 +02001699 .name = "VIA DXS Playback Volume",
1700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1701 .count = 4,
1702 .info = snd_via8233_dxs_volume_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 .get = snd_via8233_dxs_volume_get,
1704 .put = snd_via8233_dxs_volume_put,
1705};
1706
1707/*
1708 */
1709
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001710static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001712 struct via82xx *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 chip->ac97_bus = NULL;
1714}
1715
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001716static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001718 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 chip->ac97 = NULL;
1720}
1721
1722static struct ac97_quirk ac97_quirks[] = {
1723 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001724 .subvendor = 0x1106,
1725 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 .codec_id = 0x56494161, /* VT1612A */
1727 .name = "Soltek SL-75DRV5",
1728 .type = AC97_TUNE_NONE
1729 },
1730 { /* FIXME: which codec? */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001731 .subvendor = 0x1106,
1732 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 .name = "ASRock K7VT2",
1734 .type = AC97_TUNE_HP_ONLY
1735 },
1736 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001737 .subvendor = 0x1019,
1738 .subdevice = 0x0a81,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .name = "ECS K7VTA3",
1740 .type = AC97_TUNE_HP_ONLY
1741 },
1742 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001743 .subvendor = 0x1019,
1744 .subdevice = 0x0a85,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .name = "ECS L7VMM2",
1746 .type = AC97_TUNE_HP_ONLY
1747 },
1748 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001749 .subvendor = 0x1849,
1750 .subdevice = 0x3059,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 .name = "ASRock K7VM2",
1752 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1753 },
1754 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001755 .subvendor = 0x14cd,
1756 .subdevice = 0x7002,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .name = "Unknown",
1758 .type = AC97_TUNE_ALC_JACK
1759 },
1760 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001761 .subvendor = 0x1071,
1762 .subdevice = 0x8590,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 .name = "Mitac Mobo",
1764 .type = AC97_TUNE_ALC_JACK
1765 },
1766 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001767 .subvendor = 0x161f,
1768 .subdevice = 0x202b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 .name = "Arima Notebook",
1770 .type = AC97_TUNE_HP_ONLY,
1771 },
Takashi Iwaidac8ddd2005-12-14 12:00:46 +01001772 {
1773 .subvendor = 0x161f,
1774 .subdevice = 0x2032,
1775 .name = "Targa Traveller 811",
1776 .type = AC97_TUNE_HP_ONLY,
1777 },
Daniel T Chend4199f02006-06-16 16:21:54 +02001778 {
1779 .subvendor = 0x161f,
1780 .subdevice = 0x2032,
1781 .name = "m680x",
1782 .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1783 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 { } /* terminator */
1785};
1786
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001787static int __devinit snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001789 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001791 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 .write = snd_via82xx_codec_write,
1793 .read = snd_via82xx_codec_read,
1794 .wait = snd_via82xx_codec_wait,
1795 };
1796
1797 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1798 return err;
1799 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1800 chip->ac97_bus->clock = chip->ac97_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 memset(&ac97, 0, sizeof(ac97));
1803 ac97.private_data = chip;
1804 ac97.private_free = snd_via82xx_mixer_free_ac97;
1805 ac97.pci = chip->pci;
Sasha Khapyorsky2ba71972005-09-29 12:58:24 +02001806 ac97.scaps = AC97_SCAP_SKIP_MODEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1808 return err;
1809
1810 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1811
1812 if (chip->chip_type != TYPE_VIA686) {
1813 /* use slot 10/11 */
1814 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1815 }
1816
1817 return 0;
1818}
1819
1820#ifdef SUPPORT_JOYSTICK
1821#define JOYSTICK_ADDR 0x200
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001822static int __devinit snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 struct gameport *gp;
1825 struct resource *r;
1826
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001827 if (!joystick)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -ENODEV;
1829
1830 r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1831 if (!r) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001832 printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n",
1833 JOYSTICK_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return -EBUSY;
1835 }
1836
1837 chip->gameport = gp = gameport_allocate_port();
1838 if (!gp) {
1839 printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02001840 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return -ENOMEM;
1842 }
1843
1844 gameport_set_name(gp, "VIA686 Gameport");
1845 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1846 gameport_set_dev_parent(gp, &chip->pci->dev);
1847 gp->io = JOYSTICK_ADDR;
1848 gameport_set_port_data(gp, r);
1849
1850 /* Enable legacy joystick port */
1851 *legacy |= VIA_FUNC_ENABLE_GAME;
1852 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1853
1854 gameport_register_port(chip->gameport);
1855
1856 return 0;
1857}
1858
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001859static void snd_via686_free_gameport(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 if (chip->gameport) {
1862 struct resource *r = gameport_get_port_data(chip->gameport);
1863
1864 gameport_unregister_port(chip->gameport);
1865 chip->gameport = NULL;
Takashi Iwaib1d57762005-10-10 11:56:31 +02001866 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868}
1869#else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001870static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 return -ENOSYS;
1873}
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001874static inline void snd_via686_free_gameport(struct via82xx *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875#endif
1876
1877
1878/*
1879 *
1880 */
1881
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001882static int __devinit snd_via8233_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 int i, err, caps;
1885 unsigned char val;
1886
1887 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1888 for (i = 0; i < caps; i++) {
1889 snd_via8233_capture_source.index = i;
1890 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1891 if (err < 0)
1892 return err;
1893 }
1894 if (ac97_can_spdif(chip->ac97)) {
1895 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1896 if (err < 0)
1897 return err;
1898 }
1899 if (chip->chip_type != TYPE_VIA8233A) {
1900 /* when no h/w PCM volume control is found, use DXS volume control
1901 * as the PCM vol control
1902 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001903 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 memset(&sid, 0, sizeof(sid));
1905 strcpy(sid.name, "PCM Playback Volume");
1906 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1907 if (! snd_ctl_find_id(chip->card, &sid)) {
Honza Maly00f226d2005-10-14 18:18:01 +02001908 snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
1909 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1910 if (err < 0)
1911 return err;
1912 }
1913 else /* Using DXS when PCM emulation is enabled is really weird */
1914 {
1915 /* Standalone DXS controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1917 if (err < 0)
1918 return err;
1919 }
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /* select spdif data slot 10/11 */
1922 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1923 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1924 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
1925 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1926
1927 return 0;
1928}
1929
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001930static int __devinit snd_via686_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 unsigned char legacy, legacy_cfg;
1933 int rev_h = 0;
1934
1935 legacy = chip->old_legacy;
1936 legacy_cfg = chip->old_legacy_cfg;
1937 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
1938 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
1939 if (chip->revision >= VIA_REV_686_H) {
1940 rev_h = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001941 if (mpu_port >= 0x200) { /* force MIDI */
1942 mpu_port &= 0xfffc;
1943 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944#ifdef CONFIG_PM
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001945 chip->mpu_port_saved = mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946#endif
1947 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001948 mpu_port = pci_resource_start(chip->pci, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001951 switch (mpu_port) { /* force MIDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 case 0x300:
1953 case 0x310:
1954 case 0x320:
1955 case 0x330:
1956 legacy_cfg &= ~(3 << 2);
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001957 legacy_cfg |= (mpu_port & 0x0030) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 break;
1959 default: /* no, use BIOS settings */
1960 if (legacy & VIA_FUNC_ENABLE_MIDI)
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001961 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 break;
1963 }
1964 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001965 if (mpu_port >= 0x200 &&
1966 (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
1967 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (rev_h)
1969 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
1970 legacy |= VIA_FUNC_ENABLE_MIDI;
1971 } else {
1972 if (rev_h)
1973 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
1974 legacy &= ~VIA_FUNC_ENABLE_MIDI;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001975 mpu_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977
1978 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1979 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1980 if (chip->mpu_res) {
1981 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
Takashi Iwai302e4c22006-05-23 13:24:30 +02001982 mpu_port, MPU401_INFO_INTEGRATED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 chip->irq, 0, &chip->rmidi) < 0) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001984 printk(KERN_WARNING "unable to initialize MPU-401"
1985 " at 0x%lx, skipping\n", mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1987 } else {
1988 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
1989 }
1990 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1991 }
1992
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001993 snd_via686_create_gameport(chip, &legacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995#ifdef CONFIG_PM
1996 chip->legacy_saved = legacy;
1997 chip->legacy_cfg_saved = legacy_cfg;
1998#endif
1999
2000 return 0;
2001}
2002
2003
2004/*
2005 * proc interface
2006 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002007static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2008 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002010 struct via82xx *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int i;
2012
2013 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2014 for (i = 0; i < 0xa0; i += 4) {
2015 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2016 }
2017}
2018
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002019static void __devinit snd_via82xx_proc_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002021 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002024 snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027/*
2028 *
2029 */
2030
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002031static int snd_via82xx_chip_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 unsigned int val;
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002034 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 unsigned char pval;
2036
2037#if 0 /* broken on K7M? */
2038 if (chip->chip_type == TYPE_VIA686)
2039 /* disable all legacy ports */
2040 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2041#endif
2042 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2043 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2044 /* deassert ACLink reset, force SYNC */
2045 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2046 VIA_ACLINK_CTRL_ENABLE |
2047 VIA_ACLINK_CTRL_RESET |
2048 VIA_ACLINK_CTRL_SYNC);
2049 udelay(100);
2050#if 1 /* FIXME: should we do full reset here for all chip models? */
2051 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2052 udelay(100);
2053#else
2054 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2055 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2056 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2057 udelay(2);
2058#endif
2059 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2060 /* note - FM data out has trouble with non VRA codecs !! */
2061 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2062 udelay(100);
2063 }
2064
2065 /* Make sure VRA is enabled, in case we didn't do a
2066 * complete codec reset, above */
2067 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2068 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2069 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2070 /* note - FM data out has trouble with non VRA codecs !! */
2071 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2072 udelay(100);
2073 }
2074
2075 /* wait until codec ready */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002076 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 do {
2078 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2079 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2080 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002081 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002082 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
Takashi Iwai99b359b2005-10-20 18:26:44 +02002085 snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2088 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2089 VIA_REG_AC97_SECONDARY_VALID |
2090 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002091 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2093 VIA_REG_AC97_SECONDARY_VALID |
2094 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2095 do {
2096 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2097 chip->ac97_secondary = 1;
2098 goto __ac97_ok2;
2099 }
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002100 schedule_timeout_interruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002101 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /* This is ok, the most of motherboards have only one codec */
2103
2104 __ac97_ok2:
2105#endif
2106
2107 if (chip->chip_type == TYPE_VIA686) {
2108 /* route FM trap to IRQ, disable FM trap */
2109 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2110 /* disable all GPI interrupts */
2111 outl(0, VIAREG(chip, GPI_INTR));
2112 }
2113
2114 if (chip->chip_type != TYPE_VIA686) {
2115 /* Workaround for Award BIOS bug:
2116 * DXS channels don't work properly with VRA if MC97 is disabled.
2117 */
2118 struct pci_dev *pci;
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002119 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (pci) {
2121 unsigned char data;
2122 pci_read_config_byte(pci, 0x44, &data);
2123 pci_write_config_byte(pci, 0x44, data | 0x40);
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002124 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 }
2127
2128 if (chip->chip_type != TYPE_VIA8233A) {
2129 int i, idx;
2130 for (idx = 0; idx < 4; idx++) {
2131 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02002132 for (i = 0; i < 2; i++) {
2133 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002134 outb(chip->playback_volume_c[i],
2135 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
Honza Maly00f226d2005-10-14 18:18:01 +02002136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 }
2139
2140 return 0;
2141}
2142
2143#ifdef CONFIG_PM
2144/*
2145 * power management
2146 */
Takashi Iwai57feb832005-11-17 16:10:35 +01002147static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Takashi Iwai57feb832005-11-17 16:10:35 +01002149 struct snd_card *card = pci_get_drvdata(pci);
2150 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 int i;
2152
Takashi Iwai57feb832005-11-17 16:10:35 +01002153 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 for (i = 0; i < 2; i++)
Takashi Iwai57feb832005-11-17 16:10:35 +01002155 snd_pcm_suspend_all(chip->pcms[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 for (i = 0; i < chip->num_devs; i++)
2157 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2158 synchronize_irq(chip->irq);
2159 snd_ac97_suspend(chip->ac97);
2160
2161 /* save misc values */
2162 if (chip->chip_type != TYPE_VIA686) {
2163 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2164 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2165 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2166 }
2167
Takashi Iwai57feb832005-11-17 16:10:35 +01002168 pci_set_power_state(pci, PCI_D3hot);
2169 pci_disable_device(pci);
2170 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172}
2173
Takashi Iwai57feb832005-11-17 16:10:35 +01002174static int snd_via82xx_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Takashi Iwai57feb832005-11-17 16:10:35 +01002176 struct snd_card *card = pci_get_drvdata(pci);
2177 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 int i;
2179
Takashi Iwai57feb832005-11-17 16:10:35 +01002180 pci_restore_state(pci);
2181 pci_enable_device(pci);
2182 pci_set_power_state(pci, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 snd_via82xx_chip_init(chip);
2185
2186 if (chip->chip_type == TYPE_VIA686) {
2187 if (chip->mpu_port_saved)
2188 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2189 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2190 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2191 } else {
2192 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2193 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2194 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2195 }
2196
2197 snd_ac97_resume(chip->ac97);
2198
2199 for (i = 0; i < chip->num_devs; i++)
2200 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2201
Takashi Iwai57feb832005-11-17 16:10:35 +01002202 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return 0;
2204}
2205#endif /* CONFIG_PM */
2206
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002207static int snd_via82xx_free(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 unsigned int i;
2210
2211 if (chip->irq < 0)
2212 goto __end_hw;
2213 /* disable interrupts */
2214 for (i = 0; i < chip->num_devs; i++)
2215 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2216 synchronize_irq(chip->irq);
2217 __end_hw:
2218 if (chip->irq >= 0)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002219 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002220 release_and_free_resource(chip->mpu_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 pci_release_regions(chip->pci);
2222
2223 if (chip->chip_type == TYPE_VIA686) {
2224 snd_via686_free_gameport(chip);
2225 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2226 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2227 }
2228 pci_disable_device(chip->pci);
2229 kfree(chip);
2230 return 0;
2231}
2232
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002233static int snd_via82xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002235 struct via82xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return snd_via82xx_free(chip);
2237}
2238
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002239static int __devinit snd_via82xx_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 struct pci_dev *pci,
2241 int chip_type,
2242 int revision,
2243 unsigned int ac97_clock,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002244 struct via82xx ** r_via)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002246 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002248 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 .dev_free = snd_via82xx_dev_free,
2250 };
2251
2252 if ((err = pci_enable_device(pci)) < 0)
2253 return err;
2254
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002255 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 pci_disable_device(pci);
2257 return -ENOMEM;
2258 }
2259
2260 chip->chip_type = chip_type;
2261 chip->revision = revision;
2262
2263 spin_lock_init(&chip->reg_lock);
2264 spin_lock_init(&chip->rates[0].lock);
2265 spin_lock_init(&chip->rates[1].lock);
2266 chip->card = card;
2267 chip->pci = pci;
2268 chip->irq = -1;
2269
2270 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2271 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2272 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2273 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2274
2275 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2276 kfree(chip);
2277 pci_disable_device(pci);
2278 return err;
2279 }
2280 chip->port = pci_resource_start(pci, 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +02002281 if (request_irq(pci->irq,
2282 chip_type == TYPE_VIA8233 ?
2283 snd_via8233_interrupt : snd_via686_interrupt,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07002284 IRQF_DISABLED|IRQF_SHARED,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002285 card->driver, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002286 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 snd_via82xx_free(chip);
2288 return -EBUSY;
2289 }
2290 chip->irq = pci->irq;
2291 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2292 chip->ac97_clock = ac97_clock;
2293 synchronize_irq(chip->irq);
2294
2295 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2296 snd_via82xx_free(chip);
2297 return err;
2298 }
2299
2300 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2301 snd_via82xx_free(chip);
2302 return err;
2303 }
2304
2305 /* The 8233 ac97 controller does not implement the master bit
2306 * in the pci command register. IMHO this is a violation of the PCI spec.
2307 * We call pci_set_master here because it does not hurt. */
2308 pci_set_master(pci);
2309
2310 snd_card_set_dev(card, &pci->dev);
2311
2312 *r_via = chip;
2313 return 0;
2314}
2315
2316struct via823x_info {
2317 int revision;
2318 char *name;
2319 int type;
2320};
2321static struct via823x_info via823x_cards[] __devinitdata = {
2322 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2323 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2324 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2325 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2326 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2327 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
Bastiaan Jacques8263c652006-04-18 17:04:04 +02002328 { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329};
2330
2331/*
2332 * auto detection of DXS channel supports.
2333 */
2334struct dxs_whitelist {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002335 unsigned short subvendor;
2336 unsigned short subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 unsigned short mask;
2338 short action; /* new dxs_support value */
2339};
2340
Takashi Iwaia7695772006-04-27 16:56:07 +02002341static int __devinit check_dxs_list(struct pci_dev *pci, int revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 static struct dxs_whitelist whitelist[] = {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002344 { .subvendor = 0x1005, .subdevice = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
2345 { .subvendor = 0x1019, .subdevice = 0x0996, .action = VIA_DXS_48K },
2346 { .subvendor = 0x1019, .subdevice = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
2347 { .subvendor = 0x1019, .subdevice = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
Takashi Iwaidb990552005-09-16 19:07:52 +02002348 { .subvendor = 0x1019, .subdevice = 0xa101, .action = VIA_DXS_SRC },
Takashi Iwaia26e9d92006-04-06 19:59:21 +02002349 { .subvendor = 0x1019, .subdevice = 0xaa01, .action = VIA_DXS_SRC }, /* ECS K8T890-A */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002350 { .subvendor = 0x1025, .subdevice = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
Takashi Iwaif347c772005-08-12 16:47:49 +02002351 { .subvendor = 0x1025, .subdevice = 0x0046, .action = VIA_DXS_SRC }, /* Acer Aspire 1524 WLMi */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002352 { .subvendor = 0x1043, .subdevice = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
2353 { .subvendor = 0x1043, .subdevice = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
2354 { .subvendor = 0x1043, .subdevice = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
Takashi Iwaic66186e2005-09-29 13:49:44 +02002355 { .subvendor = 0x1043, .subdevice = 0x810d, .action = VIA_DXS_SRC }, /* ASUS */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002356 { .subvendor = 0x1043, .subdevice = 0x812a, .action = VIA_DXS_SRC }, /* ASUS A8V Deluxe */
Takashi Iwai6c1080c2006-01-04 12:15:07 +01002357 { .subvendor = 0x1043, .subdevice = 0x8174, .action = VIA_DXS_SRC }, /* ASUS */
Bastiaan Jacques8263c652006-04-18 17:04:04 +02002358 { .subvendor = 0x1043, .subdevice = 0x81b9, .action = VIA_DXS_SRC }, /* ASUS A8V-MX */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002359 { .subvendor = 0x1071, .subdevice = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
Jaroslav Kysela96d07812005-06-07 08:56:24 +02002360 { .subvendor = 0x1071, .subdevice = 0x8399, .action = VIA_DXS_NO_VRA }, /* Umax AB 595T (VIA K8N800A - VT8237) */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002361 { .subvendor = 0x10cf, .subdevice = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
2362 { .subvendor = 0x1106, .subdevice = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
2363 { .subvendor = 0x1106, .subdevice = 0x4552, .action = VIA_DXS_NO_VRA }, /* QDI Kudoz 7X/600-6AL */
2364 { .subvendor = 0x1106, .subdevice = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
2365 { .subvendor = 0x1106, .subdevice = 0xc001, .action = VIA_DXS_SRC }, /* Insight P4-ITX */
2366 { .subvendor = 0x1297, .subdevice = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
2367 { .subvendor = 0x1297, .subdevice = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
2368 { .subvendor = 0x1458, .subdevice = 0xa002, .action = VIA_DXS_ENABLE }, /* Gigabyte GA-7VAXP */
2369 { .subvendor = 0x1462, .subdevice = 0x0080, .action = VIA_DXS_SRC }, /* MSI K8T Neo-FIS2R */
Takashi Iwai352dbfd2005-08-19 17:49:10 +02002370 { .subvendor = 0x1462, .subdevice = 0x0430, .action = VIA_DXS_SRC }, /* MSI 7142 (K8MM-V) */
Takashi Iwaic1b8f5f2005-11-08 17:44:37 +01002371 { .subvendor = 0x1462, .subdevice = 0x0470, .action = VIA_DXS_SRC }, /* MSI KT880 Delta-FSR */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002372 { .subvendor = 0x1462, .subdevice = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
2373 { .subvendor = 0x1462, .subdevice = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
Karsten Wiese396f7392006-06-19 13:22:52 +02002374 { .subvendor = 0x1462, .subdevice = 0x7023, .action = VIA_DXS_SRC }, /* MSI K8T Neo2-FI */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002375 { .subvendor = 0x1462, .subdevice = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
Takashi Iwaif347c772005-08-12 16:47:49 +02002376 { .subvendor = 0x1462, .subdevice = 0x7142, .action = VIA_DXS_ENABLE }, /* MSI K8MM-V */
Takashi Iwai802c00f2006-01-23 15:31:11 +01002377 { .subvendor = 0x1462, .subdevice = 0xb012, .action = VIA_DXS_SRC }, /* P4M800/VIA8237R */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002378 { .subvendor = 0x147b, .subdevice = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
2379 { .subvendor = 0x147b, .subdevice = 0x1411, .action = VIA_DXS_ENABLE }, /* ABIT VA-20 */
2380 { .subvendor = 0x147b, .subdevice = 0x1413, .action = VIA_DXS_ENABLE }, /* ABIT KV8 Pro */
2381 { .subvendor = 0x147b, .subdevice = 0x1415, .action = VIA_DXS_NO_VRA }, /* Abit AV8 */
2382 { .subvendor = 0x14ff, .subdevice = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
Takashi Iwai69c3e5f2005-07-27 17:30:14 +02002383 { .subvendor = 0x14ff, .subdevice = 0x0408, .action = VIA_DXS_SRC }, /* Twinhead laptop */
Takashi Iwai94651a52005-10-10 12:08:01 +02002384 { .subvendor = 0x1558, .subdevice = 0x4701, .action = VIA_DXS_SRC }, /* Clevo D470 */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002385 { .subvendor = 0x1584, .subdevice = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
2386 { .subvendor = 0x1584, .subdevice = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
2387 { .subvendor = 0x161f, .subdevice = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
2388 { .subvendor = 0x161f, .subdevice = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
2389 { .subvendor = 0x1631, .subdevice = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
2390 { .subvendor = 0x1695, .subdevice = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
Takashi Iwaic04d0922006-03-23 17:11:32 +01002391 { .subvendor = 0x1695, .subdevice = 0x300c, .action = VIA_DXS_SRC }, /* EPoX EP-8KRAI */
Takashi Iwaicacd3342006-01-16 13:00:50 +01002392 { .subvendor = 0x1695, .subdevice = 0x300e, .action = VIA_DXS_SRC }, /* EPoX 9HEAI */
Takashi Iwaid0bd41e2006-01-09 12:36:53 +01002393 { .subvendor = 0x16f3, .subdevice = 0x6405, .action = VIA_DXS_SRC }, /* Jetway K8M8MS */
Takashi Iwaibd84b0c2006-02-09 12:04:36 +01002394 { .subvendor = 0x1734, .subdevice = 0x1078, .action = VIA_DXS_SRC }, /* FSC Amilo L7300 */
Takashi Iwai51c2bbf2006-01-23 15:20:03 +01002395 { .subvendor = 0x1734, .subdevice = 0x1093, .action = VIA_DXS_SRC }, /* FSC */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002396 { .subvendor = 0x1849, .subdevice = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
Takashi Iwai42611c02006-03-01 19:52:23 +01002397 { .subvendor = 0x1849, .subdevice = 0x9739, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
Takashi Iwai46a17362005-12-05 21:16:37 +01002398 { .subvendor = 0x1849, .subdevice = 0x9761, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
Takashi Iwaib3e28ce2005-06-17 11:54:50 +02002399 { .subvendor = 0x1919, .subdevice = 0x200a, .action = VIA_DXS_NO_VRA }, /* Soltek SL-K8Tpro-939 */
Jaroslav Kysela22019872005-07-05 10:27:09 +02002400 { .subvendor = 0x4005, .subdevice = 0x4710, .action = VIA_DXS_SRC }, /* MSI K7T266 Pro2 (MS-6380 V2.0) BIOS 3.7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 { } /* terminator */
2402 };
2403 struct dxs_whitelist *w;
2404 unsigned short subsystem_vendor;
2405 unsigned short subsystem_device;
2406
2407 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
2408 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
2409
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002410 for (w = whitelist; w->subvendor; w++) {
2411 if (w->subvendor != subsystem_vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 continue;
2413 if (w->mask) {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002414 if ((w->mask & subsystem_device) == w->subdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return w->action;
2416 } else {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002417 if (subsystem_device == w->subdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return w->action;
2419 }
2420 }
2421
Takashi Iwaia7695772006-04-27 16:56:07 +02002422 /* for newer revision, default to DXS_SRC */
2423 if (revision >= VIA_REV_8235)
2424 return VIA_DXS_SRC;
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /*
2427 * not detected, try 48k rate only to be sure.
2428 */
2429 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002430 printk(KERN_INFO " Please try dxs_support=5 option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 printk(KERN_INFO " and report if it works on your machine.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002432 printk(KERN_INFO " For more details, read ALSA-Configuration.txt.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return VIA_DXS_48K;
2434};
2435
2436static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2437 const struct pci_device_id *pci_id)
2438{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002439 struct snd_card *card;
2440 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 unsigned char revision;
2442 int chip_type = 0, card_type;
2443 unsigned int i;
2444 int err;
2445
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002446 card = snd_card_new(index, id, THIS_MODULE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (card == NULL)
2448 return -ENOMEM;
2449
2450 card_type = pci_id->driver_data;
2451 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
2452 switch (card_type) {
2453 case TYPE_CARD_VIA686:
2454 strcpy(card->driver, "VIA686A");
2455 sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
2456 chip_type = TYPE_VIA686;
2457 break;
2458 case TYPE_CARD_VIA8233:
2459 chip_type = TYPE_VIA8233;
2460 sprintf(card->shortname, "VIA 823x rev%x", revision);
2461 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2462 if (revision == via823x_cards[i].revision) {
2463 chip_type = via823x_cards[i].type;
2464 strcpy(card->shortname, via823x_cards[i].name);
2465 break;
2466 }
2467 }
2468 if (chip_type != TYPE_VIA8233A) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002469 if (dxs_support == VIA_DXS_AUTO)
Takashi Iwaia7695772006-04-27 16:56:07 +02002470 dxs_support = check_dxs_list(pci, revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* force to use VIA8233 or 8233A model according to
2472 * dxs_support module option
2473 */
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002474 if (dxs_support == VIA_DXS_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 chip_type = TYPE_VIA8233A;
2476 else
2477 chip_type = TYPE_VIA8233;
2478 }
2479 if (chip_type == TYPE_VIA8233A)
2480 strcpy(card->driver, "VIA8233A");
2481 else if (revision >= VIA_REV_8237)
2482 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2483 else
2484 strcpy(card->driver, "VIA8233");
2485 break;
2486 default:
2487 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2488 err = -EINVAL;
2489 goto __error;
2490 }
2491
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002492 if ((err = snd_via82xx_create(card, pci, chip_type, revision,
2493 ac97_clock, &chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 goto __error;
Takashi Iwai57feb832005-11-17 16:10:35 +01002495 card->private_data = chip;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002496 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 goto __error;
2498
2499 if (chip_type == TYPE_VIA686) {
2500 if ((err = snd_via686_pcm_new(chip)) < 0 ||
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002501 (err = snd_via686_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 goto __error;
2503 } else {
2504 if (chip_type == TYPE_VIA8233A) {
2505 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2506 goto __error;
2507 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2508 } else {
2509 if ((err = snd_via8233_pcm_new(chip)) < 0)
2510 goto __error;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002511 if (dxs_support == VIA_DXS_48K)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 chip->dxs_fixed = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002513 else if (dxs_support == VIA_DXS_NO_VRA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 chip->no_vra = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002515 else if (dxs_support == VIA_DXS_SRC) {
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02002516 chip->no_vra = 1;
2517 chip->dxs_src = 1;
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002520 if ((err = snd_via8233_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 goto __error;
2522 }
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /* disable interrupts */
2525 for (i = 0; i < chip->num_devs; i++)
2526 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2527
2528 snprintf(card->longname, sizeof(card->longname),
2529 "%s with %s at %#lx, irq %d", card->shortname,
2530 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2531
2532 snd_via82xx_proc_init(chip);
2533
2534 if ((err = snd_card_register(card)) < 0) {
2535 snd_card_free(card);
2536 return err;
2537 }
2538 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return 0;
2540
2541 __error:
2542 snd_card_free(card);
2543 return err;
2544}
2545
2546static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2547{
2548 snd_card_free(pci_get_drvdata(pci));
2549 pci_set_drvdata(pci, NULL);
2550}
2551
2552static struct pci_driver driver = {
2553 .name = "VIA 82xx Audio",
2554 .id_table = snd_via82xx_ids,
2555 .probe = snd_via82xx_probe,
2556 .remove = __devexit_p(snd_via82xx_remove),
Takashi Iwai57feb832005-11-17 16:10:35 +01002557#ifdef CONFIG_PM
2558 .suspend = snd_via82xx_suspend,
2559 .resume = snd_via82xx_resume,
2560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561};
2562
2563static int __init alsa_card_via82xx_init(void)
2564{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002565 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568static void __exit alsa_card_via82xx_exit(void)
2569{
2570 pci_unregister_driver(&driver);
2571}
2572
2573module_init(alsa_card_via82xx_init)
2574module_exit(alsa_card_via82xx_exit)