xref: /openbmc/linux/sound/pci/oxygen/virtuoso.c (revision 878ac3ee)
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  * CMI8788:
22  *
23  * SPI 0 -> 1st PCM1796 (front)
24  * SPI 1 -> 2nd PCM1796 (surround)
25  * SPI 2 -> 3rd PCM1796 (center/LFE)
26  * SPI 4 -> 4th PCM1796 (back)
27  *
28  * GPIO 2 -> M0 of CS5381
29  * GPIO 3 -> M1 of CS5381
30  * GPIO 5 <- external power present (D2X only)
31  * GPIO 7 -> ALT
32  * GPIO 8 -> enable output to speakers
33  *
34  * CM9780:
35  *
36  * GPIO 0 -> enable AC'97 bypass (line in -> ADC)
37  */
38 
39 #include <linux/pci.h>
40 #include <linux/delay.h>
41 #include <linux/mutex.h>
42 #include <sound/ac97_codec.h>
43 #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"
49 #include "cm9780.h"
50 
51 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
52 MODULE_DESCRIPTION("Asus AV200 driver");
53 MODULE_LICENSE("GPL");
54 MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
55 
56 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
57 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
58 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
59 
60 module_param_array(index, int, NULL, 0444);
61 MODULE_PARM_DESC(index, "card index");
62 module_param_array(id, charp, NULL, 0444);
63 MODULE_PARM_DESC(id, "ID string");
64 module_param_array(enable, bool, NULL, 0444);
65 MODULE_PARM_DESC(enable, "enable card");
66 
67 static struct pci_device_id xonar_ids[] __devinitdata = {
68 	{ OXYGEN_PCI_SUBID(0x1043, 0x8269) }, /* Asus Xonar D2 */
69 	{ OXYGEN_PCI_SUBID(0x1043, 0x82b7) }, /* Asus Xonar D2X */
70 	{ }
71 };
72 MODULE_DEVICE_TABLE(pci, xonar_ids);
73 
74 
75 #define GPIO_CS5381_M_MASK	0x000c
76 #define GPIO_CS5381_M_SINGLE	0x0000
77 #define GPIO_CS5381_M_DOUBLE	0x0004
78 #define GPIO_CS5381_M_QUAD	0x0008
79 #define GPIO_EXT_POWER		0x0020
80 #define GPIO_ALT		0x0080
81 #define GPIO_OUTPUT_ENABLE	0x0100
82 
83 /* register 16 */
84 #define PCM1796_ATL_MASK	0xff
85 /* register 17 */
86 #define PCM1796_ATR_MASK	0xff
87 /* register 18 */
88 #define PCM1796_MUTE		0x01
89 #define PCM1796_DME		0x02
90 #define PCM1796_DMF_MASK	0x0c
91 #define PCM1796_DMF_DISABLED	0x00
92 #define PCM1796_DMF_48		0x04
93 #define PCM1796_DMF_441		0x08
94 #define PCM1796_DMF_32		0x0c
95 #define PCM1796_FMT_MASK	0x70
96 #define PCM1796_FMT_16_RJUST	0x00
97 #define PCM1796_FMT_20_RJUST	0x10
98 #define PCM1796_FMT_24_RJUST	0x20
99 #define PCM1796_FMT_24_LJUST	0x30
100 #define PCM1796_FMT_16_I2S	0x40
101 #define PCM1796_FMT_24_I2S	0x50
102 #define PCM1796_ATLD		0x80
103 /* register 19 */
104 #define PCM1796_INZD		0x01
105 #define PCM1796_FLT_MASK	0x02
106 #define PCM1796_FLT_SHARP	0x00
107 #define PCM1796_FLT_SLOW	0x02
108 #define PCM1796_DFMS		0x04
109 #define PCM1796_OPE		0x10
110 #define PCM1796_ATS_MASK	0x60
111 #define PCM1796_ATS_1		0x00
112 #define PCM1796_ATS_2		0x20
113 #define PCM1796_ATS_4		0x40
114 #define PCM1796_ATS_8		0x60
115 #define PCM1796_REV		0x80
116 /* register 20 */
117 #define PCM1796_OS_MASK		0x03
118 #define PCM1796_OS_64		0x00
119 #define PCM1796_OS_32		0x01
120 #define PCM1796_OS_128		0x02
121 #define PCM1796_CHSL_MASK	0x04
122 #define PCM1796_CHSL_LEFT	0x00
123 #define PCM1796_CHSL_RIGHT	0x04
124 #define PCM1796_MONO		0x08
125 #define PCM1796_DFTH		0x10
126 #define PCM1796_DSD		0x20
127 #define PCM1796_SRST		0x40
128 /* register 21 */
129 #define PCM1796_PCMZ		0x01
130 #define PCM1796_DZ_MASK		0x06
131 /* register 22 */
132 #define PCM1796_ZFGL		0x01
133 #define PCM1796_ZFGR		0x02
134 /* register 23 */
135 #define PCM1796_ID_MASK		0x1f
136 
137 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
138 			  u8 reg, u8 value)
139 {
140 	/* maps ALSA channel pair number to SPI output */
141 	static const u8 codec_map[4] = {
142 		0, 1, 2, 4
143 	};
144 	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER  |
145 			 OXYGEN_SPI_DATA_LENGTH_2 |
146 			 OXYGEN_SPI_CLOCK_160 |
147 			 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
148 			 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
149 			 (reg << 8) | value);
150 }
151 
152 static void xonar_init(struct oxygen *chip)
153 {
154 	unsigned int i;
155 
156 	for (i = 0; i < 4; ++i) {
157 		pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
158 		pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
159 		pcm1796_write(chip, i, 20, PCM1796_OS_64);
160 		pcm1796_write(chip, i, 21, 0);
161 		pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
162 		pcm1796_write(chip, i, 17, 0xff);
163 	}
164 
165 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
166 			  GPIO_CS5381_M_MASK | GPIO_ALT);
167 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
168 			      GPIO_CS5381_M_SINGLE,
169 			      GPIO_CS5381_M_MASK | GPIO_ALT);
170 	oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
171 	msleep(300);
172 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE);
173 	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
174 
175 	snd_component_add(chip->card, "PCM1796");
176 	snd_component_add(chip->card, "CS5381");
177 }
178 
179 static void xonar_cleanup(struct oxygen *chip)
180 {
181 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
182 }
183 
184 static void set_pcm1796_params(struct oxygen *chip,
185 			       struct snd_pcm_hw_params *params)
186 {
187 #if 0
188 	unsigned int i;
189 	u8 value;
190 
191 	value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
192 	for (i = 0; i < 4; ++i)
193 		pcm1796_write(chip, i, 20, value);
194 #endif
195 }
196 
197 static void update_pcm1796_volume(struct oxygen *chip)
198 {
199 	unsigned int i;
200 
201 	for (i = 0; i < 4; ++i) {
202 		pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
203 		pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
204 	}
205 }
206 
207 static void update_pcm1796_mute(struct oxygen *chip)
208 {
209 	unsigned int i;
210 	u8 value;
211 
212 	value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
213 	if (chip->dac_mute)
214 		value |= PCM1796_MUTE;
215 	for (i = 0; i < 4; ++i)
216 		pcm1796_write(chip, i, 18, value);
217 }
218 
219 static void set_cs5381_params(struct oxygen *chip,
220 			      struct snd_pcm_hw_params *params)
221 {
222 	unsigned int value;
223 
224 	if (params_rate(params) <= 54000)
225 		value = GPIO_CS5381_M_SINGLE;
226 	else if (params_rate(params) <= 108000)
227 		value = GPIO_CS5381_M_DOUBLE;
228 	else
229 		value = GPIO_CS5381_M_QUAD;
230 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
231 			      value, GPIO_CS5381_M_MASK);
232 }
233 
234 static int pcm1796_volume_info(struct snd_kcontrol *ctl,
235 			       struct snd_ctl_elem_info *info)
236 {
237 	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
238 	info->count = 8;
239 	info->value.integer.min = 0x0f;
240 	info->value.integer.max = 0xff;
241 	return 0;
242 }
243 
244 static int alt_switch_get(struct snd_kcontrol *ctl,
245 			  struct snd_ctl_elem_value *value)
246 {
247 	struct oxygen *chip = ctl->private_data;
248 
249 	value->value.integer.value[0] =
250 		!!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_ALT);
251 	return 0;
252 }
253 
254 static int alt_switch_put(struct snd_kcontrol *ctl,
255 			  struct snd_ctl_elem_value *value)
256 {
257 	struct oxygen *chip = ctl->private_data;
258 	u16 old_bits, new_bits;
259 	int changed;
260 
261 	spin_lock_irq(&chip->reg_lock);
262 	old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
263 	if (value->value.integer.value[0])
264 		new_bits = old_bits | GPIO_ALT;
265 	else
266 		new_bits = old_bits & ~GPIO_ALT;
267 	changed = new_bits != old_bits;
268 	if (changed)
269 		oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
270 	spin_unlock_irq(&chip->reg_lock);
271 	return changed;
272 }
273 
274 static const struct snd_kcontrol_new alt_switch = {
275 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
276 	.name = "Analog Loopback Switch",
277 	.info = snd_ctl_boolean_mono_info,
278 	.get = alt_switch_get,
279 	.put = alt_switch_put,
280 };
281 
282 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
283 
284 static int xonar_control_filter(struct snd_kcontrol_new *template)
285 {
286 	if (!strcmp(template->name, "Master Playback Volume")) {
287 		template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
288 		template->info = pcm1796_volume_info,
289 		template->tlv.p = pcm1796_db_scale;
290 	} else if (!strncmp(template->name, "CD Capture ", 11)) {
291 		template->private_value ^= AC97_CD ^ AC97_VIDEO;
292 	}
293 	return 0;
294 }
295 
296 static int xonar_mixer_init(struct oxygen *chip)
297 {
298 	return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
299 }
300 
301 static const struct oxygen_model model_xonar = {
302 	.shortname = "Asus AV200",
303 	.longname = "Asus Virtuoso 200",
304 	.chip = "AV200",
305 	.init = xonar_init,
306 	.control_filter = xonar_control_filter,
307 	.mixer_init = xonar_mixer_init,
308 	.cleanup = xonar_cleanup,
309 	.set_dac_params = set_pcm1796_params,
310 	.set_adc_params = set_cs5381_params,
311 	.update_dac_volume = update_pcm1796_volume,
312 	.update_dac_mute = update_pcm1796_mute,
313 	.used_channels = OXYGEN_CHANNEL_B |
314 			 OXYGEN_CHANNEL_C |
315 			 OXYGEN_CHANNEL_SPDIF |
316 			 OXYGEN_CHANNEL_MULTICH,
317 	.function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
318 	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
319 	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
320 };
321 
322 static int __devinit xonar_probe(struct pci_dev *pci,
323 				 const struct pci_device_id *pci_id)
324 {
325 	static int dev;
326 	int err;
327 
328 	if (dev >= SNDRV_CARDS)
329 		return -ENODEV;
330 	if (!enable[dev]) {
331 		++dev;
332 		return -ENOENT;
333 	}
334 	err = oxygen_pci_probe(pci, index[dev], id[dev], 1, &model_xonar);
335 	if (err >= 0)
336 		++dev;
337 	return err;
338 }
339 
340 static struct pci_driver xonar_driver = {
341 	.name = "AV200",
342 	.id_table = xonar_ids,
343 	.probe = xonar_probe,
344 	.remove = __devexit_p(oxygen_pci_remove),
345 };
346 
347 static int __init alsa_card_xonar_init(void)
348 {
349 	return pci_register_driver(&xonar_driver);
350 }
351 
352 static void __exit alsa_card_xonar_exit(void)
353 {
354 	pci_unregister_driver(&xonar_driver);
355 }
356 
357 module_init(alsa_card_xonar_init)
358 module_exit(alsa_card_xonar_exit)
359