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

Last change on this file since 695 was 695, checked in by David Azarewicz, 4 years ago

Merge changes from next branch.

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