6603a685ccca6e73a7e228a6a8cee100e578aac3
[safe/jmp/linux-2.6] / sound / pci / oxygen / virtuoso.c
1 /*
2  * C-Media CMI8788 driver for Asus Xonar cards
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  *
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License, version 2.
9  *
10  *  This driver is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this driver; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19
20 /*
21  * SPI 0 -> 1st PCM1796 (front)
22  * SPI 1 -> 2nd PCM1796 (side)
23  * SPI 2 -> 3rd PCM1796 (center/LFE)
24  * SPI 4 -> 4th PCM1796 (rear)
25  *
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
33 #include <linux/pci.h>
34 #include <linux/delay.h>
35 #include <sound/control.h>
36 #include <sound/core.h>
37 #include <sound/initval.h>
38 #include <sound/pcm.h>
39 #include <sound/tlv.h>
40 #include "oxygen.h"
41
42 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
43 MODULE_DESCRIPTION("Asus AV200 driver");
44 MODULE_LICENSE("GPL");
45 MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
46
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
50
51 module_param_array(index, int, NULL, 0444);
52 MODULE_PARM_DESC(index, "card index");
53 module_param_array(id, charp, NULL, 0444);
54 MODULE_PARM_DESC(id, "ID string");
55 module_param_array(enable, bool, NULL, 0444);
56 MODULE_PARM_DESC(enable, "enable card");
57
58 static struct pci_device_id xonar_ids[] __devinitdata = {
59         { OXYGEN_PCI_SUBID(0x1043, 0x8269) }, /* Asus Xonar D2 */
60         { OXYGEN_PCI_SUBID(0x1043, 0x82b7) }, /* Asus Xonar D2X */
61         { }
62 };
63 MODULE_DEVICE_TABLE(pci, xonar_ids);
64
65 /* register 0x12 */
66 #define PCM1796_MUTE            0x01
67 #define PCM1796_FMT_24_MSB      0x30
68 #define PCM1796_ATLD            0x80
69 /* register 0x14 */
70 #define PCM1796_OS_64           0x00
71 #define PCM1796_OS_32           0x01
72 #define PCM1796_OS_128          0x02
73
74 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
75                           u8 reg, u8 value)
76 {
77         /* maps ALSA channel pair number to SPI output */
78         static const u8 codec_map[4] = {
79                 0, 4, 2, 1
80         };
81         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE |
82                          OXYGEN_SPI_DATA_LENGTH_2 |
83                          (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
84                          OXYGEN_SPI_MAGIC,
85                          (reg << 8) | value);
86 }
87
88 static void xonar_init(struct oxygen *chip)
89 {
90         unsigned int i;
91
92         for (i = 0; i < 4; ++i) {
93                 pcm1796_write(chip, i, 0x12, PCM1796_FMT_24_MSB | PCM1796_ATLD);
94                 pcm1796_write(chip, i, 0x13, 0);
95                 pcm1796_write(chip, i, 0x14, PCM1796_OS_64);
96                 pcm1796_write(chip, i, 0x15, 0);
97                 pcm1796_write(chip, i, 0x10, 0xff);
98                 pcm1796_write(chip, i, 0x11, 0xff);
99         }
100
101         oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x8c);
102         oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 0x00, 0x8c);
103 #if 0
104         oxygen_clear_bits16(chip, OXYGEN_I2S_MULTICH_FORMAT,
105                             OXYGEN_I2S_MAGIC1_MASK);
106 #endif
107         oxygen_ac97_set_bits(chip, 0, 0x62, 0x0080);
108         msleep(300);
109         oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x100);
110         oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
111
112         snd_component_add(chip->card, "PCM1796");
113         snd_component_add(chip->card, "CS5381");
114 }
115
116 static void xonar_cleanup(struct oxygen *chip)
117 {
118         oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
119 }
120
121 static void set_pcm1796_params(struct oxygen *chip,
122                                struct snd_pcm_hw_params *params)
123 {
124 #if 0
125         unsigned int i;
126         u8 value;
127
128         value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
129         for (i = 0; i < 4; ++i)
130                 pcm1796_write(chip, i, 0x14, value);
131 #endif
132 }
133
134 static void update_pcm1796_volume(struct oxygen *chip)
135 {
136         unsigned int i;
137
138         for (i = 0; i < 4; ++i) {
139                 pcm1796_write(chip, i, 0x10, chip->dac_volume[i * 2]);
140                 pcm1796_write(chip, i, 0x11, chip->dac_volume[i * 2 + 1]);
141         }
142 }
143
144 static void update_pcm1796_mute(struct oxygen *chip)
145 {
146         unsigned int i;
147         u8 value;
148
149         value = PCM1796_FMT_24_MSB | PCM1796_ATLD;
150         if (chip->dac_mute)
151                 value |= PCM1796_MUTE;
152         for (i = 0; i < 4; ++i)
153                 pcm1796_write(chip, i, 0x12, value);
154 }
155
156 static void set_cs5381_params(struct oxygen *chip,
157                               struct snd_pcm_hw_params *params)
158 {
159         unsigned int value;
160
161         if (params_rate(params) <= 54000)
162                 value = 0;
163         else if (params_rate(params) <= 108000)
164                 value = 4;
165         else
166                 value = 8;
167         oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x000c);
168 }
169
170 static int alt_switch_get(struct snd_kcontrol *ctl,
171                           struct snd_ctl_elem_value *value)
172 {
173         struct oxygen *chip = ctl->private_data;
174
175         value->value.integer.value[0] =
176                 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & 0x80);
177         return 0;
178 }
179
180 static int alt_switch_put(struct snd_kcontrol *ctl,
181                           struct snd_ctl_elem_value *value)
182 {
183         struct oxygen *chip = ctl->private_data;
184         u16 old_bits, new_bits;
185         int changed;
186
187         spin_lock_irq(&chip->reg_lock);
188         old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
189         if (value->value.integer.value[0])
190                 new_bits = old_bits | 0x80;
191         else
192                 new_bits = old_bits & ~0x80;
193         changed = new_bits != old_bits;
194         if (changed)
195                 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
196         spin_unlock_irq(&chip->reg_lock);
197         return changed;
198 }
199
200 static const struct snd_kcontrol_new alt_switch = {
201         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
202         .name = "Analog Loopback Switch",
203         .info = snd_ctl_boolean_mono_info,
204         .get = alt_switch_get,
205         .put = alt_switch_put,
206 };
207
208 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
209
210 static int xonar_mixer_init(struct oxygen *chip)
211 {
212         return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
213 }
214
215 static const struct oxygen_model model_xonar = {
216         .shortname = "Asus AV200",
217         .longname = "Asus Virtuoso 200",
218         .chip = "AV200",
219         .init = xonar_init,
220         .mixer_init = xonar_mixer_init,
221         .cleanup = xonar_cleanup,
222         .set_dac_params = set_pcm1796_params,
223         .set_adc_params = set_cs5381_params,
224         .update_dac_volume = update_pcm1796_volume,
225         .update_dac_mute = update_pcm1796_mute,
226         .dac_tlv = pcm1796_db_scale,
227         .record_from_dma_b = 1,
228         .cd_in_from_video_in = 1,
229         .dac_minimum_volume = 15,
230 };
231
232 static int __devinit xonar_probe(struct pci_dev *pci,
233                                  const struct pci_device_id *pci_id)
234 {
235         static int dev;
236         int err;
237
238         if (dev >= SNDRV_CARDS)
239                 return -ENODEV;
240         if (!enable[dev]) {
241                 ++dev;
242                 return -ENOENT;
243         }
244         err = oxygen_pci_probe(pci, index[dev], id[dev], &model_xonar);
245         if (err >= 0)
246                 ++dev;
247         return err;
248 }
249
250 static struct pci_driver xonar_driver = {
251         .name = "AV200",
252         .id_table = xonar_ids,
253         .probe = xonar_probe,
254         .remove = __devexit_p(oxygen_pci_remove),
255 };
256
257 static int __init alsa_card_xonar_init(void)
258 {
259         return pci_register_driver(&xonar_driver);
260 }
261
262 static void __exit alsa_card_xonar_exit(void)
263 {
264         pci_unregister_driver(&xonar_driver);
265 }
266
267 module_init(alsa_card_xonar_init)
268 module_exit(alsa_card_xonar_exit)