ALSA: virtuoso: fix Xonar STX anti-pop delay
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / pci / oxygen / xonar_pcm179x.c
blobd491fd6c0be239906380ae6af8864f1bfe0e4f6f
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 * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
42 * Xonar HDAV1.3 (Deluxe)
43 * ----------------------
45 * CMI8788:
47 * I²C <-> PCM1796 (front)
49 * GPI 0 <- external power present
51 * GPIO 0 -> enable output to speakers
52 * GPIO 2 -> M0 of CS5381
53 * GPIO 3 -> M1 of CS5381
54 * GPIO 8 -> route input jack to line-in (0) or mic-in (1)
56 * TXD -> HDMI controller
57 * RXD <- HDMI controller
59 * PCM1796 front: AD1,0 <- 0,0
61 * CM9780:
63 * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
65 * no daughterboard
66 * ----------------
68 * GPIO 4 <- 1
70 * H6 daughterboard
71 * ----------------
73 * GPIO 4 <- 0
74 * GPIO 5 <- 0
76 * I²C <-> PCM1796 (surround)
77 * <-> PCM1796 (center/LFE)
78 * <-> PCM1796 (back)
80 * PCM1796 surround: AD1,0 <- 0,1
81 * PCM1796 center/LFE: AD1,0 <- 1,0
82 * PCM1796 back: AD1,0 <- 1,1
84 * unknown daughterboard
85 * ---------------------
87 * GPIO 4 <- 0
88 * GPIO 5 <- 1
90 * I²C <-> CS4362A (surround, center/LFE, back)
92 * CS4362A: AD0 <- 0
96 * Xonar Essence ST (Deluxe)/STX
97 * -----------------------------
99 * CMI8788:
101 * I²C <-> PCM1792A
102 * <-> CS2000 (ST only)
104 * ADC1 MCLK -> REF_CLK of CS2000 (ST only)
106 * GPI 0 <- external power present (STX only)
108 * GPIO 0 -> enable output to speakers
109 * GPIO 1 -> route HP to front panel (0) or rear jack (1)
110 * GPIO 2 -> M0 of CS5381
111 * GPIO 3 -> M1 of CS5381
112 * GPIO 7 -> route output to speaker jacks (0) or HP (1)
113 * GPIO 8 -> route input jack to line-in (0) or mic-in (1)
115 * PCM1792A:
117 * AD1,0 <- 0,0
118 * SCK <- CLK_OUT of CS2000 (ST only)
120 * CS2000:
122 * AD0 <- 0
124 * CM9780:
126 * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
128 * H6 daughterboard
129 * ----------------
131 * GPIO 4 <- 0
132 * GPIO 5 <- 0
136 * Xonar HDAV1.3 Slim
137 * ------------------
139 * CMI8788:
141 * GPIO 1 -> enable output
143 * TXD -> HDMI controller
144 * RXD <- HDMI controller
147 #include <linux/pci.h>
148 #include <linux/delay.h>
149 #include <linux/mutex.h>
150 #include <sound/ac97_codec.h>
151 #include <sound/control.h>
152 #include <sound/core.h>
153 #include <sound/pcm.h>
154 #include <sound/pcm_params.h>
155 #include <sound/tlv.h>
156 #include "xonar.h"
157 #include "cm9780.h"
158 #include "pcm1796.h"
159 #include "cs2000.h"
162 #define GPIO_D2X_EXT_POWER 0x0020
163 #define GPIO_D2_ALT 0x0080
164 #define GPIO_D2_OUTPUT_ENABLE 0x0100
166 #define GPI_EXT_POWER 0x01
167 #define GPIO_INPUT_ROUTE 0x0100
169 #define GPIO_HDAV_OUTPUT_ENABLE 0x0001
171 #define GPIO_DB_MASK 0x0030
172 #define GPIO_DB_H6 0x0000
174 #define GPIO_ST_OUTPUT_ENABLE 0x0001
175 #define GPIO_ST_HP_REAR 0x0002
176 #define GPIO_ST_HP 0x0080
178 #define I2C_DEVICE_PCM1796(i) (0x98 + ((i) << 1)) /* 10011, ii, /W=0 */
179 #define I2C_DEVICE_CS2000 0x9c /* 100111, 0, /W=0 */
181 #define PCM1796_REG_BASE 16
184 struct xonar_pcm179x {
185 struct xonar_generic generic;
186 unsigned int dacs;
187 u8 pcm1796_regs[4][5];
188 unsigned int current_rate;
189 bool os_128;
190 bool hp_active;
191 s8 hp_gain_offset;
192 bool has_cs2000;
193 u8 cs2000_fun_cfg_1;
196 struct xonar_hdav {
197 struct xonar_pcm179x pcm179x;
198 struct xonar_hdmi hdmi;
202 static inline void pcm1796_write_spi(struct oxygen *chip, unsigned int codec,
203 u8 reg, u8 value)
205 /* maps ALSA channel pair number to SPI output */
206 static const u8 codec_map[4] = {
207 0, 1, 2, 4
209 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
210 OXYGEN_SPI_DATA_LENGTH_2 |
211 OXYGEN_SPI_CLOCK_160 |
212 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
213 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
214 (reg << 8) | value);
217 static inline void pcm1796_write_i2c(struct oxygen *chip, unsigned int codec,
218 u8 reg, u8 value)
220 oxygen_write_i2c(chip, I2C_DEVICE_PCM1796(codec), reg, value);
223 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
224 u8 reg, u8 value)
226 struct xonar_pcm179x *data = chip->model_data;
228 if ((chip->model.function_flags & OXYGEN_FUNCTION_2WIRE_SPI_MASK) ==
229 OXYGEN_FUNCTION_SPI)
230 pcm1796_write_spi(chip, codec, reg, value);
231 else
232 pcm1796_write_i2c(chip, codec, reg, value);
233 if ((unsigned int)(reg - PCM1796_REG_BASE)
234 < ARRAY_SIZE(data->pcm1796_regs[codec]))
235 data->pcm1796_regs[codec][reg - PCM1796_REG_BASE] = value;
238 static void pcm1796_write_cached(struct oxygen *chip, unsigned int codec,
239 u8 reg, u8 value)
241 struct xonar_pcm179x *data = chip->model_data;
243 if (value != data->pcm1796_regs[codec][reg - PCM1796_REG_BASE])
244 pcm1796_write(chip, codec, reg, value);
247 static void cs2000_write(struct oxygen *chip, u8 reg, u8 value)
249 struct xonar_pcm179x *data = chip->model_data;
251 oxygen_write_i2c(chip, I2C_DEVICE_CS2000, reg, value);
252 if (reg == CS2000_FUN_CFG_1)
253 data->cs2000_fun_cfg_1 = value;
256 static void cs2000_write_cached(struct oxygen *chip, u8 reg, u8 value)
258 struct xonar_pcm179x *data = chip->model_data;
260 if (reg != CS2000_FUN_CFG_1 ||
261 value != data->cs2000_fun_cfg_1)
262 cs2000_write(chip, reg, value);
265 static void pcm1796_registers_init(struct oxygen *chip)
267 struct xonar_pcm179x *data = chip->model_data;
268 unsigned int i;
269 s8 gain_offset;
271 gain_offset = data->hp_active ? data->hp_gain_offset : 0;
272 for (i = 0; i < data->dacs; ++i) {
273 /* set ATLD before ATL/ATR */
274 pcm1796_write(chip, i, 18,
275 data->pcm1796_regs[0][18 - PCM1796_REG_BASE]);
276 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]
277 + gain_offset);
278 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]
279 + gain_offset);
280 pcm1796_write(chip, i, 19,
281 data->pcm1796_regs[0][19 - PCM1796_REG_BASE]);
282 pcm1796_write(chip, i, 20,
283 data->pcm1796_regs[0][20 - PCM1796_REG_BASE]);
284 pcm1796_write(chip, i, 21, 0);
288 static void pcm1796_init(struct oxygen *chip)
290 struct xonar_pcm179x *data = chip->model_data;
292 data->pcm1796_regs[0][18 - PCM1796_REG_BASE] = PCM1796_MUTE |
293 PCM1796_DMF_DISABLED | PCM1796_FMT_24_LJUST | PCM1796_ATLD;
294 data->pcm1796_regs[0][19 - PCM1796_REG_BASE] =
295 PCM1796_FLT_SHARP | PCM1796_ATS_1;
296 data->pcm1796_regs[0][20 - PCM1796_REG_BASE] = PCM1796_OS_64;
297 pcm1796_registers_init(chip);
298 data->current_rate = 48000;
301 static void xonar_d2_init(struct oxygen *chip)
303 struct xonar_pcm179x *data = chip->model_data;
305 data->generic.anti_pop_delay = 300;
306 data->generic.output_enable_bit = GPIO_D2_OUTPUT_ENABLE;
307 data->dacs = 4;
309 pcm1796_init(chip);
311 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT);
312 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT);
314 oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
316 xonar_init_cs53x1(chip);
317 xonar_enable_output(chip);
319 snd_component_add(chip->card, "PCM1796");
320 snd_component_add(chip->card, "CS5381");
323 static void xonar_d2x_init(struct oxygen *chip)
325 struct xonar_pcm179x *data = chip->model_data;
327 data->generic.ext_power_reg = OXYGEN_GPIO_DATA;
328 data->generic.ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK;
329 data->generic.ext_power_bit = GPIO_D2X_EXT_POWER;
330 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2X_EXT_POWER);
331 xonar_init_ext_power(chip);
332 xonar_d2_init(chip);
335 static void xonar_hdav_init(struct oxygen *chip)
337 struct xonar_hdav *data = chip->model_data;
339 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
340 OXYGEN_2WIRE_LENGTH_8 |
341 OXYGEN_2WIRE_INTERRUPT_MASK |
342 OXYGEN_2WIRE_SPEED_FAST);
344 data->pcm179x.generic.anti_pop_delay = 100;
345 data->pcm179x.generic.output_enable_bit = GPIO_HDAV_OUTPUT_ENABLE;
346 data->pcm179x.generic.ext_power_reg = OXYGEN_GPI_DATA;
347 data->pcm179x.generic.ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
348 data->pcm179x.generic.ext_power_bit = GPI_EXT_POWER;
349 data->pcm179x.dacs = chip->model.private_data ? 4 : 1;
351 pcm1796_init(chip);
353 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_INPUT_ROUTE);
354 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_INPUT_ROUTE);
356 xonar_init_cs53x1(chip);
357 xonar_init_ext_power(chip);
358 xonar_hdmi_init(chip, &data->hdmi);
359 xonar_enable_output(chip);
361 snd_component_add(chip->card, "PCM1796");
362 snd_component_add(chip->card, "CS5381");
365 static void xonar_st_init_i2c(struct oxygen *chip)
367 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
368 OXYGEN_2WIRE_LENGTH_8 |
369 OXYGEN_2WIRE_INTERRUPT_MASK |
370 OXYGEN_2WIRE_SPEED_FAST);
373 static void xonar_st_init_common(struct oxygen *chip)
375 struct xonar_pcm179x *data = chip->model_data;
377 data->generic.output_enable_bit = GPIO_ST_OUTPUT_ENABLE;
378 data->dacs = chip->model.private_data ? 4 : 1;
379 data->hp_gain_offset = 2*-18;
381 pcm1796_init(chip);
383 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
384 GPIO_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP);
385 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
386 GPIO_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP);
388 xonar_init_cs53x1(chip);
389 xonar_enable_output(chip);
391 snd_component_add(chip->card, "PCM1792A");
392 snd_component_add(chip->card, "CS5381");
395 static void cs2000_registers_init(struct oxygen *chip)
397 struct xonar_pcm179x *data = chip->model_data;
399 cs2000_write(chip, CS2000_GLOBAL_CFG, CS2000_FREEZE);
400 cs2000_write(chip, CS2000_DEV_CTRL, 0);
401 cs2000_write(chip, CS2000_DEV_CFG_1,
402 CS2000_R_MOD_SEL_1 |
403 (0 << CS2000_R_SEL_SHIFT) |
404 CS2000_AUX_OUT_SRC_REF_CLK |
405 CS2000_EN_DEV_CFG_1);
406 cs2000_write(chip, CS2000_DEV_CFG_2,
407 (0 << CS2000_LOCK_CLK_SHIFT) |
408 CS2000_FRAC_N_SRC_STATIC);
409 cs2000_write(chip, CS2000_RATIO_0 + 0, 0x00); /* 1.0 */
410 cs2000_write(chip, CS2000_RATIO_0 + 1, 0x10);
411 cs2000_write(chip, CS2000_RATIO_0 + 2, 0x00);
412 cs2000_write(chip, CS2000_RATIO_0 + 3, 0x00);
413 cs2000_write(chip, CS2000_FUN_CFG_1, data->cs2000_fun_cfg_1);
414 cs2000_write(chip, CS2000_FUN_CFG_2, 0);
415 cs2000_write(chip, CS2000_GLOBAL_CFG, CS2000_EN_DEV_CFG_2);
418 static void xonar_st_init(struct oxygen *chip)
420 struct xonar_pcm179x *data = chip->model_data;
422 data->generic.anti_pop_delay = 100;
423 data->has_cs2000 = 1;
424 data->cs2000_fun_cfg_1 = CS2000_REF_CLK_DIV_1;
426 oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
427 OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_I2S |
428 OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 |
429 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
431 xonar_st_init_i2c(chip);
432 cs2000_registers_init(chip);
433 xonar_st_init_common(chip);
435 snd_component_add(chip->card, "CS2000");
438 static void xonar_stx_init(struct oxygen *chip)
440 struct xonar_pcm179x *data = chip->model_data;
442 xonar_st_init_i2c(chip);
443 data->generic.anti_pop_delay = 800;
444 data->generic.ext_power_reg = OXYGEN_GPI_DATA;
445 data->generic.ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
446 data->generic.ext_power_bit = GPI_EXT_POWER;
447 xonar_init_ext_power(chip);
448 xonar_st_init_common(chip);
451 static void xonar_d2_cleanup(struct oxygen *chip)
453 xonar_disable_output(chip);
456 static void xonar_hdav_cleanup(struct oxygen *chip)
458 xonar_hdmi_cleanup(chip);
459 xonar_disable_output(chip);
460 msleep(2);
463 static void xonar_st_cleanup(struct oxygen *chip)
465 xonar_disable_output(chip);
468 static void xonar_d2_suspend(struct oxygen *chip)
470 xonar_d2_cleanup(chip);
473 static void xonar_hdav_suspend(struct oxygen *chip)
475 xonar_hdav_cleanup(chip);
478 static void xonar_st_suspend(struct oxygen *chip)
480 xonar_st_cleanup(chip);
483 static void xonar_d2_resume(struct oxygen *chip)
485 pcm1796_registers_init(chip);
486 xonar_enable_output(chip);
489 static void xonar_hdav_resume(struct oxygen *chip)
491 struct xonar_hdav *data = chip->model_data;
493 pcm1796_registers_init(chip);
494 xonar_hdmi_resume(chip, &data->hdmi);
495 xonar_enable_output(chip);
498 static void xonar_stx_resume(struct oxygen *chip)
500 pcm1796_registers_init(chip);
501 xonar_enable_output(chip);
504 static void xonar_st_resume(struct oxygen *chip)
506 cs2000_registers_init(chip);
507 xonar_stx_resume(chip);
510 static unsigned int mclk_from_rate(struct oxygen *chip, unsigned int rate)
512 struct xonar_pcm179x *data = chip->model_data;
514 if (rate <= 32000)
515 return OXYGEN_I2S_MCLK_512;
516 else if (rate <= 48000 && data->os_128)
517 return OXYGEN_I2S_MCLK_512;
518 else if (rate <= 96000)
519 return OXYGEN_I2S_MCLK_256;
520 else
521 return OXYGEN_I2S_MCLK_128;
524 static unsigned int get_pcm1796_i2s_mclk(struct oxygen *chip,
525 unsigned int channel,
526 struct snd_pcm_hw_params *params)
528 if (channel == PCM_MULTICH)
529 return mclk_from_rate(chip, params_rate(params));
530 else
531 return oxygen_default_i2s_mclk(chip, channel, params);
534 static void update_pcm1796_oversampling(struct oxygen *chip)
536 struct xonar_pcm179x *data = chip->model_data;
537 unsigned int i;
538 u8 reg;
540 if (data->current_rate <= 32000)
541 reg = PCM1796_OS_128;
542 else if (data->current_rate <= 48000 && data->os_128)
543 reg = PCM1796_OS_128;
544 else if (data->current_rate <= 96000 || data->os_128)
545 reg = PCM1796_OS_64;
546 else
547 reg = PCM1796_OS_32;
548 for (i = 0; i < data->dacs; ++i)
549 pcm1796_write_cached(chip, i, 20, reg);
552 static void set_pcm1796_params(struct oxygen *chip,
553 struct snd_pcm_hw_params *params)
555 struct xonar_pcm179x *data = chip->model_data;
557 data->current_rate = params_rate(params);
558 update_pcm1796_oversampling(chip);
561 static void update_pcm1796_volume(struct oxygen *chip)
563 struct xonar_pcm179x *data = chip->model_data;
564 unsigned int i;
565 s8 gain_offset;
567 gain_offset = data->hp_active ? data->hp_gain_offset : 0;
568 for (i = 0; i < data->dacs; ++i) {
569 pcm1796_write_cached(chip, i, 16, chip->dac_volume[i * 2]
570 + gain_offset);
571 pcm1796_write_cached(chip, i, 17, chip->dac_volume[i * 2 + 1]
572 + gain_offset);
576 static void update_pcm1796_mute(struct oxygen *chip)
578 struct xonar_pcm179x *data = chip->model_data;
579 unsigned int i;
580 u8 value;
582 value = PCM1796_DMF_DISABLED | PCM1796_FMT_24_LJUST | PCM1796_ATLD;
583 if (chip->dac_mute)
584 value |= PCM1796_MUTE;
585 for (i = 0; i < data->dacs; ++i)
586 pcm1796_write_cached(chip, i, 18, value);
589 static void update_cs2000_rate(struct oxygen *chip, unsigned int rate)
591 struct xonar_pcm179x *data = chip->model_data;
592 u8 rate_mclk, reg;
594 switch (rate) {
595 /* XXX Why is the I2S A MCLK half the actual I2S MCLK? */
596 case 32000:
597 rate_mclk = OXYGEN_RATE_32000 | OXYGEN_I2S_MCLK_256;
598 break;
599 case 44100:
600 if (data->os_128)
601 rate_mclk = OXYGEN_RATE_44100 | OXYGEN_I2S_MCLK_256;
602 else
603 rate_mclk = OXYGEN_RATE_44100 | OXYGEN_I2S_MCLK_128;
604 break;
605 default: /* 48000 */
606 if (data->os_128)
607 rate_mclk = OXYGEN_RATE_48000 | OXYGEN_I2S_MCLK_256;
608 else
609 rate_mclk = OXYGEN_RATE_48000 | OXYGEN_I2S_MCLK_128;
610 break;
611 case 64000:
612 rate_mclk = OXYGEN_RATE_32000 | OXYGEN_I2S_MCLK_256;
613 break;
614 case 88200:
615 rate_mclk = OXYGEN_RATE_44100 | OXYGEN_I2S_MCLK_256;
616 break;
617 case 96000:
618 rate_mclk = OXYGEN_RATE_48000 | OXYGEN_I2S_MCLK_256;
619 break;
620 case 176400:
621 rate_mclk = OXYGEN_RATE_44100 | OXYGEN_I2S_MCLK_256;
622 break;
623 case 192000:
624 rate_mclk = OXYGEN_RATE_48000 | OXYGEN_I2S_MCLK_256;
625 break;
627 oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT, rate_mclk,
628 OXYGEN_I2S_RATE_MASK | OXYGEN_I2S_MCLK_MASK);
629 if ((rate_mclk & OXYGEN_I2S_MCLK_MASK) <= OXYGEN_I2S_MCLK_128)
630 reg = CS2000_REF_CLK_DIV_1;
631 else
632 reg = CS2000_REF_CLK_DIV_2;
633 cs2000_write_cached(chip, CS2000_FUN_CFG_1, reg);
636 static void set_st_params(struct oxygen *chip,
637 struct snd_pcm_hw_params *params)
639 update_cs2000_rate(chip, params_rate(params));
640 set_pcm1796_params(chip, params);
643 static void set_hdav_params(struct oxygen *chip,
644 struct snd_pcm_hw_params *params)
646 struct xonar_hdav *data = chip->model_data;
648 set_pcm1796_params(chip, params);
649 xonar_set_hdmi_params(chip, &data->hdmi, params);
652 static const struct snd_kcontrol_new alt_switch = {
653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
654 .name = "Analog Loopback Switch",
655 .info = snd_ctl_boolean_mono_info,
656 .get = xonar_gpio_bit_switch_get,
657 .put = xonar_gpio_bit_switch_put,
658 .private_value = GPIO_D2_ALT,
661 static int rolloff_info(struct snd_kcontrol *ctl,
662 struct snd_ctl_elem_info *info)
664 static const char *const names[2] = {
665 "Sharp Roll-off", "Slow Roll-off"
668 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
669 info->count = 1;
670 info->value.enumerated.items = 2;
671 if (info->value.enumerated.item >= 2)
672 info->value.enumerated.item = 1;
673 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
674 return 0;
677 static int rolloff_get(struct snd_kcontrol *ctl,
678 struct snd_ctl_elem_value *value)
680 struct oxygen *chip = ctl->private_data;
681 struct xonar_pcm179x *data = chip->model_data;
683 value->value.enumerated.item[0] =
684 (data->pcm1796_regs[0][19 - PCM1796_REG_BASE] &
685 PCM1796_FLT_MASK) != PCM1796_FLT_SHARP;
686 return 0;
689 static int rolloff_put(struct snd_kcontrol *ctl,
690 struct snd_ctl_elem_value *value)
692 struct oxygen *chip = ctl->private_data;
693 struct xonar_pcm179x *data = chip->model_data;
694 unsigned int i;
695 int changed;
696 u8 reg;
698 mutex_lock(&chip->mutex);
699 reg = data->pcm1796_regs[0][19 - PCM1796_REG_BASE];
700 reg &= ~PCM1796_FLT_MASK;
701 if (!value->value.enumerated.item[0])
702 reg |= PCM1796_FLT_SHARP;
703 else
704 reg |= PCM1796_FLT_SLOW;
705 changed = reg != data->pcm1796_regs[0][19 - PCM1796_REG_BASE];
706 if (changed) {
707 for (i = 0; i < data->dacs; ++i)
708 pcm1796_write(chip, i, 19, reg);
710 mutex_unlock(&chip->mutex);
711 return changed;
714 static const struct snd_kcontrol_new rolloff_control = {
715 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
716 .name = "DAC Filter Playback Enum",
717 .info = rolloff_info,
718 .get = rolloff_get,
719 .put = rolloff_put,
722 static int os_128_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
724 static const char *const names[2] = { "64x", "128x" };
726 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
727 info->count = 1;
728 info->value.enumerated.items = 2;
729 if (info->value.enumerated.item >= 2)
730 info->value.enumerated.item = 1;
731 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
732 return 0;
735 static int os_128_get(struct snd_kcontrol *ctl,
736 struct snd_ctl_elem_value *value)
738 struct oxygen *chip = ctl->private_data;
739 struct xonar_pcm179x *data = chip->model_data;
741 value->value.enumerated.item[0] = data->os_128;
742 return 0;
745 static int os_128_put(struct snd_kcontrol *ctl,
746 struct snd_ctl_elem_value *value)
748 struct oxygen *chip = ctl->private_data;
749 struct xonar_pcm179x *data = chip->model_data;
750 int changed;
752 mutex_lock(&chip->mutex);
753 changed = value->value.enumerated.item[0] != data->os_128;
754 if (changed) {
755 data->os_128 = value->value.enumerated.item[0];
756 if (data->has_cs2000)
757 update_cs2000_rate(chip, data->current_rate);
758 oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
759 mclk_from_rate(chip, data->current_rate),
760 OXYGEN_I2S_MCLK_MASK);
761 update_pcm1796_oversampling(chip);
763 mutex_unlock(&chip->mutex);
764 return changed;
767 static const struct snd_kcontrol_new os_128_control = {
768 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
769 .name = "DAC Oversampling Playback Enum",
770 .info = os_128_info,
771 .get = os_128_get,
772 .put = os_128_put,
775 static int st_output_switch_info(struct snd_kcontrol *ctl,
776 struct snd_ctl_elem_info *info)
778 static const char *const names[3] = {
779 "Speakers", "Headphones", "FP Headphones"
782 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
783 info->count = 1;
784 info->value.enumerated.items = 3;
785 if (info->value.enumerated.item >= 3)
786 info->value.enumerated.item = 2;
787 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
788 return 0;
791 static int st_output_switch_get(struct snd_kcontrol *ctl,
792 struct snd_ctl_elem_value *value)
794 struct oxygen *chip = ctl->private_data;
795 u16 gpio;
797 gpio = oxygen_read16(chip, OXYGEN_GPIO_DATA);
798 if (!(gpio & GPIO_ST_HP))
799 value->value.enumerated.item[0] = 0;
800 else if (gpio & GPIO_ST_HP_REAR)
801 value->value.enumerated.item[0] = 1;
802 else
803 value->value.enumerated.item[0] = 2;
804 return 0;
808 static int st_output_switch_put(struct snd_kcontrol *ctl,
809 struct snd_ctl_elem_value *value)
811 struct oxygen *chip = ctl->private_data;
812 struct xonar_pcm179x *data = chip->model_data;
813 u16 gpio_old, gpio;
815 mutex_lock(&chip->mutex);
816 gpio_old = oxygen_read16(chip, OXYGEN_GPIO_DATA);
817 gpio = gpio_old;
818 switch (value->value.enumerated.item[0]) {
819 case 0:
820 gpio &= ~(GPIO_ST_HP | GPIO_ST_HP_REAR);
821 break;
822 case 1:
823 gpio |= GPIO_ST_HP | GPIO_ST_HP_REAR;
824 break;
825 case 2:
826 gpio = (gpio | GPIO_ST_HP) & ~GPIO_ST_HP_REAR;
827 break;
829 oxygen_write16(chip, OXYGEN_GPIO_DATA, gpio);
830 data->hp_active = gpio & GPIO_ST_HP;
831 update_pcm1796_volume(chip);
832 mutex_unlock(&chip->mutex);
833 return gpio != gpio_old;
836 static int st_hp_volume_offset_info(struct snd_kcontrol *ctl,
837 struct snd_ctl_elem_info *info)
839 static const char *const names[3] = {
840 "< 64 ohms", "64-300 ohms", "300-600 ohms"
843 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
844 info->count = 1;
845 info->value.enumerated.items = 3;
846 if (info->value.enumerated.item > 2)
847 info->value.enumerated.item = 2;
848 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
849 return 0;
852 static int st_hp_volume_offset_get(struct snd_kcontrol *ctl,
853 struct snd_ctl_elem_value *value)
855 struct oxygen *chip = ctl->private_data;
856 struct xonar_pcm179x *data = chip->model_data;
858 mutex_lock(&chip->mutex);
859 if (data->hp_gain_offset < 2*-6)
860 value->value.enumerated.item[0] = 0;
861 else if (data->hp_gain_offset < 0)
862 value->value.enumerated.item[0] = 1;
863 else
864 value->value.enumerated.item[0] = 2;
865 mutex_unlock(&chip->mutex);
866 return 0;
870 static int st_hp_volume_offset_put(struct snd_kcontrol *ctl,
871 struct snd_ctl_elem_value *value)
873 static const s8 offsets[] = { 2*-18, 2*-6, 0 };
874 struct oxygen *chip = ctl->private_data;
875 struct xonar_pcm179x *data = chip->model_data;
876 s8 offset;
877 int changed;
879 if (value->value.enumerated.item[0] > 2)
880 return -EINVAL;
881 offset = offsets[value->value.enumerated.item[0]];
882 mutex_lock(&chip->mutex);
883 changed = offset != data->hp_gain_offset;
884 if (changed) {
885 data->hp_gain_offset = offset;
886 update_pcm1796_volume(chip);
888 mutex_unlock(&chip->mutex);
889 return changed;
892 static const struct snd_kcontrol_new st_controls[] = {
894 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
895 .name = "Analog Output",
896 .info = st_output_switch_info,
897 .get = st_output_switch_get,
898 .put = st_output_switch_put,
901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
902 .name = "Headphones Impedance Playback Enum",
903 .info = st_hp_volume_offset_info,
904 .get = st_hp_volume_offset_get,
905 .put = st_hp_volume_offset_put,
909 static void xonar_line_mic_ac97_switch(struct oxygen *chip,
910 unsigned int reg, unsigned int mute)
912 if (reg == AC97_LINE) {
913 spin_lock_irq(&chip->reg_lock);
914 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
915 mute ? GPIO_INPUT_ROUTE : 0,
916 GPIO_INPUT_ROUTE);
917 spin_unlock_irq(&chip->reg_lock);
921 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -6000, 50, 0);
923 static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
925 if (!strncmp(template->name, "CD Capture ", 11))
926 /* CD in is actually connected to the video in pin */
927 template->private_value ^= AC97_CD ^ AC97_VIDEO;
928 return 0;
931 static int add_pcm1796_controls(struct oxygen *chip)
933 int err;
935 err = snd_ctl_add(chip->card, snd_ctl_new1(&rolloff_control, chip));
936 if (err < 0)
937 return err;
938 err = snd_ctl_add(chip->card, snd_ctl_new1(&os_128_control, chip));
939 if (err < 0)
940 return err;
941 return 0;
944 static int xonar_d2_mixer_init(struct oxygen *chip)
946 int err;
948 err = snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
949 if (err < 0)
950 return err;
951 err = add_pcm1796_controls(chip);
952 if (err < 0)
953 return err;
954 return 0;
957 static int xonar_hdav_mixer_init(struct oxygen *chip)
959 return add_pcm1796_controls(chip);
962 static int xonar_st_mixer_init(struct oxygen *chip)
964 unsigned int i;
965 int err;
967 for (i = 0; i < ARRAY_SIZE(st_controls); ++i) {
968 err = snd_ctl_add(chip->card,
969 snd_ctl_new1(&st_controls[i], chip));
970 if (err < 0)
971 return err;
973 err = add_pcm1796_controls(chip);
974 if (err < 0)
975 return err;
976 return 0;
979 static const struct oxygen_model model_xonar_d2 = {
980 .longname = "Asus Virtuoso 200",
981 .chip = "AV200",
982 .init = xonar_d2_init,
983 .control_filter = xonar_d2_control_filter,
984 .mixer_init = xonar_d2_mixer_init,
985 .cleanup = xonar_d2_cleanup,
986 .suspend = xonar_d2_suspend,
987 .resume = xonar_d2_resume,
988 .get_i2s_mclk = get_pcm1796_i2s_mclk,
989 .set_dac_params = set_pcm1796_params,
990 .set_adc_params = xonar_set_cs53x1_params,
991 .update_dac_volume = update_pcm1796_volume,
992 .update_dac_mute = update_pcm1796_mute,
993 .dac_tlv = pcm1796_db_scale,
994 .model_data_size = sizeof(struct xonar_pcm179x),
995 .device_config = PLAYBACK_0_TO_I2S |
996 PLAYBACK_1_TO_SPDIF |
997 CAPTURE_0_FROM_I2S_2 |
998 CAPTURE_1_FROM_SPDIF |
999 MIDI_OUTPUT |
1000 MIDI_INPUT |
1001 AC97_CD_INPUT,
1002 .dac_channels = 8,
1003 .dac_volume_min = 255 - 2*60,
1004 .dac_volume_max = 255,
1005 .misc_flags = OXYGEN_MISC_MIDI,
1006 .function_flags = OXYGEN_FUNCTION_SPI |
1007 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
1008 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1009 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1012 static const struct oxygen_model model_xonar_hdav = {
1013 .longname = "Asus Virtuoso 200",
1014 .chip = "AV200",
1015 .init = xonar_hdav_init,
1016 .mixer_init = xonar_hdav_mixer_init,
1017 .cleanup = xonar_hdav_cleanup,
1018 .suspend = xonar_hdav_suspend,
1019 .resume = xonar_hdav_resume,
1020 .pcm_hardware_filter = xonar_hdmi_pcm_hardware_filter,
1021 .get_i2s_mclk = get_pcm1796_i2s_mclk,
1022 .set_dac_params = set_hdav_params,
1023 .set_adc_params = xonar_set_cs53x1_params,
1024 .update_dac_volume = update_pcm1796_volume,
1025 .update_dac_mute = update_pcm1796_mute,
1026 .uart_input = xonar_hdmi_uart_input,
1027 .ac97_switch = xonar_line_mic_ac97_switch,
1028 .dac_tlv = pcm1796_db_scale,
1029 .model_data_size = sizeof(struct xonar_hdav),
1030 .device_config = PLAYBACK_0_TO_I2S |
1031 PLAYBACK_1_TO_SPDIF |
1032 CAPTURE_0_FROM_I2S_2 |
1033 CAPTURE_1_FROM_SPDIF,
1034 .dac_channels = 8,
1035 .dac_volume_min = 255 - 2*60,
1036 .dac_volume_max = 255,
1037 .misc_flags = OXYGEN_MISC_MIDI,
1038 .function_flags = OXYGEN_FUNCTION_2WIRE,
1039 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1040 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1043 static const struct oxygen_model model_xonar_st = {
1044 .longname = "Asus Virtuoso 100",
1045 .chip = "AV200",
1046 .init = xonar_st_init,
1047 .mixer_init = xonar_st_mixer_init,
1048 .cleanup = xonar_st_cleanup,
1049 .suspend = xonar_st_suspend,
1050 .resume = xonar_st_resume,
1051 .get_i2s_mclk = get_pcm1796_i2s_mclk,
1052 .set_dac_params = set_st_params,
1053 .set_adc_params = xonar_set_cs53x1_params,
1054 .update_dac_volume = update_pcm1796_volume,
1055 .update_dac_mute = update_pcm1796_mute,
1056 .ac97_switch = xonar_line_mic_ac97_switch,
1057 .dac_tlv = pcm1796_db_scale,
1058 .model_data_size = sizeof(struct xonar_pcm179x),
1059 .device_config = PLAYBACK_0_TO_I2S |
1060 PLAYBACK_1_TO_SPDIF |
1061 CAPTURE_0_FROM_I2S_2,
1062 .dac_channels = 2,
1063 .dac_volume_min = 255 - 2*60,
1064 .dac_volume_max = 255,
1065 .function_flags = OXYGEN_FUNCTION_2WIRE,
1066 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1067 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1070 int __devinit get_xonar_pcm179x_model(struct oxygen *chip,
1071 const struct pci_device_id *id)
1073 switch (id->subdevice) {
1074 case 0x8269:
1075 chip->model = model_xonar_d2;
1076 chip->model.shortname = "Xonar D2";
1077 break;
1078 case 0x82b7:
1079 chip->model = model_xonar_d2;
1080 chip->model.shortname = "Xonar D2X";
1081 chip->model.init = xonar_d2x_init;
1082 break;
1083 case 0x8314:
1084 chip->model = model_xonar_hdav;
1085 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
1086 switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
1087 default:
1088 chip->model.shortname = "Xonar HDAV1.3";
1089 break;
1090 case GPIO_DB_H6:
1091 chip->model.shortname = "Xonar HDAV1.3+H6";
1092 chip->model.private_data = 1;
1093 break;
1095 break;
1096 case 0x835d:
1097 chip->model = model_xonar_st;
1098 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
1099 switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
1100 default:
1101 chip->model.shortname = "Xonar ST";
1102 break;
1103 case GPIO_DB_H6:
1104 chip->model.shortname = "Xonar ST+H6";
1105 chip->model.dac_channels = 8;
1106 chip->model.private_data = 1;
1107 break;
1109 break;
1110 case 0x835c:
1111 chip->model = model_xonar_st;
1112 chip->model.shortname = "Xonar STX";
1113 chip->model.init = xonar_stx_init;
1114 chip->model.resume = xonar_stx_resume;
1115 chip->model.set_dac_params = set_pcm1796_params;
1116 break;
1117 case 0x835e:
1118 snd_printk(KERN_ERR "the HDAV1.3 Slim is not supported\n");
1119 return -ENODEV;
1120 default:
1121 return -EINVAL;
1123 return 0;