1 /* 2 * C-Media CMI8788 driver for Asus Xonar cards 3 * 4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 5 * 6 * 7 * This driver is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License, version 2. 9 * 10 * This driver is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this driver; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 /* 21 * Xonar D2/D2X 22 * ------------ 23 * 24 * CMI8788: 25 * 26 * SPI 0 -> 1st PCM1796 (front) 27 * SPI 1 -> 2nd PCM1796 (surround) 28 * SPI 2 -> 3rd PCM1796 (center/LFE) 29 * SPI 4 -> 4th PCM1796 (back) 30 * 31 * GPIO 2 -> M0 of CS5381 32 * GPIO 3 -> M1 of CS5381 33 * GPIO 5 <- external power present (D2X only) 34 * GPIO 7 -> ALT 35 * GPIO 8 -> enable output to speakers 36 */ 37 38 /* 39 * Xonar D1/DX 40 * ----------- 41 * 42 * CMI8788: 43 * 44 * I²C <-> CS4398 (front) 45 * <-> CS4362A (surround, center/LFE, back) 46 * 47 * GPI 0 <- external power present (DX only) 48 * 49 * GPIO 0 -> enable output to speakers 50 * GPIO 1 -> enable front panel I/O 51 * GPIO 2 -> M0 of CS5361 52 * GPIO 3 -> M1 of CS5361 53 * GPIO 8 -> route input jack to line-in (0) or mic-in (1) 54 * 55 * CS4398: 56 * 57 * AD0 <- 1 58 * AD1 <- 1 59 * 60 * CS4362A: 61 * 62 * AD0 <- 0 63 */ 64 65 /* 66 * Xonar HDAV1.3 (Deluxe) 67 * ---------------------- 68 * 69 * CMI8788: 70 * 71 * I²C <-> PCM1796 (front) 72 * 73 * GPI 0 <- external power present 74 * 75 * GPIO 0 -> enable output to speakers 76 * GPIO 2 -> M0 of CS5381 77 * GPIO 3 -> M1 of CS5381 78 * GPIO 8 -> route input jack to line-in (0) or mic-in (1) 79 * 80 * TXD -> HDMI controller 81 * RXD <- HDMI controller 82 * 83 * PCM1796 front: AD1,0 <- 0,0 84 * 85 * no daughterboard 86 * ---------------- 87 * 88 * GPIO 4 <- 1 89 * 90 * H6 daughterboard 91 * ---------------- 92 * 93 * GPIO 4 <- 0 94 * GPIO 5 <- 0 95 * 96 * I²C <-> PCM1796 (surround) 97 * <-> PCM1796 (center/LFE) 98 * <-> PCM1796 (back) 99 * 100 * PCM1796 surround: AD1,0 <- 0,1 101 * PCM1796 center/LFE: AD1,0 <- 1,0 102 * PCM1796 back: AD1,0 <- 1,1 103 * 104 * unknown daughterboard 105 * --------------------- 106 * 107 * GPIO 4 <- 0 108 * GPIO 5 <- 1 109 * 110 * I²C <-> CS4362A (surround, center/LFE, back) 111 * 112 * CS4362A: AD0 <- 0 113 */ 114 115 /* 116 * Xonar Essence STX 117 * ----------------- 118 * 119 * CMI8788: 120 * 121 * I²C <-> PCM1792A 122 * 123 * GPI 0 <- external power present 124 * 125 * GPIO 0 -> enable output to speakers 126 * GPIO 1 -> route HP to front panel (0) or rear jack (1) 127 * GPIO 2 -> M0 of CS5381 128 * GPIO 3 -> M1 of CS5381 129 * GPIO 7 -> route output to speaker jacks (0) or HP (1) 130 * GPIO 8 -> route input jack to line-in (0) or mic-in (1) 131 * 132 * PCM1792A: 133 * 134 * AD0 <- 0 135 * 136 * H6 daughterboard 137 * ---------------- 138 * 139 * GPIO 4 <- 0 140 * GPIO 5 <- 0 141 */ 142 143 #include <linux/pci.h> 144 #include <linux/delay.h> 145 #include <linux/mutex.h> 146 #include <sound/ac97_codec.h> 147 #include <sound/asoundef.h> 148 #include <sound/control.h> 149 #include <sound/core.h> 150 #include <sound/initval.h> 151 #include <sound/pcm.h> 152 #include <sound/pcm_params.h> 153 #include <sound/tlv.h> 154 #include "oxygen.h" 155 #include "cm9780.h" 156 #include "pcm1796.h" 157 #include "cs4398.h" 158 #include "cs4362a.h" 159 160 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 161 MODULE_DESCRIPTION("Asus AVx00 driver"); 162 MODULE_LICENSE("GPL v2"); 163 MODULE_SUPPORTED_DEVICE("{{Asus,AV100},{Asus,AV200}}"); 164 165 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 166 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 167 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 168 169 module_param_array(index, int, NULL, 0444); 170 MODULE_PARM_DESC(index, "card index"); 171 module_param_array(id, charp, NULL, 0444); 172 MODULE_PARM_DESC(id, "ID string"); 173 module_param_array(enable, bool, NULL, 0444); 174 MODULE_PARM_DESC(enable, "enable card"); 175 176 enum { 177 MODEL_D2, 178 MODEL_D2X, 179 MODEL_D1, 180 MODEL_DX, 181 MODEL_HDAV, /* without daughterboard */ 182 MODEL_HDAV_H6, /* with H6 daughterboard */ 183 MODEL_STX, 184 }; 185 186 static struct pci_device_id xonar_ids[] __devinitdata = { 187 { OXYGEN_PCI_SUBID(0x1043, 0x8269), .driver_data = MODEL_D2 }, 188 { OXYGEN_PCI_SUBID(0x1043, 0x8275), .driver_data = MODEL_DX }, 189 { OXYGEN_PCI_SUBID(0x1043, 0x82b7), .driver_data = MODEL_D2X }, 190 { OXYGEN_PCI_SUBID(0x1043, 0x8314), .driver_data = MODEL_HDAV }, 191 { OXYGEN_PCI_SUBID(0x1043, 0x8327), .driver_data = MODEL_DX }, 192 { OXYGEN_PCI_SUBID(0x1043, 0x834f), .driver_data = MODEL_D1 }, 193 { OXYGEN_PCI_SUBID(0x1043, 0x835c), .driver_data = MODEL_STX }, 194 { OXYGEN_PCI_SUBID_BROKEN_EEPROM }, 195 { } 196 }; 197 MODULE_DEVICE_TABLE(pci, xonar_ids); 198 199 200 #define GPIO_CS53x1_M_MASK 0x000c 201 #define GPIO_CS53x1_M_SINGLE 0x0000 202 #define GPIO_CS53x1_M_DOUBLE 0x0004 203 #define GPIO_CS53x1_M_QUAD 0x0008 204 205 #define GPIO_D2X_EXT_POWER 0x0020 206 #define GPIO_D2_ALT 0x0080 207 #define GPIO_D2_OUTPUT_ENABLE 0x0100 208 209 #define GPI_DX_EXT_POWER 0x01 210 #define GPIO_DX_OUTPUT_ENABLE 0x0001 211 #define GPIO_DX_FRONT_PANEL 0x0002 212 #define GPIO_DX_INPUT_ROUTE 0x0100 213 214 #define GPIO_HDAV_DB_MASK 0x0030 215 #define GPIO_HDAV_DB_H6 0x0000 216 #define GPIO_HDAV_DB_XX 0x0020 217 218 #define GPIO_ST_HP_REAR 0x0002 219 #define GPIO_ST_HP 0x0080 220 221 #define I2C_DEVICE_PCM1796(i) (0x98 + ((i) << 1)) /* 10011, ADx=i, /W=0 */ 222 #define I2C_DEVICE_CS4398 0x9e /* 10011, AD1=1, AD0=1, /W=0 */ 223 #define I2C_DEVICE_CS4362A 0x30 /* 001100, AD0=0, /W=0 */ 224 225 struct xonar_data { 226 unsigned int anti_pop_delay; 227 unsigned int dacs; 228 u16 output_enable_bit; 229 u8 ext_power_reg; 230 u8 ext_power_int_reg; 231 u8 ext_power_bit; 232 u8 has_power; 233 u8 pcm1796_oversampling; 234 u8 cs4398_fm; 235 u8 cs4362a_fm; 236 u8 hdmi_params[5]; 237 }; 238 239 static void xonar_gpio_changed(struct oxygen *chip); 240 241 static inline void pcm1796_write_spi(struct oxygen *chip, unsigned int codec, 242 u8 reg, u8 value) 243 { 244 /* maps ALSA channel pair number to SPI output */ 245 static const u8 codec_map[4] = { 246 0, 1, 2, 4 247 }; 248 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER | 249 OXYGEN_SPI_DATA_LENGTH_2 | 250 OXYGEN_SPI_CLOCK_160 | 251 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) | 252 OXYGEN_SPI_CEN_LATCH_CLOCK_HI, 253 (reg << 8) | value); 254 } 255 256 static inline void pcm1796_write_i2c(struct oxygen *chip, unsigned int codec, 257 u8 reg, u8 value) 258 { 259 oxygen_write_i2c(chip, I2C_DEVICE_PCM1796(codec), reg, value); 260 } 261 262 static void pcm1796_write(struct oxygen *chip, unsigned int codec, 263 u8 reg, u8 value) 264 { 265 if ((chip->model.function_flags & OXYGEN_FUNCTION_2WIRE_SPI_MASK) == 266 OXYGEN_FUNCTION_SPI) 267 pcm1796_write_spi(chip, codec, reg, value); 268 else 269 pcm1796_write_i2c(chip, codec, reg, value); 270 } 271 272 static void cs4398_write(struct oxygen *chip, u8 reg, u8 value) 273 { 274 oxygen_write_i2c(chip, I2C_DEVICE_CS4398, reg, value); 275 } 276 277 static void cs4362a_write(struct oxygen *chip, u8 reg, u8 value) 278 { 279 oxygen_write_i2c(chip, I2C_DEVICE_CS4362A, reg, value); 280 } 281 282 static void hdmi_write_command(struct oxygen *chip, u8 command, 283 unsigned int count, const u8 *params) 284 { 285 unsigned int i; 286 u8 checksum; 287 288 oxygen_write_uart(chip, 0xfb); 289 oxygen_write_uart(chip, 0xef); 290 oxygen_write_uart(chip, command); 291 oxygen_write_uart(chip, count); 292 for (i = 0; i < count; ++i) 293 oxygen_write_uart(chip, params[i]); 294 checksum = 0xfb + 0xef + command + count; 295 for (i = 0; i < count; ++i) 296 checksum += params[i]; 297 oxygen_write_uart(chip, checksum); 298 } 299 300 static void xonar_enable_output(struct oxygen *chip) 301 { 302 struct xonar_data *data = chip->model_data; 303 304 msleep(data->anti_pop_delay); 305 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit); 306 } 307 308 static void xonar_common_init(struct oxygen *chip) 309 { 310 struct xonar_data *data = chip->model_data; 311 312 if (data->ext_power_reg) { 313 oxygen_set_bits8(chip, data->ext_power_int_reg, 314 data->ext_power_bit); 315 chip->interrupt_mask |= OXYGEN_INT_GPIO; 316 chip->model.gpio_changed = xonar_gpio_changed; 317 data->has_power = !!(oxygen_read8(chip, data->ext_power_reg) 318 & data->ext_power_bit); 319 } 320 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 321 GPIO_CS53x1_M_MASK | data->output_enable_bit); 322 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 323 GPIO_CS53x1_M_SINGLE, GPIO_CS53x1_M_MASK); 324 oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC); 325 xonar_enable_output(chip); 326 } 327 328 static void update_pcm1796_volume(struct oxygen *chip) 329 { 330 struct xonar_data *data = chip->model_data; 331 unsigned int i; 332 333 for (i = 0; i < data->dacs; ++i) { 334 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]); 335 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]); 336 } 337 } 338 339 static void update_pcm1796_mute(struct oxygen *chip) 340 { 341 struct xonar_data *data = chip->model_data; 342 unsigned int i; 343 u8 value; 344 345 value = PCM1796_DMF_DISABLED | PCM1796_FMT_24_LJUST | PCM1796_ATLD; 346 if (chip->dac_mute) 347 value |= PCM1796_MUTE; 348 for (i = 0; i < data->dacs; ++i) 349 pcm1796_write(chip, i, 18, value); 350 } 351 352 static void pcm1796_init(struct oxygen *chip) 353 { 354 struct xonar_data *data = chip->model_data; 355 unsigned int i; 356 357 for (i = 0; i < data->dacs; ++i) { 358 pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1); 359 pcm1796_write(chip, i, 20, data->pcm1796_oversampling); 360 pcm1796_write(chip, i, 21, 0); 361 } 362 update_pcm1796_mute(chip); /* set ATLD before ATL/ATR */ 363 update_pcm1796_volume(chip); 364 } 365 366 static void xonar_d2_init(struct oxygen *chip) 367 { 368 struct xonar_data *data = chip->model_data; 369 370 data->anti_pop_delay = 300; 371 data->dacs = 4; 372 data->output_enable_bit = GPIO_D2_OUTPUT_ENABLE; 373 data->pcm1796_oversampling = PCM1796_OS_64; 374 375 pcm1796_init(chip); 376 377 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT); 378 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT); 379 380 xonar_common_init(chip); 381 382 snd_component_add(chip->card, "PCM1796"); 383 snd_component_add(chip->card, "CS5381"); 384 } 385 386 static void xonar_d2x_init(struct oxygen *chip) 387 { 388 struct xonar_data *data = chip->model_data; 389 390 data->ext_power_reg = OXYGEN_GPIO_DATA; 391 data->ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK; 392 data->ext_power_bit = GPIO_D2X_EXT_POWER; 393 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2X_EXT_POWER); 394 395 xonar_d2_init(chip); 396 } 397 398 static void update_cs4362a_volumes(struct oxygen *chip) 399 { 400 u8 mute; 401 402 mute = chip->dac_mute ? CS4362A_MUTE : 0; 403 cs4362a_write(chip, 7, (127 - chip->dac_volume[2]) | mute); 404 cs4362a_write(chip, 8, (127 - chip->dac_volume[3]) | mute); 405 cs4362a_write(chip, 10, (127 - chip->dac_volume[4]) | mute); 406 cs4362a_write(chip, 11, (127 - chip->dac_volume[5]) | mute); 407 cs4362a_write(chip, 13, (127 - chip->dac_volume[6]) | mute); 408 cs4362a_write(chip, 14, (127 - chip->dac_volume[7]) | mute); 409 } 410 411 static void update_cs43xx_volume(struct oxygen *chip) 412 { 413 cs4398_write(chip, 5, (127 - chip->dac_volume[0]) * 2); 414 cs4398_write(chip, 6, (127 - chip->dac_volume[1]) * 2); 415 update_cs4362a_volumes(chip); 416 } 417 418 static void update_cs43xx_mute(struct oxygen *chip) 419 { 420 u8 reg; 421 422 reg = CS4398_MUTEP_LOW | CS4398_PAMUTE; 423 if (chip->dac_mute) 424 reg |= CS4398_MUTE_B | CS4398_MUTE_A; 425 cs4398_write(chip, 4, reg); 426 update_cs4362a_volumes(chip); 427 } 428 429 static void cs43xx_init(struct oxygen *chip) 430 { 431 struct xonar_data *data = chip->model_data; 432 433 /* set CPEN (control port mode) and power down */ 434 cs4398_write(chip, 8, CS4398_CPEN | CS4398_PDN); 435 cs4362a_write(chip, 0x01, CS4362A_PDN | CS4362A_CPEN); 436 /* configure */ 437 cs4398_write(chip, 2, data->cs4398_fm); 438 cs4398_write(chip, 3, CS4398_ATAPI_B_R | CS4398_ATAPI_A_L); 439 cs4398_write(chip, 7, CS4398_RMP_DN | CS4398_RMP_UP | 440 CS4398_ZERO_CROSS | CS4398_SOFT_RAMP); 441 cs4362a_write(chip, 0x02, CS4362A_DIF_LJUST); 442 cs4362a_write(chip, 0x03, CS4362A_MUTEC_6 | CS4362A_AMUTE | 443 CS4362A_RMP_UP | CS4362A_ZERO_CROSS | CS4362A_SOFT_RAMP); 444 cs4362a_write(chip, 0x04, CS4362A_RMP_DN | CS4362A_DEM_NONE); 445 cs4362a_write(chip, 0x05, 0); 446 cs4362a_write(chip, 0x06, data->cs4362a_fm); 447 cs4362a_write(chip, 0x09, data->cs4362a_fm); 448 cs4362a_write(chip, 0x0c, data->cs4362a_fm); 449 update_cs43xx_volume(chip); 450 update_cs43xx_mute(chip); 451 /* clear power down */ 452 cs4398_write(chip, 8, CS4398_CPEN); 453 cs4362a_write(chip, 0x01, CS4362A_CPEN); 454 } 455 456 static void xonar_d1_init(struct oxygen *chip) 457 { 458 struct xonar_data *data = chip->model_data; 459 460 data->anti_pop_delay = 800; 461 data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE; 462 data->cs4398_fm = CS4398_FM_SINGLE | CS4398_DEM_NONE | CS4398_DIF_LJUST; 463 data->cs4362a_fm = CS4362A_FM_SINGLE | 464 CS4362A_ATAPI_B_R | CS4362A_ATAPI_A_L; 465 466 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS, 467 OXYGEN_2WIRE_LENGTH_8 | 468 OXYGEN_2WIRE_INTERRUPT_MASK | 469 OXYGEN_2WIRE_SPEED_FAST); 470 471 cs43xx_init(chip); 472 473 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 474 GPIO_DX_FRONT_PANEL | GPIO_DX_INPUT_ROUTE); 475 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 476 GPIO_DX_FRONT_PANEL | GPIO_DX_INPUT_ROUTE); 477 478 xonar_common_init(chip); 479 480 snd_component_add(chip->card, "CS4398"); 481 snd_component_add(chip->card, "CS4362A"); 482 snd_component_add(chip->card, "CS5361"); 483 } 484 485 static void xonar_dx_init(struct oxygen *chip) 486 { 487 struct xonar_data *data = chip->model_data; 488 489 data->ext_power_reg = OXYGEN_GPI_DATA; 490 data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK; 491 data->ext_power_bit = GPI_DX_EXT_POWER; 492 493 xonar_d1_init(chip); 494 } 495 496 static void xonar_hdav_init(struct oxygen *chip) 497 { 498 struct xonar_data *data = chip->model_data; 499 u8 param; 500 501 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS, 502 OXYGEN_2WIRE_LENGTH_8 | 503 OXYGEN_2WIRE_INTERRUPT_MASK | 504 OXYGEN_2WIRE_SPEED_FAST); 505 506 data->anti_pop_delay = 100; 507 data->dacs = chip->model.private_data == MODEL_HDAV_H6 ? 4 : 1; 508 data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE; 509 data->ext_power_reg = OXYGEN_GPI_DATA; 510 data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK; 511 data->ext_power_bit = GPI_DX_EXT_POWER; 512 data->pcm1796_oversampling = PCM1796_OS_64; 513 514 pcm1796_init(chip); 515 516 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DX_INPUT_ROUTE); 517 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_DX_INPUT_ROUTE); 518 519 oxygen_reset_uart(chip); 520 param = 0; 521 hdmi_write_command(chip, 0x61, 1, ¶m); 522 param = 1; 523 hdmi_write_command(chip, 0x74, 1, ¶m); 524 data->hdmi_params[1] = IEC958_AES3_CON_FS_48000; 525 data->hdmi_params[4] = 1; 526 hdmi_write_command(chip, 0x54, 5, data->hdmi_params); 527 528 xonar_common_init(chip); 529 530 snd_component_add(chip->card, "PCM1796"); 531 snd_component_add(chip->card, "CS5381"); 532 } 533 534 static void xonar_stx_init(struct oxygen *chip) 535 { 536 struct xonar_data *data = chip->model_data; 537 538 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS, 539 OXYGEN_2WIRE_LENGTH_8 | 540 OXYGEN_2WIRE_INTERRUPT_MASK | 541 OXYGEN_2WIRE_SPEED_FAST); 542 543 data->anti_pop_delay = 100; 544 data->dacs = 1; 545 data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE; 546 data->ext_power_reg = OXYGEN_GPI_DATA; 547 data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK; 548 data->ext_power_bit = GPI_DX_EXT_POWER; 549 data->pcm1796_oversampling = PCM1796_OS_64; 550 551 pcm1796_init(chip); 552 553 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 554 GPIO_DX_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP); 555 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 556 GPIO_DX_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP); 557 558 xonar_common_init(chip); 559 560 snd_component_add(chip->card, "PCM1792A"); 561 snd_component_add(chip->card, "CS5381"); 562 } 563 564 static void xonar_disable_output(struct oxygen *chip) 565 { 566 struct xonar_data *data = chip->model_data; 567 568 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit); 569 } 570 571 static void xonar_d2_cleanup(struct oxygen *chip) 572 { 573 xonar_disable_output(chip); 574 } 575 576 static void xonar_d1_cleanup(struct oxygen *chip) 577 { 578 xonar_disable_output(chip); 579 cs4362a_write(chip, 0x01, CS4362A_PDN | CS4362A_CPEN); 580 oxygen_clear_bits8(chip, OXYGEN_FUNCTION, OXYGEN_FUNCTION_RESET_CODEC); 581 } 582 583 static void xonar_hdav_cleanup(struct oxygen *chip) 584 { 585 u8 param = 0; 586 587 hdmi_write_command(chip, 0x74, 1, ¶m); 588 xonar_disable_output(chip); 589 } 590 591 static void xonar_st_cleanup(struct oxygen *chip) 592 { 593 xonar_disable_output(chip); 594 } 595 596 static void xonar_d2_suspend(struct oxygen *chip) 597 { 598 xonar_d2_cleanup(chip); 599 } 600 601 static void xonar_d1_suspend(struct oxygen *chip) 602 { 603 xonar_d1_cleanup(chip); 604 } 605 606 static void xonar_hdav_suspend(struct oxygen *chip) 607 { 608 xonar_hdav_cleanup(chip); 609 msleep(2); 610 } 611 612 static void xonar_st_suspend(struct oxygen *chip) 613 { 614 xonar_st_cleanup(chip); 615 } 616 617 static void xonar_d2_resume(struct oxygen *chip) 618 { 619 pcm1796_init(chip); 620 xonar_enable_output(chip); 621 } 622 623 static void xonar_d1_resume(struct oxygen *chip) 624 { 625 cs43xx_init(chip); 626 xonar_enable_output(chip); 627 } 628 629 static void xonar_hdav_resume(struct oxygen *chip) 630 { 631 struct xonar_data *data = chip->model_data; 632 u8 param; 633 634 oxygen_reset_uart(chip); 635 param = 0; 636 hdmi_write_command(chip, 0x61, 1, ¶m); 637 param = 1; 638 hdmi_write_command(chip, 0x74, 1, ¶m); 639 hdmi_write_command(chip, 0x54, 5, data->hdmi_params); 640 pcm1796_init(chip); 641 xonar_enable_output(chip); 642 } 643 644 static void xonar_st_resume(struct oxygen *chip) 645 { 646 pcm1796_init(chip); 647 xonar_enable_output(chip); 648 } 649 650 static void xonar_hdav_pcm_hardware_filter(unsigned int channel, 651 struct snd_pcm_hardware *hardware) 652 { 653 if (channel == PCM_MULTICH) { 654 hardware->rates = SNDRV_PCM_RATE_44100 | 655 SNDRV_PCM_RATE_48000 | 656 SNDRV_PCM_RATE_96000 | 657 SNDRV_PCM_RATE_192000; 658 hardware->rate_min = 44100; 659 } 660 } 661 662 static void set_pcm1796_params(struct oxygen *chip, 663 struct snd_pcm_hw_params *params) 664 { 665 struct xonar_data *data = chip->model_data; 666 unsigned int i; 667 668 data->pcm1796_oversampling = 669 params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64; 670 for (i = 0; i < data->dacs; ++i) 671 pcm1796_write(chip, i, 20, data->pcm1796_oversampling); 672 } 673 674 static void set_cs53x1_params(struct oxygen *chip, 675 struct snd_pcm_hw_params *params) 676 { 677 unsigned int value; 678 679 if (params_rate(params) <= 54000) 680 value = GPIO_CS53x1_M_SINGLE; 681 else if (params_rate(params) <= 108000) 682 value = GPIO_CS53x1_M_DOUBLE; 683 else 684 value = GPIO_CS53x1_M_QUAD; 685 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 686 value, GPIO_CS53x1_M_MASK); 687 } 688 689 static void set_cs43xx_params(struct oxygen *chip, 690 struct snd_pcm_hw_params *params) 691 { 692 struct xonar_data *data = chip->model_data; 693 694 data->cs4398_fm = CS4398_DEM_NONE | CS4398_DIF_LJUST; 695 data->cs4362a_fm = CS4362A_ATAPI_B_R | CS4362A_ATAPI_A_L; 696 if (params_rate(params) <= 50000) { 697 data->cs4398_fm |= CS4398_FM_SINGLE; 698 data->cs4362a_fm |= CS4362A_FM_SINGLE; 699 } else if (params_rate(params) <= 100000) { 700 data->cs4398_fm |= CS4398_FM_DOUBLE; 701 data->cs4362a_fm |= CS4362A_FM_DOUBLE; 702 } else { 703 data->cs4398_fm |= CS4398_FM_QUAD; 704 data->cs4362a_fm |= CS4362A_FM_QUAD; 705 } 706 cs4398_write(chip, 2, data->cs4398_fm); 707 cs4362a_write(chip, 0x06, data->cs4362a_fm); 708 cs4362a_write(chip, 0x09, data->cs4362a_fm); 709 cs4362a_write(chip, 0x0c, data->cs4362a_fm); 710 } 711 712 static void set_hdmi_params(struct oxygen *chip, 713 struct snd_pcm_hw_params *params) 714 { 715 struct xonar_data *data = chip->model_data; 716 717 data->hdmi_params[0] = 0; /* 1 = non-audio */ 718 switch (params_rate(params)) { 719 case 44100: 720 data->hdmi_params[1] = IEC958_AES3_CON_FS_44100; 721 break; 722 case 48000: 723 data->hdmi_params[1] = IEC958_AES3_CON_FS_48000; 724 break; 725 default: /* 96000 */ 726 data->hdmi_params[1] = IEC958_AES3_CON_FS_96000; 727 break; 728 case 192000: 729 data->hdmi_params[1] = IEC958_AES3_CON_FS_192000; 730 break; 731 } 732 data->hdmi_params[2] = params_channels(params) / 2 - 1; 733 if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE) 734 data->hdmi_params[3] = 0; 735 else 736 data->hdmi_params[3] = 0xc0; 737 data->hdmi_params[4] = 1; /* ? */ 738 hdmi_write_command(chip, 0x54, 5, data->hdmi_params); 739 } 740 741 static void set_hdav_params(struct oxygen *chip, 742 struct snd_pcm_hw_params *params) 743 { 744 set_pcm1796_params(chip, params); 745 set_hdmi_params(chip, params); 746 } 747 748 static void xonar_gpio_changed(struct oxygen *chip) 749 { 750 struct xonar_data *data = chip->model_data; 751 u8 has_power; 752 753 has_power = !!(oxygen_read8(chip, data->ext_power_reg) 754 & data->ext_power_bit); 755 if (has_power != data->has_power) { 756 data->has_power = has_power; 757 if (has_power) { 758 snd_printk(KERN_NOTICE "power restored\n"); 759 } else { 760 snd_printk(KERN_CRIT 761 "Hey! Don't unplug the power cable!\n"); 762 /* TODO: stop PCMs */ 763 } 764 } 765 } 766 767 static void xonar_hdav_uart_input(struct oxygen *chip) 768 { 769 if (chip->uart_input_count >= 2 && 770 chip->uart_input[chip->uart_input_count - 2] == 'O' && 771 chip->uart_input[chip->uart_input_count - 1] == 'K') { 772 printk(KERN_DEBUG "message from Xonar HDAV HDMI chip received:\n"); 773 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 774 chip->uart_input, chip->uart_input_count); 775 chip->uart_input_count = 0; 776 } 777 } 778 779 static int gpio_bit_switch_get(struct snd_kcontrol *ctl, 780 struct snd_ctl_elem_value *value) 781 { 782 struct oxygen *chip = ctl->private_data; 783 u16 bit = ctl->private_value; 784 785 value->value.integer.value[0] = 786 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & bit); 787 return 0; 788 } 789 790 static int gpio_bit_switch_put(struct snd_kcontrol *ctl, 791 struct snd_ctl_elem_value *value) 792 { 793 struct oxygen *chip = ctl->private_data; 794 u16 bit = ctl->private_value; 795 u16 old_bits, new_bits; 796 int changed; 797 798 spin_lock_irq(&chip->reg_lock); 799 old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA); 800 if (value->value.integer.value[0]) 801 new_bits = old_bits | bit; 802 else 803 new_bits = old_bits & ~bit; 804 changed = new_bits != old_bits; 805 if (changed) 806 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits); 807 spin_unlock_irq(&chip->reg_lock); 808 return changed; 809 } 810 811 static const struct snd_kcontrol_new alt_switch = { 812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 813 .name = "Analog Loopback Switch", 814 .info = snd_ctl_boolean_mono_info, 815 .get = gpio_bit_switch_get, 816 .put = gpio_bit_switch_put, 817 .private_value = GPIO_D2_ALT, 818 }; 819 820 static const struct snd_kcontrol_new front_panel_switch = { 821 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 822 .name = "Front Panel Switch", 823 .info = snd_ctl_boolean_mono_info, 824 .get = gpio_bit_switch_get, 825 .put = gpio_bit_switch_put, 826 .private_value = GPIO_DX_FRONT_PANEL, 827 }; 828 829 static int st_output_switch_info(struct snd_kcontrol *ctl, 830 struct snd_ctl_elem_info *info) 831 { 832 static const char *const names[3] = { 833 "Speakers", "Headphones", "FP Headphones" 834 }; 835 836 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 837 info->count = 1; 838 info->value.enumerated.items = 3; 839 if (info->value.enumerated.item >= 3) 840 info->value.enumerated.item = 2; 841 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]); 842 return 0; 843 } 844 845 static int st_output_switch_get(struct snd_kcontrol *ctl, 846 struct snd_ctl_elem_value *value) 847 { 848 struct oxygen *chip = ctl->private_data; 849 u16 gpio; 850 851 gpio = oxygen_read16(chip, OXYGEN_GPIO_DATA); 852 if (!(gpio & GPIO_ST_HP)) 853 value->value.enumerated.item[0] = 0; 854 else if (gpio & GPIO_ST_HP_REAR) 855 value->value.enumerated.item[0] = 1; 856 else 857 value->value.enumerated.item[0] = 2; 858 return 0; 859 } 860 861 862 static int st_output_switch_put(struct snd_kcontrol *ctl, 863 struct snd_ctl_elem_value *value) 864 { 865 struct oxygen *chip = ctl->private_data; 866 u16 gpio_old, gpio; 867 868 mutex_lock(&chip->mutex); 869 gpio_old = oxygen_read16(chip, OXYGEN_GPIO_DATA); 870 gpio = gpio_old; 871 switch (value->value.enumerated.item[0]) { 872 case 0: 873 gpio &= ~(GPIO_ST_HP | GPIO_ST_HP_REAR); 874 break; 875 case 1: 876 gpio |= GPIO_ST_HP | GPIO_ST_HP_REAR; 877 break; 878 case 2: 879 gpio = (gpio | GPIO_ST_HP) & ~GPIO_ST_HP_REAR; 880 break; 881 } 882 oxygen_write16(chip, OXYGEN_GPIO_DATA, gpio); 883 mutex_unlock(&chip->mutex); 884 return gpio != gpio_old; 885 } 886 887 static const struct snd_kcontrol_new st_output_switch = { 888 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 889 .name = "Analog Output", 890 .info = st_output_switch_info, 891 .get = st_output_switch_get, 892 .put = st_output_switch_put, 893 }; 894 895 static void xonar_line_mic_ac97_switch(struct oxygen *chip, 896 unsigned int reg, unsigned int mute) 897 { 898 if (reg == AC97_LINE) { 899 spin_lock_irq(&chip->reg_lock); 900 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 901 mute ? GPIO_DX_INPUT_ROUTE : 0, 902 GPIO_DX_INPUT_ROUTE); 903 spin_unlock_irq(&chip->reg_lock); 904 } 905 } 906 907 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -6000, 50, 0); 908 static const DECLARE_TLV_DB_SCALE(cs4362a_db_scale, -6000, 100, 0); 909 910 static int xonar_d2_control_filter(struct snd_kcontrol_new *template) 911 { 912 if (!strncmp(template->name, "CD Capture ", 11)) 913 /* CD in is actually connected to the video in pin */ 914 template->private_value ^= AC97_CD ^ AC97_VIDEO; 915 return 0; 916 } 917 918 static int xonar_d1_control_filter(struct snd_kcontrol_new *template) 919 { 920 if (!strncmp(template->name, "CD Capture ", 11)) 921 return 1; /* no CD input */ 922 return 0; 923 } 924 925 static int xonar_st_control_filter(struct snd_kcontrol_new *template) 926 { 927 if (!strncmp(template->name, "CD Capture ", 11)) 928 return 1; /* no CD input */ 929 if (!strcmp(template->name, "Stereo Upmixing")) 930 return 1; /* stereo only - we don't need upmixing */ 931 return 0; 932 } 933 934 static int xonar_d2_mixer_init(struct oxygen *chip) 935 { 936 return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip)); 937 } 938 939 static int xonar_d1_mixer_init(struct oxygen *chip) 940 { 941 return snd_ctl_add(chip->card, snd_ctl_new1(&front_panel_switch, chip)); 942 } 943 944 static int xonar_st_mixer_init(struct oxygen *chip) 945 { 946 return snd_ctl_add(chip->card, snd_ctl_new1(&st_output_switch, chip)); 947 } 948 949 static const struct oxygen_model model_xonar_d2 = { 950 .longname = "Asus Virtuoso 200", 951 .chip = "AV200", 952 .init = xonar_d2_init, 953 .control_filter = xonar_d2_control_filter, 954 .mixer_init = xonar_d2_mixer_init, 955 .cleanup = xonar_d2_cleanup, 956 .suspend = xonar_d2_suspend, 957 .resume = xonar_d2_resume, 958 .set_dac_params = set_pcm1796_params, 959 .set_adc_params = set_cs53x1_params, 960 .update_dac_volume = update_pcm1796_volume, 961 .update_dac_mute = update_pcm1796_mute, 962 .dac_tlv = pcm1796_db_scale, 963 .model_data_size = sizeof(struct xonar_data), 964 .device_config = PLAYBACK_0_TO_I2S | 965 PLAYBACK_1_TO_SPDIF | 966 CAPTURE_0_FROM_I2S_2 | 967 CAPTURE_1_FROM_SPDIF | 968 MIDI_OUTPUT | 969 MIDI_INPUT, 970 .dac_channels = 8, 971 .dac_volume_min = 255 - 2*60, 972 .dac_volume_max = 255, 973 .misc_flags = OXYGEN_MISC_MIDI, 974 .function_flags = OXYGEN_FUNCTION_SPI | 975 OXYGEN_FUNCTION_ENABLE_SPI_4_5, 976 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 977 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 978 }; 979 980 static const struct oxygen_model model_xonar_d1 = { 981 .longname = "Asus Virtuoso 100", 982 .chip = "AV200", 983 .init = xonar_d1_init, 984 .control_filter = xonar_d1_control_filter, 985 .mixer_init = xonar_d1_mixer_init, 986 .cleanup = xonar_d1_cleanup, 987 .suspend = xonar_d1_suspend, 988 .resume = xonar_d1_resume, 989 .set_dac_params = set_cs43xx_params, 990 .set_adc_params = set_cs53x1_params, 991 .update_dac_volume = update_cs43xx_volume, 992 .update_dac_mute = update_cs43xx_mute, 993 .ac97_switch = xonar_line_mic_ac97_switch, 994 .dac_tlv = cs4362a_db_scale, 995 .model_data_size = sizeof(struct xonar_data), 996 .device_config = PLAYBACK_0_TO_I2S | 997 PLAYBACK_1_TO_SPDIF | 998 CAPTURE_0_FROM_I2S_2, 999 .dac_channels = 8, 1000 .dac_volume_min = 127 - 60, 1001 .dac_volume_max = 127, 1002 .function_flags = OXYGEN_FUNCTION_2WIRE, 1003 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 1004 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 1005 }; 1006 1007 static const struct oxygen_model model_xonar_hdav = { 1008 .longname = "Asus Virtuoso 200", 1009 .chip = "AV200", 1010 .init = xonar_hdav_init, 1011 .cleanup = xonar_hdav_cleanup, 1012 .suspend = xonar_hdav_suspend, 1013 .resume = xonar_hdav_resume, 1014 .pcm_hardware_filter = xonar_hdav_pcm_hardware_filter, 1015 .set_dac_params = set_hdav_params, 1016 .set_adc_params = set_cs53x1_params, 1017 .update_dac_volume = update_pcm1796_volume, 1018 .update_dac_mute = update_pcm1796_mute, 1019 .uart_input = xonar_hdav_uart_input, 1020 .ac97_switch = xonar_line_mic_ac97_switch, 1021 .dac_tlv = pcm1796_db_scale, 1022 .model_data_size = sizeof(struct xonar_data), 1023 .device_config = PLAYBACK_0_TO_I2S | 1024 PLAYBACK_1_TO_SPDIF | 1025 CAPTURE_0_FROM_I2S_2, 1026 .dac_channels = 8, 1027 .dac_volume_min = 255 - 2*60, 1028 .dac_volume_max = 255, 1029 .misc_flags = OXYGEN_MISC_MIDI, 1030 .function_flags = OXYGEN_FUNCTION_2WIRE, 1031 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 1032 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 1033 }; 1034 1035 static const struct oxygen_model model_xonar_st = { 1036 .longname = "Asus Virtuoso 100", 1037 .chip = "AV200", 1038 .init = xonar_stx_init, 1039 .control_filter = xonar_st_control_filter, 1040 .mixer_init = xonar_st_mixer_init, 1041 .cleanup = xonar_st_cleanup, 1042 .suspend = xonar_st_suspend, 1043 .resume = xonar_st_resume, 1044 .set_dac_params = set_pcm1796_params, 1045 .set_adc_params = set_cs53x1_params, 1046 .update_dac_volume = update_pcm1796_volume, 1047 .update_dac_mute = update_pcm1796_mute, 1048 .ac97_switch = xonar_line_mic_ac97_switch, 1049 .dac_tlv = pcm1796_db_scale, 1050 .model_data_size = sizeof(struct xonar_data), 1051 .device_config = PLAYBACK_0_TO_I2S | 1052 PLAYBACK_1_TO_SPDIF | 1053 CAPTURE_0_FROM_I2S_2, 1054 .dac_channels = 2, 1055 .dac_volume_min = 255 - 2*60, 1056 .dac_volume_max = 255, 1057 .function_flags = OXYGEN_FUNCTION_2WIRE, 1058 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 1059 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 1060 }; 1061 1062 static int __devinit get_xonar_model(struct oxygen *chip, 1063 const struct pci_device_id *id) 1064 { 1065 static const struct oxygen_model *const models[] = { 1066 [MODEL_D1] = &model_xonar_d1, 1067 [MODEL_DX] = &model_xonar_d1, 1068 [MODEL_D2] = &model_xonar_d2, 1069 [MODEL_D2X] = &model_xonar_d2, 1070 [MODEL_HDAV] = &model_xonar_hdav, 1071 [MODEL_STX] = &model_xonar_st, 1072 }; 1073 static const char *const names[] = { 1074 [MODEL_D1] = "Xonar D1", 1075 [MODEL_DX] = "Xonar DX", 1076 [MODEL_D2] = "Xonar D2", 1077 [MODEL_D2X] = "Xonar D2X", 1078 [MODEL_HDAV] = "Xonar HDAV1.3", 1079 [MODEL_HDAV_H6] = "Xonar HDAV1.3+H6", 1080 [MODEL_STX] = "Xonar Essence STX", 1081 }; 1082 unsigned int model = id->driver_data; 1083 1084 if (model >= ARRAY_SIZE(models) || !models[model]) 1085 return -EINVAL; 1086 chip->model = *models[model]; 1087 1088 switch (model) { 1089 case MODEL_D2X: 1090 chip->model.init = xonar_d2x_init; 1091 break; 1092 case MODEL_DX: 1093 chip->model.init = xonar_dx_init; 1094 break; 1095 case MODEL_HDAV: 1096 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, 1097 GPIO_HDAV_DB_MASK); 1098 switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & 1099 GPIO_HDAV_DB_MASK) { 1100 case GPIO_HDAV_DB_H6: 1101 model = MODEL_HDAV_H6; 1102 break; 1103 case GPIO_HDAV_DB_XX: 1104 snd_printk(KERN_ERR "unknown daughterboard\n"); 1105 return -ENODEV; 1106 } 1107 break; 1108 case MODEL_STX: 1109 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, 1110 GPIO_HDAV_DB_MASK); 1111 break; 1112 } 1113 1114 chip->model.shortname = names[model]; 1115 chip->model.private_data = model; 1116 return 0; 1117 } 1118 1119 static int __devinit xonar_probe(struct pci_dev *pci, 1120 const struct pci_device_id *pci_id) 1121 { 1122 static int dev; 1123 int err; 1124 1125 if (dev >= SNDRV_CARDS) 1126 return -ENODEV; 1127 if (!enable[dev]) { 1128 ++dev; 1129 return -ENOENT; 1130 } 1131 err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE, 1132 xonar_ids, get_xonar_model); 1133 if (err >= 0) 1134 ++dev; 1135 return err; 1136 } 1137 1138 static struct pci_driver xonar_driver = { 1139 .name = "AV200", 1140 .id_table = xonar_ids, 1141 .probe = xonar_probe, 1142 .remove = __devexit_p(oxygen_pci_remove), 1143 #ifdef CONFIG_PM 1144 .suspend = oxygen_pci_suspend, 1145 .resume = oxygen_pci_resume, 1146 #endif 1147 }; 1148 1149 static int __init alsa_card_xonar_init(void) 1150 { 1151 return pci_register_driver(&xonar_driver); 1152 } 1153 1154 static void __exit alsa_card_xonar_exit(void) 1155 { 1156 pci_unregister_driver(&xonar_driver); 1157 } 1158 1159 module_init(alsa_card_xonar_init) 1160 module_exit(alsa_card_xonar_exit) 1161