blob: 154d88ce88131c96863360cf7dcdb926a56984c2 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ALSA driver for VIA VT82xx (South Bridge)
4 *
5 * VT82C686A/B/C, VT8233A/C, VT8235
6 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02007 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
9 * 2002 Takashi Iwai <tiwai@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12/*
13 * Changes:
14 *
15 * Dec. 19, 2002 Takashi Iwai <tiwai@suse.de>
16 * - use the DSX channels for the first pcm playback.
17 * (on VIA8233, 8233C and 8235 only)
18 * this will allow you play simultaneously up to 4 streams.
19 * multi-channel playback is assigned to the second device
20 * on these chips.
21 * - support the secondary capture (on VIA8233/C,8235)
22 * - SPDIF support
23 * the DSX3 channel can be used for SPDIF output.
24 * on VIA8233A, this channel is assigned to the second pcm
25 * playback.
26 * the card config of alsa-lib will assign the correct
27 * device for applications.
28 * - clean up the code, separate low-level initialization
29 * routines for each chipset.
Karsten Wiese4f550df2005-10-18 14:31:07 +020030 *
31 * Sep. 26, 2005 Karsten Wiese <annabellesgarden@yahoo.de>
32 * - Optimize position calculation for the 823x chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010035#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/interrupt.h>
38#include <linux/init.h>
39#include <linux/pci.h>
40#include <linux/slab.h>
41#include <linux/gameport.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040042#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
46#include <sound/info.h>
Takashi Iwai7058c042006-08-21 18:44:54 +020047#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <sound/ac97_codec.h>
49#include <sound/mpu401.h>
50#include <sound/initval.h>
51
52#if 0
53#define POINTER_DEBUG
54#endif
55
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020056MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_DESCRIPTION("VIA VT82xx audio");
58MODULE_LICENSE("GPL");
59MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
60
Fabian Frederickb2fac072016-11-12 23:26:41 +010061#if IS_REACHABLE(CONFIG_GAMEPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define SUPPORT_JOYSTICK 1
63#endif
64
Clemens Ladischb7fe4622005-10-04 08:46:51 +020065static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
66static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
67static long mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifdef SUPPORT_JOYSTICK
Rusty Russella67ff6a2011-12-15 13:49:36 +103069static bool joystick;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +020071static int ac97_clock = 48000;
72static char *ac97_quirk;
73static int dxs_support;
Clemens Ladisch395c61d2010-07-12 16:27:24 +020074static int dxs_init_volume = 31;
Simon Arlott115551d2009-06-21 19:50:48 +010075static int nodelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Clemens Ladischb7fe4622005-10-04 08:46:51 +020077module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020079module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
David Howells6192c412017-04-04 16:54:30 +010081module_param_hw(mpu_port, long, ioport, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
83#ifdef SUPPORT_JOYSTICK
Clemens Ladischb7fe4622005-10-04 08:46:51 +020084module_param(joystick, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
86#endif
Clemens Ladischb7fe4622005-10-04 08:46:51 +020087module_param(ac97_clock, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020089module_param(ac97_quirk, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020091module_param(dxs_support, int, 0444);
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +020092MODULE_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 +020093module_param(dxs_init_volume, int, 0644);
94MODULE_PARM_DESC(dxs_init_volume, "initial DXS volume (0-31)");
Simon Arlott115551d2009-06-21 19:50:48 +010095module_param(nodelay, int, 0444);
96MODULE_PARM_DESC(nodelay, "Disable 500ms init delay");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Takashi Iwai2b3e5842005-10-06 13:47:23 +020098/* just for backward compatibility */
Rusty Russella67ff6a2011-12-15 13:49:36 +103099static bool enable;
Takashi Iwai698444f2005-10-20 16:53:49 +0200100module_param(enable, bool, 0444);
Takashi Iwai2b3e5842005-10-06 13:47:23 +0200101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* revision numbers for via686 */
104#define VIA_REV_686_A 0x10
105#define VIA_REV_686_B 0x11
106#define VIA_REV_686_C 0x12
107#define VIA_REV_686_D 0x13
108#define VIA_REV_686_E 0x14
109#define VIA_REV_686_H 0x20
110
111/* revision numbers for via8233 */
112#define VIA_REV_PRE_8233 0x10 /* not in market */
113#define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
114#define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
115#define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
116#define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
117#define VIA_REV_8237 0x60
Bastiaan Jacques8263c652006-04-18 17:04:04 +0200118#define VIA_REV_8251 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * Direct registers
122 */
123
124#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
125#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
126
127/* common offsets */
128#define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
129#define VIA_REG_STAT_ACTIVE 0x80 /* RO */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200130#define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define VIA_REG_STAT_PAUSED 0x40 /* RO */
132#define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
133#define VIA_REG_STAT_STOPPED 0x04 /* RWC */
134#define VIA_REG_STAT_EOL 0x02 /* RWC */
135#define VIA_REG_STAT_FLAG 0x01 /* RWC */
136#define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
137#define VIA_REG_CTRL_START 0x80 /* WO */
138#define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
139#define VIA_REG_CTRL_AUTOSTART 0x20
140#define VIA_REG_CTRL_PAUSE 0x08 /* RW */
141#define VIA_REG_CTRL_INT_STOP 0x04
142#define VIA_REG_CTRL_INT_EOL 0x02
143#define VIA_REG_CTRL_INT_FLAG 0x01
144#define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
145#define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
146#define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
147#define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
148#define VIA_REG_TYPE_16BIT 0x20 /* RW */
149#define VIA_REG_TYPE_STEREO 0x10 /* RW */
150#define VIA_REG_TYPE_INT_LLINE 0x00
151#define VIA_REG_TYPE_INT_LSAMPLE 0x04
152#define VIA_REG_TYPE_INT_LESSONE 0x08
153#define VIA_REG_TYPE_INT_MASK 0x0c
154#define VIA_REG_TYPE_INT_EOL 0x02
155#define VIA_REG_TYPE_INT_FLAG 0x01
156#define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
157#define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
158#define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
159#define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
160#define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
161#define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
162#define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
163
164#define DEFINE_VIA_REGSET(name,val) \
165enum {\
166 VIA_REG_##name##_STATUS = (val),\
167 VIA_REG_##name##_CONTROL = (val) + 0x01,\
168 VIA_REG_##name##_TYPE = (val) + 0x02,\
169 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
170 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
171 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
172 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
173}
174
175/* playback block */
176DEFINE_VIA_REGSET(PLAYBACK, 0x00);
177DEFINE_VIA_REGSET(CAPTURE, 0x10);
178DEFINE_VIA_REGSET(FM, 0x20);
179
180/* AC'97 */
181#define VIA_REG_AC97 0x80 /* dword */
182#define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
183#define VIA_REG_AC97_CODEC_ID_SHIFT 30
184#define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
185#define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
186#define VIA_REG_AC97_SECONDARY_VALID (1<<27)
187#define VIA_REG_AC97_PRIMARY_VALID (1<<25)
188#define VIA_REG_AC97_BUSY (1<<24)
189#define VIA_REG_AC97_READ (1<<23)
190#define VIA_REG_AC97_CMD_SHIFT 16
191#define VIA_REG_AC97_CMD_MASK 0x7e
192#define VIA_REG_AC97_DATA_SHIFT 0
193#define VIA_REG_AC97_DATA_MASK 0xffff
194
195#define VIA_REG_SGD_SHADOW 0x84 /* dword */
196/* via686 */
197#define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
198#define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
199#define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
200#define VIA_REG_SGD_STAT_PB_EOL (1<<4)
201#define VIA_REG_SGD_STAT_CP_EOL (1<<5)
202#define VIA_REG_SGD_STAT_FM_EOL (1<<6)
203#define VIA_REG_SGD_STAT_PB_STOP (1<<8)
204#define VIA_REG_SGD_STAT_CP_STOP (1<<9)
205#define VIA_REG_SGD_STAT_FM_STOP (1<<10)
206#define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
207#define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
208#define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
209/* via8233 */
210#define VIA8233_REG_SGD_STAT_FLAG (1<<0)
211#define VIA8233_REG_SGD_STAT_EOL (1<<1)
212#define VIA8233_REG_SGD_STAT_STOP (1<<2)
213#define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
214#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
215#define VIA8233_REG_SGD_CHAN_SDX 0
216#define VIA8233_REG_SGD_CHAN_MULTI 4
217#define VIA8233_REG_SGD_CHAN_REC 6
218#define VIA8233_REG_SGD_CHAN_REC1 7
219
220#define VIA_REG_GPI_STATUS 0x88
221#define VIA_REG_GPI_INTR 0x8c
222
223/* multi-channel and capture registers for via8233 */
224DEFINE_VIA_REGSET(MULTPLAY, 0x40);
225DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
226
227/* via8233-specific registers */
228#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
229#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
230#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
231#define VIA_REG_MULTPLAY_FMT_8BIT 0x00
232#define VIA_REG_MULTPLAY_FMT_16BIT 0x80
233#define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
234#define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
235#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
236
237#define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
238
239#define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
240#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
241#define VIA_REG_CAPTURE_CHANNEL_LINE 0
242#define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
243
244#define VIA_TBL_BIT_FLAG 0x40000000
245#define VIA_TBL_BIT_EOL 0x80000000
246
247/* pci space */
248#define VIA_ACLINK_STAT 0x40
249#define VIA_ACLINK_C11_READY 0x20
250#define VIA_ACLINK_C10_READY 0x10
251#define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
252#define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
253#define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
254#define VIA_ACLINK_CTRL 0x41
255#define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
256#define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
257#define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
258#define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
259#define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
260#define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
261#define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
262#define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
263#define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
264 VIA_ACLINK_CTRL_RESET|\
265 VIA_ACLINK_CTRL_PCM|\
266 VIA_ACLINK_CTRL_VRA)
267#define VIA_FUNC_ENABLE 0x42
268#define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
269#define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
270#define VIA_FUNC_RX2C_WRITE 0x20
271#define VIA_FUNC_SB_FIFO_EMPTY 0x10
272#define VIA_FUNC_ENABLE_GAME 0x08
273#define VIA_FUNC_ENABLE_FM 0x04
274#define VIA_FUNC_ENABLE_MIDI 0x02
275#define VIA_FUNC_ENABLE_SB 0x01
276#define VIA_PNP_CONTROL 0x43
277#define VIA_FM_NMI_CTRL 0x48
278#define VIA8233_VOLCHG_CTRL 0x48
279#define VIA8233_SPDIF_CTRL 0x49
280#define VIA8233_SPDIF_DX3 0x08
281#define VIA8233_SPDIF_SLOT_MASK 0x03
282#define VIA8233_SPDIF_SLOT_1011 0x00
283#define VIA8233_SPDIF_SLOT_34 0x01
284#define VIA8233_SPDIF_SLOT_78 0x02
285#define VIA8233_SPDIF_SLOT_69 0x03
286
287/*
288 */
289
290#define VIA_DXS_AUTO 0
291#define VIA_DXS_ENABLE 1
292#define VIA_DXS_DISABLE 2
293#define VIA_DXS_48K 3
294#define VIA_DXS_NO_VRA 4
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200295#define VIA_DXS_SRC 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297
298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * pcm stream
300 */
301
302struct snd_via_sg_table {
303 unsigned int offset;
304 unsigned int size;
305} ;
306
307#define VIA_TABLE_SIZE 255
Takashi Iwai55036002008-07-30 15:23:06 +0200308#define VIA_MAX_BUFSIZE (1<<24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100310struct viadev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned int reg_offset;
312 unsigned long port;
313 int direction; /* playback = 0, capture = 1 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100314 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int running;
316 unsigned int tbl_entries; /* # descriptors */
317 struct snd_dma_buffer table;
318 struct snd_via_sg_table *idx_table;
319 /* for recovery from the unexpected pointer */
320 unsigned int lastpos;
321 unsigned int fragsize;
322 unsigned int bufsize;
323 unsigned int bufsize2;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200324 int hwptr_done; /* processed frame position in the buffer */
325 int in_interrupt;
326 int shadow_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329
330enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
331enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
332
333#define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
334
335struct via_rate_lock {
336 spinlock_t lock;
337 int rate;
338 int used;
339};
340
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100341struct via82xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int irq;
343
344 unsigned long port;
345 struct resource *mpu_res;
346 int chip_type;
347 unsigned char revision;
348
349 unsigned char old_legacy;
350 unsigned char old_legacy_cfg;
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200351#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned char legacy_saved;
353 unsigned char legacy_cfg_saved;
354 unsigned char spdif_ctrl_saved;
355 unsigned char capture_src_saved[2];
356 unsigned int mpu_port_saved;
357#endif
358
Honza Maly00f226d2005-10-14 18:18:01 +0200359 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
360 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
363
364 struct pci_dev *pci;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100365 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 unsigned int num_devs;
368 unsigned int playback_devno, multi_devno, capture_devno;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100369 struct viadev devs[VIA_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct via_rate_lock rates[2]; /* playback and capture */
371 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
372 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +0200373 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
375
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100376 struct snd_pcm *pcms[2];
377 struct snd_rawmidi *rmidi;
Clemens Ladisch3d009412009-10-22 09:04:09 +0200378 struct snd_kcontrol *dxs_controls[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100380 struct snd_ac97_bus *ac97_bus;
381 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned int ac97_clock;
383 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
384
385 spinlock_t reg_lock;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100386 struct snd_info_entry *proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388#ifdef SUPPORT_JOYSTICK
389 struct gameport *gameport;
390#endif
391};
392
Benoit Taine9baa3c32014-08-08 15:56:03 +0200393static const struct pci_device_id snd_via82xx_ids[] = {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200394 /* 0x1106, 0x3058 */
Joe Perches28d27aa2009-06-24 22:13:35 -0700395 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C686_5), TYPE_CARD_VIA686, }, /* 686A */
Karsten Wiese4f550df2005-10-18 14:31:07 +0200396 /* 0x1106, 0x3059 */
Joe Perches28d27aa2009-06-24 22:13:35 -0700397 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_8233_5), TYPE_CARD_VIA8233, }, /* VT8233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 { 0, }
399};
400
401MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
402
403/*
404 */
405
406/*
407 * allocate and initialize the descriptor buffers
408 * periods = number of periods
409 * fragsize = period size in bytes
410 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100411static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct pci_dev *pci,
413 unsigned int periods, unsigned int fragsize)
414{
415 unsigned int i, idx, ofs, rest;
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100416 struct via82xx *chip = snd_pcm_substream_chip(substream);
Takashi Iwaic5bb0862020-02-06 17:31:50 +0100417 __le32 *pgtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (dev->table.area == NULL) {
420 /* the start of each lists must be aligned to 8 bytes,
421 * but the kernel pages are much bigger, so we don't care
422 */
Takashi Iwai6974f8a2019-11-05 16:18:55 +0100423 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
425 &dev->table) < 0)
426 return -ENOMEM;
427 }
428 if (! dev->idx_table) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700429 dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
430 sizeof(*dev->idx_table),
431 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (! dev->idx_table)
433 return -ENOMEM;
434 }
435
436 /* fill the entries */
437 idx = 0;
438 ofs = 0;
Takashi Iwaic5bb0862020-02-06 17:31:50 +0100439 pgtbl = (__le32 *)dev->table.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 for (i = 0; i < periods; i++) {
441 rest = fragsize;
442 /* fill descriptors for a period.
443 * a period can be split to several descriptors if it's
444 * over page boundary.
445 */
446 do {
447 unsigned int r;
448 unsigned int flag;
Takashi Iwai77a23f22008-08-21 13:00:13 +0200449 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (idx >= VIA_TABLE_SIZE) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100452 dev_err(&pci->dev, "too much table size!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EINVAL;
454 }
Takashi Iwai77a23f22008-08-21 13:00:13 +0200455 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
Takashi Iwaic5bb0862020-02-06 17:31:50 +0100456 pgtbl[idx << 1] = cpu_to_le32(addr);
Takashi Iwai55036002008-07-30 15:23:06 +0200457 r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 rest -= r;
459 if (! rest) {
460 if (i == periods - 1)
461 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
462 else
463 flag = VIA_TBL_BIT_FLAG; /* period boundary */
464 } else
465 flag = 0; /* period continues to the next */
Takashi Iwaiee419652009-02-05 16:11:31 +0100466 /*
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100467 dev_dbg(&pci->dev,
468 "tbl %d: at %d size %d (rest %d)\n",
469 idx, ofs, r, rest);
Takashi Iwaiee419652009-02-05 16:11:31 +0100470 */
Takashi Iwaic5bb0862020-02-06 17:31:50 +0100471 pgtbl[(idx<<1) + 1] = cpu_to_le32(r | flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 dev->idx_table[idx].offset = ofs;
473 dev->idx_table[idx].size = r;
474 ofs += r;
475 idx++;
476 } while (rest > 0);
477 }
478 dev->tbl_entries = idx;
479 dev->bufsize = periods * fragsize;
480 dev->bufsize2 = dev->bufsize / 2;
481 dev->fragsize = fragsize;
482 return 0;
483}
484
485
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100486static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct pci_dev *pci)
488{
489 if (dev->table.area) {
490 snd_dma_free_pages(&dev->table);
491 dev->table.area = NULL;
492 }
Jesper Juhl4d572772005-05-30 17:30:32 +0200493 kfree(dev->idx_table);
494 dev->idx_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496}
497
498/*
499 * Basic I/O
500 */
501
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100502static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 return inl(VIAREG(chip, AC97));
505}
506
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100507static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 outl(val, VIAREG(chip, AC97));
510}
511
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100512static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int timeout = 1000; /* 1ms */
515 unsigned int val;
516
517 while (timeout-- > 0) {
518 udelay(1);
519 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
520 return val & 0xffff;
521 }
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100522 dev_err(chip->card->dev, "codec_ready: codec %i is not ready [0x%x]\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100523 secondary, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -EIO;
525}
526
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100527static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 unsigned int timeout = 1000; /* 1ms */
530 unsigned int val, val1;
531 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
532 VIA_REG_AC97_SECONDARY_VALID;
533
534 while (timeout-- > 0) {
535 val = snd_via82xx_codec_xread(chip);
536 val1 = val & (VIA_REG_AC97_BUSY | stat);
537 if (val1 == stat)
538 return val & 0xffff;
539 udelay(1);
540 }
541 return -EIO;
542}
543
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100544static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100546 struct via82xx *chip = ac97->private_data;
Pierre-Louis Bossart86a5d9c2020-07-02 14:36:01 -0500547 __always_unused int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 err = snd_via82xx_codec_ready(chip, ac97->num);
549 /* here we need to wait fairly for long time.. */
Simon Arlott115551d2009-06-21 19:50:48 +0100550 if (!nodelay)
551 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100554static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 unsigned short reg,
556 unsigned short val)
557{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100558 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 unsigned int xval;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
562 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
563 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
564 xval |= val << VIA_REG_AC97_DATA_SHIFT;
565 snd_via82xx_codec_xwrite(chip, xval);
566 snd_via82xx_codec_ready(chip, ac97->num);
567}
568
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100569static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100571 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unsigned int xval, val = 0xffff;
573 int again = 0;
574
575 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
576 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
577 xval |= VIA_REG_AC97_READ;
578 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
579 while (1) {
580 if (again++ > 3) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100581 dev_err(chip->card->dev,
582 "codec_read: codec %i is not valid [0x%x]\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100583 ac97->num, snd_via82xx_codec_xread(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 0xffff;
585 }
586 snd_via82xx_codec_xwrite(chip, xval);
587 udelay (20);
588 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
589 udelay(25);
590 val = snd_via82xx_codec_xread(chip);
591 break;
592 }
593 }
594 return val & 0xffff;
595}
596
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100597static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
600 VIADEV_REG(viadev, OFFSET_CONTROL));
601 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
602 udelay(50);
603 /* disable interrupts */
604 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
605 /* clear interrupts */
606 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
607 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
608 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
609 viadev->lastpos = 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200610 viadev->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613
614/*
615 * Interrupt handler
Karsten Wiese4f550df2005-10-18 14:31:07 +0200616 * Used for 686 and 8233A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
David Howells7d12e782006-10-05 14:55:46 +0100618static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100620 struct via82xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned int status;
622 unsigned int i;
623
624 status = inl(VIAREG(chip, SGD_SHADOW));
625 if (! (status & chip->intr_mask)) {
626 if (chip->rmidi)
627 /* check mpu401 interrupt */
David Howells7d12e782006-10-05 14:55:46 +0100628 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return IRQ_NONE;
630 }
631
632 /* check status for each stream */
633 spin_lock(&chip->reg_lock);
634 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100635 struct viadev *viadev = &chip->devs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
Karsten Wiese4f550df2005-10-18 14:31:07 +0200637 if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 continue;
639 if (viadev->substream && viadev->running) {
Karsten Wiese4f550df2005-10-18 14:31:07 +0200640 /*
641 * Update hwptr_done based on 'period elapsed'
642 * interrupts. We'll use it, when the chip returns 0
643 * for OFFSET_CURR_COUNT.
644 */
645 if (c_status & VIA_REG_STAT_EOL)
646 viadev->hwptr_done = 0;
647 else
648 viadev->hwptr_done += viadev->fragsize;
649 viadev->in_interrupt = c_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 spin_unlock(&chip->reg_lock);
651 snd_pcm_period_elapsed(viadev->substream);
652 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200653 viadev->in_interrupt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
656 }
657 spin_unlock(&chip->reg_lock);
658 return IRQ_HANDLED;
659}
660
661/*
Karsten Wiese4f550df2005-10-18 14:31:07 +0200662 * Interrupt handler
663 */
David Howells7d12e782006-10-05 14:55:46 +0100664static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
Karsten Wiese4f550df2005-10-18 14:31:07 +0200665{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100666 struct via82xx *chip = dev_id;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200667 unsigned int status;
668 unsigned int i;
669 int irqreturn = 0;
670
671 /* check status for each stream */
672 spin_lock(&chip->reg_lock);
673 status = inl(VIAREG(chip, SGD_SHADOW));
674
675 for (i = 0; i < chip->num_devs; i++) {
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100676 struct viadev *viadev = &chip->devs[i];
677 struct snd_pcm_substream *substream;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200678 unsigned char c_status, shadow_status;
679
680 shadow_status = (status >> viadev->shadow_shift) &
681 (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
682 VIA_REG_STAT_FLAG);
683 c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
684 if (!c_status)
685 continue;
686
687 substream = viadev->substream;
688 if (substream && viadev->running) {
689 /*
690 * Update hwptr_done based on 'period elapsed'
691 * interrupts. We'll use it, when the chip returns 0
692 * for OFFSET_CURR_COUNT.
693 */
694 if (c_status & VIA_REG_STAT_EOL)
695 viadev->hwptr_done = 0;
696 else
697 viadev->hwptr_done += viadev->fragsize;
698 viadev->in_interrupt = c_status;
699 if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
700 viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
701 spin_unlock(&chip->reg_lock);
702
703 snd_pcm_period_elapsed(substream);
704
705 spin_lock(&chip->reg_lock);
706 viadev->in_interrupt = 0;
707 }
708 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
709 irqreturn = 1;
710 }
711 spin_unlock(&chip->reg_lock);
712 return IRQ_RETVAL(irqreturn);
713}
714
715/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * PCM callbacks
717 */
718
719/*
720 * trigger callback
721 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100722static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100724 struct via82xx *chip = snd_pcm_substream_chip(substream);
725 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned char val;
727
728 if (chip->chip_type != TYPE_VIA686)
729 val = VIA_REG_CTRL_INT;
730 else
731 val = 0;
732 switch (cmd) {
733 case SNDRV_PCM_TRIGGER_START:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200734 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 val |= VIA_REG_CTRL_START;
736 viadev->running = 1;
737 break;
738 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200739 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 val = VIA_REG_CTRL_TERMINATE;
741 viadev->running = 0;
742 break;
743 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
744 val |= VIA_REG_CTRL_PAUSE;
745 viadev->running = 0;
746 break;
747 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
748 viadev->running = 1;
749 break;
750 default:
751 return -EINVAL;
752 }
753 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
754 if (cmd == SNDRV_PCM_TRIGGER_STOP)
755 snd_via82xx_channel_reset(chip, viadev);
756 return 0;
757}
758
759
760/*
761 * pointer callbacks
762 */
763
764/*
765 * calculate the linear position at the given sg-buffer index and the rest count
766 */
767
768#define check_invalid_pos(viadev,pos) \
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100769 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
770 viadev->lastpos < viadev->bufsize2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100772static inline unsigned int calc_linear_pos(struct via82xx *chip,
773 struct viadev *viadev,
774 unsigned int idx,
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100775 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 unsigned int size, base, res;
778
779 size = viadev->idx_table[idx].size;
780 base = viadev->idx_table[idx].offset;
781 res = base + size - count;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200782 if (res >= viadev->bufsize)
783 res -= viadev->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* check the validity of the calculated position */
786 if (size < count) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100787 dev_dbg(chip->card->dev,
788 "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100789 (int)size, (int)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 res = viadev->lastpos;
791 } else {
792 if (! count) {
793 /* Some mobos report count = 0 on the DMA boundary,
794 * i.e. count = size indeed.
795 * Let's check whether this step is above the expected size.
796 */
797 int delta = res - viadev->lastpos;
798 if (delta < 0)
799 delta += viadev->bufsize;
800 if ((unsigned int)delta > viadev->fragsize)
801 res = base;
802 }
803 if (check_invalid_pos(viadev, res)) {
804#ifdef POINTER_DEBUG
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100805 dev_dbg(chip->card->dev,
806 "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n",
807 idx, viadev->tbl_entries,
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100808 viadev->lastpos, viadev->bufsize2,
809 viadev->idx_table[idx].offset,
810 viadev->idx_table[idx].size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811#endif
812 /* count register returns full size when end of buffer is reached */
813 res = base + size;
814 if (check_invalid_pos(viadev, res)) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100815 dev_dbg(chip->card->dev,
816 "invalid via82xx_cur_ptr (2), using last valid pointer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 res = viadev->lastpos;
818 }
819 }
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return res;
822}
823
824/*
825 * get the current pointer on via686
826 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100827static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100829 struct via82xx *chip = snd_pcm_substream_chip(substream);
830 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int idx, ptr, count, res;
832
Takashi Iwaida3cec32008-08-08 17:12:14 +0200833 if (snd_BUG_ON(!viadev->tbl_entries))
834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
836 return 0;
837
838 spin_lock(&chip->reg_lock);
839 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
840 /* The via686a does not have the current index register,
841 * so we need to calculate the index from CURR_PTR.
842 */
843 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
844 if (ptr <= (unsigned int)viadev->table.addr)
845 idx = 0;
846 else /* CURR_PTR holds the address + 8 */
847 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100848 res = calc_linear_pos(chip, viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200849 viadev->lastpos = res; /* remember the last position */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 spin_unlock(&chip->reg_lock);
851
852 return bytes_to_frames(substream->runtime, res);
853}
854
855/*
856 * get the current pointer on via823x
857 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100858static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100860 struct via82xx *chip = snd_pcm_substream_chip(substream);
861 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unsigned int idx, count, res;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200863 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Takashi Iwaida3cec32008-08-08 17:12:14 +0200865 if (snd_BUG_ON(!viadev->tbl_entries))
866 return 0;
Karsten Wiese4f550df2005-10-18 14:31:07 +0200867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 spin_lock(&chip->reg_lock);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200869 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
870 status = viadev->in_interrupt;
871 if (!status)
872 status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
873
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200874 /* An apparent bug in the 8251 is worked around by sending a
875 * REG_CTRL_START. */
876 if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
877 snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
878
Karsten Wiese4f550df2005-10-18 14:31:07 +0200879 if (!(status & VIA_REG_STAT_ACTIVE)) {
Bastiaan Jacquesc6cc0e32006-04-20 12:27:09 +0200880 res = 0;
881 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Karsten Wiese4f550df2005-10-18 14:31:07 +0200883 if (count & 0xffffff) {
884 idx = count >> 24;
885 if (idx >= viadev->tbl_entries) {
886#ifdef POINTER_DEBUG
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100887 dev_dbg(chip->card->dev,
888 "fail: invalid idx = %i/%i\n", idx,
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100889 viadev->tbl_entries);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200890#endif
891 res = viadev->lastpos;
892 } else {
893 count &= 0xffffff;
Takashi Iwai59d3acfa2014-02-25 15:21:56 +0100894 res = calc_linear_pos(chip, viadev, idx, count);
Karsten Wiese4f550df2005-10-18 14:31:07 +0200895 }
896 } else {
897 res = viadev->hwptr_done;
898 if (!viadev->in_interrupt) {
899 if (status & VIA_REG_STAT_EOL) {
900 res = 0;
901 } else
902 if (status & VIA_REG_STAT_FLAG) {
903 res += viadev->fragsize;
904 }
905 }
906 }
907unlock:
908 viadev->lastpos = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 spin_unlock(&chip->reg_lock);
910
911 return bytes_to_frames(substream->runtime, res);
912}
913
914
915/*
916 * hw_params callback:
917 * allocate the buffer and build up the buffer description table
918 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100919static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
920 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100922 struct via82xx *chip = snd_pcm_substream_chip(substream);
923 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Takashi Iwai8e2c7522019-12-09 10:49:27 +0100925 return build_via_table(viadev, substream, chip->pci,
926 params_periods(hw_params),
927 params_period_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/*
931 * hw_free callback:
932 * clean up the buffer description table and release the buffer
933 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100934static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100936 struct via82xx *chip = snd_pcm_substream_chip(substream);
937 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 clean_via_table(viadev, substream, chip->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
943
944/*
945 * set up the table pointer
946 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100947static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 snd_via82xx_codec_ready(chip, 0);
950 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
951 udelay(20);
952 snd_via82xx_codec_ready(chip, 0);
953}
954
955/*
956 * prepare callback for playback and capture on via686
957 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100958static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
959 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 snd_via82xx_channel_reset(chip, viadev);
962 /* this must be set after channel_reset */
963 snd_via82xx_set_table_ptr(chip, viadev);
964 outb(VIA_REG_TYPE_AUTOSTART |
965 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
966 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
967 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
968 VIA_REG_TYPE_INT_EOL |
969 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
970}
971
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100972static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100974 struct via82xx *chip = snd_pcm_substream_chip(substream);
975 struct viadev *viadev = substream->runtime->private_data;
976 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
979 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
980 via686_setup_format(chip, viadev, runtime);
981 return 0;
982}
983
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100984static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwaie437e3d2005-11-17 15:06:15 +0100986 struct via82xx *chip = snd_pcm_substream_chip(substream);
987 struct viadev *viadev = substream->runtime->private_data;
988 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
991 via686_setup_format(chip, viadev, runtime);
992 return 0;
993}
994
995/*
996 * lock the current rate
997 */
998static int via_lock_rate(struct via_rate_lock *rec, int rate)
999{
1000 int changed = 0;
1001
1002 spin_lock_irq(&rec->lock);
1003 if (rec->rate != rate) {
1004 if (rec->rate && rec->used > 1) /* already set */
1005 changed = -EINVAL;
1006 else {
1007 rec->rate = rate;
1008 changed = 1;
1009 }
1010 }
1011 spin_unlock_irq(&rec->lock);
1012 return changed;
1013}
1014
1015/*
1016 * prepare callback for DSX playback on via823x
1017 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001018static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001020 struct via82xx *chip = snd_pcm_substream_chip(substream);
1021 struct viadev *viadev = substream->runtime->private_data;
1022 struct snd_pcm_runtime *runtime = substream->runtime;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001023 int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int rate_changed;
1025 u32 rbits;
1026
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001027 if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return rate_changed;
Takashi Iwai16d3f142005-08-15 15:02:28 +02001029 if (rate_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1031 chip->no_vra ? 48000 : runtime->rate);
Takashi Iwai16d3f142005-08-15 15:02:28 +02001032 if (chip->spdif_on && viadev->reg_offset == 0x30)
1033 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (runtime->rate == 48000)
1036 rbits = 0xfffff;
1037 else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001038 rbits = (0x100000 / 48000) * runtime->rate +
1039 ((0x100000 % 48000) * runtime->rate) / 48000;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001040 snd_BUG_ON(rbits & ~0xfffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 snd_via82xx_channel_reset(chip, viadev);
1042 snd_via82xx_set_table_ptr(chip, viadev);
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001043 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1044 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1045 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1046 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1048 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1049 rbits | /* rate */
1050 0xff000000, /* STOP index is never reached */
1051 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1052 udelay(20);
1053 snd_via82xx_codec_ready(chip, 0);
1054 return 0;
1055}
1056
1057/*
1058 * prepare callback for multi-channel playback on via823x
1059 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001060static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001062 struct via82xx *chip = snd_pcm_substream_chip(substream);
1063 struct viadev *viadev = substream->runtime->private_data;
1064 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 unsigned int slots;
1066 int fmt;
1067
1068 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1069 return -EINVAL;
1070 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1071 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1072 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1073 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1074 snd_via82xx_channel_reset(chip, viadev);
1075 snd_via82xx_set_table_ptr(chip, viadev);
1076
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001077 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1078 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 fmt |= runtime->channels << 4;
1080 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1081#if 0
1082 if (chip->revision == VIA_REV_8233A)
1083 slots = 0;
1084 else
1085#endif
1086 {
1087 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1088 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1089 switch (runtime->channels) {
1090 case 1: slots = (1<<0) | (1<<4); break;
1091 case 2: slots = (1<<0) | (2<<4); break;
1092 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1093 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1094 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1095 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1096 default: slots = 0; break;
1097 }
1098 }
1099 /* STOP index is never reached */
1100 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1101 udelay(20);
1102 snd_via82xx_codec_ready(chip, 0);
1103 return 0;
1104}
1105
1106/*
1107 * prepare callback for capture on via823x
1108 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001109static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001111 struct via82xx *chip = snd_pcm_substream_chip(substream);
1112 struct viadev *viadev = substream->runtime->private_data;
1113 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1116 return -EINVAL;
1117 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1118 snd_via82xx_channel_reset(chip, viadev);
1119 snd_via82xx_set_table_ptr(chip, viadev);
1120 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1121 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1122 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1123 0xff000000, /* STOP index is never reached */
1124 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1125 udelay(20);
1126 snd_via82xx_codec_ready(chip, 0);
1127 return 0;
1128}
1129
1130
1131/*
1132 * pcm hardware definition, identical for both playback and capture
1133 */
Bhumika Goyaldee49892017-08-12 21:01:28 +05301134static const struct snd_pcm_hardware snd_via82xx_hw =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1137 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1138 SNDRV_PCM_INFO_MMAP_VALID |
Jaroslav Kysela41e48452005-08-18 13:43:12 +02001139 /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 SNDRV_PCM_INFO_PAUSE),
1141 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1142 .rates = SNDRV_PCM_RATE_48000,
1143 .rate_min = 48000,
1144 .rate_max = 48000,
1145 .channels_min = 1,
1146 .channels_max = 2,
Takashi Iwai55036002008-07-30 15:23:06 +02001147 .buffer_bytes_max = VIA_MAX_BUFSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 .period_bytes_min = 32,
Takashi Iwai55036002008-07-30 15:23:06 +02001149 .period_bytes_max = VIA_MAX_BUFSIZE / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 .periods_min = 2,
1151 .periods_max = VIA_TABLE_SIZE / 2,
1152 .fifo_size = 0,
1153};
1154
1155
1156/*
1157 * open callback skeleton
1158 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001159static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1160 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001162 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int err;
1164 struct via_rate_lock *ratep;
Clemens Ladisch5495ffb2011-09-16 23:16:05 +02001165 bool use_src = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 runtime->hw = snd_via82xx_hw;
1168
1169 /* set the hw rate condition */
1170 ratep = &chip->rates[viadev->direction];
1171 spin_lock_irq(&ratep->lock);
1172 ratep->used++;
1173 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1174 /* DXS#3 and spdif is on */
1175 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1176 snd_pcm_limit_hw_rates(runtime);
1177 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1178 /* fixed DXS playback rate */
1179 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1180 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02001181 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1182 /* use full SRC capabilities of DXS */
1183 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1184 SNDRV_PCM_RATE_8000_48000);
1185 runtime->hw.rate_min = 8000;
1186 runtime->hw.rate_max = 48000;
Clemens Ladisch5495ffb2011-09-16 23:16:05 +02001187 use_src = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 } else if (! ratep->rate) {
1189 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1190 runtime->hw.rates = chip->ac97->rates[idx];
1191 snd_pcm_limit_hw_rates(runtime);
1192 } else {
1193 /* a fixed rate */
1194 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1195 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1196 }
1197 spin_unlock_irq(&ratep->lock);
1198
1199 /* we may remove following constaint when we modify table entries
1200 in interrupt */
1201 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1202 return err;
1203
Clemens Ladisch5495ffb2011-09-16 23:16:05 +02001204 if (use_src) {
1205 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1206 if (err < 0)
1207 return err;
1208 }
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 runtime->private_data = viadev;
1211 viadev->substream = substream;
1212
1213 return 0;
1214}
1215
1216
1217/*
Clemens Ladisch3d009412009-10-22 09:04:09 +02001218 * open callback for playback on via686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Clemens Ladisch3d009412009-10-22 09:04:09 +02001220static int snd_via686_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001222 struct via82xx *chip = snd_pcm_substream_chip(substream);
1223 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int err;
1225
1226 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1227 return err;
1228 return 0;
1229}
1230
1231/*
Clemens Ladisch3d009412009-10-22 09:04:09 +02001232 * open callback for playback on via823x DXS
1233 */
1234static int snd_via8233_playback_open(struct snd_pcm_substream *substream)
1235{
1236 struct via82xx *chip = snd_pcm_substream_chip(substream);
1237 struct viadev *viadev;
1238 unsigned int stream;
1239 int err;
1240
1241 viadev = &chip->devs[chip->playback_devno + substream->number];
1242 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1243 return err;
1244 stream = viadev->reg_offset / 0x10;
1245 if (chip->dxs_controls[stream]) {
Clemens Ladisch395c61d2010-07-12 16:27:24 +02001246 chip->playback_volume[stream][0] =
1247 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1248 chip->playback_volume[stream][1] =
1249 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
Clemens Ladisch3d009412009-10-22 09:04:09 +02001250 chip->dxs_controls[stream]->vd[0].access &=
1251 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1252 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1253 SNDRV_CTL_EVENT_MASK_INFO,
1254 &chip->dxs_controls[stream]->id);
1255 }
1256 return 0;
1257}
1258
1259/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * open callback for playback on via823x multi-channel
1261 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001262static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001264 struct via82xx *chip = snd_pcm_substream_chip(substream);
1265 struct viadev *viadev = &chip->devs[chip->multi_devno];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int err;
1267 /* channels constraint for VIA8233A
1268 * 3 and 5 channels are not supported
1269 */
Takashi Iwaifbc57b22017-06-07 14:23:13 +02001270 static const unsigned int channels[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 1, 2, 4, 6
1272 };
Takashi Iwaifbc57b22017-06-07 14:23:13 +02001273 static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 .count = ARRAY_SIZE(channels),
1275 .list = channels,
1276 .mask = 0,
1277 };
1278
1279 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1280 return err;
1281 substream->runtime->hw.channels_max = 6;
1282 if (chip->revision == VIA_REV_8233A)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001283 snd_pcm_hw_constraint_list(substream->runtime, 0,
1284 SNDRV_PCM_HW_PARAM_CHANNELS,
1285 &hw_constraints_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
1288
1289/*
1290 * open callback for capture on via686 and via823x
1291 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001292static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001294 struct via82xx *chip = snd_pcm_substream_chip(substream);
1295 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 return snd_via82xx_pcm_open(chip, viadev, substream);
1298}
1299
1300/*
1301 * close callback
1302 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001303static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001305 struct via82xx *chip = snd_pcm_substream_chip(substream);
1306 struct viadev *viadev = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct via_rate_lock *ratep;
1308
1309 /* release the rate lock */
1310 ratep = &chip->rates[viadev->direction];
1311 spin_lock_irq(&ratep->lock);
1312 ratep->used--;
1313 if (! ratep->used)
1314 ratep->rate = 0;
1315 spin_unlock_irq(&ratep->lock);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02001316 if (! ratep->rate) {
1317 if (! viadev->direction) {
1318 snd_ac97_update_power(chip->ac97,
1319 AC97_PCM_FRONT_DAC_RATE, 0);
1320 snd_ac97_update_power(chip->ac97,
1321 AC97_PCM_SURR_DAC_RATE, 0);
1322 snd_ac97_update_power(chip->ac97,
1323 AC97_PCM_LFE_DAC_RATE, 0);
1324 } else
1325 snd_ac97_update_power(chip->ac97,
1326 AC97_PCM_LR_ADC_RATE, 0);
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 viadev->substream = NULL;
1329 return 0;
1330}
1331
Clemens Ladisch3d009412009-10-22 09:04:09 +02001332static int snd_via8233_playback_close(struct snd_pcm_substream *substream)
1333{
1334 struct via82xx *chip = snd_pcm_substream_chip(substream);
1335 struct viadev *viadev = substream->runtime->private_data;
1336 unsigned int stream;
1337
1338 stream = viadev->reg_offset / 0x10;
1339 if (chip->dxs_controls[stream]) {
1340 chip->dxs_controls[stream]->vd[0].access |=
1341 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1342 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1343 &chip->dxs_controls[stream]->id);
1344 }
1345 return snd_via82xx_pcm_close(substream);
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349/* via686 playback callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001350static const struct snd_pcm_ops snd_via686_playback_ops = {
Clemens Ladisch3d009412009-10-22 09:04:09 +02001351 .open = snd_via686_playback_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 .close = snd_via82xx_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 .hw_params = snd_via82xx_hw_params,
1354 .hw_free = snd_via82xx_hw_free,
1355 .prepare = snd_via686_playback_prepare,
1356 .trigger = snd_via82xx_pcm_trigger,
1357 .pointer = snd_via686_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358};
1359
1360/* via686 capture callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001361static const struct snd_pcm_ops snd_via686_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 .open = snd_via82xx_capture_open,
1363 .close = snd_via82xx_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 .hw_params = snd_via82xx_hw_params,
1365 .hw_free = snd_via82xx_hw_free,
1366 .prepare = snd_via686_capture_prepare,
1367 .trigger = snd_via82xx_pcm_trigger,
1368 .pointer = snd_via686_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369};
1370
1371/* via823x DSX playback callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001372static const struct snd_pcm_ops snd_via8233_playback_ops = {
Clemens Ladisch3d009412009-10-22 09:04:09 +02001373 .open = snd_via8233_playback_open,
1374 .close = snd_via8233_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 .hw_params = snd_via82xx_hw_params,
1376 .hw_free = snd_via82xx_hw_free,
1377 .prepare = snd_via8233_playback_prepare,
1378 .trigger = snd_via82xx_pcm_trigger,
1379 .pointer = snd_via8233_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380};
1381
1382/* via823x multi-channel playback callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001383static const struct snd_pcm_ops snd_via8233_multi_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .open = snd_via8233_multi_open,
1385 .close = snd_via82xx_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 .hw_params = snd_via82xx_hw_params,
1387 .hw_free = snd_via82xx_hw_free,
1388 .prepare = snd_via8233_multi_prepare,
1389 .trigger = snd_via82xx_pcm_trigger,
1390 .pointer = snd_via8233_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391};
1392
1393/* via823x capture callbacks */
Julia Lawall6769e9882016-09-02 00:13:10 +02001394static const struct snd_pcm_ops snd_via8233_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 .open = snd_via82xx_capture_open,
1396 .close = snd_via82xx_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 .hw_params = snd_via82xx_hw_params,
1398 .hw_free = snd_via82xx_hw_free,
1399 .prepare = snd_via8233_capture_prepare,
1400 .trigger = snd_via82xx_pcm_trigger,
1401 .pointer = snd_via8233_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402};
1403
1404
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001405static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1406 int shadow_pos, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 chip->devs[idx].reg_offset = reg_offset;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001409 chip->devs[idx].shadow_shift = shadow_pos * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 chip->devs[idx].direction = direction;
1411 chip->devs[idx].port = chip->port + reg_offset;
1412}
1413
1414/*
1415 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1416 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001417static int snd_via8233_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001419 struct snd_pcm *pcm;
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001420 struct snd_pcm_chmap *chmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int i, err;
1422
1423 chip->playback_devno = 0; /* x 4 */
1424 chip->multi_devno = 4; /* x 1 */
1425 chip->capture_devno = 5; /* x 2 */
1426 chip->num_devs = 7;
1427 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1428
1429 /* PCM #0: 4 DSX playbacks and 1 capture */
1430 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1431 if (err < 0)
1432 return err;
1433 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1434 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1435 pcm->private_data = chip;
1436 strcpy(pcm->name, chip->card->shortname);
1437 chip->pcms[0] = pcm;
1438 /* set up playbacks */
1439 for (i = 0; i < 4; i++)
Karsten Wiese4f550df2005-10-18 14:31:07 +02001440 init_viadev(chip, i, 0x10 * i, i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001442 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Takashi Iwai8e2c7522019-12-09 10:49:27 +01001444 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1445 &chip->pci->dev,
1446 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001448 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1449 snd_pcm_std_chmaps, 2, 0,
1450 &chmap);
1451 if (err < 0)
1452 return err;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* PCM #1: multi-channel playback and 2nd capture */
1455 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1456 if (err < 0)
1457 return err;
1458 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1459 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1460 pcm->private_data = chip;
1461 strcpy(pcm->name, chip->card->shortname);
1462 chip->pcms[1] = pcm;
1463 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001464 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* set up capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001466 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Takashi Iwai8e2c7522019-12-09 10:49:27 +01001468 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1469 &chip->pci->dev,
1470 64*1024, VIA_MAX_BUFSIZE);
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001471
1472 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1473 snd_pcm_alt_chmaps, 6, 0,
1474 &chmap);
1475 if (err < 0)
1476 return err;
1477 chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480}
1481
1482/*
1483 * create pcm instances for VIA8233A
1484 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001485static int snd_via8233a_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001487 struct snd_pcm *pcm;
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001488 struct snd_pcm_chmap *chmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 int err;
1490
1491 chip->multi_devno = 0;
1492 chip->playback_devno = 1;
1493 chip->capture_devno = 2;
1494 chip->num_devs = 3;
1495 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1496
1497 /* PCM #0: multi-channel playback and capture */
1498 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1499 if (err < 0)
1500 return err;
1501 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1502 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1503 pcm->private_data = chip;
1504 strcpy(pcm->name, chip->card->shortname);
1505 chip->pcms[0] = pcm;
1506 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001507 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* capture */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001509 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Takashi Iwai8e2c7522019-12-09 10:49:27 +01001511 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1512 &chip->pci->dev,
1513 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Takashi Iwaie36e3b82012-08-03 18:01:40 +02001515 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1516 snd_pcm_alt_chmaps, 6, 0,
1517 &chmap);
1518 if (err < 0)
1519 return err;
1520 chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /* SPDIF supported? */
1523 if (! ac97_can_spdif(chip->ac97))
1524 return 0;
1525
1526 /* PCM #1: DXS3 playback (for spdif) */
1527 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1528 if (err < 0)
1529 return err;
1530 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1531 pcm->private_data = chip;
1532 strcpy(pcm->name, chip->card->shortname);
1533 chip->pcms[1] = pcm;
1534 /* set up playback */
Karsten Wiese4f550df2005-10-18 14:31:07 +02001535 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Takashi Iwai8e2c7522019-12-09 10:49:27 +01001537 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1538 &chip->pci->dev,
1539 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return 0;
1541}
1542
1543/*
1544 * create a pcm instance for via686a/b
1545 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001546static int snd_via686_pcm_new(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001548 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int err;
1550
1551 chip->playback_devno = 0;
1552 chip->capture_devno = 1;
1553 chip->num_devs = 2;
1554 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1555
1556 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1557 if (err < 0)
1558 return err;
1559 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1560 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1561 pcm->private_data = chip;
1562 strcpy(pcm->name, chip->card->shortname);
1563 chip->pcms[0] = pcm;
Karsten Wiese4f550df2005-10-18 14:31:07 +02001564 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1565 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Takashi Iwai8e2c7522019-12-09 10:49:27 +01001567 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1568 &chip->pci->dev,
1569 64*1024, VIA_MAX_BUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return 0;
1571}
1572
1573
1574/*
1575 * Mixer part
1576 */
1577
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001578static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1579 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 /* formerly they were "Line" and "Mic", but it looks like that they
1582 * have nothing to do with the actual physical connections...
1583 */
Takashi Iwai9883ab92014-10-20 18:20:18 +02001584 static const char * const texts[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 "Input1", "Input2"
1586 };
Takashi Iwai9883ab92014-10-20 18:20:18 +02001587 return snd_ctl_enum_info(uinfo, 1, 2, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001590static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001593 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1595 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1596 return 0;
1597}
1598
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001599static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1600 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001602 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1604 u8 val, oval;
1605
1606 spin_lock_irq(&chip->reg_lock);
1607 oval = inb(port);
1608 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1609 if (ucontrol->value.enumerated.item[0])
1610 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1611 if (val != oval)
1612 outb(val, port);
1613 spin_unlock_irq(&chip->reg_lock);
1614 return val != oval;
1615}
1616
Bill Pembertone23e7a12012-12-06 12:35:10 -05001617static struct snd_kcontrol_new snd_via8233_capture_source = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .name = "Input Source Select",
1619 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1620 .info = snd_via8233_capture_source_info,
1621 .get = snd_via8233_capture_source_get,
1622 .put = snd_via8233_capture_source_put,
1623};
1624
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001625#define snd_via8233_dxs3_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001627static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1628 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001630 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 u8 val;
1632
1633 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1634 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1635 return 0;
1636}
1637
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001638static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001641 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 u8 val, oval;
1643
1644 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1645 val = oval & ~VIA8233_SPDIF_DX3;
1646 if (ucontrol->value.integer.value[0])
1647 val |= VIA8233_SPDIF_DX3;
1648 /* save the spdif flag for rate filtering */
1649 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1650 if (val != oval) {
1651 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1652 return 1;
1653 }
1654 return 0;
1655}
1656
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301657static const struct snd_kcontrol_new snd_via8233_dxs3_spdif_control = {
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001658 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1660 .info = snd_via8233_dxs3_spdif_info,
1661 .get = snd_via8233_dxs3_spdif_get,
1662 .put = snd_via8233_dxs3_spdif_put,
1663};
1664
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001665static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1666 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1669 uinfo->count = 2;
1670 uinfo->value.integer.min = 0;
1671 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1672 return 0;
1673}
1674
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001675static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1676 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001678 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001679 unsigned int idx = kcontrol->id.subdevice;
Honza Maly00f226d2005-10-14 18:18:01 +02001680
1681 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1682 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1683 return 0;
1684}
1685
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001686static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001688{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001689 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Honza Maly00f226d2005-10-14 18:18:01 +02001690 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1691 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return 0;
1693}
1694
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001695static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1696 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001698 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001699 unsigned int idx = kcontrol->id.subdevice;
Honza Maly00f226d2005-10-14 18:18:01 +02001700 unsigned long port = chip->port + 0x10 * idx;
1701 unsigned char val;
1702 int i, change = 0;
1703
1704 for (i = 0; i < 2; i++) {
1705 val = ucontrol->value.integer.value[i];
1706 if (val > VIA_DXS_MAX_VOLUME)
1707 val = VIA_DXS_MAX_VOLUME;
1708 val = VIA_DXS_MAX_VOLUME - val;
1709 change |= val != chip->playback_volume[idx][i];
1710 if (change) {
1711 chip->playback_volume[idx][i] = val;
1712 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1713 }
1714 }
1715 return change;
1716}
1717
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001718static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
Honza Maly00f226d2005-10-14 18:18:01 +02001720{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001721 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 unsigned int idx;
1723 unsigned char val;
1724 int i, change = 0;
1725
1726 for (i = 0; i < 2; i++) {
1727 val = ucontrol->value.integer.value[i];
1728 if (val > VIA_DXS_MAX_VOLUME)
1729 val = VIA_DXS_MAX_VOLUME;
1730 val = VIA_DXS_MAX_VOLUME - val;
Honza Maly00f226d2005-10-14 18:18:01 +02001731 if (val != chip->playback_volume_c[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 change = 1;
Honza Maly00f226d2005-10-14 18:18:01 +02001733 chip->playback_volume_c[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 for (idx = 0; idx < 4; idx++) {
1735 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02001736 chip->playback_volume[idx][i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1738 }
1739 }
1740 }
1741 return change;
1742}
1743
Clemens Ladischb452e082009-05-05 15:40:12 +02001744static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -4650, 150, 1);
Takashi Iwai7058c042006-08-21 18:44:54 +02001745
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301746static const struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 .name = "PCM Playback Volume",
1748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwai7058c042006-08-21 18:44:54 +02001749 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1750 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 .info = snd_via8233_dxs_volume_info,
Honza Maly00f226d2005-10-14 18:18:01 +02001752 .get = snd_via8233_pcmdxs_volume_get,
1753 .put = snd_via8233_pcmdxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001754 .tlv = { .p = db_scale_dxs }
Honza Maly00f226d2005-10-14 18:18:01 +02001755};
1756
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301757static const struct snd_kcontrol_new snd_via8233_dxs_volume_control = {
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001758 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1759 .device = 0,
1760 /* .subdevice set later */
1761 .name = "PCM Playback Volume",
Clemens Ladisch3d009412009-10-22 09:04:09 +02001762 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1763 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1764 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Honza Maly00f226d2005-10-14 18:18:01 +02001765 .info = snd_via8233_dxs_volume_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 .get = snd_via8233_dxs_volume_get,
1767 .put = snd_via8233_dxs_volume_put,
Takashi Iwai7058c042006-08-21 18:44:54 +02001768 .tlv = { .p = db_scale_dxs }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769};
1770
1771/*
1772 */
1773
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001774static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001776 struct via82xx *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 chip->ac97_bus = NULL;
1778}
1779
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001780static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001782 struct via82xx *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 chip->ac97 = NULL;
1784}
1785
Joe Percheseab0fbf2015-04-04 13:38:25 -07001786static const struct ac97_quirk ac97_quirks[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001788 .subvendor = 0x1106,
1789 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 .codec_id = 0x56494161, /* VT1612A */
1791 .name = "Soltek SL-75DRV5",
1792 .type = AC97_TUNE_NONE
1793 },
1794 { /* FIXME: which codec? */
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001795 .subvendor = 0x1106,
1796 .subdevice = 0x4161,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 .name = "ASRock K7VT2",
1798 .type = AC97_TUNE_HP_ONLY
1799 },
1800 {
Clemens Ladischbf30a432010-02-22 10:33:13 +01001801 .subvendor = 0x110a,
1802 .subdevice = 0x0079,
1803 .name = "Fujitsu Siemens D1289",
1804 .type = AC97_TUNE_HP_ONLY
1805 },
1806 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001807 .subvendor = 0x1019,
1808 .subdevice = 0x0a81,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 .name = "ECS K7VTA3",
1810 .type = AC97_TUNE_HP_ONLY
1811 },
1812 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001813 .subvendor = 0x1019,
1814 .subdevice = 0x0a85,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 .name = "ECS L7VMM2",
1816 .type = AC97_TUNE_HP_ONLY
1817 },
1818 {
Walter Sheets942fd1e2008-06-27 11:53:31 +02001819 .subvendor = 0x1019,
1820 .subdevice = 0x1841,
1821 .name = "ECS K7VTA3",
1822 .type = AC97_TUNE_HP_ONLY
1823 },
1824 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001825 .subvendor = 0x1849,
1826 .subdevice = 0x3059,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 .name = "ASRock K7VM2",
1828 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1829 },
1830 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001831 .subvendor = 0x14cd,
1832 .subdevice = 0x7002,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 .name = "Unknown",
1834 .type = AC97_TUNE_ALC_JACK
1835 },
1836 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001837 .subvendor = 0x1071,
1838 .subdevice = 0x8590,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 .name = "Mitac Mobo",
1840 .type = AC97_TUNE_ALC_JACK
1841 },
1842 {
Jaroslav Kysela69ad07c2005-05-30 14:48:16 +02001843 .subvendor = 0x161f,
1844 .subdevice = 0x202b,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 .name = "Arima Notebook",
1846 .type = AC97_TUNE_HP_ONLY,
1847 },
Takashi Iwaidac8ddd2005-12-14 12:00:46 +01001848 {
1849 .subvendor = 0x161f,
1850 .subdevice = 0x2032,
1851 .name = "Targa Traveller 811",
1852 .type = AC97_TUNE_HP_ONLY,
1853 },
Daniel T Chend4199f02006-06-16 16:21:54 +02001854 {
1855 .subvendor = 0x161f,
1856 .subdevice = 0x2032,
1857 .name = "m680x",
1858 .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1859 },
Takashi Iwai96745132007-10-25 11:46:24 +02001860 {
1861 .subvendor = 0x1297,
1862 .subdevice = 0xa232,
1863 .name = "Shuttle AK32VN",
1864 .type = AC97_TUNE_HP_ONLY
1865 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 { } /* terminator */
1867};
1868
Bill Pembertone23e7a12012-12-06 12:35:10 -05001869static int snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001871 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 int err;
Takashi Iwai51055da2020-01-03 09:16:43 +01001873 static const struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 .write = snd_via82xx_codec_write,
1875 .read = snd_via82xx_codec_read,
1876 .wait = snd_via82xx_codec_wait,
1877 };
1878
1879 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1880 return err;
1881 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1882 chip->ac97_bus->clock = chip->ac97_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 memset(&ac97, 0, sizeof(ac97));
1885 ac97.private_data = chip;
1886 ac97.private_free = snd_via82xx_mixer_free_ac97;
1887 ac97.pci = chip->pci;
Takashi Iwaif1a63a32006-10-24 18:25:29 +02001888 ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1890 return err;
1891
1892 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1893
1894 if (chip->chip_type != TYPE_VIA686) {
1895 /* use slot 10/11 */
1896 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1897 }
1898
1899 return 0;
1900}
1901
1902#ifdef SUPPORT_JOYSTICK
1903#define JOYSTICK_ADDR 0x200
Bill Pembertone23e7a12012-12-06 12:35:10 -05001904static int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct gameport *gp;
1907 struct resource *r;
1908
Clemens Ladischb7fe4622005-10-04 08:46:51 +02001909 if (!joystick)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return -ENODEV;
1911
1912 r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1913 if (!r) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01001914 dev_warn(chip->card->dev, "cannot reserve joystick port %#x\n",
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001915 JOYSTICK_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return -EBUSY;
1917 }
1918
1919 chip->gameport = gp = gameport_allocate_port();
1920 if (!gp) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01001921 dev_err(chip->card->dev,
1922 "cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02001923 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return -ENOMEM;
1925 }
1926
1927 gameport_set_name(gp, "VIA686 Gameport");
1928 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1929 gameport_set_dev_parent(gp, &chip->pci->dev);
1930 gp->io = JOYSTICK_ADDR;
1931 gameport_set_port_data(gp, r);
1932
1933 /* Enable legacy joystick port */
1934 *legacy |= VIA_FUNC_ENABLE_GAME;
1935 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1936
1937 gameport_register_port(chip->gameport);
1938
1939 return 0;
1940}
1941
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001942static void snd_via686_free_gameport(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 if (chip->gameport) {
1945 struct resource *r = gameport_get_port_data(chip->gameport);
1946
1947 gameport_unregister_port(chip->gameport);
1948 chip->gameport = NULL;
Takashi Iwaib1d57762005-10-10 11:56:31 +02001949 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951}
1952#else
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001953static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 return -ENOSYS;
1956}
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001957static inline void snd_via686_free_gameport(struct via82xx *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958#endif
1959
1960
1961/*
1962 *
1963 */
1964
Bill Pembertone23e7a12012-12-06 12:35:10 -05001965static int snd_via8233_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 int i, err, caps;
1968 unsigned char val;
1969
1970 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1971 for (i = 0; i < caps; i++) {
1972 snd_via8233_capture_source.index = i;
1973 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1974 if (err < 0)
1975 return err;
1976 }
1977 if (ac97_can_spdif(chip->ac97)) {
1978 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1979 if (err < 0)
1980 return err;
1981 }
1982 if (chip->chip_type != TYPE_VIA8233A) {
1983 /* when no h/w PCM volume control is found, use DXS volume control
1984 * as the PCM vol control
1985 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01001986 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 memset(&sid, 0, sizeof(sid));
1988 strcpy(sid.name, "PCM Playback Volume");
1989 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1990 if (! snd_ctl_find_id(chip->card, &sid)) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01001991 dev_info(chip->card->dev,
1992 "Using DXS as PCM Playback\n");
Honza Maly00f226d2005-10-14 18:18:01 +02001993 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1994 if (err < 0)
1995 return err;
1996 }
1997 else /* Using DXS when PCM emulation is enabled is really weird */
1998 {
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02001999 for (i = 0; i < 4; ++i) {
2000 struct snd_kcontrol *kctl;
2001
2002 kctl = snd_ctl_new1(
2003 &snd_via8233_dxs_volume_control, chip);
2004 if (!kctl)
2005 return -ENOMEM;
2006 kctl->id.subdevice = i;
2007 err = snd_ctl_add(chip->card, kctl);
2008 if (err < 0)
2009 return err;
Clemens Ladisch3d009412009-10-22 09:04:09 +02002010 chip->dxs_controls[i] = kctl;
Clemens Ladisch2fb930b2009-10-06 08:21:04 +02002011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 /* select spdif data slot 10/11 */
2015 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
2016 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
2017 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
2018 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
2019
2020 return 0;
2021}
2022
Bill Pembertone23e7a12012-12-06 12:35:10 -05002023static int snd_via686_init_misc(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 unsigned char legacy, legacy_cfg;
2026 int rev_h = 0;
2027
2028 legacy = chip->old_legacy;
2029 legacy_cfg = chip->old_legacy_cfg;
2030 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
2031 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
2032 if (chip->revision >= VIA_REV_686_H) {
2033 rev_h = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002034 if (mpu_port >= 0x200) { /* force MIDI */
2035 mpu_port &= 0xfffc;
2036 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002037#ifdef CONFIG_PM_SLEEP
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002038 chip->mpu_port_saved = mpu_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039#endif
2040 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002041 mpu_port = pci_resource_start(chip->pci, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 } else {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002044 switch (mpu_port) { /* force MIDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 case 0x300:
2046 case 0x310:
2047 case 0x320:
2048 case 0x330:
2049 legacy_cfg &= ~(3 << 2);
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002050 legacy_cfg |= (mpu_port & 0x0030) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 break;
2052 default: /* no, use BIOS settings */
2053 if (legacy & VIA_FUNC_ENABLE_MIDI)
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002054 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 break;
2056 }
2057 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002058 if (mpu_port >= 0x200 &&
2059 (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
2060 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (rev_h)
2062 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
2063 legacy |= VIA_FUNC_ENABLE_MIDI;
2064 } else {
2065 if (rev_h)
2066 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
2067 legacy &= ~VIA_FUNC_ENABLE_MIDI;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002068 mpu_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
2071 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2072 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
2073 if (chip->mpu_res) {
2074 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002075 mpu_port, MPU401_INFO_INTEGRATED |
2076 MPU401_INFO_IRQ_HOOK, -1,
2077 &chip->rmidi) < 0) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002078 dev_warn(chip->card->dev,
2079 "unable to initialize MPU-401 at 0x%lx, skipping\n",
2080 mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 legacy &= ~VIA_FUNC_ENABLE_MIDI;
2082 } else {
2083 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
2084 }
2085 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2086 }
2087
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002088 snd_via686_create_gameport(chip, &legacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002090#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 chip->legacy_saved = legacy;
2092 chip->legacy_cfg_saved = legacy_cfg;
2093#endif
2094
2095 return 0;
2096}
2097
2098
2099/*
2100 * proc interface
2101 */
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002102static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2103 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002105 struct via82xx *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int i;
2107
2108 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2109 for (i = 0; i < 0xa0; i += 4) {
2110 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2111 }
2112}
2113
Bill Pembertone23e7a12012-12-06 12:35:10 -05002114static void snd_via82xx_proc_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Takashi Iwai47f27692019-02-04 16:01:39 +01002116 snd_card_ro_proc_new(chip->card, "via82xx", chip,
2117 snd_via82xx_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120/*
2121 *
2122 */
2123
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002124static int snd_via82xx_chip_init(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 unsigned int val;
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002127 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 unsigned char pval;
2129
2130#if 0 /* broken on K7M? */
2131 if (chip->chip_type == TYPE_VIA686)
2132 /* disable all legacy ports */
2133 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2134#endif
2135 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2136 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2137 /* deassert ACLink reset, force SYNC */
2138 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2139 VIA_ACLINK_CTRL_ENABLE |
2140 VIA_ACLINK_CTRL_RESET |
2141 VIA_ACLINK_CTRL_SYNC);
2142 udelay(100);
2143#if 1 /* FIXME: should we do full reset here for all chip models? */
2144 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2145 udelay(100);
2146#else
2147 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2148 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2149 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2150 udelay(2);
2151#endif
2152 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2153 /* note - FM data out has trouble with non VRA codecs !! */
2154 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2155 udelay(100);
2156 }
2157
2158 /* Make sure VRA is enabled, in case we didn't do a
2159 * complete codec reset, above */
2160 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2161 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2162 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2163 /* note - FM data out has trouble with non VRA codecs !! */
2164 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2165 udelay(100);
2166 }
2167
2168 /* wait until codec ready */
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002169 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 do {
2171 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2172 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2173 break;
Rene Hermand86d0192007-09-18 18:10:49 +02002174 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002175 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002178 dev_err(chip->card->dev,
2179 "AC'97 codec is not ready [0x%x]\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2182 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2183 VIA_REG_AC97_SECONDARY_VALID |
2184 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002185 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2187 VIA_REG_AC97_SECONDARY_VALID |
2188 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2189 do {
2190 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2191 chip->ac97_secondary = 1;
2192 goto __ac97_ok2;
2193 }
Rene Hermand86d0192007-09-18 18:10:49 +02002194 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002195 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* This is ok, the most of motherboards have only one codec */
2197
2198 __ac97_ok2:
2199#endif
2200
2201 if (chip->chip_type == TYPE_VIA686) {
2202 /* route FM trap to IRQ, disable FM trap */
2203 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2204 /* disable all GPI interrupts */
2205 outl(0, VIAREG(chip, GPI_INTR));
2206 }
2207
2208 if (chip->chip_type != TYPE_VIA686) {
2209 /* Workaround for Award BIOS bug:
2210 * DXS channels don't work properly with VRA if MC97 is disabled.
2211 */
2212 struct pci_dev *pci;
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002213 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (pci) {
2215 unsigned char data;
2216 pci_read_config_byte(pci, 0x44, &data);
2217 pci_write_config_byte(pci, 0x44, data | 0x40);
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002218 pci_dev_put(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
2220 }
2221
2222 if (chip->chip_type != TYPE_VIA8233A) {
2223 int i, idx;
2224 for (idx = 0; idx < 4; idx++) {
2225 unsigned long port = chip->port + 0x10 * idx;
Honza Maly00f226d2005-10-14 18:18:01 +02002226 for (i = 0; i < 2; i++) {
2227 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002228 outb(chip->playback_volume_c[i],
2229 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
Honza Maly00f226d2005-10-14 18:18:01 +02002230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 }
2233
2234 return 0;
2235}
2236
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002237#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/*
2239 * power management
2240 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002241static int snd_via82xx_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002243 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai57feb832005-11-17 16:10:35 +01002244 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 int i;
2246
Takashi Iwai57feb832005-11-17 16:10:35 +01002247 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 for (i = 0; i < chip->num_devs; i++)
2249 snd_via82xx_channel_reset(chip, &chip->devs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 snd_ac97_suspend(chip->ac97);
2251
2252 /* save misc values */
2253 if (chip->chip_type != TYPE_VIA686) {
2254 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2255 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2256 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2257 }
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return 0;
2260}
2261
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002262static int snd_via82xx_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002264 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai57feb832005-11-17 16:10:35 +01002265 struct via82xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 int i;
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 snd_via82xx_chip_init(chip);
2269
2270 if (chip->chip_type == TYPE_VIA686) {
2271 if (chip->mpu_port_saved)
2272 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2273 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2274 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2275 } else {
2276 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2277 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2278 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2279 }
2280
2281 snd_ac97_resume(chip->ac97);
2282
2283 for (i = 0; i < chip->num_devs; i++)
2284 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2285
Takashi Iwai57feb832005-11-17 16:10:35 +01002286 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
2288}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002289
2290static SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
2291#define SND_VIA82XX_PM_OPS &snd_via82xx_pm
2292#else
2293#define SND_VIA82XX_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002294#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002296static int snd_via82xx_free(struct via82xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 unsigned int i;
2299
2300 if (chip->irq < 0)
2301 goto __end_hw;
2302 /* disable interrupts */
2303 for (i = 0; i < chip->num_devs; i++)
2304 snd_via82xx_channel_reset(chip, &chip->devs[i]);
Jeff Garzikf000fd82008-04-22 13:50:34 +02002305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (chip->irq >= 0)
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002307 free_irq(chip->irq, chip);
Marcin Åšlusarz757d5a72007-12-14 12:51:24 +01002308 __end_hw:
Takashi Iwaib1d57762005-10-10 11:56:31 +02002309 release_and_free_resource(chip->mpu_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pci_release_regions(chip->pci);
2311
2312 if (chip->chip_type == TYPE_VIA686) {
2313 snd_via686_free_gameport(chip);
2314 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2315 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2316 }
2317 pci_disable_device(chip->pci);
2318 kfree(chip);
2319 return 0;
2320}
2321
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002322static int snd_via82xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002324 struct via82xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return snd_via82xx_free(chip);
2326}
2327
Bill Pembertone23e7a12012-12-06 12:35:10 -05002328static int snd_via82xx_create(struct snd_card *card,
2329 struct pci_dev *pci,
2330 int chip_type,
2331 int revision,
2332 unsigned int ac97_clock,
2333 struct via82xx **r_via)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002335 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 int err;
Takashi Iwaiefb0ad22020-01-03 09:16:25 +01002337 static const struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 .dev_free = snd_via82xx_dev_free,
2339 };
2340
2341 if ((err = pci_enable_device(pci)) < 0)
2342 return err;
2343
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002344 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 pci_disable_device(pci);
2346 return -ENOMEM;
2347 }
2348
2349 chip->chip_type = chip_type;
2350 chip->revision = revision;
2351
2352 spin_lock_init(&chip->reg_lock);
2353 spin_lock_init(&chip->rates[0].lock);
2354 spin_lock_init(&chip->rates[1].lock);
2355 chip->card = card;
2356 chip->pci = pci;
2357 chip->irq = -1;
2358
2359 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2360 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2361 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2362 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2363
2364 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2365 kfree(chip);
2366 pci_disable_device(pci);
2367 return err;
2368 }
2369 chip->port = pci_resource_start(pci, 0);
Karsten Wiese4f550df2005-10-18 14:31:07 +02002370 if (request_irq(pci->irq,
2371 chip_type == TYPE_VIA8233 ?
2372 snd_via8233_interrupt : snd_via686_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002373 IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002374 KBUILD_MODNAME, chip)) {
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002375 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 snd_via82xx_free(chip);
2377 return -EBUSY;
2378 }
2379 chip->irq = pci->irq;
Takashi Iwaic47583b2019-12-10 07:34:36 +01002380 card->sync_irq = chip->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2382 chip->ac97_clock = ac97_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2385 snd_via82xx_free(chip);
2386 return err;
2387 }
2388
2389 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2390 snd_via82xx_free(chip);
2391 return err;
2392 }
2393
2394 /* The 8233 ac97 controller does not implement the master bit
2395 * in the pci command register. IMHO this is a violation of the PCI spec.
2396 * We call pci_set_master here because it does not hurt. */
2397 pci_set_master(pci);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 *r_via = chip;
2400 return 0;
2401}
2402
2403struct via823x_info {
2404 int revision;
2405 char *name;
2406 int type;
2407};
Takashi Iwaia05c0732020-01-05 15:48:20 +01002408static const struct via823x_info via823x_cards[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2410 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2411 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2412 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2413 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2414 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
Bastiaan Jacques8263c652006-04-18 17:04:04 +02002415 { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416};
2417
2418/*
2419 * auto detection of DXS channel supports.
2420 */
Takashi Iwai9d749582006-11-24 15:37:18 +01002421
Takashi Iwai216416722020-07-14 19:26:30 +02002422static const struct snd_pci_quirk dxs_allowlist[] = {
Takashi Iwai9d749582006-11-24 15:37:18 +01002423 SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2424 SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2425 SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2426 SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002427 SND_PCI_QUIRK_VENDOR(0x1019, "ESC K8", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002428 SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2429 SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2430 SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002431 SND_PCI_QUIRK_VENDOR(0x1043, "ASUS A7/A8", VIA_DXS_NO_VRA),
2432 SND_PCI_QUIRK_VENDOR(0x1071, "Diverse Notebook", VIA_DXS_NO_VRA),
Takashi Iwai9d749582006-11-24 15:37:18 +01002433 SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002434 SND_PCI_QUIRK_VENDOR(0x1106, "ASRock", VIA_DXS_SRC),
Takashi Iwai11be2652007-10-11 14:35:00 +02002435 SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
Takashi Iwai96745132007-10-25 11:46:24 +02002436 SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2437 SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002438 SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2439 SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2440 SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2441 SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002442 SND_PCI_QUIRK_VENDOR(0x1462, "MSI Mobo", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002443 SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2444 SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2445 SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2446 SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2447 SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2448 SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2449 SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2450 SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2451 SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2452 SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2453 SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2454 SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2455 SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002456 SND_PCI_QUIRK_VENDOR(0x1695, "EPoX mobo", VIA_DXS_SRC),
2457 SND_PCI_QUIRK_VENDOR(0x16f3, "Jetway K8", VIA_DXS_SRC),
2458 SND_PCI_QUIRK_VENDOR(0x1734, "FSC Laptop", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002459 SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
Takashi Iwaia85165c2009-02-09 17:15:50 +01002460 SND_PCI_QUIRK_VENDOR(0x1849, "ASRock mobo", VIA_DXS_SRC),
Takashi Iwai9d749582006-11-24 15:37:18 +01002461 SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8", VIA_DXS_NO_VRA),
2462 SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2463 { } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464};
2465
Bill Pembertone23e7a12012-12-06 12:35:10 -05002466static int check_dxs_list(struct pci_dev *pci, int revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Takashi Iwai9d749582006-11-24 15:37:18 +01002468 const struct snd_pci_quirk *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Takashi Iwai216416722020-07-14 19:26:30 +02002470 w = snd_pci_quirk_lookup(pci, dxs_allowlist);
Takashi Iwai9d749582006-11-24 15:37:18 +01002471 if (w) {
Takashi Iwai216416722020-07-14 19:26:30 +02002472 dev_dbg(&pci->dev, "DXS allow list for %s found\n",
Takashi Iwai86b27232013-01-25 10:54:07 +01002473 snd_pci_quirk_name(w));
Takashi Iwai9d749582006-11-24 15:37:18 +01002474 return w->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Takashi Iwaia7695772006-04-27 16:56:07 +02002477 /* for newer revision, default to DXS_SRC */
2478 if (revision >= VIA_REV_8235)
2479 return VIA_DXS_SRC;
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /*
2482 * not detected, try 48k rate only to be sure.
2483 */
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002484 dev_info(&pci->dev, "Assuming DXS channels with 48k fixed sample rate.\n");
2485 dev_info(&pci->dev, " Please try dxs_support=5 option\n");
2486 dev_info(&pci->dev, " and report if it works on your machine.\n");
2487 dev_info(&pci->dev, " For more details, read ALSA-Configuration.txt.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return VIA_DXS_48K;
2489};
2490
Bill Pembertone23e7a12012-12-06 12:35:10 -05002491static int snd_via82xx_probe(struct pci_dev *pci,
2492 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Takashi Iwaie437e3d2005-11-17 15:06:15 +01002494 struct snd_card *card;
2495 struct via82xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 int chip_type = 0, card_type;
2497 unsigned int i;
2498 int err;
2499
Takashi Iwai60c57722014-01-29 14:20:19 +01002500 err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002501 if (err < 0)
2502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 card_type = pci_id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 switch (card_type) {
2506 case TYPE_CARD_VIA686:
2507 strcpy(card->driver, "VIA686A");
Auke Kok44c10132007-06-08 15:46:36 -07002508 sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 chip_type = TYPE_VIA686;
2510 break;
2511 case TYPE_CARD_VIA8233:
2512 chip_type = TYPE_VIA8233;
Auke Kok44c10132007-06-08 15:46:36 -07002513 sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
Auke Kok44c10132007-06-08 15:46:36 -07002515 if (pci->revision == via823x_cards[i].revision) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 chip_type = via823x_cards[i].type;
2517 strcpy(card->shortname, via823x_cards[i].name);
2518 break;
2519 }
2520 }
2521 if (chip_type != TYPE_VIA8233A) {
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002522 if (dxs_support == VIA_DXS_AUTO)
Auke Kok44c10132007-06-08 15:46:36 -07002523 dxs_support = check_dxs_list(pci, pci->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /* force to use VIA8233 or 8233A model according to
2525 * dxs_support module option
2526 */
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002527 if (dxs_support == VIA_DXS_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 chip_type = TYPE_VIA8233A;
2529 else
2530 chip_type = TYPE_VIA8233;
2531 }
2532 if (chip_type == TYPE_VIA8233A)
2533 strcpy(card->driver, "VIA8233A");
Auke Kok44c10132007-06-08 15:46:36 -07002534 else if (pci->revision >= VIA_REV_8237)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2536 else
2537 strcpy(card->driver, "VIA8233");
2538 break;
2539 default:
Takashi Iwai59d3acfa2014-02-25 15:21:56 +01002540 dev_err(card->dev, "invalid card type %d\n", card_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 err = -EINVAL;
2542 goto __error;
2543 }
2544
Auke Kok44c10132007-06-08 15:46:36 -07002545 if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002546 ac97_clock, &chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 goto __error;
Takashi Iwai57feb832005-11-17 16:10:35 +01002548 card->private_data = chip;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002549 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 goto __error;
2551
2552 if (chip_type == TYPE_VIA686) {
2553 if ((err = snd_via686_pcm_new(chip)) < 0 ||
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002554 (err = snd_via686_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 goto __error;
2556 } else {
2557 if (chip_type == TYPE_VIA8233A) {
2558 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2559 goto __error;
2560 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2561 } else {
2562 if ((err = snd_via8233_pcm_new(chip)) < 0)
2563 goto __error;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002564 if (dxs_support == VIA_DXS_48K)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 chip->dxs_fixed = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002566 else if (dxs_support == VIA_DXS_NO_VRA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 chip->no_vra = 1;
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002568 else if (dxs_support == VIA_DXS_SRC) {
Sergey Vlasov2d7eb7c2005-04-11 15:04:33 +02002569 chip->no_vra = 1;
2570 chip->dxs_src = 1;
2571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002573 if ((err = snd_via8233_init_misc(chip)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 goto __error;
2575 }
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /* disable interrupts */
2578 for (i = 0; i < chip->num_devs; i++)
2579 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2580
2581 snprintf(card->longname, sizeof(card->longname),
2582 "%s with %s at %#lx, irq %d", card->shortname,
2583 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2584
2585 snd_via82xx_proc_init(chip);
2586
2587 if ((err = snd_card_register(card)) < 0) {
2588 snd_card_free(card);
2589 return err;
2590 }
2591 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return 0;
2593
2594 __error:
2595 snd_card_free(card);
2596 return err;
2597}
2598
Bill Pembertone23e7a12012-12-06 12:35:10 -05002599static void snd_via82xx_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002604static struct pci_driver via82xx_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002605 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 .id_table = snd_via82xx_ids,
2607 .probe = snd_via82xx_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002608 .remove = snd_via82xx_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002609 .driver = {
2610 .pm = SND_VIA82XX_PM_OPS,
2611 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612};
2613
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002614module_pci_driver(via82xx_driver);