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

Last change on this file since 679 was 679, checked in by David Azarewicz, 5 years ago

Merge changes from Paul's uniaud32next branch.

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