blob: 6ebcb6bdd7129eb8dd04daf344aad880aabf8ce9 [file] [log] [blame]
Clemens Ladisch1b8ff222007-12-23 19:52:08 +01001/*
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/*
Clemens Ladischa9d3cc42008-04-07 10:29:44 +020021 * Xonar D2/D2X
22 * ------------
23 *
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010024 * CMI8788:
25 *
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010026 * SPI 0 -> 1st PCM1796 (front)
Clemens Ladisch7113e952008-01-14 08:55:03 +010027 * SPI 1 -> 2nd PCM1796 (surround)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010028 * SPI 2 -> 3rd PCM1796 (center/LFE)
Clemens Ladisch6ce6c472009-02-17 09:50:30 +010029 * SPI 4 -> 4th PCM1796 (back)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010030 *
31 * GPIO 2 -> M0 of CS5381
32 * GPIO 3 -> M1 of CS5381
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010033 * GPIO 5 <- external power present (D2X only)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010034 * GPIO 7 -> ALT
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010035 * GPIO 8 -> enable output to speakers
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010036 */
37
Clemens Ladischa9d3cc42008-04-07 10:29:44 +020038/*
Clemens Ladisch5430c722008-07-28 10:28:43 +020039 * Xonar D1/DX
40 * -----------
Clemens Ladischa9d3cc42008-04-07 10:29:44 +020041 *
42 * CMI8788:
43 *
44 * I²C <-> CS4398 (front)
45 * <-> CS4362A (surround, center/LFE, back)
46 *
Clemens Ladisch5430c722008-07-28 10:28:43 +020047 * GPI 0 <- external power present (DX only)
Clemens Ladischa9d3cc42008-04-07 10:29:44 +020048 *
49 * GPIO 0 -> enable output to speakers
Clemens Ladischa8bb1ba2008-04-15 08:57:31 +020050 * GPIO 1 -> enable front panel I/O
Clemens Ladischa9d3cc42008-04-07 10:29:44 +020051 * GPIO 2 -> M0 of CS5361
52 * GPIO 3 -> M1 of CS5361
Clemens Ladisch11864b42008-04-09 09:16:14 +020053 * GPIO 8 -> route input jack to line-in (0) or mic-in (1)
Clemens Ladischa9d3cc42008-04-07 10:29:44 +020054 *
55 * CS4398:
56 *
57 * AD0 <- 1
58 * AD1 <- 1
59 *
60 * CS4362A:
61 *
62 * AD0 <- 0
63 */
64
Clemens Ladisch7407a2e2008-09-22 09:12:11 +020065/*
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
Clemens Ladisch930738d2009-02-26 09:27:20 +0100115/*
Clemens Ladisch04f98902009-05-25 10:11:29 +0200116 * Xonar Essence ST (Deluxe)/STX
117 * -----------------------------
Clemens Ladisch930738d2009-02-26 09:27:20 +0100118 *
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
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100143#include <linux/pci.h>
144#include <linux/delay.h>
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100145#include <linux/mutex.h>
146#include <sound/ac97_codec.h>
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200147#include <sound/asoundef.h>
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100148#include <sound/control.h>
149#include <sound/core.h>
150#include <sound/initval.h>
151#include <sound/pcm.h>
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200152#include <sound/pcm_params.h>
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100153#include <sound/tlv.h>
154#include "oxygen.h"
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100155#include "cm9780.h"
Clemens Ladisch33fa7242008-03-19 08:16:40 +0100156#include "pcm1796.h"
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200157#include "cs4398.h"
158#include "cs4362a.h"
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100159
160MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200161MODULE_DESCRIPTION("Asus AVx00 driver");
Clemens Ladischd023dc02008-05-13 09:18:27 +0200162MODULE_LICENSE("GPL v2");
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200163MODULE_SUPPORTED_DEVICE("{{Asus,AV100},{Asus,AV200}}");
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100164
165static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
166static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
167static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
168
169module_param_array(index, int, NULL, 0444);
170MODULE_PARM_DESC(index, "card index");
171module_param_array(id, charp, NULL, 0444);
172MODULE_PARM_DESC(id, "ID string");
173module_param_array(enable, bool, NULL, 0444);
174MODULE_PARM_DESC(enable, "enable card");
175
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200176enum {
177 MODEL_D2,
178 MODEL_D2X,
Clemens Ladisch5430c722008-07-28 10:28:43 +0200179 MODEL_D1,
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200180 MODEL_DX,
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200181 MODEL_HDAV, /* without daughterboard */
182 MODEL_HDAV_H6, /* with H6 daughterboard */
Clemens Ladisch04f98902009-05-25 10:11:29 +0200183 MODEL_ST,
184 MODEL_ST_H6,
Clemens Ladisch930738d2009-02-26 09:27:20 +0100185 MODEL_STX,
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200186};
187
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100188static struct pci_device_id xonar_ids[] __devinitdata = {
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200189 { OXYGEN_PCI_SUBID(0x1043, 0x8269), .driver_data = MODEL_D2 },
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200190 { OXYGEN_PCI_SUBID(0x1043, 0x8275), .driver_data = MODEL_DX },
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200191 { OXYGEN_PCI_SUBID(0x1043, 0x82b7), .driver_data = MODEL_D2X },
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200192 { OXYGEN_PCI_SUBID(0x1043, 0x8314), .driver_data = MODEL_HDAV },
Clemens Ladisch53bb7052009-05-25 10:05:43 +0200193 { OXYGEN_PCI_SUBID(0x1043, 0x8327), .driver_data = MODEL_DX },
Clemens Ladisch5430c722008-07-28 10:28:43 +0200194 { OXYGEN_PCI_SUBID(0x1043, 0x834f), .driver_data = MODEL_D1 },
Clemens Ladisch930738d2009-02-26 09:27:20 +0100195 { OXYGEN_PCI_SUBID(0x1043, 0x835c), .driver_data = MODEL_STX },
Clemens Ladisch04f98902009-05-25 10:11:29 +0200196 { OXYGEN_PCI_SUBID(0x1043, 0x835d), .driver_data = MODEL_ST },
Clemens Ladisch30459d72009-02-19 08:42:44 +0100197 { OXYGEN_PCI_SUBID_BROKEN_EEPROM },
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100198 { }
199};
200MODULE_DEVICE_TABLE(pci, xonar_ids);
201
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100202
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200203#define GPIO_CS53x1_M_MASK 0x000c
204#define GPIO_CS53x1_M_SINGLE 0x0000
205#define GPIO_CS53x1_M_DOUBLE 0x0004
206#define GPIO_CS53x1_M_QUAD 0x0008
207
Clemens Ladischaf9af172008-04-07 10:26:03 +0200208#define GPIO_D2X_EXT_POWER 0x0020
209#define GPIO_D2_ALT 0x0080
210#define GPIO_D2_OUTPUT_ENABLE 0x0100
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100211
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200212#define GPI_DX_EXT_POWER 0x01
213#define GPIO_DX_OUTPUT_ENABLE 0x0001
Clemens Ladischa8bb1ba2008-04-15 08:57:31 +0200214#define GPIO_DX_FRONT_PANEL 0x0002
Clemens Ladisch11864b42008-04-09 09:16:14 +0200215#define GPIO_DX_INPUT_ROUTE 0x0100
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200216
Clemens Ladisch04f98902009-05-25 10:11:29 +0200217#define GPIO_DB_MASK 0x0030
218#define GPIO_DB_H6 0x0000
219#define GPIO_DB_XX 0x0020
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200220
Clemens Ladisch930738d2009-02-26 09:27:20 +0100221#define GPIO_ST_HP_REAR 0x0002
222#define GPIO_ST_HP 0x0080
223
Clemens Ladisch4f50d2f2008-09-22 09:06:04 +0200224#define I2C_DEVICE_PCM1796(i) (0x98 + ((i) << 1)) /* 10011, ADx=i, /W=0 */
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200225#define I2C_DEVICE_CS4398 0x9e /* 10011, AD1=1, AD0=1, /W=0 */
226#define I2C_DEVICE_CS4362A 0x30 /* 001100, AD0=0, /W=0 */
227
Clemens Ladisch7c014152008-01-28 08:36:55 +0100228struct xonar_data {
Clemens Ladischaf9af172008-04-07 10:26:03 +0200229 unsigned int anti_pop_delay;
Clemens Ladisch70c27d32008-09-22 09:06:38 +0200230 unsigned int dacs;
Clemens Ladischaf9af172008-04-07 10:26:03 +0200231 u16 output_enable_bit;
232 u8 ext_power_reg;
233 u8 ext_power_int_reg;
234 u8 ext_power_bit;
Clemens Ladisch7c014152008-01-28 08:36:55 +0100235 u8 has_power;
Clemens Ladische58aee92008-05-13 09:20:51 +0200236 u8 pcm1796_oversampling;
237 u8 cs4398_fm;
238 u8 cs4362a_fm;
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200239 u8 hdmi_params[5];
Clemens Ladisch7c014152008-01-28 08:36:55 +0100240};
241
Clemens Ladischfe106622008-09-22 09:00:30 +0200242static void xonar_gpio_changed(struct oxygen *chip);
243
Clemens Ladisch4f50d2f2008-09-22 09:06:04 +0200244static inline void pcm1796_write_spi(struct oxygen *chip, unsigned int codec,
245 u8 reg, u8 value)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100246{
247 /* maps ALSA channel pair number to SPI output */
248 static const u8 codec_map[4] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +0100249 0, 1, 2, 4
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100250 };
Clemens Ladischc2353a02008-01-18 09:17:53 +0100251 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100252 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100253 OXYGEN_SPI_CLOCK_160 |
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100254 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100255 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100256 (reg << 8) | value);
257}
258
Clemens Ladisch4f50d2f2008-09-22 09:06:04 +0200259static inline void pcm1796_write_i2c(struct oxygen *chip, unsigned int codec,
260 u8 reg, u8 value)
261{
262 oxygen_write_i2c(chip, I2C_DEVICE_PCM1796(codec), reg, value);
263}
264
265static void pcm1796_write(struct oxygen *chip, unsigned int codec,
266 u8 reg, u8 value)
267{
268 if ((chip->model.function_flags & OXYGEN_FUNCTION_2WIRE_SPI_MASK) ==
269 OXYGEN_FUNCTION_SPI)
270 pcm1796_write_spi(chip, codec, reg, value);
271 else
272 pcm1796_write_i2c(chip, codec, reg, value);
273}
274
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200275static void cs4398_write(struct oxygen *chip, u8 reg, u8 value)
276{
277 oxygen_write_i2c(chip, I2C_DEVICE_CS4398, reg, value);
278}
279
280static void cs4362a_write(struct oxygen *chip, u8 reg, u8 value)
281{
282 oxygen_write_i2c(chip, I2C_DEVICE_CS4362A, reg, value);
283}
284
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200285static void hdmi_write_command(struct oxygen *chip, u8 command,
286 unsigned int count, const u8 *params)
287{
288 unsigned int i;
289 u8 checksum;
290
291 oxygen_write_uart(chip, 0xfb);
292 oxygen_write_uart(chip, 0xef);
293 oxygen_write_uart(chip, command);
294 oxygen_write_uart(chip, count);
295 for (i = 0; i < count; ++i)
296 oxygen_write_uart(chip, params[i]);
297 checksum = 0xfb + 0xef + command + count;
298 for (i = 0; i < count; ++i)
299 checksum += params[i];
300 oxygen_write_uart(chip, checksum);
301}
302
Clemens Ladisch92215f32008-05-13 09:23:02 +0200303static void xonar_enable_output(struct oxygen *chip)
304{
305 struct xonar_data *data = chip->model_data;
306
307 msleep(data->anti_pop_delay);
308 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
309}
310
Clemens Ladischaf9af172008-04-07 10:26:03 +0200311static void xonar_common_init(struct oxygen *chip)
312{
313 struct xonar_data *data = chip->model_data;
314
315 if (data->ext_power_reg) {
316 oxygen_set_bits8(chip, data->ext_power_int_reg,
317 data->ext_power_bit);
318 chip->interrupt_mask |= OXYGEN_INT_GPIO;
Clemens Ladischfe106622008-09-22 09:00:30 +0200319 chip->model.gpio_changed = xonar_gpio_changed;
Clemens Ladischaf9af172008-04-07 10:26:03 +0200320 data->has_power = !!(oxygen_read8(chip, data->ext_power_reg)
321 & data->ext_power_bit);
322 }
Clemens Ladisch92215f32008-05-13 09:23:02 +0200323 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
324 GPIO_CS53x1_M_MASK | data->output_enable_bit);
Clemens Ladischaf9af172008-04-07 10:26:03 +0200325 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
326 GPIO_CS53x1_M_SINGLE, GPIO_CS53x1_M_MASK);
327 oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
Clemens Ladisch92215f32008-05-13 09:23:02 +0200328 xonar_enable_output(chip);
Clemens Ladischaf9af172008-04-07 10:26:03 +0200329}
330
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200331static void update_pcm1796_volume(struct oxygen *chip)
332{
Clemens Ladisch70c27d32008-09-22 09:06:38 +0200333 struct xonar_data *data = chip->model_data;
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200334 unsigned int i;
335
Clemens Ladisch70c27d32008-09-22 09:06:38 +0200336 for (i = 0; i < data->dacs; ++i) {
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200337 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
338 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
339 }
340}
341
342static void update_pcm1796_mute(struct oxygen *chip)
343{
Clemens Ladisch70c27d32008-09-22 09:06:38 +0200344 struct xonar_data *data = chip->model_data;
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200345 unsigned int i;
346 u8 value;
347
348 value = PCM1796_DMF_DISABLED | PCM1796_FMT_24_LJUST | PCM1796_ATLD;
349 if (chip->dac_mute)
350 value |= PCM1796_MUTE;
Clemens Ladisch70c27d32008-09-22 09:06:38 +0200351 for (i = 0; i < data->dacs; ++i)
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200352 pcm1796_write(chip, i, 18, value);
353}
354
Clemens Ladisch75146fc2008-05-13 09:22:43 +0200355static void pcm1796_init(struct oxygen *chip)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100356{
Clemens Ladischaf9af172008-04-07 10:26:03 +0200357 struct xonar_data *data = chip->model_data;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100358 unsigned int i;
359
Clemens Ladisch70c27d32008-09-22 09:06:38 +0200360 for (i = 0; i < data->dacs; ++i) {
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100361 pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
Clemens Ladische58aee92008-05-13 09:20:51 +0200362 pcm1796_write(chip, i, 20, data->pcm1796_oversampling);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100363 pcm1796_write(chip, i, 21, 0);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100364 }
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200365 update_pcm1796_mute(chip); /* set ATLD before ATL/ATR */
366 update_pcm1796_volume(chip);
Clemens Ladisch75146fc2008-05-13 09:22:43 +0200367}
368
369static void xonar_d2_init(struct oxygen *chip)
370{
371 struct xonar_data *data = chip->model_data;
372
373 data->anti_pop_delay = 300;
Clemens Ladisch30459d72009-02-19 08:42:44 +0100374 data->dacs = 4;
Clemens Ladisch75146fc2008-05-13 09:22:43 +0200375 data->output_enable_bit = GPIO_D2_OUTPUT_ENABLE;
376 data->pcm1796_oversampling = PCM1796_OS_64;
377
378 pcm1796_init(chip);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100379
Clemens Ladischaf9af172008-04-07 10:26:03 +0200380 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT);
381 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT);
382
383 xonar_common_init(chip);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100384
385 snd_component_add(chip->card, "PCM1796");
386 snd_component_add(chip->card, "CS5381");
387}
388
Clemens Ladisch30459d72009-02-19 08:42:44 +0100389static void xonar_d2x_init(struct oxygen *chip)
390{
391 struct xonar_data *data = chip->model_data;
392
393 data->ext_power_reg = OXYGEN_GPIO_DATA;
394 data->ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK;
395 data->ext_power_bit = GPIO_D2X_EXT_POWER;
396 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2X_EXT_POWER);
397
398 xonar_d2_init(chip);
399}
400
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200401static void update_cs4362a_volumes(struct oxygen *chip)
402{
403 u8 mute;
404
405 mute = chip->dac_mute ? CS4362A_MUTE : 0;
406 cs4362a_write(chip, 7, (127 - chip->dac_volume[2]) | mute);
407 cs4362a_write(chip, 8, (127 - chip->dac_volume[3]) | mute);
408 cs4362a_write(chip, 10, (127 - chip->dac_volume[4]) | mute);
409 cs4362a_write(chip, 11, (127 - chip->dac_volume[5]) | mute);
410 cs4362a_write(chip, 13, (127 - chip->dac_volume[6]) | mute);
411 cs4362a_write(chip, 14, (127 - chip->dac_volume[7]) | mute);
412}
413
414static void update_cs43xx_volume(struct oxygen *chip)
415{
416 cs4398_write(chip, 5, (127 - chip->dac_volume[0]) * 2);
417 cs4398_write(chip, 6, (127 - chip->dac_volume[1]) * 2);
418 update_cs4362a_volumes(chip);
419}
420
421static void update_cs43xx_mute(struct oxygen *chip)
422{
423 u8 reg;
424
425 reg = CS4398_MUTEP_LOW | CS4398_PAMUTE;
426 if (chip->dac_mute)
427 reg |= CS4398_MUTE_B | CS4398_MUTE_A;
428 cs4398_write(chip, 4, reg);
429 update_cs4362a_volumes(chip);
430}
431
Clemens Ladisch75146fc2008-05-13 09:22:43 +0200432static void cs43xx_init(struct oxygen *chip)
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200433{
434 struct xonar_data *data = chip->model_data;
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200435
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200436 /* set CPEN (control port mode) and power down */
437 cs4398_write(chip, 8, CS4398_CPEN | CS4398_PDN);
438 cs4362a_write(chip, 0x01, CS4362A_PDN | CS4362A_CPEN);
439 /* configure */
Clemens Ladische58aee92008-05-13 09:20:51 +0200440 cs4398_write(chip, 2, data->cs4398_fm);
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200441 cs4398_write(chip, 3, CS4398_ATAPI_B_R | CS4398_ATAPI_A_L);
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200442 cs4398_write(chip, 7, CS4398_RMP_DN | CS4398_RMP_UP |
443 CS4398_ZERO_CROSS | CS4398_SOFT_RAMP);
444 cs4362a_write(chip, 0x02, CS4362A_DIF_LJUST);
445 cs4362a_write(chip, 0x03, CS4362A_MUTEC_6 | CS4362A_AMUTE |
446 CS4362A_RMP_UP | CS4362A_ZERO_CROSS | CS4362A_SOFT_RAMP);
447 cs4362a_write(chip, 0x04, CS4362A_RMP_DN | CS4362A_DEM_NONE);
448 cs4362a_write(chip, 0x05, 0);
Clemens Ladische58aee92008-05-13 09:20:51 +0200449 cs4362a_write(chip, 0x06, data->cs4362a_fm);
Clemens Ladische58aee92008-05-13 09:20:51 +0200450 cs4362a_write(chip, 0x09, data->cs4362a_fm);
Clemens Ladische58aee92008-05-13 09:20:51 +0200451 cs4362a_write(chip, 0x0c, data->cs4362a_fm);
Clemens Ladischbbbfb552008-05-13 09:21:48 +0200452 update_cs43xx_volume(chip);
453 update_cs43xx_mute(chip);
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200454 /* clear power down */
455 cs4398_write(chip, 8, CS4398_CPEN);
456 cs4362a_write(chip, 0x01, CS4362A_CPEN);
Clemens Ladisch75146fc2008-05-13 09:22:43 +0200457}
458
Clemens Ladisch5430c722008-07-28 10:28:43 +0200459static void xonar_d1_init(struct oxygen *chip)
Clemens Ladisch75146fc2008-05-13 09:22:43 +0200460{
461 struct xonar_data *data = chip->model_data;
462
463 data->anti_pop_delay = 800;
464 data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE;
Clemens Ladisch75146fc2008-05-13 09:22:43 +0200465 data->cs4398_fm = CS4398_FM_SINGLE | CS4398_DEM_NONE | CS4398_DIF_LJUST;
466 data->cs4362a_fm = CS4362A_FM_SINGLE |
467 CS4362A_ATAPI_B_R | CS4362A_ATAPI_A_L;
468
469 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
470 OXYGEN_2WIRE_LENGTH_8 |
471 OXYGEN_2WIRE_INTERRUPT_MASK |
472 OXYGEN_2WIRE_SPEED_FAST);
473
474 cs43xx_init(chip);
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200475
476 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
Clemens Ladischa8bb1ba2008-04-15 08:57:31 +0200477 GPIO_DX_FRONT_PANEL | GPIO_DX_INPUT_ROUTE);
478 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
479 GPIO_DX_FRONT_PANEL | GPIO_DX_INPUT_ROUTE);
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200480
481 xonar_common_init(chip);
482
483 snd_component_add(chip->card, "CS4398");
484 snd_component_add(chip->card, "CS4362A");
485 snd_component_add(chip->card, "CS5361");
486}
487
Clemens Ladisch30459d72009-02-19 08:42:44 +0100488static void xonar_dx_init(struct oxygen *chip)
489{
490 struct xonar_data *data = chip->model_data;
491
492 data->ext_power_reg = OXYGEN_GPI_DATA;
493 data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
494 data->ext_power_bit = GPI_DX_EXT_POWER;
495
496 xonar_d1_init(chip);
497}
498
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200499static void xonar_hdav_init(struct oxygen *chip)
500{
501 struct xonar_data *data = chip->model_data;
502 u8 param;
503
504 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
505 OXYGEN_2WIRE_LENGTH_8 |
506 OXYGEN_2WIRE_INTERRUPT_MASK |
507 OXYGEN_2WIRE_SPEED_FAST);
508
509 data->anti_pop_delay = 100;
Clemens Ladisch30459d72009-02-19 08:42:44 +0100510 data->dacs = chip->model.private_data == MODEL_HDAV_H6 ? 4 : 1;
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200511 data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE;
512 data->ext_power_reg = OXYGEN_GPI_DATA;
513 data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
514 data->ext_power_bit = GPI_DX_EXT_POWER;
515 data->pcm1796_oversampling = PCM1796_OS_64;
516
517 pcm1796_init(chip);
518
519 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DX_INPUT_ROUTE);
520 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_DX_INPUT_ROUTE);
521
522 oxygen_reset_uart(chip);
523 param = 0;
524 hdmi_write_command(chip, 0x61, 1, &param);
525 param = 1;
526 hdmi_write_command(chip, 0x74, 1, &param);
527 data->hdmi_params[1] = IEC958_AES3_CON_FS_48000;
528 data->hdmi_params[4] = 1;
529 hdmi_write_command(chip, 0x54, 5, data->hdmi_params);
530
531 xonar_common_init(chip);
532
533 snd_component_add(chip->card, "PCM1796");
534 snd_component_add(chip->card, "CS5381");
535}
536
Clemens Ladisch04f98902009-05-25 10:11:29 +0200537static void xonar_st_init(struct oxygen *chip)
Clemens Ladisch930738d2009-02-26 09:27:20 +0100538{
539 struct xonar_data *data = chip->model_data;
540
541 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
542 OXYGEN_2WIRE_LENGTH_8 |
543 OXYGEN_2WIRE_INTERRUPT_MASK |
544 OXYGEN_2WIRE_SPEED_FAST);
545
Clemens Ladisch04f98902009-05-25 10:11:29 +0200546 if (chip->model.private_data == MODEL_ST_H6)
547 chip->model.dac_channels = 8;
Clemens Ladisch930738d2009-02-26 09:27:20 +0100548 data->anti_pop_delay = 100;
Clemens Ladisch04f98902009-05-25 10:11:29 +0200549 data->dacs = chip->model.private_data == MODEL_ST_H6 ? 4 : 1;
Clemens Ladisch930738d2009-02-26 09:27:20 +0100550 data->output_enable_bit = GPIO_DX_OUTPUT_ENABLE;
Clemens Ladisch930738d2009-02-26 09:27:20 +0100551 data->pcm1796_oversampling = PCM1796_OS_64;
552
553 pcm1796_init(chip);
554
555 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
556 GPIO_DX_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP);
557 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
558 GPIO_DX_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP);
559
560 xonar_common_init(chip);
561
562 snd_component_add(chip->card, "PCM1792A");
563 snd_component_add(chip->card, "CS5381");
564}
565
Clemens Ladisch04f98902009-05-25 10:11:29 +0200566static void xonar_stx_init(struct oxygen *chip)
567{
568 struct xonar_data *data = chip->model_data;
569
570 data->ext_power_reg = OXYGEN_GPI_DATA;
571 data->ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
572 data->ext_power_bit = GPI_DX_EXT_POWER;
573
574 xonar_st_init(chip);
575}
576
Clemens Ladischc2bc4ff2008-09-22 09:05:29 +0200577static void xonar_disable_output(struct oxygen *chip)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100578{
Clemens Ladischaf9af172008-04-07 10:26:03 +0200579 struct xonar_data *data = chip->model_data;
580
581 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100582}
583
Clemens Ladischc2bc4ff2008-09-22 09:05:29 +0200584static void xonar_d2_cleanup(struct oxygen *chip)
585{
586 xonar_disable_output(chip);
587}
588
Clemens Ladisch5430c722008-07-28 10:28:43 +0200589static void xonar_d1_cleanup(struct oxygen *chip)
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200590{
Clemens Ladischc2bc4ff2008-09-22 09:05:29 +0200591 xonar_disable_output(chip);
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200592 cs4362a_write(chip, 0x01, CS4362A_PDN | CS4362A_CPEN);
593 oxygen_clear_bits8(chip, OXYGEN_FUNCTION, OXYGEN_FUNCTION_RESET_CODEC);
594}
595
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200596static void xonar_hdav_cleanup(struct oxygen *chip)
597{
598 u8 param = 0;
599
600 hdmi_write_command(chip, 0x74, 1, &param);
601 xonar_disable_output(chip);
602}
603
Clemens Ladisch930738d2009-02-26 09:27:20 +0100604static void xonar_st_cleanup(struct oxygen *chip)
605{
606 xonar_disable_output(chip);
607}
608
Clemens Ladischc2bc4ff2008-09-22 09:05:29 +0200609static void xonar_d2_suspend(struct oxygen *chip)
610{
611 xonar_d2_cleanup(chip);
612}
613
614static void xonar_d1_suspend(struct oxygen *chip)
615{
616 xonar_d1_cleanup(chip);
617}
618
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200619static void xonar_hdav_suspend(struct oxygen *chip)
620{
621 xonar_hdav_cleanup(chip);
622 msleep(2);
623}
624
Clemens Ladisch930738d2009-02-26 09:27:20 +0100625static void xonar_st_suspend(struct oxygen *chip)
626{
627 xonar_st_cleanup(chip);
628}
629
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200630static void xonar_d2_resume(struct oxygen *chip)
631{
632 pcm1796_init(chip);
633 xonar_enable_output(chip);
634}
635
Clemens Ladisch5430c722008-07-28 10:28:43 +0200636static void xonar_d1_resume(struct oxygen *chip)
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200637{
Clemens Ladisch82639072009-07-02 08:31:30 +0200638 oxygen_set_bits8(chip, OXYGEN_FUNCTION, OXYGEN_FUNCTION_RESET_CODEC);
639 msleep(1);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200640 cs43xx_init(chip);
641 xonar_enable_output(chip);
642}
643
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200644static void xonar_hdav_resume(struct oxygen *chip)
645{
646 struct xonar_data *data = chip->model_data;
647 u8 param;
648
649 oxygen_reset_uart(chip);
650 param = 0;
651 hdmi_write_command(chip, 0x61, 1, &param);
652 param = 1;
653 hdmi_write_command(chip, 0x74, 1, &param);
654 hdmi_write_command(chip, 0x54, 5, data->hdmi_params);
655 pcm1796_init(chip);
656 xonar_enable_output(chip);
657}
658
Clemens Ladisch930738d2009-02-26 09:27:20 +0100659static void xonar_st_resume(struct oxygen *chip)
660{
661 pcm1796_init(chip);
662 xonar_enable_output(chip);
663}
664
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200665static void xonar_hdav_pcm_hardware_filter(unsigned int channel,
666 struct snd_pcm_hardware *hardware)
667{
668 if (channel == PCM_MULTICH) {
669 hardware->rates = SNDRV_PCM_RATE_44100 |
670 SNDRV_PCM_RATE_48000 |
671 SNDRV_PCM_RATE_96000 |
672 SNDRV_PCM_RATE_192000;
673 hardware->rate_min = 44100;
674 }
675}
676
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100677static void set_pcm1796_params(struct oxygen *chip,
678 struct snd_pcm_hw_params *params)
679{
Clemens Ladische58aee92008-05-13 09:20:51 +0200680 struct xonar_data *data = chip->model_data;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100681 unsigned int i;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100682
Clemens Ladische58aee92008-05-13 09:20:51 +0200683 data->pcm1796_oversampling =
684 params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
Clemens Ladisch70c27d32008-09-22 09:06:38 +0200685 for (i = 0; i < data->dacs; ++i)
Clemens Ladische58aee92008-05-13 09:20:51 +0200686 pcm1796_write(chip, i, 20, data->pcm1796_oversampling);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100687}
688
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200689static void set_cs53x1_params(struct oxygen *chip,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100690 struct snd_pcm_hw_params *params)
691{
692 unsigned int value;
693
694 if (params_rate(params) <= 54000)
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200695 value = GPIO_CS53x1_M_SINGLE;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100696 else if (params_rate(params) <= 108000)
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200697 value = GPIO_CS53x1_M_DOUBLE;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100698 else
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200699 value = GPIO_CS53x1_M_QUAD;
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100700 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200701 value, GPIO_CS53x1_M_MASK);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100702}
703
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200704static void set_cs43xx_params(struct oxygen *chip,
705 struct snd_pcm_hw_params *params)
706{
Clemens Ladische58aee92008-05-13 09:20:51 +0200707 struct xonar_data *data = chip->model_data;
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200708
Clemens Ladische58aee92008-05-13 09:20:51 +0200709 data->cs4398_fm = CS4398_DEM_NONE | CS4398_DIF_LJUST;
710 data->cs4362a_fm = CS4362A_ATAPI_B_R | CS4362A_ATAPI_A_L;
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200711 if (params_rate(params) <= 50000) {
Clemens Ladische58aee92008-05-13 09:20:51 +0200712 data->cs4398_fm |= CS4398_FM_SINGLE;
713 data->cs4362a_fm |= CS4362A_FM_SINGLE;
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200714 } else if (params_rate(params) <= 100000) {
Clemens Ladische58aee92008-05-13 09:20:51 +0200715 data->cs4398_fm |= CS4398_FM_DOUBLE;
716 data->cs4362a_fm |= CS4362A_FM_DOUBLE;
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200717 } else {
Clemens Ladische58aee92008-05-13 09:20:51 +0200718 data->cs4398_fm |= CS4398_FM_QUAD;
719 data->cs4362a_fm |= CS4362A_FM_QUAD;
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200720 }
Clemens Ladische58aee92008-05-13 09:20:51 +0200721 cs4398_write(chip, 2, data->cs4398_fm);
722 cs4362a_write(chip, 0x06, data->cs4362a_fm);
723 cs4362a_write(chip, 0x09, data->cs4362a_fm);
724 cs4362a_write(chip, 0x0c, data->cs4362a_fm);
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200725}
726
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200727static void set_hdmi_params(struct oxygen *chip,
728 struct snd_pcm_hw_params *params)
729{
730 struct xonar_data *data = chip->model_data;
731
732 data->hdmi_params[0] = 0; /* 1 = non-audio */
733 switch (params_rate(params)) {
734 case 44100:
735 data->hdmi_params[1] = IEC958_AES3_CON_FS_44100;
736 break;
737 case 48000:
738 data->hdmi_params[1] = IEC958_AES3_CON_FS_48000;
739 break;
740 default: /* 96000 */
741 data->hdmi_params[1] = IEC958_AES3_CON_FS_96000;
742 break;
743 case 192000:
744 data->hdmi_params[1] = IEC958_AES3_CON_FS_192000;
745 break;
746 }
747 data->hdmi_params[2] = params_channels(params) / 2 - 1;
748 if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE)
749 data->hdmi_params[3] = 0;
750 else
751 data->hdmi_params[3] = 0xc0;
752 data->hdmi_params[4] = 1; /* ? */
753 hdmi_write_command(chip, 0x54, 5, data->hdmi_params);
754}
755
756static void set_hdav_params(struct oxygen *chip,
757 struct snd_pcm_hw_params *params)
758{
759 set_pcm1796_params(chip, params);
760 set_hdmi_params(chip, params);
761}
762
Clemens Ladisch7c014152008-01-28 08:36:55 +0100763static void xonar_gpio_changed(struct oxygen *chip)
764{
765 struct xonar_data *data = chip->model_data;
766 u8 has_power;
767
Clemens Ladischaf9af172008-04-07 10:26:03 +0200768 has_power = !!(oxygen_read8(chip, data->ext_power_reg)
769 & data->ext_power_bit);
Clemens Ladisch7c014152008-01-28 08:36:55 +0100770 if (has_power != data->has_power) {
771 data->has_power = has_power;
772 if (has_power) {
773 snd_printk(KERN_NOTICE "power restored\n");
774 } else {
775 snd_printk(KERN_CRIT
776 "Hey! Don't unplug the power cable!\n");
777 /* TODO: stop PCMs */
778 }
779 }
780}
781
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200782static void xonar_hdav_uart_input(struct oxygen *chip)
783{
784 if (chip->uart_input_count >= 2 &&
785 chip->uart_input[chip->uart_input_count - 2] == 'O' &&
786 chip->uart_input[chip->uart_input_count - 1] == 'K') {
Clemens Ladisch45bf8102009-01-19 10:07:58 +0100787 printk(KERN_DEBUG "message from Xonar HDAV HDMI chip received:\n");
Clemens Ladisch7407a2e2008-09-22 09:12:11 +0200788 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
789 chip->uart_input, chip->uart_input_count);
790 chip->uart_input_count = 0;
791 }
792}
793
Clemens Ladischc3f00732008-09-22 09:01:11 +0200794static int gpio_bit_switch_get(struct snd_kcontrol *ctl,
795 struct snd_ctl_elem_value *value)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100796{
797 struct oxygen *chip = ctl->private_data;
Clemens Ladischc3f00732008-09-22 09:01:11 +0200798 u16 bit = ctl->private_value;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100799
800 value->value.integer.value[0] =
Clemens Ladischc3f00732008-09-22 09:01:11 +0200801 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & bit);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100802 return 0;
803}
804
Clemens Ladischc3f00732008-09-22 09:01:11 +0200805static int gpio_bit_switch_put(struct snd_kcontrol *ctl,
806 struct snd_ctl_elem_value *value)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100807{
808 struct oxygen *chip = ctl->private_data;
Clemens Ladischc3f00732008-09-22 09:01:11 +0200809 u16 bit = ctl->private_value;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100810 u16 old_bits, new_bits;
811 int changed;
812
813 spin_lock_irq(&chip->reg_lock);
814 old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
815 if (value->value.integer.value[0])
Clemens Ladischc3f00732008-09-22 09:01:11 +0200816 new_bits = old_bits | bit;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100817 else
Clemens Ladischc3f00732008-09-22 09:01:11 +0200818 new_bits = old_bits & ~bit;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100819 changed = new_bits != old_bits;
820 if (changed)
821 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
822 spin_unlock_irq(&chip->reg_lock);
823 return changed;
824}
825
826static const struct snd_kcontrol_new alt_switch = {
827 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
828 .name = "Analog Loopback Switch",
829 .info = snd_ctl_boolean_mono_info,
Clemens Ladischc3f00732008-09-22 09:01:11 +0200830 .get = gpio_bit_switch_get,
831 .put = gpio_bit_switch_put,
832 .private_value = GPIO_D2_ALT,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100833};
834
Clemens Ladischa8bb1ba2008-04-15 08:57:31 +0200835static const struct snd_kcontrol_new front_panel_switch = {
Clemens Ladisch387fb6a2008-04-11 10:24:48 +0200836 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischa8bb1ba2008-04-15 08:57:31 +0200837 .name = "Front Panel Switch",
838 .info = snd_ctl_boolean_mono_info,
Clemens Ladischc3f00732008-09-22 09:01:11 +0200839 .get = gpio_bit_switch_get,
840 .put = gpio_bit_switch_put,
841 .private_value = GPIO_DX_FRONT_PANEL,
Clemens Ladisch387fb6a2008-04-11 10:24:48 +0200842};
843
Clemens Ladisch930738d2009-02-26 09:27:20 +0100844static int st_output_switch_info(struct snd_kcontrol *ctl,
845 struct snd_ctl_elem_info *info)
846{
847 static const char *const names[3] = {
848 "Speakers", "Headphones", "FP Headphones"
849 };
850
851 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
852 info->count = 1;
853 info->value.enumerated.items = 3;
854 if (info->value.enumerated.item >= 3)
855 info->value.enumerated.item = 2;
856 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
857 return 0;
858}
859
860static int st_output_switch_get(struct snd_kcontrol *ctl,
861 struct snd_ctl_elem_value *value)
862{
863 struct oxygen *chip = ctl->private_data;
864 u16 gpio;
865
866 gpio = oxygen_read16(chip, OXYGEN_GPIO_DATA);
867 if (!(gpio & GPIO_ST_HP))
868 value->value.enumerated.item[0] = 0;
869 else if (gpio & GPIO_ST_HP_REAR)
870 value->value.enumerated.item[0] = 1;
871 else
872 value->value.enumerated.item[0] = 2;
873 return 0;
874}
875
876
877static int st_output_switch_put(struct snd_kcontrol *ctl,
878 struct snd_ctl_elem_value *value)
879{
880 struct oxygen *chip = ctl->private_data;
881 u16 gpio_old, gpio;
882
883 mutex_lock(&chip->mutex);
884 gpio_old = oxygen_read16(chip, OXYGEN_GPIO_DATA);
885 gpio = gpio_old;
886 switch (value->value.enumerated.item[0]) {
887 case 0:
888 gpio &= ~(GPIO_ST_HP | GPIO_ST_HP_REAR);
889 break;
890 case 1:
891 gpio |= GPIO_ST_HP | GPIO_ST_HP_REAR;
892 break;
893 case 2:
894 gpio = (gpio | GPIO_ST_HP) & ~GPIO_ST_HP_REAR;
895 break;
896 }
897 oxygen_write16(chip, OXYGEN_GPIO_DATA, gpio);
898 mutex_unlock(&chip->mutex);
899 return gpio != gpio_old;
900}
901
902static const struct snd_kcontrol_new st_output_switch = {
903 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
904 .name = "Analog Output",
905 .info = st_output_switch_info,
906 .get = st_output_switch_get,
907 .put = st_output_switch_put,
908};
909
Clemens Ladischc871c4a2008-09-22 09:07:20 +0200910static void xonar_line_mic_ac97_switch(struct oxygen *chip,
911 unsigned int reg, unsigned int mute)
Clemens Ladisch11864b42008-04-09 09:16:14 +0200912{
913 if (reg == AC97_LINE) {
914 spin_lock_irq(&chip->reg_lock);
915 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
916 mute ? GPIO_DX_INPUT_ROUTE : 0,
917 GPIO_DX_INPUT_ROUTE);
918 spin_unlock_irq(&chip->reg_lock);
919 }
920}
921
Clemens Ladischeacbb9d2009-02-20 09:33:40 +0100922static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -6000, 50, 0);
923static const DECLARE_TLV_DB_SCALE(cs4362a_db_scale, -6000, 100, 0);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100924
Clemens Ladischaf9af172008-04-07 10:26:03 +0200925static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100926{
Clemens Ladisch4972a172008-04-16 09:15:45 +0200927 if (!strncmp(template->name, "CD Capture ", 11))
Clemens Ladisch911b4992008-01-28 08:33:44 +0100928 /* CD in is actually connected to the video in pin */
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100929 template->private_value ^= AC97_CD ^ AC97_VIDEO;
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100930 return 0;
931}
932
Clemens Ladisch5430c722008-07-28 10:28:43 +0200933static int xonar_d1_control_filter(struct snd_kcontrol_new *template)
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200934{
Clemens Ladisch4972a172008-04-16 09:15:45 +0200935 if (!strncmp(template->name, "CD Capture ", 11))
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200936 return 1; /* no CD input */
Clemens Ladischa9d3cc42008-04-07 10:29:44 +0200937 return 0;
938}
939
Clemens Ladisch930738d2009-02-26 09:27:20 +0100940static int xonar_st_control_filter(struct snd_kcontrol_new *template)
941{
942 if (!strncmp(template->name, "CD Capture ", 11))
943 return 1; /* no CD input */
944 if (!strcmp(template->name, "Stereo Upmixing"))
945 return 1; /* stereo only - we don't need upmixing */
946 return 0;
947}
948
Clemens Ladischfe106622008-09-22 09:00:30 +0200949static int xonar_d2_mixer_init(struct oxygen *chip)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100950{
951 return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
952}
953
Clemens Ladisch5430c722008-07-28 10:28:43 +0200954static int xonar_d1_mixer_init(struct oxygen *chip)
Clemens Ladisch387fb6a2008-04-11 10:24:48 +0200955{
Clemens Ladischa8bb1ba2008-04-15 08:57:31 +0200956 return snd_ctl_add(chip->card, snd_ctl_new1(&front_panel_switch, chip));
Clemens Ladisch387fb6a2008-04-11 10:24:48 +0200957}
958
Clemens Ladisch930738d2009-02-26 09:27:20 +0100959static int xonar_st_mixer_init(struct oxygen *chip)
960{
961 return snd_ctl_add(chip->card, snd_ctl_new1(&st_output_switch, chip));
962}
963
Clemens Ladischfe106622008-09-22 09:00:30 +0200964static const struct oxygen_model model_xonar_d2 = {
965 .longname = "Asus Virtuoso 200",
966 .chip = "AV200",
Clemens Ladischfe106622008-09-22 09:00:30 +0200967 .init = xonar_d2_init,
968 .control_filter = xonar_d2_control_filter,
969 .mixer_init = xonar_d2_mixer_init,
Clemens Ladischc2bc4ff2008-09-22 09:05:29 +0200970 .cleanup = xonar_d2_cleanup,
971 .suspend = xonar_d2_suspend,
Clemens Ladischfe106622008-09-22 09:00:30 +0200972 .resume = xonar_d2_resume,
973 .set_dac_params = set_pcm1796_params,
974 .set_adc_params = set_cs53x1_params,
975 .update_dac_volume = update_pcm1796_volume,
976 .update_dac_mute = update_pcm1796_mute,
977 .dac_tlv = pcm1796_db_scale,
978 .model_data_size = sizeof(struct xonar_data),
Clemens Ladischd76596b2008-09-22 09:02:08 +0200979 .device_config = PLAYBACK_0_TO_I2S |
980 PLAYBACK_1_TO_SPDIF |
981 CAPTURE_0_FROM_I2S_2 |
Clemens Ladischdbbbd672008-09-22 09:03:42 +0200982 CAPTURE_1_FROM_SPDIF |
983 MIDI_OUTPUT |
984 MIDI_INPUT,
Clemens Ladischfe106622008-09-22 09:00:30 +0200985 .dac_channels = 8,
Clemens Ladischeacbb9d2009-02-20 09:33:40 +0100986 .dac_volume_min = 255 - 2*60,
987 .dac_volume_max = 255,
Clemens Ladischfe106622008-09-22 09:00:30 +0200988 .misc_flags = OXYGEN_MISC_MIDI,
Clemens Ladisch6ce6c472009-02-17 09:50:30 +0100989 .function_flags = OXYGEN_FUNCTION_SPI |
990 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
991 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladischfe106622008-09-22 09:00:30 +0200992 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
993};
994
995static const struct oxygen_model model_xonar_d1 = {
996 .longname = "Asus Virtuoso 100",
997 .chip = "AV200",
Clemens Ladischfe106622008-09-22 09:00:30 +0200998 .init = xonar_d1_init,
999 .control_filter = xonar_d1_control_filter,
1000 .mixer_init = xonar_d1_mixer_init,
1001 .cleanup = xonar_d1_cleanup,
Clemens Ladischc2bc4ff2008-09-22 09:05:29 +02001002 .suspend = xonar_d1_suspend,
Clemens Ladischfe106622008-09-22 09:00:30 +02001003 .resume = xonar_d1_resume,
1004 .set_dac_params = set_cs43xx_params,
1005 .set_adc_params = set_cs53x1_params,
1006 .update_dac_volume = update_cs43xx_volume,
1007 .update_dac_mute = update_cs43xx_mute,
Clemens Ladischc871c4a2008-09-22 09:07:20 +02001008 .ac97_switch = xonar_line_mic_ac97_switch,
Clemens Ladischfe106622008-09-22 09:00:30 +02001009 .dac_tlv = cs4362a_db_scale,
1010 .model_data_size = sizeof(struct xonar_data),
Clemens Ladischd76596b2008-09-22 09:02:08 +02001011 .device_config = PLAYBACK_0_TO_I2S |
1012 PLAYBACK_1_TO_SPDIF |
1013 CAPTURE_0_FROM_I2S_2,
Clemens Ladischfe106622008-09-22 09:00:30 +02001014 .dac_channels = 8,
Clemens Ladischeacbb9d2009-02-20 09:33:40 +01001015 .dac_volume_min = 127 - 60,
Clemens Ladischfe106622008-09-22 09:00:30 +02001016 .dac_volume_max = 127,
1017 .function_flags = OXYGEN_FUNCTION_2WIRE,
1018 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1019 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +01001020};
1021
Clemens Ladisch7407a2e2008-09-22 09:12:11 +02001022static const struct oxygen_model model_xonar_hdav = {
1023 .longname = "Asus Virtuoso 200",
1024 .chip = "AV200",
Clemens Ladisch7407a2e2008-09-22 09:12:11 +02001025 .init = xonar_hdav_init,
1026 .cleanup = xonar_hdav_cleanup,
1027 .suspend = xonar_hdav_suspend,
1028 .resume = xonar_hdav_resume,
1029 .pcm_hardware_filter = xonar_hdav_pcm_hardware_filter,
1030 .set_dac_params = set_hdav_params,
1031 .set_adc_params = set_cs53x1_params,
1032 .update_dac_volume = update_pcm1796_volume,
1033 .update_dac_mute = update_pcm1796_mute,
1034 .uart_input = xonar_hdav_uart_input,
1035 .ac97_switch = xonar_line_mic_ac97_switch,
1036 .dac_tlv = pcm1796_db_scale,
1037 .model_data_size = sizeof(struct xonar_data),
1038 .device_config = PLAYBACK_0_TO_I2S |
1039 PLAYBACK_1_TO_SPDIF |
Clemens Ladischb990ae92009-05-25 10:06:22 +02001040 CAPTURE_0_FROM_I2S_2 |
1041 CAPTURE_1_FROM_SPDIF,
Clemens Ladisch7407a2e2008-09-22 09:12:11 +02001042 .dac_channels = 8,
Clemens Ladischeacbb9d2009-02-20 09:33:40 +01001043 .dac_volume_min = 255 - 2*60,
1044 .dac_volume_max = 255,
Clemens Ladisch22c73372009-01-19 10:07:21 +01001045 .misc_flags = OXYGEN_MISC_MIDI,
Clemens Ladisch7407a2e2008-09-22 09:12:11 +02001046 .function_flags = OXYGEN_FUNCTION_2WIRE,
1047 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1048 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1049};
1050
Clemens Ladisch930738d2009-02-26 09:27:20 +01001051static const struct oxygen_model model_xonar_st = {
1052 .longname = "Asus Virtuoso 100",
1053 .chip = "AV200",
Clemens Ladisch04f98902009-05-25 10:11:29 +02001054 .init = xonar_st_init,
Clemens Ladisch930738d2009-02-26 09:27:20 +01001055 .control_filter = xonar_st_control_filter,
1056 .mixer_init = xonar_st_mixer_init,
1057 .cleanup = xonar_st_cleanup,
1058 .suspend = xonar_st_suspend,
1059 .resume = xonar_st_resume,
1060 .set_dac_params = set_pcm1796_params,
1061 .set_adc_params = set_cs53x1_params,
1062 .update_dac_volume = update_pcm1796_volume,
1063 .update_dac_mute = update_pcm1796_mute,
1064 .ac97_switch = xonar_line_mic_ac97_switch,
1065 .dac_tlv = pcm1796_db_scale,
1066 .model_data_size = sizeof(struct xonar_data),
1067 .device_config = PLAYBACK_0_TO_I2S |
1068 PLAYBACK_1_TO_SPDIF |
1069 CAPTURE_0_FROM_I2S_2,
1070 .dac_channels = 2,
1071 .dac_volume_min = 255 - 2*60,
1072 .dac_volume_max = 255,
1073 .function_flags = OXYGEN_FUNCTION_2WIRE,
1074 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1075 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
1076};
1077
Clemens Ladisch30459d72009-02-19 08:42:44 +01001078static int __devinit get_xonar_model(struct oxygen *chip,
1079 const struct pci_device_id *id)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +01001080{
Clemens Ladischfe106622008-09-22 09:00:30 +02001081 static const struct oxygen_model *const models[] = {
1082 [MODEL_D1] = &model_xonar_d1,
1083 [MODEL_DX] = &model_xonar_d1,
1084 [MODEL_D2] = &model_xonar_d2,
1085 [MODEL_D2X] = &model_xonar_d2,
Clemens Ladisch7407a2e2008-09-22 09:12:11 +02001086 [MODEL_HDAV] = &model_xonar_hdav,
Clemens Ladisch04f98902009-05-25 10:11:29 +02001087 [MODEL_ST] = &model_xonar_st,
Clemens Ladisch930738d2009-02-26 09:27:20 +01001088 [MODEL_STX] = &model_xonar_st,
Clemens Ladischfe106622008-09-22 09:00:30 +02001089 };
Clemens Ladisch30459d72009-02-19 08:42:44 +01001090 static const char *const names[] = {
1091 [MODEL_D1] = "Xonar D1",
1092 [MODEL_DX] = "Xonar DX",
1093 [MODEL_D2] = "Xonar D2",
1094 [MODEL_D2X] = "Xonar D2X",
1095 [MODEL_HDAV] = "Xonar HDAV1.3",
1096 [MODEL_HDAV_H6] = "Xonar HDAV1.3+H6",
Clemens Ladisch04f98902009-05-25 10:11:29 +02001097 [MODEL_ST] = "Xonar Essence ST",
1098 [MODEL_ST_H6] = "Xonar Essence ST+H6",
Clemens Ladisch930738d2009-02-26 09:27:20 +01001099 [MODEL_STX] = "Xonar Essence STX",
Clemens Ladisch30459d72009-02-19 08:42:44 +01001100 };
1101 unsigned int model = id->driver_data;
1102
1103 if (model >= ARRAY_SIZE(models) || !models[model])
1104 return -EINVAL;
1105 chip->model = *models[model];
1106
1107 switch (model) {
1108 case MODEL_D2X:
1109 chip->model.init = xonar_d2x_init;
1110 break;
1111 case MODEL_DX:
1112 chip->model.init = xonar_dx_init;
1113 break;
1114 case MODEL_HDAV:
Clemens Ladisch04f98902009-05-25 10:11:29 +02001115 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
1116 switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
1117 case GPIO_DB_H6:
Clemens Ladisch30459d72009-02-19 08:42:44 +01001118 model = MODEL_HDAV_H6;
1119 break;
Clemens Ladisch04f98902009-05-25 10:11:29 +02001120 case GPIO_DB_XX:
Clemens Ladisch30459d72009-02-19 08:42:44 +01001121 snd_printk(KERN_ERR "unknown daughterboard\n");
1122 return -ENODEV;
1123 }
1124 break;
Clemens Ladisch04f98902009-05-25 10:11:29 +02001125 case MODEL_ST:
1126 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
1127 switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
1128 case GPIO_DB_H6:
1129 model = MODEL_ST_H6;
1130 break;
1131 }
1132 break;
Clemens Ladisch930738d2009-02-26 09:27:20 +01001133 case MODEL_STX:
Clemens Ladisch04f98902009-05-25 10:11:29 +02001134 chip->model.init = xonar_stx_init;
1135 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
Clemens Ladisch930738d2009-02-26 09:27:20 +01001136 break;
Clemens Ladisch30459d72009-02-19 08:42:44 +01001137 }
1138
1139 chip->model.shortname = names[model];
1140 chip->model.private_data = model;
1141 return 0;
1142}
1143
1144static int __devinit xonar_probe(struct pci_dev *pci,
1145 const struct pci_device_id *pci_id)
1146{
Clemens Ladisch1b8ff222007-12-23 19:52:08 +01001147 static int dev;
1148 int err;
1149
1150 if (dev >= SNDRV_CARDS)
1151 return -ENODEV;
1152 if (!enable[dev]) {
1153 ++dev;
1154 return -ENOENT;
1155 }
Clemens Ladischbb718582009-02-19 08:37:13 +01001156 err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE,
Clemens Ladisch30459d72009-02-19 08:42:44 +01001157 xonar_ids, get_xonar_model);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +01001158 if (err >= 0)
1159 ++dev;
1160 return err;
1161}
1162
1163static struct pci_driver xonar_driver = {
1164 .name = "AV200",
1165 .id_table = xonar_ids,
1166 .probe = xonar_probe,
1167 .remove = __devexit_p(oxygen_pci_remove),
Clemens Ladisch4a4bc532008-05-13 09:24:39 +02001168#ifdef CONFIG_PM
1169 .suspend = oxygen_pci_suspend,
1170 .resume = oxygen_pci_resume,
1171#endif
Clemens Ladisch1b8ff222007-12-23 19:52:08 +01001172};
1173
1174static int __init alsa_card_xonar_init(void)
1175{
1176 return pci_register_driver(&xonar_driver);
1177}
1178
1179static void __exit alsa_card_xonar_exit(void)
1180{
1181 pci_unregister_driver(&xonar_driver);
1182}
1183
1184module_init(alsa_card_xonar_init)
1185module_exit(alsa_card_xonar_exit)