]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/pci/via82xx.c
ALSA: hda - check page continuity
[mirror_ubuntu-bionic-kernel.git] / sound / pci / via82xx.c
CommitLineData
1da177e4
LT
1/*
2 * ALSA driver for VIA VT82xx (South Bridge)
3 *
4 * VT82C686A/B/C, VT8233A/C, VT8235
5 *
c1017a4c 6 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
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.
4f550df5
KW
44 *
45 * Sep. 26, 2005 Karsten Wiese <annabellesgarden@yahoo.de>
46 * - Optimize position calculation for the 823x chips.
1da177e4
LT
47 */
48
1da177e4
LT
49#include <asm/io.h>
50#include <linux/delay.h>
51#include <linux/interrupt.h>
52#include <linux/init.h>
53#include <linux/pci.h>
54#include <linux/slab.h>
55#include <linux/gameport.h>
56#include <linux/moduleparam.h>
57#include <sound/core.h>
58#include <sound/pcm.h>
59#include <sound/pcm_params.h>
60#include <sound/info.h>
7058c042 61#include <sound/tlv.h>
1da177e4
LT
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
c1017a4c 70MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
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
b7fe4622
CL
79static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
80static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
81static long mpu_port;
1da177e4 82#ifdef SUPPORT_JOYSTICK
b7fe4622 83static int joystick;
1da177e4 84#endif
b7fe4622
CL
85static int ac97_clock = 48000;
86static char *ac97_quirk;
87static int dxs_support;
1da177e4 88
b7fe4622 89module_param(index, int, 0444);
1da177e4 90MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
b7fe4622 91module_param(id, charp, 0444);
1da177e4 92MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
b7fe4622 93module_param(mpu_port, long, 0444);
1da177e4
LT
94MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
95#ifdef SUPPORT_JOYSTICK
b7fe4622 96module_param(joystick, bool, 0444);
1da177e4
LT
97MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
98#endif
b7fe4622 99module_param(ac97_clock, int, 0444);
1da177e4 100MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
b7fe4622 101module_param(ac97_quirk, charp, 0444);
1da177e4 102MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
b7fe4622 103module_param(dxs_support, int, 0444);
2d7eb7cb 104MODULE_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)");
1da177e4 105
2b3e584b
TI
106/* just for backward compatibility */
107static int enable;
698444f3 108module_param(enable, bool, 0444);
2b3e584b 109
1da177e4 110
1da177e4
LT
111/* 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
8263c65f 126#define VIA_REV_8251 0x70
1da177e4
LT
127
128/*
129 * Direct registers
130 */
131
132#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
133#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
134
135/* common offsets */
136#define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
137#define VIA_REG_STAT_ACTIVE 0x80 /* RO */
4f550df5 138#define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
1da177e4
LT
139#define VIA_REG_STAT_PAUSED 0x40 /* RO */
140#define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
141#define VIA_REG_STAT_STOPPED 0x04 /* RWC */
142#define VIA_REG_STAT_EOL 0x02 /* RWC */
143#define VIA_REG_STAT_FLAG 0x01 /* RWC */
144#define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
145#define VIA_REG_CTRL_START 0x80 /* WO */
146#define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
147#define VIA_REG_CTRL_AUTOSTART 0x20
148#define VIA_REG_CTRL_PAUSE 0x08 /* RW */
149#define VIA_REG_CTRL_INT_STOP 0x04
150#define VIA_REG_CTRL_INT_EOL 0x02
151#define VIA_REG_CTRL_INT_FLAG 0x01
152#define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
153#define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
154#define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
155#define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
156#define VIA_REG_TYPE_16BIT 0x20 /* RW */
157#define VIA_REG_TYPE_STEREO 0x10 /* RW */
158#define VIA_REG_TYPE_INT_LLINE 0x00
159#define VIA_REG_TYPE_INT_LSAMPLE 0x04
160#define VIA_REG_TYPE_INT_LESSONE 0x08
161#define VIA_REG_TYPE_INT_MASK 0x0c
162#define VIA_REG_TYPE_INT_EOL 0x02
163#define VIA_REG_TYPE_INT_FLAG 0x01
164#define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
165#define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
166#define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
167#define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
168#define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
169#define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
170#define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
171
172#define DEFINE_VIA_REGSET(name,val) \
173enum {\
174 VIA_REG_##name##_STATUS = (val),\
175 VIA_REG_##name##_CONTROL = (val) + 0x01,\
176 VIA_REG_##name##_TYPE = (val) + 0x02,\
177 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
178 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
179 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
180 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
181}
182
183/* playback block */
184DEFINE_VIA_REGSET(PLAYBACK, 0x00);
185DEFINE_VIA_REGSET(CAPTURE, 0x10);
186DEFINE_VIA_REGSET(FM, 0x20);
187
188/* AC'97 */
189#define VIA_REG_AC97 0x80 /* dword */
190#define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
191#define VIA_REG_AC97_CODEC_ID_SHIFT 30
192#define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
193#define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
194#define VIA_REG_AC97_SECONDARY_VALID (1<<27)
195#define VIA_REG_AC97_PRIMARY_VALID (1<<25)
196#define VIA_REG_AC97_BUSY (1<<24)
197#define VIA_REG_AC97_READ (1<<23)
198#define VIA_REG_AC97_CMD_SHIFT 16
199#define VIA_REG_AC97_CMD_MASK 0x7e
200#define VIA_REG_AC97_DATA_SHIFT 0
201#define VIA_REG_AC97_DATA_MASK 0xffff
202
203#define VIA_REG_SGD_SHADOW 0x84 /* dword */
204/* via686 */
205#define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
206#define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
207#define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
208#define VIA_REG_SGD_STAT_PB_EOL (1<<4)
209#define VIA_REG_SGD_STAT_CP_EOL (1<<5)
210#define VIA_REG_SGD_STAT_FM_EOL (1<<6)
211#define VIA_REG_SGD_STAT_PB_STOP (1<<8)
212#define VIA_REG_SGD_STAT_CP_STOP (1<<9)
213#define VIA_REG_SGD_STAT_FM_STOP (1<<10)
214#define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
215#define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
216#define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
217/* via8233 */
218#define VIA8233_REG_SGD_STAT_FLAG (1<<0)
219#define VIA8233_REG_SGD_STAT_EOL (1<<1)
220#define VIA8233_REG_SGD_STAT_STOP (1<<2)
221#define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
222#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
223#define VIA8233_REG_SGD_CHAN_SDX 0
224#define VIA8233_REG_SGD_CHAN_MULTI 4
225#define VIA8233_REG_SGD_CHAN_REC 6
226#define VIA8233_REG_SGD_CHAN_REC1 7
227
228#define VIA_REG_GPI_STATUS 0x88
229#define VIA_REG_GPI_INTR 0x8c
230
231/* multi-channel and capture registers for via8233 */
232DEFINE_VIA_REGSET(MULTPLAY, 0x40);
233DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
234
235/* via8233-specific registers */
236#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
237#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
238#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
239#define VIA_REG_MULTPLAY_FMT_8BIT 0x00
240#define VIA_REG_MULTPLAY_FMT_16BIT 0x80
241#define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
242#define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
243#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
244
245#define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
246
247#define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
248#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
249#define VIA_REG_CAPTURE_CHANNEL_LINE 0
250#define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
251
252#define VIA_TBL_BIT_FLAG 0x40000000
253#define VIA_TBL_BIT_EOL 0x80000000
254
255/* pci space */
256#define VIA_ACLINK_STAT 0x40
257#define VIA_ACLINK_C11_READY 0x20
258#define VIA_ACLINK_C10_READY 0x10
259#define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
260#define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
261#define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
262#define VIA_ACLINK_CTRL 0x41
263#define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
264#define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
265#define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
266#define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
267#define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
268#define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
269#define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
270#define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
271#define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
272 VIA_ACLINK_CTRL_RESET|\
273 VIA_ACLINK_CTRL_PCM|\
274 VIA_ACLINK_CTRL_VRA)
275#define VIA_FUNC_ENABLE 0x42
276#define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
277#define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
278#define VIA_FUNC_RX2C_WRITE 0x20
279#define VIA_FUNC_SB_FIFO_EMPTY 0x10
280#define VIA_FUNC_ENABLE_GAME 0x08
281#define VIA_FUNC_ENABLE_FM 0x04
282#define VIA_FUNC_ENABLE_MIDI 0x02
283#define VIA_FUNC_ENABLE_SB 0x01
284#define VIA_PNP_CONTROL 0x43
285#define VIA_FM_NMI_CTRL 0x48
286#define VIA8233_VOLCHG_CTRL 0x48
287#define VIA8233_SPDIF_CTRL 0x49
288#define VIA8233_SPDIF_DX3 0x08
289#define VIA8233_SPDIF_SLOT_MASK 0x03
290#define VIA8233_SPDIF_SLOT_1011 0x00
291#define VIA8233_SPDIF_SLOT_34 0x01
292#define VIA8233_SPDIF_SLOT_78 0x02
293#define VIA8233_SPDIF_SLOT_69 0x03
294
295/*
296 */
297
298#define VIA_DXS_AUTO 0
299#define VIA_DXS_ENABLE 1
300#define VIA_DXS_DISABLE 2
301#define VIA_DXS_48K 3
302#define VIA_DXS_NO_VRA 4
2d7eb7cb 303#define VIA_DXS_SRC 5
1da177e4
LT
304
305
1da177e4
LT
306/*
307 * pcm stream
308 */
309
310struct snd_via_sg_table {
311 unsigned int offset;
312 unsigned int size;
313} ;
314
315#define VIA_TABLE_SIZE 255
316
e437e3d7 317struct viadev {
1da177e4
LT
318 unsigned int reg_offset;
319 unsigned long port;
320 int direction; /* playback = 0, capture = 1 */
e437e3d7 321 struct snd_pcm_substream *substream;
1da177e4
LT
322 int running;
323 unsigned int tbl_entries; /* # descriptors */
324 struct snd_dma_buffer table;
325 struct snd_via_sg_table *idx_table;
326 /* for recovery from the unexpected pointer */
327 unsigned int lastpos;
328 unsigned int fragsize;
329 unsigned int bufsize;
330 unsigned int bufsize2;
4f550df5
KW
331 int hwptr_done; /* processed frame position in the buffer */
332 int in_interrupt;
333 int shadow_shift;
1da177e4
LT
334};
335
336
337enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
338enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
339
340#define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
341
342struct via_rate_lock {
343 spinlock_t lock;
344 int rate;
345 int used;
346};
347
e437e3d7 348struct via82xx {
1da177e4
LT
349 int irq;
350
351 unsigned long port;
352 struct resource *mpu_res;
353 int chip_type;
354 unsigned char revision;
355
356 unsigned char old_legacy;
357 unsigned char old_legacy_cfg;
358#ifdef CONFIG_PM
359 unsigned char legacy_saved;
360 unsigned char legacy_cfg_saved;
361 unsigned char spdif_ctrl_saved;
362 unsigned char capture_src_saved[2];
363 unsigned int mpu_port_saved;
364#endif
365
00f226d4
HM
366 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
367 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
1da177e4
LT
368
369 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
370
371 struct pci_dev *pci;
e437e3d7 372 struct snd_card *card;
1da177e4
LT
373
374 unsigned int num_devs;
375 unsigned int playback_devno, multi_devno, capture_devno;
e437e3d7 376 struct viadev devs[VIA_MAX_DEVS];
1da177e4
LT
377 struct via_rate_lock rates[2]; /* playback and capture */
378 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
379 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
2d7eb7cb 380 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
1da177e4
LT
381 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
382
e437e3d7
TI
383 struct snd_pcm *pcms[2];
384 struct snd_rawmidi *rmidi;
1da177e4 385
e437e3d7
TI
386 struct snd_ac97_bus *ac97_bus;
387 struct snd_ac97 *ac97;
1da177e4
LT
388 unsigned int ac97_clock;
389 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
390
391 spinlock_t reg_lock;
e437e3d7 392 struct snd_info_entry *proc_entry;
1da177e4
LT
393
394#ifdef SUPPORT_JOYSTICK
395 struct gameport *gameport;
396#endif
397};
398
f40b6890 399static struct pci_device_id snd_via82xx_ids[] = {
4f550df5
KW
400 /* 0x1106, 0x3058 */
401 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
402 /* 0x1106, 0x3059 */
403 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
1da177e4
LT
404 { 0, }
405};
406
407MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
408
409/*
410 */
411
412/*
413 * allocate and initialize the descriptor buffers
414 * periods = number of periods
415 * fragsize = period size in bytes
416 */
e437e3d7 417static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
1da177e4
LT
418 struct pci_dev *pci,
419 unsigned int periods, unsigned int fragsize)
420{
421 unsigned int i, idx, ofs, rest;
e437e3d7 422 struct via82xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
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;
77a23f26 451 unsigned int addr;
1da177e4
LT
452
453 if (idx >= VIA_TABLE_SIZE) {
454 snd_printk(KERN_ERR "via82xx: too much table size!\n");
455 return -EINVAL;
456 }
77a23f26
TI
457 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
458 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
1da177e4
LT
459 r = PAGE_SIZE - (ofs % PAGE_SIZE);
460 if (rest < r)
461 r = rest;
462 rest -= r;
463 if (! rest) {
464 if (i == periods - 1)
465 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
466 else
467 flag = VIA_TBL_BIT_FLAG; /* period boundary */
468 } else
469 flag = 0; /* period continues to the next */
470 // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
471 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
472 dev->idx_table[idx].offset = ofs;
473 dev->idx_table[idx].size = r;
474 ofs += r;
475 idx++;
476 } while (rest > 0);
477 }
478 dev->tbl_entries = idx;
479 dev->bufsize = periods * fragsize;
480 dev->bufsize2 = dev->bufsize / 2;
481 dev->fragsize = fragsize;
482 return 0;
483}
484
485
e437e3d7 486static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
1da177e4
LT
487 struct pci_dev *pci)
488{
489 if (dev->table.area) {
490 snd_dma_free_pages(&dev->table);
491 dev->table.area = NULL;
492 }
4d572776
JJ
493 kfree(dev->idx_table);
494 dev->idx_table = NULL;
1da177e4
LT
495 return 0;
496}
497
498/*
499 * Basic I/O
500 */
501
e437e3d7 502static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
1da177e4
LT
503{
504 return inl(VIAREG(chip, AC97));
505}
506
e437e3d7 507static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
1da177e4
LT
508{
509 outl(val, VIAREG(chip, AC97));
510}
511
e437e3d7 512static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
1da177e4
LT
513{
514 unsigned int timeout = 1000; /* 1ms */
515 unsigned int val;
516
517 while (timeout-- > 0) {
518 udelay(1);
519 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
520 return val & 0xffff;
521 }
e437e3d7
TI
522 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
523 secondary, snd_via82xx_codec_xread(chip));
1da177e4
LT
524 return -EIO;
525}
526
e437e3d7 527static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
1da177e4
LT
528{
529 unsigned int timeout = 1000; /* 1ms */
530 unsigned int val, val1;
531 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
532 VIA_REG_AC97_SECONDARY_VALID;
533
534 while (timeout-- > 0) {
535 val = snd_via82xx_codec_xread(chip);
536 val1 = val & (VIA_REG_AC97_BUSY | stat);
537 if (val1 == stat)
538 return val & 0xffff;
539 udelay(1);
540 }
541 return -EIO;
542}
543
e437e3d7 544static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
1da177e4 545{
e437e3d7 546 struct via82xx *chip = ac97->private_data;
1da177e4
LT
547 int err;
548 err = snd_via82xx_codec_ready(chip, ac97->num);
549 /* here we need to wait fairly for long time.. */
ef21ca24 550 msleep(500);
1da177e4
LT
551}
552
e437e3d7 553static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
1da177e4
LT
554 unsigned short reg,
555 unsigned short val)
556{
e437e3d7 557 struct via82xx *chip = ac97->private_data;
1da177e4 558 unsigned int xval;
4f550df5 559
1da177e4
LT
560 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
561 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
562 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
563 xval |= val << VIA_REG_AC97_DATA_SHIFT;
564 snd_via82xx_codec_xwrite(chip, xval);
565 snd_via82xx_codec_ready(chip, ac97->num);
566}
567
e437e3d7 568static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
1da177e4 569{
e437e3d7 570 struct via82xx *chip = ac97->private_data;
1da177e4
LT
571 unsigned int xval, val = 0xffff;
572 int again = 0;
573
574 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
575 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
576 xval |= VIA_REG_AC97_READ;
577 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
578 while (1) {
579 if (again++ > 3) {
e437e3d7
TI
580 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
581 ac97->num, snd_via82xx_codec_xread(chip));
1da177e4
LT
582 return 0xffff;
583 }
584 snd_via82xx_codec_xwrite(chip, xval);
585 udelay (20);
586 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
587 udelay(25);
588 val = snd_via82xx_codec_xread(chip);
589 break;
590 }
591 }
592 return val & 0xffff;
593}
594
e437e3d7 595static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
1da177e4
LT
596{
597 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
598 VIADEV_REG(viadev, OFFSET_CONTROL));
599 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
600 udelay(50);
601 /* disable interrupts */
602 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
603 /* clear interrupts */
604 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
605 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
606 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
607 viadev->lastpos = 0;
4f550df5 608 viadev->hwptr_done = 0;
1da177e4
LT
609}
610
611
612/*
613 * Interrupt handler
4f550df5 614 * Used for 686 and 8233A
1da177e4 615 */
7d12e780 616static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
1da177e4 617{
e437e3d7 618 struct via82xx *chip = dev_id;
1da177e4
LT
619 unsigned int status;
620 unsigned int i;
621
622 status = inl(VIAREG(chip, SGD_SHADOW));
623 if (! (status & chip->intr_mask)) {
624 if (chip->rmidi)
625 /* check mpu401 interrupt */
7d12e780 626 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
1da177e4
LT
627 return IRQ_NONE;
628 }
629
630 /* check status for each stream */
631 spin_lock(&chip->reg_lock);
632 for (i = 0; i < chip->num_devs; i++) {
e437e3d7 633 struct viadev *viadev = &chip->devs[i];
1da177e4 634 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
4f550df5 635 if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
1da177e4
LT
636 continue;
637 if (viadev->substream && viadev->running) {
4f550df5
KW
638 /*
639 * Update hwptr_done based on 'period elapsed'
640 * interrupts. We'll use it, when the chip returns 0
641 * for OFFSET_CURR_COUNT.
642 */
643 if (c_status & VIA_REG_STAT_EOL)
644 viadev->hwptr_done = 0;
645 else
646 viadev->hwptr_done += viadev->fragsize;
647 viadev->in_interrupt = c_status;
1da177e4
LT
648 spin_unlock(&chip->reg_lock);
649 snd_pcm_period_elapsed(viadev->substream);
650 spin_lock(&chip->reg_lock);
4f550df5 651 viadev->in_interrupt = 0;
1da177e4
LT
652 }
653 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
654 }
655 spin_unlock(&chip->reg_lock);
656 return IRQ_HANDLED;
657}
658
4f550df5
KW
659/*
660 * Interrupt handler
661 */
7d12e780 662static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
4f550df5 663{
e437e3d7 664 struct via82xx *chip = dev_id;
4f550df5
KW
665 unsigned int status;
666 unsigned int i;
667 int irqreturn = 0;
668
669 /* check status for each stream */
670 spin_lock(&chip->reg_lock);
671 status = inl(VIAREG(chip, SGD_SHADOW));
672
673 for (i = 0; i < chip->num_devs; i++) {
e437e3d7
TI
674 struct viadev *viadev = &chip->devs[i];
675 struct snd_pcm_substream *substream;
4f550df5
KW
676 unsigned char c_status, shadow_status;
677
678 shadow_status = (status >> viadev->shadow_shift) &
679 (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
680 VIA_REG_STAT_FLAG);
681 c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
682 if (!c_status)
683 continue;
684
685 substream = viadev->substream;
686 if (substream && viadev->running) {
687 /*
688 * Update hwptr_done based on 'period elapsed'
689 * interrupts. We'll use it, when the chip returns 0
690 * for OFFSET_CURR_COUNT.
691 */
692 if (c_status & VIA_REG_STAT_EOL)
693 viadev->hwptr_done = 0;
694 else
695 viadev->hwptr_done += viadev->fragsize;
696 viadev->in_interrupt = c_status;
697 if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
698 viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
699 spin_unlock(&chip->reg_lock);
700
701 snd_pcm_period_elapsed(substream);
702
703 spin_lock(&chip->reg_lock);
704 viadev->in_interrupt = 0;
705 }
706 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
707 irqreturn = 1;
708 }
709 spin_unlock(&chip->reg_lock);
710 return IRQ_RETVAL(irqreturn);
711}
712
1da177e4
LT
713/*
714 * PCM callbacks
715 */
716
717/*
718 * trigger callback
719 */
e437e3d7 720static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1da177e4 721{
e437e3d7
TI
722 struct via82xx *chip = snd_pcm_substream_chip(substream);
723 struct viadev *viadev = substream->runtime->private_data;
1da177e4
LT
724 unsigned char val;
725
726 if (chip->chip_type != TYPE_VIA686)
727 val = VIA_REG_CTRL_INT;
728 else
729 val = 0;
730 switch (cmd) {
731 case SNDRV_PCM_TRIGGER_START:
41e4845c 732 case SNDRV_PCM_TRIGGER_RESUME:
1da177e4
LT
733 val |= VIA_REG_CTRL_START;
734 viadev->running = 1;
735 break;
736 case SNDRV_PCM_TRIGGER_STOP:
41e4845c 737 case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4
LT
738 val = VIA_REG_CTRL_TERMINATE;
739 viadev->running = 0;
740 break;
741 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
742 val |= VIA_REG_CTRL_PAUSE;
743 viadev->running = 0;
744 break;
745 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
746 viadev->running = 1;
747 break;
748 default:
749 return -EINVAL;
750 }
751 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
752 if (cmd == SNDRV_PCM_TRIGGER_STOP)
753 snd_via82xx_channel_reset(chip, viadev);
754 return 0;
755}
756
757
758/*
759 * pointer callbacks
760 */
761
762/*
763 * calculate the linear position at the given sg-buffer index and the rest count
764 */
765
766#define check_invalid_pos(viadev,pos) \
e437e3d7
TI
767 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
768 viadev->lastpos < viadev->bufsize2))
1da177e4 769
e437e3d7
TI
770static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
771 unsigned int count)
1da177e4
LT
772{
773 unsigned int size, base, res;
774
775 size = viadev->idx_table[idx].size;
776 base = viadev->idx_table[idx].offset;
777 res = base + size - count;
4f550df5
KW
778 if (res >= viadev->bufsize)
779 res -= viadev->bufsize;
1da177e4
LT
780
781 /* check the validity of the calculated position */
782 if (size < count) {
e437e3d7
TI
783 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
784 (int)size, (int)count);
1da177e4
LT
785 res = viadev->lastpos;
786 } else {
787 if (! count) {
788 /* Some mobos report count = 0 on the DMA boundary,
789 * i.e. count = size indeed.
790 * Let's check whether this step is above the expected size.
791 */
792 int delta = res - viadev->lastpos;
793 if (delta < 0)
794 delta += viadev->bufsize;
795 if ((unsigned int)delta > viadev->fragsize)
796 res = base;
797 }
798 if (check_invalid_pos(viadev, res)) {
799#ifdef POINTER_DEBUG
e437e3d7
TI
800 printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
801 "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
802 "count = 0x%x\n", idx, viadev->tbl_entries,
803 viadev->lastpos, viadev->bufsize2,
804 viadev->idx_table[idx].offset,
805 viadev->idx_table[idx].size, count);
1da177e4
LT
806#endif
807 /* count register returns full size when end of buffer is reached */
808 res = base + size;
809 if (check_invalid_pos(viadev, res)) {
e437e3d7
TI
810 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
811 "using last valid pointer\n");
1da177e4
LT
812 res = viadev->lastpos;
813 }
814 }
815 }
1da177e4
LT
816 return res;
817}
818
819/*
820 * get the current pointer on via686
821 */
e437e3d7 822static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
1da177e4 823{
e437e3d7
TI
824 struct via82xx *chip = snd_pcm_substream_chip(substream);
825 struct viadev *viadev = substream->runtime->private_data;
1da177e4
LT
826 unsigned int idx, ptr, count, res;
827
da3cec35
TI
828 if (snd_BUG_ON(!viadev->tbl_entries))
829 return 0;
1da177e4
LT
830 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
831 return 0;
832
833 spin_lock(&chip->reg_lock);
834 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
835 /* The via686a does not have the current index register,
836 * so we need to calculate the index from CURR_PTR.
837 */
838 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
839 if (ptr <= (unsigned int)viadev->table.addr)
840 idx = 0;
841 else /* CURR_PTR holds the address + 8 */
842 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
843 res = calc_linear_pos(viadev, idx, count);
4f550df5 844 viadev->lastpos = res; /* remember the last position */
1da177e4
LT
845 spin_unlock(&chip->reg_lock);
846
847 return bytes_to_frames(substream->runtime, res);
848}
849
850/*
851 * get the current pointer on via823x
852 */
e437e3d7 853static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
1da177e4 854{
e437e3d7
TI
855 struct via82xx *chip = snd_pcm_substream_chip(substream);
856 struct viadev *viadev = substream->runtime->private_data;
1da177e4 857 unsigned int idx, count, res;
4f550df5 858 int status;
1da177e4 859
da3cec35
TI
860 if (snd_BUG_ON(!viadev->tbl_entries))
861 return 0;
4f550df5 862
1da177e4 863 spin_lock(&chip->reg_lock);
4f550df5
KW
864 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
865 status = viadev->in_interrupt;
866 if (!status)
867 status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
868
c6cc0e3b
BJ
869 /* An apparent bug in the 8251 is worked around by sending a
870 * REG_CTRL_START. */
871 if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
872 snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
873
4f550df5 874 if (!(status & VIA_REG_STAT_ACTIVE)) {
c6cc0e3b
BJ
875 res = 0;
876 goto unlock;
4f550df5
KW
877 }
878 if (count & 0xffffff) {
879 idx = count >> 24;
880 if (idx >= viadev->tbl_entries) {
1da177e4 881#ifdef POINTER_DEBUG
e437e3d7
TI
882 printk(KERN_DEBUG "fail: invalid idx = %i/%i\n", idx,
883 viadev->tbl_entries);
1da177e4 884#endif
4f550df5
KW
885 res = viadev->lastpos;
886 } else {
887 count &= 0xffffff;
888 res = calc_linear_pos(viadev, idx, count);
889 }
1da177e4 890 } else {
4f550df5
KW
891 res = viadev->hwptr_done;
892 if (!viadev->in_interrupt) {
893 if (status & VIA_REG_STAT_EOL) {
894 res = 0;
895 } else
896 if (status & VIA_REG_STAT_FLAG) {
897 res += viadev->fragsize;
898 }
899 }
900 }
901unlock:
902 viadev->lastpos = res;
1da177e4
LT
903 spin_unlock(&chip->reg_lock);
904
905 return bytes_to_frames(substream->runtime, res);
906}
907
908
909/*
910 * hw_params callback:
911 * allocate the buffer and build up the buffer description table
912 */
e437e3d7
TI
913static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
914 struct snd_pcm_hw_params *hw_params)
1da177e4 915{
e437e3d7
TI
916 struct via82xx *chip = snd_pcm_substream_chip(substream);
917 struct viadev *viadev = substream->runtime->private_data;
1da177e4
LT
918 int err;
919
920 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
921 if (err < 0)
922 return err;
923 err = build_via_table(viadev, substream, chip->pci,
924 params_periods(hw_params),
925 params_period_bytes(hw_params));
926 if (err < 0)
927 return err;
928
929 return 0;
930}
931
932/*
933 * hw_free callback:
934 * clean up the buffer description table and release the buffer
935 */
e437e3d7 936static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
1da177e4 937{
e437e3d7
TI
938 struct via82xx *chip = snd_pcm_substream_chip(substream);
939 struct viadev *viadev = substream->runtime->private_data;
1da177e4
LT
940
941 clean_via_table(viadev, substream, chip->pci);
942 snd_pcm_lib_free_pages(substream);
943 return 0;
944}
945
946
947/*
948 * set up the table pointer
949 */
e437e3d7 950static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
1da177e4
LT
951{
952 snd_via82xx_codec_ready(chip, 0);
953 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
954 udelay(20);
955 snd_via82xx_codec_ready(chip, 0);
956}
957
958/*
959 * prepare callback for playback and capture on via686
960 */
e437e3d7
TI
961static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
962 struct snd_pcm_runtime *runtime)
1da177e4
LT
963{
964 snd_via82xx_channel_reset(chip, viadev);
965 /* this must be set after channel_reset */
966 snd_via82xx_set_table_ptr(chip, viadev);
967 outb(VIA_REG_TYPE_AUTOSTART |
968 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
969 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
970 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
971 VIA_REG_TYPE_INT_EOL |
972 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
973}
974
e437e3d7 975static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
1da177e4 976{
e437e3d7
TI
977 struct via82xx *chip = snd_pcm_substream_chip(substream);
978 struct viadev *viadev = substream->runtime->private_data;
979 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
980
981 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
982 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
983 via686_setup_format(chip, viadev, runtime);
984 return 0;
985}
986
e437e3d7 987static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
1da177e4 988{
e437e3d7
TI
989 struct via82xx *chip = snd_pcm_substream_chip(substream);
990 struct viadev *viadev = substream->runtime->private_data;
991 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
992
993 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
994 via686_setup_format(chip, viadev, runtime);
995 return 0;
996}
997
998/*
999 * lock the current rate
1000 */
1001static int via_lock_rate(struct via_rate_lock *rec, int rate)
1002{
1003 int changed = 0;
1004
1005 spin_lock_irq(&rec->lock);
1006 if (rec->rate != rate) {
1007 if (rec->rate && rec->used > 1) /* already set */
1008 changed = -EINVAL;
1009 else {
1010 rec->rate = rate;
1011 changed = 1;
1012 }
1013 }
1014 spin_unlock_irq(&rec->lock);
1015 return changed;
1016}
1017
1018/*
1019 * prepare callback for DSX playback on via823x
1020 */
e437e3d7 1021static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
1da177e4 1022{
e437e3d7
TI
1023 struct via82xx *chip = snd_pcm_substream_chip(substream);
1024 struct viadev *viadev = substream->runtime->private_data;
1025 struct snd_pcm_runtime *runtime = substream->runtime;
2d7eb7cb 1026 int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
1da177e4
LT
1027 int rate_changed;
1028 u32 rbits;
1029
2d7eb7cb 1030 if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
1da177e4 1031 return rate_changed;
16d3f140 1032 if (rate_changed)
1da177e4
LT
1033 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1034 chip->no_vra ? 48000 : runtime->rate);
16d3f140
TI
1035 if (chip->spdif_on && viadev->reg_offset == 0x30)
1036 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1037
1da177e4
LT
1038 if (runtime->rate == 48000)
1039 rbits = 0xfffff;
1040 else
e437e3d7
TI
1041 rbits = (0x100000 / 48000) * runtime->rate +
1042 ((0x100000 % 48000) * runtime->rate) / 48000;
da3cec35 1043 snd_BUG_ON(rbits & ~0xfffff);
1da177e4
LT
1044 snd_via82xx_channel_reset(chip, viadev);
1045 snd_via82xx_set_table_ptr(chip, viadev);
e437e3d7
TI
1046 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1047 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1048 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1049 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
1da177e4
LT
1050 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1051 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1052 rbits | /* rate */
1053 0xff000000, /* STOP index is never reached */
1054 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1055 udelay(20);
1056 snd_via82xx_codec_ready(chip, 0);
1057 return 0;
1058}
1059
1060/*
1061 * prepare callback for multi-channel playback on via823x
1062 */
e437e3d7 1063static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
1da177e4 1064{
e437e3d7
TI
1065 struct via82xx *chip = snd_pcm_substream_chip(substream);
1066 struct viadev *viadev = substream->runtime->private_data;
1067 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1068 unsigned int slots;
1069 int fmt;
1070
1071 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1072 return -EINVAL;
1073 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1074 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1075 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1076 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1077 snd_via82xx_channel_reset(chip, viadev);
1078 snd_via82xx_set_table_ptr(chip, viadev);
1079
e437e3d7
TI
1080 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1081 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
1da177e4
LT
1082 fmt |= runtime->channels << 4;
1083 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1084#if 0
1085 if (chip->revision == VIA_REV_8233A)
1086 slots = 0;
1087 else
1088#endif
1089 {
1090 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1091 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1092 switch (runtime->channels) {
1093 case 1: slots = (1<<0) | (1<<4); break;
1094 case 2: slots = (1<<0) | (2<<4); break;
1095 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1096 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1097 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1098 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1099 default: slots = 0; break;
1100 }
1101 }
1102 /* STOP index is never reached */
1103 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1104 udelay(20);
1105 snd_via82xx_codec_ready(chip, 0);
1106 return 0;
1107}
1108
1109/*
1110 * prepare callback for capture on via823x
1111 */
e437e3d7 1112static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
1da177e4 1113{
e437e3d7
TI
1114 struct via82xx *chip = snd_pcm_substream_chip(substream);
1115 struct viadev *viadev = substream->runtime->private_data;
1116 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1117
1118 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1119 return -EINVAL;
1120 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1121 snd_via82xx_channel_reset(chip, viadev);
1122 snd_via82xx_set_table_ptr(chip, viadev);
1123 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1124 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1125 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1126 0xff000000, /* STOP index is never reached */
1127 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1128 udelay(20);
1129 snd_via82xx_codec_ready(chip, 0);
1130 return 0;
1131}
1132
1133
1134/*
1135 * pcm hardware definition, identical for both playback and capture
1136 */
e437e3d7 1137static struct snd_pcm_hardware snd_via82xx_hw =
1da177e4
LT
1138{
1139 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1140 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1141 SNDRV_PCM_INFO_MMAP_VALID |
41e4845c 1142 /* SNDRV_PCM_INFO_RESUME | */
1da177e4
LT
1143 SNDRV_PCM_INFO_PAUSE),
1144 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1145 .rates = SNDRV_PCM_RATE_48000,
1146 .rate_min = 48000,
1147 .rate_max = 48000,
1148 .channels_min = 1,
1149 .channels_max = 2,
1150 .buffer_bytes_max = 128 * 1024,
1151 .period_bytes_min = 32,
1152 .period_bytes_max = 128 * 1024,
1153 .periods_min = 2,
1154 .periods_max = VIA_TABLE_SIZE / 2,
1155 .fifo_size = 0,
1156};
1157
1158
1159/*
1160 * open callback skeleton
1161 */
e437e3d7
TI
1162static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1163 struct snd_pcm_substream *substream)
1da177e4 1164{
e437e3d7 1165 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1166 int err;
1167 struct via_rate_lock *ratep;
1168
1169 runtime->hw = snd_via82xx_hw;
1170
1171 /* set the hw rate condition */
1172 ratep = &chip->rates[viadev->direction];
1173 spin_lock_irq(&ratep->lock);
1174 ratep->used++;
1175 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1176 /* DXS#3 and spdif is on */
1177 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1178 snd_pcm_limit_hw_rates(runtime);
1179 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1180 /* fixed DXS playback rate */
1181 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1182 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
2d7eb7cb
SV
1183 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1184 /* use full SRC capabilities of DXS */
1185 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1186 SNDRV_PCM_RATE_8000_48000);
1187 runtime->hw.rate_min = 8000;
1188 runtime->hw.rate_max = 48000;
1da177e4
LT
1189 } else if (! ratep->rate) {
1190 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1191 runtime->hw.rates = chip->ac97->rates[idx];
1192 snd_pcm_limit_hw_rates(runtime);
1193 } else {
1194 /* a fixed rate */
1195 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1196 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1197 }
1198 spin_unlock_irq(&ratep->lock);
1199
1200 /* we may remove following constaint when we modify table entries
1201 in interrupt */
1202 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1203 return err;
1204
1205 runtime->private_data = viadev;
1206 viadev->substream = substream;
1207
1208 return 0;
1209}
1210
1211
1212/*
1213 * open callback for playback on via686 and via823x DSX
1214 */
e437e3d7 1215static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
1da177e4 1216{
e437e3d7
TI
1217 struct via82xx *chip = snd_pcm_substream_chip(substream);
1218 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
1da177e4
LT
1219 int err;
1220
1221 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1222 return err;
1223 return 0;
1224}
1225
1226/*
1227 * open callback for playback on via823x multi-channel
1228 */
e437e3d7 1229static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
1da177e4 1230{
e437e3d7
TI
1231 struct via82xx *chip = snd_pcm_substream_chip(substream);
1232 struct viadev *viadev = &chip->devs[chip->multi_devno];
1da177e4
LT
1233 int err;
1234 /* channels constraint for VIA8233A
1235 * 3 and 5 channels are not supported
1236 */
1237 static unsigned int channels[] = {
1238 1, 2, 4, 6
1239 };
e437e3d7 1240 static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
1da177e4
LT
1241 .count = ARRAY_SIZE(channels),
1242 .list = channels,
1243 .mask = 0,
1244 };
1245
1246 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1247 return err;
1248 substream->runtime->hw.channels_max = 6;
1249 if (chip->revision == VIA_REV_8233A)
e437e3d7
TI
1250 snd_pcm_hw_constraint_list(substream->runtime, 0,
1251 SNDRV_PCM_HW_PARAM_CHANNELS,
1252 &hw_constraints_channels);
1da177e4
LT
1253 return 0;
1254}
1255
1256/*
1257 * open callback for capture on via686 and via823x
1258 */
e437e3d7 1259static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
1da177e4 1260{
e437e3d7
TI
1261 struct via82xx *chip = snd_pcm_substream_chip(substream);
1262 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1da177e4
LT
1263
1264 return snd_via82xx_pcm_open(chip, viadev, substream);
1265}
1266
1267/*
1268 * close callback
1269 */
e437e3d7 1270static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
1da177e4 1271{
e437e3d7
TI
1272 struct via82xx *chip = snd_pcm_substream_chip(substream);
1273 struct viadev *viadev = substream->runtime->private_data;
1da177e4
LT
1274 struct via_rate_lock *ratep;
1275
1276 /* release the rate lock */
1277 ratep = &chip->rates[viadev->direction];
1278 spin_lock_irq(&ratep->lock);
1279 ratep->used--;
1280 if (! ratep->used)
1281 ratep->rate = 0;
1282 spin_unlock_irq(&ratep->lock);
6dbe6628
TI
1283 if (! ratep->rate) {
1284 if (! viadev->direction) {
1285 snd_ac97_update_power(chip->ac97,
1286 AC97_PCM_FRONT_DAC_RATE, 0);
1287 snd_ac97_update_power(chip->ac97,
1288 AC97_PCM_SURR_DAC_RATE, 0);
1289 snd_ac97_update_power(chip->ac97,
1290 AC97_PCM_LFE_DAC_RATE, 0);
1291 } else
1292 snd_ac97_update_power(chip->ac97,
1293 AC97_PCM_LR_ADC_RATE, 0);
1294 }
1da177e4
LT
1295 viadev->substream = NULL;
1296 return 0;
1297}
1298
1299
1300/* via686 playback callbacks */
e437e3d7 1301static struct snd_pcm_ops snd_via686_playback_ops = {
1da177e4
LT
1302 .open = snd_via82xx_playback_open,
1303 .close = snd_via82xx_pcm_close,
1304 .ioctl = snd_pcm_lib_ioctl,
1305 .hw_params = snd_via82xx_hw_params,
1306 .hw_free = snd_via82xx_hw_free,
1307 .prepare = snd_via686_playback_prepare,
1308 .trigger = snd_via82xx_pcm_trigger,
1309 .pointer = snd_via686_pcm_pointer,
1310 .page = snd_pcm_sgbuf_ops_page,
1311};
1312
1313/* via686 capture callbacks */
e437e3d7 1314static struct snd_pcm_ops snd_via686_capture_ops = {
1da177e4
LT
1315 .open = snd_via82xx_capture_open,
1316 .close = snd_via82xx_pcm_close,
1317 .ioctl = snd_pcm_lib_ioctl,
1318 .hw_params = snd_via82xx_hw_params,
1319 .hw_free = snd_via82xx_hw_free,
1320 .prepare = snd_via686_capture_prepare,
1321 .trigger = snd_via82xx_pcm_trigger,
1322 .pointer = snd_via686_pcm_pointer,
1323 .page = snd_pcm_sgbuf_ops_page,
1324};
1325
1326/* via823x DSX playback callbacks */
e437e3d7 1327static struct snd_pcm_ops snd_via8233_playback_ops = {
1da177e4
LT
1328 .open = snd_via82xx_playback_open,
1329 .close = snd_via82xx_pcm_close,
1330 .ioctl = snd_pcm_lib_ioctl,
1331 .hw_params = snd_via82xx_hw_params,
1332 .hw_free = snd_via82xx_hw_free,
1333 .prepare = snd_via8233_playback_prepare,
1334 .trigger = snd_via82xx_pcm_trigger,
1335 .pointer = snd_via8233_pcm_pointer,
1336 .page = snd_pcm_sgbuf_ops_page,
1337};
1338
1339/* via823x multi-channel playback callbacks */
e437e3d7 1340static struct snd_pcm_ops snd_via8233_multi_ops = {
1da177e4
LT
1341 .open = snd_via8233_multi_open,
1342 .close = snd_via82xx_pcm_close,
1343 .ioctl = snd_pcm_lib_ioctl,
1344 .hw_params = snd_via82xx_hw_params,
1345 .hw_free = snd_via82xx_hw_free,
1346 .prepare = snd_via8233_multi_prepare,
1347 .trigger = snd_via82xx_pcm_trigger,
1348 .pointer = snd_via8233_pcm_pointer,
1349 .page = snd_pcm_sgbuf_ops_page,
1350};
1351
1352/* via823x capture callbacks */
e437e3d7 1353static struct snd_pcm_ops snd_via8233_capture_ops = {
1da177e4
LT
1354 .open = snd_via82xx_capture_open,
1355 .close = snd_via82xx_pcm_close,
1356 .ioctl = snd_pcm_lib_ioctl,
1357 .hw_params = snd_via82xx_hw_params,
1358 .hw_free = snd_via82xx_hw_free,
1359 .prepare = snd_via8233_capture_prepare,
1360 .trigger = snd_via82xx_pcm_trigger,
1361 .pointer = snd_via8233_pcm_pointer,
1362 .page = snd_pcm_sgbuf_ops_page,
1363};
1364
1365
e437e3d7
TI
1366static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1367 int shadow_pos, int direction)
1da177e4
LT
1368{
1369 chip->devs[idx].reg_offset = reg_offset;
4f550df5 1370 chip->devs[idx].shadow_shift = shadow_pos * 4;
1da177e4
LT
1371 chip->devs[idx].direction = direction;
1372 chip->devs[idx].port = chip->port + reg_offset;
1373}
1374
1375/*
1376 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1377 */
e437e3d7 1378static int __devinit snd_via8233_pcm_new(struct via82xx *chip)
1da177e4 1379{
e437e3d7 1380 struct snd_pcm *pcm;
1da177e4
LT
1381 int i, err;
1382
1383 chip->playback_devno = 0; /* x 4 */
1384 chip->multi_devno = 4; /* x 1 */
1385 chip->capture_devno = 5; /* x 2 */
1386 chip->num_devs = 7;
1387 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1388
1389 /* PCM #0: 4 DSX playbacks and 1 capture */
1390 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1391 if (err < 0)
1392 return err;
1393 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_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[0] = pcm;
1398 /* set up playbacks */
1399 for (i = 0; i < 4; i++)
4f550df5 1400 init_viadev(chip, i, 0x10 * i, i, 0);
1da177e4 1401 /* capture */
4f550df5 1402 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1da177e4
LT
1403
1404 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
e437e3d7
TI
1405 snd_dma_pci_data(chip->pci),
1406 64*1024, 128*1024)) < 0)
1da177e4
LT
1407 return err;
1408
1409 /* PCM #1: multi-channel playback and 2nd capture */
1410 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1411 if (err < 0)
1412 return err;
1413 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1414 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1415 pcm->private_data = chip;
1416 strcpy(pcm->name, chip->card->shortname);
1417 chip->pcms[1] = pcm;
1418 /* set up playback */
4f550df5 1419 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1da177e4 1420 /* set up capture */
4f550df5 1421 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
1da177e4
LT
1422
1423 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
e437e3d7
TI
1424 snd_dma_pci_data(chip->pci),
1425 64*1024, 128*1024)) < 0)
1da177e4
LT
1426 return err;
1427
1428 return 0;
1429}
1430
1431/*
1432 * create pcm instances for VIA8233A
1433 */
e437e3d7 1434static int __devinit snd_via8233a_pcm_new(struct via82xx *chip)
1da177e4 1435{
e437e3d7 1436 struct snd_pcm *pcm;
1da177e4
LT
1437 int err;
1438
1439 chip->multi_devno = 0;
1440 chip->playback_devno = 1;
1441 chip->capture_devno = 2;
1442 chip->num_devs = 3;
1443 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1444
1445 /* PCM #0: multi-channel playback and capture */
1446 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1447 if (err < 0)
1448 return err;
1449 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1450 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1451 pcm->private_data = chip;
1452 strcpy(pcm->name, chip->card->shortname);
1453 chip->pcms[0] = pcm;
1454 /* set up playback */
4f550df5 1455 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1da177e4 1456 /* capture */
4f550df5 1457 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1da177e4
LT
1458
1459 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
e437e3d7
TI
1460 snd_dma_pci_data(chip->pci),
1461 64*1024, 128*1024)) < 0)
1da177e4
LT
1462 return err;
1463
1464 /* SPDIF supported? */
1465 if (! ac97_can_spdif(chip->ac97))
1466 return 0;
1467
1468 /* PCM #1: DXS3 playback (for spdif) */
1469 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1470 if (err < 0)
1471 return err;
1472 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1473 pcm->private_data = chip;
1474 strcpy(pcm->name, chip->card->shortname);
1475 chip->pcms[1] = pcm;
1476 /* set up playback */
4f550df5 1477 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
1da177e4
LT
1478
1479 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
e437e3d7
TI
1480 snd_dma_pci_data(chip->pci),
1481 64*1024, 128*1024)) < 0)
1da177e4
LT
1482 return err;
1483
1484 return 0;
1485}
1486
1487/*
1488 * create a pcm instance for via686a/b
1489 */
e437e3d7 1490static int __devinit snd_via686_pcm_new(struct via82xx *chip)
1da177e4 1491{
e437e3d7 1492 struct snd_pcm *pcm;
1da177e4
LT
1493 int err;
1494
1495 chip->playback_devno = 0;
1496 chip->capture_devno = 1;
1497 chip->num_devs = 2;
1498 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1499
1500 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1501 if (err < 0)
1502 return err;
1503 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1504 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1505 pcm->private_data = chip;
1506 strcpy(pcm->name, chip->card->shortname);
1507 chip->pcms[0] = pcm;
4f550df5
KW
1508 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1509 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
1da177e4
LT
1510
1511 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
e437e3d7
TI
1512 snd_dma_pci_data(chip->pci),
1513 64*1024, 128*1024)) < 0)
1da177e4
LT
1514 return err;
1515
1516 return 0;
1517}
1518
1519
1520/*
1521 * Mixer part
1522 */
1523
e437e3d7
TI
1524static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1526{
1527 /* formerly they were "Line" and "Mic", but it looks like that they
1528 * have nothing to do with the actual physical connections...
1529 */
1530 static char *texts[2] = {
1531 "Input1", "Input2"
1532 };
1533 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1534 uinfo->count = 1;
1535 uinfo->value.enumerated.items = 2;
1536 if (uinfo->value.enumerated.item >= 2)
1537 uinfo->value.enumerated.item = 1;
1538 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1539 return 0;
1540}
1541
e437e3d7
TI
1542static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1543 struct snd_ctl_elem_value *ucontrol)
1da177e4 1544{
e437e3d7 1545 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1546 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1547 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1548 return 0;
1549}
1550
e437e3d7
TI
1551static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1552 struct snd_ctl_elem_value *ucontrol)
1da177e4 1553{
e437e3d7 1554 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1555 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1556 u8 val, oval;
1557
1558 spin_lock_irq(&chip->reg_lock);
1559 oval = inb(port);
1560 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1561 if (ucontrol->value.enumerated.item[0])
1562 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1563 if (val != oval)
1564 outb(val, port);
1565 spin_unlock_irq(&chip->reg_lock);
1566 return val != oval;
1567}
1568
e437e3d7 1569static struct snd_kcontrol_new snd_via8233_capture_source __devinitdata = {
1da177e4
LT
1570 .name = "Input Source Select",
1571 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1572 .info = snd_via8233_capture_source_info,
1573 .get = snd_via8233_capture_source_get,
1574 .put = snd_via8233_capture_source_put,
1575};
1576
a5ce8890 1577#define snd_via8233_dxs3_spdif_info snd_ctl_boolean_mono_info
1da177e4 1578
e437e3d7
TI
1579static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_value *ucontrol)
1da177e4 1581{
e437e3d7 1582 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1583 u8 val;
1584
1585 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1586 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1587 return 0;
1588}
1589
e437e3d7
TI
1590static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
1da177e4 1592{
e437e3d7 1593 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1594 u8 val, oval;
1595
1596 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1597 val = oval & ~VIA8233_SPDIF_DX3;
1598 if (ucontrol->value.integer.value[0])
1599 val |= VIA8233_SPDIF_DX3;
1600 /* save the spdif flag for rate filtering */
1601 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1602 if (val != oval) {
1603 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1604 return 1;
1605 }
1606 return 0;
1607}
1608
e437e3d7 1609static struct snd_kcontrol_new snd_via8233_dxs3_spdif_control __devinitdata = {
10e8d78a 1610 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
1da177e4
LT
1611 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1612 .info = snd_via8233_dxs3_spdif_info,
1613 .get = snd_via8233_dxs3_spdif_get,
1614 .put = snd_via8233_dxs3_spdif_put,
1615};
1616
e437e3d7
TI
1617static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1618 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1619{
1620 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1621 uinfo->count = 2;
1622 uinfo->value.integer.min = 0;
1623 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1624 return 0;
1625}
1626
e437e3d7
TI
1627static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1628 struct snd_ctl_elem_value *ucontrol)
1da177e4 1629{
e437e3d7 1630 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
00f226d4
HM
1631 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1632
1633 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1634 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1635 return 0;
1636}
1637
e437e3d7
TI
1638static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol)
00f226d4 1640{
e437e3d7 1641 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
00f226d4
HM
1642 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1643 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
1da177e4
LT
1644 return 0;
1645}
1646
e437e3d7
TI
1647static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1648 struct snd_ctl_elem_value *ucontrol)
00f226d4 1649{
e437e3d7 1650 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
00f226d4
HM
1651 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1652 unsigned long port = chip->port + 0x10 * idx;
1653 unsigned char val;
1654 int i, change = 0;
1655
1656 for (i = 0; i < 2; i++) {
1657 val = ucontrol->value.integer.value[i];
1658 if (val > VIA_DXS_MAX_VOLUME)
1659 val = VIA_DXS_MAX_VOLUME;
1660 val = VIA_DXS_MAX_VOLUME - val;
1661 change |= val != chip->playback_volume[idx][i];
1662 if (change) {
1663 chip->playback_volume[idx][i] = val;
1664 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1665 }
1666 }
1667 return change;
1668}
1669
e437e3d7
TI
1670static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1671 struct snd_ctl_elem_value *ucontrol)
1da177e4 1672{
e437e3d7 1673 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1674 unsigned int idx;
1675 unsigned char val;
1676 int i, change = 0;
1677
1678 for (i = 0; i < 2; i++) {
1679 val = ucontrol->value.integer.value[i];
1680 if (val > VIA_DXS_MAX_VOLUME)
1681 val = VIA_DXS_MAX_VOLUME;
1682 val = VIA_DXS_MAX_VOLUME - val;
00f226d4 1683 if (val != chip->playback_volume_c[i]) {
1da177e4 1684 change = 1;
00f226d4 1685 chip->playback_volume_c[i] = val;
1da177e4
LT
1686 for (idx = 0; idx < 4; idx++) {
1687 unsigned long port = chip->port + 0x10 * idx;
00f226d4 1688 chip->playback_volume[idx][i] = val;
1da177e4
LT
1689 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1690 }
1691 }
1692 }
1693 return change;
1694}
1695
0cb29ea0 1696static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -9450, 150, 1);
7058c042 1697
e437e3d7 1698static struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control __devinitdata = {
1da177e4
LT
1699 .name = "PCM Playback Volume",
1700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7058c042
TI
1701 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1702 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1da177e4 1703 .info = snd_via8233_dxs_volume_info,
00f226d4
HM
1704 .get = snd_via8233_pcmdxs_volume_get,
1705 .put = snd_via8233_pcmdxs_volume_put,
7058c042 1706 .tlv = { .p = db_scale_dxs }
00f226d4
HM
1707};
1708
e437e3d7 1709static struct snd_kcontrol_new snd_via8233_dxs_volume_control __devinitdata = {
00f226d4
HM
1710 .name = "VIA DXS Playback Volume",
1711 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7058c042
TI
1712 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1713 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
00f226d4
HM
1714 .count = 4,
1715 .info = snd_via8233_dxs_volume_info,
1da177e4
LT
1716 .get = snd_via8233_dxs_volume_get,
1717 .put = snd_via8233_dxs_volume_put,
7058c042 1718 .tlv = { .p = db_scale_dxs }
1da177e4
LT
1719};
1720
1721/*
1722 */
1723
e437e3d7 1724static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1da177e4 1725{
e437e3d7 1726 struct via82xx *chip = bus->private_data;
1da177e4
LT
1727 chip->ac97_bus = NULL;
1728}
1729
e437e3d7 1730static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
1da177e4 1731{
e437e3d7 1732 struct via82xx *chip = ac97->private_data;
1da177e4
LT
1733 chip->ac97 = NULL;
1734}
1735
1736static struct ac97_quirk ac97_quirks[] = {
1737 {
69ad07cf
JK
1738 .subvendor = 0x1106,
1739 .subdevice = 0x4161,
1da177e4
LT
1740 .codec_id = 0x56494161, /* VT1612A */
1741 .name = "Soltek SL-75DRV5",
1742 .type = AC97_TUNE_NONE
1743 },
1744 { /* FIXME: which codec? */
69ad07cf
JK
1745 .subvendor = 0x1106,
1746 .subdevice = 0x4161,
1da177e4
LT
1747 .name = "ASRock K7VT2",
1748 .type = AC97_TUNE_HP_ONLY
1749 },
1750 {
69ad07cf
JK
1751 .subvendor = 0x1019,
1752 .subdevice = 0x0a81,
1da177e4
LT
1753 .name = "ECS K7VTA3",
1754 .type = AC97_TUNE_HP_ONLY
1755 },
1756 {
69ad07cf
JK
1757 .subvendor = 0x1019,
1758 .subdevice = 0x0a85,
1da177e4
LT
1759 .name = "ECS L7VMM2",
1760 .type = AC97_TUNE_HP_ONLY
1761 },
942fd1eb
WS
1762 {
1763 .subvendor = 0x1019,
1764 .subdevice = 0x1841,
1765 .name = "ECS K7VTA3",
1766 .type = AC97_TUNE_HP_ONLY
1767 },
1da177e4 1768 {
69ad07cf
JK
1769 .subvendor = 0x1849,
1770 .subdevice = 0x3059,
1da177e4
LT
1771 .name = "ASRock K7VM2",
1772 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1773 },
1774 {
69ad07cf
JK
1775 .subvendor = 0x14cd,
1776 .subdevice = 0x7002,
1da177e4
LT
1777 .name = "Unknown",
1778 .type = AC97_TUNE_ALC_JACK
1779 },
1780 {
69ad07cf
JK
1781 .subvendor = 0x1071,
1782 .subdevice = 0x8590,
1da177e4
LT
1783 .name = "Mitac Mobo",
1784 .type = AC97_TUNE_ALC_JACK
1785 },
1786 {
69ad07cf
JK
1787 .subvendor = 0x161f,
1788 .subdevice = 0x202b,
1da177e4
LT
1789 .name = "Arima Notebook",
1790 .type = AC97_TUNE_HP_ONLY,
1791 },
dac8dddd
TI
1792 {
1793 .subvendor = 0x161f,
1794 .subdevice = 0x2032,
1795 .name = "Targa Traveller 811",
1796 .type = AC97_TUNE_HP_ONLY,
1797 },
d4199f01
DC
1798 {
1799 .subvendor = 0x161f,
1800 .subdevice = 0x2032,
1801 .name = "m680x",
1802 .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1803 },
9674513d
TI
1804 {
1805 .subvendor = 0x1297,
1806 .subdevice = 0xa232,
1807 .name = "Shuttle AK32VN",
1808 .type = AC97_TUNE_HP_ONLY
1809 },
1da177e4
LT
1810 { } /* terminator */
1811};
1812
e437e3d7 1813static int __devinit snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
1da177e4 1814{
e437e3d7 1815 struct snd_ac97_template ac97;
1da177e4 1816 int err;
e437e3d7 1817 static struct snd_ac97_bus_ops ops = {
1da177e4
LT
1818 .write = snd_via82xx_codec_write,
1819 .read = snd_via82xx_codec_read,
1820 .wait = snd_via82xx_codec_wait,
1821 };
1822
1823 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1824 return err;
1825 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1826 chip->ac97_bus->clock = chip->ac97_clock;
1da177e4
LT
1827
1828 memset(&ac97, 0, sizeof(ac97));
1829 ac97.private_data = chip;
1830 ac97.private_free = snd_via82xx_mixer_free_ac97;
1831 ac97.pci = chip->pci;
f1a63a38 1832 ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
1da177e4
LT
1833 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1834 return err;
1835
1836 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1837
1838 if (chip->chip_type != TYPE_VIA686) {
1839 /* use slot 10/11 */
1840 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1841 }
1842
1843 return 0;
1844}
1845
1846#ifdef SUPPORT_JOYSTICK
1847#define JOYSTICK_ADDR 0x200
e437e3d7 1848static int __devinit snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1da177e4
LT
1849{
1850 struct gameport *gp;
1851 struct resource *r;
1852
b7fe4622 1853 if (!joystick)
1da177e4
LT
1854 return -ENODEV;
1855
1856 r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1857 if (!r) {
e437e3d7
TI
1858 printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n",
1859 JOYSTICK_ADDR);
1da177e4
LT
1860 return -EBUSY;
1861 }
1862
1863 chip->gameport = gp = gameport_allocate_port();
1864 if (!gp) {
1865 printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
b1d5776d 1866 release_and_free_resource(r);
1da177e4
LT
1867 return -ENOMEM;
1868 }
1869
1870 gameport_set_name(gp, "VIA686 Gameport");
1871 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1872 gameport_set_dev_parent(gp, &chip->pci->dev);
1873 gp->io = JOYSTICK_ADDR;
1874 gameport_set_port_data(gp, r);
1875
1876 /* Enable legacy joystick port */
1877 *legacy |= VIA_FUNC_ENABLE_GAME;
1878 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1879
1880 gameport_register_port(chip->gameport);
1881
1882 return 0;
1883}
1884
e437e3d7 1885static void snd_via686_free_gameport(struct via82xx *chip)
1da177e4
LT
1886{
1887 if (chip->gameport) {
1888 struct resource *r = gameport_get_port_data(chip->gameport);
1889
1890 gameport_unregister_port(chip->gameport);
1891 chip->gameport = NULL;
b1d5776d 1892 release_and_free_resource(r);
1da177e4
LT
1893 }
1894}
1895#else
e437e3d7 1896static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1da177e4
LT
1897{
1898 return -ENOSYS;
1899}
e437e3d7 1900static inline void snd_via686_free_gameport(struct via82xx *chip) { }
1da177e4
LT
1901#endif
1902
1903
1904/*
1905 *
1906 */
1907
e437e3d7 1908static int __devinit snd_via8233_init_misc(struct via82xx *chip)
1da177e4
LT
1909{
1910 int i, err, caps;
1911 unsigned char val;
1912
1913 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1914 for (i = 0; i < caps; i++) {
1915 snd_via8233_capture_source.index = i;
1916 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1917 if (err < 0)
1918 return err;
1919 }
1920 if (ac97_can_spdif(chip->ac97)) {
1921 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1922 if (err < 0)
1923 return err;
1924 }
1925 if (chip->chip_type != TYPE_VIA8233A) {
1926 /* when no h/w PCM volume control is found, use DXS volume control
1927 * as the PCM vol control
1928 */
e437e3d7 1929 struct snd_ctl_elem_id sid;
1da177e4
LT
1930 memset(&sid, 0, sizeof(sid));
1931 strcpy(sid.name, "PCM Playback Volume");
1932 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1933 if (! snd_ctl_find_id(chip->card, &sid)) {
00f226d4
HM
1934 snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
1935 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1936 if (err < 0)
1937 return err;
1938 }
1939 else /* Using DXS when PCM emulation is enabled is really weird */
1940 {
1941 /* Standalone DXS controls */
1da177e4
LT
1942 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1943 if (err < 0)
1944 return err;
1945 }
1946 }
1da177e4
LT
1947 /* select spdif data slot 10/11 */
1948 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1949 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1950 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
1951 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1952
1953 return 0;
1954}
1955
e437e3d7 1956static int __devinit snd_via686_init_misc(struct via82xx *chip)
1da177e4
LT
1957{
1958 unsigned char legacy, legacy_cfg;
1959 int rev_h = 0;
1960
1961 legacy = chip->old_legacy;
1962 legacy_cfg = chip->old_legacy_cfg;
1963 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
1964 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
1965 if (chip->revision >= VIA_REV_686_H) {
1966 rev_h = 1;
b7fe4622
CL
1967 if (mpu_port >= 0x200) { /* force MIDI */
1968 mpu_port &= 0xfffc;
1969 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
1da177e4 1970#ifdef CONFIG_PM
b7fe4622 1971 chip->mpu_port_saved = mpu_port;
1da177e4
LT
1972#endif
1973 } else {
b7fe4622 1974 mpu_port = pci_resource_start(chip->pci, 2);
1da177e4
LT
1975 }
1976 } else {
b7fe4622 1977 switch (mpu_port) { /* force MIDI */
1da177e4
LT
1978 case 0x300:
1979 case 0x310:
1980 case 0x320:
1981 case 0x330:
1982 legacy_cfg &= ~(3 << 2);
b7fe4622 1983 legacy_cfg |= (mpu_port & 0x0030) >> 2;
1da177e4
LT
1984 break;
1985 default: /* no, use BIOS settings */
1986 if (legacy & VIA_FUNC_ENABLE_MIDI)
b7fe4622 1987 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
1da177e4
LT
1988 break;
1989 }
1990 }
b7fe4622
CL
1991 if (mpu_port >= 0x200 &&
1992 (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
1993 != NULL) {
1da177e4
LT
1994 if (rev_h)
1995 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
1996 legacy |= VIA_FUNC_ENABLE_MIDI;
1997 } else {
1998 if (rev_h)
1999 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
2000 legacy &= ~VIA_FUNC_ENABLE_MIDI;
b7fe4622 2001 mpu_port = 0;
1da177e4
LT
2002 }
2003
2004 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2005 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
2006 if (chip->mpu_res) {
2007 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
302e4c2f 2008 mpu_port, MPU401_INFO_INTEGRATED,
1da177e4 2009 chip->irq, 0, &chip->rmidi) < 0) {
b7fe4622
CL
2010 printk(KERN_WARNING "unable to initialize MPU-401"
2011 " at 0x%lx, skipping\n", mpu_port);
1da177e4
LT
2012 legacy &= ~VIA_FUNC_ENABLE_MIDI;
2013 } else {
2014 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
2015 }
2016 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2017 }
2018
b7fe4622 2019 snd_via686_create_gameport(chip, &legacy);
1da177e4
LT
2020
2021#ifdef CONFIG_PM
2022 chip->legacy_saved = legacy;
2023 chip->legacy_cfg_saved = legacy_cfg;
2024#endif
2025
2026 return 0;
2027}
2028
2029
2030/*
2031 * proc interface
2032 */
e437e3d7
TI
2033static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2034 struct snd_info_buffer *buffer)
1da177e4 2035{
e437e3d7 2036 struct via82xx *chip = entry->private_data;
1da177e4
LT
2037 int i;
2038
2039 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2040 for (i = 0; i < 0xa0; i += 4) {
2041 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2042 }
2043}
2044
e437e3d7 2045static void __devinit snd_via82xx_proc_init(struct via82xx *chip)
1da177e4 2046{
e437e3d7 2047 struct snd_info_entry *entry;
1da177e4
LT
2048
2049 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
bf850204 2050 snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
1da177e4
LT
2051}
2052
2053/*
2054 *
2055 */
2056
e437e3d7 2057static int snd_via82xx_chip_init(struct via82xx *chip)
1da177e4
LT
2058{
2059 unsigned int val;
ef21ca24 2060 unsigned long end_time;
1da177e4
LT
2061 unsigned char pval;
2062
2063#if 0 /* broken on K7M? */
2064 if (chip->chip_type == TYPE_VIA686)
2065 /* disable all legacy ports */
2066 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2067#endif
2068 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2069 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2070 /* deassert ACLink reset, force SYNC */
2071 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2072 VIA_ACLINK_CTRL_ENABLE |
2073 VIA_ACLINK_CTRL_RESET |
2074 VIA_ACLINK_CTRL_SYNC);
2075 udelay(100);
2076#if 1 /* FIXME: should we do full reset here for all chip models? */
2077 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2078 udelay(100);
2079#else
2080 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2081 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2082 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2083 udelay(2);
2084#endif
2085 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2086 /* note - FM data out has trouble with non VRA codecs !! */
2087 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2088 udelay(100);
2089 }
2090
2091 /* Make sure VRA is enabled, in case we didn't do a
2092 * complete codec reset, above */
2093 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2094 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2095 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2096 /* note - FM data out has trouble with non VRA codecs !! */
2097 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2098 udelay(100);
2099 }
2100
2101 /* wait until codec ready */
ef21ca24 2102 end_time = jiffies + msecs_to_jiffies(750);
1da177e4
LT
2103 do {
2104 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2105 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2106 break;
d86d0193 2107 schedule_timeout_uninterruptible(1);
ef21ca24 2108 } while (time_before(jiffies, end_time));
1da177e4
LT
2109
2110 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
99b359ba 2111 snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
1da177e4
LT
2112
2113#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2114 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2115 VIA_REG_AC97_SECONDARY_VALID |
2116 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
ef21ca24 2117 end_time = jiffies + msecs_to_jiffies(750);
1da177e4
LT
2118 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2119 VIA_REG_AC97_SECONDARY_VALID |
2120 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2121 do {
2122 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2123 chip->ac97_secondary = 1;
2124 goto __ac97_ok2;
2125 }
d86d0193 2126 schedule_timeout_uninterruptible(1);
ef21ca24 2127 } while (time_before(jiffies, end_time));
1da177e4
LT
2128 /* This is ok, the most of motherboards have only one codec */
2129
2130 __ac97_ok2:
2131#endif
2132
2133 if (chip->chip_type == TYPE_VIA686) {
2134 /* route FM trap to IRQ, disable FM trap */
2135 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2136 /* disable all GPI interrupts */
2137 outl(0, VIAREG(chip, GPI_INTR));
2138 }
2139
2140 if (chip->chip_type != TYPE_VIA686) {
2141 /* Workaround for Award BIOS bug:
2142 * DXS channels don't work properly with VRA if MC97 is disabled.
2143 */
2144 struct pci_dev *pci;
0dd119f7 2145 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
1da177e4
LT
2146 if (pci) {
2147 unsigned char data;
2148 pci_read_config_byte(pci, 0x44, &data);
2149 pci_write_config_byte(pci, 0x44, data | 0x40);
0dd119f7 2150 pci_dev_put(pci);
1da177e4
LT
2151 }
2152 }
2153
2154 if (chip->chip_type != TYPE_VIA8233A) {
2155 int i, idx;
2156 for (idx = 0; idx < 4; idx++) {
2157 unsigned long port = chip->port + 0x10 * idx;
00f226d4
HM
2158 for (i = 0; i < 2; i++) {
2159 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
e437e3d7
TI
2160 outb(chip->playback_volume_c[i],
2161 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
00f226d4 2162 }
1da177e4
LT
2163 }
2164 }
2165
2166 return 0;
2167}
2168
2169#ifdef CONFIG_PM
2170/*
2171 * power management
2172 */
57feb835 2173static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
1da177e4 2174{
57feb835
TI
2175 struct snd_card *card = pci_get_drvdata(pci);
2176 struct via82xx *chip = card->private_data;
1da177e4
LT
2177 int i;
2178
57feb835 2179 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1da177e4 2180 for (i = 0; i < 2; i++)
57feb835 2181 snd_pcm_suspend_all(chip->pcms[i]);
1da177e4
LT
2182 for (i = 0; i < chip->num_devs; i++)
2183 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2184 synchronize_irq(chip->irq);
2185 snd_ac97_suspend(chip->ac97);
2186
2187 /* save misc values */
2188 if (chip->chip_type != TYPE_VIA686) {
2189 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2190 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2191 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2192 }
2193
57feb835
TI
2194 pci_disable_device(pci);
2195 pci_save_state(pci);
30b35399 2196 pci_set_power_state(pci, pci_choose_state(pci, state));
1da177e4
LT
2197 return 0;
2198}
2199
57feb835 2200static int snd_via82xx_resume(struct pci_dev *pci)
1da177e4 2201{
57feb835
TI
2202 struct snd_card *card = pci_get_drvdata(pci);
2203 struct via82xx *chip = card->private_data;
1da177e4
LT
2204 int i;
2205
57feb835 2206 pci_set_power_state(pci, PCI_D0);
30b35399
TI
2207 pci_restore_state(pci);
2208 if (pci_enable_device(pci) < 0) {
2209 printk(KERN_ERR "via82xx: pci_enable_device failed, "
2210 "disabling device\n");
2211 snd_card_disconnect(card);
2212 return -EIO;
2213 }
2214 pci_set_master(pci);
1da177e4
LT
2215
2216 snd_via82xx_chip_init(chip);
2217
2218 if (chip->chip_type == TYPE_VIA686) {
2219 if (chip->mpu_port_saved)
2220 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2221 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2222 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2223 } else {
2224 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2225 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2226 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2227 }
2228
2229 snd_ac97_resume(chip->ac97);
2230
2231 for (i = 0; i < chip->num_devs; i++)
2232 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2233
57feb835 2234 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1da177e4
LT
2235 return 0;
2236}
2237#endif /* CONFIG_PM */
2238
e437e3d7 2239static int snd_via82xx_free(struct via82xx *chip)
1da177e4
LT
2240{
2241 unsigned int i;
2242
2243 if (chip->irq < 0)
2244 goto __end_hw;
2245 /* disable interrupts */
2246 for (i = 0; i < chip->num_devs; i++)
2247 snd_via82xx_channel_reset(chip, &chip->devs[i]);
f000fd80 2248
1da177e4 2249 if (chip->irq >= 0)
e437e3d7 2250 free_irq(chip->irq, chip);
757d5a75 2251 __end_hw:
b1d5776d 2252 release_and_free_resource(chip->mpu_res);
1da177e4
LT
2253 pci_release_regions(chip->pci);
2254
2255 if (chip->chip_type == TYPE_VIA686) {
2256 snd_via686_free_gameport(chip);
2257 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2258 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2259 }
2260 pci_disable_device(chip->pci);
2261 kfree(chip);
2262 return 0;
2263}
2264
e437e3d7 2265static int snd_via82xx_dev_free(struct snd_device *device)
1da177e4 2266{
e437e3d7 2267 struct via82xx *chip = device->device_data;
1da177e4
LT
2268 return snd_via82xx_free(chip);
2269}
2270
e437e3d7 2271static int __devinit snd_via82xx_create(struct snd_card *card,
1da177e4
LT
2272 struct pci_dev *pci,
2273 int chip_type,
2274 int revision,
2275 unsigned int ac97_clock,
e437e3d7 2276 struct via82xx ** r_via)
1da177e4 2277{
e437e3d7 2278 struct via82xx *chip;
1da177e4 2279 int err;
e437e3d7 2280 static struct snd_device_ops ops = {
1da177e4
LT
2281 .dev_free = snd_via82xx_dev_free,
2282 };
2283
2284 if ((err = pci_enable_device(pci)) < 0)
2285 return err;
2286
e560d8d8 2287 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
1da177e4
LT
2288 pci_disable_device(pci);
2289 return -ENOMEM;
2290 }
2291
2292 chip->chip_type = chip_type;
2293 chip->revision = revision;
2294
2295 spin_lock_init(&chip->reg_lock);
2296 spin_lock_init(&chip->rates[0].lock);
2297 spin_lock_init(&chip->rates[1].lock);
2298 chip->card = card;
2299 chip->pci = pci;
2300 chip->irq = -1;
2301
2302 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2303 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2304 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2305 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2306
2307 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2308 kfree(chip);
2309 pci_disable_device(pci);
2310 return err;
2311 }
2312 chip->port = pci_resource_start(pci, 0);
4f550df5
KW
2313 if (request_irq(pci->irq,
2314 chip_type == TYPE_VIA8233 ?
2315 snd_via8233_interrupt : snd_via686_interrupt,
437a5a46 2316 IRQF_SHARED,
e437e3d7 2317 card->driver, chip)) {
99b359ba 2318 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1da177e4
LT
2319 snd_via82xx_free(chip);
2320 return -EBUSY;
2321 }
2322 chip->irq = pci->irq;
2323 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2324 chip->ac97_clock = ac97_clock;
2325 synchronize_irq(chip->irq);
2326
2327 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2328 snd_via82xx_free(chip);
2329 return err;
2330 }
2331
2332 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2333 snd_via82xx_free(chip);
2334 return err;
2335 }
2336
2337 /* The 8233 ac97 controller does not implement the master bit
2338 * in the pci command register. IMHO this is a violation of the PCI spec.
2339 * We call pci_set_master here because it does not hurt. */
2340 pci_set_master(pci);
2341
2342 snd_card_set_dev(card, &pci->dev);
2343
2344 *r_via = chip;
2345 return 0;
2346}
2347
2348struct via823x_info {
2349 int revision;
2350 char *name;
2351 int type;
2352};
2353static struct via823x_info via823x_cards[] __devinitdata = {
2354 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2355 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2356 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2357 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2358 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2359 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
8263c65f 2360 { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
1da177e4
LT
2361};
2362
2363/*
2364 * auto detection of DXS channel supports.
2365 */
9d74958a
TI
2366
2367static struct snd_pci_quirk dxs_whitelist[] __devinitdata = {
2368 SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2369 SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2370 SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2371 SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
2372 SND_PCI_QUIRK(0x1019, 0, "ESC K8", VIA_DXS_SRC),
2373 SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2374 SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2375 SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
2376 SND_PCI_QUIRK(0x1043, 0, "ASUS A7/A8", VIA_DXS_NO_VRA),
2377 SND_PCI_QUIRK(0x1071, 0, "Diverse Notebook", VIA_DXS_NO_VRA),
2378 SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
2379 SND_PCI_QUIRK(0x1106, 0, "ASRock", VIA_DXS_SRC),
11be265f 2380 SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
9674513d
TI
2381 SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2382 SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
9d74958a
TI
2383 SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2384 SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2385 SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2386 SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
2387 SND_PCI_QUIRK(0x1462, 0, "MSI Mobo", VIA_DXS_SRC),
2388 SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2389 SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2390 SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2391 SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2392 SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2393 SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2394 SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2395 SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2396 SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2397 SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2398 SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2399 SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2400 SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
2401 SND_PCI_QUIRK(0x1695, 0, "EPoX mobo", VIA_DXS_SRC),
2402 SND_PCI_QUIRK(0x16f3, 0, "Jetway K8", VIA_DXS_SRC),
2403 SND_PCI_QUIRK(0x1734, 0, "FSC Laptop", VIA_DXS_SRC),
2404 SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
2405 SND_PCI_QUIRK(0x1849, 0, "ASRock mobo", VIA_DXS_SRC),
2406 SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8", VIA_DXS_NO_VRA),
2407 SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2408 { } /* terminator */
1da177e4
LT
2409};
2410
a769577b 2411static int __devinit check_dxs_list(struct pci_dev *pci, int revision)
1da177e4 2412{
9d74958a 2413 const struct snd_pci_quirk *w;
1da177e4 2414
9d74958a
TI
2415 w = snd_pci_quirk_lookup(pci, dxs_whitelist);
2416 if (w) {
2417 snd_printdd(KERN_INFO "via82xx: DXS white list for %s found\n",
2418 w->name);
2419 return w->value;
1da177e4
LT
2420 }
2421
a769577b
TI
2422 /* for newer revision, default to DXS_SRC */
2423 if (revision >= VIA_REV_8235)
2424 return VIA_DXS_SRC;
2425
1da177e4
LT
2426 /*
2427 * not detected, try 48k rate only to be sure.
2428 */
2429 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
ee3b4c60 2430 printk(KERN_INFO " Please try dxs_support=5 option\n");
1da177e4 2431 printk(KERN_INFO " and report if it works on your machine.\n");
ee3b4c60 2432 printk(KERN_INFO " For more details, read ALSA-Configuration.txt.\n");
1da177e4
LT
2433 return VIA_DXS_48K;
2434};
2435
2436static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2437 const struct pci_device_id *pci_id)
2438{
e437e3d7
TI
2439 struct snd_card *card;
2440 struct via82xx *chip;
1da177e4
LT
2441 int chip_type = 0, card_type;
2442 unsigned int i;
2443 int err;
2444
b7fe4622 2445 card = snd_card_new(index, id, THIS_MODULE, 0);
1da177e4
LT
2446 if (card == NULL)
2447 return -ENOMEM;
2448
2449 card_type = pci_id->driver_data;
1da177e4
LT
2450 switch (card_type) {
2451 case TYPE_CARD_VIA686:
2452 strcpy(card->driver, "VIA686A");
44c10138 2453 sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
1da177e4
LT
2454 chip_type = TYPE_VIA686;
2455 break;
2456 case TYPE_CARD_VIA8233:
2457 chip_type = TYPE_VIA8233;
44c10138 2458 sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
1da177e4 2459 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
44c10138 2460 if (pci->revision == via823x_cards[i].revision) {
1da177e4
LT
2461 chip_type = via823x_cards[i].type;
2462 strcpy(card->shortname, via823x_cards[i].name);
2463 break;
2464 }
2465 }
2466 if (chip_type != TYPE_VIA8233A) {
b7fe4622 2467 if (dxs_support == VIA_DXS_AUTO)
44c10138 2468 dxs_support = check_dxs_list(pci, pci->revision);
1da177e4
LT
2469 /* force to use VIA8233 or 8233A model according to
2470 * dxs_support module option
2471 */
b7fe4622 2472 if (dxs_support == VIA_DXS_DISABLE)
1da177e4
LT
2473 chip_type = TYPE_VIA8233A;
2474 else
2475 chip_type = TYPE_VIA8233;
2476 }
2477 if (chip_type == TYPE_VIA8233A)
2478 strcpy(card->driver, "VIA8233A");
44c10138 2479 else if (pci->revision >= VIA_REV_8237)
1da177e4
LT
2480 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2481 else
2482 strcpy(card->driver, "VIA8233");
2483 break;
2484 default:
2485 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2486 err = -EINVAL;
2487 goto __error;
2488 }
2489
44c10138 2490 if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
b7fe4622 2491 ac97_clock, &chip)) < 0)
1da177e4 2492 goto __error;
57feb835 2493 card->private_data = chip;
b7fe4622 2494 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
1da177e4
LT
2495 goto __error;
2496
2497 if (chip_type == TYPE_VIA686) {
2498 if ((err = snd_via686_pcm_new(chip)) < 0 ||
b7fe4622 2499 (err = snd_via686_init_misc(chip)) < 0)
1da177e4
LT
2500 goto __error;
2501 } else {
2502 if (chip_type == TYPE_VIA8233A) {
2503 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2504 goto __error;
2505 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2506 } else {
2507 if ((err = snd_via8233_pcm_new(chip)) < 0)
2508 goto __error;
b7fe4622 2509 if (dxs_support == VIA_DXS_48K)
1da177e4 2510 chip->dxs_fixed = 1;
b7fe4622 2511 else if (dxs_support == VIA_DXS_NO_VRA)
1da177e4 2512 chip->no_vra = 1;
b7fe4622 2513 else if (dxs_support == VIA_DXS_SRC) {
2d7eb7cb
SV
2514 chip->no_vra = 1;
2515 chip->dxs_src = 1;
2516 }
1da177e4 2517 }
b7fe4622 2518 if ((err = snd_via8233_init_misc(chip)) < 0)
1da177e4
LT
2519 goto __error;
2520 }
2521
1da177e4
LT
2522 /* disable interrupts */
2523 for (i = 0; i < chip->num_devs; i++)
2524 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2525
2526 snprintf(card->longname, sizeof(card->longname),
2527 "%s with %s at %#lx, irq %d", card->shortname,
2528 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2529
2530 snd_via82xx_proc_init(chip);
2531
2532 if ((err = snd_card_register(card)) < 0) {
2533 snd_card_free(card);
2534 return err;
2535 }
2536 pci_set_drvdata(pci, card);
1da177e4
LT
2537 return 0;
2538
2539 __error:
2540 snd_card_free(card);
2541 return err;
2542}
2543
2544static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2545{
2546 snd_card_free(pci_get_drvdata(pci));
2547 pci_set_drvdata(pci, NULL);
2548}
2549
2550static struct pci_driver driver = {
2551 .name = "VIA 82xx Audio",
2552 .id_table = snd_via82xx_ids,
2553 .probe = snd_via82xx_probe,
2554 .remove = __devexit_p(snd_via82xx_remove),
57feb835
TI
2555#ifdef CONFIG_PM
2556 .suspend = snd_via82xx_suspend,
2557 .resume = snd_via82xx_resume,
2558#endif
1da177e4
LT
2559};
2560
2561static int __init alsa_card_via82xx_init(void)
2562{
01d25d46 2563 return pci_register_driver(&driver);
1da177e4
LT
2564}
2565
2566static void __exit alsa_card_via82xx_exit(void)
2567{
2568 pci_unregister_driver(&driver);
2569}
2570
2571module_init(alsa_card_via82xx_init)
2572module_exit(alsa_card_via82xx_exit)