blob: 127dd664fc16bc38ec839b236fa8a0bcbf9179b9 [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
33 *
34 * CM9780:
35 *
36 * GPIO 0 -> enable AC'97 bypass (line in -> ADC)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010037 */
38
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010039#include <linux/pci.h>
40#include <linux/delay.h>
Clemens Ladischccc80fb2008-01-16 08:32:08 +010041#include <linux/mutex.h>
42#include <sound/ac97_codec.h>
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010043#include <sound/control.h>
44#include <sound/core.h>
45#include <sound/initval.h>
46#include <sound/pcm.h>
47#include <sound/tlv.h>
48#include "oxygen.h"
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010049#include "cm9780.h"
Clemens Ladisch33fa7242008-03-19 08:16:40 +010050#include "pcm1796.h"
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010051
52MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
53MODULE_DESCRIPTION("Asus AV200 driver");
54MODULE_LICENSE("GPL");
55MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
56
57static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
58static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
59static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
60
61module_param_array(index, int, NULL, 0444);
62MODULE_PARM_DESC(index, "card index");
63module_param_array(id, charp, NULL, 0444);
64MODULE_PARM_DESC(id, "ID string");
65module_param_array(enable, bool, NULL, 0444);
66MODULE_PARM_DESC(enable, "enable card");
67
68static struct pci_device_id xonar_ids[] __devinitdata = {
69 { OXYGEN_PCI_SUBID(0x1043, 0x8269) }, /* Asus Xonar D2 */
70 { OXYGEN_PCI_SUBID(0x1043, 0x82b7) }, /* Asus Xonar D2X */
71 { }
72};
73MODULE_DEVICE_TABLE(pci, xonar_ids);
74
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010075
76#define GPIO_CS5381_M_MASK 0x000c
77#define GPIO_CS5381_M_SINGLE 0x0000
78#define GPIO_CS5381_M_DOUBLE 0x0004
79#define GPIO_CS5381_M_QUAD 0x0008
80#define GPIO_EXT_POWER 0x0020
81#define GPIO_ALT 0x0080
82#define GPIO_OUTPUT_ENABLE 0x0100
83
Clemens Ladisch911b4992008-01-28 08:33:44 +010084#define GPIO_LINE_MUTE CM9780_GPO0
85
Clemens Ladisch7c014152008-01-28 08:36:55 +010086struct xonar_data {
87 u8 is_d2x;
88 u8 has_power;
89};
90
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010091static void pcm1796_write(struct oxygen *chip, unsigned int codec,
92 u8 reg, u8 value)
93{
94 /* maps ALSA channel pair number to SPI output */
95 static const u8 codec_map[4] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +010096 0, 1, 2, 4
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010097 };
Clemens Ladischc2353a02008-01-18 09:17:53 +010098 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010099 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100100 OXYGEN_SPI_CLOCK_160 |
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100101 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100102 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100103 (reg << 8) | value);
104}
105
106static void xonar_init(struct oxygen *chip)
107{
Clemens Ladisch7c014152008-01-28 08:36:55 +0100108 struct xonar_data *data = chip->model_data;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100109 unsigned int i;
110
Clemens Ladisch7c014152008-01-28 08:36:55 +0100111 data->is_d2x = chip->pci->subsystem_device == 0x82b7;
112
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100113 for (i = 0; i < 4; ++i) {
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100114 pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
115 pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
116 pcm1796_write(chip, i, 20, PCM1796_OS_64);
117 pcm1796_write(chip, i, 21, 0);
118 pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
119 pcm1796_write(chip, i, 17, 0xff);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100120 }
121
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100122 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
123 GPIO_CS5381_M_MASK | GPIO_ALT);
124 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
125 GPIO_CS5381_M_SINGLE,
126 GPIO_CS5381_M_MASK | GPIO_ALT);
Clemens Ladisch7c014152008-01-28 08:36:55 +0100127 if (data->is_d2x) {
128 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL,
129 GPIO_EXT_POWER);
130 oxygen_set_bits16(chip, OXYGEN_GPIO_INTERRUPT_MASK,
131 GPIO_EXT_POWER);
132 chip->interrupt_mask |= OXYGEN_INT_GPIO;
133 data->has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA)
134 & GPIO_EXT_POWER);
135 }
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100136 oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
Clemens Ladisch911b4992008-01-28 08:33:44 +0100137 oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS, GPIO_LINE_MUTE);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100138 msleep(300);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100139 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE);
140 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100141
142 snd_component_add(chip->card, "PCM1796");
143 snd_component_add(chip->card, "CS5381");
144}
145
146static void xonar_cleanup(struct oxygen *chip)
147{
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100148 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100149}
150
151static void set_pcm1796_params(struct oxygen *chip,
152 struct snd_pcm_hw_params *params)
153{
154#if 0
155 unsigned int i;
156 u8 value;
157
158 value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
159 for (i = 0; i < 4; ++i)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100160 pcm1796_write(chip, i, 20, value);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100161#endif
162}
163
164static void update_pcm1796_volume(struct oxygen *chip)
165{
166 unsigned int i;
167
168 for (i = 0; i < 4; ++i) {
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100169 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
170 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100171 }
172}
173
174static void update_pcm1796_mute(struct oxygen *chip)
175{
176 unsigned int i;
177 u8 value;
178
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100179 value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100180 if (chip->dac_mute)
181 value |= PCM1796_MUTE;
182 for (i = 0; i < 4; ++i)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100183 pcm1796_write(chip, i, 18, value);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100184}
185
186static void set_cs5381_params(struct oxygen *chip,
187 struct snd_pcm_hw_params *params)
188{
189 unsigned int value;
190
191 if (params_rate(params) <= 54000)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100192 value = GPIO_CS5381_M_SINGLE;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100193 else if (params_rate(params) <= 108000)
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100194 value = GPIO_CS5381_M_DOUBLE;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100195 else
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100196 value = GPIO_CS5381_M_QUAD;
197 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
198 value, GPIO_CS5381_M_MASK);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100199}
200
Clemens Ladisch7c014152008-01-28 08:36:55 +0100201static void xonar_gpio_changed(struct oxygen *chip)
202{
203 struct xonar_data *data = chip->model_data;
204 u8 has_power;
205
206 if (!data->is_d2x)
207 return;
208 has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA)
209 & GPIO_EXT_POWER);
210 if (has_power != data->has_power) {
211 data->has_power = has_power;
212 if (has_power) {
213 snd_printk(KERN_NOTICE "power restored\n");
214 } else {
215 snd_printk(KERN_CRIT
216 "Hey! Don't unplug the power cable!\n");
217 /* TODO: stop PCMs */
218 }
219 }
220}
221
Clemens Ladisch911b4992008-01-28 08:33:44 +0100222static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
223{
Harvey Harrisonff143872008-02-29 11:46:57 +0100224 unsigned int priv_idx = chip->controls[control]->private_value & 0xff;
Clemens Ladisch911b4992008-01-28 08:33:44 +0100225 u16 value;
226
Harvey Harrisonff143872008-02-29 11:46:57 +0100227 value = oxygen_read_ac97(chip, 0, priv_idx);
Clemens Ladisch911b4992008-01-28 08:33:44 +0100228 if (!(value & 0x8000)) {
Harvey Harrisonff143872008-02-29 11:46:57 +0100229 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
Clemens Ladisch911b4992008-01-28 08:33:44 +0100230 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
231 &chip->controls[control]->id);
232 }
233}
234
Clemens Ladischa3601562008-01-28 08:35:20 +0100235static void xonar_ac97_switch_hook(struct oxygen *chip, unsigned int codec,
Clemens Ladisch911b4992008-01-28 08:33:44 +0100236 unsigned int reg, int mute)
237{
Clemens Ladischa3601562008-01-28 08:35:20 +0100238 if (codec != 0)
239 return;
Clemens Ladisch911b4992008-01-28 08:33:44 +0100240 /* line-in is exclusive */
241 switch (reg) {
242 case AC97_LINE:
243 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
244 mute ? GPIO_LINE_MUTE : 0,
245 GPIO_LINE_MUTE);
246 if (!mute) {
247 mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
248 mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
249 mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
250 }
251 break;
252 case AC97_MIC:
253 case AC97_CD:
254 case AC97_VIDEO:
255 case AC97_AUX:
256 if (!mute) {
257 oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_STATUS,
258 GPIO_LINE_MUTE);
259 mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
260 }
261 break;
262 }
263}
264
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100265static int pcm1796_volume_info(struct snd_kcontrol *ctl,
266 struct snd_ctl_elem_info *info)
267{
268 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
269 info->count = 8;
270 info->value.integer.min = 0x0f;
271 info->value.integer.max = 0xff;
272 return 0;
273}
274
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100275static int alt_switch_get(struct snd_kcontrol *ctl,
276 struct snd_ctl_elem_value *value)
277{
278 struct oxygen *chip = ctl->private_data;
279
280 value->value.integer.value[0] =
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100281 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_ALT);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100282 return 0;
283}
284
285static int alt_switch_put(struct snd_kcontrol *ctl,
286 struct snd_ctl_elem_value *value)
287{
288 struct oxygen *chip = ctl->private_data;
289 u16 old_bits, new_bits;
290 int changed;
291
292 spin_lock_irq(&chip->reg_lock);
293 old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
294 if (value->value.integer.value[0])
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100295 new_bits = old_bits | GPIO_ALT;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100296 else
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100297 new_bits = old_bits & ~GPIO_ALT;
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100298 changed = new_bits != old_bits;
299 if (changed)
300 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
301 spin_unlock_irq(&chip->reg_lock);
302 return changed;
303}
304
305static const struct snd_kcontrol_new alt_switch = {
306 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
307 .name = "Analog Loopback Switch",
308 .info = snd_ctl_boolean_mono_info,
309 .get = alt_switch_get,
310 .put = alt_switch_put,
311};
312
313static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
314
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100315static int xonar_control_filter(struct snd_kcontrol_new *template)
316{
317 if (!strcmp(template->name, "Master Playback Volume")) {
318 template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
319 template->info = pcm1796_volume_info,
320 template->tlv.p = pcm1796_db_scale;
321 } else if (!strncmp(template->name, "CD Capture ", 11)) {
Clemens Ladisch911b4992008-01-28 08:33:44 +0100322 /* CD in is actually connected to the video in pin */
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100323 template->private_value ^= AC97_CD ^ AC97_VIDEO;
Clemens Ladisch911b4992008-01-28 08:33:44 +0100324 } else if (!strcmp(template->name, "Line Capture Volume")) {
325 return 1; /* line-in bypasses the AC'97 mixer */
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100326 }
327 return 0;
328}
329
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100330static int xonar_mixer_init(struct oxygen *chip)
331{
332 return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
333}
334
335static const struct oxygen_model model_xonar = {
336 .shortname = "Asus AV200",
337 .longname = "Asus Virtuoso 200",
338 .chip = "AV200",
Clemens Ladischb930b9f2008-02-26 08:40:57 +0100339 .owner = THIS_MODULE,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100340 .init = xonar_init,
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100341 .control_filter = xonar_control_filter,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100342 .mixer_init = xonar_mixer_init,
343 .cleanup = xonar_cleanup,
344 .set_dac_params = set_pcm1796_params,
345 .set_adc_params = set_cs5381_params,
346 .update_dac_volume = update_pcm1796_volume,
347 .update_dac_mute = update_pcm1796_mute,
Clemens Ladisch911b4992008-01-28 08:33:44 +0100348 .ac97_switch_hook = xonar_ac97_switch_hook,
Clemens Ladisch7c014152008-01-28 08:36:55 +0100349 .gpio_changed = xonar_gpio_changed,
350 .model_data_size = sizeof(struct xonar_data),
Clemens Ladisch976cd622008-01-25 08:37:49 +0100351 .dac_channels = 8,
Clemens Ladische85e0922008-01-16 08:30:38 +0100352 .used_channels = OXYGEN_CHANNEL_B |
353 OXYGEN_CHANNEL_C |
354 OXYGEN_CHANNEL_SPDIF |
355 OXYGEN_CHANNEL_MULTICH,
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100356 .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100357 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
358 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100359};
360
361static int __devinit xonar_probe(struct pci_dev *pci,
362 const struct pci_device_id *pci_id)
363{
364 static int dev;
365 int err;
366
367 if (dev >= SNDRV_CARDS)
368 return -ENODEV;
369 if (!enable[dev]) {
370 ++dev;
371 return -ENOENT;
372 }
Clemens Ladisch44fb7aa2008-01-21 08:45:37 +0100373 err = oxygen_pci_probe(pci, index[dev], id[dev], 1, &model_xonar);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100374 if (err >= 0)
375 ++dev;
376 return err;
377}
378
379static struct pci_driver xonar_driver = {
380 .name = "AV200",
381 .id_table = xonar_ids,
382 .probe = xonar_probe,
383 .remove = __devexit_p(oxygen_pci_remove),
384};
385
386static int __init alsa_card_xonar_init(void)
387{
388 return pci_register_driver(&xonar_driver);
389}
390
391static void __exit alsa_card_xonar_exit(void)
392{
393 pci_unregister_driver(&xonar_driver);
394}
395
396module_init(alsa_card_xonar_init)
397module_exit(alsa_card_xonar_exit)