blob: 2d7b96e3f49ff29eb88607f8057368d69526b754 [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 Ladisch878ac3e2008-01-21 08:50:19 +010021 * CMI8788:
22 *
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010023 * SPI 0 -> 1st PCM1796 (front)
Clemens Ladisch7113e952008-01-14 08:55:03 +010024 * SPI 1 -> 2nd PCM1796 (surround)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010025 * SPI 2 -> 3rd PCM1796 (center/LFE)
Clemens Ladisch7113e952008-01-14 08:55:03 +010026 * SPI 4 -> 4th PCM1796 (back)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010027 *
28 * GPIO 2 -> M0 of CS5381
29 * GPIO 3 -> M1 of CS5381
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010030 * GPIO 5 <- external power present (D2X only)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010031 * GPIO 7 -> ALT
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010032 * GPIO 8 -> enable output to speakers
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010033 */
34
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010035#include <linux/pci.h>
36#include <linux/delay.h>
Clemens Ladischccc80fb2008-01-16 08:32:08 +010037#include <linux/mutex.h>
38#include <sound/ac97_codec.h>
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010039#include <sound/control.h>
40#include <sound/core.h>
41#include <sound/initval.h>
42#include <sound/pcm.h>
43#include <sound/tlv.h>
44#include "oxygen.h"
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010045#include "cm9780.h"
Clemens Ladisch33fa7242008-03-19 08:16:40 +010046#include "pcm1796.h"
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010047
48MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
49MODULE_DESCRIPTION("Asus AV200 driver");
50MODULE_LICENSE("GPL");
51MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
52
53static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
54static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
55static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
56
57module_param_array(index, int, NULL, 0444);
58MODULE_PARM_DESC(index, "card index");
59module_param_array(id, charp, NULL, 0444);
60MODULE_PARM_DESC(id, "ID string");
61module_param_array(enable, bool, NULL, 0444);
62MODULE_PARM_DESC(enable, "enable card");
63
Clemens Ladisch271ebfc2008-04-07 10:24:22 +020064enum {
65 MODEL_D2,
66 MODEL_D2X,
67};
68
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010069static struct pci_device_id xonar_ids[] __devinitdata = {
Clemens Ladisch271ebfc2008-04-07 10:24:22 +020070 { OXYGEN_PCI_SUBID(0x1043, 0x8269), .driver_data = MODEL_D2 },
71 { OXYGEN_PCI_SUBID(0x1043, 0x82b7), .driver_data = MODEL_D2X },
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010072 { }
73};
74MODULE_DEVICE_TABLE(pci, xonar_ids);
75
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010076
Clemens Ladischa694a6a2008-04-07 10:25:30 +020077#define GPIO_CS53x1_M_MASK 0x000c
78#define GPIO_CS53x1_M_SINGLE 0x0000
79#define GPIO_CS53x1_M_DOUBLE 0x0004
80#define GPIO_CS53x1_M_QUAD 0x0008
81
Clemens Ladischaf9af172008-04-07 10:26:03 +020082#define GPIO_D2X_EXT_POWER 0x0020
83#define GPIO_D2_ALT 0x0080
84#define GPIO_D2_OUTPUT_ENABLE 0x0100
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010085
Clemens Ladisch7c014152008-01-28 08:36:55 +010086struct xonar_data {
Clemens Ladischaf9af172008-04-07 10:26:03 +020087 unsigned int anti_pop_delay;
88 u16 output_enable_bit;
89 u8 ext_power_reg;
90 u8 ext_power_int_reg;
91 u8 ext_power_bit;
Clemens Ladisch7c014152008-01-28 08:36:55 +010092 u8 has_power;
93};
94
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010095static void pcm1796_write(struct oxygen *chip, unsigned int codec,
96 u8 reg, u8 value)
97{
98 /* maps ALSA channel pair number to SPI output */
99 static const u8 codec_map[4] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +0100100 0, 1, 2, 4
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100101 };
Clemens Ladischc2353a02008-01-18 09:17:53 +0100102 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100103 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100104 OXYGEN_SPI_CLOCK_160 |
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100105 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100106 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100107 (reg << 8) | value);
108}
109
Clemens Ladischaf9af172008-04-07 10:26:03 +0200110static void xonar_common_init(struct oxygen *chip)
111{
112 struct xonar_data *data = chip->model_data;
113
114 if (data->ext_power_reg) {
115 oxygen_set_bits8(chip, data->ext_power_int_reg,
116 data->ext_power_bit);
117 chip->interrupt_mask |= OXYGEN_INT_GPIO;
118 data->has_power = !!(oxygen_read8(chip, data->ext_power_reg)
119 & data->ext_power_bit);
120 }
121 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_CS53x1_M_MASK);
122 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
123 GPIO_CS53x1_M_SINGLE, GPIO_CS53x1_M_MASK);
124 oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
125 msleep(data->anti_pop_delay);
126 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, data->output_enable_bit);
127 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
128}
129
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200130static void xonar_d2_init(struct oxygen *chip)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100131{
Clemens Ladischaf9af172008-04-07 10:26:03 +0200132 struct xonar_data *data = chip->model_data;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100133 unsigned int i;
134
Clemens Ladischaf9af172008-04-07 10:26:03 +0200135 data->anti_pop_delay = 300;
136 data->output_enable_bit = GPIO_D2_OUTPUT_ENABLE;
137
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100138 for (i = 0; i < 4; ++i) {
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100139 pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
140 pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
141 pcm1796_write(chip, i, 20, PCM1796_OS_64);
142 pcm1796_write(chip, i, 21, 0);
143 pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
144 pcm1796_write(chip, i, 17, 0xff);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100145 }
146
Clemens Ladischaf9af172008-04-07 10:26:03 +0200147 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT);
148 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT);
149
150 xonar_common_init(chip);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100151
152 snd_component_add(chip->card, "PCM1796");
153 snd_component_add(chip->card, "CS5381");
154}
155
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200156static void xonar_d2x_init(struct oxygen *chip)
157{
158 struct xonar_data *data = chip->model_data;
159
Clemens Ladischaf9af172008-04-07 10:26:03 +0200160 data->ext_power_reg = OXYGEN_GPIO_DATA;
161 data->ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK;
162 data->ext_power_bit = GPIO_D2X_EXT_POWER;
163 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2X_EXT_POWER);
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200164 xonar_d2_init(chip);
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200165}
166
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100167static void xonar_cleanup(struct oxygen *chip)
168{
Clemens Ladischaf9af172008-04-07 10:26:03 +0200169 struct xonar_data *data = chip->model_data;
170
171 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100172}
173
174static void set_pcm1796_params(struct oxygen *chip,
175 struct snd_pcm_hw_params *params)
176{
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100177 unsigned int i;
178 u8 value;
179
180 value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
181 for (i = 0; i < 4; ++i)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100182 pcm1796_write(chip, i, 20, value);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100183}
184
185static void update_pcm1796_volume(struct oxygen *chip)
186{
187 unsigned int i;
188
189 for (i = 0; i < 4; ++i) {
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100190 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
191 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100192 }
193}
194
195static void update_pcm1796_mute(struct oxygen *chip)
196{
197 unsigned int i;
198 u8 value;
199
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100200 value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100201 if (chip->dac_mute)
202 value |= PCM1796_MUTE;
203 for (i = 0; i < 4; ++i)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100204 pcm1796_write(chip, i, 18, value);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100205}
206
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200207static void set_cs53x1_params(struct oxygen *chip,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100208 struct snd_pcm_hw_params *params)
209{
210 unsigned int value;
211
212 if (params_rate(params) <= 54000)
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200213 value = GPIO_CS53x1_M_SINGLE;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100214 else if (params_rate(params) <= 108000)
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200215 value = GPIO_CS53x1_M_DOUBLE;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100216 else
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200217 value = GPIO_CS53x1_M_QUAD;
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100218 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200219 value, GPIO_CS53x1_M_MASK);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100220}
221
Clemens Ladisch7c014152008-01-28 08:36:55 +0100222static void xonar_gpio_changed(struct oxygen *chip)
223{
224 struct xonar_data *data = chip->model_data;
225 u8 has_power;
226
Clemens Ladischaf9af172008-04-07 10:26:03 +0200227 has_power = !!(oxygen_read8(chip, data->ext_power_reg)
228 & data->ext_power_bit);
Clemens Ladisch7c014152008-01-28 08:36:55 +0100229 if (has_power != data->has_power) {
230 data->has_power = has_power;
231 if (has_power) {
232 snd_printk(KERN_NOTICE "power restored\n");
233 } else {
234 snd_printk(KERN_CRIT
235 "Hey! Don't unplug the power cable!\n");
236 /* TODO: stop PCMs */
237 }
238 }
239}
240
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100241static int pcm1796_volume_info(struct snd_kcontrol *ctl,
242 struct snd_ctl_elem_info *info)
243{
244 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
245 info->count = 8;
246 info->value.integer.min = 0x0f;
247 info->value.integer.max = 0xff;
248 return 0;
249}
250
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100251static int alt_switch_get(struct snd_kcontrol *ctl,
252 struct snd_ctl_elem_value *value)
253{
254 struct oxygen *chip = ctl->private_data;
255
256 value->value.integer.value[0] =
Clemens Ladischaf9af172008-04-07 10:26:03 +0200257 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_D2_ALT);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100258 return 0;
259}
260
261static int alt_switch_put(struct snd_kcontrol *ctl,
262 struct snd_ctl_elem_value *value)
263{
264 struct oxygen *chip = ctl->private_data;
265 u16 old_bits, new_bits;
266 int changed;
267
268 spin_lock_irq(&chip->reg_lock);
269 old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
270 if (value->value.integer.value[0])
Clemens Ladischaf9af172008-04-07 10:26:03 +0200271 new_bits = old_bits | GPIO_D2_ALT;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100272 else
Clemens Ladischaf9af172008-04-07 10:26:03 +0200273 new_bits = old_bits & ~GPIO_D2_ALT;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100274 changed = new_bits != old_bits;
275 if (changed)
276 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
277 spin_unlock_irq(&chip->reg_lock);
278 return changed;
279}
280
281static const struct snd_kcontrol_new alt_switch = {
282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
283 .name = "Analog Loopback Switch",
284 .info = snd_ctl_boolean_mono_info,
285 .get = alt_switch_get,
286 .put = alt_switch_put,
287};
288
289static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
290
Clemens Ladischaf9af172008-04-07 10:26:03 +0200291static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100292{
293 if (!strcmp(template->name, "Master Playback Volume")) {
294 template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
295 template->info = pcm1796_volume_info,
296 template->tlv.p = pcm1796_db_scale;
297 } else if (!strncmp(template->name, "CD Capture ", 11)) {
Clemens Ladisch911b4992008-01-28 08:33:44 +0100298 /* CD in is actually connected to the video in pin */
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100299 template->private_value ^= AC97_CD ^ AC97_VIDEO;
300 }
301 return 0;
302}
303
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100304static int xonar_mixer_init(struct oxygen *chip)
305{
306 return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
307}
308
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200309static const struct oxygen_model xonar_models[] = {
310 [MODEL_D2] = {
Clemens Ladischaef1a532008-04-07 10:26:45 +0200311 .shortname = "Xonar D2",
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200312 .longname = "Asus Virtuoso 200",
313 .chip = "AV200",
314 .owner = THIS_MODULE,
315 .init = xonar_d2_init,
Clemens Ladischaf9af172008-04-07 10:26:03 +0200316 .control_filter = xonar_d2_control_filter,
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200317 .mixer_init = xonar_mixer_init,
318 .cleanup = xonar_cleanup,
319 .set_dac_params = set_pcm1796_params,
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200320 .set_adc_params = set_cs53x1_params,
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200321 .update_dac_volume = update_pcm1796_volume,
322 .update_dac_mute = update_pcm1796_mute,
323 .model_data_size = sizeof(struct xonar_data),
324 .pcm_dev_cfg = PLAYBACK_0_TO_I2S |
325 PLAYBACK_1_TO_SPDIF |
326 CAPTURE_0_FROM_I2S_2 |
327 CAPTURE_1_FROM_SPDIF,
328 .dac_channels = 8,
329 .misc_flags = OXYGEN_MISC_MIDI,
330 .function_flags = OXYGEN_FUNCTION_SPI |
331 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
332 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
333 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
334 },
335 [MODEL_D2X] = {
Clemens Ladischaef1a532008-04-07 10:26:45 +0200336 .shortname = "Xonar D2X",
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200337 .longname = "Asus Virtuoso 200",
338 .chip = "AV200",
339 .owner = THIS_MODULE,
340 .init = xonar_d2x_init,
Clemens Ladischaf9af172008-04-07 10:26:03 +0200341 .control_filter = xonar_d2_control_filter,
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200342 .mixer_init = xonar_mixer_init,
343 .cleanup = xonar_cleanup,
344 .set_dac_params = set_pcm1796_params,
Clemens Ladischa694a6a2008-04-07 10:25:30 +0200345 .set_adc_params = set_cs53x1_params,
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200346 .update_dac_volume = update_pcm1796_volume,
347 .update_dac_mute = update_pcm1796_mute,
348 .gpio_changed = xonar_gpio_changed,
349 .model_data_size = sizeof(struct xonar_data),
350 .pcm_dev_cfg = PLAYBACK_0_TO_I2S |
351 PLAYBACK_1_TO_SPDIF |
352 CAPTURE_0_FROM_I2S_2 |
353 CAPTURE_1_FROM_SPDIF,
354 .dac_channels = 8,
355 .misc_flags = OXYGEN_MISC_MIDI,
356 .function_flags = OXYGEN_FUNCTION_SPI |
357 OXYGEN_FUNCTION_ENABLE_SPI_4_5,
358 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
359 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
360 },
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100361};
362
363static int __devinit xonar_probe(struct pci_dev *pci,
364 const struct pci_device_id *pci_id)
365{
366 static int dev;
367 int err;
368
369 if (dev >= SNDRV_CARDS)
370 return -ENODEV;
371 if (!enable[dev]) {
372 ++dev;
373 return -ENOENT;
374 }
Clemens Ladisch271ebfc2008-04-07 10:24:22 +0200375 err = oxygen_pci_probe(pci, index[dev], id[dev],
376 &xonar_models[pci_id->driver_data]);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100377 if (err >= 0)
378 ++dev;
379 return err;
380}
381
382static struct pci_driver xonar_driver = {
383 .name = "AV200",
384 .id_table = xonar_ids,
385 .probe = xonar_probe,
386 .remove = __devexit_p(oxygen_pci_remove),
387};
388
389static int __init alsa_card_xonar_init(void)
390{
391 return pci_register_driver(&xonar_driver);
392}
393
394static void __exit alsa_card_xonar_exit(void)
395{
396 pci_unregister_driver(&xonar_driver);
397}
398
399module_init(alsa_card_xonar_init)
400module_exit(alsa_card_xonar_exit)