blob: 83c2c43e7b8593fc8295c6b1be83427b03610f5a [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/*
21 * SPI 0 -> 1st PCM1796 (front)
Clemens Ladisch7113e952008-01-14 08:55:03 +010022 * SPI 1 -> 2nd PCM1796 (surround)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010023 * SPI 2 -> 3rd PCM1796 (center/LFE)
Clemens Ladisch7113e952008-01-14 08:55:03 +010024 * SPI 4 -> 4th PCM1796 (back)
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010025 *
26 * GPIO 2 -> M0 of CS5381
27 * GPIO 3 -> M1 of CS5381
28 * GPIO 5 <- ? (D2X only)
29 * GPIO 7 -> ALT
30 * GPIO 8 -> ? (amps enable?)
31 */
32
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010033#include <linux/pci.h>
34#include <linux/delay.h>
Clemens Ladischccc80fb2008-01-16 08:32:08 +010035#include <linux/mutex.h>
36#include <sound/ac97_codec.h>
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010037#include <sound/control.h>
38#include <sound/core.h>
39#include <sound/initval.h>
40#include <sound/pcm.h>
41#include <sound/tlv.h>
42#include "oxygen.h"
43
44MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
45MODULE_DESCRIPTION("Asus AV200 driver");
46MODULE_LICENSE("GPL");
47MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
48
49static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
50static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
51static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
52
53module_param_array(index, int, NULL, 0444);
54MODULE_PARM_DESC(index, "card index");
55module_param_array(id, charp, NULL, 0444);
56MODULE_PARM_DESC(id, "ID string");
57module_param_array(enable, bool, NULL, 0444);
58MODULE_PARM_DESC(enable, "enable card");
59
60static struct pci_device_id xonar_ids[] __devinitdata = {
61 { OXYGEN_PCI_SUBID(0x1043, 0x8269) }, /* Asus Xonar D2 */
62 { OXYGEN_PCI_SUBID(0x1043, 0x82b7) }, /* Asus Xonar D2X */
63 { }
64};
65MODULE_DEVICE_TABLE(pci, xonar_ids);
66
67/* register 0x12 */
68#define PCM1796_MUTE 0x01
69#define PCM1796_FMT_24_MSB 0x30
70#define PCM1796_ATLD 0x80
71/* register 0x14 */
72#define PCM1796_OS_64 0x00
73#define PCM1796_OS_32 0x01
74#define PCM1796_OS_128 0x02
75
76static void pcm1796_write(struct oxygen *chip, unsigned int codec,
77 u8 reg, u8 value)
78{
79 /* maps ALSA channel pair number to SPI output */
80 static const u8 codec_map[4] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +010081 0, 1, 2, 4
Clemens Ladisch1b8ff222007-12-23 19:52:08 +010082 };
83 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE |
84 OXYGEN_SPI_DATA_LENGTH_2 |
85 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
86 OXYGEN_SPI_MAGIC,
87 (reg << 8) | value);
88}
89
90static void xonar_init(struct oxygen *chip)
91{
92 unsigned int i;
93
94 for (i = 0; i < 4; ++i) {
95 pcm1796_write(chip, i, 0x12, PCM1796_FMT_24_MSB | PCM1796_ATLD);
96 pcm1796_write(chip, i, 0x13, 0);
97 pcm1796_write(chip, i, 0x14, PCM1796_OS_64);
98 pcm1796_write(chip, i, 0x15, 0);
99 pcm1796_write(chip, i, 0x10, 0xff);
100 pcm1796_write(chip, i, 0x11, 0xff);
101 }
102
103 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x8c);
104 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 0x00, 0x8c);
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100105 oxygen_ac97_set_bits(chip, 0, 0x62, 0x0080);
106 msleep(300);
107 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x100);
108 oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
109
110 snd_component_add(chip->card, "PCM1796");
111 snd_component_add(chip->card, "CS5381");
112}
113
114static void xonar_cleanup(struct oxygen *chip)
115{
116 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
117}
118
119static void set_pcm1796_params(struct oxygen *chip,
120 struct snd_pcm_hw_params *params)
121{
122#if 0
123 unsigned int i;
124 u8 value;
125
126 value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
127 for (i = 0; i < 4; ++i)
128 pcm1796_write(chip, i, 0x14, value);
129#endif
130}
131
132static void update_pcm1796_volume(struct oxygen *chip)
133{
134 unsigned int i;
135
136 for (i = 0; i < 4; ++i) {
137 pcm1796_write(chip, i, 0x10, chip->dac_volume[i * 2]);
138 pcm1796_write(chip, i, 0x11, chip->dac_volume[i * 2 + 1]);
139 }
140}
141
142static void update_pcm1796_mute(struct oxygen *chip)
143{
144 unsigned int i;
145 u8 value;
146
147 value = PCM1796_FMT_24_MSB | PCM1796_ATLD;
148 if (chip->dac_mute)
149 value |= PCM1796_MUTE;
150 for (i = 0; i < 4; ++i)
151 pcm1796_write(chip, i, 0x12, value);
152}
153
154static void set_cs5381_params(struct oxygen *chip,
155 struct snd_pcm_hw_params *params)
156{
157 unsigned int value;
158
159 if (params_rate(params) <= 54000)
160 value = 0;
161 else if (params_rate(params) <= 108000)
162 value = 4;
163 else
164 value = 8;
165 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x000c);
166}
167
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100168static int pcm1796_volume_info(struct snd_kcontrol *ctl,
169 struct snd_ctl_elem_info *info)
170{
171 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
172 info->count = 8;
173 info->value.integer.min = 0x0f;
174 info->value.integer.max = 0xff;
175 return 0;
176}
177
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100178static int alt_switch_get(struct snd_kcontrol *ctl,
179 struct snd_ctl_elem_value *value)
180{
181 struct oxygen *chip = ctl->private_data;
182
183 value->value.integer.value[0] =
184 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & 0x80);
185 return 0;
186}
187
188static int alt_switch_put(struct snd_kcontrol *ctl,
189 struct snd_ctl_elem_value *value)
190{
191 struct oxygen *chip = ctl->private_data;
192 u16 old_bits, new_bits;
193 int changed;
194
195 spin_lock_irq(&chip->reg_lock);
196 old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
197 if (value->value.integer.value[0])
198 new_bits = old_bits | 0x80;
199 else
200 new_bits = old_bits & ~0x80;
201 changed = new_bits != old_bits;
202 if (changed)
203 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
204 spin_unlock_irq(&chip->reg_lock);
205 return changed;
206}
207
208static const struct snd_kcontrol_new alt_switch = {
209 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
210 .name = "Analog Loopback Switch",
211 .info = snd_ctl_boolean_mono_info,
212 .get = alt_switch_get,
213 .put = alt_switch_put,
214};
215
216static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
217
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100218static int xonar_control_filter(struct snd_kcontrol_new *template)
219{
220 if (!strcmp(template->name, "Master Playback Volume")) {
221 template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
222 template->info = pcm1796_volume_info,
223 template->tlv.p = pcm1796_db_scale;
224 } else if (!strncmp(template->name, "CD Capture ", 11)) {
225 template->private_value ^= AC97_CD ^ AC97_VIDEO;
226 }
227 return 0;
228}
229
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100230static int xonar_mixer_init(struct oxygen *chip)
231{
232 return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
233}
234
235static const struct oxygen_model model_xonar = {
236 .shortname = "Asus AV200",
237 .longname = "Asus Virtuoso 200",
238 .chip = "AV200",
239 .init = xonar_init,
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100240 .control_filter = xonar_control_filter,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100241 .mixer_init = xonar_mixer_init,
242 .cleanup = xonar_cleanup,
243 .set_dac_params = set_pcm1796_params,
244 .set_adc_params = set_cs5381_params,
245 .update_dac_volume = update_pcm1796_volume,
246 .update_dac_mute = update_pcm1796_mute,
Clemens Ladische85e0922008-01-16 08:30:38 +0100247 .used_channels = OXYGEN_CHANNEL_B |
248 OXYGEN_CHANNEL_C |
249 OXYGEN_CHANNEL_SPDIF |
250 OXYGEN_CHANNEL_MULTICH,
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100251 .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100252 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
253 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladisch1b8ff222007-12-23 19:52:08 +0100254};
255
256static int __devinit xonar_probe(struct pci_dev *pci,
257 const struct pci_device_id *pci_id)
258{
259 static int dev;
260 int err;
261
262 if (dev >= SNDRV_CARDS)
263 return -ENODEV;
264 if (!enable[dev]) {
265 ++dev;
266 return -ENOENT;
267 }
268 err = oxygen_pci_probe(pci, index[dev], id[dev], &model_xonar);
269 if (err >= 0)
270 ++dev;
271 return err;
272}
273
274static struct pci_driver xonar_driver = {
275 .name = "AV200",
276 .id_table = xonar_ids,
277 .probe = xonar_probe,
278 .remove = __devexit_p(oxygen_pci_remove),
279};
280
281static int __init alsa_card_xonar_init(void)
282{
283 return pci_register_driver(&xonar_driver);
284}
285
286static void __exit alsa_card_xonar_exit(void)
287{
288 pci_unregister_driver(&xonar_driver);
289}
290
291module_init(alsa_card_xonar_init)
292module_exit(alsa_card_xonar_exit)