2 * ALSA driver for VIA VT82xx (South Bridge)
4 * VT82C686A/B/C, VT8233A/C, VT8235
6 * Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
7 * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
8 * 2002 Takashi Iwai <tiwai@suse.de>
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.
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.
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
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
35 * - support the secondary capture (on VIA8233/C,8235)
37 * the DSX3 channel can be used for SPDIF output.
38 * on VIA8233A, this channel is assigned to the second pcm
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.
46 #include <sound/driver.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>
67 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
68 MODULE_DESCRIPTION("VIA VT82xx audio");
69 MODULE_LICENSE("GPL");
70 MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
72 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
73 #define SUPPORT_JOYSTICK 1
76 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
77 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
78 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
79 static long mpu_port[SNDRV_CARDS];
80 #ifdef SUPPORT_JOYSTICK
81 static int joystick[SNDRV_CARDS];
83 static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 48000};
84 static int ac97_quirk[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = AC97_TUNE_DEFAULT};
85 static int dxs_support[SNDRV_CARDS];
88 module_param_array(index, int, boot_devs, 0444);
89 MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
90 module_param_array(id, charp, boot_devs, 0444);
91 MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
92 module_param_array(enable, bool, boot_devs, 0444);
93 MODULE_PARM_DESC(enable, "Enable audio part of VIA 82xx bridge.");
94 module_param_array(mpu_port, long, boot_devs, 0444);
95 MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
96 #ifdef SUPPORT_JOYSTICK
97 module_param_array(joystick, bool, boot_devs, 0444);
98 MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
100 module_param_array(ac97_clock, int, boot_devs, 0444);
101 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
102 module_param_array(ac97_quirk, int, boot_devs, 0444);
103 MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
104 module_param_array(dxs_support, int, boot_devs, 0444);
105 MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA)");
109 #ifndef PCI_DEVICE_ID_VIA_82C686_5
110 #define PCI_DEVICE_ID_VIA_82C686_5 0x3058
112 #ifndef PCI_DEVICE_ID_VIA_8233_5
113 #define PCI_DEVICE_ID_VIA_8233_5 0x3059
116 /* revision numbers for via686 */
117 #define VIA_REV_686_A 0x10
118 #define VIA_REV_686_B 0x11
119 #define VIA_REV_686_C 0x12
120 #define VIA_REV_686_D 0x13
121 #define VIA_REV_686_E 0x14
122 #define VIA_REV_686_H 0x20
124 /* revision numbers for via8233 */
125 #define VIA_REV_PRE_8233 0x10 /* not in market */
126 #define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
127 #define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
128 #define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
129 #define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
135 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
136 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
139 #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
140 #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
141 #define VIA_REG_STAT_PAUSED 0x40 /* RO */
142 #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
143 #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
144 #define VIA_REG_STAT_EOL 0x02 /* RWC */
145 #define VIA_REG_STAT_FLAG 0x01 /* RWC */
146 #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
147 #define VIA_REG_CTRL_START 0x80 /* WO */
148 #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
149 #define VIA_REG_CTRL_AUTOSTART 0x20
150 #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
151 #define VIA_REG_CTRL_INT_STOP 0x04
152 #define VIA_REG_CTRL_INT_EOL 0x02
153 #define VIA_REG_CTRL_INT_FLAG 0x01
154 #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
155 #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
156 #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
157 #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
158 #define VIA_REG_TYPE_16BIT 0x20 /* RW */
159 #define VIA_REG_TYPE_STEREO 0x10 /* RW */
160 #define VIA_REG_TYPE_INT_LLINE 0x00
161 #define VIA_REG_TYPE_INT_LSAMPLE 0x04
162 #define VIA_REG_TYPE_INT_LESSONE 0x08
163 #define VIA_REG_TYPE_INT_MASK 0x0c
164 #define VIA_REG_TYPE_INT_EOL 0x02
165 #define VIA_REG_TYPE_INT_FLAG 0x01
166 #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
167 #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
168 #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
169 #define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
170 #define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
171 #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
172 #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
174 #define DEFINE_VIA_REGSET(name,val) \
176 VIA_REG_##name##_STATUS = (val),\
177 VIA_REG_##name##_CONTROL = (val) + 0x01,\
178 VIA_REG_##name##_TYPE = (val) + 0x02,\
179 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
180 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
181 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
182 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
186 DEFINE_VIA_REGSET(PLAYBACK, 0x00);
187 DEFINE_VIA_REGSET(CAPTURE, 0x10);
188 DEFINE_VIA_REGSET(FM, 0x20);
191 #define VIA_REG_AC97 0x80 /* dword */
192 #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
193 #define VIA_REG_AC97_CODEC_ID_SHIFT 30
194 #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
195 #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
196 #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
197 #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
198 #define VIA_REG_AC97_BUSY (1<<24)
199 #define VIA_REG_AC97_READ (1<<23)
200 #define VIA_REG_AC97_CMD_SHIFT 16
201 #define VIA_REG_AC97_CMD_MASK 0x7e
202 #define VIA_REG_AC97_DATA_SHIFT 0
203 #define VIA_REG_AC97_DATA_MASK 0xffff
205 #define VIA_REG_SGD_SHADOW 0x84 /* dword */
207 #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
208 #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
209 #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
210 #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
211 #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
212 #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
213 #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
214 #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
215 #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
216 #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
217 #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
218 #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
220 #define VIA8233_REG_SGD_STAT_FLAG (1<<0)
221 #define VIA8233_REG_SGD_STAT_EOL (1<<1)
222 #define VIA8233_REG_SGD_STAT_STOP (1<<2)
223 #define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
224 #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
225 #define VIA8233_REG_SGD_CHAN_SDX 0
226 #define VIA8233_REG_SGD_CHAN_MULTI 4
227 #define VIA8233_REG_SGD_CHAN_REC 6
228 #define VIA8233_REG_SGD_CHAN_REC1 7
230 #define VIA_REG_GPI_STATUS 0x88
231 #define VIA_REG_GPI_INTR 0x8c
233 /* multi-channel and capture registers for via8233 */
234 DEFINE_VIA_REGSET(MULTPLAY, 0x40);
235 DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
237 /* via8233-specific registers */
238 #define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
239 #define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
240 #define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
241 #define VIA_REG_MULTPLAY_FMT_8BIT 0x00
242 #define VIA_REG_MULTPLAY_FMT_16BIT 0x80
243 #define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
244 #define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
245 #define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
247 #define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
249 #define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
250 #define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
251 #define VIA_REG_CAPTURE_CHANNEL_LINE 0
252 #define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
254 #define VIA_TBL_BIT_FLAG 0x40000000
255 #define VIA_TBL_BIT_EOL 0x80000000
258 #define VIA_ACLINK_STAT 0x40
259 #define VIA_ACLINK_C11_READY 0x20
260 #define VIA_ACLINK_C10_READY 0x10
261 #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
262 #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
263 #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
264 #define VIA_ACLINK_CTRL 0x41
265 #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
266 #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
267 #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
268 #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
269 #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
270 #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
271 #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
272 #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
273 #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
274 VIA_ACLINK_CTRL_RESET|\
275 VIA_ACLINK_CTRL_PCM|\
277 #define VIA_FUNC_ENABLE 0x42
278 #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
279 #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
280 #define VIA_FUNC_RX2C_WRITE 0x20
281 #define VIA_FUNC_SB_FIFO_EMPTY 0x10
282 #define VIA_FUNC_ENABLE_GAME 0x08
283 #define VIA_FUNC_ENABLE_FM 0x04
284 #define VIA_FUNC_ENABLE_MIDI 0x02
285 #define VIA_FUNC_ENABLE_SB 0x01
286 #define VIA_PNP_CONTROL 0x43
287 #define VIA_FM_NMI_CTRL 0x48
288 #define VIA8233_VOLCHG_CTRL 0x48
289 #define VIA8233_SPDIF_CTRL 0x49
290 #define VIA8233_SPDIF_DX3 0x08
291 #define VIA8233_SPDIF_SLOT_MASK 0x03
292 #define VIA8233_SPDIF_SLOT_1011 0x00
293 #define VIA8233_SPDIF_SLOT_34 0x01
294 #define VIA8233_SPDIF_SLOT_78 0x02
295 #define VIA8233_SPDIF_SLOT_69 0x03
300 #define VIA_DXS_AUTO 0
301 #define VIA_DXS_ENABLE 1
302 #define VIA_DXS_DISABLE 2
303 #define VIA_DXS_48K 3
304 #define VIA_DXS_NO_VRA 4
310 typedef struct _snd_via82xx via82xx_t;
311 typedef struct via_dev viadev_t;
317 struct snd_via_sg_table {
322 #define VIA_TABLE_SIZE 255
325 unsigned int reg_offset;
327 int direction; /* playback = 0, capture = 1 */
328 snd_pcm_substream_t *substream;
330 unsigned int tbl_entries; /* # descriptors */
331 struct snd_dma_buffer table;
332 struct snd_via_sg_table *idx_table;
333 /* for recovery from the unexpected pointer */
334 unsigned int lastpos;
335 unsigned int bufsize;
336 unsigned int bufsize2;
340 enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
341 enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
343 #define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
345 struct via_rate_lock {
351 struct _snd_via82xx {
355 struct resource *mpu_res;
357 unsigned char revision;
359 unsigned char old_legacy;
360 unsigned char old_legacy_cfg;
362 unsigned char legacy_saved;
363 unsigned char legacy_cfg_saved;
364 unsigned char spdif_ctrl_saved;
365 unsigned char capture_src_saved[2];
366 unsigned int mpu_port_saved;
369 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
371 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
376 unsigned int num_devs;
377 unsigned int playback_devno, multi_devno, capture_devno;
378 viadev_t devs[VIA_MAX_DEVS];
379 struct via_rate_lock rates[2]; /* playback and capture */
380 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
381 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
382 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
385 snd_rawmidi_t *rmidi;
387 ac97_bus_t *ac97_bus;
389 unsigned int ac97_clock;
390 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
393 spinlock_t ac97_lock;
394 snd_info_entry_t *proc_entry;
396 #ifdef SUPPORT_JOYSTICK
397 struct gameport gameport;
398 struct resource *res_joystick;
402 static struct pci_device_id snd_via82xx_ids[] = {
403 { 0x1106, 0x3058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
404 { 0x1106, 0x3059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
408 MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
414 * allocate and initialize the descriptor buffers
415 * periods = number of periods
416 * fragsize = period size in bytes
418 static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
420 unsigned int periods, unsigned int fragsize)
422 unsigned int i, idx, ofs, rest;
423 via82xx_t *chip = snd_pcm_substream_chip(substream);
424 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
426 if (dev->table.area == NULL) {
427 /* the start of each lists must be aligned to 8 bytes,
428 * but the kernel pages are much bigger, so we don't care
430 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
431 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
435 if (! dev->idx_table) {
436 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
437 if (! dev->idx_table)
441 /* fill the entries */
444 for (i = 0; i < periods; i++) {
446 /* fill descriptors for a period.
447 * a period can be split to several descriptors if it's
448 * over page boundary.
454 if (idx >= VIA_TABLE_SIZE) {
455 snd_printk(KERN_ERR "via82xx: too much table size!\n");
458 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
459 r = PAGE_SIZE - (ofs % PAGE_SIZE);
464 if (i == periods - 1)
465 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
467 flag = VIA_TBL_BIT_FLAG; /* period boundary */
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;
478 dev->tbl_entries = idx;
479 dev->bufsize = periods * fragsize;
480 dev->bufsize2 = dev->bufsize / 2;
485 static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
488 if (dev->table.area) {
489 snd_dma_free_pages(&dev->table);
490 dev->table.area = NULL;
492 if (dev->idx_table) {
493 kfree(dev->idx_table);
494 dev->idx_table = NULL;
503 static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
505 return inl(VIAREG(chip, AC97));
508 static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
510 outl(val, VIAREG(chip, AC97));
513 static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
515 unsigned int timeout = 1000; /* 1ms */
518 while (timeout-- > 0) {
520 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
523 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
527 static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
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;
534 while (timeout-- > 0) {
535 val = snd_via82xx_codec_xread(chip);
536 val1 = val & (VIA_REG_AC97_BUSY | stat);
544 static void snd_via82xx_codec_wait(ac97_t *ac97)
546 via82xx_t *chip = ac97->private_data;
548 err = snd_via82xx_codec_ready(chip, ac97->num);
549 /* here we need to wait fairly for long time.. */
550 set_current_state(TASK_UNINTERRUPTIBLE);
551 schedule_timeout(HZ/2);
554 static void snd_via82xx_codec_write(ac97_t *ac97,
558 via82xx_t *chip = ac97->private_data;
561 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
562 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
563 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
564 xval |= val << VIA_REG_AC97_DATA_SHIFT;
565 spin_lock(&chip->ac97_lock);
566 snd_via82xx_codec_xwrite(chip, xval);
567 snd_via82xx_codec_ready(chip, ac97->num);
568 spin_unlock(&chip->ac97_lock);
571 static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
573 via82xx_t *chip = ac97->private_data;
574 unsigned int xval, val = 0xffff;
577 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
578 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
579 xval |= VIA_REG_AC97_READ;
580 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
581 spin_lock(&chip->ac97_lock);
584 spin_unlock(&chip->ac97_lock);
585 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
588 snd_via82xx_codec_xwrite(chip, xval);
590 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
592 val = snd_via82xx_codec_xread(chip);
596 spin_unlock(&chip->ac97_lock);
600 static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
602 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
603 VIADEV_REG(viadev, OFFSET_CONTROL));
604 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
606 /* disable interrupts */
607 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
608 /* clear interrupts */
609 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
610 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
611 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
620 static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
622 via82xx_t *chip = dev_id;
627 /* FIXME: does it work on via823x? */
628 if (chip->chip_type != TYPE_VIA686)
631 status = inl(VIAREG(chip, SGD_SHADOW));
632 if (! (status & chip->intr_mask)) {
634 /* check mpu401 interrupt */
635 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
640 /* check status for each stream */
641 spin_lock(&chip->reg_lock);
642 for (i = 0; i < chip->num_devs; i++) {
643 viadev_t *viadev = &chip->devs[i];
644 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
645 c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
648 if (viadev->substream && viadev->running) {
649 spin_unlock(&chip->reg_lock);
650 snd_pcm_period_elapsed(viadev->substream);
651 spin_lock(&chip->reg_lock);
653 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
655 spin_unlock(&chip->reg_lock);
666 static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
668 via82xx_t *chip = snd_pcm_substream_chip(substream);
669 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
672 if (chip->chip_type != TYPE_VIA686)
673 val = VIA_REG_CTRL_INT;
677 case SNDRV_PCM_TRIGGER_START:
678 val |= VIA_REG_CTRL_START;
681 case SNDRV_PCM_TRIGGER_STOP:
682 val = VIA_REG_CTRL_TERMINATE;
685 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
686 val |= VIA_REG_CTRL_PAUSE;
689 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
695 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
696 if (cmd == SNDRV_PCM_TRIGGER_STOP)
697 snd_via82xx_channel_reset(chip, viadev);
707 * calculate the linear position at the given sg-buffer index and the rest count
710 #define check_invalid_pos(viadev,pos) \
711 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))
713 static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
715 unsigned int size, res;
717 size = viadev->idx_table[idx].size;
718 res = viadev->idx_table[idx].offset + size - count;
720 /* check the validity of the calculated position */
722 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n", (int)size, (int)count);
723 res = viadev->lastpos;
724 } else if (check_invalid_pos(viadev, res)) {
726 printk("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);
728 if (count && size < count) {
729 snd_printd(KERN_ERR "invalid via82xx_cur_ptr, using last valid pointer\n");
730 res = viadev->lastpos;
733 /* bogus count 0 on the DMA boundary? */
734 res = viadev->idx_table[idx].offset;
736 /* count register returns full size when end of buffer is reached */
737 res = viadev->idx_table[idx].offset + size;
738 if (check_invalid_pos(viadev, res)) {
739 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
740 res = viadev->lastpos;
744 viadev->lastpos = res; /* remember the last position */
745 if (res >= viadev->bufsize)
746 res -= viadev->bufsize;
751 * get the current pointer on via686
753 static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
755 via82xx_t *chip = snd_pcm_substream_chip(substream);
756 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
757 unsigned int idx, ptr, count, res;
759 snd_assert(viadev->tbl_entries, return 0);
760 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
763 spin_lock(&chip->reg_lock);
764 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
765 /* The via686a does not have the current index register,
766 * so we need to calculate the index from CURR_PTR.
768 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
769 if (ptr <= (unsigned int)viadev->table.addr)
771 else /* CURR_PTR holds the address + 8 */
772 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
773 res = calc_linear_pos(viadev, idx, count);
774 spin_unlock(&chip->reg_lock);
776 return bytes_to_frames(substream->runtime, res);
780 * get the current pointer on via823x
782 static snd_pcm_uframes_t snd_via8233_pcm_pointer(snd_pcm_substream_t *substream)
784 via82xx_t *chip = snd_pcm_substream_chip(substream);
785 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
786 unsigned int idx, count, res;
788 snd_assert(viadev->tbl_entries, return 0);
789 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
791 spin_lock(&chip->reg_lock);
792 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
794 if (idx >= viadev->tbl_entries) {
796 printk("fail: invalid idx = %i/%i\n", idx, viadev->tbl_entries);
798 res = viadev->lastpos;
801 res = calc_linear_pos(viadev, idx, count);
803 spin_unlock(&chip->reg_lock);
805 return bytes_to_frames(substream->runtime, res);
810 * hw_params callback:
811 * allocate the buffer and build up the buffer description table
813 static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
814 snd_pcm_hw_params_t * hw_params)
816 via82xx_t *chip = snd_pcm_substream_chip(substream);
817 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
820 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
823 err = build_via_table(viadev, substream, chip->pci,
824 params_periods(hw_params),
825 params_period_bytes(hw_params));
834 * clean up the buffer description table and release the buffer
836 static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
838 via82xx_t *chip = snd_pcm_substream_chip(substream);
839 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
841 clean_via_table(viadev, substream, chip->pci);
842 snd_pcm_lib_free_pages(substream);
848 * set up the table pointer
850 static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
852 snd_via82xx_codec_ready(chip, 0);
853 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
855 snd_via82xx_codec_ready(chip, 0);
859 * prepare callback for playback and capture on via686
861 static void via686_setup_format(via82xx_t *chip, viadev_t *viadev, snd_pcm_runtime_t *runtime)
863 snd_via82xx_channel_reset(chip, viadev);
864 /* this must be set after channel_reset */
865 snd_via82xx_set_table_ptr(chip, viadev);
866 outb(VIA_REG_TYPE_AUTOSTART |
867 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
868 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
869 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
870 VIA_REG_TYPE_INT_EOL |
871 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
874 static int snd_via686_playback_prepare(snd_pcm_substream_t *substream)
876 via82xx_t *chip = snd_pcm_substream_chip(substream);
877 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
878 snd_pcm_runtime_t *runtime = substream->runtime;
880 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
881 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
882 via686_setup_format(chip, viadev, runtime);
886 static int snd_via686_capture_prepare(snd_pcm_substream_t *substream)
888 via82xx_t *chip = snd_pcm_substream_chip(substream);
889 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
890 snd_pcm_runtime_t *runtime = substream->runtime;
892 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
893 via686_setup_format(chip, viadev, runtime);
898 * lock the current rate
900 static int via_lock_rate(struct via_rate_lock *rec, int rate)
904 spin_lock_irq(&rec->lock);
905 if (rec->rate != rate) {
906 if (rec->rate && rec->used > 1) /* already set */
913 spin_unlock_irq(&rec->lock);
918 * prepare callback for DSX playback on via823x
920 static int snd_via8233_playback_prepare(snd_pcm_substream_t *substream)
922 via82xx_t *chip = snd_pcm_substream_chip(substream);
923 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
924 snd_pcm_runtime_t *runtime = substream->runtime;
928 if ((rate_changed = via_lock_rate(&chip->rates[0], runtime->rate)) < 0)
931 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
932 chip->no_vra ? 48000 : runtime->rate);
933 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
935 if (runtime->rate == 48000)
938 rbits = (0x100000 / 48000) * runtime->rate + ((0x100000 % 48000) * runtime->rate) / 48000;
939 snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
940 snd_via82xx_channel_reset(chip, viadev);
941 snd_via82xx_set_table_ptr(chip, viadev);
942 outb(chip->playback_volume[viadev->reg_offset / 0x10][0], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
943 outb(chip->playback_volume[viadev->reg_offset / 0x10][1], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
944 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
945 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
947 0xff000000, /* STOP index is never reached */
948 VIADEV_REG(viadev, OFFSET_STOP_IDX));
950 snd_via82xx_codec_ready(chip, 0);
955 * prepare callback for multi-channel playback on via823x
957 static int snd_via8233_multi_prepare(snd_pcm_substream_t *substream)
959 via82xx_t *chip = snd_pcm_substream_chip(substream);
960 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
961 snd_pcm_runtime_t *runtime = substream->runtime;
965 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
967 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
968 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
969 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
970 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
971 snd_via82xx_channel_reset(chip, viadev);
972 snd_via82xx_set_table_ptr(chip, viadev);
974 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ? VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
975 fmt |= runtime->channels << 4;
976 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
978 if (chip->revision == VIA_REV_8233A)
983 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
984 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
985 switch (runtime->channels) {
986 case 1: slots = (1<<0) | (1<<4); break;
987 case 2: slots = (1<<0) | (2<<4); break;
988 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
989 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
990 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
991 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
992 default: slots = 0; break;
995 /* STOP index is never reached */
996 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
998 snd_via82xx_codec_ready(chip, 0);
1003 * prepare callback for capture on via823x
1005 static int snd_via8233_capture_prepare(snd_pcm_substream_t *substream)
1007 via82xx_t *chip = snd_pcm_substream_chip(substream);
1008 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1009 snd_pcm_runtime_t *runtime = substream->runtime;
1011 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1013 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1014 snd_via82xx_channel_reset(chip, viadev);
1015 snd_via82xx_set_table_ptr(chip, viadev);
1016 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1017 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1018 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1019 0xff000000, /* STOP index is never reached */
1020 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1022 snd_via82xx_codec_ready(chip, 0);
1028 * pcm hardware definition, identical for both playback and capture
1030 static snd_pcm_hardware_t snd_via82xx_hw =
1032 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1033 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1034 SNDRV_PCM_INFO_MMAP_VALID |
1035 SNDRV_PCM_INFO_RESUME |
1036 SNDRV_PCM_INFO_PAUSE),
1037 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1038 .rates = SNDRV_PCM_RATE_48000,
1043 .buffer_bytes_max = 128 * 1024,
1044 .period_bytes_min = 32,
1045 .period_bytes_max = 128 * 1024,
1047 .periods_max = VIA_TABLE_SIZE / 2,
1053 * open callback skeleton
1055 static int snd_via82xx_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
1057 snd_pcm_runtime_t *runtime = substream->runtime;
1059 struct via_rate_lock *ratep;
1061 runtime->hw = snd_via82xx_hw;
1063 /* set the hw rate condition */
1064 ratep = &chip->rates[viadev->direction];
1065 spin_lock_irq(&ratep->lock);
1067 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1068 /* DXS#3 and spdif is on */
1069 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1070 snd_pcm_limit_hw_rates(runtime);
1071 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1072 /* fixed DXS playback rate */
1073 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1074 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1075 } else if (! ratep->rate) {
1076 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1077 runtime->hw.rates = chip->ac97->rates[idx];
1078 snd_pcm_limit_hw_rates(runtime);
1081 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1082 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1084 spin_unlock_irq(&ratep->lock);
1086 /* we may remove following constaint when we modify table entries
1088 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1091 runtime->private_data = viadev;
1092 viadev->substream = substream;
1099 * open callback for playback on via686 and via823x DSX
1101 static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
1103 via82xx_t *chip = snd_pcm_substream_chip(substream);
1104 viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
1107 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1113 * open callback for playback on via823x multi-channel
1115 static int snd_via8233_multi_open(snd_pcm_substream_t * substream)
1117 via82xx_t *chip = snd_pcm_substream_chip(substream);
1118 viadev_t *viadev = &chip->devs[chip->multi_devno];
1120 /* channels constraint for VIA8233A
1121 * 3 and 5 channels are not supported
1123 static unsigned int channels[] = {
1126 static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
1127 .count = ARRAY_SIZE(channels),
1132 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1134 substream->runtime->hw.channels_max = 6;
1135 if (chip->revision == VIA_REV_8233A)
1136 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
1141 * open callback for capture on via686 and via823x
1143 static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
1145 via82xx_t *chip = snd_pcm_substream_chip(substream);
1146 viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1148 return snd_via82xx_pcm_open(chip, viadev, substream);
1154 static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
1156 via82xx_t *chip = snd_pcm_substream_chip(substream);
1157 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1158 struct via_rate_lock *ratep;
1160 /* release the rate lock */
1161 ratep = &chip->rates[viadev->direction];
1162 spin_lock_irq(&ratep->lock);
1166 spin_unlock_irq(&ratep->lock);
1168 viadev->substream = NULL;
1173 /* via686 playback callbacks */
1174 static snd_pcm_ops_t snd_via686_playback_ops = {
1175 .open = snd_via82xx_playback_open,
1176 .close = snd_via82xx_pcm_close,
1177 .ioctl = snd_pcm_lib_ioctl,
1178 .hw_params = snd_via82xx_hw_params,
1179 .hw_free = snd_via82xx_hw_free,
1180 .prepare = snd_via686_playback_prepare,
1181 .trigger = snd_via82xx_pcm_trigger,
1182 .pointer = snd_via686_pcm_pointer,
1183 .page = snd_pcm_sgbuf_ops_page,
1186 /* via686 capture callbacks */
1187 static snd_pcm_ops_t snd_via686_capture_ops = {
1188 .open = snd_via82xx_capture_open,
1189 .close = snd_via82xx_pcm_close,
1190 .ioctl = snd_pcm_lib_ioctl,
1191 .hw_params = snd_via82xx_hw_params,
1192 .hw_free = snd_via82xx_hw_free,
1193 .prepare = snd_via686_capture_prepare,
1194 .trigger = snd_via82xx_pcm_trigger,
1195 .pointer = snd_via686_pcm_pointer,
1196 .page = snd_pcm_sgbuf_ops_page,
1199 /* via823x DSX playback callbacks */
1200 static snd_pcm_ops_t snd_via8233_playback_ops = {
1201 .open = snd_via82xx_playback_open,
1202 .close = snd_via82xx_pcm_close,
1203 .ioctl = snd_pcm_lib_ioctl,
1204 .hw_params = snd_via82xx_hw_params,
1205 .hw_free = snd_via82xx_hw_free,
1206 .prepare = snd_via8233_playback_prepare,
1207 .trigger = snd_via82xx_pcm_trigger,
1208 .pointer = snd_via8233_pcm_pointer,
1209 .page = snd_pcm_sgbuf_ops_page,
1212 /* via823x multi-channel playback callbacks */
1213 static snd_pcm_ops_t snd_via8233_multi_ops = {
1214 .open = snd_via8233_multi_open,
1215 .close = snd_via82xx_pcm_close,
1216 .ioctl = snd_pcm_lib_ioctl,
1217 .hw_params = snd_via82xx_hw_params,
1218 .hw_free = snd_via82xx_hw_free,
1219 .prepare = snd_via8233_multi_prepare,
1220 .trigger = snd_via82xx_pcm_trigger,
1221 .pointer = snd_via8233_pcm_pointer,
1222 .page = snd_pcm_sgbuf_ops_page,
1225 /* via823x capture callbacks */
1226 static snd_pcm_ops_t snd_via8233_capture_ops = {
1227 .open = snd_via82xx_capture_open,
1228 .close = snd_via82xx_pcm_close,
1229 .ioctl = snd_pcm_lib_ioctl,
1230 .hw_params = snd_via82xx_hw_params,
1231 .hw_free = snd_via82xx_hw_free,
1232 .prepare = snd_via8233_capture_prepare,
1233 .trigger = snd_via82xx_pcm_trigger,
1234 .pointer = snd_via8233_pcm_pointer,
1235 .page = snd_pcm_sgbuf_ops_page,
1239 static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
1241 chip->devs[idx].reg_offset = reg_offset;
1242 chip->devs[idx].direction = direction;
1243 chip->devs[idx].port = chip->port + reg_offset;
1247 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1249 static int __devinit snd_via8233_pcm_new(via82xx_t *chip)
1254 chip->playback_devno = 0; /* x 4 */
1255 chip->multi_devno = 4; /* x 1 */
1256 chip->capture_devno = 5; /* x 2 */
1258 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1260 /* PCM #0: 4 DSX playbacks and 1 capture */
1261 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1264 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1265 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1266 pcm->private_data = chip;
1267 strcpy(pcm->name, chip->card->shortname);
1268 chip->pcms[0] = pcm;
1269 /* set up playbacks */
1270 for (i = 0; i < 4; i++)
1271 init_viadev(chip, i, 0x10 * i, 0);
1273 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1275 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1276 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1279 /* PCM #1: multi-channel playback and 2nd capture */
1280 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1283 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1284 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1285 pcm->private_data = chip;
1286 strcpy(pcm->name, chip->card->shortname);
1287 chip->pcms[1] = pcm;
1288 /* set up playback */
1289 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1290 /* set up capture */
1291 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 1);
1293 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1294 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1301 * create pcm instances for VIA8233A
1303 static int __devinit snd_via8233a_pcm_new(via82xx_t *chip)
1308 chip->multi_devno = 0;
1309 chip->playback_devno = 1;
1310 chip->capture_devno = 2;
1312 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1314 /* PCM #0: multi-channel playback and capture */
1315 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1318 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1319 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1320 pcm->private_data = chip;
1321 strcpy(pcm->name, chip->card->shortname);
1322 chip->pcms[0] = pcm;
1323 /* set up playback */
1324 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1326 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1328 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1329 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1332 /* SPDIF supported? */
1333 if (! ac97_can_spdif(chip->ac97))
1336 /* PCM #1: DXS3 playback (for spdif) */
1337 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1340 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1341 pcm->private_data = chip;
1342 strcpy(pcm->name, chip->card->shortname);
1343 chip->pcms[1] = pcm;
1344 /* set up playback */
1345 init_viadev(chip, chip->playback_devno, 0x30, 0);
1347 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1348 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1355 * create a pcm instance for via686a/b
1357 static int __devinit snd_via686_pcm_new(via82xx_t *chip)
1362 chip->playback_devno = 0;
1363 chip->capture_devno = 1;
1365 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1367 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1370 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1371 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1372 pcm->private_data = chip;
1373 strcpy(pcm->name, chip->card->shortname);
1374 chip->pcms[0] = pcm;
1375 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0);
1376 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 1);
1378 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1379 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1390 static int snd_via8233_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1392 /* formerly they were "Line" and "Mic", but it looks like that they
1393 * have nothing to do with the actual physical connections...
1395 static char *texts[2] = {
1398 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1400 uinfo->value.enumerated.items = 2;
1401 if (uinfo->value.enumerated.item >= 2)
1402 uinfo->value.enumerated.item = 1;
1403 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1407 static int snd_via8233_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1409 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1410 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1411 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1415 static int snd_via8233_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1417 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1418 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1421 spin_lock_irq(&chip->reg_lock);
1423 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1424 if (ucontrol->value.enumerated.item[0])
1425 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1428 spin_unlock_irq(&chip->reg_lock);
1432 static snd_kcontrol_new_t snd_via8233_capture_source __devinitdata = {
1433 .name = "Input Source Select",
1434 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1435 .info = snd_via8233_capture_source_info,
1436 .get = snd_via8233_capture_source_get,
1437 .put = snd_via8233_capture_source_put,
1440 static int snd_via8233_dxs3_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1442 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1444 uinfo->value.integer.min = 0;
1445 uinfo->value.integer.max = 1;
1449 static int snd_via8233_dxs3_spdif_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1451 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1454 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1455 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1459 static int snd_via8233_dxs3_spdif_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1461 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1464 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1465 val = oval & ~VIA8233_SPDIF_DX3;
1466 if (ucontrol->value.integer.value[0])
1467 val |= VIA8233_SPDIF_DX3;
1468 /* save the spdif flag for rate filtering */
1469 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1471 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1477 static snd_kcontrol_new_t snd_via8233_dxs3_spdif_control __devinitdata = {
1478 .name = "IEC958 Output Switch",
1479 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1480 .info = snd_via8233_dxs3_spdif_info,
1481 .get = snd_via8233_dxs3_spdif_get,
1482 .put = snd_via8233_dxs3_spdif_put,
1485 static int snd_via8233_dxs_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1489 uinfo->value.integer.min = 0;
1490 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1494 static int snd_via8233_dxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1496 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1497 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1498 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1499 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1503 static int snd_via8233_dxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1505 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1506 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1507 unsigned long port = chip->port + 0x10 * idx;
1511 for (i = 0; i < 2; i++) {
1512 val = ucontrol->value.integer.value[i];
1513 if (val > VIA_DXS_MAX_VOLUME)
1514 val = VIA_DXS_MAX_VOLUME;
1515 val = VIA_DXS_MAX_VOLUME - val;
1516 change |= val != chip->playback_volume[idx][i];
1518 chip->playback_volume[idx][i] = val;
1519 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1525 static snd_kcontrol_new_t snd_via8233_dxs_volume_control __devinitdata = {
1526 .name = "VIA DXS Playback Volume",
1527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1529 .info = snd_via8233_dxs_volume_info,
1530 .get = snd_via8233_dxs_volume_get,
1531 .put = snd_via8233_dxs_volume_put,
1537 static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
1539 via82xx_t *chip = bus->private_data;
1540 chip->ac97_bus = NULL;
1543 static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
1545 via82xx_t *chip = ac97->private_data;
1549 static struct ac97_quirk ac97_quirks[] = {
1550 { /* FIXME: which codec? */
1553 .name = "ASRock K7VT2",
1554 .type = AC97_TUNE_HP_ONLY
1559 .name = "ECS K7VTA3",
1560 .type = AC97_TUNE_HP_ONLY
1565 .name = "ECS L7VMM2",
1566 .type = AC97_TUNE_HP_ONLY
1571 .name = "ASRock K7VM2",
1572 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1578 .type = AC97_TUNE_ALC_JACK
1583 .name = "Mitac Mobo",
1584 .type = AC97_TUNE_ALC_JACK
1589 .name = "Arima Notebook",
1590 .type = AC97_TUNE_HP_ONLY,
1592 { } /* terminator */
1595 static int __devinit snd_via82xx_mixer_new(via82xx_t *chip, int ac97_quirk)
1597 ac97_template_t ac97;
1599 static ac97_bus_ops_t ops = {
1600 .write = snd_via82xx_codec_write,
1601 .read = snd_via82xx_codec_read,
1602 .wait = snd_via82xx_codec_wait,
1605 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1607 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1608 chip->ac97_bus->clock = chip->ac97_clock;
1610 memset(&ac97, 0, sizeof(ac97));
1611 ac97.private_data = chip;
1612 ac97.private_free = snd_via82xx_mixer_free_ac97;
1613 ac97.pci = chip->pci;
1614 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1617 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, ac97_quirk);
1619 if (chip->chip_type != TYPE_VIA686) {
1620 /* use slot 10/11 */
1621 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1631 static int snd_via8233_init_misc(via82xx_t *chip, int dev)
1636 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
1637 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
1638 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1639 for (i = 0; i < caps; i++) {
1640 snd_via8233_capture_source.index = i;
1641 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1645 if (ac97_can_spdif(chip->ac97)) {
1646 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1650 if (chip->chip_type != TYPE_VIA8233A) {
1651 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1656 /* select spdif data slot 10/11 */
1657 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1658 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1659 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
1660 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1665 static int snd_via686_init_misc(via82xx_t *chip, int dev)
1667 unsigned char legacy, legacy_cfg;
1670 legacy = chip->old_legacy;
1671 legacy_cfg = chip->old_legacy_cfg;
1672 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
1673 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
1674 legacy &= ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM); /* diable SB & FM */
1675 if (chip->revision >= VIA_REV_686_H) {
1677 if (mpu_port[dev] >= 0x200) { /* force MIDI */
1678 mpu_port[dev] &= 0xfffc;
1679 pci_write_config_dword(chip->pci, 0x18, mpu_port[dev] | 0x01);
1681 chip->mpu_port_saved = mpu_port[dev];
1684 mpu_port[dev] = pci_resource_start(chip->pci, 2);
1687 switch (mpu_port[dev]) { /* force MIDI */
1692 legacy_cfg &= ~(3 << 2);
1693 legacy_cfg |= (mpu_port[dev] & 0x0030) >> 2;
1695 default: /* no, use BIOS settings */
1696 if (legacy & VIA_FUNC_ENABLE_MIDI)
1697 mpu_port[dev] = 0x300 + ((legacy_cfg & 0x000c) << 2);
1701 if (mpu_port[dev] >= 0x200 &&
1702 (chip->mpu_res = request_region(mpu_port[dev], 2, "VIA82xx MPU401")) != NULL) {
1704 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
1705 legacy |= VIA_FUNC_ENABLE_MIDI;
1708 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
1709 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1713 #ifdef SUPPORT_JOYSTICK
1714 #define JOYSTICK_ADDR 0x200
1715 if (joystick[dev] &&
1716 (chip->res_joystick = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport")) != NULL) {
1717 legacy |= VIA_FUNC_ENABLE_GAME;
1718 chip->gameport.io = JOYSTICK_ADDR;
1722 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1723 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1724 if (chip->mpu_res) {
1725 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
1727 chip->irq, 0, &chip->rmidi) < 0) {
1728 printk(KERN_WARNING "unable to initialize MPU-401 at 0x%lx, skipping\n", mpu_port[dev]);
1729 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1731 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
1733 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1736 #ifdef SUPPORT_JOYSTICK
1737 if (chip->res_joystick)
1738 gameport_register_port(&chip->gameport);
1742 chip->legacy_saved = legacy;
1743 chip->legacy_cfg_saved = legacy_cfg;
1753 static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1755 via82xx_t *chip = entry->private_data;
1758 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1759 for (i = 0; i < 0xa0; i += 4) {
1760 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
1764 static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
1766 snd_info_entry_t *entry;
1768 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
1769 snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
1776 static int __devinit snd_via82xx_chip_init(via82xx_t *chip)
1783 memset(&ac97, 0, sizeof(ac97));
1784 ac97.private_data = chip;
1786 #if 0 /* broken on K7M? */
1787 if (chip->chip_type == TYPE_VIA686)
1788 /* disable all legacy ports */
1789 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
1791 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1792 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
1793 /* deassert ACLink reset, force SYNC */
1794 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1795 VIA_ACLINK_CTRL_ENABLE |
1796 VIA_ACLINK_CTRL_RESET |
1797 VIA_ACLINK_CTRL_SYNC);
1799 #if 1 /* FIXME: should we do full reset here for all chip models? */
1800 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
1803 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
1804 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1805 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
1808 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
1809 /* note - FM data out has trouble with non VRA codecs !! */
1810 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1814 /* Make sure VRA is enabled, in case we didn't do a
1815 * complete codec reset, above */
1816 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
1817 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
1818 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
1819 /* note - FM data out has trouble with non VRA codecs !! */
1820 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1824 /* wait until codec ready */
1825 max_count = ((3 * HZ) / 4) + 1;
1827 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1828 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
1830 set_current_state(TASK_UNINTERRUPTIBLE);
1831 schedule_timeout(1);
1832 } while (--max_count > 0);
1834 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
1835 snd_printk("AC'97 codec is not ready [0x%x]\n", val);
1837 /* and then reset codec.. */
1838 snd_via82xx_codec_ready(chip, 0);
1839 snd_via82xx_codec_write(&ac97, AC97_RESET, 0x0000);
1840 snd_via82xx_codec_read(&ac97, 0);
1842 #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
1843 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1844 VIA_REG_AC97_SECONDARY_VALID |
1845 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1846 max_count = ((3 * HZ) / 4) + 1;
1847 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1848 VIA_REG_AC97_SECONDARY_VALID |
1849 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1851 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
1852 chip->ac97_secondary = 1;
1855 set_current_state(TASK_INTERRUPTIBLE);
1856 schedule_timeout(1);
1857 } while (--max_count > 0);
1858 /* This is ok, the most of motherboards have only one codec */
1863 if (chip->chip_type == TYPE_VIA686) {
1864 /* route FM trap to IRQ, disable FM trap */
1865 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
1866 /* disable all GPI interrupts */
1867 outl(0, VIAREG(chip, GPI_INTR));
1870 if (chip->chip_type != TYPE_VIA686) {
1871 /* Workaround for Award BIOS bug:
1872 * DXS channels don't work properly with VRA if MC97 is disabled.
1874 struct pci_dev *pci;
1875 pci = pci_find_device(0x1106, 0x3068, NULL); /* MC97 */
1878 pci_read_config_byte(pci, 0x44, &data);
1879 pci_write_config_byte(pci, 0x44, data | 0x40);
1890 static int snd_via82xx_suspend(snd_card_t *card, unsigned int state)
1892 via82xx_t *chip = card->pm_private_data;
1895 for (i = 0; i < 2; i++)
1897 snd_pcm_suspend_all(chip->pcms[i]);
1898 for (i = 0; i < chip->num_devs; i++)
1899 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1900 synchronize_irq(chip->irq);
1901 snd_ac97_suspend(chip->ac97);
1903 /* save misc values */
1904 if (chip->chip_type != TYPE_VIA686) {
1905 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
1906 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
1907 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
1910 pci_set_power_state(chip->pci, 3);
1911 pci_disable_device(chip->pci);
1912 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1916 static int snd_via82xx_resume(snd_card_t *card, unsigned int state)
1918 via82xx_t *chip = card->pm_private_data;
1921 pci_enable_device(chip->pci);
1922 pci_set_power_state(chip->pci, 0);
1924 snd_via82xx_chip_init(chip);
1926 if (chip->chip_type == TYPE_VIA686) {
1927 if (chip->mpu_port_saved)
1928 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
1929 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
1930 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
1932 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
1933 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
1934 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
1935 for (idx = 0; idx < 4; idx++) {
1936 unsigned long port = chip->port + 0x10 * idx;
1937 for (i = 0; i < 2; i++)
1938 outb(chip->playback_volume[idx][i], port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1942 snd_ac97_resume(chip->ac97);
1944 for (i = 0; i < chip->num_devs; i++)
1945 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1947 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1950 #endif /* CONFIG_PM */
1952 static int snd_via82xx_free(via82xx_t *chip)
1958 /* disable interrupts */
1959 for (i = 0; i < chip->num_devs; i++)
1960 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1961 synchronize_irq(chip->irq);
1964 free_irq(chip->irq, (void *)chip);
1965 if (chip->mpu_res) {
1966 release_resource(chip->mpu_res);
1967 kfree_nocheck(chip->mpu_res);
1969 pci_release_regions(chip->pci);
1970 if (chip->chip_type == TYPE_VIA686) {
1971 #ifdef SUPPORT_JOYSTICK
1972 if (chip->res_joystick) {
1973 gameport_unregister_port(&chip->gameport);
1974 release_resource(chip->res_joystick);
1975 kfree_nocheck(chip->res_joystick);
1978 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
1979 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
1985 static int snd_via82xx_dev_free(snd_device_t *device)
1987 via82xx_t *chip = device->device_data;
1988 return snd_via82xx_free(chip);
1991 static int __devinit snd_via82xx_create(snd_card_t * card,
1992 struct pci_dev *pci,
1995 unsigned int ac97_clock,
2000 static snd_device_ops_t ops = {
2001 .dev_free = snd_via82xx_dev_free,
2004 if ((err = pci_enable_device(pci)) < 0)
2007 if ((chip = kcalloc(1, sizeof(*chip), GFP_KERNEL)) == NULL)
2010 chip->chip_type = chip_type;
2011 chip->revision = revision;
2013 spin_lock_init(&chip->reg_lock);
2014 spin_lock_init(&chip->ac97_lock);
2015 spin_lock_init(&chip->rates[0].lock);
2016 spin_lock_init(&chip->rates[1].lock);
2021 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2022 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2024 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2028 chip->port = pci_resource_start(pci, 0);
2029 if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
2030 card->driver, (void *)chip)) {
2031 snd_printk("unable to grab IRQ %d\n", pci->irq);
2032 snd_via82xx_free(chip);
2035 chip->irq = pci->irq;
2036 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2037 chip->ac97_clock = ac97_clock;
2038 synchronize_irq(chip->irq);
2040 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2041 snd_via82xx_free(chip);
2045 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2046 snd_via82xx_free(chip);
2050 /* The 8233 ac97 controller does not implement the master bit
2051 * in the pci command register. IMHO this is a violation of the PCI spec.
2052 * We call pci_set_master here because it does not hurt. */
2053 pci_set_master(pci);
2055 snd_card_set_dev(card, &pci->dev);
2061 struct via823x_info {
2066 static struct via823x_info via823x_cards[] __devinitdata = {
2067 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2068 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2069 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2070 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2071 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2075 * auto detection of DXS channel supports.
2077 struct dxs_whitelist {
2078 unsigned short vendor;
2079 unsigned short device;
2080 unsigned short mask;
2081 short action; /* new dxs_support value */
2084 static int __devinit check_dxs_list(struct pci_dev *pci)
2086 static struct dxs_whitelist whitelist[] = {
2087 { .vendor = 0x1005, .device = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
2088 { .vendor = 0x1019, .device = 0x0996, .action = VIA_DXS_48K },
2089 { .vendor = 0x1019, .device = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
2090 { .vendor = 0x1019, .device = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
2091 { .vendor = 0x1025, .device = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
2092 { .vendor = 0x1043, .device = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
2093 { .vendor = 0x1043, .device = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
2094 { .vendor = 0x1043, .device = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
2095 { .vendor = 0x1071, .device = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
2096 { .vendor = 0x10cf, .device = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
2097 { .vendor = 0x1106, .device = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
2098 { .vendor = 0x1106, .device = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
2099 { .vendor = 0x1297, .device = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
2100 { .vendor = 0x1297, .device = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
2101 { .vendor = 0x1458, .device = 0xa002, .action = VIA_DXS_NO_VRA }, /* Gigabyte GA-7VAXP (FIXME: or DXS_ENABLE?) */
2102 { .vendor = 0x147b, .device = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
2103 { .vendor = 0x14ff, .device = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
2104 { .vendor = 0x1462, .device = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
2105 { .vendor = 0x1462, .device = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
2106 { .vendor = 0x1462, .device = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
2107 { .vendor = 0x1584, .device = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
2108 { .vendor = 0x1584, .device = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
2109 { .vendor = 0x161f, .device = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
2110 { .vendor = 0x161f, .device = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
2111 { .vendor = 0x1631, .device = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
2112 { .vendor = 0x1695, .device = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
2113 { .vendor = 0x1849, .device = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
2114 { } /* terminator */
2116 struct dxs_whitelist *w;
2117 unsigned short subsystem_vendor;
2118 unsigned short subsystem_device;
2120 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
2121 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
2123 for (w = whitelist; w->vendor; w++) {
2124 if (w->vendor != subsystem_vendor)
2127 if ((w->mask & subsystem_device) == w->device)
2130 if (subsystem_device == w->device)
2136 * not detected, try 48k rate only to be sure.
2138 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
2139 printk(KERN_INFO " Please try dxs_support=1 or dxs_support=4 option\n");
2140 printk(KERN_INFO " and report if it works on your machine.\n");
2144 static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2145 const struct pci_device_id *pci_id)
2150 unsigned char revision;
2151 int chip_type = 0, card_type;
2155 if (dev >= SNDRV_CARDS)
2162 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2166 card_type = pci_id->driver_data;
2167 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
2168 switch (card_type) {
2169 case TYPE_CARD_VIA686:
2170 strcpy(card->driver, "VIA686A");
2171 sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
2172 chip_type = TYPE_VIA686;
2174 case TYPE_CARD_VIA8233:
2175 chip_type = TYPE_VIA8233;
2176 sprintf(card->shortname, "VIA 823x rev%x", revision);
2177 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2178 if (revision == via823x_cards[i].revision) {
2179 chip_type = via823x_cards[i].type;
2180 strcpy(card->shortname, via823x_cards[i].name);
2184 if (chip_type != TYPE_VIA8233A) {
2185 if (dxs_support[dev] == VIA_DXS_AUTO)
2186 dxs_support[dev] = check_dxs_list(pci);
2187 /* force to use VIA8233 or 8233A model according to
2188 * dxs_support module option
2190 if (dxs_support[dev] == VIA_DXS_DISABLE)
2191 chip_type = TYPE_VIA8233A;
2193 chip_type = TYPE_VIA8233;
2195 if (chip_type == TYPE_VIA8233A)
2196 strcpy(card->driver, "VIA8233A");
2198 strcpy(card->driver, "VIA8233");
2201 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2206 if ((err = snd_via82xx_create(card, pci, chip_type, revision, ac97_clock[dev], &chip)) < 0)
2208 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk[dev])) < 0)
2211 if (chip_type == TYPE_VIA686) {
2212 if ((err = snd_via686_pcm_new(chip)) < 0 ||
2213 (err = snd_via686_init_misc(chip, dev)) < 0)
2216 if (chip_type == TYPE_VIA8233A) {
2217 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2219 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2221 if ((err = snd_via8233_pcm_new(chip)) < 0)
2223 if (dxs_support[dev] == VIA_DXS_48K)
2224 chip->dxs_fixed = 1;
2225 else if (dxs_support[dev] == VIA_DXS_NO_VRA)
2228 if ((err = snd_via8233_init_misc(chip, dev)) < 0)
2232 snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
2234 /* disable interrupts */
2235 for (i = 0; i < chip->num_devs; i++)
2236 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2238 sprintf(card->longname, "%s at 0x%lx, irq %d",
2239 card->shortname, chip->port, chip->irq);
2241 snd_via82xx_proc_init(chip);
2243 if ((err = snd_card_register(card)) < 0) {
2244 snd_card_free(card);
2247 pci_set_drvdata(pci, card);
2252 snd_card_free(card);
2256 static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2258 snd_card_free(pci_get_drvdata(pci));
2259 pci_set_drvdata(pci, NULL);
2262 static struct pci_driver driver = {
2263 .name = "VIA 82xx Audio",
2264 .id_table = snd_via82xx_ids,
2265 .probe = snd_via82xx_probe,
2266 .remove = __devexit_p(snd_via82xx_remove),
2267 SND_PCI_PM_CALLBACKS
2270 static int __init alsa_card_via82xx_init(void)
2272 return pci_module_init(&driver);
2275 static void __exit alsa_card_via82xx_exit(void)
2277 pci_unregister_driver(&driver);
2280 module_init(alsa_card_via82xx_init)
2281 module_exit(alsa_card_via82xx_exit)