blob: d8705547dae106fb203193e0a3a8d892bbe48bb5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for VIA VT82xx (South Bridge)
3 *
4 * VT82C686A/B/C, VT8233A/C, VT8235
5 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02006 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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.
Karsten Wiese4f550df2005-10-18 14:31:07 +020044 *
45 * Sep. 26, 2005 Karsten Wiese <annabellesgarden@yahoo.de>
46 * - Optimize position calculation for the 823x chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50#include <linux/delay.h>
51#include <linux/interrupt.h>
52#include <linux/init.h>
53#include <linux/pci.h>
54#include <linux/slab.h>
55#include <linux/gameport.h>
56#include <linux/moduleparam.h>
57#include <sound/core.h>
58#include <sound/pcm.h>
59#include <sound/pcm_params.h>
60#include <sound/info.h>
Takashi Iwai7058c042006-08-21 18:44:54 +020061#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#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
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020070MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071MODULE_DESCRIPTION("VIA VT82xx audio");
72MODULE_LICENSE("GPL");
73MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
74
75#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
76#define SUPPORT_JOYSTICK 1
77#endif
78
Clemens Ladischb7fe4622005-10-04 08:46:51 +020079static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
80static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
81static long mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef SUPPORT_JOYSTICK
Clemens Ladischb7fe4622005-10-04 08:46:51 +020083static int joystick;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +020085static int ac97_clock = 48000;
86static char *ac97_quirk;
87static int dxs_support;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Clemens Ladischb7fe4622005-10-04 08:46:51 +020089module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020091module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020093module_param(mpu_port, long, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
95#ifdef SUPPORT_JOYSTICK
Clemens Ladischb7fe4622005-10-04 08:46:51 +020096module_param(joystick, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
98#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +020099module_param(ac97_clock, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200101module_param(ac97_quirk, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200103module_param(dxs_support, int, 0444);
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200104MODULE_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)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200106/* just for backward compatibility */
107static int enable;
Takashi Iwai698444f2005-10-20 16:53:49 +0200108module_param(enable, bool, 0444);
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* revision numbers for via686 */
112#define VIA_REV_686_A 0x10
113#define VIA_REV_686_B 0x11
114#define VIA_REV_686_C 0x12
115#define VIA_REV_686_D 0x13
116#define VIA_REV_686_E 0x14
117#define VIA_REV_686_H 0x20
118
119/* revision numbers for via8233 */
120#define VIA_REV_PRE_8233 0x10 /* not in market */
121#define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
122#define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
123#define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
124#define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
125#define VIA_REV_8237 0x60
Bastiaan Jacques8263c652006-04-18 17:04:04 +0200126#define VIA_REV_8251 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
129 * Direct registers
130 */
131
132#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
133#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
134
135/* common offsets */
136#define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
137#define VIA_REG_STAT_ACTIVE 0x80 /* RO */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200138#define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define VIA_REG_STAT_PAUSED 0x40 /* RO */
140#define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
141#define VIA_REG_STAT_STOPPED 0x04 /* RWC */
142#define VIA_REG_STAT_EOL 0x02 /* RWC */
143#define VIA_REG_STAT_FLAG 0x01 /* RWC */
144#define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
145#define VIA_REG_CTRL_START 0x80 /* WO */
146#define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
147#define VIA_REG_CTRL_AUTOSTART 0x20
148#define VIA_REG_CTRL_PAUSE 0x08 /* RW */
149#define VIA_REG_CTRL_INT_STOP 0x04
150#define VIA_REG_CTRL_INT_EOL 0x02
151#define VIA_REG_CTRL_INT_FLAG 0x01
152#define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
153#define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
154#define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
155#define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
156#define VIA_REG_TYPE_16BIT 0x20 /* RW */
157#define VIA_REG_TYPE_STEREO 0x10 /* RW */
158#define VIA_REG_TYPE_INT_LLINE 0x00
159#define VIA_REG_TYPE_INT_LSAMPLE 0x04
160#define VIA_REG_TYPE_INT_LESSONE 0x08
161#define VIA_REG_TYPE_INT_MASK 0x0c
162#define VIA_REG_TYPE_INT_EOL 0x02
163#define VIA_REG_TYPE_INT_FLAG 0x01
164#define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
165#define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
166#define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
167#define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
168#define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
169#define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
170#define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
171
172#define DEFINE_VIA_REGSET(name,val) \
173enum {\
174 VIA_REG_##name##_STATUS = (val),\
175 VIA_REG_##name##_CONTROL = (val) + 0x01,\
176 VIA_REG_##name##_TYPE = (val) + 0x02,\
177 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
178 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
179 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
180 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
181}
182
183/* playback block */
184DEFINE_VIA_REGSET(PLAYBACK, 0x00);
185DEFINE_VIA_REGSET(CAPTURE, 0x10);
186DEFINE_VIA_REGSET(FM, 0x20);
187
188/* AC'97 */
189#define VIA_REG_AC97 0x80 /* dword */
190#define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
191#define VIA_REG_AC97_CODEC_ID_SHIFT 30
192#define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
193#define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
194#define VIA_REG_AC97_SECONDARY_VALID (1<<27)
195#define VIA_REG_AC97_PRIMARY_VALID (1<<25)
196#define VIA_REG_AC97_BUSY (1<<24)
197#define VIA_REG_AC97_READ (1<<23)
198#define VIA_REG_AC97_CMD_SHIFT 16
199#define VIA_REG_AC97_CMD_MASK 0x7e
200#define VIA_REG_AC97_DATA_SHIFT 0
201#define VIA_REG_AC97_DATA_MASK 0xffff
202
203#define VIA_REG_SGD_SHADOW 0x84 /* dword */
204/* via686 */
205#define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
206#define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
207#define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
208#define VIA_REG_SGD_STAT_PB_EOL (1<<4)
209#define VIA_REG_SGD_STAT_CP_EOL (1<<5)
210#define VIA_REG_SGD_STAT_FM_EOL (1<<6)
211#define VIA_REG_SGD_STAT_PB_STOP (1<<8)
212#define VIA_REG_SGD_STAT_CP_STOP (1<<9)
213#define VIA_REG_SGD_STAT_FM_STOP (1<<10)
214#define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
215#define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
216#define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
217/* via8233 */
218#define VIA8233_REG_SGD_STAT_FLAG (1<<0)
219#define VIA8233_REG_SGD_STAT_EOL (1<<1)
220#define VIA8233_REG_SGD_STAT_STOP (1<<2)
221#define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
222#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
223#define VIA8233_REG_SGD_CHAN_SDX 0
224#define VIA8233_REG_SGD_CHAN_MULTI 4
225#define VIA8233_REG_SGD_CHAN_REC 6
226#define VIA8233_REG_SGD_CHAN_REC1 7
227
228#define VIA_REG_GPI_STATUS 0x88
229#define VIA_REG_GPI_INTR 0x8c
230
231/* multi-channel and capture registers for via8233 */
232DEFINE_VIA_REGSET(MULTPLAY, 0x40);
233DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
234
235/* via8233-specific registers */
236#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
237#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
238#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
239#define VIA_REG_MULTPLAY_FMT_8BIT 0x00
240#define VIA_REG_MULTPLAY_FMT_16BIT 0x80
241#define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
242#define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
243#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
244
245#define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
246
247#define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
248#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
249#define VIA_REG_CAPTURE_CHANNEL_LINE 0
250#define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
251
252#define VIA_TBL_BIT_FLAG 0x40000000
253#define VIA_TBL_BIT_EOL 0x80000000
254
255/* pci space */
256#define VIA_ACLINK_STAT 0x40
257#define VIA_ACLINK_C11_READY 0x20
258#define VIA_ACLINK_C10_READY 0x10
259#define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
260#define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
261#define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
262#define VIA_ACLINK_CTRL 0x41
263#define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
264#define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
265#define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
266#define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
267#define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
268#define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
269#define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
270#define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
271#define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
272 VIA_ACLINK_CTRL_RESET|\
273 VIA_ACLINK_CTRL_PCM|\
274 VIA_ACLINK_CTRL_VRA)
275#define VIA_FUNC_ENABLE 0x42
276#define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
277#define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
278#define VIA_FUNC_RX2C_WRITE 0x20
279#define VIA_FUNC_SB_FIFO_EMPTY 0x10
280#define VIA_FUNC_ENABLE_GAME 0x08
281#define VIA_FUNC_ENABLE_FM 0x04
282#define VIA_FUNC_ENABLE_MIDI 0x02
283#define VIA_FUNC_ENABLE_SB 0x01
284#define VIA_PNP_CONTROL 0x43
285#define VIA_FM_NMI_CTRL 0x48
286#define VIA8233_VOLCHG_CTRL 0x48
287#define VIA8233_SPDIF_CTRL 0x49
288#define VIA8233_SPDIF_DX3 0x08
289#define VIA8233_SPDIF_SLOT_MASK 0x03
290#define VIA8233_SPDIF_SLOT_1011 0x00
291#define VIA8233_SPDIF_SLOT_34 0x01
292#define VIA8233_SPDIF_SLOT_78 0x02
293#define VIA8233_SPDIF_SLOT_69 0x03
294
295/*
296 */
297
298#define VIA_DXS_AUTO 0
299#define VIA_DXS_ENABLE 1
300#define VIA_DXS_DISABLE 2
301#define VIA_DXS_48K 3
302#define VIA_DXS_NO_VRA 4
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200303#define VIA_DXS_SRC 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305
306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * pcm stream
308 */
309
310struct snd_via_sg_table {
311 unsigned int offset;
312 unsigned int size;
313} ;
314
315#define VIA_TABLE_SIZE 255
Takashi Iwai55036002008-07-30 15:23:06 +0200316#define VIA_MAX_BUFSIZE (1<<24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100318struct viadev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 unsigned int reg_offset;
320 unsigned long port;
321 int direction; /* playback = 0, capture = 1 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100322 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int running;
324 unsigned int tbl_entries; /* # descriptors */
325 struct snd_dma_buffer table;
326 struct snd_via_sg_table *idx_table;
327 /* for recovery from the unexpected pointer */
328 unsigned int lastpos;
329 unsigned int fragsize;
330 unsigned int bufsize;
331 unsigned int bufsize2;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200332 int hwptr_done; /* processed frame position in the buffer */
333 int in_interrupt;
334 int shadow_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337
338enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
339enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
340
341#define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
342
343struct via_rate_lock {
344 spinlock_t lock;
345 int rate;
346 int used;
347};
348
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100349struct via82xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int irq;
351
352 unsigned long port;
353 struct resource *mpu_res;
354 int chip_type;
355 unsigned char revision;
356
357 unsigned char old_legacy;
358 unsigned char old_legacy_cfg;
359#ifdef CONFIG_PM
360 unsigned char legacy_saved;
361 unsigned char legacy_cfg_saved;
362 unsigned char spdif_ctrl_saved;
363 unsigned char capture_src_saved[2];
364 unsigned int mpu_port_saved;
365#endif
366
Honza Maly00f226d2005-10-14 18:18:01 +0200367 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
368 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
371
372 struct pci_dev *pci;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100373 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 unsigned int num_devs;
376 unsigned int playback_devno, multi_devno, capture_devno;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100377 struct viadev devs[VIA_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct via_rate_lock rates[2]; /* playback and capture */
379 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
380 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200381 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
383
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100384 struct snd_pcm *pcms[2];
385 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100387 struct snd_ac97_bus *ac97_bus;
388 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned int ac97_clock;
390 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
391
392 spinlock_t reg_lock;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100393 struct snd_info_entry *proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395#ifdef SUPPORT_JOYSTICK
396 struct gameport *gameport;
397#endif
398};
399
Takashi Iwaif40b6892006-07-05 16:51:05 +0200400static struct pci_device_id snd_via82xx_ids[] = {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200401 /* 0x1106, 0x3058 */
402 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
403 /* 0x1106, 0x3059 */
404 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 { 0, }
406};
407
408MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
409
410/*
411 */
412
413/*
414 * allocate and initialize the descriptor buffers
415 * periods = number of periods
416 * fragsize = period size in bytes
417 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100418static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct pci_dev *pci,
420 unsigned int periods, unsigned int fragsize)
421{
422 unsigned int i, idx, ofs, rest;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100423 struct via82xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (dev->table.area == NULL) {
426 /* the start of each lists must be aligned to 8 bytes,
427 * but the kernel pages are much bigger, so we don't care
428 */
429 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
430 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
431 &dev->table) < 0)
432 return -ENOMEM;
433 }
434 if (! dev->idx_table) {
435 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
436 if (! dev->idx_table)
437 return -ENOMEM;
438 }
439
440 /* fill the entries */
441 idx = 0;
442 ofs = 0;
443 for (i = 0; i < periods; i++) {
444 rest = fragsize;
445 /* fill descriptors for a period.
446 * a period can be split to several descriptors if it's
447 * over page boundary.
448 */
449 do {
450 unsigned int r;
451 unsigned int flag;
Takashi Iwai77a23f22008-08-21 13:00:13 +0200452 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (idx >= VIA_TABLE_SIZE) {
455 snd_printk(KERN_ERR "via82xx: too much table size!\n");
456 return -EINVAL;
457 }
Takashi Iwai77a23f22008-08-21 13:00:13 +0200458 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
459 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
Takashi Iwai55036002008-07-30 15:23:06 +0200460 r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 rest -= r;
462 if (! rest) {
463 if (i == periods - 1)
464 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
465 else
466 flag = VIA_TBL_BIT_FLAG; /* period boundary */
467 } else
468 flag = 0; /* period continues to the next */
469 // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
470 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
471 dev->idx_table[idx].offset = ofs;
472 dev->idx_table[idx].size = r;
473 ofs += r;
474 idx++;
475 } while (rest > 0);
476 }
477 dev->tbl_entries = idx;
478 dev->bufsize = periods * fragsize;
479 dev->bufsize2 = dev->bufsize / 2;
480 dev->fragsize = fragsize;
481 return 0;
482}
483
484
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100485static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct pci_dev *pci)
487{
488 if (dev->table.area) {
489 snd_dma_free_pages(&dev->table);
490 dev->table.area = NULL;
491 }
Jesper Juhl4d572772005-05-30 17:30:32 +0200492 kfree(dev->idx_table);
493 dev->idx_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495}
496
497/*
498 * Basic I/O
499 */
500
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100501static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 return inl(VIAREG(chip, AC97));
504}
505
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100506static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 outl(val, VIAREG(chip, AC97));
509}
510
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100511static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 unsigned int timeout = 1000; /* 1ms */
514 unsigned int val;
515
516 while (timeout-- > 0) {
517 udelay(1);
518 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
519 return val & 0xffff;
520 }
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100521 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
522 secondary, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EIO;
524}
525
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100526static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 unsigned int timeout = 1000; /* 1ms */
529 unsigned int val, val1;
530 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
531 VIA_REG_AC97_SECONDARY_VALID;
532
533 while (timeout-- > 0) {
534 val = snd_via82xx_codec_xread(chip);
535 val1 = val & (VIA_REG_AC97_BUSY | stat);
536 if (val1 == stat)
537 return val & 0xffff;
538 udelay(1);
539 }
540 return -EIO;
541}
542
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100543static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100545 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int err;
547 err = snd_via82xx_codec_ready(chip, ac97->num);
548 /* here we need to wait fairly for long time.. */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +0200549 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100552static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned short reg,
554 unsigned short val)
555{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100556 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned int xval;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
560 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
561 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
562 xval |= val << VIA_REG_AC97_DATA_SHIFT;
563 snd_via82xx_codec_xwrite(chip, xval);
564 snd_via82xx_codec_ready(chip, ac97->num);
565}
566
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100567static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100569 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned int xval, val = 0xffff;
571 int again = 0;
572
573 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
574 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
575 xval |= VIA_REG_AC97_READ;
576 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
577 while (1) {
578 if (again++ > 3) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100579 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
580 ac97->num, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0xffff;
582 }
583 snd_via82xx_codec_xwrite(chip, xval);
584 udelay (20);
585 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
586 udelay(25);
587 val = snd_via82xx_codec_xread(chip);
588 break;
589 }
590 }
591 return val & 0xffff;
592}
593
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100594static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
597 VIADEV_REG(viadev, OFFSET_CONTROL));
598 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
599 udelay(50);
600 /* disable interrupts */
601 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
602 /* clear interrupts */
603 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
604 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
605 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
606 viadev->lastpos = 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200607 viadev->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610
611/*
612 * Interrupt handler
Karsten Wiese4f550df2005-10-18 14:31:07 +0200613 * Used for 686 and 8233A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
David Howells7d12e782006-10-05 14:55:46 +0100615static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100617 struct via82xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unsigned int status;
619 unsigned int i;
620
621 status = inl(VIAREG(chip, SGD_SHADOW));
622 if (! (status & chip->intr_mask)) {
623 if (chip->rmidi)
624 /* check mpu401 interrupt */
David Howells7d12e782006-10-05 14:55:46 +0100625 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return IRQ_NONE;
627 }
628
629 /* check status for each stream */
630 spin_lock(&chip->reg_lock);
631 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100632 struct viadev *viadev = &chip->devs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
Karsten Wiese4f550df2005-10-18 14:31:07 +0200634 if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 continue;
636 if (viadev->substream && viadev->running) {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200637 /*
638 * Update hwptr_done based on 'period elapsed'
639 * interrupts. We'll use it, when the chip returns 0
640 * for OFFSET_CURR_COUNT.
641 */
642 if (c_status & VIA_REG_STAT_EOL)
643 viadev->hwptr_done = 0;
644 else
645 viadev->hwptr_done += viadev->fragsize;
646 viadev->in_interrupt = c_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 spin_unlock(&chip->reg_lock);
648 snd_pcm_period_elapsed(viadev->substream);
649 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200650 viadev->in_interrupt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
653 }
654 spin_unlock(&chip->reg_lock);
655 return IRQ_HANDLED;
656}
657
658/*
Karsten Wiese4f550df2005-10-18 14:31:07 +0200659 * Interrupt handler
660 */
David Howells7d12e782006-10-05 14:55:46 +0100661static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
Karsten Wiese4f550df2005-10-18 14:31:07 +0200662{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100663 struct via82xx *chip = dev_id;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200664 unsigned int status;
665 unsigned int i;
666 int irqreturn = 0;
667
668 /* check status for each stream */
669 spin_lock(&chip->reg_lock);
670 status = inl(VIAREG(chip, SGD_SHADOW));
671
672 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100673 struct viadev *viadev = &chip->devs[i];
674 struct snd_pcm_substream *substream;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200675 unsigned char c_status, shadow_status;
676
677 shadow_status = (status >> viadev->shadow_shift) &
678 (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
679 VIA_REG_STAT_FLAG);
680 c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
681 if (!c_status)
682 continue;
683
684 substream = viadev->substream;
685 if (substream && viadev->running) {
686 /*
687 * Update hwptr_done based on 'period elapsed'
688 * interrupts. We'll use it, when the chip returns 0
689 * for OFFSET_CURR_COUNT.
690 */
691 if (c_status & VIA_REG_STAT_EOL)
692 viadev->hwptr_done = 0;
693 else
694 viadev->hwptr_done += viadev->fragsize;
695 viadev->in_interrupt = c_status;
696 if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
697 viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
698 spin_unlock(&chip->reg_lock);
699
700 snd_pcm_period_elapsed(substream);
701
702 spin_lock(&chip->reg_lock);
703 viadev->in_interrupt = 0;
704 }
705 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
706 irqreturn = 1;
707 }
708 spin_unlock(&chip->reg_lock);
709 return IRQ_RETVAL(irqreturn);
710}
711
712/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * PCM callbacks
714 */
715
716/*
717 * trigger callback
718 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100719static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100721 struct via82xx *chip = snd_pcm_substream_chip(substream);
722 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned char val;
724
725 if (chip->chip_type != TYPE_VIA686)
726 val = VIA_REG_CTRL_INT;
727 else
728 val = 0;
729 switch (cmd) {
730 case SNDRV_PCM_TRIGGER_START:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200731 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 val |= VIA_REG_CTRL_START;
733 viadev->running = 1;
734 break;
735 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200736 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 val = VIA_REG_CTRL_TERMINATE;
738 viadev->running = 0;
739 break;
740 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
741 val |= VIA_REG_CTRL_PAUSE;
742 viadev->running = 0;
743 break;
744 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
745 viadev->running = 1;
746 break;
747 default:
748 return -EINVAL;
749 }
750 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
751 if (cmd == SNDRV_PCM_TRIGGER_STOP)
752 snd_via82xx_channel_reset(chip, viadev);
753 return 0;
754}
755
756
757/*
758 * pointer callbacks
759 */
760
761/*
762 * calculate the linear position at the given sg-buffer index and the rest count
763 */
764
765#define check_invalid_pos(viadev,pos) \
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100766 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
767 viadev->lastpos < viadev->bufsize2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100769static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
770 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 unsigned int size, base, res;
773
774 size = viadev->idx_table[idx].size;
775 base = viadev->idx_table[idx].offset;
776 res = base + size - count;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200777 if (res >= viadev->bufsize)
778 res -= viadev->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* check the validity of the calculated position */
781 if (size < count) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100782 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
783 (int)size, (int)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 res = viadev->lastpos;
785 } else {
786 if (! count) {
787 /* Some mobos report count = 0 on the DMA boundary,
788 * i.e. count = size indeed.
789 * Let's check whether this step is above the expected size.
790 */
791 int delta = res - viadev->lastpos;
792 if (delta < 0)
793 delta += viadev->bufsize;
794 if ((unsigned int)delta > viadev->fragsize)
795 res = base;
796 }
797 if (check_invalid_pos(viadev, res)) {
798#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100799 printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
800 "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
801 "count = 0x%x\n", idx, viadev->tbl_entries,
802 viadev->lastpos, viadev->bufsize2,
803 viadev->idx_table[idx].offset,
804 viadev->idx_table[idx].size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#endif
806 /* count register returns full size when end of buffer is reached */
807 res = base + size;
808 if (check_invalid_pos(viadev, res)) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100809 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
810 "using last valid pointer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 res = viadev->lastpos;
812 }
813 }
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return res;
816}
817
818/*
819 * get the current pointer on via686
820 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100821static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100823 struct via82xx *chip = snd_pcm_substream_chip(substream);
824 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 unsigned int idx, ptr, count, res;
826
Takashi Iwaida3cec32008-08-08 17:12:14 +0200827 if (snd_BUG_ON(!viadev->tbl_entries))
828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
830 return 0;
831
832 spin_lock(&chip->reg_lock);
833 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
834 /* The via686a does not have the current index register,
835 * so we need to calculate the index from CURR_PTR.
836 */
837 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
838 if (ptr <= (unsigned int)viadev->table.addr)
839 idx = 0;
840 else /* CURR_PTR holds the address + 8 */
841 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
842 res = calc_linear_pos(viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200843 viadev->lastpos = res; /* remember the last position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 spin_unlock(&chip->reg_lock);
845
846 return bytes_to_frames(substream->runtime, res);
847}
848
849/*
850 * get the current pointer on via823x
851 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100852static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100854 struct via82xx *chip = snd_pcm_substream_chip(substream);
855 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 unsigned int idx, count, res;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200857 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Takashi Iwaida3cec32008-08-08 17:12:14 +0200859 if (snd_BUG_ON(!viadev->tbl_entries))
860 return 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200863 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
864 status = viadev->in_interrupt;
865 if (!status)
866 status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
867
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200868 /* An apparent bug in the 8251 is worked around by sending a
869 * REG_CTRL_START. */
870 if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
871 snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
872
Karsten Wiese4f550df2005-10-18 14:31:07 +0200873 if (!(status & VIA_REG_STAT_ACTIVE)) {
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200874 res = 0;
875 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Karsten Wiese4f550df2005-10-18 14:31:07 +0200877 if (count & 0xffffff) {
878 idx = count >> 24;
879 if (idx >= viadev->tbl_entries) {
880#ifdef POINTER_DEBUG
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100881 printk(KERN_DEBUG "fail: invalid idx = %i/%i\n", idx,
882 viadev->tbl_entries);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200883#endif
884 res = viadev->lastpos;
885 } else {
886 count &= 0xffffff;
887 res = calc_linear_pos(viadev, idx, count);
888 }
889 } else {
890 res = viadev->hwptr_done;
891 if (!viadev->in_interrupt) {
892 if (status & VIA_REG_STAT_EOL) {
893 res = 0;
894 } else
895 if (status & VIA_REG_STAT_FLAG) {
896 res += viadev->fragsize;
897 }
898 }
899 }
900unlock:
901 viadev->lastpos = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&chip->reg_lock);
903
904 return bytes_to_frames(substream->runtime, res);
905}
906
907
908/*
909 * hw_params callback:
910 * allocate the buffer and build up the buffer description table
911 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100912static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
913 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100915 struct via82xx *chip = snd_pcm_substream_chip(substream);
916 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int err;
918
919 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
920 if (err < 0)
921 return err;
922 err = build_via_table(viadev, substream, chip->pci,
923 params_periods(hw_params),
924 params_period_bytes(hw_params));
925 if (err < 0)
926 return err;
927
928 return 0;
929}
930
931/*
932 * hw_free callback:
933 * clean up the buffer description table and release the buffer
934 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100935static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100937 struct via82xx *chip = snd_pcm_substream_chip(substream);
938 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 clean_via_table(viadev, substream, chip->pci);
941 snd_pcm_lib_free_pages(substream);
942 return 0;
943}
944
945
946/*
947 * set up the table pointer
948 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100949static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 snd_via82xx_codec_ready(chip, 0);
952 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
953 udelay(20);
954 snd_via82xx_codec_ready(chip, 0);
955}
956
957/*
958 * prepare callback for playback and capture on via686
959 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100960static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
961 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 snd_via82xx_channel_reset(chip, viadev);
964 /* this must be set after channel_reset */
965 snd_via82xx_set_table_ptr(chip, viadev);
966 outb(VIA_REG_TYPE_AUTOSTART |
967 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
968 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
969 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
970 VIA_REG_TYPE_INT_EOL |
971 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
972}
973
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100974static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100976 struct via82xx *chip = snd_pcm_substream_chip(substream);
977 struct viadev *viadev = substream->runtime->private_data;
978 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
981 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
982 via686_setup_format(chip, viadev, runtime);
983 return 0;
984}
985
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100986static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100988 struct via82xx *chip = snd_pcm_substream_chip(substream);
989 struct viadev *viadev = substream->runtime->private_data;
990 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
993 via686_setup_format(chip, viadev, runtime);
994 return 0;
995}
996
997/*
998 * lock the current rate
999 */
1000static int via_lock_rate(struct via_rate_lock *rec, int rate)
1001{
1002 int changed = 0;
1003
1004 spin_lock_irq(&rec->lock);
1005 if (rec->rate != rate) {
1006 if (rec->rate && rec->used > 1) /* already set */
1007 changed = -EINVAL;
1008 else {
1009 rec->rate = rate;
1010 changed = 1;
1011 }
1012 }
1013 spin_unlock_irq(&rec->lock);
1014 return changed;
1015}
1016
1017/*
1018 * prepare callback for DSX playback on via823x
1019 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001020static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001022 struct via82xx *chip = snd_pcm_substream_chip(substream);
1023 struct viadev *viadev = substream->runtime->private_data;
1024 struct snd_pcm_runtime *runtime = substream->runtime;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001025 int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int rate_changed;
1027 u32 rbits;
1028
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001029 if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return rate_changed;
Takashi Iwai16d3f142005-08-15 15:02:28 +02001031 if (rate_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1033 chip->no_vra ? 48000 : runtime->rate);
Takashi Iwai16d3f142005-08-15 15:02:28 +02001034 if (chip->spdif_on && viadev->reg_offset == 0x30)
1035 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (runtime->rate == 48000)
1038 rbits = 0xfffff;
1039 else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001040 rbits = (0x100000 / 48000) * runtime->rate +
1041 ((0x100000 % 48000) * runtime->rate) / 48000;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001042 snd_BUG_ON(rbits & ~0xfffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 snd_via82xx_channel_reset(chip, viadev);
1044 snd_via82xx_set_table_ptr(chip, viadev);
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001045 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1046 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1047 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1048 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1050 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1051 rbits | /* rate */
1052 0xff000000, /* STOP index is never reached */
1053 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1054 udelay(20);
1055 snd_via82xx_codec_ready(chip, 0);
1056 return 0;
1057}
1058
1059/*
1060 * prepare callback for multi-channel playback on via823x
1061 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001062static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001064 struct via82xx *chip = snd_pcm_substream_chip(substream);
1065 struct viadev *viadev = substream->runtime->private_data;
1066 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 unsigned int slots;
1068 int fmt;
1069
1070 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1071 return -EINVAL;
1072 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1073 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1074 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1075 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1076 snd_via82xx_channel_reset(chip, viadev);
1077 snd_via82xx_set_table_ptr(chip, viadev);
1078
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001079 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1080 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 fmt |= runtime->channels << 4;
1082 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1083#if 0
1084 if (chip->revision == VIA_REV_8233A)
1085 slots = 0;
1086 else
1087#endif
1088 {
1089 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1090 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1091 switch (runtime->channels) {
1092 case 1: slots = (1<<0) | (1<<4); break;
1093 case 2: slots = (1<<0) | (2<<4); break;
1094 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1095 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1096 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1097 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1098 default: slots = 0; break;
1099 }
1100 }
1101 /* STOP index is never reached */
1102 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1103 udelay(20);
1104 snd_via82xx_codec_ready(chip, 0);
1105 return 0;
1106}
1107
1108/*
1109 * prepare callback for capture on via823x
1110 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001111static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001113 struct via82xx *chip = snd_pcm_substream_chip(substream);
1114 struct viadev *viadev = substream->runtime->private_data;
1115 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1118 return -EINVAL;
1119 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1120 snd_via82xx_channel_reset(chip, viadev);
1121 snd_via82xx_set_table_ptr(chip, viadev);
1122 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1123 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1124 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1125 0xff000000, /* STOP index is never reached */
1126 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1127 udelay(20);
1128 snd_via82xx_codec_ready(chip, 0);
1129 return 0;
1130}
1131
1132
1133/*
1134 * pcm hardware definition, identical for both playback and capture
1135 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001136static struct snd_pcm_hardware snd_via82xx_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1139 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1140 SNDRV_PCM_INFO_MMAP_VALID |
Jaroslav Kysela41e48452005-08-18 13:43:12 +02001141 /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 SNDRV_PCM_INFO_PAUSE),
1143 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1144 .rates = SNDRV_PCM_RATE_48000,
1145 .rate_min = 48000,
1146 .rate_max = 48000,
1147 .channels_min = 1,
1148 .channels_max = 2,
Takashi Iwai55036002008-07-30 15:23:06 +02001149 .buffer_bytes_max = VIA_MAX_BUFSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 .period_bytes_min = 32,
Takashi Iwai55036002008-07-30 15:23:06 +02001151 .period_bytes_max = VIA_MAX_BUFSIZE / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 .periods_min = 2,
1153 .periods_max = VIA_TABLE_SIZE / 2,
1154 .fifo_size = 0,
1155};
1156
1157
1158/*
1159 * open callback skeleton
1160 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001161static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1162 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001164 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int err;
1166 struct via_rate_lock *ratep;
1167
1168 runtime->hw = snd_via82xx_hw;
1169
1170 /* set the hw rate condition */
1171 ratep = &chip->rates[viadev->direction];
1172 spin_lock_irq(&ratep->lock);
1173 ratep->used++;
1174 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1175 /* DXS#3 and spdif is on */
1176 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1177 snd_pcm_limit_hw_rates(runtime);
1178 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1179 /* fixed DXS playback rate */
1180 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1181 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001182 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1183 /* use full SRC capabilities of DXS */
1184 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1185 SNDRV_PCM_RATE_8000_48000);
1186 runtime->hw.rate_min = 8000;
1187 runtime->hw.rate_max = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 } else if (! ratep->rate) {
1189 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1190 runtime->hw.rates = chip->ac97->rates[idx];
1191 snd_pcm_limit_hw_rates(runtime);
1192 } else {
1193 /* a fixed rate */
1194 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1195 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1196 }
1197 spin_unlock_irq(&ratep->lock);
1198
1199 /* we may remove following constaint when we modify table entries
1200 in interrupt */
1201 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1202 return err;
1203
1204 runtime->private_data = viadev;
1205 viadev->substream = substream;
1206
1207 return 0;
1208}
1209
1210
1211/*
1212 * open callback for playback on via686 and via823x DSX
1213 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001214static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001216 struct via82xx *chip = snd_pcm_substream_chip(substream);
1217 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int err;
1219
1220 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1221 return err;
1222 return 0;
1223}
1224
1225/*
1226 * open callback for playback on via823x multi-channel
1227 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001228static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001230 struct via82xx *chip = snd_pcm_substream_chip(substream);
1231 struct viadev *viadev = &chip->devs[chip->multi_devno];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err;
1233 /* channels constraint for VIA8233A
1234 * 3 and 5 channels are not supported
1235 */
1236 static unsigned int channels[] = {
1237 1, 2, 4, 6
1238 };
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001239 static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 .count = ARRAY_SIZE(channels),
1241 .list = channels,
1242 .mask = 0,
1243 };
1244
1245 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1246 return err;
1247 substream->runtime->hw.channels_max = 6;
1248 if (chip->revision == VIA_REV_8233A)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001249 snd_pcm_hw_constraint_list(substream->runtime, 0,
1250 SNDRV_PCM_HW_PARAM_CHANNELS,
1251 &hw_constraints_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
1255/*
1256 * open callback for capture on via686 and via823x
1257 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001258static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001260 struct via82xx *chip = snd_pcm_substream_chip(substream);
1261 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 return snd_via82xx_pcm_open(chip, viadev, substream);
1264}
1265
1266/*
1267 * close callback
1268 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001269static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001271 struct via82xx *chip = snd_pcm_substream_chip(substream);
1272 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct via_rate_lock *ratep;
1274
1275 /* release the rate lock */
1276 ratep = &chip->rates[viadev->direction];
1277 spin_lock_irq(&ratep->lock);
1278 ratep->used--;
1279 if (! ratep->used)
1280 ratep->rate = 0;
1281 spin_unlock_irq(&ratep->lock);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001282 if (! ratep->rate) {
1283 if (! viadev->direction) {
1284 snd_ac97_update_power(chip->ac97,
1285 AC97_PCM_FRONT_DAC_RATE, 0);
1286 snd_ac97_update_power(chip->ac97,
1287 AC97_PCM_SURR_DAC_RATE, 0);
1288 snd_ac97_update_power(chip->ac97,
1289 AC97_PCM_LFE_DAC_RATE, 0);
1290 } else
1291 snd_ac97_update_power(chip->ac97,
1292 AC97_PCM_LR_ADC_RATE, 0);
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 viadev->substream = NULL;
1295 return 0;
1296}
1297
1298
1299/* via686 playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001300static struct snd_pcm_ops snd_via686_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 .open = snd_via82xx_playback_open,
1302 .close = snd_via82xx_pcm_close,
1303 .ioctl = snd_pcm_lib_ioctl,
1304 .hw_params = snd_via82xx_hw_params,
1305 .hw_free = snd_via82xx_hw_free,
1306 .prepare = snd_via686_playback_prepare,
1307 .trigger = snd_via82xx_pcm_trigger,
1308 .pointer = snd_via686_pcm_pointer,
1309 .page = snd_pcm_sgbuf_ops_page,
1310};
1311
1312/* via686 capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001313static struct snd_pcm_ops snd_via686_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 .open = snd_via82xx_capture_open,
1315 .close = snd_via82xx_pcm_close,
1316 .ioctl = snd_pcm_lib_ioctl,
1317 .hw_params = snd_via82xx_hw_params,
1318 .hw_free = snd_via82xx_hw_free,
1319 .prepare = snd_via686_capture_prepare,
1320 .trigger = snd_via82xx_pcm_trigger,
1321 .pointer = snd_via686_pcm_pointer,
1322 .page = snd_pcm_sgbuf_ops_page,
1323};
1324
1325/* via823x DSX playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001326static struct snd_pcm_ops snd_via8233_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 .open = snd_via82xx_playback_open,
1328 .close = snd_via82xx_pcm_close,
1329 .ioctl = snd_pcm_lib_ioctl,
1330 .hw_params = snd_via82xx_hw_params,
1331 .hw_free = snd_via82xx_hw_free,
1332 .prepare = snd_via8233_playback_prepare,
1333 .trigger = snd_via82xx_pcm_trigger,
1334 .pointer = snd_via8233_pcm_pointer,
1335 .page = snd_pcm_sgbuf_ops_page,
1336};
1337
1338/* via823x multi-channel playback callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001339static struct snd_pcm_ops snd_via8233_multi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 .open = snd_via8233_multi_open,
1341 .close = snd_via82xx_pcm_close,
1342 .ioctl = snd_pcm_lib_ioctl,
1343 .hw_params = snd_via82xx_hw_params,
1344 .hw_free = snd_via82xx_hw_free,
1345 .prepare = snd_via8233_multi_prepare,
1346 .trigger = snd_via82xx_pcm_trigger,
1347 .pointer = snd_via8233_pcm_pointer,
1348 .page = snd_pcm_sgbuf_ops_page,
1349};
1350
1351/* via823x capture callbacks */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001352static struct snd_pcm_ops snd_via8233_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 .open = snd_via82xx_capture_open,
1354 .close = snd_via82xx_pcm_close,
1355 .ioctl = snd_pcm_lib_ioctl,
1356 .hw_params = snd_via82xx_hw_params,
1357 .hw_free = snd_via82xx_hw_free,
1358 .prepare = snd_via8233_capture_prepare,
1359 .trigger = snd_via82xx_pcm_trigger,
1360 .pointer = snd_via8233_pcm_pointer,
1361 .page = snd_pcm_sgbuf_ops_page,
1362};
1363
1364
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001365static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1366 int shadow_pos, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 chip->devs[idx].reg_offset = reg_offset;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001369 chip->devs[idx].shadow_shift = shadow_pos * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 chip->devs[idx].direction = direction;
1371 chip->devs[idx].port = chip->port + reg_offset;
1372}
1373
1374/*
1375 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1376 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001377static int __devinit snd_via8233_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001379 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int i, err;
1381
1382 chip->playback_devno = 0; /* x 4 */
1383 chip->multi_devno = 4; /* x 1 */
1384 chip->capture_devno = 5; /* x 2 */
1385 chip->num_devs = 7;
1386 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1387
1388 /* PCM #0: 4 DSX playbacks and 1 capture */
1389 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1390 if (err < 0)
1391 return err;
1392 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1393 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1394 pcm->private_data = chip;
1395 strcpy(pcm->name, chip->card->shortname);
1396 chip->pcms[0] = pcm;
1397 /* set up playbacks */
1398 for (i = 0; i < 4; i++)
Karsten Wiese4f550df2005-10-18 14:31:07 +02001399 init_viadev(chip, i, 0x10 * i, i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001401 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Takashi Iwaia2e39612008-08-21 14:17:38 +02001403 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1404 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001405 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /* PCM #1: multi-channel playback and 2nd capture */
1408 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1409 if (err < 0)
1410 return err;
1411 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1412 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1413 pcm->private_data = chip;
1414 strcpy(pcm->name, chip->card->shortname);
1415 chip->pcms[1] = pcm;
1416 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001417 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* set up capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001419 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Takashi Iwaia2e39612008-08-21 14:17:38 +02001421 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1422 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001423 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425}
1426
1427/*
1428 * create pcm instances for VIA8233A
1429 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001430static int __devinit snd_via8233a_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001432 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 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 */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001451 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001453 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Takashi Iwaia2e39612008-08-21 14:17:38 +02001455 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1456 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001457 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /* SPDIF supported? */
1460 if (! ac97_can_spdif(chip->ac97))
1461 return 0;
1462
1463 /* PCM #1: DXS3 playback (for spdif) */
1464 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1465 if (err < 0)
1466 return err;
1467 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1468 pcm->private_data = chip;
1469 strcpy(pcm->name, chip->card->shortname);
1470 chip->pcms[1] = pcm;
1471 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001472 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Takashi Iwaia2e39612008-08-21 14:17:38 +02001474 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1475 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001476 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478}
1479
1480/*
1481 * create a pcm instance for via686a/b
1482 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001483static int __devinit snd_via686_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001485 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int err;
1487
1488 chip->playback_devno = 0;
1489 chip->capture_devno = 1;
1490 chip->num_devs = 2;
1491 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1492
1493 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1494 if (err < 0)
1495 return err;
1496 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1497 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1498 pcm->private_data = chip;
1499 strcpy(pcm->name, chip->card->shortname);
1500 chip->pcms[0] = pcm;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001501 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1502 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Takashi Iwaia2e39612008-08-21 14:17:38 +02001504 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1505 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001506 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return 0;
1508}
1509
1510
1511/*
1512 * Mixer part
1513 */
1514
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001515static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 /* formerly they were "Line" and "Mic", but it looks like that they
1519 * have nothing to do with the actual physical connections...
1520 */
1521 static char *texts[2] = {
1522 "Input1", "Input2"
1523 };
1524 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1525 uinfo->count = 1;
1526 uinfo->value.enumerated.items = 2;
1527 if (uinfo->value.enumerated.item >= 2)
1528 uinfo->value.enumerated.item = 1;
1529 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1530 return 0;
1531}
1532
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001533static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001536 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1538 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1539 return 0;
1540}
1541
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001542static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1543 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001545 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1547 u8 val, oval;
1548
1549 spin_lock_irq(&chip->reg_lock);
1550 oval = inb(port);
1551 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1552 if (ucontrol->value.enumerated.item[0])
1553 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1554 if (val != oval)
1555 outb(val, port);
1556 spin_unlock_irq(&chip->reg_lock);
1557 return val != oval;
1558}
1559
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001560static struct snd_kcontrol_new snd_via8233_capture_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 .name = "Input Source Select",
1562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1563 .info = snd_via8233_capture_source_info,
1564 .get = snd_via8233_capture_source_get,
1565 .put = snd_via8233_capture_source_put,
1566};
1567
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001568#define snd_via8233_dxs3_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001570static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001573 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 u8 val;
1575
1576 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1577 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1578 return 0;
1579}
1580
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001581static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1582 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001584 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 u8 val, oval;
1586
1587 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1588 val = oval & ~VIA8233_SPDIF_DX3;
1589 if (ucontrol->value.integer.value[0])
1590 val |= VIA8233_SPDIF_DX3;
1591 /* save the spdif flag for rate filtering */
1592 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1593 if (val != oval) {
1594 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1595 return 1;
1596 }
1597 return 0;
1598}
1599
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001600static struct snd_kcontrol_new snd_via8233_dxs3_spdif_control __devinitdata = {
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001601 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1603 .info = snd_via8233_dxs3_spdif_info,
1604 .get = snd_via8233_dxs3_spdif_get,
1605 .put = snd_via8233_dxs3_spdif_put,
1606};
1607
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001608static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1609 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1612 uinfo->count = 2;
1613 uinfo->value.integer.min = 0;
1614 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1615 return 0;
1616}
1617
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001618static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001621 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001622 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1623
1624 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1625 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1626 return 0;
1627}
1628
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001629static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1630 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001631{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001632 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001633 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1634 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636}
1637
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001638static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001641 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001642 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1643 unsigned long port = chip->port + 0x10 * idx;
1644 unsigned char val;
1645 int i, change = 0;
1646
1647 for (i = 0; i < 2; i++) {
1648 val = ucontrol->value.integer.value[i];
1649 if (val > VIA_DXS_MAX_VOLUME)
1650 val = VIA_DXS_MAX_VOLUME;
1651 val = VIA_DXS_MAX_VOLUME - val;
1652 change |= val != chip->playback_volume[idx][i];
1653 if (change) {
1654 chip->playback_volume[idx][i] = val;
1655 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1656 }
1657 }
1658 return change;
1659}
1660
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001661static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001663{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001664 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 unsigned int idx;
1666 unsigned char val;
1667 int i, change = 0;
1668
1669 for (i = 0; i < 2; i++) {
1670 val = ucontrol->value.integer.value[i];
1671 if (val > VIA_DXS_MAX_VOLUME)
1672 val = VIA_DXS_MAX_VOLUME;
1673 val = VIA_DXS_MAX_VOLUME - val;
Honza Maly00f226d2005-10-14 18:18:01 +02001674 if (val != chip->playback_volume_c[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 change = 1;
Honza Maly00f226d2005-10-14 18:18:01 +02001676 chip->playback_volume_c[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 for (idx = 0; idx < 4; idx++) {
1678 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02001679 chip->playback_volume[idx][i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1681 }
1682 }
1683 }
1684 return change;
1685}
1686
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001687static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -9450, 150, 1);
Takashi Iwai7058c042006-08-21 18:44:54 +02001688
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001689static struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 .name = "PCM Playback Volume",
1691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwai7058c042006-08-21 18:44:54 +02001692 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1693 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 .info = snd_via8233_dxs_volume_info,
Honza Maly00f226d2005-10-14 18:18:01 +02001695 .get = snd_via8233_pcmdxs_volume_get,
1696 .put = snd_via8233_pcmdxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001697 .tlv = { .p = db_scale_dxs }
Honza Maly00f226d2005-10-14 18:18:01 +02001698};
1699
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001700static struct snd_kcontrol_new snd_via8233_dxs_volume_control __devinitdata = {
Honza Maly00f226d2005-10-14 18:18:01 +02001701 .name = "VIA DXS Playback Volume",
1702 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwai7058c042006-08-21 18:44:54 +02001703 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1704 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Honza Maly00f226d2005-10-14 18:18:01 +02001705 .count = 4,
1706 .info = snd_via8233_dxs_volume_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 .get = snd_via8233_dxs_volume_get,
1708 .put = snd_via8233_dxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001709 .tlv = { .p = db_scale_dxs }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710};
1711
1712/*
1713 */
1714
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001715static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001717 struct via82xx *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 chip->ac97_bus = NULL;
1719}
1720
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001721static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001723 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 chip->ac97 = NULL;
1725}
1726
1727static struct ac97_quirk ac97_quirks[] = {
1728 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001729 .subvendor = 0x1106,
1730 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 .codec_id = 0x56494161, /* VT1612A */
1732 .name = "Soltek SL-75DRV5",
1733 .type = AC97_TUNE_NONE
1734 },
1735 { /* FIXME: which codec? */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001736 .subvendor = 0x1106,
1737 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 .name = "ASRock K7VT2",
1739 .type = AC97_TUNE_HP_ONLY
1740 },
1741 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001742 .subvendor = 0x1019,
1743 .subdevice = 0x0a81,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 .name = "ECS K7VTA3",
1745 .type = AC97_TUNE_HP_ONLY
1746 },
1747 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001748 .subvendor = 0x1019,
1749 .subdevice = 0x0a85,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 .name = "ECS L7VMM2",
1751 .type = AC97_TUNE_HP_ONLY
1752 },
1753 {
Walter Sheets942fd1e2008-06-27 11:53:31 +02001754 .subvendor = 0x1019,
1755 .subdevice = 0x1841,
1756 .name = "ECS K7VTA3",
1757 .type = AC97_TUNE_HP_ONLY
1758 },
1759 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001760 .subvendor = 0x1849,
1761 .subdevice = 0x3059,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 .name = "ASRock K7VM2",
1763 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1764 },
1765 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001766 .subvendor = 0x14cd,
1767 .subdevice = 0x7002,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 .name = "Unknown",
1769 .type = AC97_TUNE_ALC_JACK
1770 },
1771 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001772 .subvendor = 0x1071,
1773 .subdevice = 0x8590,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 .name = "Mitac Mobo",
1775 .type = AC97_TUNE_ALC_JACK
1776 },
1777 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001778 .subvendor = 0x161f,
1779 .subdevice = 0x202b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 .name = "Arima Notebook",
1781 .type = AC97_TUNE_HP_ONLY,
1782 },
Takashi Iwaidac8ddd2005-12-14 12:00:46 +01001783 {
1784 .subvendor = 0x161f,
1785 .subdevice = 0x2032,
1786 .name = "Targa Traveller 811",
1787 .type = AC97_TUNE_HP_ONLY,
1788 },
Daniel T Chend4199f02006-06-16 16:21:54 +02001789 {
1790 .subvendor = 0x161f,
1791 .subdevice = 0x2032,
1792 .name = "m680x",
1793 .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1794 },
Takashi Iwai96745132007-10-25 11:46:24 +02001795 {
1796 .subvendor = 0x1297,
1797 .subdevice = 0xa232,
1798 .name = "Shuttle AK32VN",
1799 .type = AC97_TUNE_HP_ONLY
1800 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 { } /* terminator */
1802};
1803
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001804static int __devinit snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001806 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001808 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 .write = snd_via82xx_codec_write,
1810 .read = snd_via82xx_codec_read,
1811 .wait = snd_via82xx_codec_wait,
1812 };
1813
1814 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1815 return err;
1816 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1817 chip->ac97_bus->clock = chip->ac97_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 memset(&ac97, 0, sizeof(ac97));
1820 ac97.private_data = chip;
1821 ac97.private_free = snd_via82xx_mixer_free_ac97;
1822 ac97.pci = chip->pci;
Takashi Iwaif1a63a32006-10-24 18:25:29 +02001823 ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1825 return err;
1826
1827 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1828
1829 if (chip->chip_type != TYPE_VIA686) {
1830 /* use slot 10/11 */
1831 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1832 }
1833
1834 return 0;
1835}
1836
1837#ifdef SUPPORT_JOYSTICK
1838#define JOYSTICK_ADDR 0x200
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001839static int __devinit snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct gameport *gp;
1842 struct resource *r;
1843
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001844 if (!joystick)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return -ENODEV;
1846
1847 r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1848 if (!r) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001849 printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n",
1850 JOYSTICK_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return -EBUSY;
1852 }
1853
1854 chip->gameport = gp = gameport_allocate_port();
1855 if (!gp) {
1856 printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02001857 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return -ENOMEM;
1859 }
1860
1861 gameport_set_name(gp, "VIA686 Gameport");
1862 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1863 gameport_set_dev_parent(gp, &chip->pci->dev);
1864 gp->io = JOYSTICK_ADDR;
1865 gameport_set_port_data(gp, r);
1866
1867 /* Enable legacy joystick port */
1868 *legacy |= VIA_FUNC_ENABLE_GAME;
1869 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1870
1871 gameport_register_port(chip->gameport);
1872
1873 return 0;
1874}
1875
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001876static void snd_via686_free_gameport(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878 if (chip->gameport) {
1879 struct resource *r = gameport_get_port_data(chip->gameport);
1880
1881 gameport_unregister_port(chip->gameport);
1882 chip->gameport = NULL;
Takashi Iwaib1d57762005-10-10 11:56:31 +02001883 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885}
1886#else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001887static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 return -ENOSYS;
1890}
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001891static inline void snd_via686_free_gameport(struct via82xx *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892#endif
1893
1894
1895/*
1896 *
1897 */
1898
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001899static int __devinit snd_via8233_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 int i, err, caps;
1902 unsigned char val;
1903
1904 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1905 for (i = 0; i < caps; i++) {
1906 snd_via8233_capture_source.index = i;
1907 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1908 if (err < 0)
1909 return err;
1910 }
1911 if (ac97_can_spdif(chip->ac97)) {
1912 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1913 if (err < 0)
1914 return err;
1915 }
1916 if (chip->chip_type != TYPE_VIA8233A) {
1917 /* when no h/w PCM volume control is found, use DXS volume control
1918 * as the PCM vol control
1919 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001920 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 memset(&sid, 0, sizeof(sid));
1922 strcpy(sid.name, "PCM Playback Volume");
1923 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1924 if (! snd_ctl_find_id(chip->card, &sid)) {
Honza Maly00f226d2005-10-14 18:18:01 +02001925 snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
1926 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1927 if (err < 0)
1928 return err;
1929 }
1930 else /* Using DXS when PCM emulation is enabled is really weird */
1931 {
1932 /* Standalone DXS controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1934 if (err < 0)
1935 return err;
1936 }
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /* select spdif data slot 10/11 */
1939 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1940 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1941 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
1942 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1943
1944 return 0;
1945}
1946
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001947static int __devinit snd_via686_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 unsigned char legacy, legacy_cfg;
1950 int rev_h = 0;
1951
1952 legacy = chip->old_legacy;
1953 legacy_cfg = chip->old_legacy_cfg;
1954 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
1955 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
1956 if (chip->revision >= VIA_REV_686_H) {
1957 rev_h = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001958 if (mpu_port >= 0x200) { /* force MIDI */
1959 mpu_port &= 0xfffc;
1960 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961#ifdef CONFIG_PM
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001962 chip->mpu_port_saved = mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963#endif
1964 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001965 mpu_port = pci_resource_start(chip->pci, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001968 switch (mpu_port) { /* force MIDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 case 0x300:
1970 case 0x310:
1971 case 0x320:
1972 case 0x330:
1973 legacy_cfg &= ~(3 << 2);
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001974 legacy_cfg |= (mpu_port & 0x0030) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 break;
1976 default: /* no, use BIOS settings */
1977 if (legacy & VIA_FUNC_ENABLE_MIDI)
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001978 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break;
1980 }
1981 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001982 if (mpu_port >= 0x200 &&
1983 (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
1984 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (rev_h)
1986 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
1987 legacy |= VIA_FUNC_ENABLE_MIDI;
1988 } else {
1989 if (rev_h)
1990 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
1991 legacy &= ~VIA_FUNC_ENABLE_MIDI;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001992 mpu_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994
1995 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1996 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1997 if (chip->mpu_res) {
1998 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
Takashi Iwai302e4c22006-05-23 13:24:30 +02001999 mpu_port, MPU401_INFO_INTEGRATED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 chip->irq, 0, &chip->rmidi) < 0) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002001 printk(KERN_WARNING "unable to initialize MPU-401"
2002 " at 0x%lx, skipping\n", mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 legacy &= ~VIA_FUNC_ENABLE_MIDI;
2004 } else {
2005 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
2006 }
2007 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2008 }
2009
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002010 snd_via686_create_gameport(chip, &legacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012#ifdef CONFIG_PM
2013 chip->legacy_saved = legacy;
2014 chip->legacy_cfg_saved = legacy_cfg;
2015#endif
2016
2017 return 0;
2018}
2019
2020
2021/*
2022 * proc interface
2023 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002024static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2025 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002027 struct via82xx *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 int i;
2029
2030 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2031 for (i = 0; i < 0xa0; i += 4) {
2032 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2033 }
2034}
2035
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002036static void __devinit snd_via82xx_proc_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002038 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002041 snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
2044/*
2045 *
2046 */
2047
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002048static int snd_via82xx_chip_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 unsigned int val;
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002051 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 unsigned char pval;
2053
2054#if 0 /* broken on K7M? */
2055 if (chip->chip_type == TYPE_VIA686)
2056 /* disable all legacy ports */
2057 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2058#endif
2059 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2060 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2061 /* deassert ACLink reset, force SYNC */
2062 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2063 VIA_ACLINK_CTRL_ENABLE |
2064 VIA_ACLINK_CTRL_RESET |
2065 VIA_ACLINK_CTRL_SYNC);
2066 udelay(100);
2067#if 1 /* FIXME: should we do full reset here for all chip models? */
2068 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2069 udelay(100);
2070#else
2071 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2072 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2073 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2074 udelay(2);
2075#endif
2076 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2077 /* note - FM data out has trouble with non VRA codecs !! */
2078 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2079 udelay(100);
2080 }
2081
2082 /* Make sure VRA is enabled, in case we didn't do a
2083 * complete codec reset, above */
2084 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2085 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2086 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2087 /* note - FM data out has trouble with non VRA codecs !! */
2088 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2089 udelay(100);
2090 }
2091
2092 /* wait until codec ready */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002093 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 do {
2095 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2096 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2097 break;
Rene Hermand86d0192007-09-18 18:10:49 +02002098 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002099 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
Takashi Iwai99b359b2005-10-20 18:26:44 +02002102 snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2105 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2106 VIA_REG_AC97_SECONDARY_VALID |
2107 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002108 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2110 VIA_REG_AC97_SECONDARY_VALID |
2111 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2112 do {
2113 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2114 chip->ac97_secondary = 1;
2115 goto __ac97_ok2;
2116 }
Rene Hermand86d0192007-09-18 18:10:49 +02002117 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002118 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* This is ok, the most of motherboards have only one codec */
2120
2121 __ac97_ok2:
2122#endif
2123
2124 if (chip->chip_type == TYPE_VIA686) {
2125 /* route FM trap to IRQ, disable FM trap */
2126 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2127 /* disable all GPI interrupts */
2128 outl(0, VIAREG(chip, GPI_INTR));
2129 }
2130
2131 if (chip->chip_type != TYPE_VIA686) {
2132 /* Workaround for Award BIOS bug:
2133 * DXS channels don't work properly with VRA if MC97 is disabled.
2134 */
2135 struct pci_dev *pci;
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002136 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (pci) {
2138 unsigned char data;
2139 pci_read_config_byte(pci, 0x44, &data);
2140 pci_write_config_byte(pci, 0x44, data | 0x40);
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002141 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 }
2144
2145 if (chip->chip_type != TYPE_VIA8233A) {
2146 int i, idx;
2147 for (idx = 0; idx < 4; idx++) {
2148 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02002149 for (i = 0; i < 2; i++) {
2150 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002151 outb(chip->playback_volume_c[i],
2152 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
Honza Maly00f226d2005-10-14 18:18:01 +02002153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155 }
2156
2157 return 0;
2158}
2159
2160#ifdef CONFIG_PM
2161/*
2162 * power management
2163 */
Takashi Iwai57feb832005-11-17 16:10:35 +01002164static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Takashi Iwai57feb832005-11-17 16:10:35 +01002166 struct snd_card *card = pci_get_drvdata(pci);
2167 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int i;
2169
Takashi Iwai57feb832005-11-17 16:10:35 +01002170 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 for (i = 0; i < 2; i++)
Takashi Iwai57feb832005-11-17 16:10:35 +01002172 snd_pcm_suspend_all(chip->pcms[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 for (i = 0; i < chip->num_devs; i++)
2174 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2175 synchronize_irq(chip->irq);
2176 snd_ac97_suspend(chip->ac97);
2177
2178 /* save misc values */
2179 if (chip->chip_type != TYPE_VIA686) {
2180 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2181 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2182 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2183 }
2184
Takashi Iwai57feb832005-11-17 16:10:35 +01002185 pci_disable_device(pci);
2186 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002187 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return 0;
2189}
2190
Takashi Iwai57feb832005-11-17 16:10:35 +01002191static int snd_via82xx_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Takashi Iwai57feb832005-11-17 16:10:35 +01002193 struct snd_card *card = pci_get_drvdata(pci);
2194 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 int i;
2196
Takashi Iwai57feb832005-11-17 16:10:35 +01002197 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02002198 pci_restore_state(pci);
2199 if (pci_enable_device(pci) < 0) {
2200 printk(KERN_ERR "via82xx: pci_enable_device failed, "
2201 "disabling device\n");
2202 snd_card_disconnect(card);
2203 return -EIO;
2204 }
2205 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 snd_via82xx_chip_init(chip);
2208
2209 if (chip->chip_type == TYPE_VIA686) {
2210 if (chip->mpu_port_saved)
2211 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2212 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2213 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2214 } else {
2215 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2216 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2217 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2218 }
2219
2220 snd_ac97_resume(chip->ac97);
2221
2222 for (i = 0; i < chip->num_devs; i++)
2223 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2224
Takashi Iwai57feb832005-11-17 16:10:35 +01002225 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227}
2228#endif /* CONFIG_PM */
2229
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002230static int snd_via82xx_free(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 unsigned int i;
2233
2234 if (chip->irq < 0)
2235 goto __end_hw;
2236 /* disable interrupts */
2237 for (i = 0; i < chip->num_devs; i++)
2238 snd_via82xx_channel_reset(chip, &chip->devs[i]);
Jeff Garzikf000fd82008-04-22 13:50:34 +02002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (chip->irq >= 0)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002241 free_irq(chip->irq, chip);
Marcin Åšlusarz757d5a72007-12-14 12:51:24 +01002242 __end_hw:
Takashi Iwaib1d57762005-10-10 11:56:31 +02002243 release_and_free_resource(chip->mpu_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 pci_release_regions(chip->pci);
2245
2246 if (chip->chip_type == TYPE_VIA686) {
2247 snd_via686_free_gameport(chip);
2248 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2249 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2250 }
2251 pci_disable_device(chip->pci);
2252 kfree(chip);
2253 return 0;
2254}
2255
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002256static int snd_via82xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002258 struct via82xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return snd_via82xx_free(chip);
2260}
2261
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002262static int __devinit snd_via82xx_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 struct pci_dev *pci,
2264 int chip_type,
2265 int revision,
2266 unsigned int ac97_clock,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002267 struct via82xx ** r_via)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002269 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002271 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 .dev_free = snd_via82xx_dev_free,
2273 };
2274
2275 if ((err = pci_enable_device(pci)) < 0)
2276 return err;
2277
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002278 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 pci_disable_device(pci);
2280 return -ENOMEM;
2281 }
2282
2283 chip->chip_type = chip_type;
2284 chip->revision = revision;
2285
2286 spin_lock_init(&chip->reg_lock);
2287 spin_lock_init(&chip->rates[0].lock);
2288 spin_lock_init(&chip->rates[1].lock);
2289 chip->card = card;
2290 chip->pci = pci;
2291 chip->irq = -1;
2292
2293 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2294 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2295 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2296 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2297
2298 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2299 kfree(chip);
2300 pci_disable_device(pci);
2301 return err;
2302 }
2303 chip->port = pci_resource_start(pci, 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +02002304 if (request_irq(pci->irq,
2305 chip_type == TYPE_VIA8233 ?
2306 snd_via8233_interrupt : snd_via686_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002307 IRQF_SHARED,
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002308 card->driver, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002309 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 snd_via82xx_free(chip);
2311 return -EBUSY;
2312 }
2313 chip->irq = pci->irq;
2314 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2315 chip->ac97_clock = ac97_clock;
2316 synchronize_irq(chip->irq);
2317
2318 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2319 snd_via82xx_free(chip);
2320 return err;
2321 }
2322
2323 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2324 snd_via82xx_free(chip);
2325 return err;
2326 }
2327
2328 /* The 8233 ac97 controller does not implement the master bit
2329 * in the pci command register. IMHO this is a violation of the PCI spec.
2330 * We call pci_set_master here because it does not hurt. */
2331 pci_set_master(pci);
2332
2333 snd_card_set_dev(card, &pci->dev);
2334
2335 *r_via = chip;
2336 return 0;
2337}
2338
2339struct via823x_info {
2340 int revision;
2341 char *name;
2342 int type;
2343};
2344static struct via823x_info via823x_cards[] __devinitdata = {
2345 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2346 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2347 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2348 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2349 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2350 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
Bastiaan Jacques8263c652006-04-18 17:04:04 +02002351 { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352};
2353
2354/*
2355 * auto detection of DXS channel supports.
2356 */
Takashi Iwai9d749582006-11-24 15:37:18 +01002357
2358static struct snd_pci_quirk dxs_whitelist[] __devinitdata = {
2359 SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2360 SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2361 SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2362 SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
2363 SND_PCI_QUIRK(0x1019, 0, "ESC K8", VIA_DXS_SRC),
2364 SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2365 SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2366 SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
2367 SND_PCI_QUIRK(0x1043, 0, "ASUS A7/A8", VIA_DXS_NO_VRA),
2368 SND_PCI_QUIRK(0x1071, 0, "Diverse Notebook", VIA_DXS_NO_VRA),
2369 SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
2370 SND_PCI_QUIRK(0x1106, 0, "ASRock", VIA_DXS_SRC),
Takashi Iwai11be2652007-10-11 14:35:00 +02002371 SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
Takashi Iwai96745132007-10-25 11:46:24 +02002372 SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2373 SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002374 SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2375 SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2376 SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2377 SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
2378 SND_PCI_QUIRK(0x1462, 0, "MSI Mobo", VIA_DXS_SRC),
2379 SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2380 SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2381 SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2382 SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2383 SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2384 SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2385 SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2386 SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2387 SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2388 SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2389 SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2390 SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2391 SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
2392 SND_PCI_QUIRK(0x1695, 0, "EPoX mobo", VIA_DXS_SRC),
2393 SND_PCI_QUIRK(0x16f3, 0, "Jetway K8", VIA_DXS_SRC),
2394 SND_PCI_QUIRK(0x1734, 0, "FSC Laptop", VIA_DXS_SRC),
2395 SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
2396 SND_PCI_QUIRK(0x1849, 0, "ASRock mobo", VIA_DXS_SRC),
2397 SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8", VIA_DXS_NO_VRA),
2398 SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2399 { } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400};
2401
Takashi Iwaia7695772006-04-27 16:56:07 +02002402static int __devinit check_dxs_list(struct pci_dev *pci, int revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Takashi Iwai9d749582006-11-24 15:37:18 +01002404 const struct snd_pci_quirk *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Takashi Iwai9d749582006-11-24 15:37:18 +01002406 w = snd_pci_quirk_lookup(pci, dxs_whitelist);
2407 if (w) {
2408 snd_printdd(KERN_INFO "via82xx: DXS white list for %s found\n",
2409 w->name);
2410 return w->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412
Takashi Iwaia7695772006-04-27 16:56:07 +02002413 /* for newer revision, default to DXS_SRC */
2414 if (revision >= VIA_REV_8235)
2415 return VIA_DXS_SRC;
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 /*
2418 * not detected, try 48k rate only to be sure.
2419 */
2420 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002421 printk(KERN_INFO " Please try dxs_support=5 option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 printk(KERN_INFO " and report if it works on your machine.\n");
Takashi Iwaiee3b4c62005-06-14 10:18:20 +02002423 printk(KERN_INFO " For more details, read ALSA-Configuration.txt.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return VIA_DXS_48K;
2425};
2426
2427static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2428 const struct pci_device_id *pci_id)
2429{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002430 struct snd_card *card;
2431 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 int chip_type = 0, card_type;
2433 unsigned int i;
2434 int err;
2435
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002436 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
2437 if (err < 0)
2438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 card_type = pci_id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 switch (card_type) {
2442 case TYPE_CARD_VIA686:
2443 strcpy(card->driver, "VIA686A");
Auke Kok44c10132007-06-08 15:46:36 -07002444 sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 chip_type = TYPE_VIA686;
2446 break;
2447 case TYPE_CARD_VIA8233:
2448 chip_type = TYPE_VIA8233;
Auke Kok44c10132007-06-08 15:46:36 -07002449 sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
Auke Kok44c10132007-06-08 15:46:36 -07002451 if (pci->revision == via823x_cards[i].revision) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 chip_type = via823x_cards[i].type;
2453 strcpy(card->shortname, via823x_cards[i].name);
2454 break;
2455 }
2456 }
2457 if (chip_type != TYPE_VIA8233A) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002458 if (dxs_support == VIA_DXS_AUTO)
Auke Kok44c10132007-06-08 15:46:36 -07002459 dxs_support = check_dxs_list(pci, pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 /* force to use VIA8233 or 8233A model according to
2461 * dxs_support module option
2462 */
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002463 if (dxs_support == VIA_DXS_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 chip_type = TYPE_VIA8233A;
2465 else
2466 chip_type = TYPE_VIA8233;
2467 }
2468 if (chip_type == TYPE_VIA8233A)
2469 strcpy(card->driver, "VIA8233A");
Auke Kok44c10132007-06-08 15:46:36 -07002470 else if (pci->revision >= VIA_REV_8237)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2472 else
2473 strcpy(card->driver, "VIA8233");
2474 break;
2475 default:
2476 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2477 err = -EINVAL;
2478 goto __error;
2479 }
2480
Auke Kok44c10132007-06-08 15:46:36 -07002481 if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002482 ac97_clock, &chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 goto __error;
Takashi Iwai57feb832005-11-17 16:10:35 +01002484 card->private_data = chip;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002485 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 goto __error;
2487
2488 if (chip_type == TYPE_VIA686) {
2489 if ((err = snd_via686_pcm_new(chip)) < 0 ||
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002490 (err = snd_via686_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto __error;
2492 } else {
2493 if (chip_type == TYPE_VIA8233A) {
2494 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2495 goto __error;
2496 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2497 } else {
2498 if ((err = snd_via8233_pcm_new(chip)) < 0)
2499 goto __error;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002500 if (dxs_support == VIA_DXS_48K)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 chip->dxs_fixed = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002502 else if (dxs_support == VIA_DXS_NO_VRA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 chip->no_vra = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002504 else if (dxs_support == VIA_DXS_SRC) {
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02002505 chip->no_vra = 1;
2506 chip->dxs_src = 1;
2507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002509 if ((err = snd_via8233_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 goto __error;
2511 }
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 /* disable interrupts */
2514 for (i = 0; i < chip->num_devs; i++)
2515 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2516
2517 snprintf(card->longname, sizeof(card->longname),
2518 "%s with %s at %#lx, irq %d", card->shortname,
2519 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2520
2521 snd_via82xx_proc_init(chip);
2522
2523 if ((err = snd_card_register(card)) < 0) {
2524 snd_card_free(card);
2525 return err;
2526 }
2527 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return 0;
2529
2530 __error:
2531 snd_card_free(card);
2532 return err;
2533}
2534
2535static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2536{
2537 snd_card_free(pci_get_drvdata(pci));
2538 pci_set_drvdata(pci, NULL);
2539}
2540
2541static struct pci_driver driver = {
2542 .name = "VIA 82xx Audio",
2543 .id_table = snd_via82xx_ids,
2544 .probe = snd_via82xx_probe,
2545 .remove = __devexit_p(snd_via82xx_remove),
Takashi Iwai57feb832005-11-17 16:10:35 +01002546#ifdef CONFIG_PM
2547 .suspend = snd_via82xx_suspend,
2548 .resume = snd_via82xx_resume,
2549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550};
2551
2552static int __init alsa_card_via82xx_init(void)
2553{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002554 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
2557static void __exit alsa_card_via82xx_exit(void)
2558{
2559 pci_unregister_driver(&driver);
2560}
2561
2562module_init(alsa_card_via82xx_init)
2563module_exit(alsa_card_via82xx_exit)