xref: /openbmc/linux/sound/pci/oxygen/virtuoso.c (revision 7407a2e4)
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 #include <linux/pci.h>
116 #include <linux/delay.h>
117 #include <linux/mutex.h>
118 #include <sound/ac97_codec.h>
119 #include <sound/asoundef.h>
120 #include <sound/control.h>
121 #include <sound/core.h>
122 #include <sound/initval.h>
123 #include <sound/pcm.h>
124 #include <sound/pcm_params.h>
125 #include <sound/tlv.h>
126 #include "oxygen.h"
127 #include "cm9780.h"
128 #include "pcm1796.h"
129 #include "cs4398.h"
130 #include "cs4362a.h"
131 
132 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
133 MODULE_DESCRIPTION("Asus AVx00 driver");
134 MODULE_LICENSE("GPL v2");
135 MODULE_SUPPORTED_DEVICE("{{Asus,AV100},{Asus,AV200}}");
136 
137 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
138 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
139 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
140 
141 module_param_array(index, int, NULL, 0444);
142 MODULE_PARM_DESC(index, "card index");
143 module_param_array(id, charp, NULL, 0444);
144 MODULE_PARM_DESC(id, "ID string");
145 module_param_array(enable, bool, NULL, 0444);
146 MODULE_PARM_DESC(enable, "enable card");
147 
148 enum {
149 	MODEL_D2,
150 	MODEL_D2X,
151 	MODEL_D1,
152 	MODEL_DX,
153 	MODEL_HDAV,	/* without daughterboard */
154 	MODEL_HDAV_H6,	/* with H6 daughterboard */
155 };
156 
157 static struct pci_device_id xonar_ids[] __devinitdata = {
158 	{ OXYGEN_PCI_SUBID(0x1043, 0x8269), .driver_data = MODEL_D2 },
159 	{ OXYGEN_PCI_SUBID(0x1043, 0x8275), .driver_data = MODEL_DX },
160 	{ OXYGEN_PCI_SUBID(0x1043, 0x82b7), .driver_data = MODEL_D2X },
161 	{ OXYGEN_PCI_SUBID(0x1043, 0x8314), .driver_data = MODEL_HDAV },
162 	{ OXYGEN_PCI_SUBID(0x1043, 0x834f), .driver_data = MODEL_D1 },
163 	{ }
164 };
165 MODULE_DEVICE_TABLE(pci, xonar_ids);
166 
167 
168 #define GPIO_CS53x1_M_MASK	0x000c
169 #define GPIO_CS53x1_M_SINGLE	0x0000
170 #define GPIO_CS53x1_M_DOUBLE	0x0004
171 #define GPIO_CS53x1_M_QUAD	0x0008
172 
173 #define GPIO_D2X_EXT_POWER	0x0020
174 #define GPIO_D2_ALT		0x0080
175 #define GPIO_D2_OUTPUT_ENABLE	0x0100
176 
177 #define GPI_DX_EXT_POWER	0x01
178 #define GPIO_DX_OUTPUT_ENABLE	0x0001
179 #define GPIO_DX_FRONT_PANEL	0x0002
180 #define GPIO_DX_INPUT_ROUTE	0x0100
181 
182 #define GPIO_HDAV_DB_MASK	0x0030
183 #define GPIO_HDAV_DB_H6		0x0000
184 #define GPIO_HDAV_DB_XX		0x0020
185 
186 #define I2C_DEVICE_PCM1796(i)	(0x98 + ((i) << 1))	/* 10011, ADx=i, /W=0 */
187 #define I2C_DEVICE_CS4398	0x9e	/* 10011, AD1=1, AD0=1, /W=0 */
188 #define I2C_DEVICE_CS4362A	0x30	/* 001100, AD0=0, /W=0 */
189 
190 struct xonar_data {
191 	unsigned int model;
192 	unsigned int anti_pop_delay;
193 	unsigned int dacs;
194 	u16 output_enable_bit;
195 	u8 ext_power_reg;
196 	u8 ext_power_int_reg;
197 	u8 ext_power_bit;
198 	u8 has_power;
199 	u8 pcm1796_oversampling;
200 	u8 cs4398_fm;
201 	u8 cs4362a_fm;
202 	u8 hdmi_params[5];
203 };
204 
205 static void xonar_gpio_changed(struct oxygen *chip);
206 
207 static inline void pcm1796_write_spi(struct oxygen *chip, unsigned int codec,
208 				     u8 reg, u8 value)
209 {
210 	/* maps ALSA channel pair number to SPI output */
211 	static const u8 codec_map[4] = {
212 		0, 1, 2, 4
213 	};
214 	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER  |
215 			 OXYGEN_SPI_DATA_LENGTH_2 |
216 			 OXYGEN_SPI_CLOCK_160 |
217 			 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
218 			 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
219 			 (reg << 8) | value);
220 }
221 
222 static inline void pcm1796_write_i2c(struct oxygen *chip, unsigned int codec,
223 				     u8 reg, u8 value)
224 {
225 	oxygen_write_i2c(chip, I2C_DEVICE_PCM1796(codec), reg, value);
226 }
227 
228 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
229 			  u8 reg, u8 value)
230 {
231 	if ((chip->model.function_flags & OXYGEN_FUNCTION_2WIRE_SPI_MASK) ==
232 	    OXYGEN_FUNCTION_SPI)
233 		pcm1796_write_spi(chip, codec, reg, value);
234 	else
235 		pcm1796_write_i2c(chip, codec, reg, value);
236 }
237 
238 static void cs4398_write(struct oxygen *chip, u8 reg, u8 value)
239 {
240 	oxygen_write_i2c(chip, I2C_DEVICE_CS4398, reg, value);
241 }
242 
243 static void cs4362a_write(struct oxygen *chip, u8 reg, u8 value)
244 {
245 	oxygen_write_i2c(chip, I2C_DEVICE_CS4362A, reg, value);
246 }
247 
248 static void hdmi_write_command(struct oxygen *chip, u8 command,
249 			       unsigned int count, const u8 *params)
250 {
251 	unsigned int i;
252 	u8 checksum;
253 
254 	oxygen_write_uart(chip, 0xfb);
255 	oxygen_write_uart(chip, 0xef);
256 	oxygen_write_uart(chip, command);
257 	oxygen_write_uart(chip, count);
258 	for (i = 0; i < count; ++i)
259 		oxygen_write_uart(chip, params[i]);
260 	checksum = 0xfb + 0xef + command + count;
261 	for (i = 0; i < count; ++i)
262 		checksum += params[i];
263 	oxygen_write_uart(chip, checksum);
264 }
265 
266 static void xonar_enable_output(struct oxygen *chip)
267 {
268 	struct xonar_data *data = chip->model_data;
269 
270 	msleep(data->anti_pop_delay);
271 	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
272 }
273 
274 static void xonar_common_init(struct oxygen *chip)
275 {
276 	struct xonar_data *data = chip->model_data;
277 
278 	if (data->ext_power_reg) {
279 		oxygen_set_bits8(chip, data->ext_power_int_reg,
280 				 data->ext_power_bit);
281 		chip->interrupt_mask |= OXYGEN_INT_GPIO;
282 		chip->model.gpio_changed = xonar_gpio_changed;
283 		data->has_power = !!(oxygen_read8(chip, data->ext_power_reg)
284 				     & data->ext_power_bit);
285 	}
286 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
287 			  GPIO_CS53x1_M_MASK | data->output_enable_bit);
288 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
289 			      GPIO_CS53x1_M_SINGLE, GPIO_CS53x1_M_MASK);
290 	oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
291 	xonar_enable_output(chip);
292 }
293 
294 static void update_pcm1796_volume(struct oxygen *chip)
295 {
296 	struct xonar_data *data = chip->model_data;
297 	unsigned int i;
298 
299 	for (i = 0; i < data->dacs; ++i) {
300 		pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
301 		pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
302 	}
303 }
304 
305 static void update_pcm1796_mute(struct oxygen *chip)
306 {
307 	struct xonar_data *data = chip->model_data;
308 	unsigned int i;
309 	u8 value;
310 
311 	value = PCM1796_DMF_DISABLED | PCM1796_FMT_24_LJUST | PCM1796_ATLD;
312 	if (chip->dac_mute)
313 		value |= PCM1796_MUTE;
314 	for (i = 0; i < data->dacs; ++i)
315 		pcm1796_write(chip, i, 18, value);
316 }
317 
318 static void pcm1796_init(struct oxygen *chip)
319 {
320 	struct xonar_data *data = chip->model_data;
321 	unsigned int i;
322 
323 	for (i = 0; i < data->dacs; ++i) {
324 		pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
325 		pcm1796_write(chip, i, 20, data->pcm1796_oversampling);
326 		pcm1796_write(chip, i, 21, 0);
327 	}
328 	update_pcm1796_mute(chip); /* set ATLD before ATL/ATR */
329 	update_pcm1796_volume(chip);
330 }
331 
332 static void xonar_d2_init(struct oxygen *chip)
333 {
334 	struct xonar_data *data = chip->model_data;
335 
336 	data->anti_pop_delay = 300;
337 	data->output_enable_bit = GPIO_D2_OUTPUT_ENABLE;
338 	data->pcm1796_oversampling = PCM1796_OS_64;
339 	if (data->model == MODEL_D2X) {
340 		data->ext_power_reg = OXYGEN_GPIO_DATA;
341 		data->ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK;
342 		data->ext_power_bit = GPIO_D2X_EXT_POWER;
343 		oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL,
344 				    GPIO_D2X_EXT_POWER);
345 	}
346 
347 	pcm1796_init(chip);
348 
349 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT);
350 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT);
351 
352 	xonar_common_init(chip);
353 
354 	snd_component_add(chip->card, "PCM1796");
355 	snd_component_add(chip->card, "CS5381");
356 }
357 
358 static void update_cs4362a_volumes(struct oxygen *chip)
359 {
360 	u8 mute;
361 
362 	mute = chip->dac_mute ? CS4362A_MUTE : 0;
363 	cs4362a_write(chip, 7, (127 - chip->dac_volume[2]) | mute);
364 	cs4362a_write(chip, 8, (127 - chip->dac_volume[3]) | mute);
365 	cs4362a_write(chip, 10, (127 - chip->dac_volume[4]) | mute);
366 	cs4362a_write(chip, 11, (127 - chip->dac_volume[5]) | mute);
367 	cs4362a_write(chip, 13, (127 - chip->dac_volume[6]) | mute);
368 	cs4362a_write(chip, 14, (127 - chip->dac_volume[7]) | mute);
369 }
370 
371 static void update_cs43xx_volume(struct oxygen *chip)
372 {
373 	cs4398_write(chip, 5, (127 - chip->dac_volume[0]) * 2);
374 	cs4398_write(chip, 6, (127 - chip->dac_volume[1]) * 2);
375 	update_cs4362a_volumes(chip);
376 }
377 
378 static void update_cs43xx_mute(struct oxygen *chip)
379 {
380 	u8 reg;
381 
382 	reg = CS4398_MUTEP_LOW | CS4398_PAMUTE;
383 	if (chip->dac_mute)
384 		reg |= CS4398_MUTE_B | CS4398_MUTE_A;
385 	cs4398_write(chip, 4, reg);
386 	update_cs4362a_volumes(chip);
387 }
388 
389 static void cs43xx_init(struct oxygen *chip)
390 {
391 	struct xonar_data *data = chip->model_data;
392 
393 	/* set CPEN (control port mode) and power down */
394 	cs4398_write(chip, 8, CS4398_CPEN | CS4398_PDN);
395 	cs4362a_write(chip, 0x01, CS4362A_PDN | CS4362A_CPEN);
396 	/* configure */
397 	cs4398_write(chip, 2, data->cs4398_fm);
398 	cs4398_write(chip, 3, CS4398_ATAPI_B_R | CS4398_ATAPI_A_L);
399 	cs4398_write(chip, 7, CS4398_RMP_DN | CS4398_RMP_UP |
400 		     CS4398_ZERO_CROSS | CS4398_SOFT_RAMP);
401 	cs4362a_write(chip, 0x02, CS4362A_DIF_LJUST);
402 	cs4362a_write(chip, 0x03, CS4362A_MUTEC_6 | CS4362A_AMUTE |
403 		      CS4362A_RMP_UP | CS4362A_ZERO_CROSS | CS4362A_SOFT_RAMP);
404 	cs4362a_write(chip, 0x04, CS4362A_RMP_DN | CS4362A_DEM_NONE);
405 	cs4362a_write(chip, 0x05, 0);
406 	cs4362a_write(chip, 0x06, data->cs4362a_fm);
407 	cs4362a_write(chip, 0x09, data->cs4362a_fm);
408 	cs4362a_write(chip, 0x0c, data->cs4362a_fm);
409 	update_cs43xx_volume(chip);
410 	update_cs43xx_mute(chip);
411 	/* clear power down */
412 	cs4398_write(chip, 8, CS4398_CPEN);
413 	cs4362a_write(chip, 0x01, CS4362A_CPEN);
414 }
415 
416 static void xonar_d1_init(struct oxygen *chip)
417 {
418 	struct xonar_data *data = chip->model_data;
419 
420 	data->anti_pop_delay = 800;
421 	data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE;
422 	data->cs4398_fm = CS4398_FM_SINGLE | CS4398_DEM_NONE | CS4398_DIF_LJUST;
423 	data->cs4362a_fm = CS4362A_FM_SINGLE |
424 		CS4362A_ATAPI_B_R | CS4362A_ATAPI_A_L;
425 	if (data->model == MODEL_DX) {
426 		data->ext_power_reg = OXYGEN_GPI_DATA;
427 		data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
428 		data->ext_power_bit = GPI_DX_EXT_POWER;
429 	}
430 
431 	oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
432 		       OXYGEN_2WIRE_LENGTH_8 |
433 		       OXYGEN_2WIRE_INTERRUPT_MASK |
434 		       OXYGEN_2WIRE_SPEED_FAST);
435 
436 	cs43xx_init(chip);
437 
438 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
439 			  GPIO_DX_FRONT_PANEL | GPIO_DX_INPUT_ROUTE);
440 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
441 			    GPIO_DX_FRONT_PANEL | GPIO_DX_INPUT_ROUTE);
442 
443 	xonar_common_init(chip);
444 
445 	snd_component_add(chip->card, "CS4398");
446 	snd_component_add(chip->card, "CS4362A");
447 	snd_component_add(chip->card, "CS5361");
448 }
449 
450 static void xonar_hdav_init(struct oxygen *chip)
451 {
452 	struct xonar_data *data = chip->model_data;
453 	u8 param;
454 
455 	oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
456 		       OXYGEN_2WIRE_LENGTH_8 |
457 		       OXYGEN_2WIRE_INTERRUPT_MASK |
458 		       OXYGEN_2WIRE_SPEED_FAST);
459 
460 	data->anti_pop_delay = 100;
461 	data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE;
462 	data->ext_power_reg = OXYGEN_GPI_DATA;
463 	data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
464 	data->ext_power_bit = GPI_DX_EXT_POWER;
465 	data->pcm1796_oversampling = PCM1796_OS_64;
466 
467 	pcm1796_init(chip);
468 
469 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DX_INPUT_ROUTE);
470 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_DX_INPUT_ROUTE);
471 
472 	oxygen_reset_uart(chip);
473 	param = 0;
474 	hdmi_write_command(chip, 0x61, 1, &param);
475 	param = 1;
476 	hdmi_write_command(chip, 0x74, 1, &param);
477 	data->hdmi_params[1] = IEC958_AES3_CON_FS_48000;
478 	data->hdmi_params[4] = 1;
479 	hdmi_write_command(chip, 0x54, 5, data->hdmi_params);
480 
481 	xonar_common_init(chip);
482 
483 	snd_component_add(chip->card, "PCM1796");
484 	snd_component_add(chip->card, "CS5381");
485 }
486 
487 static void xonar_disable_output(struct oxygen *chip)
488 {
489 	struct xonar_data *data = chip->model_data;
490 
491 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
492 }
493 
494 static void xonar_d2_cleanup(struct oxygen *chip)
495 {
496 	xonar_disable_output(chip);
497 }
498 
499 static void xonar_d1_cleanup(struct oxygen *chip)
500 {
501 	xonar_disable_output(chip);
502 	cs4362a_write(chip, 0x01, CS4362A_PDN | CS4362A_CPEN);
503 	oxygen_clear_bits8(chip, OXYGEN_FUNCTION, OXYGEN_FUNCTION_RESET_CODEC);
504 }
505 
506 static void xonar_hdav_cleanup(struct oxygen *chip)
507 {
508 	u8 param = 0;
509 
510 	hdmi_write_command(chip, 0x74, 1, &param);
511 	xonar_disable_output(chip);
512 }
513 
514 static void xonar_d2_suspend(struct oxygen *chip)
515 {
516 	xonar_d2_cleanup(chip);
517 }
518 
519 static void xonar_d1_suspend(struct oxygen *chip)
520 {
521 	xonar_d1_cleanup(chip);
522 }
523 
524 static void xonar_hdav_suspend(struct oxygen *chip)
525 {
526 	xonar_hdav_cleanup(chip);
527 	msleep(2);
528 }
529 
530 static void xonar_d2_resume(struct oxygen *chip)
531 {
532 	pcm1796_init(chip);
533 	xonar_enable_output(chip);
534 }
535 
536 static void xonar_d1_resume(struct oxygen *chip)
537 {
538 	cs43xx_init(chip);
539 	xonar_enable_output(chip);
540 }
541 
542 static void xonar_hdav_resume(struct oxygen *chip)
543 {
544 	struct xonar_data *data = chip->model_data;
545 	u8 param;
546 
547 	oxygen_reset_uart(chip);
548 	param = 0;
549 	hdmi_write_command(chip, 0x61, 1, &param);
550 	param = 1;
551 	hdmi_write_command(chip, 0x74, 1, &param);
552 	hdmi_write_command(chip, 0x54, 5, data->hdmi_params);
553 	pcm1796_init(chip);
554 	xonar_enable_output(chip);
555 }
556 
557 static void xonar_hdav_pcm_hardware_filter(unsigned int channel,
558 					   struct snd_pcm_hardware *hardware)
559 {
560 	if (channel == PCM_MULTICH) {
561 		hardware->rates = SNDRV_PCM_RATE_44100 |
562 				  SNDRV_PCM_RATE_48000 |
563 				  SNDRV_PCM_RATE_96000 |
564 				  SNDRV_PCM_RATE_192000;
565 		hardware->rate_min = 44100;
566 	}
567 }
568 
569 static void set_pcm1796_params(struct oxygen *chip,
570 			       struct snd_pcm_hw_params *params)
571 {
572 	struct xonar_data *data = chip->model_data;
573 	unsigned int i;
574 
575 	data->pcm1796_oversampling =
576 		params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
577 	for (i = 0; i < data->dacs; ++i)
578 		pcm1796_write(chip, i, 20, data->pcm1796_oversampling);
579 }
580 
581 static void set_cs53x1_params(struct oxygen *chip,
582 			      struct snd_pcm_hw_params *params)
583 {
584 	unsigned int value;
585 
586 	if (params_rate(params) <= 54000)
587 		value = GPIO_CS53x1_M_SINGLE;
588 	else if (params_rate(params) <= 108000)
589 		value = GPIO_CS53x1_M_DOUBLE;
590 	else
591 		value = GPIO_CS53x1_M_QUAD;
592 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
593 			      value, GPIO_CS53x1_M_MASK);
594 }
595 
596 static void set_cs43xx_params(struct oxygen *chip,
597 			      struct snd_pcm_hw_params *params)
598 {
599 	struct xonar_data *data = chip->model_data;
600 
601 	data->cs4398_fm = CS4398_DEM_NONE | CS4398_DIF_LJUST;
602 	data->cs4362a_fm = CS4362A_ATAPI_B_R | CS4362A_ATAPI_A_L;
603 	if (params_rate(params) <= 50000) {
604 		data->cs4398_fm |= CS4398_FM_SINGLE;
605 		data->cs4362a_fm |= CS4362A_FM_SINGLE;
606 	} else if (params_rate(params) <= 100000) {
607 		data->cs4398_fm |= CS4398_FM_DOUBLE;
608 		data->cs4362a_fm |= CS4362A_FM_DOUBLE;
609 	} else {
610 		data->cs4398_fm |= CS4398_FM_QUAD;
611 		data->cs4362a_fm |= CS4362A_FM_QUAD;
612 	}
613 	cs4398_write(chip, 2, data->cs4398_fm);
614 	cs4362a_write(chip, 0x06, data->cs4362a_fm);
615 	cs4362a_write(chip, 0x09, data->cs4362a_fm);
616 	cs4362a_write(chip, 0x0c, data->cs4362a_fm);
617 }
618 
619 static void set_hdmi_params(struct oxygen *chip,
620 			    struct snd_pcm_hw_params *params)
621 {
622 	struct xonar_data *data = chip->model_data;
623 
624 	data->hdmi_params[0] = 0; /* 1 = non-audio */
625 	switch (params_rate(params)) {
626 	case 44100:
627 		data->hdmi_params[1] = IEC958_AES3_CON_FS_44100;
628 		break;
629 	case 48000:
630 		data->hdmi_params[1] = IEC958_AES3_CON_FS_48000;
631 		break;
632 	default: /* 96000 */
633 		data->hdmi_params[1] = IEC958_AES3_CON_FS_96000;
634 		break;
635 	case 192000:
636 		data->hdmi_params[1] = IEC958_AES3_CON_FS_192000;
637 		break;
638 	}
639 	data->hdmi_params[2] = params_channels(params) / 2 - 1;
640 	if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE)
641 		data->hdmi_params[3] = 0;
642 	else
643 		data->hdmi_params[3] = 0xc0;
644 	data->hdmi_params[4] = 1; /* ? */
645 	hdmi_write_command(chip, 0x54, 5, data->hdmi_params);
646 }
647 
648 static void set_hdav_params(struct oxygen *chip,
649 			    struct snd_pcm_hw_params *params)
650 {
651 	set_pcm1796_params(chip, params);
652 	set_hdmi_params(chip, params);
653 }
654 
655 static void xonar_gpio_changed(struct oxygen *chip)
656 {
657 	struct xonar_data *data = chip->model_data;
658 	u8 has_power;
659 
660 	has_power = !!(oxygen_read8(chip, data->ext_power_reg)
661 		       & data->ext_power_bit);
662 	if (has_power != data->has_power) {
663 		data->has_power = has_power;
664 		if (has_power) {
665 			snd_printk(KERN_NOTICE "power restored\n");
666 		} else {
667 			snd_printk(KERN_CRIT
668 				   "Hey! Don't unplug the power cable!\n");
669 			/* TODO: stop PCMs */
670 		}
671 	}
672 }
673 
674 static void xonar_hdav_uart_input(struct oxygen *chip)
675 {
676 	if (chip->uart_input_count >= 2 &&
677 	    chip->uart_input[chip->uart_input_count - 2] == 'O' &&
678 	    chip->uart_input[chip->uart_input_count - 1] == 'K') {
679 		printk(KERN_DEBUG "message from Xonar HDAV HDMI chip received:");
680 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
681 				     chip->uart_input, chip->uart_input_count);
682 		chip->uart_input_count = 0;
683 	}
684 }
685 
686 static int gpio_bit_switch_get(struct snd_kcontrol *ctl,
687 			       struct snd_ctl_elem_value *value)
688 {
689 	struct oxygen *chip = ctl->private_data;
690 	u16 bit = ctl->private_value;
691 
692 	value->value.integer.value[0] =
693 		!!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & bit);
694 	return 0;
695 }
696 
697 static int gpio_bit_switch_put(struct snd_kcontrol *ctl,
698 			       struct snd_ctl_elem_value *value)
699 {
700 	struct oxygen *chip = ctl->private_data;
701 	u16 bit = ctl->private_value;
702 	u16 old_bits, new_bits;
703 	int changed;
704 
705 	spin_lock_irq(&chip->reg_lock);
706 	old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
707 	if (value->value.integer.value[0])
708 		new_bits = old_bits | bit;
709 	else
710 		new_bits = old_bits & ~bit;
711 	changed = new_bits != old_bits;
712 	if (changed)
713 		oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
714 	spin_unlock_irq(&chip->reg_lock);
715 	return changed;
716 }
717 
718 static const struct snd_kcontrol_new alt_switch = {
719 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
720 	.name = "Analog Loopback Switch",
721 	.info = snd_ctl_boolean_mono_info,
722 	.get = gpio_bit_switch_get,
723 	.put = gpio_bit_switch_put,
724 	.private_value = GPIO_D2_ALT,
725 };
726 
727 static const struct snd_kcontrol_new front_panel_switch = {
728 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
729 	.name = "Front Panel Switch",
730 	.info = snd_ctl_boolean_mono_info,
731 	.get = gpio_bit_switch_get,
732 	.put = gpio_bit_switch_put,
733 	.private_value = GPIO_DX_FRONT_PANEL,
734 };
735 
736 static void xonar_line_mic_ac97_switch(struct oxygen *chip,
737 				       unsigned int reg, unsigned int mute)
738 {
739 	if (reg == AC97_LINE) {
740 		spin_lock_irq(&chip->reg_lock);
741 		oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
742 				      mute ? GPIO_DX_INPUT_ROUTE : 0,
743 				      GPIO_DX_INPUT_ROUTE);
744 		spin_unlock_irq(&chip->reg_lock);
745 	}
746 }
747 
748 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
749 static const DECLARE_TLV_DB_SCALE(cs4362a_db_scale, -12700, 100, 0);
750 
751 static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
752 {
753 	if (!strncmp(template->name, "CD Capture ", 11))
754 		/* CD in is actually connected to the video in pin */
755 		template->private_value ^= AC97_CD ^ AC97_VIDEO;
756 	return 0;
757 }
758 
759 static int xonar_d1_control_filter(struct snd_kcontrol_new *template)
760 {
761 	if (!strncmp(template->name, "CD Capture ", 11))
762 		return 1; /* no CD input */
763 	return 0;
764 }
765 
766 static int xonar_d2_mixer_init(struct oxygen *chip)
767 {
768 	return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
769 }
770 
771 static int xonar_d1_mixer_init(struct oxygen *chip)
772 {
773 	return snd_ctl_add(chip->card, snd_ctl_new1(&front_panel_switch, chip));
774 }
775 
776 static int xonar_model_probe(struct oxygen *chip, unsigned long driver_data)
777 {
778 	static const char *const names[] = {
779 		[MODEL_D1]	= "Xonar D1",
780 		[MODEL_DX]	= "Xonar DX",
781 		[MODEL_D2]	= "Xonar D2",
782 		[MODEL_D2X]	= "Xonar D2X",
783 		[MODEL_HDAV]	= "Xonar HDAV1.3",
784 		[MODEL_HDAV_H6]	= "Xonar HDAV1.3+H6",
785 	};
786 	static const u8 dacs[] = {
787 		[MODEL_D1]	= 2,
788 		[MODEL_DX]	= 2,
789 		[MODEL_D2]	= 4,
790 		[MODEL_D2X]	= 4,
791 		[MODEL_HDAV]	= 1,
792 		[MODEL_HDAV_H6]	= 4,
793 	};
794 	struct xonar_data *data = chip->model_data;
795 
796 	data->model = driver_data;
797 	if (data->model == MODEL_HDAV) {
798 		oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL,
799 				    GPIO_HDAV_DB_MASK);
800 		switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) &
801 			GPIO_HDAV_DB_MASK) {
802 		case GPIO_HDAV_DB_H6:
803 			data->model = MODEL_HDAV_H6;
804 			break;
805 		case GPIO_HDAV_DB_XX:
806 			snd_printk(KERN_ERR "unknown daughterboard\n");
807 			return -ENODEV;
808 		}
809 	}
810 
811 	data->dacs = dacs[data->model];
812 	chip->model.shortname = names[data->model];
813 	return 0;
814 }
815 
816 static const struct oxygen_model model_xonar_d2 = {
817 	.longname = "Asus Virtuoso 200",
818 	.chip = "AV200",
819 	.owner = THIS_MODULE,
820 	.probe = xonar_model_probe,
821 	.init = xonar_d2_init,
822 	.control_filter = xonar_d2_control_filter,
823 	.mixer_init = xonar_d2_mixer_init,
824 	.cleanup = xonar_d2_cleanup,
825 	.suspend = xonar_d2_suspend,
826 	.resume = xonar_d2_resume,
827 	.set_dac_params = set_pcm1796_params,
828 	.set_adc_params = set_cs53x1_params,
829 	.update_dac_volume = update_pcm1796_volume,
830 	.update_dac_mute = update_pcm1796_mute,
831 	.dac_tlv = pcm1796_db_scale,
832 	.model_data_size = sizeof(struct xonar_data),
833 	.device_config = PLAYBACK_0_TO_I2S |
834 			 PLAYBACK_1_TO_SPDIF |
835 			 CAPTURE_0_FROM_I2S_2 |
836 			 CAPTURE_1_FROM_SPDIF |
837 			 MIDI_OUTPUT |
838 			 MIDI_INPUT,
839 	.dac_channels = 8,
840 	.dac_volume_min = 0x0f,
841 	.dac_volume_max = 0xff,
842 	.misc_flags = OXYGEN_MISC_MIDI,
843 	.function_flags = OXYGEN_FUNCTION_SPI |
844 			  OXYGEN_FUNCTION_ENABLE_SPI_4_5,
845 	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
846 	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
847 };
848 
849 static const struct oxygen_model model_xonar_d1 = {
850 	.longname = "Asus Virtuoso 100",
851 	.chip = "AV200",
852 	.owner = THIS_MODULE,
853 	.probe = xonar_model_probe,
854 	.init = xonar_d1_init,
855 	.control_filter = xonar_d1_control_filter,
856 	.mixer_init = xonar_d1_mixer_init,
857 	.cleanup = xonar_d1_cleanup,
858 	.suspend = xonar_d1_suspend,
859 	.resume = xonar_d1_resume,
860 	.set_dac_params = set_cs43xx_params,
861 	.set_adc_params = set_cs53x1_params,
862 	.update_dac_volume = update_cs43xx_volume,
863 	.update_dac_mute = update_cs43xx_mute,
864 	.ac97_switch = xonar_line_mic_ac97_switch,
865 	.dac_tlv = cs4362a_db_scale,
866 	.model_data_size = sizeof(struct xonar_data),
867 	.device_config = PLAYBACK_0_TO_I2S |
868 			 PLAYBACK_1_TO_SPDIF |
869 			 CAPTURE_0_FROM_I2S_2,
870 	.dac_channels = 8,
871 	.dac_volume_min = 0,
872 	.dac_volume_max = 127,
873 	.function_flags = OXYGEN_FUNCTION_2WIRE,
874 	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
875 	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
876 };
877 
878 static const struct oxygen_model model_xonar_hdav = {
879 	.longname = "Asus Virtuoso 200",
880 	.chip = "AV200",
881 	.owner = THIS_MODULE,
882 	.probe = xonar_model_probe,
883 	.init = xonar_hdav_init,
884 	.cleanup = xonar_hdav_cleanup,
885 	.suspend = xonar_hdav_suspend,
886 	.resume = xonar_hdav_resume,
887 	.pcm_hardware_filter = xonar_hdav_pcm_hardware_filter,
888 	.set_dac_params = set_hdav_params,
889 	.set_adc_params = set_cs53x1_params,
890 	.update_dac_volume = update_pcm1796_volume,
891 	.update_dac_mute = update_pcm1796_mute,
892 	.uart_input = xonar_hdav_uart_input,
893 	.ac97_switch = xonar_line_mic_ac97_switch,
894 	.dac_tlv = pcm1796_db_scale,
895 	.model_data_size = sizeof(struct xonar_data),
896 	.device_config = PLAYBACK_0_TO_I2S |
897 			 PLAYBACK_1_TO_SPDIF |
898 			 CAPTURE_0_FROM_I2S_2,
899 	.dac_channels = 8,
900 	.dac_volume_min = 0x0f,
901 	.dac_volume_max = 0xff,
902 	.function_flags = OXYGEN_FUNCTION_2WIRE,
903 	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
904 	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
905 };
906 
907 static int __devinit xonar_probe(struct pci_dev *pci,
908 				 const struct pci_device_id *pci_id)
909 {
910 	static const struct oxygen_model *const models[] = {
911 		[MODEL_D1]	= &model_xonar_d1,
912 		[MODEL_DX]	= &model_xonar_d1,
913 		[MODEL_D2]	= &model_xonar_d2,
914 		[MODEL_D2X]	= &model_xonar_d2,
915 		[MODEL_HDAV]	= &model_xonar_hdav,
916 	};
917 	static int dev;
918 	int err;
919 
920 	if (dev >= SNDRV_CARDS)
921 		return -ENODEV;
922 	if (!enable[dev]) {
923 		++dev;
924 		return -ENOENT;
925 	}
926 	BUG_ON(pci_id->driver_data >= ARRAY_SIZE(models));
927 	err = oxygen_pci_probe(pci, index[dev], id[dev],
928 			       models[pci_id->driver_data],
929 			       pci_id->driver_data);
930 	if (err >= 0)
931 		++dev;
932 	return err;
933 }
934 
935 static struct pci_driver xonar_driver = {
936 	.name = "AV200",
937 	.id_table = xonar_ids,
938 	.probe = xonar_probe,
939 	.remove = __devexit_p(oxygen_pci_remove),
940 #ifdef CONFIG_PM
941 	.suspend = oxygen_pci_suspend,
942 	.resume = oxygen_pci_resume,
943 #endif
944 };
945 
946 static int __init alsa_card_xonar_init(void)
947 {
948 	return pci_register_driver(&xonar_driver);
949 }
950 
951 static void __exit alsa_card_xonar_exit(void)
952 {
953 	pci_unregister_driver(&xonar_driver);
954 }
955 
956 module_init(alsa_card_xonar_init)
957 module_exit(alsa_card_xonar_exit)
958