Add linux-next specific files for 20110615
[linux-2.6/next.git] / sound / pci / oxygen / xonar_pcm179x.c
blob54cad38ec30a27f681d1adfb5bce7e2e0b27c394
1 /*
2 * card driver for models with PCM1796 DACs (Xonar D2/D2X/HDAV1.3/ST/STX)
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this driver; if not, see <http://www.gnu.org/licenses/>.
20 * Xonar D2/D2X
21 * ------------
23 * CMI8788:
25 * SPI 0 -> 1st PCM1796 (front)
26 * SPI 1 -> 2nd PCM1796 (surround)
27 * SPI 2 -> 3rd PCM1796 (center/LFE)
28 * SPI 4 -> 4th PCM1796 (back)
30 * GPIO 2 -> M0 of CS5381
31 * GPIO 3 -> M1 of CS5381
32 * GPIO 5 <- external power present (D2X only)
33 * GPIO 7 -> ALT
34 * GPIO 8 -> enable output to speakers
36 * CM9780:
38 * LINE_OUT -> input of ADC
40 * AUX_IN <- aux
41 * VIDEO_IN <- CD
42 * FMIC_IN <- mic
44 * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
48 * Xonar HDAV1.3 (Deluxe)
49 * ----------------------
51 * CMI8788:
53 * I²C <-> PCM1796 (addr 1001100) (front)
55 * GPI 0 <- external power present
57 * GPIO 0 -> enable HDMI (0) or speaker (1) output
58 * GPIO 2 -> M0 of CS5381
59 * GPIO 3 -> M1 of CS5381
60 * GPIO 4 <- daughterboard detection
61 * GPIO 5 <- daughterboard detection
62 * GPIO 6 -> ?
63 * GPIO 7 -> ?
64 * GPIO 8 -> route input jack to line-in (0) or mic-in (1)
66 * UART <-> HDMI controller
68 * CM9780:
70 * LINE_OUT -> input of ADC
72 * AUX_IN <- aux
73 * CD_IN <- CD
74 * MIC_IN <- mic
76 * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
78 * no daughterboard
79 * ----------------
81 * GPIO 4 <- 1
83 * H6 daughterboard
84 * ----------------
86 * GPIO 4 <- 0
87 * GPIO 5 <- 0
89 * I²C <-> PCM1796 (addr 1001101) (surround)
90 * <-> PCM1796 (addr 1001110) (center/LFE)
91 * <-> PCM1796 (addr 1001111) (back)
93 * unknown daughterboard
94 * ---------------------
96 * GPIO 4 <- 0
97 * GPIO 5 <- 1
99 * I²C <-> CS4362A (addr 0011000) (surround, center/LFE, back)
103 * Xonar Essence ST (Deluxe)/STX
104 * -----------------------------
106 * CMI8788:
108 * I²C <-> PCM1792A (addr 1001100)
109 * <-> CS2000 (addr 1001110) (ST only)
111 * ADC1 MCLK -> REF_CLK of CS2000 (ST only)
113 * GPI 0 <- external power present (STX only)
115 * GPIO 0 -> enable output to speakers
116 * GPIO 1 -> route HP to front panel (0) or rear jack (1)
117 * GPIO 2 -> M0 of CS5381
118 * GPIO 3 -> M1 of CS5381
119 * GPIO 4 <- daughterboard detection
120 * GPIO 5 <- daughterboard detection
121 * GPIO 6 -> ?
122 * GPIO 7 -> route output to speaker jacks (0) or HP (1)
123 * GPIO 8 -> route input jack to line-in (0) or mic-in (1)
125 * PCM1792A:
127 * SCK <- CLK_OUT of CS2000 (ST only)
129 * CM9780:
131 * LINE_OUT -> input of ADC
133 * AUX_IN <- aux
134 * MIC_IN <- mic
136 * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
138 * H6 daughterboard
139 * ----------------
141 * GPIO 4 <- 0
142 * GPIO 5 <- 0
146 * Xonar Xense
147 * -----------
149 * CMI8788:
151 * I²C <-> PCM1796 (addr 1001100) (front)
152 * <-> CS4362A (addr 0011000) (surround, center/LFE, back)
153 * <-> CS2000 (addr 1001110)
155 * ADC1 MCLK -> REF_CLK of CS2000
157 * GPI 0 <- external power present
159 * GPIO 0 -> enable output
160 * GPIO 1 -> route HP to front panel (0) or rear jack (1)
161 * GPIO 2 -> M0 of CS5381
162 * GPIO 3 -> M1 of CS5381
163 * GPIO 4 -> enable output
164 * GPIO 5 -> enable output
165 * GPIO 6 -> ?
166 * GPIO 7 -> route output to HP (0) or speaker (1)
167 * GPIO 8 -> route input jack to mic-in (0) or line-in (1)
169 * CM9780:
171 * LINE_OUT -> input of ADC
173 * AUX_IN <- aux
174 * VIDEO_IN <- ?
175 * FMIC_IN <- mic
177 * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
178 * GPO 1 -> route mic-in from input jack (0) or front panel header (1)
181 #include <linux/pci.h>
182 #include <linux/delay.h>
183 #include <linux/mutex.h>
184 #include <sound/ac97_codec.h>
185 #include <sound/control.h>
186 #include <sound/core.h>
187 #include <sound/info.h>
188 #include <sound/pcm.h>
189 #include <sound/pcm_params.h>
190 #include <sound/tlv.h>
191 #include "xonar.h"
192 #include "cm9780.h"
193 #include "pcm1796.h"
194 #include "cs2000.h"
197 #define GPIO_D2X_EXT_POWER 0x0020
198 #define GPIO_D2_ALT 0x0080
199 #define GPIO_D2_OUTPUT_ENABLE 0x0100
201 #define GPI_EXT_POWER 0x01
202 #define GPIO_INPUT_ROUTE 0x0100
204 #define GPIO_HDAV_OUTPUT_ENABLE 0x0001
205 #define GPIO_HDAV_MAGIC 0x00c0
207 #define GPIO_DB_MASK 0x0030
208 #define GPIO_DB_H6 0x0000
210 #define GPIO_ST_OUTPUT_ENABLE 0x0001
211 #define GPIO_ST_HP_REAR 0x0002
212 #define GPIO_ST_MAGIC 0x0040
213 #define GPIO_ST_HP 0x0080
215 #define I2C_DEVICE_PCM1796(i) (0x98 + ((i) << 1)) /* 10011, ii, /W=0 */
216 #define I2C_DEVICE_CS2000 0x9c /* 100111, 0, /W=0 */
218 #define PCM1796_REG_BASE 16
221 struct xonar_pcm179x {
222 struct xonar_generic generic;
223 unsigned int dacs;
224 u8 pcm1796_regs[4][5];
225 unsigned int current_rate;
226 bool h6;
227 bool hp_active;
228 s8 hp_gain_offset;
229 bool has_cs2000;
230 u8 cs2000_regs[0x1f];
231 bool broken_i2c;
234 struct xonar_hdav {
235 struct xonar_pcm179x pcm179x;
236 struct xonar_hdmi hdmi;
240 static inline void pcm1796_write_spi(struct oxygen *chip, unsigned int codec,
241 u8 reg, u8 value)
243 /* maps ALSA channel pair number to SPI output */
244 static const u8 codec_map[4] = {
245 0, 1, 2, 4
247 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
248 OXYGEN_SPI_DATA_LENGTH_2 |
249 OXYGEN_SPI_CLOCK_160 |
250 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
251 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
252 (reg << 8) | value);
255 static inline void pcm1796_write_i2c(struct oxygen *chip, unsigned int codec,
256 u8 reg, u8 value)
258 oxygen_write_i2c(chip, I2C_DEVICE_PCM1796(codec), reg, value);
261 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
262 u8 reg, u8 value)
264 struct xonar_pcm179x *data = chip->model_data;
266 if ((chip->model.function_flags & OXYGEN_FUNCTION_2WIRE_SPI_MASK) ==
267 OXYGEN_FUNCTION_SPI)
268 pcm1796_write_spi(chip, codec, reg, value);
269 else
270 pcm1796_write_i2c(chip, codec, reg, value);
271 if ((unsigned int)(reg - PCM1796_REG_BASE)
272 < ARRAY_SIZE(data->pcm1796_regs[codec]))
273 data->pcm1796_regs[codec][reg - PCM1796_REG_BASE] = value;
276 static void pcm1796_write_cached(struct oxygen *chip, unsigned int codec,
277 u8 reg, u8 value)
279 struct xonar_pcm179x *data = chip->model_data;
281 if (value != data->pcm1796_regs[codec][reg - PCM1796_REG_BASE])
282 pcm1796_write(chip, codec, reg, value);
285 static void cs2000_write(struct oxygen *chip, u8 reg, u8 value)
287 struct xonar_pcm179x *data = chip->model_data;
289 oxygen_write_i2c(chip, I2C_DEVICE_CS2000, reg, value);
290 data->cs2000_regs[reg] = value;
293 static void cs2000_write_cached(struct oxygen *chip, u8 reg, u8 value)
295 struct xonar_pcm179x *data = chip->model_data;
297 if (value != data->cs2000_regs[reg])
298 cs2000_write(chip, reg, value);
301 static void pcm1796_registers_init(struct oxygen *chip)
303 struct xonar_pcm179x *data = chip->model_data;
304 unsigned int i;
305 s8 gain_offset;
307 msleep(1);
308 gain_offset = data->hp_active ? data->hp_gain_offset : 0;
309 for (i = 0; i < data->dacs; ++i) {
310 /* set ATLD before ATL/ATR */
311 pcm1796_write(chip, i, 18,
312 data->pcm1796_regs[0][18 - PCM1796_REG_BASE]);
313 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]
314 + gain_offset);
315 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]
316 + gain_offset);
317 pcm1796_write(chip, i, 19,
318 data->pcm1796_regs[0][19 - PCM1796_REG_BASE]);
319 pcm1796_write(chip, i, 20,
320 data->pcm1796_regs[0][20 - PCM1796_REG_BASE]);
321 pcm1796_write(chip, i, 21, 0);
322 gain_offset = 0;
326 static void pcm1796_init(struct oxygen *chip)
328 struct xonar_pcm179x *data = chip->model_data;
330 data->pcm1796_regs[0][18 - PCM1796_REG_BASE] = PCM1796_MUTE |
331 PCM1796_DMF_DISABLED | PCM1796_FMT_24_I2S | PCM1796_ATLD;
332 data->pcm1796_regs[0][19 - PCM1796_REG_BASE] =
333 PCM1796_FLT_SHARP | PCM1796_ATS_1;
334 data->pcm1796_regs[0][20 - PCM1796_REG_BASE] =
335 data->h6 ? PCM1796_OS_64 : PCM1796_OS_128;
336 pcm1796_registers_init(chip);
337 data->current_rate = 48000;
340 static void xonar_d2_init(struct oxygen *chip)
342 struct xonar_pcm179x *data = chip->model_data;
344 data->generic.anti_pop_delay = 300;
345 data->generic.output_enable_bit = GPIO_D2_OUTPUT_ENABLE;
346 data->dacs = 4;
348 pcm1796_init(chip);
350 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT);
351 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT);
353 oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
355 xonar_init_cs53x1(chip);
356 xonar_enable_output(chip);
358 snd_component_add(chip->card, "PCM1796");
359 snd_component_add(chip->card, "CS5381");
362 static void xonar_d2x_init(struct oxygen *chip)
364 struct xonar_pcm179x *data = chip->model_data;
366 data->generic.ext_power_reg = OXYGEN_GPIO_DATA;
367 data->generic.ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK;
368 data->generic.ext_power_bit = GPIO_D2X_EXT_POWER;
369 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2X_EXT_POWER);
370 xonar_init_ext_power(chip);
371 xonar_d2_init(chip);
374 static void xonar_hdav_init(struct oxygen *chip)
376 struct xonar_hdav *data = chip->model_data;
378 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
379 OXYGEN_2WIRE_LENGTH_8 |
380 OXYGEN_2WIRE_INTERRUPT_MASK |
381 OXYGEN_2WIRE_SPEED_STANDARD);
383 data->pcm179x.generic.anti_pop_delay = 100;
384 data->pcm179x.generic.output_enable_bit = GPIO_HDAV_OUTPUT_ENABLE;
385 data->pcm179x.generic.ext_power_reg = OXYGEN_GPI_DATA;
386 data->pcm179x.generic.ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
387 data->pcm179x.generic.ext_power_bit = GPI_EXT_POWER;
388 data->pcm179x.dacs = chip->model.dac_channels_mixer / 2;
389 data->pcm179x.h6 = chip->model.dac_channels_mixer > 2;
391 pcm1796_init(chip);
393 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
394 GPIO_HDAV_MAGIC | GPIO_INPUT_ROUTE);
395 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_INPUT_ROUTE);
397 xonar_init_cs53x1(chip);
398 xonar_init_ext_power(chip);
399 xonar_hdmi_init(chip, &data->hdmi);
400 xonar_enable_output(chip);
402 snd_component_add(chip->card, "PCM1796");
403 snd_component_add(chip->card, "CS5381");
406 static void xonar_st_init_i2c(struct oxygen *chip)
408 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
409 OXYGEN_2WIRE_LENGTH_8 |
410 OXYGEN_2WIRE_INTERRUPT_MASK |
411 OXYGEN_2WIRE_SPEED_STANDARD);
414 static void xonar_st_init_common(struct oxygen *chip)
416 struct xonar_pcm179x *data = chip->model_data;
418 data->generic.output_enable_bit = GPIO_ST_OUTPUT_ENABLE;
419 data->dacs = chip->model.dac_channels_mixer / 2;
420 data->hp_gain_offset = 2*-18;
422 pcm1796_init(chip);
424 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
425 GPIO_INPUT_ROUTE | GPIO_ST_HP_REAR |
426 GPIO_ST_MAGIC | GPIO_ST_HP);
427 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
428 GPIO_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP);
430 xonar_init_cs53x1(chip);
431 xonar_enable_output(chip);
433 snd_component_add(chip->card, "PCM1792A");
434 snd_component_add(chip->card, "CS5381");
437 static void cs2000_registers_init(struct oxygen *chip)
439 struct xonar_pcm179x *data = chip->model_data;
441 cs2000_write(chip, CS2000_GLOBAL_CFG, CS2000_FREEZE);
442 cs2000_write(chip, CS2000_DEV_CTRL, 0);
443 cs2000_write(chip, CS2000_DEV_CFG_1,
444 CS2000_R_MOD_SEL_1 |
445 (0 << CS2000_R_SEL_SHIFT) |
446 CS2000_AUX_OUT_SRC_REF_CLK |
447 CS2000_EN_DEV_CFG_1);
448 cs2000_write(chip, CS2000_DEV_CFG_2,
449 (0 << CS2000_LOCK_CLK_SHIFT) |
450 CS2000_FRAC_N_SRC_STATIC);
451 cs2000_write(chip, CS2000_RATIO_0 + 0, 0x00); /* 1.0 */
452 cs2000_write(chip, CS2000_RATIO_0 + 1, 0x10);
453 cs2000_write(chip, CS2000_RATIO_0 + 2, 0x00);
454 cs2000_write(chip, CS2000_RATIO_0 + 3, 0x00);
455 cs2000_write(chip, CS2000_FUN_CFG_1,
456 data->cs2000_regs[CS2000_FUN_CFG_1]);
457 cs2000_write(chip, CS2000_FUN_CFG_2, 0);
458 cs2000_write(chip, CS2000_GLOBAL_CFG, CS2000_EN_DEV_CFG_2);
459 msleep(3); /* PLL lock delay */
462 static void xonar_st_init(struct oxygen *chip)
464 struct xonar_pcm179x *data = chip->model_data;
466 data->generic.anti_pop_delay = 100;
467 data->h6 = chip->model.dac_channels_mixer > 2;
468 data->has_cs2000 = 1;
469 data->cs2000_regs[CS2000_FUN_CFG_1] = CS2000_REF_CLK_DIV_1;
470 data->broken_i2c = true;
472 oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
473 OXYGEN_RATE_48000 |
474 OXYGEN_I2S_FORMAT_I2S |
475 OXYGEN_I2S_MCLK(data->h6 ? MCLK_256 : MCLK_512) |
476 OXYGEN_I2S_BITS_16 |
477 OXYGEN_I2S_MASTER |
478 OXYGEN_I2S_BCLK_64);
480 xonar_st_init_i2c(chip);
481 cs2000_registers_init(chip);
482 xonar_st_init_common(chip);
484 snd_component_add(chip->card, "CS2000");
487 static void xonar_stx_init(struct oxygen *chip)
489 struct xonar_pcm179x *data = chip->model_data;
491 xonar_st_init_i2c(chip);
492 data->generic.anti_pop_delay = 800;
493 data->generic.ext_power_reg = OXYGEN_GPI_DATA;
494 data->generic.ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
495 data->generic.ext_power_bit = GPI_EXT_POWER;
496 xonar_init_ext_power(chip);
497 xonar_st_init_common(chip);
500 static void xonar_d2_cleanup(struct oxygen *chip)
502 xonar_disable_output(chip);
505 static void xonar_hdav_cleanup(struct oxygen *chip)
507 xonar_hdmi_cleanup(chip);
508 xonar_disable_output(chip);
509 msleep(2);
512 static void xonar_st_cleanup(struct oxygen *chip)
514 xonar_disable_output(chip);
517 static void xonar_d2_suspend(struct oxygen *chip)
519 xonar_d2_cleanup(chip);
522 static void xonar_hdav_suspend(struct oxygen *chip)
524 xonar_hdav_cleanup(chip);
527 static void xonar_st_suspend(struct oxygen *chip)
529 xonar_st_cleanup(chip);
532 static void xonar_d2_resume(struct oxygen *chip)
534 pcm1796_registers_init(chip);
535 xonar_enable_output(chip);
538 static void xonar_hdav_resume(struct oxygen *chip)
540 struct xonar_hdav *data = chip->model_data;
542 pcm1796_registers_init(chip);
543 xonar_hdmi_resume(chip, &data->hdmi);
544 xonar_enable_output(chip);
547 static void xonar_stx_resume(struct oxygen *chip)
549 pcm1796_registers_init(chip);
550 xonar_enable_output(chip);
553 static void xonar_st_resume(struct oxygen *chip)
555 cs2000_registers_init(chip);
556 xonar_stx_resume(chip);
559 static void update_pcm1796_oversampling(struct oxygen *chip)
561 struct xonar_pcm179x *data = chip->model_data;
562 unsigned int i;
563 u8 reg;
565 if (data->current_rate <= 48000 && !data->h6)
566 reg = PCM1796_OS_128;
567 else
568 reg = PCM1796_OS_64;
569 for (i = 0; i < data->dacs; ++i)
570 pcm1796_write_cached(chip, i, 20, reg);
573 static void set_pcm1796_params(struct oxygen *chip,
574 struct snd_pcm_hw_params *params)
576 struct xonar_pcm179x *data = chip->model_data;
578 msleep(1);
579 data->current_rate = params_rate(params);
580 update_pcm1796_oversampling(chip);
583 static void update_pcm1796_volume(struct oxygen *chip)
585 struct xonar_pcm179x *data = chip->model_data;
586 unsigned int i;
587 s8 gain_offset;
589 gain_offset = data->hp_active ? data->hp_gain_offset : 0;
590 for (i = 0; i < data->dacs; ++i) {
591 pcm1796_write_cached(chip, i, 16, chip->dac_volume[i * 2]
592 + gain_offset);
593 pcm1796_write_cached(chip, i, 17, chip->dac_volume[i * 2 + 1]
594 + gain_offset);
595 gain_offset = 0;
599 static void update_pcm1796_mute(struct oxygen *chip)
601 struct xonar_pcm179x *data = chip->model_data;
602 unsigned int i;
603 u8 value;
605 value = PCM1796_DMF_DISABLED | PCM1796_FMT_24_I2S | PCM1796_ATLD;
606 if (chip->dac_mute)
607 value |= PCM1796_MUTE;
608 for (i = 0; i < data->dacs; ++i)
609 pcm1796_write_cached(chip, i, 18, value);
612 static void update_cs2000_rate(struct oxygen *chip, unsigned int rate)
614 struct xonar_pcm179x *data = chip->model_data;
615 u8 rate_mclk, reg;
617 switch (rate) {
618 case 32000:
619 case 64000:
620 rate_mclk = OXYGEN_RATE_32000;
621 break;
622 case 44100:
623 case 88200:
624 case 176400:
625 rate_mclk = OXYGEN_RATE_44100;
626 break;
627 default:
628 case 48000:
629 case 96000:
630 case 192000:
631 rate_mclk = OXYGEN_RATE_48000;
632 break;
635 if (rate <= 96000 && (rate > 48000 || data->h6)) {
636 rate_mclk |= OXYGEN_I2S_MCLK(MCLK_256);
637 reg = CS2000_REF_CLK_DIV_1;
638 } else {
639 rate_mclk |= OXYGEN_I2S_MCLK(MCLK_512);
640 reg = CS2000_REF_CLK_DIV_2;
643 oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT, rate_mclk,
644 OXYGEN_I2S_RATE_MASK | OXYGEN_I2S_MCLK_MASK);
645 cs2000_write_cached(chip, CS2000_FUN_CFG_1, reg);
646 msleep(3); /* PLL lock delay */
649 static void set_st_params(struct oxygen *chip,
650 struct snd_pcm_hw_params *params)
652 update_cs2000_rate(chip, params_rate(params));
653 set_pcm1796_params(chip, params);
656 static void set_hdav_params(struct oxygen *chip,
657 struct snd_pcm_hw_params *params)
659 struct xonar_hdav *data = chip->model_data;
661 set_pcm1796_params(chip, params);
662 xonar_set_hdmi_params(chip, &data->hdmi, params);
665 static const struct snd_kcontrol_new alt_switch = {
666 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
667 .name = "Analog Loopback Switch",
668 .info = snd_ctl_boolean_mono_info,
669 .get = xonar_gpio_bit_switch_get,
670 .put = xonar_gpio_bit_switch_put,
671 .private_value = GPIO_D2_ALT,
674 static int rolloff_info(struct snd_kcontrol *ctl,
675 struct snd_ctl_elem_info *info)
677 static const char *const names[2] = {
678 "Sharp Roll-off", "Slow Roll-off"
681 return snd_ctl_enum_info(info, 1, 2, names);
684 static int rolloff_get(struct snd_kcontrol *ctl,
685 struct snd_ctl_elem_value *value)
687 struct oxygen *chip = ctl->private_data;
688 struct xonar_pcm179x *data = chip->model_data;
690 value->value.enumerated.item[0] =
691 (data->pcm1796_regs[0][19 - PCM1796_REG_BASE] &
692 PCM1796_FLT_MASK) != PCM1796_FLT_SHARP;
693 return 0;
696 static int rolloff_put(struct snd_kcontrol *ctl,
697 struct snd_ctl_elem_value *value)
699 struct oxygen *chip = ctl->private_data;
700 struct xonar_pcm179x *data = chip->model_data;
701 unsigned int i;
702 int changed;
703 u8 reg;
705 mutex_lock(&chip->mutex);
706 reg = data->pcm1796_regs[0][19 - PCM1796_REG_BASE];
707 reg &= ~PCM1796_FLT_MASK;
708 if (!value->value.enumerated.item[0])
709 reg |= PCM1796_FLT_SHARP;
710 else
711 reg |= PCM1796_FLT_SLOW;
712 changed = reg != data->pcm1796_regs[0][19 - PCM1796_REG_BASE];
713 if (changed) {
714 for (i = 0; i < data->dacs; ++i)
715 pcm1796_write(chip, i, 19, reg);
717 mutex_unlock(&chip->mutex);
718 return changed;
721 static const struct snd_kcontrol_new rolloff_control = {
722 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
723 .name = "DAC Filter Playback Enum",
724 .info = rolloff_info,
725 .get = rolloff_get,
726 .put = rolloff_put,
729 static const struct snd_kcontrol_new hdav_hdmi_control = {
730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
731 .name = "HDMI Playback Switch",
732 .info = snd_ctl_boolean_mono_info,
733 .get = xonar_gpio_bit_switch_get,
734 .put = xonar_gpio_bit_switch_put,
735 .private_value = GPIO_HDAV_OUTPUT_ENABLE | XONAR_GPIO_BIT_INVERT,
738 static int st_output_switch_info(struct snd_kcontrol *ctl,
739 struct snd_ctl_elem_info *info)
741 static const char *const names[3] = {
742 "Speakers", "Headphones", "FP Headphones"
745 return snd_ctl_enum_info(info, 1, 3, names);
748 static int st_output_switch_get(struct snd_kcontrol *ctl,
749 struct snd_ctl_elem_value *value)
751 struct oxygen *chip = ctl->private_data;
752 u16 gpio;
754 gpio = oxygen_read16(chip, OXYGEN_GPIO_DATA);
755 if (!(gpio & GPIO_ST_HP))
756 value->value.enumerated.item[0] = 0;
757 else if (gpio & GPIO_ST_HP_REAR)
758 value->value.enumerated.item[0] = 1;
759 else
760 value->value.enumerated.item[0] = 2;
761 return 0;
765 static int st_output_switch_put(struct snd_kcontrol *ctl,
766 struct snd_ctl_elem_value *value)
768 struct oxygen *chip = ctl->private_data;
769 struct xonar_pcm179x *data = chip->model_data;
770 u16 gpio_old, gpio;
772 mutex_lock(&chip->mutex);
773 gpio_old = oxygen_read16(chip, OXYGEN_GPIO_DATA);
774 gpio = gpio_old;
775 switch (value->value.enumerated.item[0]) {
776 case 0:
777 gpio &= ~(GPIO_ST_HP | GPIO_ST_HP_REAR);
778 break;
779 case 1:
780 gpio |= GPIO_ST_HP | GPIO_ST_HP_REAR;
781 break;
782 case 2:
783 gpio = (gpio | GPIO_ST_HP) & ~GPIO_ST_HP_REAR;
784 break;
786 oxygen_write16(chip, OXYGEN_GPIO_DATA, gpio);
787 data->hp_active = gpio & GPIO_ST_HP;
788 update_pcm1796_volume(chip);
789 mutex_unlock(&chip->mutex);
790 return gpio != gpio_old;
793 static int st_hp_volume_offset_info(struct snd_kcontrol *ctl,
794 struct snd_ctl_elem_info *info)
796 static const char *const names[3] = {
797 "< 64 ohms", "64-300 ohms", "300-600 ohms"
800 return snd_ctl_enum_info(info, 1, 3, names);
803 static int st_hp_volume_offset_get(struct snd_kcontrol *ctl,
804 struct snd_ctl_elem_value *value)
806 struct oxygen *chip = ctl->private_data;
807 struct xonar_pcm179x *data = chip->model_data;
809 mutex_lock(&chip->mutex);
810 if (data->hp_gain_offset < 2*-6)
811 value->value.enumerated.item[0] = 0;
812 else if (data->hp_gain_offset < 0)
813 value->value.enumerated.item[0] = 1;
814 else
815 value->value.enumerated.item[0] = 2;
816 mutex_unlock(&chip->mutex);
817 return 0;
821 static int st_hp_volume_offset_put(struct snd_kcontrol *ctl,
822 struct snd_ctl_elem_value *value)
824 static const s8 offsets[] = { 2*-18, 2*-6, 0 };
825 struct oxygen *chip = ctl->private_data;
826 struct xonar_pcm179x *data = chip->model_data;
827 s8 offset;
828 int changed;
830 if (value->value.enumerated.item[0] > 2)
831 return -EINVAL;
832 offset = offsets[value->value.enumerated.item[0]];
833 mutex_lock(&chip->mutex);
834 changed = offset != data->hp_gain_offset;
835 if (changed) {
836 data->hp_gain_offset = offset;
837 update_pcm1796_volume(chip);
839 mutex_unlock(&chip->mutex);
840 return changed;
843 static const struct snd_kcontrol_new st_controls[] = {
845 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
846 .name = "Analog Output",
847 .info = st_output_switch_info,
848 .get = st_output_switch_get,
849 .put = st_output_switch_put,
852 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
853 .name = "Headphones Impedance Playback Enum",
854 .info = st_hp_volume_offset_info,
855 .get = st_hp_volume_offset_get,
856 .put = st_hp_volume_offset_put,
860 static void xonar_line_mic_ac97_switch(struct oxygen *chip,
861 unsigned int reg, unsigned int mute)
863 if (reg == AC97_LINE) {
864 spin_lock_irq(&chip->reg_lock);
865 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
866 mute ? GPIO_INPUT_ROUTE : 0,
867 GPIO_INPUT_ROUTE);
868 spin_unlock_irq(&chip->reg_lock);
872 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -6000, 50, 0);
874 static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
876 if (!strncmp(template->name, "CD Capture ", 11))
877 /* CD in is actually connected to the video in pin */
878 template->private_value ^= AC97_CD ^ AC97_VIDEO;
879 return 0;
882 static int xonar_st_h6_control_filter(struct snd_kcontrol_new *template)
884 if (!strncmp(template->name, "Master Playback ", 16))
885 /* no volume/mute, as I²C to the third DAC does not work */
886 return 1;
887 return 0;
890 static int add_pcm1796_controls(struct oxygen *chip)
892 struct xonar_pcm179x *data = chip->model_data;
893 int err;
895 if (!data->broken_i2c) {
896 err = snd_ctl_add(chip->card,
897 snd_ctl_new1(&rolloff_control, chip));
898 if (err < 0)
899 return err;
901 return 0;
904 static int xonar_d2_mixer_init(struct oxygen *chip)
906 int err;
908 err = snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
909 if (err < 0)
910 return err;
911 err = add_pcm1796_controls(chip);
912 if (err < 0)
913 return err;
914 return 0;
917 static int xonar_hdav_mixer_init(struct oxygen *chip)
919 int err;
921 err = snd_ctl_add(chip->card, snd_ctl_new1(&hdav_hdmi_control, chip));
922 if (err < 0)
923 return err;
924 err = add_pcm1796_controls(chip);
925 if (err < 0)
926 return err;
927 return 0;
930 static int xonar_st_mixer_init(struct oxygen *chip)
932 unsigned int i;
933 int err;
935 for (i = 0; i < ARRAY_SIZE(st_controls); ++i) {
936 err = snd_ctl_add(chip->card,
937 snd_ctl_new1(&st_controls[i], chip));
938 if (err < 0)
939 return err;
941 err = add_pcm1796_controls(chip);
942 if (err < 0)
943 return err;
944 return 0;
947 static void dump_pcm1796_registers(struct oxygen *chip,
948 struct snd_info_buffer *buffer)
950 struct xonar_pcm179x *data = chip->model_data;
951 unsigned int dac, i;
953 for (dac = 0; dac < data->dacs; ++dac) {
954 snd_iprintf(buffer, "\nPCM1796 %u:", dac + 1);
955 for (i = 0; i < 5; ++i)
956 snd_iprintf(buffer, " %02x",
957 data->pcm1796_regs[dac][i]);
959 snd_iprintf(buffer, "\n");
962 static void dump_cs2000_registers(struct oxygen *chip,
963 struct snd_info_buffer *buffer)
965 struct xonar_pcm179x *data = chip->model_data;
966 unsigned int i;
968 if (data->has_cs2000) {
969 snd_iprintf(buffer, "\nCS2000:\n00: ");
970 for (i = 1; i < 0x10; ++i)
971 snd_iprintf(buffer, " %02x", data->cs2000_regs[i]);
972 snd_iprintf(buffer, "\n10:");
973 for (i = 0x10; i < 0x1f; ++i)
974 snd_iprintf(buffer, " %02x", data->cs2000_regs[i]);
975 snd_iprintf(buffer, "\n");
979 static void dump_st_registers(struct oxygen *chip,
980 struct snd_info_buffer *buffer)
982 dump_pcm1796_registers(chip, buffer);
983 dump_cs2000_registers(chip, buffer);
986 static const struct oxygen_model model_xonar_d2 = {
987 .longname = "Asus Virtuoso 200",
988 .chip = "AV200",
989 .init = xonar_d2_init,
990 .control_filter = xonar_d2_control_filter,
991 .mixer_init = xonar_d2_mixer_init,
992 .cleanup = xonar_d2_cleanup,
993 .suspend = xonar_d2_suspend,
994 .resume = xonar_d2_resume,
995 .set_dac_params = set_pcm1796_params,
996 .set_adc_params = xonar_set_cs53x1_params,
997 .update_dac_volume = update_pcm1796_volume,
998 .update_dac_mute = update_pcm1796_mute,
999 .dump_registers = dump_pcm1796_registers,
1000 .dac_tlv = pcm1796_db_scale,
1001 .model_data_size = sizeof(struct xonar_pcm179x),
1002 .device_config = PLAYBACK_0_TO_I2S |
1003 PLAYBACK_1_TO_SPDIF |
1004 CAPTURE_0_FROM_I2S_2 |
1005 CAPTURE_1_FROM_SPDIF |
1006 MIDI_OUTPUT |
1007 MIDI_INPUT |
1008 AC97_CD_INPUT,
1009 .dac_channels_pcm = 8,
1010 .dac_channels_mixer = 8,
1011 .dac_volume_min = 255 - 2*60,
1012 .dac_volume_max = 255,
1013 .misc_flags = OXYGEN_MISC_MIDI,
1014 .function_flags = OXYGEN_FUNCTION_SPI |
1015 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
1016 .dac_mclks = OXYGEN_MCLKS(512, 128, 128),
1017 .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
1018 .dac_i2s_format = OXYGEN_I2S_FORMAT_I2S,
1019 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1022 static const struct oxygen_model model_xonar_hdav = {
1023 .longname = "Asus Virtuoso 200",
1024 .chip = "AV200",
1025 .init = xonar_hdav_init,
1026 .mixer_init = xonar_hdav_mixer_init,
1027 .cleanup = xonar_hdav_cleanup,
1028 .suspend = xonar_hdav_suspend,
1029 .resume = xonar_hdav_resume,
1030 .pcm_hardware_filter = xonar_hdmi_pcm_hardware_filter,
1031 .set_dac_params = set_hdav_params,
1032 .set_adc_params = xonar_set_cs53x1_params,
1033 .update_dac_volume = update_pcm1796_volume,
1034 .update_dac_mute = update_pcm1796_mute,
1035 .uart_input = xonar_hdmi_uart_input,
1036 .ac97_switch = xonar_line_mic_ac97_switch,
1037 .dump_registers = dump_pcm1796_registers,
1038 .dac_tlv = pcm1796_db_scale,
1039 .model_data_size = sizeof(struct xonar_hdav),
1040 .device_config = PLAYBACK_0_TO_I2S |
1041 PLAYBACK_1_TO_SPDIF |
1042 CAPTURE_0_FROM_I2S_2 |
1043 CAPTURE_1_FROM_SPDIF,
1044 .dac_channels_pcm = 8,
1045 .dac_channels_mixer = 2,
1046 .dac_volume_min = 255 - 2*60,
1047 .dac_volume_max = 255,
1048 .misc_flags = OXYGEN_MISC_MIDI,
1049 .function_flags = OXYGEN_FUNCTION_2WIRE,
1050 .dac_mclks = OXYGEN_MCLKS(512, 128, 128),
1051 .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
1052 .dac_i2s_format = OXYGEN_I2S_FORMAT_I2S,
1053 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1056 static const struct oxygen_model model_xonar_st = {
1057 .longname = "Asus Virtuoso 100",
1058 .chip = "AV200",
1059 .init = xonar_st_init,
1060 .mixer_init = xonar_st_mixer_init,
1061 .cleanup = xonar_st_cleanup,
1062 .suspend = xonar_st_suspend,
1063 .resume = xonar_st_resume,
1064 .set_dac_params = set_st_params,
1065 .set_adc_params = xonar_set_cs53x1_params,
1066 .update_dac_volume = update_pcm1796_volume,
1067 .update_dac_mute = update_pcm1796_mute,
1068 .ac97_switch = xonar_line_mic_ac97_switch,
1069 .dump_registers = dump_st_registers,
1070 .dac_tlv = pcm1796_db_scale,
1071 .model_data_size = sizeof(struct xonar_pcm179x),
1072 .device_config = PLAYBACK_0_TO_I2S |
1073 PLAYBACK_1_TO_SPDIF |
1074 CAPTURE_0_FROM_I2S_2 |
1075 AC97_FMIC_SWITCH,
1076 .dac_channels_pcm = 2,
1077 .dac_channels_mixer = 2,
1078 .dac_volume_min = 255 - 2*60,
1079 .dac_volume_max = 255,
1080 .function_flags = OXYGEN_FUNCTION_2WIRE,
1081 .dac_mclks = OXYGEN_MCLKS(512, 128, 128),
1082 .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
1083 .dac_i2s_format = OXYGEN_I2S_FORMAT_I2S,
1084 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1087 int __devinit get_xonar_pcm179x_model(struct oxygen *chip,
1088 const struct pci_device_id *id)
1090 switch (id->subdevice) {
1091 case 0x8269:
1092 chip->model = model_xonar_d2;
1093 chip->model.shortname = "Xonar D2";
1094 break;
1095 case 0x82b7:
1096 chip->model = model_xonar_d2;
1097 chip->model.shortname = "Xonar D2X";
1098 chip->model.init = xonar_d2x_init;
1099 break;
1100 case 0x8314:
1101 chip->model = model_xonar_hdav;
1102 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
1103 switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
1104 default:
1105 chip->model.shortname = "Xonar HDAV1.3";
1106 break;
1107 case GPIO_DB_H6:
1108 chip->model.shortname = "Xonar HDAV1.3+H6";
1109 chip->model.dac_channels_mixer = 8;
1110 chip->model.dac_mclks = OXYGEN_MCLKS(256, 128, 128);
1111 break;
1113 break;
1114 case 0x835d:
1115 chip->model = model_xonar_st;
1116 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
1117 switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
1118 default:
1119 chip->model.shortname = "Xonar ST";
1120 break;
1121 case GPIO_DB_H6:
1122 chip->model.shortname = "Xonar ST+H6";
1123 chip->model.control_filter = xonar_st_h6_control_filter;
1124 chip->model.dac_channels_pcm = 8;
1125 chip->model.dac_channels_mixer = 8;
1126 chip->model.dac_mclks = OXYGEN_MCLKS(256, 128, 128);
1127 break;
1129 break;
1130 case 0x835c:
1131 chip->model = model_xonar_st;
1132 chip->model.shortname = "Xonar STX";
1133 chip->model.init = xonar_stx_init;
1134 chip->model.resume = xonar_stx_resume;
1135 chip->model.set_dac_params = set_pcm1796_params;
1136 break;
1137 default:
1138 return -EINVAL;
1140 return 0;