source: GPL/trunk/alsa-kernel/pci/via82xx.c@ 83

Last change on this file since 83 was 77, checked in by vladest, 19 years ago

Provide sources to latest exist binary. See changelog for changes

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