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_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];
89 module_param_array(index, int, boot_devs, 0444);
90 MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
91 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
92 module_param_array(id, charp, boot_devs, 0444);
93 MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
94 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
95 module_param_array(enable, bool, boot_devs, 0444);
96 MODULE_PARM_DESC(enable, "Enable audio part of VIA 82xx bridge.");
97 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
98 module_param_array(mpu_port, long, boot_devs, 0444);
99 MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
100 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT_DESC);
101 #ifdef SUPPORT_JOYSTICK
102 module_param_array(joystick, bool, boot_devs, 0444);
103 MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
104 MODULE_PARM_SYNTAX(joystick, SNDRV_ENABLE_DESC "," SNDRV_BOOLEAN_FALSE_DESC);
106 module_param_array(ac97_clock, int, boot_devs, 0444);
107 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
108 MODULE_PARM_SYNTAX(ac97_clock, SNDRV_ENABLED ",default:48000");
109 module_param_array(ac97_quirk, int, boot_devs, 0444);
110 MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
111 MODULE_PARM_SYNTAX(ac97_quirk, SNDRV_ENABLED ",allows:{{-1,4}},dialog:list,default:-1");
112 module_param_array(dxs_support, int, boot_devs, 0444);
113 MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA)");
114 MODULE_PARM_SYNTAX(dxs_support, SNDRV_ENABLED ",allows:{{0,4}},dialog:list");
118 #ifndef PCI_DEVICE_ID_VIA_82C686_5
119 #define PCI_DEVICE_ID_VIA_82C686_5 0x3058
121 #ifndef PCI_DEVICE_ID_VIA_8233_5
122 #define PCI_DEVICE_ID_VIA_8233_5 0x3059
125 /* revision numbers for via686 */
126 #define VIA_REV_686_A 0x10
127 #define VIA_REV_686_B 0x11
128 #define VIA_REV_686_C 0x12
129 #define VIA_REV_686_D 0x13
130 #define VIA_REV_686_E 0x14
131 #define VIA_REV_686_H 0x20
133 /* revision numbers for via8233 */
134 #define VIA_REV_PRE_8233 0x10 /* not in market */
135 #define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
136 #define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
137 #define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
138 #define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
144 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
145 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
148 #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
149 #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
150 #define VIA_REG_STAT_PAUSED 0x40 /* RO */
151 #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
152 #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
153 #define VIA_REG_STAT_EOL 0x02 /* RWC */
154 #define VIA_REG_STAT_FLAG 0x01 /* RWC */
155 #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
156 #define VIA_REG_CTRL_START 0x80 /* WO */
157 #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
158 #define VIA_REG_CTRL_AUTOSTART 0x20
159 #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
160 #define VIA_REG_CTRL_INT_STOP 0x04
161 #define VIA_REG_CTRL_INT_EOL 0x02
162 #define VIA_REG_CTRL_INT_FLAG 0x01
163 #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
164 #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
165 #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
166 #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
167 #define VIA_REG_TYPE_16BIT 0x20 /* RW */
168 #define VIA_REG_TYPE_STEREO 0x10 /* RW */
169 #define VIA_REG_TYPE_INT_LLINE 0x00
170 #define VIA_REG_TYPE_INT_LSAMPLE 0x04
171 #define VIA_REG_TYPE_INT_LESSONE 0x08
172 #define VIA_REG_TYPE_INT_MASK 0x0c
173 #define VIA_REG_TYPE_INT_EOL 0x02
174 #define VIA_REG_TYPE_INT_FLAG 0x01
175 #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
176 #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
177 #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
178 #define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
179 #define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
180 #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
181 #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
183 #define DEFINE_VIA_REGSET(name,val) \
185 VIA_REG_##name##_STATUS = (val),\
186 VIA_REG_##name##_CONTROL = (val) + 0x01,\
187 VIA_REG_##name##_TYPE = (val) + 0x02,\
188 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
189 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
190 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
191 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
195 DEFINE_VIA_REGSET(PLAYBACK, 0x00);
196 DEFINE_VIA_REGSET(CAPTURE, 0x10);
197 DEFINE_VIA_REGSET(FM, 0x20);
200 #define VIA_REG_AC97 0x80 /* dword */
201 #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
202 #define VIA_REG_AC97_CODEC_ID_SHIFT 30
203 #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
204 #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
205 #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
206 #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
207 #define VIA_REG_AC97_BUSY (1<<24)
208 #define VIA_REG_AC97_READ (1<<23)
209 #define VIA_REG_AC97_CMD_SHIFT 16
210 #define VIA_REG_AC97_CMD_MASK 0x7e
211 #define VIA_REG_AC97_DATA_SHIFT 0
212 #define VIA_REG_AC97_DATA_MASK 0xffff
214 #define VIA_REG_SGD_SHADOW 0x84 /* dword */
216 #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
217 #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
218 #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
219 #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
220 #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
221 #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
222 #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
223 #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
224 #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
225 #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
226 #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
227 #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
229 #define VIA8233_REG_SGD_STAT_FLAG (1<<0)
230 #define VIA8233_REG_SGD_STAT_EOL (1<<1)
231 #define VIA8233_REG_SGD_STAT_STOP (1<<2)
232 #define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
233 #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
234 #define VIA8233_REG_SGD_CHAN_SDX 0
235 #define VIA8233_REG_SGD_CHAN_MULTI 4
236 #define VIA8233_REG_SGD_CHAN_REC 6
237 #define VIA8233_REG_SGD_CHAN_REC1 7
239 #define VIA_REG_GPI_STATUS 0x88
240 #define VIA_REG_GPI_INTR 0x8c
242 /* multi-channel and capture registers for via8233 */
243 DEFINE_VIA_REGSET(MULTPLAY, 0x40);
244 DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
246 /* via8233-specific registers */
247 #define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
248 #define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
249 #define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
250 #define VIA_REG_MULTPLAY_FMT_8BIT 0x00
251 #define VIA_REG_MULTPLAY_FMT_16BIT 0x80
252 #define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
253 #define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
254 #define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
256 #define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
258 #define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
259 #define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
260 #define VIA_REG_CAPTURE_CHANNEL_LINE 0
261 #define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
263 #define VIA_TBL_BIT_FLAG 0x40000000
264 #define VIA_TBL_BIT_EOL 0x80000000
267 #define VIA_ACLINK_STAT 0x40
268 #define VIA_ACLINK_C11_READY 0x20
269 #define VIA_ACLINK_C10_READY 0x10
270 #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
271 #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
272 #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
273 #define VIA_ACLINK_CTRL 0x41
274 #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
275 #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
276 #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
277 #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
278 #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
279 #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
280 #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
281 #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
282 #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
283 VIA_ACLINK_CTRL_RESET|\
284 VIA_ACLINK_CTRL_PCM|\
286 #define VIA_FUNC_ENABLE 0x42
287 #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
288 #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
289 #define VIA_FUNC_RX2C_WRITE 0x20
290 #define VIA_FUNC_SB_FIFO_EMPTY 0x10
291 #define VIA_FUNC_ENABLE_GAME 0x08
292 #define VIA_FUNC_ENABLE_FM 0x04
293 #define VIA_FUNC_ENABLE_MIDI 0x02
294 #define VIA_FUNC_ENABLE_SB 0x01
295 #define VIA_PNP_CONTROL 0x43
296 #define VIA_FM_NMI_CTRL 0x48
297 #define VIA8233_VOLCHG_CTRL 0x48
298 #define VIA8233_SPDIF_CTRL 0x49
299 #define VIA8233_SPDIF_DX3 0x08
300 #define VIA8233_SPDIF_SLOT_MASK 0x03
301 #define VIA8233_SPDIF_SLOT_1011 0x00
302 #define VIA8233_SPDIF_SLOT_34 0x01
303 #define VIA8233_SPDIF_SLOT_78 0x02
304 #define VIA8233_SPDIF_SLOT_69 0x03
309 #define VIA_DXS_AUTO 0
310 #define VIA_DXS_ENABLE 1
311 #define VIA_DXS_DISABLE 2
312 #define VIA_DXS_48K 3
313 #define VIA_DXS_NO_VRA 4
319 typedef struct _snd_via82xx via82xx_t;
320 typedef struct via_dev viadev_t;
321 #define chip_t via82xx_t
327 struct snd_via_sg_table {
332 #define VIA_TABLE_SIZE 255
335 unsigned int reg_offset;
337 int direction; /* playback = 0, capture = 1 */
338 snd_pcm_substream_t *substream;
340 unsigned int tbl_entries; /* # descriptors */
341 struct snd_dma_buffer table;
342 struct snd_via_sg_table *idx_table;
343 /* for recovery from the unexpected pointer */
344 unsigned int lastpos;
345 unsigned int bufsize;
346 unsigned int bufsize2;
350 enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
351 enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
353 #define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
355 struct via_rate_lock {
361 struct _snd_via82xx {
365 struct resource *res_port;
366 struct resource *mpu_res;
368 unsigned char revision;
370 unsigned char old_legacy;
371 unsigned char old_legacy_cfg;
373 unsigned char legacy_saved;
374 unsigned char legacy_cfg_saved;
375 unsigned char spdif_ctrl_saved;
376 unsigned char capture_src_saved[2];
377 unsigned int mpu_port_saved;
381 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
383 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
388 unsigned int num_devs;
389 unsigned int playback_devno, multi_devno, capture_devno;
390 viadev_t devs[VIA_MAX_DEVS];
391 struct via_rate_lock rates[2]; /* playback and capture */
392 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
393 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
394 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
397 snd_rawmidi_t *rmidi;
399 ac97_bus_t *ac97_bus;
401 unsigned int ac97_clock;
402 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
405 spinlock_t ac97_lock;
406 snd_info_entry_t *proc_entry;
408 struct snd_dma_device dma_dev;
410 #ifdef SUPPORT_JOYSTICK
411 struct gameport gameport;
412 struct resource *res_joystick;
416 static struct pci_device_id snd_via82xx_ids[] = {
417 { 0x1106, 0x3058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
418 { 0x1106, 0x3059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
422 MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
428 * allocate and initialize the descriptor buffers
429 * periods = number of periods
430 * fragsize = period size in bytes
432 static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
434 unsigned int periods, unsigned int fragsize)
436 unsigned int i, idx, ofs, rest;
437 via82xx_t *chip = snd_pcm_substream_chip(substream);
438 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
440 if (dev->table.area == NULL) {
441 /* the start of each lists must be aligned to 8 bytes,
442 * but the kernel pages are much bigger, so we don't care
444 if (snd_dma_alloc_pages(&chip->dma_dev, PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8), &dev->table) < 0)
447 if (! dev->idx_table) {
448 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
449 if (! dev->idx_table)
453 /* fill the entries */
456 for (i = 0; i < periods; i++) {
458 /* fill descriptors for a period.
459 * a period can be split to several descriptors if it's
460 * over page boundary.
466 if (idx >= VIA_TABLE_SIZE) {
467 snd_printk(KERN_ERR "via82xx: too much table size!\n");
470 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
471 r = PAGE_SIZE - (ofs % PAGE_SIZE);
476 if (i == periods - 1)
477 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
479 flag = VIA_TBL_BIT_FLAG; /* period boundary */
481 flag = 0; /* period continues to the next */
482 // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
483 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
484 dev->idx_table[idx].offset = ofs;
485 dev->idx_table[idx].size = r;
490 dev->tbl_entries = idx;
491 dev->bufsize = periods * fragsize;
492 dev->bufsize2 = dev->bufsize / 2;
497 static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
500 via82xx_t *chip = snd_pcm_substream_chip(substream);
501 if (dev->table.area) {
502 snd_dma_free_pages(&chip->dma_dev, &dev->table);
503 dev->table.area = NULL;
505 if (dev->idx_table) {
506 kfree(dev->idx_table);
507 dev->idx_table = NULL;
516 static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
518 return inl(VIAREG(chip, AC97));
521 static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
523 outl(val, VIAREG(chip, AC97));
526 static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
528 unsigned int timeout = 1000; /* 1ms */
531 while (timeout-- > 0) {
533 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
536 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
540 static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
542 unsigned int timeout = 1000; /* 1ms */
543 unsigned int val, val1;
544 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
545 VIA_REG_AC97_SECONDARY_VALID;
547 while (timeout-- > 0) {
548 val = snd_via82xx_codec_xread(chip);
549 val1 = val & (VIA_REG_AC97_BUSY | stat);
557 static void snd_via82xx_codec_wait(ac97_t *ac97)
559 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return);
561 err = snd_via82xx_codec_ready(chip, ac97->num);
562 /* here we need to wait fairly for long time.. */
563 set_current_state(TASK_UNINTERRUPTIBLE);
564 schedule_timeout(HZ/2);
567 static void snd_via82xx_codec_write(ac97_t *ac97,
571 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return);
574 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
575 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
576 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
577 xval |= val << VIA_REG_AC97_DATA_SHIFT;
578 spin_lock(&chip->ac97_lock);
579 snd_via82xx_codec_xwrite(chip, xval);
580 snd_via82xx_codec_ready(chip, ac97->num);
581 spin_unlock(&chip->ac97_lock);
584 static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
586 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return ~0);
587 unsigned int xval, val = 0xffff;
590 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
591 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
592 xval |= VIA_REG_AC97_READ;
593 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
594 spin_lock(&chip->ac97_lock);
597 spin_unlock(&chip->ac97_lock);
598 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
601 snd_via82xx_codec_xwrite(chip, xval);
603 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
605 val = snd_via82xx_codec_xread(chip);
609 spin_unlock(&chip->ac97_lock);
613 static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
615 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
616 VIADEV_REG(viadev, OFFSET_CONTROL));
617 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
619 /* disable interrupts */
620 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
621 /* clear interrupts */
622 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
623 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
624 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
633 static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
635 via82xx_t *chip = snd_magic_cast(via82xx_t, dev_id, return IRQ_NONE);
639 spin_lock(&chip->reg_lock);
641 /* FIXME: does it work on via823x? */
642 if (chip->chip_type != TYPE_VIA686)
645 status = inl(VIAREG(chip, SGD_SHADOW));
646 if (! (status & chip->intr_mask)) {
647 spin_unlock(&chip->reg_lock);
649 /* check mpu401 interrupt */
650 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
655 /* check status for each stream */
656 for (i = 0; i < chip->num_devs; i++) {
657 viadev_t *viadev = &chip->devs[i];
658 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
659 c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
662 if (viadev->substream && viadev->running) {
663 spin_unlock(&chip->reg_lock);
664 snd_pcm_period_elapsed(viadev->substream);
665 spin_lock(&chip->reg_lock);
667 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
669 spin_unlock(&chip->reg_lock);
680 static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
682 via82xx_t *chip = snd_pcm_substream_chip(substream);
683 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
686 if (chip->chip_type != TYPE_VIA686)
687 val = VIA_REG_CTRL_INT;
691 case SNDRV_PCM_TRIGGER_START:
692 val |= VIA_REG_CTRL_START;
695 case SNDRV_PCM_TRIGGER_STOP:
696 val = VIA_REG_CTRL_TERMINATE;
699 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
700 val |= VIA_REG_CTRL_PAUSE;
703 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
709 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
710 if (cmd == SNDRV_PCM_TRIGGER_STOP)
711 snd_via82xx_channel_reset(chip, viadev);
721 * calculate the linear position at the given sg-buffer index and the rest count
723 static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
725 unsigned int size, res;
727 size = viadev->idx_table[idx].size;
728 res = viadev->idx_table[idx].offset + size - count;
730 /* check the validity of the calculated position */
731 if (size < count || (res < viadev->lastpos && (res >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))) {
733 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);
735 /* count register returns full size when end of buffer is reached */
737 snd_printd(KERN_ERR "invalid via82xx_cur_ptr, using last valid pointer\n");
738 res = viadev->lastpos;
740 res = viadev->idx_table[idx].offset + size;
741 if (res < viadev->lastpos && (res >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2)) {
742 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
743 res = viadev->lastpos;
747 viadev->lastpos = res; /* remember the last position */
748 if (res >= viadev->bufsize)
749 res -= viadev->bufsize;
754 * get the current pointer on via686
756 static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
758 via82xx_t *chip = snd_pcm_substream_chip(substream);
759 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
760 unsigned int idx, ptr, count, res;
762 snd_assert(viadev->tbl_entries, return 0);
763 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
766 spin_lock(&chip->reg_lock);
767 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
768 /* The via686a does not have the current index register,
769 * so we need to calculate the index from CURR_PTR.
771 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
772 if (ptr <= (unsigned int)viadev->table.addr)
774 else /* CURR_PTR holds the address + 8 */
775 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
776 res = calc_linear_pos(viadev, idx, count);
777 spin_unlock(&chip->reg_lock);
779 return bytes_to_frames(substream->runtime, res);
783 * get the current pointer on via823x
785 static snd_pcm_uframes_t snd_via8233_pcm_pointer(snd_pcm_substream_t *substream)
787 via82xx_t *chip = snd_pcm_substream_chip(substream);
788 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
789 unsigned int idx, count, res;
791 snd_assert(viadev->tbl_entries, return 0);
792 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
794 spin_lock(&chip->reg_lock);
795 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
797 if (idx >= viadev->tbl_entries) {
799 printk("fail: invalid idx = %i/%i\n", idx, viadev->tbl_entries);
801 res = viadev->lastpos;
804 res = calc_linear_pos(viadev, idx, count);
806 spin_unlock(&chip->reg_lock);
808 return bytes_to_frames(substream->runtime, res);
813 * hw_params callback:
814 * allocate the buffer and build up the buffer description table
816 static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
817 snd_pcm_hw_params_t * hw_params)
819 via82xx_t *chip = snd_pcm_substream_chip(substream);
820 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
823 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
826 err = build_via_table(viadev, substream, chip->pci,
827 params_periods(hw_params),
828 params_period_bytes(hw_params));
837 * clean up the buffer description table and release the buffer
839 static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
841 via82xx_t *chip = snd_pcm_substream_chip(substream);
842 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
844 clean_via_table(viadev, substream, chip->pci);
845 snd_pcm_lib_free_pages(substream);
851 * set up the table pointer
853 static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
855 snd_via82xx_codec_ready(chip, 0);
856 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
858 snd_via82xx_codec_ready(chip, 0);
862 * prepare callback for playback and capture on via686
864 static void via686_setup_format(via82xx_t *chip, viadev_t *viadev, snd_pcm_runtime_t *runtime)
866 snd_via82xx_channel_reset(chip, viadev);
867 /* this must be set after channel_reset */
868 snd_via82xx_set_table_ptr(chip, viadev);
869 outb(VIA_REG_TYPE_AUTOSTART |
870 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
871 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
872 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
873 VIA_REG_TYPE_INT_EOL |
874 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
877 static int snd_via686_playback_prepare(snd_pcm_substream_t *substream)
879 via82xx_t *chip = snd_pcm_substream_chip(substream);
880 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
881 snd_pcm_runtime_t *runtime = substream->runtime;
883 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
884 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
885 via686_setup_format(chip, viadev, runtime);
889 static int snd_via686_capture_prepare(snd_pcm_substream_t *substream)
891 via82xx_t *chip = snd_pcm_substream_chip(substream);
892 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
893 snd_pcm_runtime_t *runtime = substream->runtime;
895 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
896 via686_setup_format(chip, viadev, runtime);
901 * lock the current rate
903 static int via_lock_rate(struct via_rate_lock *rec, int rate)
907 spin_lock(&rec->lock);
908 if (rec->rate != rate) {
909 if (rec->rate && rec->used > 1) /* already set */
916 spin_unlock(&rec->lock);
921 * prepare callback for DSX playback on via823x
923 static int snd_via8233_playback_prepare(snd_pcm_substream_t *substream)
925 via82xx_t *chip = snd_pcm_substream_chip(substream);
926 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
927 snd_pcm_runtime_t *runtime = substream->runtime;
931 if ((rate_changed = via_lock_rate(&chip->rates[0], runtime->rate)) < 0)
934 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
935 chip->no_vra ? 48000 : runtime->rate);
936 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
938 if (runtime->rate == 48000)
941 rbits = (0x100000 / 48000) * runtime->rate + ((0x100000 % 48000) * runtime->rate) / 48000;
942 snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
943 snd_via82xx_channel_reset(chip, viadev);
944 snd_via82xx_set_table_ptr(chip, viadev);
945 outb(chip->playback_volume[viadev->reg_offset / 0x10][0], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
946 outb(chip->playback_volume[viadev->reg_offset / 0x10][1], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
947 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
948 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
950 0xff000000, /* STOP index is never reached */
951 VIADEV_REG(viadev, OFFSET_STOP_IDX));
953 snd_via82xx_codec_ready(chip, 0);
958 * prepare callback for multi-channel playback on via823x
960 static int snd_via8233_multi_prepare(snd_pcm_substream_t *substream)
962 via82xx_t *chip = snd_pcm_substream_chip(substream);
963 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
964 snd_pcm_runtime_t *runtime = substream->runtime;
968 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
970 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
971 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
972 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
973 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
974 snd_via82xx_channel_reset(chip, viadev);
975 snd_via82xx_set_table_ptr(chip, viadev);
977 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ? VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
978 fmt |= runtime->channels << 4;
979 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
981 if (chip->revision == VIA_REV_8233A)
986 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
987 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
988 switch (runtime->channels) {
989 case 1: slots = (1<<0) | (1<<4); break;
990 case 2: slots = (1<<0) | (2<<4); break;
991 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
992 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
993 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
994 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
995 default: slots = 0; break;
998 /* STOP index is never reached */
999 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1001 snd_via82xx_codec_ready(chip, 0);
1006 * prepare callback for capture on via823x
1008 static int snd_via8233_capture_prepare(snd_pcm_substream_t *substream)
1010 via82xx_t *chip = snd_pcm_substream_chip(substream);
1011 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1012 snd_pcm_runtime_t *runtime = substream->runtime;
1014 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1016 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1017 snd_via82xx_channel_reset(chip, viadev);
1018 snd_via82xx_set_table_ptr(chip, viadev);
1019 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1020 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1021 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1022 0xff000000, /* STOP index is never reached */
1023 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1025 snd_via82xx_codec_ready(chip, 0);
1031 * pcm hardware definition, identical for both playback and capture
1033 static snd_pcm_hardware_t snd_via82xx_hw =
1035 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1036 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1037 SNDRV_PCM_INFO_MMAP_VALID |
1038 SNDRV_PCM_INFO_PAUSE),
1039 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1040 .rates = SNDRV_PCM_RATE_48000,
1045 .buffer_bytes_max = 128 * 1024,
1046 .period_bytes_min = 32,
1047 .period_bytes_max = 128 * 1024,
1049 .periods_max = VIA_TABLE_SIZE / 2,
1055 * open callback skeleton
1057 static int snd_via82xx_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
1059 snd_pcm_runtime_t *runtime = substream->runtime;
1061 unsigned long flags;
1062 struct via_rate_lock *ratep;
1067 {8000, SNDRV_PCM_RATE_8000},
1068 {11025, SNDRV_PCM_RATE_11025},
1069 {16000, SNDRV_PCM_RATE_16000},
1070 {22050, SNDRV_PCM_RATE_22050},
1071 {32000, SNDRV_PCM_RATE_32000},
1072 {44100, SNDRV_PCM_RATE_44100},
1073 {48000, SNDRV_PCM_RATE_48000},
1077 runtime->hw = snd_via82xx_hw;
1079 /* set the hw rate condition */
1080 ratep = &chip->rates[viadev->direction];
1081 spin_lock_irqsave(&ratep->lock, flags);
1083 if (chip->spdif_on) {
1084 runtime->hw.rates = SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000;
1085 runtime->hw.rate_min = 32000;
1086 runtime->hw.rate_max = 48000;
1087 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1088 /* fixed DXS playback rate */
1089 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1090 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1091 } else if (! ratep->rate) {
1092 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1093 runtime->hw.rates = chip->ac97->rates[idx];
1094 for (i = 0; i < (int)ARRAY_SIZE(ratebits); i++) {
1095 if (runtime->hw.rates & ratebits[i].bit) {
1096 runtime->hw.rate_min = ratebits[i].rate;
1100 for (i = ARRAY_SIZE(ratebits) - 1; i >= 0; i--) {
1101 if (runtime->hw.rates & ratebits[i].bit) {
1102 runtime->hw.rate_max = ratebits[i].rate;
1108 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1109 for (i = 0; i < (int)ARRAY_SIZE(ratebits); i++) {
1110 if (ratep->rate == ratebits[i].rate) {
1111 runtime->hw.rates = ratebits[i].bit;
1115 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1117 spin_unlock_irqrestore(&ratep->lock, flags);
1119 /* we may remove following constaint when we modify table entries
1121 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1124 runtime->private_data = viadev;
1125 viadev->substream = substream;
1132 * open callback for playback on via686 and via823x DSX
1134 static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
1136 via82xx_t *chip = snd_pcm_substream_chip(substream);
1137 viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
1140 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1146 * open callback for playback on via823x multi-channel
1148 static int snd_via8233_multi_open(snd_pcm_substream_t * substream)
1150 via82xx_t *chip = snd_pcm_substream_chip(substream);
1151 viadev_t *viadev = &chip->devs[chip->multi_devno];
1153 /* channels constraint for VIA8233A
1154 * 3 and 5 channels are not supported
1156 static unsigned int channels[] = {
1159 static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
1160 .count = ARRAY_SIZE(channels),
1165 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1167 substream->runtime->hw.channels_max = 6;
1168 if (chip->revision == VIA_REV_8233A)
1169 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
1174 * open callback for capture on via686 and via823x
1176 static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
1178 via82xx_t *chip = snd_pcm_substream_chip(substream);
1179 viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1181 return snd_via82xx_pcm_open(chip, viadev, substream);
1187 static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
1189 via82xx_t *chip = snd_pcm_substream_chip(substream);
1190 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1191 unsigned long flags;
1192 struct via_rate_lock *ratep;
1194 /* release the rate lock */
1195 ratep = &chip->rates[viadev->direction];
1196 spin_lock_irqsave(&ratep->lock, flags);
1200 spin_unlock_irqrestore(&ratep->lock, flags);
1202 viadev->substream = NULL;
1207 /* via686 playback callbacks */
1208 static snd_pcm_ops_t snd_via686_playback_ops = {
1209 .open = snd_via82xx_playback_open,
1210 .close = snd_via82xx_pcm_close,
1211 .ioctl = snd_pcm_lib_ioctl,
1212 .hw_params = snd_via82xx_hw_params,
1213 .hw_free = snd_via82xx_hw_free,
1214 .prepare = snd_via686_playback_prepare,
1215 .trigger = snd_via82xx_pcm_trigger,
1216 .pointer = snd_via686_pcm_pointer,
1217 .page = snd_pcm_sgbuf_ops_page,
1220 /* via686 capture callbacks */
1221 static snd_pcm_ops_t snd_via686_capture_ops = {
1222 .open = snd_via82xx_capture_open,
1223 .close = snd_via82xx_pcm_close,
1224 .ioctl = snd_pcm_lib_ioctl,
1225 .hw_params = snd_via82xx_hw_params,
1226 .hw_free = snd_via82xx_hw_free,
1227 .prepare = snd_via686_capture_prepare,
1228 .trigger = snd_via82xx_pcm_trigger,
1229 .pointer = snd_via686_pcm_pointer,
1230 .page = snd_pcm_sgbuf_ops_page,
1233 /* via823x DSX playback callbacks */
1234 static snd_pcm_ops_t snd_via8233_playback_ops = {
1235 .open = snd_via82xx_playback_open,
1236 .close = snd_via82xx_pcm_close,
1237 .ioctl = snd_pcm_lib_ioctl,
1238 .hw_params = snd_via82xx_hw_params,
1239 .hw_free = snd_via82xx_hw_free,
1240 .prepare = snd_via8233_playback_prepare,
1241 .trigger = snd_via82xx_pcm_trigger,
1242 .pointer = snd_via8233_pcm_pointer,
1243 .page = snd_pcm_sgbuf_ops_page,
1246 /* via823x multi-channel playback callbacks */
1247 static snd_pcm_ops_t snd_via8233_multi_ops = {
1248 .open = snd_via8233_multi_open,
1249 .close = snd_via82xx_pcm_close,
1250 .ioctl = snd_pcm_lib_ioctl,
1251 .hw_params = snd_via82xx_hw_params,
1252 .hw_free = snd_via82xx_hw_free,
1253 .prepare = snd_via8233_multi_prepare,
1254 .trigger = snd_via82xx_pcm_trigger,
1255 .pointer = snd_via8233_pcm_pointer,
1256 .page = snd_pcm_sgbuf_ops_page,
1259 /* via823x capture callbacks */
1260 static snd_pcm_ops_t snd_via8233_capture_ops = {
1261 .open = snd_via82xx_capture_open,
1262 .close = snd_via82xx_pcm_close,
1263 .ioctl = snd_pcm_lib_ioctl,
1264 .hw_params = snd_via82xx_hw_params,
1265 .hw_free = snd_via82xx_hw_free,
1266 .prepare = snd_via8233_capture_prepare,
1267 .trigger = snd_via82xx_pcm_trigger,
1268 .pointer = snd_via8233_pcm_pointer,
1269 .page = snd_pcm_sgbuf_ops_page,
1273 static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
1275 chip->devs[idx].reg_offset = reg_offset;
1276 chip->devs[idx].direction = direction;
1277 chip->devs[idx].port = chip->port + reg_offset;
1281 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1283 static int __devinit snd_via8233_pcm_new(via82xx_t *chip)
1288 chip->playback_devno = 0; /* x 4 */
1289 chip->multi_devno = 4; /* x 1 */
1290 chip->capture_devno = 5; /* x 2 */
1292 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1294 /* PCM #0: 4 DSX playbacks and 1 capture */
1295 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1298 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1299 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1300 pcm->private_data = chip;
1301 strcpy(pcm->name, chip->card->shortname);
1302 chip->pcms[0] = pcm;
1303 /* set up playbacks */
1304 for (i = 0; i < 4; i++)
1305 init_viadev(chip, i, 0x10 * i, 0);
1307 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1309 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1310 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1313 /* PCM #1: multi-channel playback and 2nd capture */
1314 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1317 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1318 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1319 pcm->private_data = chip;
1320 strcpy(pcm->name, chip->card->shortname);
1321 chip->pcms[1] = pcm;
1322 /* set up playback */
1323 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1324 /* set up capture */
1325 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 1);
1327 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1328 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1335 * create pcm instances for VIA8233A
1337 static int __devinit snd_via8233a_pcm_new(via82xx_t *chip)
1342 chip->multi_devno = 0;
1343 chip->playback_devno = 1;
1344 chip->capture_devno = 2;
1346 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1348 /* PCM #0: multi-channel playback and capture */
1349 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1352 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1353 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1354 pcm->private_data = chip;
1355 strcpy(pcm->name, chip->card->shortname);
1356 chip->pcms[0] = pcm;
1357 /* set up playback */
1358 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1360 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1362 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1363 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1366 /* PCM #1: DXS3 playback (for spdif) */
1367 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1370 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1371 pcm->private_data = chip;
1372 strcpy(pcm->name, chip->card->shortname);
1373 chip->pcms[1] = pcm;
1374 /* set up playback */
1375 init_viadev(chip, chip->playback_devno, 0x30, 0);
1377 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1378 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1385 * create a pcm instance for via686a/b
1387 static int __devinit snd_via686_pcm_new(via82xx_t *chip)
1392 chip->playback_devno = 0;
1393 chip->capture_devno = 1;
1395 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1397 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1400 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1401 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1402 pcm->private_data = chip;
1403 strcpy(pcm->name, chip->card->shortname);
1404 chip->pcms[0] = pcm;
1405 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0);
1406 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 1);
1408 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1409 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1420 static int snd_via8233_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1422 /* formerly they were "Line" and "Mic", but it looks like that they
1423 * have nothing to do with the actual physical connections...
1425 static char *texts[2] = {
1428 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1430 uinfo->value.enumerated.items = 2;
1431 if (uinfo->value.enumerated.item >= 2)
1432 uinfo->value.enumerated.item = 1;
1433 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1437 static int snd_via8233_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1439 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1440 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1441 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1445 static int snd_via8233_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1447 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1448 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1449 unsigned long flags;
1452 spin_lock_irqsave(&chip->reg_lock, flags);
1454 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1455 if (ucontrol->value.enumerated.item[0])
1456 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1459 spin_unlock_irqrestore(&chip->reg_lock, flags);
1463 static snd_kcontrol_new_t snd_via8233_capture_source __devinitdata = {
1464 .name = "Input Source Select",
1465 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1466 .info = snd_via8233_capture_source_info,
1467 .get = snd_via8233_capture_source_get,
1468 .put = snd_via8233_capture_source_put,
1471 static int snd_via8233_dxs3_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1473 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1475 uinfo->value.integer.min = 0;
1476 uinfo->value.integer.max = 1;
1480 static int snd_via8233_dxs3_spdif_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1482 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1485 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1486 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1490 static int snd_via8233_dxs3_spdif_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1492 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1495 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1496 val = oval & ~VIA8233_SPDIF_DX3;
1497 if (ucontrol->value.integer.value[0])
1498 val |= VIA8233_SPDIF_DX3;
1499 /* save the spdif flag for rate filtering */
1500 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1502 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1508 static snd_kcontrol_new_t snd_via8233_dxs3_spdif_control __devinitdata = {
1509 .name = "IEC958 Output Switch",
1510 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1511 .info = snd_via8233_dxs3_spdif_info,
1512 .get = snd_via8233_dxs3_spdif_get,
1513 .put = snd_via8233_dxs3_spdif_put,
1516 static int snd_via8233_dxs_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1518 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1520 uinfo->value.integer.min = 0;
1521 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1525 static int snd_via8233_dxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1527 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1528 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1529 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1530 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1534 static int snd_via8233_dxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1536 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1537 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1538 unsigned long port = chip->port + 0x10 * idx;
1542 for (i = 0; i < 2; i++) {
1543 val = ucontrol->value.integer.value[i];
1544 if (val > VIA_DXS_MAX_VOLUME)
1545 val = VIA_DXS_MAX_VOLUME;
1546 val = VIA_DXS_MAX_VOLUME - val;
1547 change |= val != chip->playback_volume[idx][i];
1549 chip->playback_volume[idx][i] = val;
1550 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1556 static snd_kcontrol_new_t snd_via8233_dxs_volume_control __devinitdata = {
1557 .name = "VIA DXS Playback Volume",
1558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1560 .info = snd_via8233_dxs_volume_info,
1561 .get = snd_via8233_dxs_volume_get,
1562 .put = snd_via8233_dxs_volume_put,
1568 static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
1570 via82xx_t *chip = snd_magic_cast(via82xx_t, bus->private_data, return);
1571 chip->ac97_bus = NULL;
1574 static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
1576 via82xx_t *chip = snd_magic_cast(via82xx_t, ac97->private_data, return);
1580 static struct ac97_quirk ac97_quirks[] = {
1581 { /* FIXME: which codec? */
1584 .name = "ASRock K7VT2",
1585 .type = AC97_TUNE_HP_ONLY
1590 .name = "ASRock K7VM2",
1591 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1597 .type = AC97_TUNE_ALC_JACK
1602 .name = "Mitac Mobo",
1603 .type = AC97_TUNE_ALC_JACK
1608 .name = "Arima Notebook",
1609 .type = AC97_TUNE_HP_ONLY,
1611 { } /* terminator */
1614 static int __devinit snd_via82xx_mixer_new(via82xx_t *chip, int ac97_quirk)
1620 memset(&bus, 0, sizeof(bus));
1621 bus.write = snd_via82xx_codec_write;
1622 bus.read = snd_via82xx_codec_read;
1623 bus.wait = snd_via82xx_codec_wait;
1624 bus.private_data = chip;
1625 bus.private_free = snd_via82xx_mixer_free_ac97_bus;
1626 bus.clock = chip->ac97_clock;
1627 if ((err = snd_ac97_bus(chip->card, &bus, &chip->ac97_bus)) < 0)
1630 memset(&ac97, 0, sizeof(ac97));
1631 ac97.private_data = chip;
1632 ac97.private_free = snd_via82xx_mixer_free_ac97;
1633 ac97.pci = chip->pci;
1634 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1637 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, ac97_quirk);
1639 if (chip->chip_type != TYPE_VIA686) {
1640 /* use slot 10/11 */
1641 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1651 static int snd_via8233_init_misc(via82xx_t *chip, int dev)
1656 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1657 for (i = 0; i < caps; i++) {
1658 snd_via8233_capture_source.index = i;
1659 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1663 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1666 if (chip->chip_type != TYPE_VIA8233A) {
1667 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1672 /* select spdif data slot 10/11 */
1673 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1674 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1675 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1680 static int snd_via686_init_misc(via82xx_t *chip, int dev)
1682 unsigned char legacy, legacy_cfg;
1685 legacy = chip->old_legacy;
1686 legacy_cfg = chip->old_legacy_cfg;
1687 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
1688 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
1689 if (chip->revision >= VIA_REV_686_H) {
1691 if (mpu_port[dev] >= 0x200) { /* force MIDI */
1692 mpu_port[dev] &= 0xfffc;
1693 pci_write_config_dword(chip->pci, 0x18, mpu_port[dev] | 0x01);
1695 chip->mpu_port_saved = mpu_port[dev];
1698 mpu_port[dev] = pci_resource_start(chip->pci, 2);
1701 switch (mpu_port[dev]) { /* force MIDI */
1706 legacy_cfg &= ~(3 << 2);
1707 legacy_cfg |= (mpu_port[dev] & 0x0030) >> 2;
1709 default: /* no, use BIOS settings */
1710 if (legacy & VIA_FUNC_ENABLE_MIDI)
1711 mpu_port[dev] = 0x300 + ((legacy_cfg & 0x000c) << 2);
1715 if (mpu_port[dev] >= 0x200 &&
1716 (chip->mpu_res = request_region(mpu_port[dev], 2, "VIA82xx MPU401")) != NULL) {
1718 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
1719 legacy |= VIA_FUNC_ENABLE_MIDI;
1722 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
1723 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1727 #ifdef SUPPORT_JOYSTICK
1728 #define JOYSTICK_ADDR 0x200
1729 if (joystick[dev] &&
1730 (chip->res_joystick = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport")) != NULL) {
1731 legacy |= VIA_FUNC_ENABLE_GAME;
1732 chip->gameport.io = JOYSTICK_ADDR;
1736 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1737 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1738 if (chip->mpu_res) {
1739 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
1741 chip->irq, 0, &chip->rmidi) < 0) {
1742 printk(KERN_WARNING "unable to initialize MPU-401 at 0x%lx, skipping\n", mpu_port[dev]);
1743 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1745 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
1747 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1750 #ifdef SUPPORT_JOYSTICK
1751 if (chip->res_joystick)
1752 gameport_register_port(&chip->gameport);
1756 chip->legacy_saved = legacy;
1757 chip->legacy_cfg_saved = legacy_cfg;
1767 static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1769 via82xx_t *chip = snd_magic_cast(via82xx_t, entry->private_data, return);
1772 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1773 for (i = 0; i < 0xa0; i += 4) {
1774 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
1778 static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
1780 snd_info_entry_t *entry;
1782 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
1783 snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
1790 static int __devinit snd_via82xx_chip_init(via82xx_t *chip)
1797 memset(&ac97, 0, sizeof(ac97));
1798 ac97.private_data = chip;
1800 #if 0 /* broken on K7M? */
1801 if (chip->chip_type == TYPE_VIA686)
1802 /* disable all legacy ports */
1803 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
1805 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1806 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
1807 /* deassert ACLink reset, force SYNC */
1808 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1809 VIA_ACLINK_CTRL_ENABLE |
1810 VIA_ACLINK_CTRL_RESET |
1811 VIA_ACLINK_CTRL_SYNC);
1813 #if 1 /* FIXME: should we do full reset here for all chip models? */
1814 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
1817 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
1818 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1819 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
1822 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
1823 /* note - FM data out has trouble with non VRA codecs !! */
1824 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1828 /* Make sure VRA is enabled, in case we didn't do a
1829 * complete codec reset, above */
1830 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
1831 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
1832 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
1833 /* note - FM data out has trouble with non VRA codecs !! */
1834 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1838 /* wait until codec ready */
1839 max_count = ((3 * HZ) / 4) + 1;
1841 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1842 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
1844 set_current_state(TASK_UNINTERRUPTIBLE);
1845 schedule_timeout(1);
1846 } while (--max_count > 0);
1848 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
1849 snd_printk("AC'97 codec is not ready [0x%x]\n", val);
1851 /* and then reset codec.. */
1852 snd_via82xx_codec_ready(chip, 0);
1853 snd_via82xx_codec_write(&ac97, AC97_RESET, 0x0000);
1854 snd_via82xx_codec_read(&ac97, 0);
1856 #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
1857 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1858 VIA_REG_AC97_SECONDARY_VALID |
1859 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1860 max_count = ((3 * HZ) / 4) + 1;
1861 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1862 VIA_REG_AC97_SECONDARY_VALID |
1863 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1865 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
1866 chip->ac97_secondary = 1;
1869 set_current_state(TASK_INTERRUPTIBLE);
1870 schedule_timeout(1);
1871 } while (--max_count > 0);
1872 /* This is ok, the most of motherboards have only one codec */
1877 if (chip->chip_type == TYPE_VIA686) {
1878 /* route FM trap to IRQ, disable FM trap */
1879 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
1880 /* disable all GPI interrupts */
1881 outl(0, VIAREG(chip, GPI_INTR));
1884 if (chip->chip_type != TYPE_VIA686) {
1885 /* Workaround for Award BIOS bug:
1886 * DXS channels don't work properly with VRA if MC97 is disabled.
1888 struct pci_dev *pci;
1889 pci = pci_find_device(0x1106, 0x3068, NULL); /* MC97 */
1892 pci_read_config_byte(pci, 0x44, &data);
1893 pci_write_config_byte(pci, 0x44, data | 0x40);
1904 static int snd_via82xx_suspend(snd_card_t *card, unsigned int state)
1906 via82xx_t *chip = snd_magic_cast(via82xx_t, card->pm_private_data, return -EINVAL);
1909 for (i = 0; i < 2; i++)
1911 snd_pcm_suspend_all(chip->pcms[i]);
1912 for (i = 0; i < chip->num_devs; i++)
1913 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1914 synchronize_irq(chip->irq);
1915 snd_ac97_suspend(chip->ac97);
1917 /* save misc values */
1918 if (chip->chip_type != TYPE_VIA686) {
1919 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
1920 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
1921 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
1924 pci_save_state(chip->pci, chip->pci_state);
1925 pci_set_power_state(chip->pci, 3);
1926 pci_disable_device(chip->pci);
1927 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1931 static int snd_via82xx_resume(snd_card_t *card, unsigned int state)
1933 via82xx_t *chip = snd_magic_cast(via82xx_t, card->pm_private_data, return -EINVAL);
1936 pci_enable_device(chip->pci);
1937 pci_restore_state(chip->pci, chip->pci_state);
1938 pci_set_power_state(chip->pci, 0);
1940 snd_via82xx_chip_init(chip);
1942 if (chip->chip_type == TYPE_VIA686) {
1943 if (chip->mpu_port_saved)
1944 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
1945 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
1946 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
1948 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
1949 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
1950 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
1951 for (idx = 0; idx < 4; idx++) {
1952 unsigned long port = chip->port + 0x10 * idx;
1953 for (i = 0; i < 2; i++)
1954 outb(chip->playback_volume[idx][i], port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1958 snd_ac97_resume(chip->ac97);
1960 for (i = 0; i < chip->num_devs; i++)
1961 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1963 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1966 #endif /* CONFIG_PM */
1968 static int snd_via82xx_free(via82xx_t *chip)
1974 /* disable interrupts */
1975 for (i = 0; i < chip->num_devs; i++)
1976 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1977 synchronize_irq(chip->irq);
1979 if (chip->mpu_res) {
1980 release_resource(chip->mpu_res);
1981 kfree_nocheck(chip->mpu_res);
1983 if (chip->res_port) {
1984 release_resource(chip->res_port);
1985 kfree_nocheck(chip->res_port);
1988 free_irq(chip->irq, (void *)chip);
1989 if (chip->chip_type == TYPE_VIA686) {
1990 #ifdef SUPPORT_JOYSTICK
1991 if (chip->res_joystick) {
1992 gameport_unregister_port(&chip->gameport);
1993 release_resource(chip->res_joystick);
1994 kfree_nocheck(chip->res_joystick);
1997 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
1998 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2000 snd_magic_kfree(chip);
2004 static int snd_via82xx_dev_free(snd_device_t *device)
2006 via82xx_t *chip = snd_magic_cast(via82xx_t, device->device_data, return -ENXIO);
2007 return snd_via82xx_free(chip);
2010 static int __devinit snd_via82xx_create(snd_card_t * card,
2011 struct pci_dev *pci,
2014 unsigned int ac97_clock,
2019 static snd_device_ops_t ops = {
2020 .dev_free = snd_via82xx_dev_free,
2023 if ((err = pci_enable_device(pci)) < 0)
2026 if ((chip = snd_magic_kcalloc(via82xx_t, 0, GFP_KERNEL)) == NULL)
2029 chip->chip_type = chip_type;
2030 chip->revision = revision;
2032 spin_lock_init(&chip->reg_lock);
2033 spin_lock_init(&chip->ac97_lock);
2034 spin_lock_init(&chip->rates[0].lock);
2035 spin_lock_init(&chip->rates[1].lock);
2040 memset(&chip->dma_dev, 0, sizeof(chip->dma_dev));
2041 chip->dma_dev.type = SNDRV_DMA_TYPE_DEV;
2042 chip->dma_dev.dev = snd_dma_pci_data(pci);
2044 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2045 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2047 chip->port = pci_resource_start(pci, 0);
2048 if ((chip->res_port = request_region(chip->port, 256, card->driver)) == NULL) {
2049 snd_printk("unable to grab ports 0x%lx-0x%lx\n", chip->port, chip->port + 256 - 1);
2050 snd_via82xx_free(chip);
2053 if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
2054 card->driver, (void *)chip)) {
2055 snd_printk("unable to grab IRQ %d\n", pci->irq);
2056 snd_via82xx_free(chip);
2059 chip->irq = pci->irq;
2060 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2061 chip->ac97_clock = ac97_clock;
2062 synchronize_irq(chip->irq);
2064 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2065 snd_via82xx_free(chip);
2069 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2070 snd_via82xx_free(chip);
2074 /* The 8233 ac97 controller does not implement the master bit
2075 * in the pci command register. IMHO this is a violation of the PCI spec.
2076 * We call pci_set_master here because it does not hurt. */
2077 pci_set_master(pci);
2079 snd_card_set_dev(card, &pci->dev);
2085 struct via823x_info {
2090 static struct via823x_info via823x_cards[] __devinitdata = {
2091 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2092 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2093 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2094 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2095 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2099 * auto detection of DXS channel supports.
2101 struct dxs_whitelist {
2102 unsigned short vendor;
2103 unsigned short device;
2104 unsigned short mask;
2105 short action; /* new dxs_support value */
2108 static int __devinit check_dxs_list(struct pci_dev *pci)
2110 static struct dxs_whitelist whitelist[] = {
2111 { .vendor = 0x1005, .device = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
2112 { .vendor = 0x1019, .device = 0x0996, .action = VIA_DXS_48K },
2113 { .vendor = 0x1019, .device = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
2114 { .vendor = 0x1043, .device = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
2115 { .vendor = 0x1043, .device = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
2116 { .vendor = 0x1043, .device = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
2117 { .vendor = 0x10cf, .device = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
2118 { .vendor = 0x1106, .device = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
2119 { .vendor = 0x1106, .device = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
2120 { .vendor = 0x1297, .device = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
2121 { .vendor = 0x1297, .device = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
2122 { .vendor = 0x1458, .device = 0xa002, .action = VIA_DXS_NO_VRA }, /* Gigabyte GA-7VAXP (FIXME: or DXS_ENABLE?) */
2123 { .vendor = 0x147b, .device = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
2124 { .vendor = 0x14ff, .device = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
2125 { .vendor = 0x1462, .device = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
2126 { .vendor = 0x1462, .device = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
2127 { .vendor = 0x1462, .device = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
2128 { .vendor = 0x1584, .device = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
2129 { .vendor = 0x161f, .device = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
2130 { .vendor = 0x161f, .device = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
2131 { .vendor = 0x1631, .device = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
2132 { .vendor = 0x1695, .device = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
2133 { .vendor = 0x1849, .device = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
2134 { } /* terminator */
2136 struct dxs_whitelist *w;
2137 unsigned short subsystem_vendor;
2138 unsigned short subsystem_device;
2140 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
2141 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
2143 for (w = whitelist; w->vendor; w++) {
2144 if (w->vendor != subsystem_vendor)
2147 if ((w->mask & subsystem_device) == w->device)
2150 if (subsystem_device == w->device)
2156 * not detected, try 48k rate only to be sure.
2158 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
2159 printk(KERN_INFO " Please try dxs_support=1 or dxs_support=4 option\n");
2160 printk(KERN_INFO " and report if it works on your machine.\n");
2164 static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2165 const struct pci_device_id *pci_id)
2170 unsigned char revision;
2171 int chip_type = 0, card_type;
2175 if (dev >= SNDRV_CARDS)
2182 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2186 card_type = pci_id->driver_data;
2187 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
2188 switch (card_type) {
2189 case TYPE_CARD_VIA686:
2190 strcpy(card->driver, "VIA686A");
2191 sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
2192 chip_type = TYPE_VIA686;
2194 case TYPE_CARD_VIA8233:
2195 chip_type = TYPE_VIA8233;
2196 sprintf(card->shortname, "VIA 823x rev%x", revision);
2197 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2198 if (revision == via823x_cards[i].revision) {
2199 chip_type = via823x_cards[i].type;
2200 strcpy(card->shortname, via823x_cards[i].name);
2204 if (chip_type != TYPE_VIA8233A) {
2205 if (dxs_support[dev] == VIA_DXS_AUTO)
2206 dxs_support[dev] = check_dxs_list(pci);
2207 /* force to use VIA8233 or 8233A model according to
2208 * dxs_support module option
2210 if (dxs_support[dev] == VIA_DXS_DISABLE)
2211 chip_type = TYPE_VIA8233A;
2213 chip_type = TYPE_VIA8233;
2215 if (chip_type == TYPE_VIA8233A)
2216 strcpy(card->driver, "VIA8233A");
2218 strcpy(card->driver, "VIA8233");
2221 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2226 if ((err = snd_via82xx_create(card, pci, chip_type, revision, ac97_clock[dev], &chip)) < 0)
2228 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk[dev])) < 0)
2231 if (chip_type == TYPE_VIA686) {
2232 if ((err = snd_via686_pcm_new(chip)) < 0 ||
2233 (err = snd_via686_init_misc(chip, dev)) < 0)
2236 if (chip_type == TYPE_VIA8233A) {
2237 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2239 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2241 if ((err = snd_via8233_pcm_new(chip)) < 0)
2243 if (dxs_support[dev] == VIA_DXS_48K)
2244 chip->dxs_fixed = 1;
2245 else if (dxs_support[dev] == VIA_DXS_NO_VRA)
2248 if ((err = snd_via8233_init_misc(chip, dev)) < 0)
2252 snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
2254 /* disable interrupts */
2255 for (i = 0; i < chip->num_devs; i++)
2256 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2258 sprintf(card->longname, "%s at 0x%lx, irq %d",
2259 card->shortname, chip->port, chip->irq);
2261 snd_via82xx_proc_init(chip);
2263 if ((err = snd_card_register(card)) < 0) {
2264 snd_card_free(card);
2267 pci_set_drvdata(pci, card);
2272 snd_card_free(card);
2276 static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2278 snd_card_free(pci_get_drvdata(pci));
2279 pci_set_drvdata(pci, NULL);
2282 static struct pci_driver driver = {
2283 .name = "VIA 82xx Audio",
2284 .id_table = snd_via82xx_ids,
2285 .probe = snd_via82xx_probe,
2286 .remove = __devexit_p(snd_via82xx_remove),
2287 SND_PCI_PM_CALLBACKS
2290 static int __init alsa_card_via82xx_init(void)
2292 return pci_module_init(&driver);
2295 static void __exit alsa_card_via82xx_exit(void)
2297 pci_unregister_driver(&driver);
2300 module_init(alsa_card_via82xx_init)
2301 module_exit(alsa_card_via82xx_exit)