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

Last change on this file since 777 was 777, checked in by David Azarewicz, 6 months ago

Merge from uniaud32-exp branch

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