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

Last change on this file since 717 was 717, checked in by David Azarewicz, 3 years ago

Merge changes from next branch.

File size: 77.4 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#ifdef CONFIG_PM_SLEEP
355 unsigned char legacy_saved;
356 unsigned char legacy_cfg_saved;
357 unsigned char spdif_ctrl_saved;
358 unsigned char capture_src_saved[2];
359 unsigned int mpu_port_saved;
360#endif
361
362 unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
363 unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
364
365 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
366
367 struct pci_dev *pci;
368 struct snd_card *card;
369
370 unsigned int num_devs;
371 unsigned int playback_devno, multi_devno, capture_devno;
372 struct viadev devs[VIA_MAX_DEVS];
373 struct via_rate_lock rates[2]; /* playback and capture */
374 unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
375 unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
376 unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
377 unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
378
379 struct snd_pcm *pcms[2];
380 struct snd_rawmidi *rmidi;
381 struct snd_kcontrol *dxs_controls[4];
382
383 struct snd_ac97_bus *ac97_bus;
384 struct snd_ac97 *ac97;
385 unsigned int ac97_clock;
386 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
387
388 spinlock_t reg_lock;
389 struct snd_info_entry *proc_entry;
390
391#ifdef SUPPORT_JOYSTICK
392 struct gameport *gameport;
393#endif
394};
395
396static const struct pci_device_id snd_via82xx_ids[] = {
397 /* 0x1106, 0x3058 */
398 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C686_5), TYPE_CARD_VIA686, }, /* 686A */
399 /* 0x1106, 0x3059 */
400 { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_8233_5), TYPE_CARD_VIA8233, }, /* VT8233 */
401 { 0, }
402};
403
404MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
405
406/*
407 */
408
409/*
410 * allocate and initialize the descriptor buffers
411 * periods = number of periods
412 * fragsize = period size in bytes
413 */
414static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
415 struct pci_dev *pci,
416 unsigned int periods, unsigned int fragsize)
417{
418 unsigned int i, idx, ofs, rest;
419 struct via82xx *chip = snd_pcm_substream_chip(substream);
420 __le32 *pgtbl;
421
422 if (dev->table.area == NULL) {
423 /* the start of each lists must be aligned to 8 bytes,
424 * but the kernel pages are much bigger, so we don't care
425 */
426 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
427 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
428 &dev->table) < 0)
429 return -ENOMEM;
430 }
431 if (! dev->idx_table) {
432 dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
433 sizeof(*dev->idx_table),
434 GFP_KERNEL);
435 if (! dev->idx_table)
436 return -ENOMEM;
437 }
438
439 /* fill the entries */
440 idx = 0;
441 ofs = 0;
442 pgtbl = (__le32 *)dev->table.area;
443 for (i = 0; i < periods; i++) {
444 rest = fragsize;
445 /* fill descriptors for a period.
446 * a period can be split to several descriptors if it's
447 * over page boundary.
448 */
449 do {
450 unsigned int r;
451 unsigned int flag;
452 unsigned int addr;
453
454 if (idx >= VIA_TABLE_SIZE) {
455 dev_err(&pci->dev, "too much table size!\n");
456 return -EINVAL;
457 }
458 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
459 pgtbl[idx << 1] = cpu_to_le32(addr);
460 r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
461 rest -= r;
462 if (! rest) {
463 if (i == periods - 1)
464 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
465 else
466 flag = VIA_TBL_BIT_FLAG; /* period boundary */
467 } else
468 flag = 0; /* period continues to the next */
469 /*
470 dev_dbg(&pci->dev,
471 "tbl %d: at %d size %d (rest %d)\n",
472 idx, ofs, r, rest);
473 */
474 pgtbl[(idx<<1) + 1] = cpu_to_le32(r | flag);
475 dev->idx_table[idx].offset = ofs;
476 dev->idx_table[idx].size = r;
477 ofs += r;
478 idx++;
479 } while (rest > 0);
480 }
481 dev->tbl_entries = idx;
482 dev->bufsize = periods * fragsize;
483 dev->bufsize2 = dev->bufsize / 2;
484 dev->fragsize = fragsize;
485 return 0;
486}
487
488
489static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
490 struct pci_dev *pci)
491{
492 if (dev->table.area) {
493 snd_dma_free_pages(&dev->table);
494 dev->table.area = NULL;
495 }
496 kfree(dev->idx_table);
497 dev->idx_table = NULL;
498 return 0;
499}
500
501/*
502 * Basic I/O
503 */
504
505static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
506{
507 return inl(VIAREG(chip, AC97));
508}
509
510static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
511{
512 outl(val, VIAREG(chip, AC97));
513}
514
515static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
516{
517 unsigned int timeout = 1000; /* 1ms */
518 unsigned int val;
519
520 while (timeout-- > 0) {
521 udelay(1);
522 val = snd_via82xx_codec_xread(chip);
523 if (!(val & 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 rate_changed = via_lock_rate(&chip->rates[0], ac97_rate);
1032 if (rate_changed < 0)
1033 return rate_changed;
1034 if (rate_changed)
1035 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1036 chip->no_vra ? 48000 : runtime->rate);
1037 if (chip->spdif_on && viadev->reg_offset == 0x30)
1038 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1039
1040 if (runtime->rate == 48000)
1041 rbits = 0xfffff;
1042 else
1043 rbits = (0x100000 / 48000) * runtime->rate +
1044 ((0x100000 % 48000) * runtime->rate) / 48000;
1045 snd_BUG_ON(rbits & ~0xfffff);
1046 snd_via82xx_channel_reset(chip, viadev);
1047 snd_via82xx_set_table_ptr(chip, viadev);
1048 outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1049 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1050 outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1051 VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
1052 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1053 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1054 rbits | /* rate */
1055 0xff000000, /* STOP index is never reached */
1056 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1057 udelay(20);
1058 snd_via82xx_codec_ready(chip, 0);
1059 return 0;
1060}
1061
1062/*
1063 * prepare callback for multi-channel playback on via823x
1064 */
1065static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
1066{
1067 struct via82xx *chip = snd_pcm_substream_chip(substream);
1068 struct viadev *viadev = substream->runtime->private_data;
1069 struct snd_pcm_runtime *runtime = substream->runtime;
1070 unsigned int slots;
1071 int fmt;
1072
1073 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1074 return -EINVAL;
1075 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1076 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1077 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1078 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1079 snd_via82xx_channel_reset(chip, viadev);
1080 snd_via82xx_set_table_ptr(chip, viadev);
1081
1082 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1083 VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
1084 fmt |= runtime->channels << 4;
1085 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1086#if 0
1087 if (chip->revision == VIA_REV_8233A)
1088 slots = 0;
1089 else
1090#endif
1091 {
1092 /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1093 /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1094 switch (runtime->channels) {
1095 case 1: slots = (1<<0) | (1<<4); break;
1096 case 2: slots = (1<<0) | (2<<4); break;
1097 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1098 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1099 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1100 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1101 default: slots = 0; break;
1102 }
1103 }
1104 /* STOP index is never reached */
1105 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1106 udelay(20);
1107 snd_via82xx_codec_ready(chip, 0);
1108 return 0;
1109}
1110
1111/*
1112 * prepare callback for capture on via823x
1113 */
1114static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
1115{
1116 struct via82xx *chip = snd_pcm_substream_chip(substream);
1117 struct viadev *viadev = substream->runtime->private_data;
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1119
1120 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1121 return -EINVAL;
1122 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1123 snd_via82xx_channel_reset(chip, viadev);
1124 snd_via82xx_set_table_ptr(chip, viadev);
1125 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1126 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1127 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1128 0xff000000, /* STOP index is never reached */
1129 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1130 udelay(20);
1131 snd_via82xx_codec_ready(chip, 0);
1132 return 0;
1133}
1134
1135
1136/*
1137 * pcm hardware definition, identical for both playback and capture
1138 */
1139static const struct snd_pcm_hardware snd_via82xx_hw =
1140{
1141 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1142 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1143 SNDRV_PCM_INFO_MMAP_VALID |
1144 /* SNDRV_PCM_INFO_RESUME | */
1145 SNDRV_PCM_INFO_PAUSE),
1146 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1147 .rates = SNDRV_PCM_RATE_48000,
1148 .rate_min = 48000,
1149 .rate_max = 48000,
1150 .channels_min = 1,
1151 .channels_max = 2,
1152 .buffer_bytes_max = VIA_MAX_BUFSIZE,
1153 .period_bytes_min = 32,
1154 .period_bytes_max = VIA_MAX_BUFSIZE / 2,
1155 .periods_min = 2,
1156 .periods_max = VIA_TABLE_SIZE / 2,
1157 .fifo_size = 0,
1158};
1159
1160
1161/*
1162 * open callback skeleton
1163 */
1164static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1165 struct snd_pcm_substream *substream)
1166{
1167 struct snd_pcm_runtime *runtime = substream->runtime;
1168 int err;
1169 struct via_rate_lock *ratep;
1170 bool use_src = false;
1171
1172 runtime->hw = snd_via82xx_hw;
1173
1174 /* set the hw rate condition */
1175 ratep = &chip->rates[viadev->direction];
1176 spin_lock_irq(&ratep->lock);
1177 ratep->used++;
1178 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1179 /* DXS#3 and spdif is on */
1180 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1181 snd_pcm_limit_hw_rates(runtime);
1182 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1183 /* fixed DXS playback rate */
1184 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1185 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1186 } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1187 /* use full SRC capabilities of DXS */
1188 runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1189 SNDRV_PCM_RATE_8000_48000);
1190 runtime->hw.rate_min = 8000;
1191 runtime->hw.rate_max = 48000;
1192 use_src = true;
1193 } else if (! ratep->rate) {
1194 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1195 runtime->hw.rates = chip->ac97->rates[idx];
1196 snd_pcm_limit_hw_rates(runtime);
1197 } else {
1198 /* a fixed rate */
1199 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1200 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1201 }
1202 spin_unlock_irq(&ratep->lock);
1203
1204 /* we may remove following constaint when we modify table entries
1205 in interrupt */
1206 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1207 if (err < 0)
1208 return err;
1209
1210 if (use_src) {
1211 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1212 if (err < 0)
1213 return err;
1214 }
1215
1216 runtime->private_data = viadev;
1217 viadev->substream = substream;
1218
1219 return 0;
1220}
1221
1222
1223/*
1224 * open callback for playback on via686
1225 */
1226static int snd_via686_playback_open(struct snd_pcm_substream *substream)
1227{
1228 struct via82xx *chip = snd_pcm_substream_chip(substream);
1229 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
1230 int err;
1231
1232 err = snd_via82xx_pcm_open(chip, viadev, substream);
1233 if (err < 0)
1234 return err;
1235 return 0;
1236}
1237
1238/*
1239 * open callback for playback on via823x DXS
1240 */
1241static int snd_via8233_playback_open(struct snd_pcm_substream *substream)
1242{
1243 struct via82xx *chip = snd_pcm_substream_chip(substream);
1244 struct viadev *viadev;
1245 unsigned int stream;
1246 int err;
1247
1248 viadev = &chip->devs[chip->playback_devno + substream->number];
1249 err = snd_via82xx_pcm_open(chip, viadev, substream);
1250 if (err < 0)
1251 return err;
1252 stream = viadev->reg_offset / 0x10;
1253 if (chip->dxs_controls[stream]) {
1254 chip->playback_volume[stream][0] =
1255 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1256 chip->playback_volume[stream][1] =
1257 VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1258 chip->dxs_controls[stream]->vd[0].access &=
1259 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1260 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1261 SNDRV_CTL_EVENT_MASK_INFO,
1262 &chip->dxs_controls[stream]->id);
1263 }
1264 return 0;
1265}
1266
1267/*
1268 * open callback for playback on via823x multi-channel
1269 */
1270static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
1271{
1272 struct via82xx *chip = snd_pcm_substream_chip(substream);
1273 struct viadev *viadev = &chip->devs[chip->multi_devno];
1274 int err;
1275 /* channels constraint for VIA8233A
1276 * 3 and 5 channels are not supported
1277 */
1278 static const unsigned int channels[] = {
1279 1, 2, 4, 6
1280 };
1281 static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
1282 .count = ARRAY_SIZE(channels),
1283 .list = channels,
1284 .mask = 0,
1285 };
1286
1287 err = snd_via82xx_pcm_open(chip, viadev, substream);
1288 if (err < 0)
1289 return err;
1290 substream->runtime->hw.channels_max = 6;
1291 if (chip->revision == VIA_REV_8233A)
1292 snd_pcm_hw_constraint_list(substream->runtime, 0,
1293 SNDRV_PCM_HW_PARAM_CHANNELS,
1294 &hw_constraints_channels);
1295 return 0;
1296}
1297
1298/*
1299 * open callback for capture on via686 and via823x
1300 */
1301static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
1302{
1303 struct via82xx *chip = snd_pcm_substream_chip(substream);
1304 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1305
1306 return snd_via82xx_pcm_open(chip, viadev, substream);
1307}
1308
1309/*
1310 * close callback
1311 */
1312static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
1313{
1314 struct via82xx *chip = snd_pcm_substream_chip(substream);
1315 struct viadev *viadev = substream->runtime->private_data;
1316 struct via_rate_lock *ratep;
1317
1318 /* release the rate lock */
1319 ratep = &chip->rates[viadev->direction];
1320 spin_lock_irq(&ratep->lock);
1321 ratep->used--;
1322 if (! ratep->used)
1323 ratep->rate = 0;
1324 spin_unlock_irq(&ratep->lock);
1325 if (! ratep->rate) {
1326 if (! viadev->direction) {
1327 snd_ac97_update_power(chip->ac97,
1328 AC97_PCM_FRONT_DAC_RATE, 0);
1329 snd_ac97_update_power(chip->ac97,
1330 AC97_PCM_SURR_DAC_RATE, 0);
1331 snd_ac97_update_power(chip->ac97,
1332 AC97_PCM_LFE_DAC_RATE, 0);
1333 } else
1334 snd_ac97_update_power(chip->ac97,
1335 AC97_PCM_LR_ADC_RATE, 0);
1336 }
1337 viadev->substream = NULL;
1338 return 0;
1339}
1340
1341static int snd_via8233_playback_close(struct snd_pcm_substream *substream)
1342{
1343 struct via82xx *chip = snd_pcm_substream_chip(substream);
1344 struct viadev *viadev = substream->runtime->private_data;
1345 unsigned int stream;
1346
1347 stream = viadev->reg_offset / 0x10;
1348 if (chip->dxs_controls[stream]) {
1349 chip->dxs_controls[stream]->vd[0].access |=
1350 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1351 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1352 &chip->dxs_controls[stream]->id);
1353 }
1354 return snd_via82xx_pcm_close(substream);
1355}
1356
1357
1358/* via686 playback callbacks */
1359static const struct snd_pcm_ops snd_via686_playback_ops = {
1360 .open = snd_via686_playback_open,
1361 .close = snd_via82xx_pcm_close,
1362 .hw_params = snd_via82xx_hw_params,
1363 .hw_free = snd_via82xx_hw_free,
1364 .prepare = snd_via686_playback_prepare,
1365 .trigger = snd_via82xx_pcm_trigger,
1366 .pointer = snd_via686_pcm_pointer,
1367};
1368
1369/* via686 capture callbacks */
1370static const struct snd_pcm_ops snd_via686_capture_ops = {
1371 .open = snd_via82xx_capture_open,
1372 .close = snd_via82xx_pcm_close,
1373 .hw_params = snd_via82xx_hw_params,
1374 .hw_free = snd_via82xx_hw_free,
1375 .prepare = snd_via686_capture_prepare,
1376 .trigger = snd_via82xx_pcm_trigger,
1377 .pointer = snd_via686_pcm_pointer,
1378};
1379
1380/* via823x DSX playback callbacks */
1381static const struct snd_pcm_ops snd_via8233_playback_ops = {
1382 .open = snd_via8233_playback_open,
1383 .close = snd_via8233_playback_close,
1384 .hw_params = snd_via82xx_hw_params,
1385 .hw_free = snd_via82xx_hw_free,
1386 .prepare = snd_via8233_playback_prepare,
1387 .trigger = snd_via82xx_pcm_trigger,
1388 .pointer = snd_via8233_pcm_pointer,
1389};
1390
1391/* via823x multi-channel playback callbacks */
1392static const struct snd_pcm_ops snd_via8233_multi_ops = {
1393 .open = snd_via8233_multi_open,
1394 .close = snd_via82xx_pcm_close,
1395 .hw_params = snd_via82xx_hw_params,
1396 .hw_free = snd_via82xx_hw_free,
1397 .prepare = snd_via8233_multi_prepare,
1398 .trigger = snd_via82xx_pcm_trigger,
1399 .pointer = snd_via8233_pcm_pointer,
1400};
1401
1402/* via823x capture callbacks */
1403static const struct snd_pcm_ops snd_via8233_capture_ops = {
1404 .open = snd_via82xx_capture_open,
1405 .close = snd_via82xx_pcm_close,
1406 .hw_params = snd_via82xx_hw_params,
1407 .hw_free = snd_via82xx_hw_free,
1408 .prepare = snd_via8233_capture_prepare,
1409 .trigger = snd_via82xx_pcm_trigger,
1410 .pointer = snd_via8233_pcm_pointer,
1411};
1412
1413
1414static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1415 int shadow_pos, int direction)
1416{
1417 chip->devs[idx].reg_offset = reg_offset;
1418 chip->devs[idx].shadow_shift = shadow_pos * 4;
1419 chip->devs[idx].direction = direction;
1420 chip->devs[idx].port = chip->port + reg_offset;
1421}
1422
1423/*
1424 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1425 */
1426static int snd_via8233_pcm_new(struct via82xx *chip)
1427{
1428 struct snd_pcm *pcm;
1429 struct snd_pcm_chmap *chmap;
1430 int i, err;
1431
1432 chip->playback_devno = 0; /* x 4 */
1433 chip->multi_devno = 4; /* x 1 */
1434 chip->capture_devno = 5; /* x 2 */
1435 chip->num_devs = 7;
1436 chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1437
1438 /* PCM #0: 4 DSX playbacks and 1 capture */
1439 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1440 if (err < 0)
1441 return err;
1442 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1443 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1444 pcm->private_data = chip;
1445 strcpy(pcm->name, chip->card->shortname);
1446 chip->pcms[0] = pcm;
1447 /* set up playbacks */
1448 for (i = 0; i < 4; i++)
1449 init_viadev(chip, i, 0x10 * i, i, 0);
1450 /* capture */
1451 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1452
1453 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1454 &chip->pci->dev,
1455 64*1024, VIA_MAX_BUFSIZE);
1456
1457 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1458 snd_pcm_std_chmaps, 2, 0,
1459 &chmap);
1460 if (err < 0)
1461 return err;
1462
1463 /* PCM #1: multi-channel playback and 2nd capture */
1464 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1465 if (err < 0)
1466 return err;
1467 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1468 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1469 pcm->private_data = chip;
1470 strcpy(pcm->name, chip->card->shortname);
1471 chip->pcms[1] = pcm;
1472 /* set up playback */
1473 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1474 /* set up capture */
1475 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
1476
1477 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1478 &chip->pci->dev,
1479 64*1024, VIA_MAX_BUFSIZE);
1480
1481 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1482 snd_pcm_alt_chmaps, 6, 0,
1483 &chmap);
1484 if (err < 0)
1485 return err;
1486 chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1487
1488 return 0;
1489}
1490
1491/*
1492 * create pcm instances for VIA8233A
1493 */
1494static int snd_via8233a_pcm_new(struct via82xx *chip)
1495{
1496 struct snd_pcm *pcm;
1497 struct snd_pcm_chmap *chmap;
1498 int err;
1499
1500 chip->multi_devno = 0;
1501 chip->playback_devno = 1;
1502 chip->capture_devno = 2;
1503 chip->num_devs = 3;
1504 chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1505
1506 /* PCM #0: multi-channel playback and capture */
1507 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1508 if (err < 0)
1509 return err;
1510 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1511 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1512 pcm->private_data = chip;
1513 strcpy(pcm->name, chip->card->shortname);
1514 chip->pcms[0] = pcm;
1515 /* set up playback */
1516 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1517 /* capture */
1518 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1519
1520 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1521 &chip->pci->dev,
1522 64*1024, VIA_MAX_BUFSIZE);
1523
1524 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1525 snd_pcm_alt_chmaps, 6, 0,
1526 &chmap);
1527 if (err < 0)
1528 return err;
1529 chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1530
1531 /* SPDIF supported? */
1532 if (! ac97_can_spdif(chip->ac97))
1533 return 0;
1534
1535 /* PCM #1: DXS3 playback (for spdif) */
1536 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1537 if (err < 0)
1538 return err;
1539 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1540 pcm->private_data = chip;
1541 strcpy(pcm->name, chip->card->shortname);
1542 chip->pcms[1] = pcm;
1543 /* set up playback */
1544 init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
1545
1546 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1547 &chip->pci->dev,
1548 64*1024, VIA_MAX_BUFSIZE);
1549 return 0;
1550}
1551
1552/*
1553 * create a pcm instance for via686a/b
1554 */
1555static int snd_via686_pcm_new(struct via82xx *chip)
1556{
1557 struct snd_pcm *pcm;
1558 int err;
1559
1560 chip->playback_devno = 0;
1561 chip->capture_devno = 1;
1562 chip->num_devs = 2;
1563 chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1564
1565 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1566 if (err < 0)
1567 return err;
1568 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1569 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1570 pcm->private_data = chip;
1571 strcpy(pcm->name, chip->card->shortname);
1572 chip->pcms[0] = pcm;
1573 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1574 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
1575
1576 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1577 &chip->pci->dev,
1578 64*1024, VIA_MAX_BUFSIZE);
1579 return 0;
1580}
1581
1582
1583/*
1584 * Mixer part
1585 */
1586
1587static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1588 struct snd_ctl_elem_info *uinfo)
1589{
1590 /* formerly they were "Line" and "Mic", but it looks like that they
1591 * have nothing to do with the actual physical connections...
1592 */
1593 static const char * const texts[2] = {
1594 "Input1", "Input2"
1595 };
1596 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1597}
1598
1599static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1600 struct snd_ctl_elem_value *ucontrol)
1601{
1602 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1603 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1604 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1605 return 0;
1606}
1607
1608static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1609 struct snd_ctl_elem_value *ucontrol)
1610{
1611 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1612 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1613 u8 val, oval;
1614
1615 spin_lock_irq(&chip->reg_lock);
1616 oval = inb(port);
1617 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1618 if (ucontrol->value.enumerated.item[0])
1619 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1620 if (val != oval)
1621 outb(val, port);
1622 spin_unlock_irq(&chip->reg_lock);
1623 return val != oval;
1624}
1625
1626static struct snd_kcontrol_new snd_via8233_capture_source = {
1627 .name = "Input Source Select",
1628 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1629 .info = snd_via8233_capture_source_info,
1630 .get = snd_via8233_capture_source_get,
1631 .put = snd_via8233_capture_source_put,
1632};
1633
1634#define snd_via8233_dxs3_spdif_info snd_ctl_boolean_mono_info
1635
1636static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1637 struct snd_ctl_elem_value *ucontrol)
1638{
1639 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1640 u8 val;
1641
1642 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1643 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1644 return 0;
1645}
1646
1647static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1648 struct snd_ctl_elem_value *ucontrol)
1649{
1650 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1651 u8 val, oval;
1652
1653 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1654 val = oval & ~VIA8233_SPDIF_DX3;
1655 if (ucontrol->value.integer.value[0])
1656 val |= VIA8233_SPDIF_DX3;
1657 /* save the spdif flag for rate filtering */
1658 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1659 if (val != oval) {
1660 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1661 return 1;
1662 }
1663 return 0;
1664}
1665
1666static const struct snd_kcontrol_new snd_via8233_dxs3_spdif_control = {
1667 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
1668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1669 .info = snd_via8233_dxs3_spdif_info,
1670 .get = snd_via8233_dxs3_spdif_get,
1671 .put = snd_via8233_dxs3_spdif_put,
1672};
1673
1674static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_info *uinfo)
1676{
1677 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1678 uinfo->count = 2;
1679 uinfo->value.integer.min = 0;
1680 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1681 return 0;
1682}
1683
1684static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1685 struct snd_ctl_elem_value *ucontrol)
1686{
1687 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1688 unsigned int idx = kcontrol->id.subdevice;
1689
1690 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1691 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1692 return 0;
1693}
1694
1695static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1696 struct snd_ctl_elem_value *ucontrol)
1697{
1698 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1699 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1700 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
1701 return 0;
1702}
1703
1704static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1705 struct snd_ctl_elem_value *ucontrol)
1706{
1707 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1708 unsigned int idx = kcontrol->id.subdevice;
1709 unsigned long port = chip->port + 0x10 * idx;
1710 unsigned char val;
1711 int i, change = 0;
1712
1713 for (i = 0; i < 2; i++) {
1714 val = ucontrol->value.integer.value[i];
1715 if (val > VIA_DXS_MAX_VOLUME)
1716 val = VIA_DXS_MAX_VOLUME;
1717 val = VIA_DXS_MAX_VOLUME - val;
1718 change |= val != chip->playback_volume[idx][i];
1719 if (change) {
1720 chip->playback_volume[idx][i] = val;
1721 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1722 }
1723 }
1724 return change;
1725}
1726
1727static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1728 struct snd_ctl_elem_value *ucontrol)
1729{
1730 struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1731 unsigned int idx;
1732 unsigned char val;
1733 int i, change = 0;
1734
1735 for (i = 0; i < 2; i++) {
1736 val = ucontrol->value.integer.value[i];
1737 if (val > VIA_DXS_MAX_VOLUME)
1738 val = VIA_DXS_MAX_VOLUME;
1739 val = VIA_DXS_MAX_VOLUME - val;
1740 if (val != chip->playback_volume_c[i]) {
1741 change = 1;
1742 chip->playback_volume_c[i] = val;
1743 for (idx = 0; idx < 4; idx++) {
1744 unsigned long port = chip->port + 0x10 * idx;
1745 chip->playback_volume[idx][i] = val;
1746 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1747 }
1748 }
1749 }
1750 return change;
1751}
1752
1753static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -4650, 150, 1);
1754
1755static const struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control = {
1756 .name = "PCM Playback Volume",
1757 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1758 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1759 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1760 .info = snd_via8233_dxs_volume_info,
1761 .get = snd_via8233_pcmdxs_volume_get,
1762 .put = snd_via8233_pcmdxs_volume_put,
1763 .tlv = { .p = db_scale_dxs }
1764};
1765
1766static const struct snd_kcontrol_new snd_via8233_dxs_volume_control = {
1767 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1768 .device = 0,
1769 /* .subdevice set later */
1770 .name = "PCM Playback Volume",
1771 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1772 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1773 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1774 .info = snd_via8233_dxs_volume_info,
1775 .get = snd_via8233_dxs_volume_get,
1776 .put = snd_via8233_dxs_volume_put,
1777 .tlv = { .p = db_scale_dxs }
1778};
1779
1780/*
1781 */
1782
1783static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1784{
1785 struct via82xx *chip = bus->private_data;
1786 chip->ac97_bus = NULL;
1787}
1788
1789static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
1790{
1791 struct via82xx *chip = ac97->private_data;
1792 chip->ac97 = NULL;
1793}
1794
1795static const struct ac97_quirk ac97_quirks[] = {
1796 {
1797 .subvendor = 0x1106,
1798 .subdevice = 0x4161,
1799 .codec_id = 0x56494161, /* VT1612A */
1800 .name = "Soltek SL-75DRV5",
1801 .type = AC97_TUNE_NONE
1802 },
1803 { /* FIXME: which codec? */
1804 .subvendor = 0x1106,
1805 .subdevice = 0x4161,
1806 .name = "ASRock K7VT2",
1807 .type = AC97_TUNE_HP_ONLY
1808 },
1809 {
1810 .subvendor = 0x110a,
1811 .subdevice = 0x0079,
1812 .name = "Fujitsu Siemens D1289",
1813 .type = AC97_TUNE_HP_ONLY
1814 },
1815 {
1816 .subvendor = 0x1019,
1817 .subdevice = 0x0a81,
1818 .name = "ECS K7VTA3",
1819 .type = AC97_TUNE_HP_ONLY
1820 },
1821 {
1822 .subvendor = 0x1019,
1823 .subdevice = 0x0a85,
1824 .name = "ECS L7VMM2",
1825 .type = AC97_TUNE_HP_ONLY
1826 },
1827 {
1828 .subvendor = 0x1019,
1829 .subdevice = 0x1841,
1830 .name = "ECS K7VTA3",
1831 .type = AC97_TUNE_HP_ONLY
1832 },
1833 {
1834 .subvendor = 0x1849,
1835 .subdevice = 0x3059,
1836 .name = "ASRock K7VM2",
1837 .type = AC97_TUNE_HP_ONLY /* VT1616 */
1838 },
1839 {
1840 .subvendor = 0x14cd,
1841 .subdevice = 0x7002,
1842 .name = "Unknown",
1843 .type = AC97_TUNE_ALC_JACK
1844 },
1845 {
1846 .subvendor = 0x1071,
1847 .subdevice = 0x8590,
1848 .name = "Mitac Mobo",
1849 .type = AC97_TUNE_ALC_JACK
1850 },
1851 {
1852 .subvendor = 0x161f,
1853 .subdevice = 0x202b,
1854 .name = "Arima Notebook",
1855 .type = AC97_TUNE_HP_ONLY,
1856 },
1857 {
1858 .subvendor = 0x161f,
1859 .subdevice = 0x2032,
1860 .name = "Targa Traveller 811",
1861 .type = AC97_TUNE_HP_ONLY,
1862 },
1863 {
1864 .subvendor = 0x161f,
1865 .subdevice = 0x2032,
1866 .name = "m680x",
1867 .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1868 },
1869 {
1870 .subvendor = 0x1297,
1871 .subdevice = 0xa232,
1872 .name = "Shuttle AK32VN",
1873 .type = AC97_TUNE_HP_ONLY
1874 },
1875 {0} /* terminator */
1876};
1877
1878static int snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
1879{
1880 struct snd_ac97_template ac97;
1881 int err;
1882 static const struct snd_ac97_bus_ops ops = {
1883 .write = snd_via82xx_codec_write,
1884 .read = snd_via82xx_codec_read,
1885 .wait = snd_via82xx_codec_wait,
1886 };
1887
1888 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1889 if (err < 0)
1890 return err;
1891 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1892 chip->ac97_bus->clock = chip->ac97_clock;
1893
1894 memset(&ac97, 0, sizeof(ac97));
1895 ac97.private_data = chip;
1896 ac97.private_free = snd_via82xx_mixer_free_ac97;
1897 ac97.pci = chip->pci;
1898 ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
1899 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1900 if (err < 0)
1901 return err;
1902
1903 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1904
1905 if (chip->chip_type != TYPE_VIA686) {
1906 /* use slot 10/11 */
1907 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1908 }
1909
1910 return 0;
1911}
1912
1913#ifdef SUPPORT_JOYSTICK
1914#define JOYSTICK_ADDR 0x200
1915static int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1916{
1917 struct gameport *gp;
1918
1919 if (!joystick)
1920 return -ENODEV;
1921
1922 if (!devm_request_region(chip->card->dev, JOYSTICK_ADDR, 8,
1923 "VIA686 gameport")) {
1924 dev_warn(chip->card->dev, "cannot reserve joystick port %#x\n",
1925 JOYSTICK_ADDR);
1926 return -EBUSY;
1927 }
1928
1929 chip->gameport = gp = gameport_allocate_port();
1930 if (!gp) {
1931 dev_err(chip->card->dev,
1932 "cannot allocate memory for gameport\n");
1933 return -ENOMEM;
1934 }
1935
1936 gameport_set_name(gp, "VIA686 Gameport");
1937 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1938 gameport_set_dev_parent(gp, &chip->pci->dev);
1939 gp->io = JOYSTICK_ADDR;
1940
1941 /* Enable legacy joystick port */
1942 *legacy |= VIA_FUNC_ENABLE_GAME;
1943 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1944
1945 gameport_register_port(chip->gameport);
1946
1947 return 0;
1948}
1949
1950static void snd_via686_free_gameport(struct via82xx *chip)
1951{
1952 if (chip->gameport) {
1953 gameport_unregister_port(chip->gameport);
1954 chip->gameport = NULL;
1955 }
1956}
1957#else
1958static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1959{
1960 return -ENOSYS;
1961}
1962static inline void snd_via686_free_gameport(struct via82xx *chip) { }
1963#endif
1964
1965
1966/*
1967 *
1968 */
1969
1970static int snd_via8233_init_misc(struct via82xx *chip)
1971{
1972 int i, err, caps;
1973 unsigned char val;
1974
1975 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1976 for (i = 0; i < caps; i++) {
1977 snd_via8233_capture_source.index = i;
1978 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1979 if (err < 0)
1980 return err;
1981 }
1982 if (ac97_can_spdif(chip->ac97)) {
1983 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1984 if (err < 0)
1985 return err;
1986 }
1987 if (chip->chip_type != TYPE_VIA8233A) {
1988 /* when no h/w PCM volume control is found, use DXS volume control
1989 * as the PCM vol control
1990 */
1991 struct snd_ctl_elem_id sid;
1992 memset(&sid, 0, sizeof(sid));
1993 strcpy(sid.name, "PCM Playback Volume");
1994 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1995 if (! snd_ctl_find_id(chip->card, &sid)) {
1996 dev_info(chip->card->dev,
1997 "Using DXS as PCM Playback\n");
1998 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1999 if (err < 0)
2000 return err;
2001 }
2002 else /* Using DXS when PCM emulation is enabled is really weird */
2003 {
2004 for (i = 0; i < 4; ++i) {
2005 struct snd_kcontrol *kctl;
2006
2007 kctl = snd_ctl_new1(
2008 &snd_via8233_dxs_volume_control, chip);
2009 if (!kctl)
2010 return -ENOMEM;
2011 kctl->id.subdevice = i;
2012 err = snd_ctl_add(chip->card, kctl);
2013 if (err < 0)
2014 return err;
2015 chip->dxs_controls[i] = kctl;
2016 }
2017 }
2018 }
2019 /* select spdif data slot 10/11 */
2020 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
2021 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
2022 val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
2023 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
2024
2025 return 0;
2026}
2027
2028static int snd_via686_init_misc(struct via82xx *chip)
2029{
2030 unsigned char legacy, legacy_cfg;
2031 int rev_h = 0;
2032
2033 legacy = chip->old_legacy;
2034 legacy_cfg = chip->old_legacy_cfg;
2035 legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
2036 legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
2037 if (chip->revision >= VIA_REV_686_H) {
2038 rev_h = 1;
2039 if (mpu_port >= 0x200) { /* force MIDI */
2040 mpu_port &= 0xfffc;
2041 pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
2042#ifdef CONFIG_PM_SLEEP
2043 chip->mpu_port_saved = mpu_port;
2044#endif
2045 } else {
2046 mpu_port = pci_resource_start(chip->pci, 2);
2047 }
2048 } else {
2049 switch (mpu_port) { /* force MIDI */
2050 case 0x300:
2051 case 0x310:
2052 case 0x320:
2053 case 0x330:
2054 legacy_cfg &= ~(3 << 2);
2055 legacy_cfg |= (mpu_port & 0x0030) >> 2;
2056 break;
2057 default: /* no, use BIOS settings */
2058 if (legacy & VIA_FUNC_ENABLE_MIDI)
2059 mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
2060 break;
2061 }
2062 }
2063 if (mpu_port >= 0x200)
2064 chip->mpu_res = devm_request_region(&chip->pci->dev, mpu_port,
2065 2, "VIA82xx MPU401");
2066 if (chip->mpu_res) {
2067 if (rev_h)
2068 legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
2069 legacy |= VIA_FUNC_ENABLE_MIDI;
2070 } else {
2071 if (rev_h)
2072 legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
2073 legacy &= ~VIA_FUNC_ENABLE_MIDI;
2074 mpu_port = 0;
2075 }
2076
2077 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2078 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
2079 if (chip->mpu_res) {
2080 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
2081 mpu_port, MPU401_INFO_INTEGRATED |
2082 MPU401_INFO_IRQ_HOOK, -1,
2083 &chip->rmidi) < 0) {
2084 dev_warn(chip->card->dev,
2085 "unable to initialize MPU-401 at 0x%lx, skipping\n",
2086 mpu_port);
2087 legacy &= ~VIA_FUNC_ENABLE_MIDI;
2088 } else {
2089 legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
2090 }
2091 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2092 }
2093
2094 snd_via686_create_gameport(chip, &legacy);
2095
2096#ifdef CONFIG_PM_SLEEP
2097 chip->legacy_saved = legacy;
2098 chip->legacy_cfg_saved = legacy_cfg;
2099#endif
2100
2101 return 0;
2102}
2103
2104
2105/*
2106 * proc interface
2107 */
2108static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2109 struct snd_info_buffer *buffer)
2110{
2111 struct via82xx *chip = entry->private_data;
2112 int i;
2113
2114 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2115 for (i = 0; i < 0xa0; i += 4) {
2116 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2117 }
2118}
2119
2120static void snd_via82xx_proc_init(struct via82xx *chip)
2121{
2122 snd_card_ro_proc_new(chip->card, "via82xx", chip,
2123 snd_via82xx_proc_read);
2124}
2125
2126/*
2127 *
2128 */
2129
2130static int snd_via82xx_chip_init(struct via82xx *chip)
2131{
2132 unsigned int val;
2133 unsigned long end_time;
2134 unsigned char pval;
2135
2136#if 0 /* broken on K7M? */
2137 if (chip->chip_type == TYPE_VIA686)
2138 /* disable all legacy ports */
2139 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2140#endif
2141 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2142 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2143 /* deassert ACLink reset, force SYNC */
2144 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2145 VIA_ACLINK_CTRL_ENABLE |
2146 VIA_ACLINK_CTRL_RESET |
2147 VIA_ACLINK_CTRL_SYNC);
2148 udelay(100);
2149#if 1 /* FIXME: should we do full reset here for all chip models? */
2150 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2151 udelay(100);
2152#else
2153 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2154 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2155 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2156 udelay(2);
2157#endif
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 /* Make sure VRA is enabled, in case we didn't do a
2165 * complete codec reset, above */
2166 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2167 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2168 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2169 /* note - FM data out has trouble with non VRA codecs !! */
2170 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2171 udelay(100);
2172 }
2173
2174 /* wait until codec ready */
2175 end_time = jiffies + msecs_to_jiffies(750);
2176 do {
2177 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2178 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2179 break;
2180 schedule_timeout_uninterruptible(1);
2181 } while (time_before(jiffies, end_time));
2182
2183 val = snd_via82xx_codec_xread(chip);
2184 if (val & VIA_REG_AC97_BUSY)
2185 dev_err(chip->card->dev,
2186 "AC'97 codec is not ready [0x%x]\n", val);
2187
2188#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2189 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2190 VIA_REG_AC97_SECONDARY_VALID |
2191 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2192 end_time = jiffies + msecs_to_jiffies(750);
2193 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2194 VIA_REG_AC97_SECONDARY_VALID |
2195 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2196 do {
2197 val = snd_via82xx_codec_xread(chip);
2198 if (val & VIA_REG_AC97_SECONDARY_VALID) {
2199 chip->ac97_secondary = 1;
2200 goto __ac97_ok2;
2201 }
2202 schedule_timeout_uninterruptible(1);
2203 } while (time_before(jiffies, end_time));
2204 /* This is ok, the most of motherboards have only one codec */
2205
2206 __ac97_ok2:
2207#endif
2208
2209 if (chip->chip_type == TYPE_VIA686) {
2210 /* route FM trap to IRQ, disable FM trap */
2211 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2212 /* disable all GPI interrupts */
2213 outl(0, VIAREG(chip, GPI_INTR));
2214 }
2215
2216 if (chip->chip_type != TYPE_VIA686) {
2217 /* Workaround for Award BIOS bug:
2218 * DXS channels don't work properly with VRA if MC97 is disabled.
2219 */
2220 struct pci_dev *pci;
2221 pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
2222 if (pci) {
2223 unsigned char data;
2224 pci_read_config_byte(pci, 0x44, &data);
2225 pci_write_config_byte(pci, 0x44, data | 0x40);
2226 pci_dev_put(pci);
2227 }
2228 }
2229
2230 if (chip->chip_type != TYPE_VIA8233A) {
2231 int i, idx;
2232 for (idx = 0; idx < 4; idx++) {
2233 unsigned long port = chip->port + 0x10 * idx;
2234 for (i = 0; i < 2; i++) {
2235 chip->playback_volume[idx][i]=chip->playback_volume_c[i];
2236 outb(chip->playback_volume_c[i],
2237 port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
2238 }
2239 }
2240 }
2241
2242 return 0;
2243}
2244
2245#ifdef CONFIG_PM_SLEEP
2246/*
2247 * power management
2248 */
2249static int snd_via82xx_suspend(struct device *dev)
2250{
2251 struct snd_card *card = dev_get_drvdata(dev);
2252 struct via82xx *chip = card->private_data;
2253 int i;
2254
2255 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2256 for (i = 0; i < chip->num_devs; i++)
2257 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2258 snd_ac97_suspend(chip->ac97);
2259
2260 /* save misc values */
2261 if (chip->chip_type != TYPE_VIA686) {
2262 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2263 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2264 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2265 }
2266
2267 return 0;
2268}
2269
2270static int snd_via82xx_resume(struct device *dev)
2271{
2272 struct snd_card *card = dev_get_drvdata(dev);
2273 struct via82xx *chip = card->private_data;
2274 int i;
2275
2276 snd_via82xx_chip_init(chip);
2277
2278 if (chip->chip_type == TYPE_VIA686) {
2279 if (chip->mpu_port_saved)
2280 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2281 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2282 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2283 } else {
2284 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2285 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2286 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2287 }
2288
2289 snd_ac97_resume(chip->ac97);
2290
2291 for (i = 0; i < chip->num_devs; i++)
2292 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2293
2294 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2295 return 0;
2296}
2297
2298static SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
2299#define SND_VIA82XX_PM_OPS &snd_via82xx_pm
2300#else
2301#define SND_VIA82XX_PM_OPS NULL
2302#endif /* CONFIG_PM_SLEEP */
2303
2304static void snd_via82xx_free(struct snd_card *card)
2305{
2306 struct via82xx *chip = card->private_data;
2307 unsigned int i;
2308
2309 /* disable interrupts */
2310 for (i = 0; i < chip->num_devs; i++)
2311 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2312
2313 if (chip->chip_type == TYPE_VIA686) {
2314 snd_via686_free_gameport(chip);
2315 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2316 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2317 }
2318}
2319
2320static int snd_via82xx_create(struct snd_card *card,
2321 struct pci_dev *pci,
2322 int chip_type,
2323 int revision,
2324 unsigned int ac97_clock)
2325{
2326 struct via82xx *chip = card->private_data;
2327 int err;
2328
2329 err = pcim_enable_device(pci);
2330 if (err < 0)
2331 return err;
2332
2333 chip->chip_type = chip_type;
2334 chip->revision = revision;
2335
2336 spin_lock_init(&chip->reg_lock);
2337 spin_lock_init(&chip->rates[0].lock);
2338 spin_lock_init(&chip->rates[1].lock);
2339 chip->card = card;
2340 chip->pci = pci;
2341 chip->irq = -1;
2342
2343 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2344 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2345 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2346 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2347
2348 err = pci_request_regions(pci, card->driver);
2349 if (err < 0)
2350 return err;
2351 chip->port = pci_resource_start(pci, 0);
2352 if (devm_request_irq(&pci->dev, pci->irq,
2353 chip_type == TYPE_VIA8233 ?
2354 snd_via8233_interrupt : snd_via686_interrupt,
2355 IRQF_SHARED,
2356 KBUILD_MODNAME, chip)) {
2357 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2358 return -EBUSY;
2359 }
2360 chip->irq = pci->irq;
2361 card->sync_irq = chip->irq;
2362 card->private_free = snd_via82xx_free;
2363 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2364 chip->ac97_clock = ac97_clock;
2365
2366 err = snd_via82xx_chip_init(chip);
2367 if (err < 0)
2368 return err;
2369
2370 /* The 8233 ac97 controller does not implement the master bit
2371 * in the pci command register. IMHO this is a violation of the PCI spec.
2372 * We call pci_set_master here because it does not hurt. */
2373 pci_set_master(pci);
2374 return 0;
2375}
2376
2377struct via823x_info {
2378 int revision;
2379 char *name;
2380 int type;
2381};
2382static const struct via823x_info via823x_cards[] = {
2383 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2384 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2385 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2386 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2387 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2388 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
2389 { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
2390};
2391
2392/*
2393 * auto detection of DXS channel supports.
2394 */
2395
2396static const struct snd_pci_quirk dxs_allowlist[] = {
2397 SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2398 SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2399 SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2400 SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
2401 SND_PCI_QUIRK_VENDOR(0x1019, "ESC K8", VIA_DXS_SRC),
2402 SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2403 SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2404 SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
2405 SND_PCI_QUIRK_VENDOR(0x1043, "ASUS A7/A8", VIA_DXS_NO_VRA),
2406 SND_PCI_QUIRK_VENDOR(0x1071, "Diverse Notebook", VIA_DXS_NO_VRA),
2407 SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
2408 SND_PCI_QUIRK_VENDOR(0x1106, "ASRock", VIA_DXS_SRC),
2409 SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
2410 SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2411 SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
2412 SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2413 SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2414 SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2415 SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
2416 SND_PCI_QUIRK_VENDOR(0x1462, "MSI Mobo", VIA_DXS_SRC),
2417 SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2418 SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2419 SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2420 SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2421 SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2422 SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2423 SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2424 SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2425 SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2426 SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2427 SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2428 SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2429 SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
2430 SND_PCI_QUIRK_VENDOR(0x1695, "EPoX mobo", VIA_DXS_SRC),
2431 SND_PCI_QUIRK_VENDOR(0x16f3, "Jetway K8", VIA_DXS_SRC),
2432 SND_PCI_QUIRK_VENDOR(0x1734, "FSC Laptop", VIA_DXS_SRC),
2433 SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
2434 SND_PCI_QUIRK_VENDOR(0x1849, "ASRock mobo", VIA_DXS_SRC),
2435 SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8", VIA_DXS_NO_VRA),
2436 SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2437 {0} /* terminator */
2438};
2439
2440static int check_dxs_list(struct pci_dev *pci, int revision)
2441{
2442 const struct snd_pci_quirk *w;
2443
2444 w = snd_pci_quirk_lookup(pci, dxs_allowlist);
2445 if (w) {
2446 dev_dbg(&pci->dev, "DXS allow list for %s found\n",
2447 snd_pci_quirk_name(w));
2448 return w->value;
2449 }
2450
2451 /* for newer revision, default to DXS_SRC */
2452 if (revision >= VIA_REV_8235)
2453 return VIA_DXS_SRC;
2454
2455 /*
2456 * not detected, try 48k rate only to be sure.
2457 */
2458 dev_info(&pci->dev, "Assuming DXS channels with 48k fixed sample rate.\n");
2459 dev_info(&pci->dev, " Please try dxs_support=5 option\n");
2460 dev_info(&pci->dev, " and report if it works on your machine.\n");
2461 dev_info(&pci->dev, " For more details, read ALSA-Configuration.txt.\n");
2462 return VIA_DXS_48K;
2463};
2464
2465static int __snd_via82xx_probe(struct pci_dev *pci,
2466 const struct pci_device_id *pci_id)
2467{
2468 struct snd_card *card;
2469 struct via82xx *chip;
2470 int chip_type = 0, card_type;
2471 unsigned int i;
2472 int err;
2473
2474 err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
2475 sizeof(*chip), &card);
2476 if (err < 0)
2477 return err;
2478 chip = card->private_data;
2479
2480 card_type = pci_id->driver_data;
2481 switch (card_type) {
2482 case TYPE_CARD_VIA686:
2483 strcpy(card->driver, "VIA686A");
2484 sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
2485 chip_type = TYPE_VIA686;
2486 break;
2487 case TYPE_CARD_VIA8233:
2488 chip_type = TYPE_VIA8233;
2489 sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
2490 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2491 if (pci->revision == via823x_cards[i].revision) {
2492 chip_type = via823x_cards[i].type;
2493 strcpy(card->shortname, via823x_cards[i].name);
2494 break;
2495 }
2496 }
2497 if (chip_type != TYPE_VIA8233A) {
2498 if (dxs_support == VIA_DXS_AUTO)
2499 dxs_support = check_dxs_list(pci, pci->revision);
2500 /* force to use VIA8233 or 8233A model according to
2501 * dxs_support module option
2502 */
2503 if (dxs_support == VIA_DXS_DISABLE)
2504 chip_type = TYPE_VIA8233A;
2505 else
2506 chip_type = TYPE_VIA8233;
2507 }
2508 if (chip_type == TYPE_VIA8233A)
2509 strcpy(card->driver, "VIA8233A");
2510 else if (pci->revision >= VIA_REV_8237)
2511 strcpy(card->driver, "VIA8237"); /* no slog assignment */
2512 else
2513 strcpy(card->driver, "VIA8233");
2514 break;
2515 default:
2516 dev_err(card->dev, "invalid card type %d\n", card_type);
2517 return -EINVAL;
2518 }
2519
2520 err = snd_via82xx_create(card, pci, chip_type, pci->revision,
2521 ac97_clock);
2522 if (err < 0)
2523 return err;
2524 err = snd_via82xx_mixer_new(chip, ac97_quirk);
2525 if (err < 0)
2526 return err;
2527
2528 if (chip_type == TYPE_VIA686) {
2529 err = snd_via686_pcm_new(chip);
2530 if (err < 0)
2531 return err;
2532 err = snd_via686_init_misc(chip);
2533 if (err < 0)
2534 return err;
2535 } else {
2536 if (chip_type == TYPE_VIA8233A) {
2537 err = snd_via8233a_pcm_new(chip);
2538 if (err < 0)
2539 return err;
2540 // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2541 } else {
2542 err = snd_via8233_pcm_new(chip);
2543 if (err < 0)
2544 return err;
2545 if (dxs_support == VIA_DXS_48K)
2546 chip->dxs_fixed = 1;
2547 else if (dxs_support == VIA_DXS_NO_VRA)
2548 chip->no_vra = 1;
2549 else if (dxs_support == VIA_DXS_SRC) {
2550 chip->no_vra = 1;
2551 chip->dxs_src = 1;
2552 }
2553 }
2554 err = snd_via8233_init_misc(chip);
2555 if (err < 0)
2556 return err;
2557 }
2558
2559 /* disable interrupts */
2560 for (i = 0; i < chip->num_devs; i++)
2561 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2562
2563 snprintf(card->longname, sizeof(card->longname),
2564 "%s with %s at %#lx, irq %d", card->shortname,
2565 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2566
2567 snd_via82xx_proc_init(chip);
2568
2569 err = snd_card_register(card);
2570 if (err < 0)
2571 return err;
2572 pci_set_drvdata(pci, card);
2573 return 0;
2574}
2575
2576static int snd_via82xx_probe(struct pci_dev *pci,
2577 const struct pci_device_id *pci_id)
2578{
2579 return snd_card_free_on_error(&pci->dev, __snd_via82xx_probe(pci, pci_id));
2580}
2581
2582static struct pci_driver via82xx_driver = {
2583 .name = KBUILD_MODNAME,
2584 .id_table = snd_via82xx_ids,
2585 .probe = snd_via82xx_probe,
2586 .driver = {
2587 .pm = SND_VIA82XX_PM_OPS,
2588 },
2589};
2590
2591module_pci_driver(via82xx_driver);
Note: See TracBrowser for help on using the repository browser.