blob: c488c5afa19591410d4c872bd3decf531f0c4555 [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
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010049#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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>
Paul Gortmaker65a77212011-07-15 13:13:37 -040056#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#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
Fabian Frederickb2fac072016-11-12 23:26:41 +010075#if IS_REACHABLE(CONFIG_GAMEPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#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
Rusty Russella67ff6a2011-12-15 13:49:36 +103083static bool 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;
Clemens Ladisch395c61d2010-07-12 16:27:24 +020088static int dxs_init_volume = 31;
Simon Arlott115551d2009-06-21 19:50:48 +010089static int nodelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Clemens Ladischb7fe4622005-10-04 08:46:51 +020091module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020093module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
David Howells6192c412017-04-04 16:54:30 +010095module_param_hw(mpu_port, long, ioport, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
97#ifdef SUPPORT_JOYSTICK
Clemens Ladischb7fe4622005-10-04 08:46:51 +020098module_param(joystick, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
100#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200101module_param(ac97_clock, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200103module_param(ac97_quirk, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +0200105module_param(dxs_support, int, 0444);
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200106MODULE_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)");
Clemens Ladisch395c61d2010-07-12 16:27:24 +0200107module_param(dxs_init_volume, int, 0644);
108MODULE_PARM_DESC(dxs_init_volume, "initial DXS volume (0-31)");
Simon Arlott115551d2009-06-21 19:50:48 +0100109module_param(nodelay, int, 0444);
110MODULE_PARM_DESC(nodelay, "Disable 500ms init delay");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200112/* just for backward compatibility */
Rusty Russella67ff6a2011-12-15 13:49:36 +1030113static bool enable;
Takashi Iwai698444f2005-10-20 16:53:49 +0200114module_param(enable, bool, 0444);
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* revision numbers for via686 */
118#define VIA_REV_686_A 0x10
119#define VIA_REV_686_B 0x11
120#define VIA_REV_686_C 0x12
121#define VIA_REV_686_D 0x13
122#define VIA_REV_686_E 0x14
123#define VIA_REV_686_H 0x20
124
125/* revision numbers for via8233 */
126#define VIA_REV_PRE_8233 0x10 /* not in market */
127#define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
128#define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
129#define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
130#define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
131#define VIA_REV_8237 0x60
Bastiaan Jacques8263c652006-04-18 17:04:04 +0200132#define VIA_REV_8251 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * Direct registers
136 */
137
138#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
139#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
140
141/* common offsets */
142#define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
143#define VIA_REG_STAT_ACTIVE 0x80 /* RO */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200144#define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define VIA_REG_STAT_PAUSED 0x40 /* RO */
146#define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
147#define VIA_REG_STAT_STOPPED 0x04 /* RWC */
148#define VIA_REG_STAT_EOL 0x02 /* RWC */
149#define VIA_REG_STAT_FLAG 0x01 /* RWC */
150#define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
151#define VIA_REG_CTRL_START 0x80 /* WO */
152#define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
153#define VIA_REG_CTRL_AUTOSTART 0x20
154#define VIA_REG_CTRL_PAUSE 0x08 /* RW */
155#define VIA_REG_CTRL_INT_STOP 0x04
156#define VIA_REG_CTRL_INT_EOL 0x02
157#define VIA_REG_CTRL_INT_FLAG 0x01
158#define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
159#define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
160#define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
161#define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
162#define VIA_REG_TYPE_16BIT 0x20 /* RW */
163#define VIA_REG_TYPE_STEREO 0x10 /* RW */
164#define VIA_REG_TYPE_INT_LLINE 0x00
165#define VIA_REG_TYPE_INT_LSAMPLE 0x04
166#define VIA_REG_TYPE_INT_LESSONE 0x08
167#define VIA_REG_TYPE_INT_MASK 0x0c
168#define VIA_REG_TYPE_INT_EOL 0x02
169#define VIA_REG_TYPE_INT_FLAG 0x01
170#define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
171#define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
172#define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
173#define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
174#define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
175#define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
176#define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
177
178#define DEFINE_VIA_REGSET(name,val) \
179enum {\
180 VIA_REG_##name##_STATUS = (val),\
181 VIA_REG_##name##_CONTROL = (val) + 0x01,\
182 VIA_REG_##name##_TYPE = (val) + 0x02,\
183 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
184 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
185 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
186 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
187}
188
189/* playback block */
190DEFINE_VIA_REGSET(PLAYBACK, 0x00);
191DEFINE_VIA_REGSET(CAPTURE, 0x10);
192DEFINE_VIA_REGSET(FM, 0x20);
193
194/* AC'97 */
195#define VIA_REG_AC97 0x80 /* dword */
196#define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
197#define VIA_REG_AC97_CODEC_ID_SHIFT 30
198#define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
199#define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
200#define VIA_REG_AC97_SECONDARY_VALID (1<<27)
201#define VIA_REG_AC97_PRIMARY_VALID (1<<25)
202#define VIA_REG_AC97_BUSY (1<<24)
203#define VIA_REG_AC97_READ (1<<23)
204#define VIA_REG_AC97_CMD_SHIFT 16
205#define VIA_REG_AC97_CMD_MASK 0x7e
206#define VIA_REG_AC97_DATA_SHIFT 0
207#define VIA_REG_AC97_DATA_MASK 0xffff
208
209#define VIA_REG_SGD_SHADOW 0x84 /* dword */
210/* via686 */
211#define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
212#define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
213#define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
214#define VIA_REG_SGD_STAT_PB_EOL (1<<4)
215#define VIA_REG_SGD_STAT_CP_EOL (1<<5)
216#define VIA_REG_SGD_STAT_FM_EOL (1<<6)
217#define VIA_REG_SGD_STAT_PB_STOP (1<<8)
218#define VIA_REG_SGD_STAT_CP_STOP (1<<9)
219#define VIA_REG_SGD_STAT_FM_STOP (1<<10)
220#define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
221#define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
222#define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
223/* via8233 */
224#define VIA8233_REG_SGD_STAT_FLAG (1<<0)
225#define VIA8233_REG_SGD_STAT_EOL (1<<1)
226#define VIA8233_REG_SGD_STAT_STOP (1<<2)
227#define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
228#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
229#define VIA8233_REG_SGD_CHAN_SDX 0
230#define VIA8233_REG_SGD_CHAN_MULTI 4
231#define VIA8233_REG_SGD_CHAN_REC 6
232#define VIA8233_REG_SGD_CHAN_REC1 7
233
234#define VIA_REG_GPI_STATUS 0x88
235#define VIA_REG_GPI_INTR 0x8c
236
237/* multi-channel and capture registers for via8233 */
238DEFINE_VIA_REGSET(MULTPLAY, 0x40);
239DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
240
241/* via8233-specific registers */
242#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
243#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
244#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
245#define VIA_REG_MULTPLAY_FMT_8BIT 0x00
246#define VIA_REG_MULTPLAY_FMT_16BIT 0x80
247#define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
248#define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
249#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
250
251#define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
252
253#define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
254#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
255#define VIA_REG_CAPTURE_CHANNEL_LINE 0
256#define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
257
258#define VIA_TBL_BIT_FLAG 0x40000000
259#define VIA_TBL_BIT_EOL 0x80000000
260
261/* pci space */
262#define VIA_ACLINK_STAT 0x40
263#define VIA_ACLINK_C11_READY 0x20
264#define VIA_ACLINK_C10_READY 0x10
265#define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
266#define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
267#define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
268#define VIA_ACLINK_CTRL 0x41
269#define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
270#define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
271#define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
272#define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
273#define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
274#define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
275#define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
276#define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
277#define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
278 VIA_ACLINK_CTRL_RESET|\
279 VIA_ACLINK_CTRL_PCM|\
280 VIA_ACLINK_CTRL_VRA)
281#define VIA_FUNC_ENABLE 0x42
282#define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
283#define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
284#define VIA_FUNC_RX2C_WRITE 0x20
285#define VIA_FUNC_SB_FIFO_EMPTY 0x10
286#define VIA_FUNC_ENABLE_GAME 0x08
287#define VIA_FUNC_ENABLE_FM 0x04
288#define VIA_FUNC_ENABLE_MIDI 0x02
289#define VIA_FUNC_ENABLE_SB 0x01
290#define VIA_PNP_CONTROL 0x43
291#define VIA_FM_NMI_CTRL 0x48
292#define VIA8233_VOLCHG_CTRL 0x48
293#define VIA8233_SPDIF_CTRL 0x49
294#define VIA8233_SPDIF_DX3 0x08
295#define VIA8233_SPDIF_SLOT_MASK 0x03
296#define VIA8233_SPDIF_SLOT_1011 0x00
297#define VIA8233_SPDIF_SLOT_34 0x01
298#define VIA8233_SPDIF_SLOT_78 0x02
299#define VIA8233_SPDIF_SLOT_69 0x03
300
301/*
302 */
303
304#define VIA_DXS_AUTO 0
305#define VIA_DXS_ENABLE 1
306#define VIA_DXS_DISABLE 2
307#define VIA_DXS_48K 3
308#define VIA_DXS_NO_VRA 4
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200309#define VIA_DXS_SRC 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311
312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * pcm stream
314 */
315
316struct snd_via_sg_table {
317 unsigned int offset;
318 unsigned int size;
319} ;
320
321#define VIA_TABLE_SIZE 255
Takashi Iwai55036002008-07-30 15:23:06 +0200322#define VIA_MAX_BUFSIZE (1<<24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100324struct viadev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 unsigned int reg_offset;
326 unsigned long port;
327 int direction; /* playback = 0, capture = 1 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100328 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int running;
330 unsigned int tbl_entries; /* # descriptors */
331 struct snd_dma_buffer table;
332 struct snd_via_sg_table *idx_table;
333 /* for recovery from the unexpected pointer */
334 unsigned int lastpos;
335 unsigned int fragsize;
336 unsigned int bufsize;
337 unsigned int bufsize2;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200338 int hwptr_done; /* processed frame position in the buffer */
339 int in_interrupt;
340 int shadow_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341};
342
343
344enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
345enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
346
347#define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
348
349struct via_rate_lock {
350 spinlock_t lock;
351 int rate;
352 int used;
353};
354
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100355struct via82xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int irq;
357
358 unsigned long port;
359 struct resource *mpu_res;
360 int chip_type;
361 unsigned char revision;
362
363 unsigned char old_legacy;
364 unsigned char old_legacy_cfg;
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200365#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned char legacy_saved;
367 unsigned char legacy_cfg_saved;
368 unsigned char spdif_ctrl_saved;
369 unsigned char capture_src_saved[2];
370 unsigned int mpu_port_saved;
371#endif
372
Honza Maly00f226d2005-10-14 18:18:01 +0200373 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
374 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
377
378 struct pci_dev *pci;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100379 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 unsigned int num_devs;
382 unsigned int playback_devno, multi_devno, capture_devno;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100383 struct viadev devs[VIA_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct via_rate_lock rates[2]; /* playback and capture */
385 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
386 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200387 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
389
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100390 struct snd_pcm *pcms[2];
391 struct snd_rawmidi *rmidi;
Clemens Ladisch3d009412009-10-22 09:04:09 +0200392 struct snd_kcontrol *dxs_controls[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100394 struct snd_ac97_bus *ac97_bus;
395 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 unsigned int ac97_clock;
397 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
398
399 spinlock_t reg_lock;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100400 struct snd_info_entry *proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402#ifdef SUPPORT_JOYSTICK
403 struct gameport *gameport;
404#endif
405};
406
Benoit Taine9baa3c32014-08-08 15:56:03 +0200407static const struct pci_device_id snd_via82xx_ids[] = {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200408 /* 0x1106, 0x3058 */
Joe Perches28d27aa2009-06-24 22:13:35 -0700409 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C686_5), TYPE_CARD_VIA686, }, /* 686A */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200410 /* 0x1106, 0x3059 */
Joe Perches28d27aa2009-06-24 22:13:35 -0700411 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_8233_5), TYPE_CARD_VIA8233, }, /* VT8233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 { 0, }
413};
414
415MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
416
417/*
418 */
419
420/*
421 * allocate and initialize the descriptor buffers
422 * periods = number of periods
423 * fragsize = period size in bytes
424 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100425static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct pci_dev *pci,
427 unsigned int periods, unsigned int fragsize)
428{
429 unsigned int i, idx, ofs, rest;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100430 struct via82xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (dev->table.area == NULL) {
433 /* the start of each lists must be aligned to 8 bytes,
434 * but the kernel pages are much bigger, so we don't care
435 */
436 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
437 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
438 &dev->table) < 0)
439 return -ENOMEM;
440 }
441 if (! dev->idx_table) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700442 dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
443 sizeof(*dev->idx_table),
444 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (! dev->idx_table)
446 return -ENOMEM;
447 }
448
449 /* fill the entries */
450 idx = 0;
451 ofs = 0;
452 for (i = 0; i < periods; i++) {
453 rest = fragsize;
454 /* fill descriptors for a period.
455 * a period can be split to several descriptors if it's
456 * over page boundary.
457 */
458 do {
459 unsigned int r;
460 unsigned int flag;
Takashi Iwai77a23f22008-08-21 13:00:13 +0200461 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (idx >= VIA_TABLE_SIZE) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100464 dev_err(&pci->dev, "too much table size!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -EINVAL;
466 }
Takashi Iwai77a23f22008-08-21 13:00:13 +0200467 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
468 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
Takashi Iwai55036002008-07-30 15:23:06 +0200469 r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 rest -= r;
471 if (! rest) {
472 if (i == periods - 1)
473 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
474 else
475 flag = VIA_TBL_BIT_FLAG; /* period boundary */
476 } else
477 flag = 0; /* period continues to the next */
Takashi Iwaiee419652009-02-05 16:11:31 +0100478 /*
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100479 dev_dbg(&pci->dev,
480 "tbl %d: at %d size %d (rest %d)\n",
481 idx, ofs, r, rest);
Takashi Iwaiee419652009-02-05 16:11:31 +0100482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
484 dev->idx_table[idx].offset = ofs;
485 dev->idx_table[idx].size = r;
486 ofs += r;
487 idx++;
488 } while (rest > 0);
489 }
490 dev->tbl_entries = idx;
491 dev->bufsize = periods * fragsize;
492 dev->bufsize2 = dev->bufsize / 2;
493 dev->fragsize = fragsize;
494 return 0;
495}
496
497
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100498static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct pci_dev *pci)
500{
501 if (dev->table.area) {
502 snd_dma_free_pages(&dev->table);
503 dev->table.area = NULL;
504 }
Jesper Juhl4d572772005-05-30 17:30:32 +0200505 kfree(dev->idx_table);
506 dev->idx_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508}
509
510/*
511 * Basic I/O
512 */
513
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100514static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 return inl(VIAREG(chip, AC97));
517}
518
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100519static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 outl(val, VIAREG(chip, AC97));
522}
523
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100524static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 unsigned int timeout = 1000; /* 1ms */
527 unsigned int val;
528
529 while (timeout-- > 0) {
530 udelay(1);
531 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
532 return val & 0xffff;
533 }
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100534 dev_err(chip->card->dev, "codec_ready: codec %i is not ready [0x%x]\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100535 secondary, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return -EIO;
537}
538
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100539static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 unsigned int timeout = 1000; /* 1ms */
542 unsigned int val, val1;
543 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
544 VIA_REG_AC97_SECONDARY_VALID;
545
546 while (timeout-- > 0) {
547 val = snd_via82xx_codec_xread(chip);
548 val1 = val & (VIA_REG_AC97_BUSY | stat);
549 if (val1 == stat)
550 return val & 0xffff;
551 udelay(1);
552 }
553 return -EIO;
554}
555
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100556static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100558 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int err;
560 err = snd_via82xx_codec_ready(chip, ac97->num);
561 /* here we need to wait fairly for long time.. */
Simon Arlott115551d2009-06-21 19:50:48 +0100562 if (!nodelay)
563 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100566static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 unsigned short reg,
568 unsigned short val)
569{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100570 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned int xval;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
574 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
575 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
576 xval |= val << VIA_REG_AC97_DATA_SHIFT;
577 snd_via82xx_codec_xwrite(chip, xval);
578 snd_via82xx_codec_ready(chip, ac97->num);
579}
580
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100581static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100583 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned int xval, val = 0xffff;
585 int again = 0;
586
587 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
588 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
589 xval |= VIA_REG_AC97_READ;
590 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
591 while (1) {
592 if (again++ > 3) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100593 dev_err(chip->card->dev,
594 "codec_read: codec %i is not valid [0x%x]\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100595 ac97->num, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0xffff;
597 }
598 snd_via82xx_codec_xwrite(chip, xval);
599 udelay (20);
600 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
601 udelay(25);
602 val = snd_via82xx_codec_xread(chip);
603 break;
604 }
605 }
606 return val & 0xffff;
607}
608
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100609static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
612 VIADEV_REG(viadev, OFFSET_CONTROL));
613 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
614 udelay(50);
615 /* disable interrupts */
616 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
617 /* clear interrupts */
618 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
619 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
620 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
621 viadev->lastpos = 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200622 viadev->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625
626/*
627 * Interrupt handler
Karsten Wiese4f550df2005-10-18 14:31:07 +0200628 * Used for 686 and 8233A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
David Howells7d12e782006-10-05 14:55:46 +0100630static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100632 struct via82xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned int status;
634 unsigned int i;
635
636 status = inl(VIAREG(chip, SGD_SHADOW));
637 if (! (status & chip->intr_mask)) {
638 if (chip->rmidi)
639 /* check mpu401 interrupt */
David Howells7d12e782006-10-05 14:55:46 +0100640 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return IRQ_NONE;
642 }
643
644 /* check status for each stream */
645 spin_lock(&chip->reg_lock);
646 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100647 struct viadev *viadev = &chip->devs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
Karsten Wiese4f550df2005-10-18 14:31:07 +0200649 if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 continue;
651 if (viadev->substream && viadev->running) {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200652 /*
653 * Update hwptr_done based on 'period elapsed'
654 * interrupts. We'll use it, when the chip returns 0
655 * for OFFSET_CURR_COUNT.
656 */
657 if (c_status & VIA_REG_STAT_EOL)
658 viadev->hwptr_done = 0;
659 else
660 viadev->hwptr_done += viadev->fragsize;
661 viadev->in_interrupt = c_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 spin_unlock(&chip->reg_lock);
663 snd_pcm_period_elapsed(viadev->substream);
664 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200665 viadev->in_interrupt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
668 }
669 spin_unlock(&chip->reg_lock);
670 return IRQ_HANDLED;
671}
672
673/*
Karsten Wiese4f550df2005-10-18 14:31:07 +0200674 * Interrupt handler
675 */
David Howells7d12e782006-10-05 14:55:46 +0100676static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
Karsten Wiese4f550df2005-10-18 14:31:07 +0200677{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100678 struct via82xx *chip = dev_id;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200679 unsigned int status;
680 unsigned int i;
681 int irqreturn = 0;
682
683 /* check status for each stream */
684 spin_lock(&chip->reg_lock);
685 status = inl(VIAREG(chip, SGD_SHADOW));
686
687 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100688 struct viadev *viadev = &chip->devs[i];
689 struct snd_pcm_substream *substream;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200690 unsigned char c_status, shadow_status;
691
692 shadow_status = (status >> viadev->shadow_shift) &
693 (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
694 VIA_REG_STAT_FLAG);
695 c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
696 if (!c_status)
697 continue;
698
699 substream = viadev->substream;
700 if (substream && viadev->running) {
701 /*
702 * Update hwptr_done based on 'period elapsed'
703 * interrupts. We'll use it, when the chip returns 0
704 * for OFFSET_CURR_COUNT.
705 */
706 if (c_status & VIA_REG_STAT_EOL)
707 viadev->hwptr_done = 0;
708 else
709 viadev->hwptr_done += viadev->fragsize;
710 viadev->in_interrupt = c_status;
711 if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
712 viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
713 spin_unlock(&chip->reg_lock);
714
715 snd_pcm_period_elapsed(substream);
716
717 spin_lock(&chip->reg_lock);
718 viadev->in_interrupt = 0;
719 }
720 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
721 irqreturn = 1;
722 }
723 spin_unlock(&chip->reg_lock);
724 return IRQ_RETVAL(irqreturn);
725}
726
727/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * PCM callbacks
729 */
730
731/*
732 * trigger callback
733 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100734static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100736 struct via82xx *chip = snd_pcm_substream_chip(substream);
737 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned char val;
739
740 if (chip->chip_type != TYPE_VIA686)
741 val = VIA_REG_CTRL_INT;
742 else
743 val = 0;
744 switch (cmd) {
745 case SNDRV_PCM_TRIGGER_START:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200746 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 val |= VIA_REG_CTRL_START;
748 viadev->running = 1;
749 break;
750 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200751 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 val = VIA_REG_CTRL_TERMINATE;
753 viadev->running = 0;
754 break;
755 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
756 val |= VIA_REG_CTRL_PAUSE;
757 viadev->running = 0;
758 break;
759 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
760 viadev->running = 1;
761 break;
762 default:
763 return -EINVAL;
764 }
765 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
766 if (cmd == SNDRV_PCM_TRIGGER_STOP)
767 snd_via82xx_channel_reset(chip, viadev);
768 return 0;
769}
770
771
772/*
773 * pointer callbacks
774 */
775
776/*
777 * calculate the linear position at the given sg-buffer index and the rest count
778 */
779
780#define check_invalid_pos(viadev,pos) \
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100781 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
782 viadev->lastpos < viadev->bufsize2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100784static inline unsigned int calc_linear_pos(struct via82xx *chip,
785 struct viadev *viadev,
786 unsigned int idx,
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100787 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 unsigned int size, base, res;
790
791 size = viadev->idx_table[idx].size;
792 base = viadev->idx_table[idx].offset;
793 res = base + size - count;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200794 if (res >= viadev->bufsize)
795 res -= viadev->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* check the validity of the calculated position */
798 if (size < count) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100799 dev_dbg(chip->card->dev,
800 "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100801 (int)size, (int)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 res = viadev->lastpos;
803 } else {
804 if (! count) {
805 /* Some mobos report count = 0 on the DMA boundary,
806 * i.e. count = size indeed.
807 * Let's check whether this step is above the expected size.
808 */
809 int delta = res - viadev->lastpos;
810 if (delta < 0)
811 delta += viadev->bufsize;
812 if ((unsigned int)delta > viadev->fragsize)
813 res = base;
814 }
815 if (check_invalid_pos(viadev, res)) {
816#ifdef POINTER_DEBUG
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100817 dev_dbg(chip->card->dev,
818 "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n",
819 idx, viadev->tbl_entries,
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100820 viadev->lastpos, viadev->bufsize2,
821 viadev->idx_table[idx].offset,
822 viadev->idx_table[idx].size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#endif
824 /* count register returns full size when end of buffer is reached */
825 res = base + size;
826 if (check_invalid_pos(viadev, res)) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100827 dev_dbg(chip->card->dev,
828 "invalid via82xx_cur_ptr (2), using last valid pointer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 res = viadev->lastpos;
830 }
831 }
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return res;
834}
835
836/*
837 * get the current pointer on via686
838 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100839static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100841 struct via82xx *chip = snd_pcm_substream_chip(substream);
842 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 unsigned int idx, ptr, count, res;
844
Takashi Iwaida3cec32008-08-08 17:12:14 +0200845 if (snd_BUG_ON(!viadev->tbl_entries))
846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
848 return 0;
849
850 spin_lock(&chip->reg_lock);
851 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
852 /* The via686a does not have the current index register,
853 * so we need to calculate the index from CURR_PTR.
854 */
855 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
856 if (ptr <= (unsigned int)viadev->table.addr)
857 idx = 0;
858 else /* CURR_PTR holds the address + 8 */
859 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100860 res = calc_linear_pos(chip, viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200861 viadev->lastpos = res; /* remember the last position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 spin_unlock(&chip->reg_lock);
863
864 return bytes_to_frames(substream->runtime, res);
865}
866
867/*
868 * get the current pointer on via823x
869 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100870static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100872 struct via82xx *chip = snd_pcm_substream_chip(substream);
873 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned int idx, count, res;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200875 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Takashi Iwaida3cec32008-08-08 17:12:14 +0200877 if (snd_BUG_ON(!viadev->tbl_entries))
878 return 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200881 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
882 status = viadev->in_interrupt;
883 if (!status)
884 status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
885
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200886 /* An apparent bug in the 8251 is worked around by sending a
887 * REG_CTRL_START. */
888 if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
889 snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
890
Karsten Wiese4f550df2005-10-18 14:31:07 +0200891 if (!(status & VIA_REG_STAT_ACTIVE)) {
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200892 res = 0;
893 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Karsten Wiese4f550df2005-10-18 14:31:07 +0200895 if (count & 0xffffff) {
896 idx = count >> 24;
897 if (idx >= viadev->tbl_entries) {
898#ifdef POINTER_DEBUG
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100899 dev_dbg(chip->card->dev,
900 "fail: invalid idx = %i/%i\n", idx,
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100901 viadev->tbl_entries);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200902#endif
903 res = viadev->lastpos;
904 } else {
905 count &= 0xffffff;
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100906 res = calc_linear_pos(chip, viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200907 }
908 } else {
909 res = viadev->hwptr_done;
910 if (!viadev->in_interrupt) {
911 if (status & VIA_REG_STAT_EOL) {
912 res = 0;
913 } else
914 if (status & VIA_REG_STAT_FLAG) {
915 res += viadev->fragsize;
916 }
917 }
918 }
919unlock:
920 viadev->lastpos = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 spin_unlock(&chip->reg_lock);
922
923 return bytes_to_frames(substream->runtime, res);
924}
925
926
927/*
928 * hw_params callback:
929 * allocate the buffer and build up the buffer description table
930 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100931static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
932 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100934 struct via82xx *chip = snd_pcm_substream_chip(substream);
935 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int err;
937
938 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
939 if (err < 0)
940 return err;
941 err = build_via_table(viadev, substream, chip->pci,
942 params_periods(hw_params),
943 params_period_bytes(hw_params));
944 if (err < 0)
945 return err;
946
947 return 0;
948}
949
950/*
951 * hw_free callback:
952 * clean up the buffer description table and release the buffer
953 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100954static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100956 struct via82xx *chip = snd_pcm_substream_chip(substream);
957 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 clean_via_table(viadev, substream, chip->pci);
960 snd_pcm_lib_free_pages(substream);
961 return 0;
962}
963
964
965/*
966 * set up the table pointer
967 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100968static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 snd_via82xx_codec_ready(chip, 0);
971 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
972 udelay(20);
973 snd_via82xx_codec_ready(chip, 0);
974}
975
976/*
977 * prepare callback for playback and capture on via686
978 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100979static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
980 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 snd_via82xx_channel_reset(chip, viadev);
983 /* this must be set after channel_reset */
984 snd_via82xx_set_table_ptr(chip, viadev);
985 outb(VIA_REG_TYPE_AUTOSTART |
986 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
987 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
988 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
989 VIA_REG_TYPE_INT_EOL |
990 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
991}
992
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100993static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100995 struct via82xx *chip = snd_pcm_substream_chip(substream);
996 struct viadev *viadev = substream->runtime->private_data;
997 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1000 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1001 via686_setup_format(chip, viadev, runtime);
1002 return 0;
1003}
1004
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001005static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001007 struct via82xx *chip = snd_pcm_substream_chip(substream);
1008 struct viadev *viadev = substream->runtime->private_data;
1009 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1012 via686_setup_format(chip, viadev, runtime);
1013 return 0;
1014}
1015
1016/*
1017 * lock the current rate
1018 */
1019static int via_lock_rate(struct via_rate_lock *rec, int rate)
1020{
1021 int changed = 0;
1022
1023 spin_lock_irq(&rec->lock);
1024 if (rec->rate != rate) {
1025 if (rec->rate && rec->used > 1) /* already set */
1026 changed = -EINVAL;
1027 else {
1028 rec->rate = rate;
1029 changed = 1;
1030 }
1031 }
1032 spin_unlock_irq(&rec->lock);
1033 return changed;
1034}
1035
1036/*
1037 * prepare callback for DSX playback on via823x
1038 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001039static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001041 struct via82xx *chip = snd_pcm_substream_chip(substream);
1042 struct viadev *viadev = substream->runtime->private_data;
1043 struct snd_pcm_runtime *runtime = substream->runtime;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001044 int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int rate_changed;
1046 u32 rbits;
1047
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001048 if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return rate_changed;
Takashi Iwai16d3f142005-08-15 15:02:28 +02001050 if (rate_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1052 chip->no_vra ? 48000 : runtime->rate);
Takashi Iwai16d3f142005-08-15 15:02:28 +02001053 if (chip->spdif_on && viadev->reg_offset == 0x30)
1054 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (runtime->rate == 48000)
1057 rbits = 0xfffff;
1058 else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001059 rbits = (0x100000 / 48000) * runtime->rate +
1060 ((0x100000 % 48000) * runtime->rate) / 48000;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001061 snd_BUG_ON(rbits & ~0xfffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 snd_via82xx_channel_reset(chip, viadev);
1063 snd_via82xx_set_table_ptr(chip, viadev);
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001064 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1065 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1066 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1067 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1069 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1070 rbits | /* rate */
1071 0xff000000, /* STOP index is never reached */
1072 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1073 udelay(20);
1074 snd_via82xx_codec_ready(chip, 0);
1075 return 0;
1076}
1077
1078/*
1079 * prepare callback for multi-channel playback on via823x
1080 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001081static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001083 struct via82xx *chip = snd_pcm_substream_chip(substream);
1084 struct viadev *viadev = substream->runtime->private_data;
1085 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned int slots;
1087 int fmt;
1088
1089 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1090 return -EINVAL;
1091 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1092 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1093 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1094 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1095 snd_via82xx_channel_reset(chip, viadev);
1096 snd_via82xx_set_table_ptr(chip, viadev);
1097
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001098 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1099 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 fmt |= runtime->channels << 4;
1101 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1102#if 0
1103 if (chip->revision == VIA_REV_8233A)
1104 slots = 0;
1105 else
1106#endif
1107 {
1108 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1109 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1110 switch (runtime->channels) {
1111 case 1: slots = (1<<0) | (1<<4); break;
1112 case 2: slots = (1<<0) | (2<<4); break;
1113 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1114 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1115 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1116 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1117 default: slots = 0; break;
1118 }
1119 }
1120 /* STOP index is never reached */
1121 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1122 udelay(20);
1123 snd_via82xx_codec_ready(chip, 0);
1124 return 0;
1125}
1126
1127/*
1128 * prepare callback for capture on via823x
1129 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001130static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001132 struct via82xx *chip = snd_pcm_substream_chip(substream);
1133 struct viadev *viadev = substream->runtime->private_data;
1134 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1137 return -EINVAL;
1138 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1139 snd_via82xx_channel_reset(chip, viadev);
1140 snd_via82xx_set_table_ptr(chip, viadev);
1141 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1142 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1143 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1144 0xff000000, /* STOP index is never reached */
1145 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1146 udelay(20);
1147 snd_via82xx_codec_ready(chip, 0);
1148 return 0;
1149}
1150
1151
1152/*
1153 * pcm hardware definition, identical for both playback and capture
1154 */
Bhumika Goyaldee49892017-08-12 21:01:28 +05301155static const struct snd_pcm_hardware snd_via82xx_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1158 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1159 SNDRV_PCM_INFO_MMAP_VALID |
Jaroslav Kysela41e48452005-08-18 13:43:12 +02001160 /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 SNDRV_PCM_INFO_PAUSE),
1162 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1163 .rates = SNDRV_PCM_RATE_48000,
1164 .rate_min = 48000,
1165 .rate_max = 48000,
1166 .channels_min = 1,
1167 .channels_max = 2,
Takashi Iwai55036002008-07-30 15:23:06 +02001168 .buffer_bytes_max = VIA_MAX_BUFSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 .period_bytes_min = 32,
Takashi Iwai55036002008-07-30 15:23:06 +02001170 .period_bytes_max = VIA_MAX_BUFSIZE / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 .periods_min = 2,
1172 .periods_max = VIA_TABLE_SIZE / 2,
1173 .fifo_size = 0,
1174};
1175
1176
1177/*
1178 * open callback skeleton
1179 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001180static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1181 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001183 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int err;
1185 struct via_rate_lock *ratep;
Clemens Ladisch5495ffb2011-09-16 23:16:05 +02001186 bool use_src = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 runtime->hw = snd_via82xx_hw;
1189
1190 /* set the hw rate condition */
1191 ratep = &chip->rates[viadev->direction];
1192 spin_lock_irq(&ratep->lock);
1193 ratep->used++;
1194 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1195 /* DXS#3 and spdif is on */
1196 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1197 snd_pcm_limit_hw_rates(runtime);
1198 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1199 /* fixed DXS playback rate */
1200 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1201 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001202 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1203 /* use full SRC capabilities of DXS */
1204 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1205 SNDRV_PCM_RATE_8000_48000);
1206 runtime->hw.rate_min = 8000;
1207 runtime->hw.rate_max = 48000;
Clemens Ladisch5495ffb2011-09-16 23:16:05 +02001208 use_src = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 } else if (! ratep->rate) {
1210 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1211 runtime->hw.rates = chip->ac97->rates[idx];
1212 snd_pcm_limit_hw_rates(runtime);
1213 } else {
1214 /* a fixed rate */
1215 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1216 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1217 }
1218 spin_unlock_irq(&ratep->lock);
1219
1220 /* we may remove following constaint when we modify table entries
1221 in interrupt */
1222 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1223 return err;
1224
Clemens Ladisch5495ffb2011-09-16 23:16:05 +02001225 if (use_src) {
1226 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1227 if (err < 0)
1228 return err;
1229 }
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 runtime->private_data = viadev;
1232 viadev->substream = substream;
1233
1234 return 0;
1235}
1236
1237
1238/*
Clemens Ladisch3d009412009-10-22 09:04:09 +02001239 * open callback for playback on via686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
Clemens Ladisch3d009412009-10-22 09:04:09 +02001241static int snd_via686_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001243 struct via82xx *chip = snd_pcm_substream_chip(substream);
1244 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int err;
1246
1247 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1248 return err;
1249 return 0;
1250}
1251
1252/*
Clemens Ladisch3d009412009-10-22 09:04:09 +02001253 * open callback for playback on via823x DXS
1254 */
1255static int snd_via8233_playback_open(struct snd_pcm_substream *substream)
1256{
1257 struct via82xx *chip = snd_pcm_substream_chip(substream);
1258 struct viadev *viadev;
1259 unsigned int stream;
1260 int err;
1261
1262 viadev = &chip->devs[chip->playback_devno + substream->number];
1263 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1264 return err;
1265 stream = viadev->reg_offset / 0x10;
1266 if (chip->dxs_controls[stream]) {
Clemens Ladisch395c61d2010-07-12 16:27:24 +02001267 chip->playback_volume[stream][0] =
1268 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1269 chip->playback_volume[stream][1] =
1270 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
Clemens Ladisch3d009412009-10-22 09:04:09 +02001271 chip->dxs_controls[stream]->vd[0].access &=
1272 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1273 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1274 SNDRV_CTL_EVENT_MASK_INFO,
1275 &chip->dxs_controls[stream]->id);
1276 }
1277 return 0;
1278}
1279
1280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 * open callback for playback on via823x multi-channel
1282 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001283static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001285 struct via82xx *chip = snd_pcm_substream_chip(substream);
1286 struct viadev *viadev = &chip->devs[chip->multi_devno];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int err;
1288 /* channels constraint for VIA8233A
1289 * 3 and 5 channels are not supported
1290 */
Takashi Iwaifbc57b22017-06-07 14:23:13 +02001291 static const unsigned int channels[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 1, 2, 4, 6
1293 };
Takashi Iwaifbc57b22017-06-07 14:23:13 +02001294 static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 .count = ARRAY_SIZE(channels),
1296 .list = channels,
1297 .mask = 0,
1298 };
1299
1300 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1301 return err;
1302 substream->runtime->hw.channels_max = 6;
1303 if (chip->revision == VIA_REV_8233A)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001304 snd_pcm_hw_constraint_list(substream->runtime, 0,
1305 SNDRV_PCM_HW_PARAM_CHANNELS,
1306 &hw_constraints_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
1310/*
1311 * open callback for capture on via686 and via823x
1312 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001313static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001315 struct via82xx *chip = snd_pcm_substream_chip(substream);
1316 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 return snd_via82xx_pcm_open(chip, viadev, substream);
1319}
1320
1321/*
1322 * close callback
1323 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001324static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001326 struct via82xx *chip = snd_pcm_substream_chip(substream);
1327 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct via_rate_lock *ratep;
1329
1330 /* release the rate lock */
1331 ratep = &chip->rates[viadev->direction];
1332 spin_lock_irq(&ratep->lock);
1333 ratep->used--;
1334 if (! ratep->used)
1335 ratep->rate = 0;
1336 spin_unlock_irq(&ratep->lock);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001337 if (! ratep->rate) {
1338 if (! viadev->direction) {
1339 snd_ac97_update_power(chip->ac97,
1340 AC97_PCM_FRONT_DAC_RATE, 0);
1341 snd_ac97_update_power(chip->ac97,
1342 AC97_PCM_SURR_DAC_RATE, 0);
1343 snd_ac97_update_power(chip->ac97,
1344 AC97_PCM_LFE_DAC_RATE, 0);
1345 } else
1346 snd_ac97_update_power(chip->ac97,
1347 AC97_PCM_LR_ADC_RATE, 0);
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 viadev->substream = NULL;
1350 return 0;
1351}
1352
Clemens Ladisch3d009412009-10-22 09:04:09 +02001353static int snd_via8233_playback_close(struct snd_pcm_substream *substream)
1354{
1355 struct via82xx *chip = snd_pcm_substream_chip(substream);
1356 struct viadev *viadev = substream->runtime->private_data;
1357 unsigned int stream;
1358
1359 stream = viadev->reg_offset / 0x10;
1360 if (chip->dxs_controls[stream]) {
1361 chip->dxs_controls[stream]->vd[0].access |=
1362 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1363 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1364 &chip->dxs_controls[stream]->id);
1365 }
1366 return snd_via82xx_pcm_close(substream);
1367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370/* via686 playback callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001371static const struct snd_pcm_ops snd_via686_playback_ops = {
Clemens Ladisch3d009412009-10-22 09:04:09 +02001372 .open = snd_via686_playback_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 .close = snd_via82xx_pcm_close,
1374 .ioctl = snd_pcm_lib_ioctl,
1375 .hw_params = snd_via82xx_hw_params,
1376 .hw_free = snd_via82xx_hw_free,
1377 .prepare = snd_via686_playback_prepare,
1378 .trigger = snd_via82xx_pcm_trigger,
1379 .pointer = snd_via686_pcm_pointer,
1380 .page = snd_pcm_sgbuf_ops_page,
1381};
1382
1383/* via686 capture callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001384static const struct snd_pcm_ops snd_via686_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 .open = snd_via82xx_capture_open,
1386 .close = snd_via82xx_pcm_close,
1387 .ioctl = snd_pcm_lib_ioctl,
1388 .hw_params = snd_via82xx_hw_params,
1389 .hw_free = snd_via82xx_hw_free,
1390 .prepare = snd_via686_capture_prepare,
1391 .trigger = snd_via82xx_pcm_trigger,
1392 .pointer = snd_via686_pcm_pointer,
1393 .page = snd_pcm_sgbuf_ops_page,
1394};
1395
1396/* via823x DSX playback callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001397static const struct snd_pcm_ops snd_via8233_playback_ops = {
Clemens Ladisch3d009412009-10-22 09:04:09 +02001398 .open = snd_via8233_playback_open,
1399 .close = snd_via8233_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 .ioctl = snd_pcm_lib_ioctl,
1401 .hw_params = snd_via82xx_hw_params,
1402 .hw_free = snd_via82xx_hw_free,
1403 .prepare = snd_via8233_playback_prepare,
1404 .trigger = snd_via82xx_pcm_trigger,
1405 .pointer = snd_via8233_pcm_pointer,
1406 .page = snd_pcm_sgbuf_ops_page,
1407};
1408
1409/* via823x multi-channel playback callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001410static const struct snd_pcm_ops snd_via8233_multi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 .open = snd_via8233_multi_open,
1412 .close = snd_via82xx_pcm_close,
1413 .ioctl = snd_pcm_lib_ioctl,
1414 .hw_params = snd_via82xx_hw_params,
1415 .hw_free = snd_via82xx_hw_free,
1416 .prepare = snd_via8233_multi_prepare,
1417 .trigger = snd_via82xx_pcm_trigger,
1418 .pointer = snd_via8233_pcm_pointer,
1419 .page = snd_pcm_sgbuf_ops_page,
1420};
1421
1422/* via823x capture callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001423static const struct snd_pcm_ops snd_via8233_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 .open = snd_via82xx_capture_open,
1425 .close = snd_via82xx_pcm_close,
1426 .ioctl = snd_pcm_lib_ioctl,
1427 .hw_params = snd_via82xx_hw_params,
1428 .hw_free = snd_via82xx_hw_free,
1429 .prepare = snd_via8233_capture_prepare,
1430 .trigger = snd_via82xx_pcm_trigger,
1431 .pointer = snd_via8233_pcm_pointer,
1432 .page = snd_pcm_sgbuf_ops_page,
1433};
1434
1435
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001436static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1437 int shadow_pos, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 chip->devs[idx].reg_offset = reg_offset;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001440 chip->devs[idx].shadow_shift = shadow_pos * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 chip->devs[idx].direction = direction;
1442 chip->devs[idx].port = chip->port + reg_offset;
1443}
1444
1445/*
1446 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1447 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001448static int snd_via8233_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001450 struct snd_pcm *pcm;
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001451 struct snd_pcm_chmap *chmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int i, err;
1453
1454 chip->playback_devno = 0; /* x 4 */
1455 chip->multi_devno = 4; /* x 1 */
1456 chip->capture_devno = 5; /* x 2 */
1457 chip->num_devs = 7;
1458 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1459
1460 /* PCM #0: 4 DSX playbacks and 1 capture */
1461 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1462 if (err < 0)
1463 return err;
1464 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1465 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1466 pcm->private_data = chip;
1467 strcpy(pcm->name, chip->card->shortname);
1468 chip->pcms[0] = pcm;
1469 /* set up playbacks */
1470 for (i = 0; i < 4; i++)
Karsten Wiese4f550df2005-10-18 14:31:07 +02001471 init_viadev(chip, i, 0x10 * i, i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001473 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Takashi Iwaia2e39612008-08-21 14:17:38 +02001475 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1476 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001477 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001479 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1480 snd_pcm_std_chmaps, 2, 0,
1481 &chmap);
1482 if (err < 0)
1483 return err;
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* PCM #1: multi-channel playback and 2nd capture */
1486 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1487 if (err < 0)
1488 return err;
1489 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1490 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1491 pcm->private_data = chip;
1492 strcpy(pcm->name, chip->card->shortname);
1493 chip->pcms[1] = pcm;
1494 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001495 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* set up capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001497 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Takashi Iwaia2e39612008-08-21 14:17:38 +02001499 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1500 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001501 64*1024, VIA_MAX_BUFSIZE);
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001502
1503 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1504 snd_pcm_alt_chmaps, 6, 0,
1505 &chmap);
1506 if (err < 0)
1507 return err;
1508 chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
1511}
1512
1513/*
1514 * create pcm instances for VIA8233A
1515 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001516static int snd_via8233a_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001518 struct snd_pcm *pcm;
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001519 struct snd_pcm_chmap *chmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int err;
1521
1522 chip->multi_devno = 0;
1523 chip->playback_devno = 1;
1524 chip->capture_devno = 2;
1525 chip->num_devs = 3;
1526 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1527
1528 /* PCM #0: multi-channel playback and capture */
1529 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1530 if (err < 0)
1531 return err;
1532 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1533 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1534 pcm->private_data = chip;
1535 strcpy(pcm->name, chip->card->shortname);
1536 chip->pcms[0] = pcm;
1537 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001538 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001540 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Takashi Iwaia2e39612008-08-21 14:17:38 +02001542 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1543 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001544 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001546 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1547 snd_pcm_alt_chmaps, 6, 0,
1548 &chmap);
1549 if (err < 0)
1550 return err;
1551 chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /* SPDIF supported? */
1554 if (! ac97_can_spdif(chip->ac97))
1555 return 0;
1556
1557 /* PCM #1: DXS3 playback (for spdif) */
1558 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1559 if (err < 0)
1560 return err;
1561 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1562 pcm->private_data = chip;
1563 strcpy(pcm->name, chip->card->shortname);
1564 chip->pcms[1] = pcm;
1565 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001566 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Takashi Iwaia2e39612008-08-21 14:17:38 +02001568 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1569 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001570 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572}
1573
1574/*
1575 * create a pcm instance for via686a/b
1576 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001577static int snd_via686_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001579 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int err;
1581
1582 chip->playback_devno = 0;
1583 chip->capture_devno = 1;
1584 chip->num_devs = 2;
1585 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1586
1587 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1588 if (err < 0)
1589 return err;
1590 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1591 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1592 pcm->private_data = chip;
1593 strcpy(pcm->name, chip->card->shortname);
1594 chip->pcms[0] = pcm;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001595 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1596 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Takashi Iwaia2e39612008-08-21 14:17:38 +02001598 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1599 snd_dma_pci_data(chip->pci),
Takashi Iwai55036002008-07-30 15:23:06 +02001600 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return 0;
1602}
1603
1604
1605/*
1606 * Mixer part
1607 */
1608
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001609static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 /* formerly they were "Line" and "Mic", but it looks like that they
1613 * have nothing to do with the actual physical connections...
1614 */
Takashi Iwai9883ab92014-10-20 18:20:18 +02001615 static const char * const texts[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 "Input1", "Input2"
1617 };
Takashi Iwai9883ab92014-10-20 18:20:18 +02001618 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001621static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001624 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1626 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1627 return 0;
1628}
1629
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001630static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001633 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1635 u8 val, oval;
1636
1637 spin_lock_irq(&chip->reg_lock);
1638 oval = inb(port);
1639 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1640 if (ucontrol->value.enumerated.item[0])
1641 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1642 if (val != oval)
1643 outb(val, port);
1644 spin_unlock_irq(&chip->reg_lock);
1645 return val != oval;
1646}
1647
Bill Pembertone23e7a12012-12-06 12:35:10 -05001648static struct snd_kcontrol_new snd_via8233_capture_source = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 .name = "Input Source Select",
1650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1651 .info = snd_via8233_capture_source_info,
1652 .get = snd_via8233_capture_source_get,
1653 .put = snd_via8233_capture_source_put,
1654};
1655
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001656#define snd_via8233_dxs3_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001658static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001661 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 u8 val;
1663
1664 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1665 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1666 return 0;
1667}
1668
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001669static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001672 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 u8 val, oval;
1674
1675 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1676 val = oval & ~VIA8233_SPDIF_DX3;
1677 if (ucontrol->value.integer.value[0])
1678 val |= VIA8233_SPDIF_DX3;
1679 /* save the spdif flag for rate filtering */
1680 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1681 if (val != oval) {
1682 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1683 return 1;
1684 }
1685 return 0;
1686}
1687
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301688static const struct snd_kcontrol_new snd_via8233_dxs3_spdif_control = {
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001689 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1691 .info = snd_via8233_dxs3_spdif_info,
1692 .get = snd_via8233_dxs3_spdif_get,
1693 .put = snd_via8233_dxs3_spdif_put,
1694};
1695
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001696static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1697 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1700 uinfo->count = 2;
1701 uinfo->value.integer.min = 0;
1702 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1703 return 0;
1704}
1705
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001706static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001709 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001710 unsigned int idx = kcontrol->id.subdevice;
Honza Maly00f226d2005-10-14 18:18:01 +02001711
1712 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1713 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1714 return 0;
1715}
1716
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001717static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1718 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001719{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001720 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001721 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1722 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724}
1725
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001726static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001729 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001730 unsigned int idx = kcontrol->id.subdevice;
Honza Maly00f226d2005-10-14 18:18:01 +02001731 unsigned long port = chip->port + 0x10 * idx;
1732 unsigned char val;
1733 int i, change = 0;
1734
1735 for (i = 0; i < 2; i++) {
1736 val = ucontrol->value.integer.value[i];
1737 if (val > VIA_DXS_MAX_VOLUME)
1738 val = VIA_DXS_MAX_VOLUME;
1739 val = VIA_DXS_MAX_VOLUME - val;
1740 change |= val != chip->playback_volume[idx][i];
1741 if (change) {
1742 chip->playback_volume[idx][i] = val;
1743 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1744 }
1745 }
1746 return change;
1747}
1748
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001749static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1750 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001751{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001752 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 unsigned int idx;
1754 unsigned char val;
1755 int i, change = 0;
1756
1757 for (i = 0; i < 2; i++) {
1758 val = ucontrol->value.integer.value[i];
1759 if (val > VIA_DXS_MAX_VOLUME)
1760 val = VIA_DXS_MAX_VOLUME;
1761 val = VIA_DXS_MAX_VOLUME - val;
Honza Maly00f226d2005-10-14 18:18:01 +02001762 if (val != chip->playback_volume_c[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 change = 1;
Honza Maly00f226d2005-10-14 18:18:01 +02001764 chip->playback_volume_c[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 for (idx = 0; idx < 4; idx++) {
1766 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02001767 chip->playback_volume[idx][i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1769 }
1770 }
1771 }
1772 return change;
1773}
1774
Clemens Ladischb452e082009-05-05 15:40:12 +02001775static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -4650, 150, 1);
Takashi Iwai7058c042006-08-21 18:44:54 +02001776
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301777static const struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 .name = "PCM Playback Volume",
1779 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwai7058c042006-08-21 18:44:54 +02001780 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1781 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 .info = snd_via8233_dxs_volume_info,
Honza Maly00f226d2005-10-14 18:18:01 +02001783 .get = snd_via8233_pcmdxs_volume_get,
1784 .put = snd_via8233_pcmdxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001785 .tlv = { .p = db_scale_dxs }
Honza Maly00f226d2005-10-14 18:18:01 +02001786};
1787
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301788static const struct snd_kcontrol_new snd_via8233_dxs_volume_control = {
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001789 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1790 .device = 0,
1791 /* .subdevice set later */
1792 .name = "PCM Playback Volume",
Clemens Ladisch3d009412009-10-22 09:04:09 +02001793 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1794 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1795 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Honza Maly00f226d2005-10-14 18:18:01 +02001796 .info = snd_via8233_dxs_volume_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 .get = snd_via8233_dxs_volume_get,
1798 .put = snd_via8233_dxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001799 .tlv = { .p = db_scale_dxs }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800};
1801
1802/*
1803 */
1804
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001805static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001807 struct via82xx *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 chip->ac97_bus = NULL;
1809}
1810
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001811static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001813 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 chip->ac97 = NULL;
1815}
1816
Joe Percheseab0fbf2015-04-04 13:38:25 -07001817static const struct ac97_quirk ac97_quirks[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001819 .subvendor = 0x1106,
1820 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 .codec_id = 0x56494161, /* VT1612A */
1822 .name = "Soltek SL-75DRV5",
1823 .type = AC97_TUNE_NONE
1824 },
1825 { /* FIXME: which codec? */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001826 .subvendor = 0x1106,
1827 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 .name = "ASRock K7VT2",
1829 .type = AC97_TUNE_HP_ONLY
1830 },
1831 {
Clemens Ladischbf30a432010-02-22 10:33:13 +01001832 .subvendor = 0x110a,
1833 .subdevice = 0x0079,
1834 .name = "Fujitsu Siemens D1289",
1835 .type = AC97_TUNE_HP_ONLY
1836 },
1837 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001838 .subvendor = 0x1019,
1839 .subdevice = 0x0a81,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 .name = "ECS K7VTA3",
1841 .type = AC97_TUNE_HP_ONLY
1842 },
1843 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001844 .subvendor = 0x1019,
1845 .subdevice = 0x0a85,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 .name = "ECS L7VMM2",
1847 .type = AC97_TUNE_HP_ONLY
1848 },
1849 {
Walter Sheets942fd1e2008-06-27 11:53:31 +02001850 .subvendor = 0x1019,
1851 .subdevice = 0x1841,
1852 .name = "ECS K7VTA3",
1853 .type = AC97_TUNE_HP_ONLY
1854 },
1855 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001856 .subvendor = 0x1849,
1857 .subdevice = 0x3059,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 .name = "ASRock K7VM2",
1859 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1860 },
1861 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001862 .subvendor = 0x14cd,
1863 .subdevice = 0x7002,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 .name = "Unknown",
1865 .type = AC97_TUNE_ALC_JACK
1866 },
1867 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001868 .subvendor = 0x1071,
1869 .subdevice = 0x8590,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .name = "Mitac Mobo",
1871 .type = AC97_TUNE_ALC_JACK
1872 },
1873 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001874 .subvendor = 0x161f,
1875 .subdevice = 0x202b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 .name = "Arima Notebook",
1877 .type = AC97_TUNE_HP_ONLY,
1878 },
Takashi Iwaidac8ddd2005-12-14 12:00:46 +01001879 {
1880 .subvendor = 0x161f,
1881 .subdevice = 0x2032,
1882 .name = "Targa Traveller 811",
1883 .type = AC97_TUNE_HP_ONLY,
1884 },
Daniel T Chend4199f02006-06-16 16:21:54 +02001885 {
1886 .subvendor = 0x161f,
1887 .subdevice = 0x2032,
1888 .name = "m680x",
1889 .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1890 },
Takashi Iwai96745132007-10-25 11:46:24 +02001891 {
1892 .subvendor = 0x1297,
1893 .subdevice = 0xa232,
1894 .name = "Shuttle AK32VN",
1895 .type = AC97_TUNE_HP_ONLY
1896 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 { } /* terminator */
1898};
1899
Bill Pembertone23e7a12012-12-06 12:35:10 -05001900static int snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001902 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001904 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 .write = snd_via82xx_codec_write,
1906 .read = snd_via82xx_codec_read,
1907 .wait = snd_via82xx_codec_wait,
1908 };
1909
1910 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1911 return err;
1912 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1913 chip->ac97_bus->clock = chip->ac97_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 memset(&ac97, 0, sizeof(ac97));
1916 ac97.private_data = chip;
1917 ac97.private_free = snd_via82xx_mixer_free_ac97;
1918 ac97.pci = chip->pci;
Takashi Iwaif1a63a32006-10-24 18:25:29 +02001919 ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1921 return err;
1922
1923 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1924
1925 if (chip->chip_type != TYPE_VIA686) {
1926 /* use slot 10/11 */
1927 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1928 }
1929
1930 return 0;
1931}
1932
1933#ifdef SUPPORT_JOYSTICK
1934#define JOYSTICK_ADDR 0x200
Bill Pembertone23e7a12012-12-06 12:35:10 -05001935static int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct gameport *gp;
1938 struct resource *r;
1939
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001940 if (!joystick)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return -ENODEV;
1942
1943 r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1944 if (!r) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01001945 dev_warn(chip->card->dev, "cannot reserve joystick port %#x\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001946 JOYSTICK_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return -EBUSY;
1948 }
1949
1950 chip->gameport = gp = gameport_allocate_port();
1951 if (!gp) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01001952 dev_err(chip->card->dev,
1953 "cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02001954 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return -ENOMEM;
1956 }
1957
1958 gameport_set_name(gp, "VIA686 Gameport");
1959 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1960 gameport_set_dev_parent(gp, &chip->pci->dev);
1961 gp->io = JOYSTICK_ADDR;
1962 gameport_set_port_data(gp, r);
1963
1964 /* Enable legacy joystick port */
1965 *legacy |= VIA_FUNC_ENABLE_GAME;
1966 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1967
1968 gameport_register_port(chip->gameport);
1969
1970 return 0;
1971}
1972
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001973static void snd_via686_free_gameport(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 if (chip->gameport) {
1976 struct resource *r = gameport_get_port_data(chip->gameport);
1977
1978 gameport_unregister_port(chip->gameport);
1979 chip->gameport = NULL;
Takashi Iwaib1d57762005-10-10 11:56:31 +02001980 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982}
1983#else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001984static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 return -ENOSYS;
1987}
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001988static inline void snd_via686_free_gameport(struct via82xx *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989#endif
1990
1991
1992/*
1993 *
1994 */
1995
Bill Pembertone23e7a12012-12-06 12:35:10 -05001996static int snd_via8233_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 int i, err, caps;
1999 unsigned char val;
2000
2001 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
2002 for (i = 0; i < caps; i++) {
2003 snd_via8233_capture_source.index = i;
2004 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
2005 if (err < 0)
2006 return err;
2007 }
2008 if (ac97_can_spdif(chip->ac97)) {
2009 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
2010 if (err < 0)
2011 return err;
2012 }
2013 if (chip->chip_type != TYPE_VIA8233A) {
2014 /* when no h/w PCM volume control is found, use DXS volume control
2015 * as the PCM vol control
2016 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002017 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 memset(&sid, 0, sizeof(sid));
2019 strcpy(sid.name, "PCM Playback Volume");
2020 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2021 if (! snd_ctl_find_id(chip->card, &sid)) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002022 dev_info(chip->card->dev,
2023 "Using DXS as PCM Playback\n");
Honza Maly00f226d2005-10-14 18:18:01 +02002024 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
2025 if (err < 0)
2026 return err;
2027 }
2028 else /* Using DXS when PCM emulation is enabled is really weird */
2029 {
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02002030 for (i = 0; i < 4; ++i) {
2031 struct snd_kcontrol *kctl;
2032
2033 kctl = snd_ctl_new1(
2034 &snd_via8233_dxs_volume_control, chip);
2035 if (!kctl)
2036 return -ENOMEM;
2037 kctl->id.subdevice = i;
2038 err = snd_ctl_add(chip->card, kctl);
2039 if (err < 0)
2040 return err;
Clemens Ladisch3d009412009-10-22 09:04:09 +02002041 chip->dxs_controls[i] = kctl;
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /* select spdif data slot 10/11 */
2046 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
2047 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
2048 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
2049 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
2050
2051 return 0;
2052}
2053
Bill Pembertone23e7a12012-12-06 12:35:10 -05002054static int snd_via686_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 unsigned char legacy, legacy_cfg;
2057 int rev_h = 0;
2058
2059 legacy = chip->old_legacy;
2060 legacy_cfg = chip->old_legacy_cfg;
2061 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
2062 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
2063 if (chip->revision >= VIA_REV_686_H) {
2064 rev_h = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002065 if (mpu_port >= 0x200) { /* force MIDI */
2066 mpu_port &= 0xfffc;
2067 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002068#ifdef CONFIG_PM_SLEEP
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002069 chip->mpu_port_saved = mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#endif
2071 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002072 mpu_port = pci_resource_start(chip->pci, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002075 switch (mpu_port) { /* force MIDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 case 0x300:
2077 case 0x310:
2078 case 0x320:
2079 case 0x330:
2080 legacy_cfg &= ~(3 << 2);
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002081 legacy_cfg |= (mpu_port & 0x0030) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 break;
2083 default: /* no, use BIOS settings */
2084 if (legacy & VIA_FUNC_ENABLE_MIDI)
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002085 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 break;
2087 }
2088 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002089 if (mpu_port >= 0x200 &&
2090 (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
2091 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (rev_h)
2093 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
2094 legacy |= VIA_FUNC_ENABLE_MIDI;
2095 } else {
2096 if (rev_h)
2097 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
2098 legacy &= ~VIA_FUNC_ENABLE_MIDI;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002099 mpu_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101
2102 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2103 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
2104 if (chip->mpu_res) {
2105 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002106 mpu_port, MPU401_INFO_INTEGRATED |
2107 MPU401_INFO_IRQ_HOOK, -1,
2108 &chip->rmidi) < 0) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002109 dev_warn(chip->card->dev,
2110 "unable to initialize MPU-401 at 0x%lx, skipping\n",
2111 mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 legacy &= ~VIA_FUNC_ENABLE_MIDI;
2113 } else {
2114 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
2115 }
2116 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2117 }
2118
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002119 snd_via686_create_gameport(chip, &legacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002121#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 chip->legacy_saved = legacy;
2123 chip->legacy_cfg_saved = legacy_cfg;
2124#endif
2125
2126 return 0;
2127}
2128
2129
2130/*
2131 * proc interface
2132 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002133static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2134 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002136 struct via82xx *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 int i;
2138
2139 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2140 for (i = 0; i < 0xa0; i += 4) {
2141 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2142 }
2143}
2144
Bill Pembertone23e7a12012-12-06 12:35:10 -05002145static void snd_via82xx_proc_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002147 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002150 snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153/*
2154 *
2155 */
2156
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002157static int snd_via82xx_chip_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 unsigned int val;
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002160 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 unsigned char pval;
2162
2163#if 0 /* broken on K7M? */
2164 if (chip->chip_type == TYPE_VIA686)
2165 /* disable all legacy ports */
2166 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2167#endif
2168 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2169 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2170 /* deassert ACLink reset, force SYNC */
2171 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2172 VIA_ACLINK_CTRL_ENABLE |
2173 VIA_ACLINK_CTRL_RESET |
2174 VIA_ACLINK_CTRL_SYNC);
2175 udelay(100);
2176#if 1 /* FIXME: should we do full reset here for all chip models? */
2177 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2178 udelay(100);
2179#else
2180 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2181 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2182 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2183 udelay(2);
2184#endif
2185 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2186 /* note - FM data out has trouble with non VRA codecs !! */
2187 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2188 udelay(100);
2189 }
2190
2191 /* Make sure VRA is enabled, in case we didn't do a
2192 * complete codec reset, above */
2193 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2194 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2195 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2196 /* note - FM data out has trouble with non VRA codecs !! */
2197 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2198 udelay(100);
2199 }
2200
2201 /* wait until codec ready */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002202 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 do {
2204 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2205 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2206 break;
Rene Hermand86d0192007-09-18 18:10:49 +02002207 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002208 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002211 dev_err(chip->card->dev,
2212 "AC'97 codec is not ready [0x%x]\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2215 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2216 VIA_REG_AC97_SECONDARY_VALID |
2217 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002218 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2220 VIA_REG_AC97_SECONDARY_VALID |
2221 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2222 do {
2223 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2224 chip->ac97_secondary = 1;
2225 goto __ac97_ok2;
2226 }
Rene Hermand86d0192007-09-18 18:10:49 +02002227 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002228 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /* This is ok, the most of motherboards have only one codec */
2230
2231 __ac97_ok2:
2232#endif
2233
2234 if (chip->chip_type == TYPE_VIA686) {
2235 /* route FM trap to IRQ, disable FM trap */
2236 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2237 /* disable all GPI interrupts */
2238 outl(0, VIAREG(chip, GPI_INTR));
2239 }
2240
2241 if (chip->chip_type != TYPE_VIA686) {
2242 /* Workaround for Award BIOS bug:
2243 * DXS channels don't work properly with VRA if MC97 is disabled.
2244 */
2245 struct pci_dev *pci;
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002246 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (pci) {
2248 unsigned char data;
2249 pci_read_config_byte(pci, 0x44, &data);
2250 pci_write_config_byte(pci, 0x44, data | 0x40);
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002251 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253 }
2254
2255 if (chip->chip_type != TYPE_VIA8233A) {
2256 int i, idx;
2257 for (idx = 0; idx < 4; idx++) {
2258 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02002259 for (i = 0; i < 2; i++) {
2260 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002261 outb(chip->playback_volume_c[i],
2262 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
Honza Maly00f226d2005-10-14 18:18:01 +02002263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 }
2266
2267 return 0;
2268}
2269
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002270#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271/*
2272 * power management
2273 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002274static int snd_via82xx_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002276 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai57feb832005-11-17 16:10:35 +01002277 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int i;
2279
Takashi Iwai57feb832005-11-17 16:10:35 +01002280 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 for (i = 0; i < 2; i++)
Takashi Iwai57feb832005-11-17 16:10:35 +01002282 snd_pcm_suspend_all(chip->pcms[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 for (i = 0; i < chip->num_devs; i++)
2284 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2285 synchronize_irq(chip->irq);
2286 snd_ac97_suspend(chip->ac97);
2287
2288 /* save misc values */
2289 if (chip->chip_type != TYPE_VIA686) {
2290 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2291 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2292 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2293 }
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return 0;
2296}
2297
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002298static int snd_via82xx_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002300 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai57feb832005-11-17 16:10:35 +01002301 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 int i;
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 snd_via82xx_chip_init(chip);
2305
2306 if (chip->chip_type == TYPE_VIA686) {
2307 if (chip->mpu_port_saved)
2308 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2309 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2310 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2311 } else {
2312 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2313 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2314 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2315 }
2316
2317 snd_ac97_resume(chip->ac97);
2318
2319 for (i = 0; i < chip->num_devs; i++)
2320 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2321
Takashi Iwai57feb832005-11-17 16:10:35 +01002322 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002325
2326static SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
2327#define SND_VIA82XX_PM_OPS &snd_via82xx_pm
2328#else
2329#define SND_VIA82XX_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002330#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002332static int snd_via82xx_free(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 unsigned int i;
2335
2336 if (chip->irq < 0)
2337 goto __end_hw;
2338 /* disable interrupts */
2339 for (i = 0; i < chip->num_devs; i++)
2340 snd_via82xx_channel_reset(chip, &chip->devs[i]);
Jeff Garzikf000fd82008-04-22 13:50:34 +02002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if (chip->irq >= 0)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002343 free_irq(chip->irq, chip);
Marcin Åšlusarz757d5a72007-12-14 12:51:24 +01002344 __end_hw:
Takashi Iwaib1d57762005-10-10 11:56:31 +02002345 release_and_free_resource(chip->mpu_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 pci_release_regions(chip->pci);
2347
2348 if (chip->chip_type == TYPE_VIA686) {
2349 snd_via686_free_gameport(chip);
2350 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2351 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2352 }
2353 pci_disable_device(chip->pci);
2354 kfree(chip);
2355 return 0;
2356}
2357
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002358static int snd_via82xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002360 struct via82xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return snd_via82xx_free(chip);
2362}
2363
Bill Pembertone23e7a12012-12-06 12:35:10 -05002364static int snd_via82xx_create(struct snd_card *card,
2365 struct pci_dev *pci,
2366 int chip_type,
2367 int revision,
2368 unsigned int ac97_clock,
2369 struct via82xx **r_via)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002371 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 int err;
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002373 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 .dev_free = snd_via82xx_dev_free,
2375 };
2376
2377 if ((err = pci_enable_device(pci)) < 0)
2378 return err;
2379
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002380 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 pci_disable_device(pci);
2382 return -ENOMEM;
2383 }
2384
2385 chip->chip_type = chip_type;
2386 chip->revision = revision;
2387
2388 spin_lock_init(&chip->reg_lock);
2389 spin_lock_init(&chip->rates[0].lock);
2390 spin_lock_init(&chip->rates[1].lock);
2391 chip->card = card;
2392 chip->pci = pci;
2393 chip->irq = -1;
2394
2395 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2396 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2397 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2398 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2399
2400 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2401 kfree(chip);
2402 pci_disable_device(pci);
2403 return err;
2404 }
2405 chip->port = pci_resource_start(pci, 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +02002406 if (request_irq(pci->irq,
2407 chip_type == TYPE_VIA8233 ?
2408 snd_via8233_interrupt : snd_via686_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002409 IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002410 KBUILD_MODNAME, chip)) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002411 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 snd_via82xx_free(chip);
2413 return -EBUSY;
2414 }
2415 chip->irq = pci->irq;
2416 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2417 chip->ac97_clock = ac97_clock;
2418 synchronize_irq(chip->irq);
2419
2420 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2421 snd_via82xx_free(chip);
2422 return err;
2423 }
2424
2425 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2426 snd_via82xx_free(chip);
2427 return err;
2428 }
2429
2430 /* The 8233 ac97 controller does not implement the master bit
2431 * in the pci command register. IMHO this is a violation of the PCI spec.
2432 * We call pci_set_master here because it does not hurt. */
2433 pci_set_master(pci);
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 *r_via = chip;
2436 return 0;
2437}
2438
2439struct via823x_info {
2440 int revision;
2441 char *name;
2442 int type;
2443};
Bill Pembertone23e7a12012-12-06 12:35:10 -05002444static struct via823x_info via823x_cards[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2446 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2447 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2448 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2449 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2450 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
Bastiaan Jacques8263c652006-04-18 17:04:04 +02002451 { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452};
2453
2454/*
2455 * auto detection of DXS channel supports.
2456 */
Takashi Iwai9d749582006-11-24 15:37:18 +01002457
Bill Pembertone23e7a12012-12-06 12:35:10 -05002458static struct snd_pci_quirk dxs_whitelist[] = {
Takashi Iwai9d749582006-11-24 15:37:18 +01002459 SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2460 SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2461 SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2462 SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002463 SND_PCI_QUIRK_VENDOR(0x1019, "ESC K8", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002464 SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2465 SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2466 SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002467 SND_PCI_QUIRK_VENDOR(0x1043, "ASUS A7/A8", VIA_DXS_NO_VRA),
2468 SND_PCI_QUIRK_VENDOR(0x1071, "Diverse Notebook", VIA_DXS_NO_VRA),
Takashi Iwai9d749582006-11-24 15:37:18 +01002469 SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002470 SND_PCI_QUIRK_VENDOR(0x1106, "ASRock", VIA_DXS_SRC),
Takashi Iwai11be2652007-10-11 14:35:00 +02002471 SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
Takashi Iwai96745132007-10-25 11:46:24 +02002472 SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2473 SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002474 SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2475 SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2476 SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2477 SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002478 SND_PCI_QUIRK_VENDOR(0x1462, "MSI Mobo", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002479 SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2480 SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2481 SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2482 SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2483 SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2484 SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2485 SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2486 SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2487 SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2488 SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2489 SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2490 SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2491 SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002492 SND_PCI_QUIRK_VENDOR(0x1695, "EPoX mobo", VIA_DXS_SRC),
2493 SND_PCI_QUIRK_VENDOR(0x16f3, "Jetway K8", VIA_DXS_SRC),
2494 SND_PCI_QUIRK_VENDOR(0x1734, "FSC Laptop", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002495 SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002496 SND_PCI_QUIRK_VENDOR(0x1849, "ASRock mobo", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002497 SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8", VIA_DXS_NO_VRA),
2498 SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2499 { } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500};
2501
Bill Pembertone23e7a12012-12-06 12:35:10 -05002502static int check_dxs_list(struct pci_dev *pci, int revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Takashi Iwai9d749582006-11-24 15:37:18 +01002504 const struct snd_pci_quirk *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Takashi Iwai9d749582006-11-24 15:37:18 +01002506 w = snd_pci_quirk_lookup(pci, dxs_whitelist);
2507 if (w) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002508 dev_dbg(&pci->dev, "DXS white list for %s found\n",
Takashi Iwai86b27232013-01-25 10:54:07 +01002509 snd_pci_quirk_name(w));
Takashi Iwai9d749582006-11-24 15:37:18 +01002510 return w->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
2512
Takashi Iwaia7695772006-04-27 16:56:07 +02002513 /* for newer revision, default to DXS_SRC */
2514 if (revision >= VIA_REV_8235)
2515 return VIA_DXS_SRC;
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 /*
2518 * not detected, try 48k rate only to be sure.
2519 */
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002520 dev_info(&pci->dev, "Assuming DXS channels with 48k fixed sample rate.\n");
2521 dev_info(&pci->dev, " Please try dxs_support=5 option\n");
2522 dev_info(&pci->dev, " and report if it works on your machine.\n");
2523 dev_info(&pci->dev, " For more details, read ALSA-Configuration.txt.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return VIA_DXS_48K;
2525};
2526
Bill Pembertone23e7a12012-12-06 12:35:10 -05002527static int snd_via82xx_probe(struct pci_dev *pci,
2528 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002530 struct snd_card *card;
2531 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 int chip_type = 0, card_type;
2533 unsigned int i;
2534 int err;
2535
Takashi Iwai60c57722014-01-29 14:20:19 +01002536 err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002537 if (err < 0)
2538 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 card_type = pci_id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 switch (card_type) {
2542 case TYPE_CARD_VIA686:
2543 strcpy(card->driver, "VIA686A");
Auke Kok44c10132007-06-08 15:46:36 -07002544 sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 chip_type = TYPE_VIA686;
2546 break;
2547 case TYPE_CARD_VIA8233:
2548 chip_type = TYPE_VIA8233;
Auke Kok44c10132007-06-08 15:46:36 -07002549 sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
Auke Kok44c10132007-06-08 15:46:36 -07002551 if (pci->revision == via823x_cards[i].revision) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 chip_type = via823x_cards[i].type;
2553 strcpy(card->shortname, via823x_cards[i].name);
2554 break;
2555 }
2556 }
2557 if (chip_type != TYPE_VIA8233A) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002558 if (dxs_support == VIA_DXS_AUTO)
Auke Kok44c10132007-06-08 15:46:36 -07002559 dxs_support = check_dxs_list(pci, pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 /* force to use VIA8233 or 8233A model according to
2561 * dxs_support module option
2562 */
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002563 if (dxs_support == VIA_DXS_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 chip_type = TYPE_VIA8233A;
2565 else
2566 chip_type = TYPE_VIA8233;
2567 }
2568 if (chip_type == TYPE_VIA8233A)
2569 strcpy(card->driver, "VIA8233A");
Auke Kok44c10132007-06-08 15:46:36 -07002570 else if (pci->revision >= VIA_REV_8237)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2572 else
2573 strcpy(card->driver, "VIA8233");
2574 break;
2575 default:
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002576 dev_err(card->dev, "invalid card type %d\n", card_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 err = -EINVAL;
2578 goto __error;
2579 }
2580
Auke Kok44c10132007-06-08 15:46:36 -07002581 if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002582 ac97_clock, &chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 goto __error;
Takashi Iwai57feb832005-11-17 16:10:35 +01002584 card->private_data = chip;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002585 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 goto __error;
2587
2588 if (chip_type == TYPE_VIA686) {
2589 if ((err = snd_via686_pcm_new(chip)) < 0 ||
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002590 (err = snd_via686_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 goto __error;
2592 } else {
2593 if (chip_type == TYPE_VIA8233A) {
2594 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2595 goto __error;
2596 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2597 } else {
2598 if ((err = snd_via8233_pcm_new(chip)) < 0)
2599 goto __error;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002600 if (dxs_support == VIA_DXS_48K)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 chip->dxs_fixed = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002602 else if (dxs_support == VIA_DXS_NO_VRA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 chip->no_vra = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002604 else if (dxs_support == VIA_DXS_SRC) {
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02002605 chip->no_vra = 1;
2606 chip->dxs_src = 1;
2607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002609 if ((err = snd_via8233_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 goto __error;
2611 }
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* disable interrupts */
2614 for (i = 0; i < chip->num_devs; i++)
2615 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2616
2617 snprintf(card->longname, sizeof(card->longname),
2618 "%s with %s at %#lx, irq %d", card->shortname,
2619 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2620
2621 snd_via82xx_proc_init(chip);
2622
2623 if ((err = snd_card_register(card)) < 0) {
2624 snd_card_free(card);
2625 return err;
2626 }
2627 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return 0;
2629
2630 __error:
2631 snd_card_free(card);
2632 return err;
2633}
2634
Bill Pembertone23e7a12012-12-06 12:35:10 -05002635static void snd_via82xx_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002640static struct pci_driver via82xx_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002641 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 .id_table = snd_via82xx_ids,
2643 .probe = snd_via82xx_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002644 .remove = snd_via82xx_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002645 .driver = {
2646 .pm = SND_VIA82XX_PM_OPS,
2647 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648};
2649
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002650module_pci_driver(via82xx_driver);