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