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 <sound/core.h>
55 #include <sound/pcm.h>
56 #include <sound/pcm_params.h>
57 #include <sound/info.h>
58 #include <sound/ac97_codec.h>
59 #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_CLASSES("{sound}");
71 MODULE_DEVICES("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
73 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
74 #define SUPPORT_JOYSTICK 1
77 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
78 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
79 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
80 static long mpu_port[SNDRV_CARDS];
81 #ifdef SUPPORT_JOYSTICK
82 static int joystick[SNDRV_CARDS];
84 static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 48000};
85 static int ac97_quirk[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = AC97_TUNE_DEFAULT};
86 static int dxs_support[SNDRV_CARDS];
88 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
89 MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
90 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
91 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
92 MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
93 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
94 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
95 MODULE_PARM_DESC(enable, "Enable audio part of VIA 82xx bridge.");
96 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
97 MODULE_PARM(mpu_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
98 MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
99 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT_DESC);
100 #ifdef SUPPORT_JOYSTICK
101 MODULE_PARM(joystick, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
102 MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
103 MODULE_PARM_SYNTAX(joystick, SNDRV_ENABLE_DESC "," SNDRV_BOOLEAN_FALSE_DESC);
105 MODULE_PARM(ac97_clock, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
106 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
107 MODULE_PARM_SYNTAX(ac97_clock, SNDRV_ENABLED ",default:48000");
108 MODULE_PARM(ac97_quirk, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
109 MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
110 MODULE_PARM_SYNTAX(ac97_quirk, SNDRV_ENABLED ",allows:{{-1,4}},dialog:list,default:-1");
111 MODULE_PARM(dxs_support, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
112 MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA)");
113 MODULE_PARM_SYNTAX(dxs_support, SNDRV_ENABLED ",allows:{{0,4}},dialog:list");
117 #ifndef PCI_DEVICE_ID_VIA_82C686_5
118 #define PCI_DEVICE_ID_VIA_82C686_5 0x3058
120 #ifndef PCI_DEVICE_ID_VIA_8233_5
121 #define PCI_DEVICE_ID_VIA_8233_5 0x3059
124 /* revision numbers for via686 */
125 #define VIA_REV_686_A 0x10
126 #define VIA_REV_686_B 0x11
127 #define VIA_REV_686_C 0x12
128 #define VIA_REV_686_D 0x13
129 #define VIA_REV_686_E 0x14
130 #define VIA_REV_686_H 0x20
132 /* revision numbers for via8233 */
133 #define VIA_REV_PRE_8233 0x10 /* not in market */
134 #define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
135 #define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
136 #define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
137 #define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
143 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
144 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
147 #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
148 #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
149 #define VIA_REG_STAT_PAUSED 0x40 /* RO */
150 #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
151 #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
152 #define VIA_REG_STAT_EOL 0x02 /* RWC */
153 #define VIA_REG_STAT_FLAG 0x01 /* RWC */
154 #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
155 #define VIA_REG_CTRL_START 0x80 /* WO */
156 #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
157 #define VIA_REG_CTRL_AUTOSTART 0x20
158 #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
159 #define VIA_REG_CTRL_INT_STOP 0x04
160 #define VIA_REG_CTRL_INT_EOL 0x02
161 #define VIA_REG_CTRL_INT_FLAG 0x01
162 #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
163 #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
164 #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
165 #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
166 #define VIA_REG_TYPE_16BIT 0x20 /* RW */
167 #define VIA_REG_TYPE_STEREO 0x10 /* RW */
168 #define VIA_REG_TYPE_INT_LLINE 0x00
169 #define VIA_REG_TYPE_INT_LSAMPLE 0x04
170 #define VIA_REG_TYPE_INT_LESSONE 0x08
171 #define VIA_REG_TYPE_INT_MASK 0x0c
172 #define VIA_REG_TYPE_INT_EOL 0x02
173 #define VIA_REG_TYPE_INT_FLAG 0x01
174 #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
175 #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
176 #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
177 #define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
178 #define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
179 #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
180 #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
182 #define DEFINE_VIA_REGSET(name,val) \
184 VIA_REG_##name##_STATUS = (val),\
185 VIA_REG_##name##_CONTROL = (val) + 0x01,\
186 VIA_REG_##name##_TYPE = (val) + 0x02,\
187 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
188 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
189 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
190 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
194 DEFINE_VIA_REGSET(PLAYBACK, 0x00);
195 DEFINE_VIA_REGSET(CAPTURE, 0x10);
196 DEFINE_VIA_REGSET(FM, 0x20);
199 #define VIA_REG_AC97 0x80 /* dword */
200 #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
201 #define VIA_REG_AC97_CODEC_ID_SHIFT 30
202 #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
203 #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
204 #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
205 #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
206 #define VIA_REG_AC97_BUSY (1<<24)
207 #define VIA_REG_AC97_READ (1<<23)
208 #define VIA_REG_AC97_CMD_SHIFT 16
209 #define VIA_REG_AC97_CMD_MASK 0x7e
210 #define VIA_REG_AC97_DATA_SHIFT 0
211 #define VIA_REG_AC97_DATA_MASK 0xffff
213 #define VIA_REG_SGD_SHADOW 0x84 /* dword */
215 #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
216 #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
217 #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
218 #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
219 #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
220 #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
221 #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
222 #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
223 #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
224 #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
225 #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
226 #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
228 #define VIA8233_REG_SGD_STAT_FLAG (1<<0)
229 #define VIA8233_REG_SGD_STAT_EOL (1<<1)
230 #define VIA8233_REG_SGD_STAT_STOP (1<<2)
231 #define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
232 #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
233 #define VIA8233_REG_SGD_CHAN_SDX 0
234 #define VIA8233_REG_SGD_CHAN_MULTI 4
235 #define VIA8233_REG_SGD_CHAN_REC 6
236 #define VIA8233_REG_SGD_CHAN_REC1 7
238 #define VIA_REG_GPI_STATUS 0x88
239 #define VIA_REG_GPI_INTR 0x8c
241 /* multi-channel and capture registers for via8233 */
242 DEFINE_VIA_REGSET(MULTPLAY, 0x40);
243 DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
245 /* via8233-specific registers */
246 #define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
247 #define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
248 #define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
249 #define VIA_REG_MULTPLAY_FMT_8BIT 0x00
250 #define VIA_REG_MULTPLAY_FMT_16BIT 0x80
251 #define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
252 #define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
253 #define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
255 #define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
257 #define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
258 #define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
259 #define VIA_REG_CAPTURE_CHANNEL_LINE 0
260 #define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
262 #define VIA_TBL_BIT_FLAG 0x40000000
263 #define VIA_TBL_BIT_EOL 0x80000000
266 #define VIA_ACLINK_STAT 0x40
267 #define VIA_ACLINK_C11_READY 0x20
268 #define VIA_ACLINK_C10_READY 0x10
269 #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
270 #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
271 #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
272 #define VIA_ACLINK_CTRL 0x41
273 #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
274 #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
275 #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
276 #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
277 #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
278 #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
279 #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
280 #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
281 #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
282 VIA_ACLINK_CTRL_RESET|\
283 VIA_ACLINK_CTRL_PCM|\
285 #define VIA_FUNC_ENABLE 0x42
286 #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
287 #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
288 #define VIA_FUNC_RX2C_WRITE 0x20
289 #define VIA_FUNC_SB_FIFO_EMPTY 0x10
290 #define VIA_FUNC_ENABLE_GAME 0x08
291 #define VIA_FUNC_ENABLE_FM 0x04
292 #define VIA_FUNC_ENABLE_MIDI 0x02
293 #define VIA_FUNC_ENABLE_SB 0x01
294 #define VIA_PNP_CONTROL 0x43
295 #define VIA_FM_NMI_CTRL 0x48
296 #define VIA8233_VOLCHG_CTRL 0x48
297 #define VIA8233_SPDIF_CTRL 0x49
298 #define VIA8233_SPDIF_DX3 0x08
299 #define VIA8233_SPDIF_SLOT_MASK 0x03
300 #define VIA8233_SPDIF_SLOT_1011 0x00
301 #define VIA8233_SPDIF_SLOT_34 0x01
302 #define VIA8233_SPDIF_SLOT_78 0x02
303 #define VIA8233_SPDIF_SLOT_69 0x03
308 #define VIA_DXS_AUTO 0
309 #define VIA_DXS_ENABLE 1
310 #define VIA_DXS_DISABLE 2
311 #define VIA_DXS_48K 3
312 #define VIA_DXS_NO_VRA 4
318 typedef struct _snd_via82xx via82xx_t;
319 typedef struct via_dev viadev_t;
320 #define chip_t via82xx_t
326 struct snd_via_sg_table {
331 #define VIA_TABLE_SIZE 255
334 unsigned int reg_offset;
336 int direction; /* playback = 0, capture = 1 */
337 snd_pcm_substream_t *substream;
339 unsigned int tbl_entries; /* # descriptors */
340 struct snd_dma_buffer table;
341 struct snd_via_sg_table *idx_table;
342 /* for recovery from the unexpected pointer */
343 unsigned int lastpos;
344 unsigned int bufsize;
345 unsigned int bufsize2;
349 enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
350 enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
352 #define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
354 struct via_rate_lock {
360 struct _snd_via82xx {
364 struct resource *res_port;
365 struct resource *mpu_res;
367 unsigned char revision;
369 unsigned char old_legacy;
370 unsigned char old_legacy_cfg;
372 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
374 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
379 unsigned int num_devs;
380 unsigned int playback_devno, multi_devno, capture_devno;
381 viadev_t devs[VIA_MAX_DEVS];
382 struct via_rate_lock rates[2]; /* playback and capture */
383 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
384 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
385 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
387 snd_rawmidi_t *rmidi;
389 ac97_bus_t *ac97_bus;
391 unsigned int ac97_clock;
392 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
395 spinlock_t ac97_lock;
396 snd_info_entry_t *proc_entry;
398 struct snd_dma_device dma_dev;
400 #ifdef SUPPORT_JOYSTICK
401 struct gameport gameport;
402 struct resource *res_joystick;
406 static struct pci_device_id snd_via82xx_ids[] = {
407 { 0x1106, 0x3058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
408 { 0x1106, 0x3059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
412 MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
418 * allocate and initialize the descriptor buffers
419 * periods = number of periods
420 * fragsize = period size in bytes
422 static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
424 unsigned int periods, unsigned int fragsize)
426 unsigned int i, idx, ofs, rest;
427 via82xx_t *chip = snd_pcm_substream_chip(substream);
428 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
430 if (dev->table.area == NULL) {
431 /* the start of each lists must be aligned to 8 bytes,
432 * but the kernel pages are much bigger, so we don't care
434 if (snd_dma_alloc_pages(&chip->dma_dev, PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8), &dev->table) < 0)
437 if (! dev->idx_table) {
438 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
439 if (! dev->idx_table)
443 /* fill the entries */
446 for (i = 0; i < periods; i++) {
448 /* fill descriptors for a period.
449 * a period can be split to several descriptors if it's
450 * over page boundary.
456 if (idx >= VIA_TABLE_SIZE) {
457 snd_printk(KERN_ERR "via82xx: too much table size!\n");
460 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
461 r = PAGE_SIZE - (ofs % PAGE_SIZE);
466 if (i == periods - 1)
467 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
469 flag = VIA_TBL_BIT_FLAG; /* period boundary */
471 flag = 0; /* period continues to the next */
472 // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
473 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
474 dev->idx_table[idx].offset = ofs;
475 dev->idx_table[idx].size = r;
480 dev->tbl_entries = idx;
481 dev->bufsize = periods * fragsize;
482 dev->bufsize2 = dev->bufsize / 2;
487 static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
490 via82xx_t *chip = snd_pcm_substream_chip(substream);
491 if (dev->table.area) {
492 snd_dma_free_pages(&chip->dma_dev, &dev->table);
493 dev->table.area = NULL;
495 if (dev->idx_table) {
496 kfree(dev->idx_table);
497 dev->idx_table = NULL;
506 static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
508 return inl(VIAREG(chip, AC97));
511 static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
513 outl(val, VIAREG(chip, AC97));
516 static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
518 unsigned int timeout = 1000; /* 1ms */
521 while (timeout-- > 0) {
523 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
526 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
530 static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
532 unsigned int timeout = 1000; /* 1ms */
533 unsigned int val, val1;
534 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
535 VIA_REG_AC97_SECONDARY_VALID;
537 while (timeout-- > 0) {
538 val = snd_via82xx_codec_xread(chip);
539 val1 = val & (VIA_REG_AC97_BUSY | stat);
547 static void snd_via82xx_codec_wait(ac97_t *ac97)
549 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return);
551 err = snd_via82xx_codec_ready(chip, ac97->num);
552 /* here we need to wait fairly for long time.. */
553 set_current_state(TASK_UNINTERRUPTIBLE);
554 schedule_timeout(HZ/2);
557 static void snd_via82xx_codec_write(ac97_t *ac97,
561 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return);
564 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
565 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
566 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
567 xval |= val << VIA_REG_AC97_DATA_SHIFT;
568 spin_lock(&chip->ac97_lock);
569 snd_via82xx_codec_xwrite(chip, xval);
570 snd_via82xx_codec_ready(chip, ac97->num);
571 spin_unlock(&chip->ac97_lock);
574 static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
576 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return ~0);
577 unsigned int xval, val = 0xffff;
580 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
581 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
582 xval |= VIA_REG_AC97_READ;
583 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
584 spin_lock(&chip->ac97_lock);
587 spin_unlock(&chip->ac97_lock);
588 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
591 snd_via82xx_codec_xwrite(chip, xval);
593 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
595 val = snd_via82xx_codec_xread(chip);
599 spin_unlock(&chip->ac97_lock);
603 static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
605 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
606 VIADEV_REG(viadev, OFFSET_CONTROL));
607 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
609 /* disable interrupts */
610 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
611 /* clear interrupts */
612 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
613 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
614 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
623 static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
625 via82xx_t *chip = snd_magic_cast(via82xx_t, dev_id, return IRQ_NONE);
629 spin_lock(&chip->reg_lock);
631 /* FIXME: does it work on via823x? */
632 if (chip->chip_type != TYPE_VIA686)
635 status = inl(VIAREG(chip, SGD_SHADOW));
636 if (! (status & chip->intr_mask)) {
637 spin_unlock(&chip->reg_lock);
639 /* check mpu401 interrupt */
640 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
645 /* check status for each stream */
646 for (i = 0; i < chip->num_devs; i++) {
647 viadev_t *viadev = &chip->devs[i];
648 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
649 c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
652 if (viadev->substream && viadev->running) {
653 spin_unlock(&chip->reg_lock);
654 snd_pcm_period_elapsed(viadev->substream);
655 spin_lock(&chip->reg_lock);
657 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
659 spin_unlock(&chip->reg_lock);
670 static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
672 via82xx_t *chip = snd_pcm_substream_chip(substream);
673 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
676 if (chip->chip_type != TYPE_VIA686)
677 val = VIA_REG_CTRL_INT;
681 case SNDRV_PCM_TRIGGER_START:
682 val |= VIA_REG_CTRL_START;
685 case SNDRV_PCM_TRIGGER_STOP:
686 val = VIA_REG_CTRL_TERMINATE;
689 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
690 val |= VIA_REG_CTRL_PAUSE;
693 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
699 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
700 if (cmd == SNDRV_PCM_TRIGGER_STOP)
701 snd_via82xx_channel_reset(chip, viadev);
711 * calculate the linear position at the given sg-buffer index and the rest count
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 */
721 if (size < count || (res < viadev->lastpos && (res >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))) {
723 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);
725 /* count register returns full size when end of buffer is reached */
727 snd_printd(KERN_ERR "invalid via82xx_cur_ptr, using last valid pointer\n");
728 res = viadev->lastpos;
730 res = viadev->idx_table[idx].offset + size;
731 if (res < viadev->lastpos && (res >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2)) {
732 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
733 res = viadev->lastpos;
737 viadev->lastpos = res; /* remember the last position */
738 if (res >= viadev->bufsize)
739 res -= viadev->bufsize;
744 * get the current pointer on via686
746 static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
748 via82xx_t *chip = snd_pcm_substream_chip(substream);
749 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
750 unsigned int idx, ptr, count, res;
752 snd_assert(viadev->tbl_entries, return 0);
753 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
756 spin_lock(&chip->reg_lock);
757 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
758 /* The via686a does not have the current index register,
759 * so we need to calculate the index from CURR_PTR.
761 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
762 if (ptr <= (unsigned int)viadev->table.addr)
764 else /* CURR_PTR holds the address + 8 */
765 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
766 res = calc_linear_pos(viadev, idx, count);
767 spin_unlock(&chip->reg_lock);
769 return bytes_to_frames(substream->runtime, res);
773 * get the current pointer on via823x
775 static snd_pcm_uframes_t snd_via8233_pcm_pointer(snd_pcm_substream_t *substream)
777 via82xx_t *chip = snd_pcm_substream_chip(substream);
778 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
779 unsigned int idx, count, res;
781 snd_assert(viadev->tbl_entries, return 0);
782 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
784 spin_lock(&chip->reg_lock);
785 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
787 if (idx >= viadev->tbl_entries) {
789 printk("fail: invalid idx = %i/%i\n", idx, viadev->tbl_entries);
791 res = viadev->lastpos;
794 res = calc_linear_pos(viadev, idx, count);
796 spin_unlock(&chip->reg_lock);
798 return bytes_to_frames(substream->runtime, res);
803 * hw_params callback:
804 * allocate the buffer and build up the buffer description table
806 static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
807 snd_pcm_hw_params_t * hw_params)
809 via82xx_t *chip = snd_pcm_substream_chip(substream);
810 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
813 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
816 err = build_via_table(viadev, substream, chip->pci,
817 params_periods(hw_params),
818 params_period_bytes(hw_params));
827 * clean up the buffer description table and release the buffer
829 static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
831 via82xx_t *chip = snd_pcm_substream_chip(substream);
832 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
834 clean_via_table(viadev, substream, chip->pci);
835 snd_pcm_lib_free_pages(substream);
841 * set up the table pointer
843 static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
845 snd_via82xx_codec_ready(chip, 0);
846 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
848 snd_via82xx_codec_ready(chip, 0);
852 * prepare callback for playback and capture on via686
854 static void via686_setup_format(via82xx_t *chip, viadev_t *viadev, snd_pcm_runtime_t *runtime)
856 snd_via82xx_channel_reset(chip, viadev);
857 /* this must be set after channel_reset */
858 snd_via82xx_set_table_ptr(chip, viadev);
859 outb(VIA_REG_TYPE_AUTOSTART |
860 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
861 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
862 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
863 VIA_REG_TYPE_INT_EOL |
864 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
867 static int snd_via686_playback_prepare(snd_pcm_substream_t *substream)
869 via82xx_t *chip = snd_pcm_substream_chip(substream);
870 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
871 snd_pcm_runtime_t *runtime = substream->runtime;
873 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
874 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
875 via686_setup_format(chip, viadev, runtime);
879 static int snd_via686_capture_prepare(snd_pcm_substream_t *substream)
881 via82xx_t *chip = snd_pcm_substream_chip(substream);
882 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
883 snd_pcm_runtime_t *runtime = substream->runtime;
885 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
886 via686_setup_format(chip, viadev, runtime);
891 * lock the current rate
893 static int via_lock_rate(struct via_rate_lock *rec, int rate)
897 spin_lock(&rec->lock);
898 if (rec->rate != rate) {
899 if (rec->rate && rec->used > 1) /* already set */
906 spin_unlock(&rec->lock);
911 * prepare callback for DSX playback on via823x
913 static int snd_via8233_playback_prepare(snd_pcm_substream_t *substream)
915 via82xx_t *chip = snd_pcm_substream_chip(substream);
916 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
917 snd_pcm_runtime_t *runtime = substream->runtime;
921 if ((rate_changed = via_lock_rate(&chip->rates[0], runtime->rate)) < 0)
924 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
925 chip->no_vra ? 48000 : runtime->rate);
926 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
928 if (runtime->rate == 48000)
931 rbits = (0x100000 / 48000) * runtime->rate + ((0x100000 % 48000) * runtime->rate) / 48000;
932 snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
933 snd_via82xx_channel_reset(chip, viadev);
934 snd_via82xx_set_table_ptr(chip, viadev);
935 outb(chip->playback_volume[viadev->reg_offset / 0x10][0], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
936 outb(chip->playback_volume[viadev->reg_offset / 0x10][1], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
937 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
938 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
940 0xff000000, /* STOP index is never reached */
941 VIADEV_REG(viadev, OFFSET_STOP_IDX));
943 snd_via82xx_codec_ready(chip, 0);
948 * prepare callback for multi-channel playback on via823x
950 static int snd_via8233_multi_prepare(snd_pcm_substream_t *substream)
952 via82xx_t *chip = snd_pcm_substream_chip(substream);
953 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
954 snd_pcm_runtime_t *runtime = substream->runtime;
958 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
960 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
961 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
962 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
963 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
964 snd_via82xx_channel_reset(chip, viadev);
965 snd_via82xx_set_table_ptr(chip, viadev);
967 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ? VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
968 fmt |= runtime->channels << 4;
969 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
971 if (chip->revision == VIA_REV_8233A)
976 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
977 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
978 switch (runtime->channels) {
979 case 1: slots = (1<<0) | (1<<4); break;
980 case 2: slots = (1<<0) | (2<<4); break;
981 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
982 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
983 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
984 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
985 default: slots = 0; break;
988 /* STOP index is never reached */
989 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
991 snd_via82xx_codec_ready(chip, 0);
996 * prepare callback for capture on via823x
998 static int snd_via8233_capture_prepare(snd_pcm_substream_t *substream)
1000 via82xx_t *chip = snd_pcm_substream_chip(substream);
1001 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1002 snd_pcm_runtime_t *runtime = substream->runtime;
1004 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1006 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1007 snd_via82xx_channel_reset(chip, viadev);
1008 snd_via82xx_set_table_ptr(chip, viadev);
1009 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1010 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1011 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1012 0xff000000, /* STOP index is never reached */
1013 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1015 snd_via82xx_codec_ready(chip, 0);
1021 * pcm hardware definition, identical for both playback and capture
1023 static snd_pcm_hardware_t snd_via82xx_hw =
1025 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1026 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1027 SNDRV_PCM_INFO_MMAP_VALID |
1028 SNDRV_PCM_INFO_PAUSE),
1029 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1030 .rates = SNDRV_PCM_RATE_48000,
1035 .buffer_bytes_max = 128 * 1024,
1036 .period_bytes_min = 32,
1037 .period_bytes_max = 128 * 1024,
1039 .periods_max = VIA_TABLE_SIZE / 2,
1045 * open callback skeleton
1047 static int snd_via82xx_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
1049 snd_pcm_runtime_t *runtime = substream->runtime;
1051 unsigned long flags;
1052 struct via_rate_lock *ratep;
1057 {8000, SNDRV_PCM_RATE_8000},
1058 {11025, SNDRV_PCM_RATE_11025},
1059 {16000, SNDRV_PCM_RATE_16000},
1060 {22050, SNDRV_PCM_RATE_22050},
1061 {32000, SNDRV_PCM_RATE_32000},
1062 {44100, SNDRV_PCM_RATE_44100},
1063 {48000, SNDRV_PCM_RATE_48000},
1067 runtime->hw = snd_via82xx_hw;
1069 /* set the hw rate condition */
1070 ratep = &chip->rates[viadev->direction];
1071 spin_lock_irqsave(&ratep->lock, flags);
1073 if (chip->spdif_on) {
1074 runtime->hw.rates = SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000;
1075 runtime->hw.rate_min = 32000;
1076 runtime->hw.rate_max = 48000;
1077 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1078 /* fixed DXS playback rate */
1079 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1080 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1081 } else if (! ratep->rate) {
1082 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1083 runtime->hw.rates = chip->ac97->rates[idx];
1084 for (i = 0; i < (int)ARRAY_SIZE(ratebits); i++) {
1085 if (runtime->hw.rates & ratebits[i].bit) {
1086 runtime->hw.rate_min = ratebits[i].rate;
1090 for (i = ARRAY_SIZE(ratebits) - 1; i >= 0; i--) {
1091 if (runtime->hw.rates & ratebits[i].bit) {
1092 runtime->hw.rate_max = ratebits[i].rate;
1098 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1099 for (i = 0; i < (int)ARRAY_SIZE(ratebits); i++) {
1100 if (ratep->rate == ratebits[i].rate) {
1101 runtime->hw.rates = ratebits[i].bit;
1105 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1107 spin_unlock_irqrestore(&ratep->lock, flags);
1109 /* we may remove following constaint when we modify table entries
1111 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1114 runtime->private_data = viadev;
1115 viadev->substream = substream;
1122 * open callback for playback on via686 and via823x DSX
1124 static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
1126 via82xx_t *chip = snd_pcm_substream_chip(substream);
1127 viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
1130 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1136 * open callback for playback on via823x multi-channel
1138 static int snd_via8233_multi_open(snd_pcm_substream_t * substream)
1140 via82xx_t *chip = snd_pcm_substream_chip(substream);
1141 viadev_t *viadev = &chip->devs[chip->multi_devno];
1143 /* channels constraint for VIA8233A
1144 * 3 and 5 channels are not supported
1146 static unsigned int channels[] = {
1149 static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
1150 .count = ARRAY_SIZE(channels),
1155 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1157 substream->runtime->hw.channels_max = 6;
1158 if (chip->revision == VIA_REV_8233A)
1159 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
1164 * open callback for capture on via686 and via823x
1166 static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
1168 via82xx_t *chip = snd_pcm_substream_chip(substream);
1169 viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1171 return snd_via82xx_pcm_open(chip, viadev, substream);
1177 static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
1179 via82xx_t *chip = snd_pcm_substream_chip(substream);
1180 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1181 unsigned long flags;
1182 struct via_rate_lock *ratep;
1184 /* release the rate lock */
1185 ratep = &chip->rates[viadev->direction];
1186 spin_lock_irqsave(&ratep->lock, flags);
1190 spin_unlock_irqrestore(&ratep->lock, flags);
1192 viadev->substream = NULL;
1197 /* via686 playback callbacks */
1198 static snd_pcm_ops_t snd_via686_playback_ops = {
1199 .open = snd_via82xx_playback_open,
1200 .close = snd_via82xx_pcm_close,
1201 .ioctl = snd_pcm_lib_ioctl,
1202 .hw_params = snd_via82xx_hw_params,
1203 .hw_free = snd_via82xx_hw_free,
1204 .prepare = snd_via686_playback_prepare,
1205 .trigger = snd_via82xx_pcm_trigger,
1206 .pointer = snd_via686_pcm_pointer,
1207 .page = snd_pcm_sgbuf_ops_page,
1210 /* via686 capture callbacks */
1211 static snd_pcm_ops_t snd_via686_capture_ops = {
1212 .open = snd_via82xx_capture_open,
1213 .close = snd_via82xx_pcm_close,
1214 .ioctl = snd_pcm_lib_ioctl,
1215 .hw_params = snd_via82xx_hw_params,
1216 .hw_free = snd_via82xx_hw_free,
1217 .prepare = snd_via686_capture_prepare,
1218 .trigger = snd_via82xx_pcm_trigger,
1219 .pointer = snd_via686_pcm_pointer,
1220 .page = snd_pcm_sgbuf_ops_page,
1223 /* via823x DSX playback callbacks */
1224 static snd_pcm_ops_t snd_via8233_playback_ops = {
1225 .open = snd_via82xx_playback_open,
1226 .close = snd_via82xx_pcm_close,
1227 .ioctl = snd_pcm_lib_ioctl,
1228 .hw_params = snd_via82xx_hw_params,
1229 .hw_free = snd_via82xx_hw_free,
1230 .prepare = snd_via8233_playback_prepare,
1231 .trigger = snd_via82xx_pcm_trigger,
1232 .pointer = snd_via8233_pcm_pointer,
1233 .page = snd_pcm_sgbuf_ops_page,
1236 /* via823x multi-channel playback callbacks */
1237 static snd_pcm_ops_t snd_via8233_multi_ops = {
1238 .open = snd_via8233_multi_open,
1239 .close = snd_via82xx_pcm_close,
1240 .ioctl = snd_pcm_lib_ioctl,
1241 .hw_params = snd_via82xx_hw_params,
1242 .hw_free = snd_via82xx_hw_free,
1243 .prepare = snd_via8233_multi_prepare,
1244 .trigger = snd_via82xx_pcm_trigger,
1245 .pointer = snd_via8233_pcm_pointer,
1246 .page = snd_pcm_sgbuf_ops_page,
1249 /* via823x capture callbacks */
1250 static snd_pcm_ops_t snd_via8233_capture_ops = {
1251 .open = snd_via82xx_capture_open,
1252 .close = snd_via82xx_pcm_close,
1253 .ioctl = snd_pcm_lib_ioctl,
1254 .hw_params = snd_via82xx_hw_params,
1255 .hw_free = snd_via82xx_hw_free,
1256 .prepare = snd_via8233_capture_prepare,
1257 .trigger = snd_via82xx_pcm_trigger,
1258 .pointer = snd_via8233_pcm_pointer,
1259 .page = snd_pcm_sgbuf_ops_page,
1263 static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
1265 chip->devs[idx].reg_offset = reg_offset;
1266 chip->devs[idx].direction = direction;
1267 chip->devs[idx].port = chip->port + reg_offset;
1271 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1273 static int __devinit snd_via8233_pcm_new(via82xx_t *chip)
1278 chip->playback_devno = 0; /* x 4 */
1279 chip->multi_devno = 4; /* x 1 */
1280 chip->capture_devno = 5; /* x 2 */
1282 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1284 /* PCM #0: 4 DSX playbacks and 1 capture */
1285 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1288 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1289 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1290 pcm->private_data = chip;
1291 strcpy(pcm->name, chip->card->shortname);
1292 /* set up playbacks */
1293 for (i = 0; i < 4; i++)
1294 init_viadev(chip, i, 0x10 * i, 0);
1296 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1298 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1299 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1302 /* PCM #1: multi-channel playback and 2nd capture */
1303 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1306 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1307 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1308 pcm->private_data = chip;
1309 strcpy(pcm->name, chip->card->shortname);
1310 /* set up playback */
1311 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1312 /* set up capture */
1313 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 1);
1315 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1316 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1323 * create pcm instances for VIA8233A
1325 static int __devinit snd_via8233a_pcm_new(via82xx_t *chip)
1330 chip->multi_devno = 0;
1331 chip->playback_devno = 1;
1332 chip->capture_devno = 2;
1334 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1336 /* PCM #0: multi-channel playback and capture */
1337 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1340 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1341 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1342 pcm->private_data = chip;
1343 strcpy(pcm->name, chip->card->shortname);
1344 /* set up playback */
1345 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1347 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1349 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1350 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1353 /* PCM #1: DXS3 playback (for spdif) */
1354 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1357 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1358 pcm->private_data = chip;
1359 strcpy(pcm->name, chip->card->shortname);
1360 /* set up playback */
1361 init_viadev(chip, chip->playback_devno, 0x30, 0);
1363 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1364 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1371 * create a pcm instance for via686a/b
1373 static int __devinit snd_via686_pcm_new(via82xx_t *chip)
1378 chip->playback_devno = 0;
1379 chip->capture_devno = 1;
1381 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1383 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1386 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1387 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1388 pcm->private_data = chip;
1389 strcpy(pcm->name, chip->card->shortname);
1390 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0);
1391 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 1);
1393 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1394 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1405 static int snd_via8233_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1407 static char *texts[2] = {
1410 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1412 uinfo->value.enumerated.items = 2;
1413 if (uinfo->value.enumerated.item >= 2)
1414 uinfo->value.enumerated.item = 1;
1415 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1419 static int snd_via8233_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
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 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1427 static int snd_via8233_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1429 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1430 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1431 unsigned long flags;
1434 spin_lock_irqsave(&chip->reg_lock, flags);
1436 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1437 if (ucontrol->value.enumerated.item[0])
1438 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1441 spin_unlock_irqrestore(&chip->reg_lock, flags);
1445 static snd_kcontrol_new_t snd_via8233_capture_source __devinitdata = {
1446 .name = "Input Source Select",
1447 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1448 .info = snd_via8233_capture_source_info,
1449 .get = snd_via8233_capture_source_get,
1450 .put = snd_via8233_capture_source_put,
1453 static int snd_via8233_dxs3_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1455 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1457 uinfo->value.integer.min = 0;
1458 uinfo->value.integer.max = 1;
1462 static int snd_via8233_dxs3_spdif_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1464 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1467 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1468 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1472 static int snd_via8233_dxs3_spdif_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1474 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1477 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1478 val = oval & ~VIA8233_SPDIF_DX3;
1479 if (ucontrol->value.integer.value[0])
1480 val |= VIA8233_SPDIF_DX3;
1481 /* save the spdif flag for rate filtering */
1482 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1484 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1490 static snd_kcontrol_new_t snd_via8233_dxs3_spdif_control __devinitdata = {
1491 .name = "IEC958 Output Switch",
1492 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1493 .info = snd_via8233_dxs3_spdif_info,
1494 .get = snd_via8233_dxs3_spdif_get,
1495 .put = snd_via8233_dxs3_spdif_put,
1498 static int snd_via8233_dxs_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1500 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1502 uinfo->value.integer.min = 0;
1503 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1507 static int snd_via8233_dxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1509 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1510 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1511 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1512 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1516 static int snd_via8233_dxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1518 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1519 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1520 unsigned long port = chip->port + 0x10 * idx;
1524 for (i = 0; i < 2; i++) {
1525 val = ucontrol->value.integer.value[i];
1526 if (val > VIA_DXS_MAX_VOLUME)
1527 val = VIA_DXS_MAX_VOLUME;
1528 val = VIA_DXS_MAX_VOLUME - val;
1529 change |= val != chip->playback_volume[idx][i];
1531 chip->playback_volume[idx][i] = val;
1532 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1538 static snd_kcontrol_new_t snd_via8233_dxs_volume_control __devinitdata = {
1539 .name = "VIA DXS Playback Volume",
1540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1542 .info = snd_via8233_dxs_volume_info,
1543 .get = snd_via8233_dxs_volume_get,
1544 .put = snd_via8233_dxs_volume_put,
1550 static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
1552 via82xx_t *chip = snd_magic_cast(via82xx_t, bus->private_data, return);
1553 chip->ac97_bus = NULL;
1556 static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
1558 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return);
1562 static struct ac97_quirk ac97_quirks[] = {
1563 { /* FIXME: which codec? */
1566 .name = "ASRock K7VT2",
1567 .type = AC97_TUNE_HP_ONLY
1572 .name = "ASRock K7VM2",
1573 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1579 .type = AC97_TUNE_ALC_JACK
1584 .name = "Mitac Mobo",
1585 .type = AC97_TUNE_ALC_JACK
1587 { } /* terminator */
1590 static int __devinit snd_via82xx_mixer_new(via82xx_t *chip, int ac97_quirk)
1596 memset(&bus, 0, sizeof(bus));
1597 bus.write = snd_via82xx_codec_write;
1598 bus.read = snd_via82xx_codec_read;
1599 bus.wait = snd_via82xx_codec_wait;
1600 bus.private_data = chip;
1601 bus.private_free = snd_via82xx_mixer_free_ac97_bus;
1602 bus.clock = chip->ac97_clock;
1603 if ((err = snd_ac97_bus(chip->card, &bus, &chip->ac97_bus)) < 0)
1606 memset(&ac97, 0, sizeof(ac97));
1607 ac97.private_data = chip;
1608 ac97.private_free = snd_via82xx_mixer_free_ac97;
1609 ac97.pci = chip->pci;
1610 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1613 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, ac97_quirk);
1615 if (chip->chip_type != TYPE_VIA686) {
1616 /* use slot 10/11 */
1617 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1627 static int snd_via8233_init_misc(via82xx_t *chip, int dev)
1632 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1633 for (i = 0; i < caps; i++) {
1634 snd_via8233_capture_source.index = i;
1635 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1639 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1642 if (chip->chip_type != TYPE_VIA8233A) {
1643 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1648 /* select spdif data slot 10/11 */
1649 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1650 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1651 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1656 static int snd_via686_init_misc(via82xx_t *chip, int dev)
1658 unsigned char legacy, legacy_cfg;
1661 legacy = chip->old_legacy;
1662 legacy_cfg = chip->old_legacy_cfg;
1663 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
1664 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
1665 if (chip->revision >= VIA_REV_686_H) {
1667 if (mpu_port[dev] >= 0x200) { /* force MIDI */
1668 mpu_port[dev] &= 0xfffc;
1669 pci_write_config_dword(chip->pci, 0x18, mpu_port[dev] | 0x01);
1671 mpu_port[dev] = pci_resource_start(chip->pci, 2);
1674 switch (mpu_port[dev]) { /* force MIDI */
1679 legacy_cfg &= ~(3 << 2);
1680 legacy_cfg |= (mpu_port[dev] & 0x0030) >> 2;
1682 default: /* no, use BIOS settings */
1683 if (legacy & VIA_FUNC_ENABLE_MIDI)
1684 mpu_port[dev] = 0x300 + ((legacy_cfg & 0x000c) << 2);
1688 if (mpu_port[dev] >= 0x200 &&
1689 (chip->mpu_res = request_region(mpu_port[dev], 2, "VIA82xx MPU401")) != NULL) {
1691 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
1692 legacy |= VIA_FUNC_ENABLE_MIDI;
1695 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
1696 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1700 #ifdef SUPPORT_JOYSTICK
1701 #define JOYSTICK_ADDR 0x200
1702 if (joystick[dev] &&
1703 (chip->res_joystick = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport")) != NULL) {
1704 legacy |= VIA_FUNC_ENABLE_GAME;
1705 chip->gameport.io = JOYSTICK_ADDR;
1709 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1710 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1711 if (chip->mpu_res) {
1712 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
1714 chip->irq, 0, &chip->rmidi) < 0) {
1715 printk(KERN_WARNING "unable to initialize MPU-401 at 0x%lx, skipping\n", mpu_port[dev]);
1716 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1718 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
1720 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1723 #ifdef SUPPORT_JOYSTICK
1724 if (chip->res_joystick)
1725 gameport_register_port(&chip->gameport);
1735 static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1737 via82xx_t *chip = snd_magic_cast(via82xx_t, entry->private_data, return);
1740 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1741 for (i = 0; i < 0xa0; i += 4) {
1742 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
1746 static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
1748 snd_info_entry_t *entry;
1750 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
1751 snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
1758 static int __devinit snd_via82xx_chip_init(via82xx_t *chip)
1765 memset(&ac97, 0, sizeof(ac97));
1766 ac97.private_data = chip;
1768 #if 0 /* broken on K7M? */
1769 if (chip->chip_type == TYPE_VIA686)
1770 /* disable all legacy ports */
1771 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
1773 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1774 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
1775 /* deassert ACLink reset, force SYNC */
1776 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1777 VIA_ACLINK_CTRL_ENABLE |
1778 VIA_ACLINK_CTRL_RESET |
1779 VIA_ACLINK_CTRL_SYNC);
1781 #if 1 /* FIXME: should we do full reset here for all chip models? */
1782 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
1785 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
1786 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1787 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
1790 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
1791 /* note - FM data out has trouble with non VRA codecs !! */
1792 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1796 /* Make sure VRA is enabled, in case we didn't do a
1797 * complete codec reset, above */
1798 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
1799 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
1800 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
1801 /* note - FM data out has trouble with non VRA codecs !! */
1802 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1806 /* wait until codec ready */
1807 max_count = ((3 * HZ) / 4) + 1;
1809 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1810 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
1812 set_current_state(TASK_UNINTERRUPTIBLE);
1813 schedule_timeout(1);
1814 } while (--max_count > 0);
1816 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
1817 snd_printk("AC'97 codec is not ready [0x%x]\n", val);
1819 /* and then reset codec.. */
1820 snd_via82xx_codec_ready(chip, 0);
1821 snd_via82xx_codec_write(&ac97, AC97_RESET, 0x0000);
1822 snd_via82xx_codec_read(&ac97, 0);
1824 #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
1825 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1826 VIA_REG_AC97_SECONDARY_VALID |
1827 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1828 max_count = ((3 * HZ) / 4) + 1;
1829 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1830 VIA_REG_AC97_SECONDARY_VALID |
1831 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1833 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
1834 chip->ac97_secondary = 1;
1837 set_current_state(TASK_INTERRUPTIBLE);
1838 schedule_timeout(1);
1839 } while (--max_count > 0);
1840 /* This is ok, the most of motherboards have only one codec */
1845 if (chip->chip_type == TYPE_VIA686) {
1846 /* route FM trap to IRQ, disable FM trap */
1847 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
1848 /* disable all GPI interrupts */
1849 outl(0, VIAREG(chip, GPI_INTR));
1852 if (chip->chip_type != TYPE_VIA686) {
1853 /* Workaround for Award BIOS bug:
1854 * DXS channels don't work properly with VRA if MC97 is disabled.
1856 struct pci_dev *pci;
1857 pci = pci_find_device(0x1106, 0x3068, NULL); /* MC97 */
1860 pci_read_config_byte(pci, 0x44, &data);
1861 pci_write_config_byte(pci, 0x44, data | 0x40);
1868 static int snd_via82xx_free(via82xx_t *chip)
1874 /* disable interrupts */
1875 for (i = 0; i < chip->num_devs; i++)
1876 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1877 synchronize_irq(chip->irq);
1879 if (chip->mpu_res) {
1880 release_resource(chip->mpu_res);
1881 kfree_nocheck(chip->mpu_res);
1883 if (chip->res_port) {
1884 release_resource(chip->res_port);
1885 kfree_nocheck(chip->res_port);
1888 free_irq(chip->irq, (void *)chip);
1889 if (chip->chip_type == TYPE_VIA686) {
1890 #ifdef SUPPORT_JOYSTICK
1891 if (chip->res_joystick) {
1892 gameport_unregister_port(&chip->gameport);
1893 release_resource(chip->res_joystick);
1894 kfree_nocheck(chip->res_joystick);
1897 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
1898 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
1900 snd_magic_kfree(chip);
1904 static int snd_via82xx_dev_free(snd_device_t *device)
1906 via82xx_t *chip = snd_magic_cast(via82xx_t, device->device_data, return -ENXIO);
1907 return snd_via82xx_free(chip);
1910 static int __devinit snd_via82xx_create(snd_card_t * card,
1911 struct pci_dev *pci,
1914 unsigned int ac97_clock,
1919 static snd_device_ops_t ops = {
1920 .dev_free = snd_via82xx_dev_free,
1923 if ((err = pci_enable_device(pci)) < 0)
1926 if ((chip = snd_magic_kcalloc(via82xx_t, 0, GFP_KERNEL)) == NULL)
1929 chip->chip_type = chip_type;
1930 chip->revision = revision;
1932 spin_lock_init(&chip->reg_lock);
1933 spin_lock_init(&chip->ac97_lock);
1934 spin_lock_init(&chip->rates[0].lock);
1935 spin_lock_init(&chip->rates[1].lock);
1940 memset(&chip->dma_dev, 0, sizeof(chip->dma_dev));
1941 chip->dma_dev.type = SNDRV_DMA_TYPE_DEV;
1942 chip->dma_dev.dev = snd_dma_pci_data(pci);
1944 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
1945 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
1947 chip->port = pci_resource_start(pci, 0);
1948 if ((chip->res_port = request_region(chip->port, 256, card->driver)) == NULL) {
1949 snd_printk("unable to grab ports 0x%lx-0x%lx\n", chip->port, chip->port + 256 - 1);
1950 snd_via82xx_free(chip);
1953 if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
1954 card->driver, (void *)chip)) {
1955 snd_printk("unable to grab IRQ %d\n", pci->irq);
1956 snd_via82xx_free(chip);
1959 chip->irq = pci->irq;
1960 if (ac97_clock >= 8000 && ac97_clock <= 48000)
1961 chip->ac97_clock = ac97_clock;
1962 synchronize_irq(chip->irq);
1964 if ((err = snd_via82xx_chip_init(chip)) < 0) {
1965 snd_via82xx_free(chip);
1969 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1970 snd_via82xx_free(chip);
1974 /* The 8233 ac97 controller does not implement the master bit
1975 * in the pci command register. IMHO this is a violation of the PCI spec.
1976 * We call pci_set_master here because it does not hurt. */
1977 pci_set_master(pci);
1979 snd_card_set_dev(card, &pci->dev);
1985 struct via823x_info {
1990 static struct via823x_info via823x_cards[] __devinitdata = {
1991 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
1992 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
1993 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
1994 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
1995 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
1999 * auto detection of DXS channel supports.
2001 struct dxs_whitelist {
2002 unsigned short vendor;
2003 unsigned short device;
2004 unsigned short mask;
2005 short action; /* new dxs_support value */
2008 static int __devinit check_dxs_list(struct pci_dev *pci)
2010 static struct dxs_whitelist whitelist[] = {
2011 { .vendor = 0x1005, .device = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
2012 { .vendor = 0x1019, .device = 0x0996, .action = VIA_DXS_48K },
2013 { .vendor = 0x1043, .device = 0x8095, .action = VIA_DXS_ENABLE }, /* ASUS A7V8X */
2014 { .vendor = 0x1043, .device = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
2015 { .vendor = 0x1043, .device = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
2016 { .vendor = 0x10cf, .device = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
2017 { .vendor = 0x1106, .device = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
2018 { .vendor = 0x1106, .device = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
2019 { .vendor = 0x1297, .device = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
2020 { .vendor = 0x1297, .device = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
2021 { .vendor = 0x1458, .device = 0xa002, .action = VIA_DXS_NO_VRA }, /* Gigabyte GA-7VAXP (FIXME: or DXS_ENABLE?) */
2022 { .vendor = 0x147b, .device = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
2023 { .vendor = 0x14ff, .device = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
2024 { .vendor = 0x1462, .device = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
2025 { .vendor = 0x1462, .device = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
2026 { .vendor = 0x1462, .device = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
2027 { .vendor = 0x1584, .device = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
2028 { .vendor = 0x1631, .device = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
2029 { .vendor = 0x1695, .device = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
2030 { .vendor = 0x1849, .device = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
2031 { } /* terminator */
2033 struct dxs_whitelist *w;
2034 unsigned short subsystem_vendor;
2035 unsigned short subsystem_device;
2037 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
2038 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
2040 for (w = whitelist; w->vendor; w++) {
2041 if (w->vendor != subsystem_vendor)
2044 if ((w->mask & subsystem_device) == w->device)
2047 if (subsystem_device == w->device)
2053 * not detected, try 48k rate only to be sure.
2055 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
2056 printk(KERN_INFO " Please try dxs_support=1 or dxs_support=4 option\n");
2057 printk(KERN_INFO " and report if it works on your machine.\n");
2061 static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2062 const struct pci_device_id *pci_id)
2067 unsigned char revision;
2068 int chip_type = 0, card_type;
2072 if (dev >= SNDRV_CARDS)
2079 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2083 card_type = pci_id->driver_data;
2084 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
2085 switch (card_type) {
2086 case TYPE_CARD_VIA686:
2087 strcpy(card->driver, "VIA686A");
2088 sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
2089 chip_type = TYPE_VIA686;
2091 case TYPE_CARD_VIA8233:
2092 chip_type = TYPE_VIA8233;
2093 sprintf(card->shortname, "VIA 823x rev%x", revision);
2094 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2095 if (revision == via823x_cards[i].revision) {
2096 chip_type = via823x_cards[i].type;
2097 strcpy(card->shortname, via823x_cards[i].name);
2101 if (chip_type != TYPE_VIA8233A) {
2102 if (dxs_support[dev] == VIA_DXS_AUTO)
2103 dxs_support[dev] = check_dxs_list(pci);
2104 /* force to use VIA8233 or 8233A model according to
2105 * dxs_support module option
2107 if (dxs_support[dev] == VIA_DXS_DISABLE)
2108 chip_type = TYPE_VIA8233A;
2110 chip_type = TYPE_VIA8233;
2112 if (chip_type == TYPE_VIA8233A)
2113 strcpy(card->driver, "VIA8233A");
2115 strcpy(card->driver, "VIA8233");
2118 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2123 if ((err = snd_via82xx_create(card, pci, chip_type, revision, ac97_clock[dev], &chip)) < 0)
2125 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk[dev])) < 0)
2128 if (chip_type == TYPE_VIA686) {
2129 if ((err = snd_via686_pcm_new(chip)) < 0 ||
2130 (err = snd_via686_init_misc(chip, dev)) < 0)
2133 if (chip_type == TYPE_VIA8233A) {
2134 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2136 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2138 if ((err = snd_via8233_pcm_new(chip)) < 0)
2140 if (dxs_support[dev] == VIA_DXS_48K)
2141 chip->dxs_fixed = 1;
2142 else if (dxs_support[dev] == VIA_DXS_NO_VRA)
2145 if ((err = snd_via8233_init_misc(chip, dev)) < 0)
2148 /* disable interrupts */
2149 for (i = 0; i < chip->num_devs; i++)
2150 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2152 sprintf(card->longname, "%s at 0x%lx, irq %d",
2153 card->shortname, chip->port, chip->irq);
2155 snd_via82xx_proc_init(chip);
2157 if ((err = snd_card_register(card)) < 0) {
2158 snd_card_free(card);
2161 pci_set_drvdata(pci, card);
2166 snd_card_free(card);
2170 static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2172 snd_card_free(pci_get_drvdata(pci));
2173 pci_set_drvdata(pci, NULL);
2176 static struct pci_driver driver = {
2177 .name = "VIA 82xx Audio",
2178 .id_table = snd_via82xx_ids,
2179 .probe = snd_via82xx_probe,
2180 .remove = __devexit_p(snd_via82xx_remove),
2183 static int __init alsa_card_via82xx_init(void)
2187 if ((err = pci_module_init(&driver)) < 0) {
2189 printk(KERN_ERR "VIA 82xx soundcard not found or device busy\n");
2196 static void __exit alsa_card_via82xx_exit(void)
2198 pci_unregister_driver(&driver);
2201 module_init(alsa_card_via82xx_init)
2202 module_exit(alsa_card_via82xx_exit)
2206 /* format is: snd-via82xx=enable,index,id,
2208 ac97_quirk,ac97_clock,dxs_support */
2210 static int __init alsa_card_via82xx_setup(char *str)
2212 static unsigned __initdata nr_dev = 0;
2214 if (nr_dev >= SNDRV_CARDS)
2216 (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2217 get_option(&str,&index[nr_dev]) == 2 &&
2218 get_id(&str,&id[nr_dev]) == 2 &&
2219 get_option_long(&str,&mpu_port[nr_dev]) == 2 &&
2220 #ifdef SUPPORT_JOYSTICK
2221 get_option(&str,&joystick[nr_dev]) == 2 &&
2223 get_option(&str,&ac97_quirk[nr_dev]) == 2 &&
2224 get_option(&str,&ac97_clock[nr_dev]) == 2 &&
2225 get_option(&str,&dxs_support[nr_dev]) == 2);
2230 __setup("snd-via82xx=", alsa_card_via82xx_setup);
2232 #endif /* ifndef MODULE */