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

Last change on this file since 239 was 135, checked in by stevenhl, 18 years ago

Get rid of spurious warnings

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