blob: 1957d29c119e427135ef703f2589dca676385a9b [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
126
127/*
128 * Direct registers
129 */
130
131#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
132#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
133
134/* common offsets */
135#define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
136#define VIA_REG_STAT_ACTIVE 0x80 /* RO */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200137#define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define VIA_REG_STAT_PAUSED 0x40 /* RO */
139#define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
140#define VIA_REG_STAT_STOPPED 0x04 /* RWC */
141#define VIA_REG_STAT_EOL 0x02 /* RWC */
142#define VIA_REG_STAT_FLAG 0x01 /* RWC */
143#define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
144#define VIA_REG_CTRL_START 0x80 /* WO */
145#define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
146#define VIA_REG_CTRL_AUTOSTART 0x20
147#define VIA_REG_CTRL_PAUSE 0x08 /* RW */
148#define VIA_REG_CTRL_INT_STOP 0x04
149#define VIA_REG_CTRL_INT_EOL 0x02
150#define VIA_REG_CTRL_INT_FLAG 0x01
151#define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
152#define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
153#define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
154#define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
155#define VIA_REG_TYPE_16BIT 0x20 /* RW */
156#define VIA_REG_TYPE_STEREO 0x10 /* RW */
157#define VIA_REG_TYPE_INT_LLINE 0x00
158#define VIA_REG_TYPE_INT_LSAMPLE 0x04
159#define VIA_REG_TYPE_INT_LESSONE 0x08
160#define VIA_REG_TYPE_INT_MASK 0x0c
161#define VIA_REG_TYPE_INT_EOL 0x02
162#define VIA_REG_TYPE_INT_FLAG 0x01
163#define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
164#define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
165#define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
166#define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
167#define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
168#define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
169#define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
170
171#define DEFINE_VIA_REGSET(name,val) \
172enum {\
173 VIA_REG_##name##_STATUS = (val),\
174 VIA_REG_##name##_CONTROL = (val) + 0x01,\
175 VIA_REG_##name##_TYPE = (val) + 0x02,\
176 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
177 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
178 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
179 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
180}
181
182/* playback block */
183DEFINE_VIA_REGSET(PLAYBACK, 0x00);
184DEFINE_VIA_REGSET(CAPTURE, 0x10);
185DEFINE_VIA_REGSET(FM, 0x20);
186
187/* AC'97 */
188#define VIA_REG_AC97 0x80 /* dword */
189#define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
190#define VIA_REG_AC97_CODEC_ID_SHIFT 30
191#define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
192#define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
193#define VIA_REG_AC97_SECONDARY_VALID (1<<27)
194#define VIA_REG_AC97_PRIMARY_VALID (1<<25)
195#define VIA_REG_AC97_BUSY (1<<24)
196#define VIA_REG_AC97_READ (1<<23)
197#define VIA_REG_AC97_CMD_SHIFT 16
198#define VIA_REG_AC97_CMD_MASK 0x7e
199#define VIA_REG_AC97_DATA_SHIFT 0
200#define VIA_REG_AC97_DATA_MASK 0xffff
201
202#define VIA_REG_SGD_SHADOW 0x84 /* dword */
203/* via686 */
204#define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
205#define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
206#define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
207#define VIA_REG_SGD_STAT_PB_EOL (1<<4)
208#define VIA_REG_SGD_STAT_CP_EOL (1<<5)
209#define VIA_REG_SGD_STAT_FM_EOL (1<<6)
210#define VIA_REG_SGD_STAT_PB_STOP (1<<8)
211#define VIA_REG_SGD_STAT_CP_STOP (1<<9)
212#define VIA_REG_SGD_STAT_FM_STOP (1<<10)
213#define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
214#define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
215#define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
216/* via8233 */
217#define VIA8233_REG_SGD_STAT_FLAG (1<<0)
218#define VIA8233_REG_SGD_STAT_EOL (1<<1)
219#define VIA8233_REG_SGD_STAT_STOP (1<<2)
220#define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
221#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
222#define VIA8233_REG_SGD_CHAN_SDX 0
223#define VIA8233_REG_SGD_CHAN_MULTI 4
224#define VIA8233_REG_SGD_CHAN_REC 6
225#define VIA8233_REG_SGD_CHAN_REC1 7
226
227#define VIA_REG_GPI_STATUS 0x88
228#define VIA_REG_GPI_INTR 0x8c
229
230/* multi-channel and capture registers for via8233 */
231DEFINE_VIA_REGSET(MULTPLAY, 0x40);
232DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
233
234/* via8233-specific registers */
235#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
236#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
237#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
238#define VIA_REG_MULTPLAY_FMT_8BIT 0x00
239#define VIA_REG_MULTPLAY_FMT_16BIT 0x80
240#define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
241#define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
242#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
243
244#define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
245
246#define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
247#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
248#define VIA_REG_CAPTURE_CHANNEL_LINE 0
249#define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
250
251#define VIA_TBL_BIT_FLAG 0x40000000
252#define VIA_TBL_BIT_EOL 0x80000000
253
254/* pci space */
255#define VIA_ACLINK_STAT 0x40
256#define VIA_ACLINK_C11_READY 0x20
257#define VIA_ACLINK_C10_READY 0x10
258#define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
259#define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
260#define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
261#define VIA_ACLINK_CTRL 0x41
262#define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
263#define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
264#define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
265#define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
266#define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
267#define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
268#define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
269#define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
270#define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
271 VIA_ACLINK_CTRL_RESET|\
272 VIA_ACLINK_CTRL_PCM|\
273 VIA_ACLINK_CTRL_VRA)
274#define VIA_FUNC_ENABLE 0x42
275#define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
276#define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
277#define VIA_FUNC_RX2C_WRITE 0x20
278#define VIA_FUNC_SB_FIFO_EMPTY 0x10
279#define VIA_FUNC_ENABLE_GAME 0x08
280#define VIA_FUNC_ENABLE_FM 0x04
281#define VIA_FUNC_ENABLE_MIDI 0x02
282#define VIA_FUNC_ENABLE_SB 0x01
283#define VIA_PNP_CONTROL 0x43
284#define VIA_FM_NMI_CTRL 0x48
285#define VIA8233_VOLCHG_CTRL 0x48
286#define VIA8233_SPDIF_CTRL 0x49
287#define VIA8233_SPDIF_DX3 0x08
288#define VIA8233_SPDIF_SLOT_MASK 0x03
289#define VIA8233_SPDIF_SLOT_1011 0x00
290#define VIA8233_SPDIF_SLOT_34 0x01
291#define VIA8233_SPDIF_SLOT_78 0x02
292#define VIA8233_SPDIF_SLOT_69 0x03
293
294/*
295 */
296
297#define VIA_DXS_AUTO 0
298#define VIA_DXS_ENABLE 1
299#define VIA_DXS_DISABLE 2
300#define VIA_DXS_48K 3
301#define VIA_DXS_NO_VRA 4
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200302#define VIA_DXS_SRC 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304
305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * pcm stream
307 */
308
309struct snd_via_sg_table {
310 unsigned int offset;
311 unsigned int size;
312} ;
313
314#define VIA_TABLE_SIZE 255
315
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100316struct viadev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned int reg_offset;
318 unsigned long port;
319 int direction; /* playback = 0, capture = 1 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100320 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int running;
322 unsigned int tbl_entries; /* # descriptors */
323 struct snd_dma_buffer table;
324 struct snd_via_sg_table *idx_table;
325 /* for recovery from the unexpected pointer */
326 unsigned int lastpos;
327 unsigned int fragsize;
328 unsigned int bufsize;
329 unsigned int bufsize2;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200330 int hwptr_done; /* processed frame position in the buffer */
331 int in_interrupt;
332 int shadow_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
335
336enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
337enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
338
339#define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
340
341struct via_rate_lock {
342 spinlock_t lock;
343 int rate;
344 int used;
345};
346
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100347struct via82xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int irq;
349
350 unsigned long port;
351 struct resource *mpu_res;
352 int chip_type;
353 unsigned char revision;
354
355 unsigned char old_legacy;
356 unsigned char old_legacy_cfg;
357#ifdef CONFIG_PM
358 unsigned char legacy_saved;
359 unsigned char legacy_cfg_saved;
360 unsigned char spdif_ctrl_saved;
361 unsigned char capture_src_saved[2];
362 unsigned int mpu_port_saved;
363#endif
364
Honza Maly00f226d2005-10-14 18:18:01 +0200365 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
366 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
369
370 struct pci_dev *pci;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100371 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 unsigned int num_devs;
374 unsigned int playback_devno, multi_devno, capture_devno;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100375 struct viadev devs[VIA_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct via_rate_lock rates[2]; /* playback and capture */
377 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
378 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200379 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
381
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100382 struct snd_pcm *pcms[2];
383 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100385 struct snd_ac97_bus *ac97_bus;
386 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned int ac97_clock;
388 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
389
390 spinlock_t reg_lock;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100391 struct snd_info_entry *proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393#ifdef SUPPORT_JOYSTICK
394 struct gameport *gameport;
395#endif
396};
397
398static struct pci_device_id snd_via82xx_ids[] = {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200399 /* 0x1106, 0x3058 */
400 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
401 /* 0x1106, 0x3059 */
402 { 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 -0700403 { 0, }
404};
405
406MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
407
408/*
409 */
410
411/*
412 * allocate and initialize the descriptor buffers
413 * periods = number of periods
414 * fragsize = period size in bytes
415 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100416static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct pci_dev *pci,
418 unsigned int periods, unsigned int fragsize)
419{
420 unsigned int i, idx, ofs, rest;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100421 struct via82xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
423
424 if (dev->table.area == NULL) {
425 /* the start of each lists must be aligned to 8 bytes,
426 * but the kernel pages are much bigger, so we don't care
427 */
428 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
429 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
430 &dev->table) < 0)
431 return -ENOMEM;
432 }
433 if (! dev->idx_table) {
434 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
435 if (! dev->idx_table)
436 return -ENOMEM;
437 }
438
439 /* fill the entries */
440 idx = 0;
441 ofs = 0;
442 for (i = 0; i < periods; i++) {
443 rest = fragsize;
444 /* fill descriptors for a period.
445 * a period can be split to several descriptors if it's
446 * over page boundary.
447 */
448 do {
449 unsigned int r;
450 unsigned int flag;
451
452 if (idx >= VIA_TABLE_SIZE) {
453 snd_printk(KERN_ERR "via82xx: too much table size!\n");
454 return -EINVAL;
455 }
456 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
457 r = PAGE_SIZE - (ofs % PAGE_SIZE);
458 if (rest < r)
459 r = rest;
460 rest -= r;
461 if (! rest) {
462 if (i == periods - 1)
463 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
464 else
465 flag = VIA_TBL_BIT_FLAG; /* period boundary */
466 } else
467 flag = 0; /* period continues to the next */
468 // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
469 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
470 dev->idx_table[idx].offset = ofs;
471 dev->idx_table[idx].size = r;
472 ofs += r;
473 idx++;
474 } while (rest > 0);
475 }
476 dev->tbl_entries = idx;
477 dev->bufsize = periods * fragsize;
478 dev->bufsize2 = dev->bufsize / 2;
479 dev->fragsize = fragsize;
480 return 0;
481}
482
483
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100484static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct pci_dev *pci)
486{
487 if (dev->table.area) {
488 snd_dma_free_pages(&dev->table);
489 dev->table.area = NULL;
490 }
Jesper Juhl4d572772005-05-30 17:30:32 +0200491 kfree(dev->idx_table);
492 dev->idx_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 0;
494}
495
496/*
497 * Basic I/O
498 */
499
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100500static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 return inl(VIAREG(chip, AC97));
503}
504
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100505static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 outl(val, VIAREG(chip, AC97));
508}
509
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100510static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 unsigned int timeout = 1000; /* 1ms */
513 unsigned int val;
514
515 while (timeout-- > 0) {
516 udelay(1);
517 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
518 return val & 0xffff;
519 }
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100520 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
521 secondary, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -EIO;
523}
524
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100525static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 unsigned int timeout = 1000; /* 1ms */
528 unsigned int val, val1;
529 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
530 VIA_REG_AC97_SECONDARY_VALID;
531
532 while (timeout-- > 0) {
533 val = snd_via82xx_codec_xread(chip);
534 val1 = val & (VIA_REG_AC97_BUSY | stat);
535 if (val1 == stat)
536 return val & 0xffff;
537 udelay(1);
538 }
539 return -EIO;
540}
541
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100542static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100544 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int err;
546 err = snd_via82xx_codec_ready(chip, ac97->num);
547 /* here we need to wait fairly for long time.. */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +0200548 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100551static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 unsigned short reg,
553 unsigned short val)
554{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100555 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 unsigned int xval;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
559 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
560 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
561 xval |= val << VIA_REG_AC97_DATA_SHIFT;
562 snd_via82xx_codec_xwrite(chip, xval);
563 snd_via82xx_codec_ready(chip, ac97->num);
564}
565
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100566static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100568 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 unsigned int xval, val = 0xffff;
570 int again = 0;
571
572 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
573 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
574 xval |= VIA_REG_AC97_READ;
575 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
576 while (1) {
577 if (again++ > 3) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100578 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
579 ac97->num, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0xffff;
581 }
582 snd_via82xx_codec_xwrite(chip, xval);
583 udelay (20);
584 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
585 udelay(25);
586 val = snd_via82xx_codec_xread(chip);
587 break;
588 }
589 }
590 return val & 0xffff;
591}
592
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100593static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
596 VIADEV_REG(viadev, OFFSET_CONTROL));
597 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
598 udelay(50);
599 /* disable interrupts */
600 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
601 /* clear interrupts */
602 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
603 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
604 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
605 viadev->lastpos = 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200606 viadev->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609
610/*
611 * Interrupt handler
Karsten Wiese4f550df2005-10-18 14:31:07 +0200612 * Used for 686 and 8233A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200614static irqreturn_t snd_via686_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100616 struct via82xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 unsigned int status;
618 unsigned int i;
619
620 status = inl(VIAREG(chip, SGD_SHADOW));
621 if (! (status & chip->intr_mask)) {
622 if (chip->rmidi)
623 /* check mpu401 interrupt */
624 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
625 return IRQ_NONE;
626 }
627
628 /* check status for each stream */
629 spin_lock(&chip->reg_lock);
630 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100631 struct viadev *viadev = &chip->devs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
Karsten Wiese4f550df2005-10-18 14:31:07 +0200633 if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 continue;
635 if (viadev->substream && viadev->running) {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200636 /*
637 * Update hwptr_done based on 'period elapsed'
638 * interrupts. We'll use it, when the chip returns 0
639 * for OFFSET_CURR_COUNT.
640 */
641 if (c_status & VIA_REG_STAT_EOL)
642 viadev->hwptr_done = 0;
643 else
644 viadev->hwptr_done += viadev->fragsize;
645 viadev->in_interrupt = c_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 spin_unlock(&chip->reg_lock);
647 snd_pcm_period_elapsed(viadev->substream);
648 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200649 viadev->in_interrupt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
652 }
653 spin_unlock(&chip->reg_lock);
654 return IRQ_HANDLED;
655}
656
657/*
Karsten Wiese4f550df2005-10-18 14:31:07 +0200658 * Interrupt handler
659 */
660static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id, struct pt_regs *regs)
661{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100662 struct via82xx *chip = dev_id;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200663 unsigned int status;
664 unsigned int i;
665 int irqreturn = 0;
666
667 /* check status for each stream */
668 spin_lock(&chip->reg_lock);
669 status = inl(VIAREG(chip, SGD_SHADOW));
670
671 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100672 struct viadev *viadev = &chip->devs[i];
673 struct snd_pcm_substream *substream;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200674 unsigned char c_status, shadow_status;
675
676 shadow_status = (status >> viadev->shadow_shift) &
677 (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
678 VIA_REG_STAT_FLAG);
679 c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
680 if (!c_status)
681 continue;
682
683 substream = viadev->substream;
684 if (substream && viadev->running) {
685 /*
686 * Update hwptr_done based on 'period elapsed'
687 * interrupts. We'll use it, when the chip returns 0
688 * for OFFSET_CURR_COUNT.
689 */
690 if (c_status & VIA_REG_STAT_EOL)
691 viadev->hwptr_done = 0;
692 else
693 viadev->hwptr_done += viadev->fragsize;
694 viadev->in_interrupt = c_status;
695 if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
696 viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
697 spin_unlock(&chip->reg_lock);
698
699 snd_pcm_period_elapsed(substream);
700
701 spin_lock(&chip->reg_lock);
702 viadev->in_interrupt = 0;
703 }
704 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
705 irqreturn = 1;
706 }
707 spin_unlock(&chip->reg_lock);
708 return IRQ_RETVAL(irqreturn);
709}
710
711/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * PCM callbacks
713 */
714
715/*
716 * trigger callback
717 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100718static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100720 struct via82xx *chip = snd_pcm_substream_chip(substream);
721 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned char val;
723
724 if (chip->chip_type != TYPE_VIA686)
725 val = VIA_REG_CTRL_INT;
726 else
727 val = 0;
728 switch (cmd) {
729 case SNDRV_PCM_TRIGGER_START:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200730 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 val |= VIA_REG_CTRL_START;
732 viadev->running = 1;
733 break;
734 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200735 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 val = VIA_REG_CTRL_TERMINATE;
737 viadev->running = 0;
738 break;
739 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
740 val |= VIA_REG_CTRL_PAUSE;
741 viadev->running = 0;
742 break;
743 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
744 viadev->running = 1;
745 break;
746 default:
747 return -EINVAL;
748 }
749 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
750 if (cmd == SNDRV_PCM_TRIGGER_STOP)
751 snd_via82xx_channel_reset(chip, viadev);
752 return 0;
753}
754
755
756/*
757 * pointer callbacks
758 */
759
760/*
761 * calculate the linear position at the given sg-buffer index and the rest count
762 */
763
764#define check_invalid_pos(viadev,pos) \
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100765 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
766 viadev->lastpos < viadev->bufsize2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100768static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
769 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 unsigned int size, base, res;
772
773 size = viadev->idx_table[idx].size;
774 base = viadev->idx_table[idx].offset;
775 res = base + size - count;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200776 if (res >= viadev->bufsize)
777 res -= viadev->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* check the validity of the calculated position */
780 if (size < count) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100781 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
782 (int)size, (int)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 res = viadev->lastpos;
784 } else {
785 if (! count) {
786 /* Some mobos report count = 0 on the DMA boundary,
787 * i.e. count = size indeed.
788 * Let's check whether this step is above the expected size.
789 */
790 int delta = res - viadev->lastpos;
791 if (delta < 0)
792 delta += viadev->bufsize;
793 if ((unsigned int)delta > viadev->fragsize)
794 res = base;
795 }
796 if (check_invalid_pos(viadev, res)) {
797#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100798 printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
799 "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
800 "count = 0x%x\n", idx, viadev->tbl_entries,
801 viadev->lastpos, viadev->bufsize2,
802 viadev->idx_table[idx].offset,
803 viadev->idx_table[idx].size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#endif
805 /* count register returns full size when end of buffer is reached */
806 res = base + size;
807 if (check_invalid_pos(viadev, res)) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100808 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
809 "using last valid pointer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 res = viadev->lastpos;
811 }
812 }
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return res;
815}
816
817/*
818 * get the current pointer on via686
819 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100820static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100822 struct via82xx *chip = snd_pcm_substream_chip(substream);
823 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 unsigned int idx, ptr, count, res;
825
826 snd_assert(viadev->tbl_entries, return 0);
827 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
828 return 0;
829
830 spin_lock(&chip->reg_lock);
831 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
832 /* The via686a does not have the current index register,
833 * so we need to calculate the index from CURR_PTR.
834 */
835 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
836 if (ptr <= (unsigned int)viadev->table.addr)
837 idx = 0;
838 else /* CURR_PTR holds the address + 8 */
839 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
840 res = calc_linear_pos(viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200841 viadev->lastpos = res; /* remember the last position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 spin_unlock(&chip->reg_lock);
843
844 return bytes_to_frames(substream->runtime, res);
845}
846
847/*
848 * get the current pointer on via823x
849 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100850static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100852 struct via82xx *chip = snd_pcm_substream_chip(substream);
853 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 unsigned int idx, count, res;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200855 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 snd_assert(viadev->tbl_entries, return 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200860 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
861 status = viadev->in_interrupt;
862 if (!status)
863 status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
864
865 if (!(status & VIA_REG_STAT_ACTIVE)) {
866 res = 0;
867 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Karsten Wiese4f550df2005-10-18 14:31:07 +0200869 if (count & 0xffffff) {
870 idx = count >> 24;
871 if (idx >= viadev->tbl_entries) {
872#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100873 printk(KERN_DEBUG "fail: invalid idx = %i/%i\n", idx,
874 viadev->tbl_entries);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200875#endif
876 res = viadev->lastpos;
877 } else {
878 count &= 0xffffff;
879 res = calc_linear_pos(viadev, idx, count);
880 }
881 } else {
882 res = viadev->hwptr_done;
883 if (!viadev->in_interrupt) {
884 if (status & VIA_REG_STAT_EOL) {
885 res = 0;
886 } else
887 if (status & VIA_REG_STAT_FLAG) {
888 res += viadev->fragsize;
889 }
890 }
891 }
892unlock:
893 viadev->lastpos = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 spin_unlock(&chip->reg_lock);
895
896 return bytes_to_frames(substream->runtime, res);
897}
898
899
900/*
901 * hw_params callback:
902 * allocate the buffer and build up the buffer description table
903 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100904static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
905 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100907 struct via82xx *chip = snd_pcm_substream_chip(substream);
908 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int err;
910
911 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
912 if (err < 0)
913 return err;
914 err = build_via_table(viadev, substream, chip->pci,
915 params_periods(hw_params),
916 params_period_bytes(hw_params));
917 if (err < 0)
918 return err;
919
920 return 0;
921}
922
923/*
924 * hw_free callback:
925 * clean up the buffer description table and release the buffer
926 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100927static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100929 struct via82xx *chip = snd_pcm_substream_chip(substream);
930 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 clean_via_table(viadev, substream, chip->pci);
933 snd_pcm_lib_free_pages(substream);
934 return 0;
935}
936
937
938/*
939 * set up the table pointer
940 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100941static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 snd_via82xx_codec_ready(chip, 0);
944 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
945 udelay(20);
946 snd_via82xx_codec_ready(chip, 0);
947}
948
949/*
950 * prepare callback for playback and capture on via686
951 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100952static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
953 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 snd_via82xx_channel_reset(chip, viadev);
956 /* this must be set after channel_reset */
957 snd_via82xx_set_table_ptr(chip, viadev);
958 outb(VIA_REG_TYPE_AUTOSTART |
959 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
960 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
961 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
962 VIA_REG_TYPE_INT_EOL |
963 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
964}
965
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100966static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100968 struct via82xx *chip = snd_pcm_substream_chip(substream);
969 struct viadev *viadev = substream->runtime->private_data;
970 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
973 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
974 via686_setup_format(chip, viadev, runtime);
975 return 0;
976}
977
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100978static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100980 struct via82xx *chip = snd_pcm_substream_chip(substream);
981 struct viadev *viadev = substream->runtime->private_data;
982 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
985 via686_setup_format(chip, viadev, runtime);
986 return 0;
987}
988
989/*
990 * lock the current rate
991 */
992static int via_lock_rate(struct via_rate_lock *rec, int rate)
993{
994 int changed = 0;
995
996 spin_lock_irq(&rec->lock);
997 if (rec->rate != rate) {
998 if (rec->rate && rec->used > 1) /* already set */
999 changed = -EINVAL;
1000 else {
1001 rec->rate = rate;
1002 changed = 1;
1003 }
1004 }
1005 spin_unlock_irq(&rec->lock);
1006 return changed;
1007}
1008
1009/*
1010 * prepare callback for DSX playback on via823x
1011 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001012static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001014 struct via82xx *chip = snd_pcm_substream_chip(substream);
1015 struct viadev *viadev = substream->runtime->private_data;
1016 struct snd_pcm_runtime *runtime = substream->runtime;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001017 int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int rate_changed;
1019 u32 rbits;
1020
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001021 if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return rate_changed;
Takashi Iwai16d3f142005-08-15 15:02:28 +02001023 if (rate_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1025 chip->no_vra ? 48000 : runtime->rate);
Takashi Iwai16d3f142005-08-15 15:02:28 +02001026 if (chip->spdif_on && viadev->reg_offset == 0x30)
1027 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (runtime->rate == 48000)
1030 rbits = 0xfffff;
1031 else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001032 rbits = (0x100000 / 48000) * runtime->rate +
1033 ((0x100000 % 48000) * runtime->rate) / 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
1035 snd_via82xx_channel_reset(chip, viadev);
1036 snd_via82xx_set_table_ptr(chip, viadev);
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001037 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1038 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1039 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1040 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1042 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1043 rbits | /* rate */
1044 0xff000000, /* STOP index is never reached */
1045 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1046 udelay(20);
1047 snd_via82xx_codec_ready(chip, 0);
1048 return 0;
1049}
1050
1051/*
1052 * prepare callback for multi-channel playback on via823x
1053 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001054static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001056 struct via82xx *chip = snd_pcm_substream_chip(substream);
1057 struct viadev *viadev = substream->runtime->private_data;
1058 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 unsigned int slots;
1060 int fmt;
1061
1062 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1063 return -EINVAL;
1064 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1065 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1066 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1067 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1068 snd_via82xx_channel_reset(chip, viadev);
1069 snd_via82xx_set_table_ptr(chip, viadev);
1070
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001071 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1072 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 fmt |= runtime->channels << 4;
1074 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1075#if 0
1076 if (chip->revision == VIA_REV_8233A)
1077 slots = 0;
1078 else
1079#endif
1080 {
1081 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1082 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1083 switch (runtime->channels) {
1084 case 1: slots = (1<<0) | (1<<4); break;
1085 case 2: slots = (1<<0) | (2<<4); break;
1086 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1087 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1088 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1089 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1090 default: slots = 0; break;
1091 }
1092 }
1093 /* STOP index is never reached */
1094 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1095 udelay(20);
1096 snd_via82xx_codec_ready(chip, 0);
1097 return 0;
1098}
1099
1100/*
1101 * prepare callback for capture on via823x
1102 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001103static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001105 struct via82xx *chip = snd_pcm_substream_chip(substream);
1106 struct viadev *viadev = substream->runtime->private_data;
1107 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1110 return -EINVAL;
1111 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1112 snd_via82xx_channel_reset(chip, viadev);
1113 snd_via82xx_set_table_ptr(chip, viadev);
1114 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1115 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1116 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1117 0xff000000, /* STOP index is never reached */
1118 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1119 udelay(20);
1120 snd_via82xx_codec_ready(chip, 0);
1121 return 0;
1122}
1123
1124
1125/*
1126 * pcm hardware definition, identical for both playback and capture
1127 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001128static struct snd_pcm_hardware snd_via82xx_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1131 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1132 SNDRV_PCM_INFO_MMAP_VALID |
Jaroslav Kysela41e48452005-08-18 13:43:12 +02001133 /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 SNDRV_PCM_INFO_PAUSE),
1135 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1136 .rates = SNDRV_PCM_RATE_48000,
1137 .rate_min = 48000,
1138 .rate_max = 48000,
1139 .channels_min = 1,
1140 .channels_max = 2,
1141 .buffer_bytes_max = 128 * 1024,
1142 .period_bytes_min = 32,
1143 .period_bytes_max = 128 * 1024,
1144 .periods_min = 2,
1145 .periods_max = VIA_TABLE_SIZE / 2,
1146 .fifo_size = 0,
1147};
1148
1149
1150/*
1151 * open callback skeleton
1152 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001153static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1154 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001156 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int err;
1158 struct via_rate_lock *ratep;
1159
1160 runtime->hw = snd_via82xx_hw;
1161
1162 /* set the hw rate condition */
1163 ratep = &chip->rates[viadev->direction];
1164 spin_lock_irq(&ratep->lock);
1165 ratep->used++;
1166 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1167 /* DXS#3 and spdif is on */
1168 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1169 snd_pcm_limit_hw_rates(runtime);
1170 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1171 /* fixed DXS playback rate */
1172 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1173 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001174 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1175 /* use full SRC capabilities of DXS */
1176 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1177 SNDRV_PCM_RATE_8000_48000);
1178 runtime->hw.rate_min = 8000;
1179 runtime->hw.rate_max = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 } else if (! ratep->rate) {
1181 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1182 runtime->hw.rates = chip->ac97->rates[idx];
1183 snd_pcm_limit_hw_rates(runtime);
1184 } else {
1185 /* a fixed rate */
1186 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1187 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1188 }
1189 spin_unlock_irq(&ratep->lock);
1190
1191 /* we may remove following constaint when we modify table entries
1192 in interrupt */
1193 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1194 return err;
1195
1196 runtime->private_data = viadev;
1197 viadev->substream = substream;
1198
1199 return 0;
1200}
1201
1202
1203/*
1204 * open callback for playback on via686 and via823x DSX
1205 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001206static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001208 struct via82xx *chip = snd_pcm_substream_chip(substream);
1209 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int err;
1211
1212 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1213 return err;
1214 return 0;
1215}
1216
1217/*
1218 * open callback for playback on via823x multi-channel
1219 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001220static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001222 struct via82xx *chip = snd_pcm_substream_chip(substream);
1223 struct viadev *viadev = &chip->devs[chip->multi_devno];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int err;
1225 /* channels constraint for VIA8233A
1226 * 3 and 5 channels are not supported
1227 */
1228 static unsigned int channels[] = {
1229 1, 2, 4, 6
1230 };
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001231 static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 .count = ARRAY_SIZE(channels),
1233 .list = channels,
1234 .mask = 0,
1235 };
1236
1237 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1238 return err;
1239 substream->runtime->hw.channels_max = 6;
1240 if (chip->revision == VIA_REV_8233A)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001241 snd_pcm_hw_constraint_list(substream->runtime, 0,
1242 SNDRV_PCM_HW_PARAM_CHANNELS,
1243 &hw_constraints_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245}
1246
1247/*
1248 * open callback for capture on via686 and via823x
1249 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001250static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001252 struct via82xx *chip = snd_pcm_substream_chip(substream);
1253 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 return snd_via82xx_pcm_open(chip, viadev, substream);
1256}
1257
1258/*
1259 * close callback
1260 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001261static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001263 struct via82xx *chip = snd_pcm_substream_chip(substream);
1264 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 struct via_rate_lock *ratep;
1266
1267 /* release the rate lock */
1268 ratep = &chip->rates[viadev->direction];
1269 spin_lock_irq(&ratep->lock);
1270 ratep->used--;
1271 if (! ratep->used)
1272 ratep->rate = 0;
1273 spin_unlock_irq(&ratep->lock);
1274
1275 viadev->substream = NULL;
1276 return 0;
1277}
1278
1279
1280/* via686 playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001281static struct snd_pcm_ops snd_via686_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 .open = snd_via82xx_playback_open,
1283 .close = snd_via82xx_pcm_close,
1284 .ioctl = snd_pcm_lib_ioctl,
1285 .hw_params = snd_via82xx_hw_params,
1286 .hw_free = snd_via82xx_hw_free,
1287 .prepare = snd_via686_playback_prepare,
1288 .trigger = snd_via82xx_pcm_trigger,
1289 .pointer = snd_via686_pcm_pointer,
1290 .page = snd_pcm_sgbuf_ops_page,
1291};
1292
1293/* via686 capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001294static struct snd_pcm_ops snd_via686_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 .open = snd_via82xx_capture_open,
1296 .close = snd_via82xx_pcm_close,
1297 .ioctl = snd_pcm_lib_ioctl,
1298 .hw_params = snd_via82xx_hw_params,
1299 .hw_free = snd_via82xx_hw_free,
1300 .prepare = snd_via686_capture_prepare,
1301 .trigger = snd_via82xx_pcm_trigger,
1302 .pointer = snd_via686_pcm_pointer,
1303 .page = snd_pcm_sgbuf_ops_page,
1304};
1305
1306/* via823x DSX playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001307static struct snd_pcm_ops snd_via8233_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 .open = snd_via82xx_playback_open,
1309 .close = snd_via82xx_pcm_close,
1310 .ioctl = snd_pcm_lib_ioctl,
1311 .hw_params = snd_via82xx_hw_params,
1312 .hw_free = snd_via82xx_hw_free,
1313 .prepare = snd_via8233_playback_prepare,
1314 .trigger = snd_via82xx_pcm_trigger,
1315 .pointer = snd_via8233_pcm_pointer,
1316 .page = snd_pcm_sgbuf_ops_page,
1317};
1318
1319/* via823x multi-channel playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001320static struct snd_pcm_ops snd_via8233_multi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 .open = snd_via8233_multi_open,
1322 .close = snd_via82xx_pcm_close,
1323 .ioctl = snd_pcm_lib_ioctl,
1324 .hw_params = snd_via82xx_hw_params,
1325 .hw_free = snd_via82xx_hw_free,
1326 .prepare = snd_via8233_multi_prepare,
1327 .trigger = snd_via82xx_pcm_trigger,
1328 .pointer = snd_via8233_pcm_pointer,
1329 .page = snd_pcm_sgbuf_ops_page,
1330};
1331
1332/* via823x capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001333static struct snd_pcm_ops snd_via8233_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 .open = snd_via82xx_capture_open,
1335 .close = snd_via82xx_pcm_close,
1336 .ioctl = snd_pcm_lib_ioctl,
1337 .hw_params = snd_via82xx_hw_params,
1338 .hw_free = snd_via82xx_hw_free,
1339 .prepare = snd_via8233_capture_prepare,
1340 .trigger = snd_via82xx_pcm_trigger,
1341 .pointer = snd_via8233_pcm_pointer,
1342 .page = snd_pcm_sgbuf_ops_page,
1343};
1344
1345
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001346static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1347 int shadow_pos, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 chip->devs[idx].reg_offset = reg_offset;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001350 chip->devs[idx].shadow_shift = shadow_pos * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 chip->devs[idx].direction = direction;
1352 chip->devs[idx].port = chip->port + reg_offset;
1353}
1354
1355/*
1356 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1357 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001358static int __devinit snd_via8233_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001360 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int i, err;
1362
1363 chip->playback_devno = 0; /* x 4 */
1364 chip->multi_devno = 4; /* x 1 */
1365 chip->capture_devno = 5; /* x 2 */
1366 chip->num_devs = 7;
1367 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1368
1369 /* PCM #0: 4 DSX playbacks and 1 capture */
1370 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1371 if (err < 0)
1372 return err;
1373 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1374 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1375 pcm->private_data = chip;
1376 strcpy(pcm->name, chip->card->shortname);
1377 chip->pcms[0] = pcm;
1378 /* set up playbacks */
1379 for (i = 0; i < 4; i++)
Karsten Wiese4f550df2005-10-18 14:31:07 +02001380 init_viadev(chip, i, 0x10 * i, i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001382 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001385 snd_dma_pci_data(chip->pci),
1386 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return err;
1388
1389 /* PCM #1: multi-channel playback and 2nd capture */
1390 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1391 if (err < 0)
1392 return err;
1393 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1394 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1395 pcm->private_data = chip;
1396 strcpy(pcm->name, chip->card->shortname);
1397 chip->pcms[1] = pcm;
1398 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001399 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* set up capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001401 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001404 snd_dma_pci_data(chip->pci),
1405 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return err;
1407
1408 return 0;
1409}
1410
1411/*
1412 * create pcm instances for VIA8233A
1413 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001414static int __devinit snd_via8233a_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001416 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int err;
1418
1419 chip->multi_devno = 0;
1420 chip->playback_devno = 1;
1421 chip->capture_devno = 2;
1422 chip->num_devs = 3;
1423 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1424
1425 /* PCM #0: multi-channel playback and capture */
1426 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1427 if (err < 0)
1428 return err;
1429 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1430 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1431 pcm->private_data = chip;
1432 strcpy(pcm->name, chip->card->shortname);
1433 chip->pcms[0] = pcm;
1434 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001435 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001437 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001440 snd_dma_pci_data(chip->pci),
1441 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return err;
1443
1444 /* SPDIF supported? */
1445 if (! ac97_can_spdif(chip->ac97))
1446 return 0;
1447
1448 /* PCM #1: DXS3 playback (for spdif) */
1449 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1450 if (err < 0)
1451 return err;
1452 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1453 pcm->private_data = chip;
1454 strcpy(pcm->name, chip->card->shortname);
1455 chip->pcms[1] = pcm;
1456 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001457 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001460 snd_dma_pci_data(chip->pci),
1461 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return err;
1463
1464 return 0;
1465}
1466
1467/*
1468 * create a pcm instance for via686a/b
1469 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001470static int __devinit snd_via686_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001472 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int err;
1474
1475 chip->playback_devno = 0;
1476 chip->capture_devno = 1;
1477 chip->num_devs = 2;
1478 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1479
1480 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1481 if (err < 0)
1482 return err;
1483 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1484 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1485 pcm->private_data = chip;
1486 strcpy(pcm->name, chip->card->shortname);
1487 chip->pcms[0] = pcm;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001488 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1489 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001492 snd_dma_pci_data(chip->pci),
1493 64*1024, 128*1024)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return err;
1495
1496 return 0;
1497}
1498
1499
1500/*
1501 * Mixer part
1502 */
1503
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001504static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1505 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 /* formerly they were "Line" and "Mic", but it looks like that they
1508 * have nothing to do with the actual physical connections...
1509 */
1510 static char *texts[2] = {
1511 "Input1", "Input2"
1512 };
1513 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1514 uinfo->count = 1;
1515 uinfo->value.enumerated.items = 2;
1516 if (uinfo->value.enumerated.item >= 2)
1517 uinfo->value.enumerated.item = 1;
1518 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1519 return 0;
1520}
1521
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001522static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1523 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001525 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1527 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1528 return 0;
1529}
1530
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001531static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1532 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001534 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1536 u8 val, oval;
1537
1538 spin_lock_irq(&chip->reg_lock);
1539 oval = inb(port);
1540 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1541 if (ucontrol->value.enumerated.item[0])
1542 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1543 if (val != oval)
1544 outb(val, port);
1545 spin_unlock_irq(&chip->reg_lock);
1546 return val != oval;
1547}
1548
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001549static struct snd_kcontrol_new snd_via8233_capture_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 .name = "Input Source Select",
1551 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1552 .info = snd_via8233_capture_source_info,
1553 .get = snd_via8233_capture_source_get,
1554 .put = snd_via8233_capture_source_put,
1555};
1556
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001557static int snd_via8233_dxs3_spdif_info(struct snd_kcontrol *kcontrol,
1558 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1561 uinfo->count = 1;
1562 uinfo->value.integer.min = 0;
1563 uinfo->value.integer.max = 1;
1564 return 0;
1565}
1566
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001567static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001570 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 u8 val;
1572
1573 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1574 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1575 return 0;
1576}
1577
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001578static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1579 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001581 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 u8 val, oval;
1583
1584 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1585 val = oval & ~VIA8233_SPDIF_DX3;
1586 if (ucontrol->value.integer.value[0])
1587 val |= VIA8233_SPDIF_DX3;
1588 /* save the spdif flag for rate filtering */
1589 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1590 if (val != oval) {
1591 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1592 return 1;
1593 }
1594 return 0;
1595}
1596
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001597static struct snd_kcontrol_new snd_via8233_dxs3_spdif_control __devinitdata = {
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001598 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1600 .info = snd_via8233_dxs3_spdif_info,
1601 .get = snd_via8233_dxs3_spdif_get,
1602 .put = snd_via8233_dxs3_spdif_put,
1603};
1604
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001605static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1606 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1609 uinfo->count = 2;
1610 uinfo->value.integer.min = 0;
1611 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1612 return 0;
1613}
1614
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001615static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001618 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001619 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1620
1621 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1622 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1623 return 0;
1624}
1625
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001626static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1627 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001628{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001629 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001630 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1631 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return 0;
1633}
1634
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001635static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1636 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001638 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001639 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1640 unsigned long port = chip->port + 0x10 * idx;
1641 unsigned char val;
1642 int i, change = 0;
1643
1644 for (i = 0; i < 2; i++) {
1645 val = ucontrol->value.integer.value[i];
1646 if (val > VIA_DXS_MAX_VOLUME)
1647 val = VIA_DXS_MAX_VOLUME;
1648 val = VIA_DXS_MAX_VOLUME - val;
1649 change |= val != chip->playback_volume[idx][i];
1650 if (change) {
1651 chip->playback_volume[idx][i] = val;
1652 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1653 }
1654 }
1655 return change;
1656}
1657
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001658static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001660{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001661 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 unsigned int idx;
1663 unsigned char val;
1664 int i, change = 0;
1665
1666 for (i = 0; i < 2; i++) {
1667 val = ucontrol->value.integer.value[i];
1668 if (val > VIA_DXS_MAX_VOLUME)
1669 val = VIA_DXS_MAX_VOLUME;
1670 val = VIA_DXS_MAX_VOLUME - val;
Honza Maly00f226d2005-10-14 18:18:01 +02001671 if (val != chip->playback_volume_c[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 change = 1;
Honza Maly00f226d2005-10-14 18:18:01 +02001673 chip->playback_volume_c[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 for (idx = 0; idx < 4; idx++) {
1675 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02001676 chip->playback_volume[idx][i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1678 }
1679 }
1680 }
1681 return change;
1682}
1683
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001684static struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 .name = "PCM Playback Volume",
1686 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1687 .info = snd_via8233_dxs_volume_info,
Honza Maly00f226d2005-10-14 18:18:01 +02001688 .get = snd_via8233_pcmdxs_volume_get,
1689 .put = snd_via8233_pcmdxs_volume_put,
1690};
1691
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001692static struct snd_kcontrol_new snd_via8233_dxs_volume_control __devinitdata = {
Honza Maly00f226d2005-10-14 18:18:01 +02001693 .name = "VIA DXS Playback Volume",
1694 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1695 .count = 4,
1696 .info = snd_via8233_dxs_volume_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 .get = snd_via8233_dxs_volume_get,
1698 .put = snd_via8233_dxs_volume_put,
1699};
1700
1701/*
1702 */
1703
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001704static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001706 struct via82xx *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 chip->ac97_bus = NULL;
1708}
1709
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001710static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001712 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 chip->ac97 = NULL;
1714}
1715
1716static struct ac97_quirk ac97_quirks[] = {
1717 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001718 .subvendor = 0x1106,
1719 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 .codec_id = 0x56494161, /* VT1612A */
1721 .name = "Soltek SL-75DRV5",
1722 .type = AC97_TUNE_NONE
1723 },
1724 { /* FIXME: which codec? */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001725 .subvendor = 0x1106,
1726 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 .name = "ASRock K7VT2",
1728 .type = AC97_TUNE_HP_ONLY
1729 },
1730 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001731 .subvendor = 0x1019,
1732 .subdevice = 0x0a81,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 .name = "ECS K7VTA3",
1734 .type = AC97_TUNE_HP_ONLY
1735 },
1736 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001737 .subvendor = 0x1019,
1738 .subdevice = 0x0a85,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .name = "ECS L7VMM2",
1740 .type = AC97_TUNE_HP_ONLY
1741 },
1742 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001743 .subvendor = 0x1849,
1744 .subdevice = 0x3059,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .name = "ASRock K7VM2",
1746 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1747 },
1748 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001749 .subvendor = 0x14cd,
1750 .subdevice = 0x7002,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 .name = "Unknown",
1752 .type = AC97_TUNE_ALC_JACK
1753 },
1754 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001755 .subvendor = 0x1071,
1756 .subdevice = 0x8590,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .name = "Mitac Mobo",
1758 .type = AC97_TUNE_ALC_JACK
1759 },
1760 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001761 .subvendor = 0x161f,
1762 .subdevice = 0x202b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 .name = "Arima Notebook",
1764 .type = AC97_TUNE_HP_ONLY,
1765 },
Takashi Iwaidac8ddd2005-12-14 12:00:46 +01001766 {
1767 .subvendor = 0x161f,
1768 .subdevice = 0x2032,
1769 .name = "Targa Traveller 811",
1770 .type = AC97_TUNE_HP_ONLY,
1771 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 { } /* terminator */
1773};
1774
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001775static int __devinit snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001777 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001779 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 .write = snd_via82xx_codec_write,
1781 .read = snd_via82xx_codec_read,
1782 .wait = snd_via82xx_codec_wait,
1783 };
1784
1785 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1786 return err;
1787 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1788 chip->ac97_bus->clock = chip->ac97_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 memset(&ac97, 0, sizeof(ac97));
1791 ac97.private_data = chip;
1792 ac97.private_free = snd_via82xx_mixer_free_ac97;
1793 ac97.pci = chip->pci;
Sasha Khapyorsky2ba71972005-09-29 12:58:24 +02001794 ac97.scaps = AC97_SCAP_SKIP_MODEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1796 return err;
1797
1798 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1799
1800 if (chip->chip_type != TYPE_VIA686) {
1801 /* use slot 10/11 */
1802 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1803 }
1804
1805 return 0;
1806}
1807
1808#ifdef SUPPORT_JOYSTICK
1809#define JOYSTICK_ADDR 0x200
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001810static int __devinit snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct gameport *gp;
1813 struct resource *r;
1814
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001815 if (!joystick)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return -ENODEV;
1817
1818 r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1819 if (!r) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001820 printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n",
1821 JOYSTICK_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return -EBUSY;
1823 }
1824
1825 chip->gameport = gp = gameport_allocate_port();
1826 if (!gp) {
1827 printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02001828 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return -ENOMEM;
1830 }
1831
1832 gameport_set_name(gp, "VIA686 Gameport");
1833 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1834 gameport_set_dev_parent(gp, &chip->pci->dev);
1835 gp->io = JOYSTICK_ADDR;
1836 gameport_set_port_data(gp, r);
1837
1838 /* Enable legacy joystick port */
1839 *legacy |= VIA_FUNC_ENABLE_GAME;
1840 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1841
1842 gameport_register_port(chip->gameport);
1843
1844 return 0;
1845}
1846
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001847static void snd_via686_free_gameport(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 if (chip->gameport) {
1850 struct resource *r = gameport_get_port_data(chip->gameport);
1851
1852 gameport_unregister_port(chip->gameport);
1853 chip->gameport = NULL;
Takashi Iwaib1d57762005-10-10 11:56:31 +02001854 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856}
1857#else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001858static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 return -ENOSYS;
1861}
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001862static inline void snd_via686_free_gameport(struct via82xx *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863#endif
1864
1865
1866/*
1867 *
1868 */
1869
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001870static int __devinit snd_via8233_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 int i, err, caps;
1873 unsigned char val;
1874
1875 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1876 for (i = 0; i < caps; i++) {
1877 snd_via8233_capture_source.index = i;
1878 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1879 if (err < 0)
1880 return err;
1881 }
1882 if (ac97_can_spdif(chip->ac97)) {
1883 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1884 if (err < 0)
1885 return err;
1886 }
1887 if (chip->chip_type != TYPE_VIA8233A) {
1888 /* when no h/w PCM volume control is found, use DXS volume control
1889 * as the PCM vol control
1890 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001891 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 memset(&sid, 0, sizeof(sid));
1893 strcpy(sid.name, "PCM Playback Volume");
1894 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1895 if (! snd_ctl_find_id(chip->card, &sid)) {
Honza Maly00f226d2005-10-14 18:18:01 +02001896 snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
1897 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1898 if (err < 0)
1899 return err;
1900 }
1901 else /* Using DXS when PCM emulation is enabled is really weird */
1902 {
1903 /* Standalone DXS controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1905 if (err < 0)
1906 return err;
1907 }
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /* select spdif data slot 10/11 */
1910 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1911 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1912 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
1913 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1914
1915 return 0;
1916}
1917
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001918static int __devinit snd_via686_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 unsigned char legacy, legacy_cfg;
1921 int rev_h = 0;
1922
1923 legacy = chip->old_legacy;
1924 legacy_cfg = chip->old_legacy_cfg;
1925 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
1926 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
1927 if (chip->revision >= VIA_REV_686_H) {
1928 rev_h = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001929 if (mpu_port >= 0x200) { /* force MIDI */
1930 mpu_port &= 0xfffc;
1931 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932#ifdef CONFIG_PM
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001933 chip->mpu_port_saved = mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934#endif
1935 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001936 mpu_port = pci_resource_start(chip->pci, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001939 switch (mpu_port) { /* force MIDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 case 0x300:
1941 case 0x310:
1942 case 0x320:
1943 case 0x330:
1944 legacy_cfg &= ~(3 << 2);
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001945 legacy_cfg |= (mpu_port & 0x0030) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
1947 default: /* no, use BIOS settings */
1948 if (legacy & VIA_FUNC_ENABLE_MIDI)
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001949 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
1951 }
1952 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001953 if (mpu_port >= 0x200 &&
1954 (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
1955 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (rev_h)
1957 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
1958 legacy |= VIA_FUNC_ENABLE_MIDI;
1959 } else {
1960 if (rev_h)
1961 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
1962 legacy &= ~VIA_FUNC_ENABLE_MIDI;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001963 mpu_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965
1966 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1967 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1968 if (chip->mpu_res) {
1969 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001970 mpu_port, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 chip->irq, 0, &chip->rmidi) < 0) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001972 printk(KERN_WARNING "unable to initialize MPU-401"
1973 " at 0x%lx, skipping\n", mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1975 } else {
1976 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
1977 }
1978 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1979 }
1980
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001981 snd_via686_create_gameport(chip, &legacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983#ifdef CONFIG_PM
1984 chip->legacy_saved = legacy;
1985 chip->legacy_cfg_saved = legacy_cfg;
1986#endif
1987
1988 return 0;
1989}
1990
1991
1992/*
1993 * proc interface
1994 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001995static void snd_via82xx_proc_read(struct snd_info_entry *entry,
1996 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001998 struct via82xx *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int i;
2000
2001 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2002 for (i = 0; i < 0xa0; i += 4) {
2003 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2004 }
2005}
2006
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002007static void __devinit snd_via82xx_proc_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002009 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
2012 snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
2013}
2014
2015/*
2016 *
2017 */
2018
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002019static int snd_via82xx_chip_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 unsigned int val;
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002022 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 unsigned char pval;
2024
2025#if 0 /* broken on K7M? */
2026 if (chip->chip_type == TYPE_VIA686)
2027 /* disable all legacy ports */
2028 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2029#endif
2030 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2031 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2032 /* deassert ACLink reset, force SYNC */
2033 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2034 VIA_ACLINK_CTRL_ENABLE |
2035 VIA_ACLINK_CTRL_RESET |
2036 VIA_ACLINK_CTRL_SYNC);
2037 udelay(100);
2038#if 1 /* FIXME: should we do full reset here for all chip models? */
2039 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2040 udelay(100);
2041#else
2042 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2043 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2044 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2045 udelay(2);
2046#endif
2047 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2048 /* note - FM data out has trouble with non VRA codecs !! */
2049 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2050 udelay(100);
2051 }
2052
2053 /* Make sure VRA is enabled, in case we didn't do a
2054 * complete codec reset, above */
2055 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2056 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2057 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2058 /* note - FM data out has trouble with non VRA codecs !! */
2059 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2060 udelay(100);
2061 }
2062
2063 /* wait until codec ready */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002064 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 do {
2066 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2067 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2068 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002069 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002070 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
Takashi Iwai99b359b2005-10-20 18:26:44 +02002073 snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2076 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2077 VIA_REG_AC97_SECONDARY_VALID |
2078 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002079 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2081 VIA_REG_AC97_SECONDARY_VALID |
2082 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2083 do {
2084 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2085 chip->ac97_secondary = 1;
2086 goto __ac97_ok2;
2087 }
Nishanth Aravamudan8433a502005-10-24 15:02:37 +02002088 schedule_timeout_interruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002089 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /* This is ok, the most of motherboards have only one codec */
2091
2092 __ac97_ok2:
2093#endif
2094
2095 if (chip->chip_type == TYPE_VIA686) {
2096 /* route FM trap to IRQ, disable FM trap */
2097 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2098 /* disable all GPI interrupts */
2099 outl(0, VIAREG(chip, GPI_INTR));
2100 }
2101
2102 if (chip->chip_type != TYPE_VIA686) {
2103 /* Workaround for Award BIOS bug:
2104 * DXS channels don't work properly with VRA if MC97 is disabled.
2105 */
2106 struct pci_dev *pci;
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002107 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (pci) {
2109 unsigned char data;
2110 pci_read_config_byte(pci, 0x44, &data);
2111 pci_write_config_byte(pci, 0x44, data | 0x40);
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002112 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 }
2115
2116 if (chip->chip_type != TYPE_VIA8233A) {
2117 int i, idx;
2118 for (idx = 0; idx < 4; idx++) {
2119 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02002120 for (i = 0; i < 2; i++) {
2121 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002122 outb(chip->playback_volume_c[i],
2123 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
Honza Maly00f226d2005-10-14 18:18:01 +02002124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 }
2127
2128 return 0;
2129}
2130
2131#ifdef CONFIG_PM
2132/*
2133 * power management
2134 */
Takashi Iwai57feb832005-11-17 16:10:35 +01002135static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Takashi Iwai57feb832005-11-17 16:10:35 +01002137 struct snd_card *card = pci_get_drvdata(pci);
2138 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 int i;
2140
Takashi Iwai57feb832005-11-17 16:10:35 +01002141 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 for (i = 0; i < 2; i++)
Takashi Iwai57feb832005-11-17 16:10:35 +01002143 snd_pcm_suspend_all(chip->pcms[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 for (i = 0; i < chip->num_devs; i++)
2145 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2146 synchronize_irq(chip->irq);
2147 snd_ac97_suspend(chip->ac97);
2148
2149 /* save misc values */
2150 if (chip->chip_type != TYPE_VIA686) {
2151 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2152 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2153 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2154 }
2155
Takashi Iwai57feb832005-11-17 16:10:35 +01002156 pci_set_power_state(pci, PCI_D3hot);
2157 pci_disable_device(pci);
2158 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return 0;
2160}
2161
Takashi Iwai57feb832005-11-17 16:10:35 +01002162static int snd_via82xx_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Takashi Iwai57feb832005-11-17 16:10:35 +01002164 struct snd_card *card = pci_get_drvdata(pci);
2165 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 int i;
2167
Takashi Iwai57feb832005-11-17 16:10:35 +01002168 pci_restore_state(pci);
2169 pci_enable_device(pci);
2170 pci_set_power_state(pci, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 snd_via82xx_chip_init(chip);
2173
2174 if (chip->chip_type == TYPE_VIA686) {
2175 if (chip->mpu_port_saved)
2176 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2177 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2178 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2179 } else {
2180 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2181 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2182 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2183 }
2184
2185 snd_ac97_resume(chip->ac97);
2186
2187 for (i = 0; i < chip->num_devs; i++)
2188 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2189
Takashi Iwai57feb832005-11-17 16:10:35 +01002190 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return 0;
2192}
2193#endif /* CONFIG_PM */
2194
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002195static int snd_via82xx_free(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 unsigned int i;
2198
2199 if (chip->irq < 0)
2200 goto __end_hw;
2201 /* disable interrupts */
2202 for (i = 0; i < chip->num_devs; i++)
2203 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2204 synchronize_irq(chip->irq);
2205 __end_hw:
2206 if (chip->irq >= 0)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002207 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002208 release_and_free_resource(chip->mpu_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 pci_release_regions(chip->pci);
2210
2211 if (chip->chip_type == TYPE_VIA686) {
2212 snd_via686_free_gameport(chip);
2213 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2214 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2215 }
2216 pci_disable_device(chip->pci);
2217 kfree(chip);
2218 return 0;
2219}
2220
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002221static int snd_via82xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002223 struct via82xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return snd_via82xx_free(chip);
2225}
2226
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002227static int __devinit snd_via82xx_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct pci_dev *pci,
2229 int chip_type,
2230 int revision,
2231 unsigned int ac97_clock,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002232 struct via82xx ** r_via)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002234 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002236 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 .dev_free = snd_via82xx_dev_free,
2238 };
2239
2240 if ((err = pci_enable_device(pci)) < 0)
2241 return err;
2242
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002243 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 pci_disable_device(pci);
2245 return -ENOMEM;
2246 }
2247
2248 chip->chip_type = chip_type;
2249 chip->revision = revision;
2250
2251 spin_lock_init(&chip->reg_lock);
2252 spin_lock_init(&chip->rates[0].lock);
2253 spin_lock_init(&chip->rates[1].lock);
2254 chip->card = card;
2255 chip->pci = pci;
2256 chip->irq = -1;
2257
2258 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2259 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2260 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2261 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2262
2263 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2264 kfree(chip);
2265 pci_disable_device(pci);
2266 return err;
2267 }
2268 chip->port = pci_resource_start(pci, 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +02002269 if (request_irq(pci->irq,
2270 chip_type == TYPE_VIA8233 ?
2271 snd_via8233_interrupt : snd_via686_interrupt,
2272 SA_INTERRUPT|SA_SHIRQ,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002273 card->driver, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002274 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 snd_via82xx_free(chip);
2276 return -EBUSY;
2277 }
2278 chip->irq = pci->irq;
2279 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2280 chip->ac97_clock = ac97_clock;
2281 synchronize_irq(chip->irq);
2282
2283 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2284 snd_via82xx_free(chip);
2285 return err;
2286 }
2287
2288 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2289 snd_via82xx_free(chip);
2290 return err;
2291 }
2292
2293 /* The 8233 ac97 controller does not implement the master bit
2294 * in the pci command register. IMHO this is a violation of the PCI spec.
2295 * We call pci_set_master here because it does not hurt. */
2296 pci_set_master(pci);
2297
2298 snd_card_set_dev(card, &pci->dev);
2299
2300 *r_via = chip;
2301 return 0;
2302}
2303
2304struct via823x_info {
2305 int revision;
2306 char *name;
2307 int type;
2308};
2309static struct via823x_info via823x_cards[] __devinitdata = {
2310 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2311 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2312 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2313 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2314 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2315 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
2316};
2317
2318/*
2319 * auto detection of DXS channel supports.
2320 */
2321struct dxs_whitelist {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002322 unsigned short subvendor;
2323 unsigned short subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 unsigned short mask;
2325 short action; /* new dxs_support value */
2326};
2327
2328static int __devinit check_dxs_list(struct pci_dev *pci)
2329{
2330 static struct dxs_whitelist whitelist[] = {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002331 { .subvendor = 0x1005, .subdevice = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
2332 { .subvendor = 0x1019, .subdevice = 0x0996, .action = VIA_DXS_48K },
2333 { .subvendor = 0x1019, .subdevice = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
2334 { .subvendor = 0x1019, .subdevice = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
Takashi Iwaidb990552005-09-16 19:07:52 +02002335 { .subvendor = 0x1019, .subdevice = 0xa101, .action = VIA_DXS_SRC },
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002336 { .subvendor = 0x1025, .subdevice = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
Takashi Iwaif347c772005-08-12 16:47:49 +02002337 { .subvendor = 0x1025, .subdevice = 0x0046, .action = VIA_DXS_SRC }, /* Acer Aspire 1524 WLMi */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002338 { .subvendor = 0x1043, .subdevice = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
2339 { .subvendor = 0x1043, .subdevice = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
2340 { .subvendor = 0x1043, .subdevice = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
Takashi Iwaic66186e2005-09-29 13:49:44 +02002341 { .subvendor = 0x1043, .subdevice = 0x810d, .action = VIA_DXS_SRC }, /* ASUS */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002342 { .subvendor = 0x1043, .subdevice = 0x812a, .action = VIA_DXS_SRC }, /* ASUS A8V Deluxe */
Takashi Iwai6c1080c2006-01-04 12:15:07 +01002343 { .subvendor = 0x1043, .subdevice = 0x8174, .action = VIA_DXS_SRC }, /* ASUS */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002344 { .subvendor = 0x1071, .subdevice = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
Jaroslav Kysela96d07812005-06-07 08:56:24 +02002345 { .subvendor = 0x1071, .subdevice = 0x8399, .action = VIA_DXS_NO_VRA }, /* Umax AB 595T (VIA K8N800A - VT8237) */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002346 { .subvendor = 0x10cf, .subdevice = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
2347 { .subvendor = 0x1106, .subdevice = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
2348 { .subvendor = 0x1106, .subdevice = 0x4552, .action = VIA_DXS_NO_VRA }, /* QDI Kudoz 7X/600-6AL */
2349 { .subvendor = 0x1106, .subdevice = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
2350 { .subvendor = 0x1106, .subdevice = 0xc001, .action = VIA_DXS_SRC }, /* Insight P4-ITX */
2351 { .subvendor = 0x1297, .subdevice = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
2352 { .subvendor = 0x1297, .subdevice = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
2353 { .subvendor = 0x1458, .subdevice = 0xa002, .action = VIA_DXS_ENABLE }, /* Gigabyte GA-7VAXP */
2354 { .subvendor = 0x1462, .subdevice = 0x0080, .action = VIA_DXS_SRC }, /* MSI K8T Neo-FIS2R */
Takashi Iwai352dbfd2005-08-19 17:49:10 +02002355 { .subvendor = 0x1462, .subdevice = 0x0430, .action = VIA_DXS_SRC }, /* MSI 7142 (K8MM-V) */
Takashi Iwaic1b8f5f2005-11-08 17:44:37 +01002356 { .subvendor = 0x1462, .subdevice = 0x0470, .action = VIA_DXS_SRC }, /* MSI KT880 Delta-FSR */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002357 { .subvendor = 0x1462, .subdevice = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
2358 { .subvendor = 0x1462, .subdevice = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
2359 { .subvendor = 0x1462, .subdevice = 0x7023, .action = VIA_DXS_NO_VRA }, /* MSI K8T Neo2-FI */
2360 { .subvendor = 0x1462, .subdevice = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
Takashi Iwaif347c772005-08-12 16:47:49 +02002361 { .subvendor = 0x1462, .subdevice = 0x7142, .action = VIA_DXS_ENABLE }, /* MSI K8MM-V */
Takashi Iwai802c00f2006-01-23 15:31:11 +01002362 { .subvendor = 0x1462, .subdevice = 0xb012, .action = VIA_DXS_SRC }, /* P4M800/VIA8237R */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002363 { .subvendor = 0x147b, .subdevice = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
2364 { .subvendor = 0x147b, .subdevice = 0x1411, .action = VIA_DXS_ENABLE }, /* ABIT VA-20 */
2365 { .subvendor = 0x147b, .subdevice = 0x1413, .action = VIA_DXS_ENABLE }, /* ABIT KV8 Pro */
2366 { .subvendor = 0x147b, .subdevice = 0x1415, .action = VIA_DXS_NO_VRA }, /* Abit AV8 */
2367 { .subvendor = 0x14ff, .subdevice = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
Takashi Iwai69c3e5f2005-07-27 17:30:14 +02002368 { .subvendor = 0x14ff, .subdevice = 0x0408, .action = VIA_DXS_SRC }, /* Twinhead laptop */
Takashi Iwai94651a52005-10-10 12:08:01 +02002369 { .subvendor = 0x1558, .subdevice = 0x4701, .action = VIA_DXS_SRC }, /* Clevo D470 */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002370 { .subvendor = 0x1584, .subdevice = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
2371 { .subvendor = 0x1584, .subdevice = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
2372 { .subvendor = 0x161f, .subdevice = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
2373 { .subvendor = 0x161f, .subdevice = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
2374 { .subvendor = 0x1631, .subdevice = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
2375 { .subvendor = 0x1695, .subdevice = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
Takashi Iwaicacd3342006-01-16 13:00:50 +01002376 { .subvendor = 0x1695, .subdevice = 0x300e, .action = VIA_DXS_SRC }, /* EPoX 9HEAI */
Takashi Iwaid0bd41e2006-01-09 12:36:53 +01002377 { .subvendor = 0x16f3, .subdevice = 0x6405, .action = VIA_DXS_SRC }, /* Jetway K8M8MS */
Takashi Iwaibd84b0c2006-02-09 12:04:36 +01002378 { .subvendor = 0x1734, .subdevice = 0x1078, .action = VIA_DXS_SRC }, /* FSC Amilo L7300 */
Takashi Iwai51c2bbf2006-01-23 15:20:03 +01002379 { .subvendor = 0x1734, .subdevice = 0x1093, .action = VIA_DXS_SRC }, /* FSC */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002380 { .subvendor = 0x1849, .subdevice = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
Takashi Iwai42611c02006-03-01 19:52:23 +01002381 { .subvendor = 0x1849, .subdevice = 0x9739, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
Takashi Iwai46a17362005-12-05 21:16:37 +01002382 { .subvendor = 0x1849, .subdevice = 0x9761, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
Takashi Iwaib3e28ce2005-06-17 11:54:50 +02002383 { .subvendor = 0x1919, .subdevice = 0x200a, .action = VIA_DXS_NO_VRA }, /* Soltek SL-K8Tpro-939 */
Jaroslav Kysela22019872005-07-05 10:27:09 +02002384 { .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 -07002385 { } /* terminator */
2386 };
2387 struct dxs_whitelist *w;
2388 unsigned short subsystem_vendor;
2389 unsigned short subsystem_device;
2390
2391 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
2392 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
2393
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002394 for (w = whitelist; w->subvendor; w++) {
2395 if (w->subvendor != subsystem_vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 continue;
2397 if (w->mask) {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002398 if ((w->mask & subsystem_device) == w->subdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return w->action;
2400 } else {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02002401 if (subsystem_device == w->subdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return w->action;
2403 }
2404 }
2405
2406 /*
2407 * not detected, try 48k rate only to be sure.
2408 */
2409 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002410 printk(KERN_INFO " Please try dxs_support=5 option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 printk(KERN_INFO " and report if it works on your machine.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002412 printk(KERN_INFO " For more details, read ALSA-Configuration.txt.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return VIA_DXS_48K;
2414};
2415
2416static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2417 const struct pci_device_id *pci_id)
2418{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002419 struct snd_card *card;
2420 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 unsigned char revision;
2422 int chip_type = 0, card_type;
2423 unsigned int i;
2424 int err;
2425
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002426 card = snd_card_new(index, id, THIS_MODULE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (card == NULL)
2428 return -ENOMEM;
2429
2430 card_type = pci_id->driver_data;
2431 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
2432 switch (card_type) {
2433 case TYPE_CARD_VIA686:
2434 strcpy(card->driver, "VIA686A");
2435 sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
2436 chip_type = TYPE_VIA686;
2437 break;
2438 case TYPE_CARD_VIA8233:
2439 chip_type = TYPE_VIA8233;
2440 sprintf(card->shortname, "VIA 823x rev%x", revision);
2441 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2442 if (revision == via823x_cards[i].revision) {
2443 chip_type = via823x_cards[i].type;
2444 strcpy(card->shortname, via823x_cards[i].name);
2445 break;
2446 }
2447 }
2448 if (chip_type != TYPE_VIA8233A) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002449 if (dxs_support == VIA_DXS_AUTO)
2450 dxs_support = check_dxs_list(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* force to use VIA8233 or 8233A model according to
2452 * dxs_support module option
2453 */
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002454 if (dxs_support == VIA_DXS_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 chip_type = TYPE_VIA8233A;
2456 else
2457 chip_type = TYPE_VIA8233;
2458 }
2459 if (chip_type == TYPE_VIA8233A)
2460 strcpy(card->driver, "VIA8233A");
2461 else if (revision >= VIA_REV_8237)
2462 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2463 else
2464 strcpy(card->driver, "VIA8233");
2465 break;
2466 default:
2467 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2468 err = -EINVAL;
2469 goto __error;
2470 }
2471
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002472 if ((err = snd_via82xx_create(card, pci, chip_type, revision,
2473 ac97_clock, &chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 goto __error;
Takashi Iwai57feb832005-11-17 16:10:35 +01002475 card->private_data = chip;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002476 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 goto __error;
2478
2479 if (chip_type == TYPE_VIA686) {
2480 if ((err = snd_via686_pcm_new(chip)) < 0 ||
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002481 (err = snd_via686_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 goto __error;
2483 } else {
2484 if (chip_type == TYPE_VIA8233A) {
2485 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2486 goto __error;
2487 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2488 } else {
2489 if ((err = snd_via8233_pcm_new(chip)) < 0)
2490 goto __error;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002491 if (dxs_support == VIA_DXS_48K)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 chip->dxs_fixed = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002493 else if (dxs_support == VIA_DXS_NO_VRA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 chip->no_vra = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002495 else if (dxs_support == VIA_DXS_SRC) {
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02002496 chip->no_vra = 1;
2497 chip->dxs_src = 1;
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002500 if ((err = snd_via8233_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 goto __error;
2502 }
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 /* disable interrupts */
2505 for (i = 0; i < chip->num_devs; i++)
2506 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2507
2508 snprintf(card->longname, sizeof(card->longname),
2509 "%s with %s at %#lx, irq %d", card->shortname,
2510 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2511
2512 snd_via82xx_proc_init(chip);
2513
2514 if ((err = snd_card_register(card)) < 0) {
2515 snd_card_free(card);
2516 return err;
2517 }
2518 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return 0;
2520
2521 __error:
2522 snd_card_free(card);
2523 return err;
2524}
2525
2526static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2527{
2528 snd_card_free(pci_get_drvdata(pci));
2529 pci_set_drvdata(pci, NULL);
2530}
2531
2532static struct pci_driver driver = {
2533 .name = "VIA 82xx Audio",
2534 .id_table = snd_via82xx_ids,
2535 .probe = snd_via82xx_probe,
2536 .remove = __devexit_p(snd_via82xx_remove),
Takashi Iwai57feb832005-11-17 16:10:35 +01002537#ifdef CONFIG_PM
2538 .suspend = snd_via82xx_suspend,
2539 .resume = snd_via82xx_resume,
2540#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541};
2542
2543static int __init alsa_card_via82xx_init(void)
2544{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002545 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
2548static void __exit alsa_card_via82xx_exit(void)
2549{
2550 pci_unregister_driver(&driver);
2551}
2552
2553module_init(alsa_card_via82xx_init)
2554module_exit(alsa_card_via82xx_exit)