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