xref: /openbmc/linux/sound/pci/oxygen/virtuoso.c (revision a3601560)
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 #define GPIO_LINE_MUTE		CM9780_GPO0
84 
85 /* register 16 */
86 #define PCM1796_ATL_MASK	0xff
87 /* register 17 */
88 #define PCM1796_ATR_MASK	0xff
89 /* register 18 */
90 #define PCM1796_MUTE		0x01
91 #define PCM1796_DME		0x02
92 #define PCM1796_DMF_MASK	0x0c
93 #define PCM1796_DMF_DISABLED	0x00
94 #define PCM1796_DMF_48		0x04
95 #define PCM1796_DMF_441		0x08
96 #define PCM1796_DMF_32		0x0c
97 #define PCM1796_FMT_MASK	0x70
98 #define PCM1796_FMT_16_RJUST	0x00
99 #define PCM1796_FMT_20_RJUST	0x10
100 #define PCM1796_FMT_24_RJUST	0x20
101 #define PCM1796_FMT_24_LJUST	0x30
102 #define PCM1796_FMT_16_I2S	0x40
103 #define PCM1796_FMT_24_I2S	0x50
104 #define PCM1796_ATLD		0x80
105 /* register 19 */
106 #define PCM1796_INZD		0x01
107 #define PCM1796_FLT_MASK	0x02
108 #define PCM1796_FLT_SHARP	0x00
109 #define PCM1796_FLT_SLOW	0x02
110 #define PCM1796_DFMS		0x04
111 #define PCM1796_OPE		0x10
112 #define PCM1796_ATS_MASK	0x60
113 #define PCM1796_ATS_1		0x00
114 #define PCM1796_ATS_2		0x20
115 #define PCM1796_ATS_4		0x40
116 #define PCM1796_ATS_8		0x60
117 #define PCM1796_REV		0x80
118 /* register 20 */
119 #define PCM1796_OS_MASK		0x03
120 #define PCM1796_OS_64		0x00
121 #define PCM1796_OS_32		0x01
122 #define PCM1796_OS_128		0x02
123 #define PCM1796_CHSL_MASK	0x04
124 #define PCM1796_CHSL_LEFT	0x00
125 #define PCM1796_CHSL_RIGHT	0x04
126 #define PCM1796_MONO		0x08
127 #define PCM1796_DFTH		0x10
128 #define PCM1796_DSD		0x20
129 #define PCM1796_SRST		0x40
130 /* register 21 */
131 #define PCM1796_PCMZ		0x01
132 #define PCM1796_DZ_MASK		0x06
133 /* register 22 */
134 #define PCM1796_ZFGL		0x01
135 #define PCM1796_ZFGR		0x02
136 /* register 23 */
137 #define PCM1796_ID_MASK		0x1f
138 
139 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
140 			  u8 reg, u8 value)
141 {
142 	/* maps ALSA channel pair number to SPI output */
143 	static const u8 codec_map[4] = {
144 		0, 1, 2, 4
145 	};
146 	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER  |
147 			 OXYGEN_SPI_DATA_LENGTH_2 |
148 			 OXYGEN_SPI_CLOCK_160 |
149 			 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
150 			 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
151 			 (reg << 8) | value);
152 }
153 
154 static void xonar_init(struct oxygen *chip)
155 {
156 	unsigned int i;
157 
158 	for (i = 0; i < 4; ++i) {
159 		pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
160 		pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
161 		pcm1796_write(chip, i, 20, PCM1796_OS_64);
162 		pcm1796_write(chip, i, 21, 0);
163 		pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
164 		pcm1796_write(chip, i, 17, 0xff);
165 	}
166 
167 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
168 			  GPIO_CS5381_M_MASK | GPIO_ALT);
169 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
170 			      GPIO_CS5381_M_SINGLE,
171 			      GPIO_CS5381_M_MASK | GPIO_ALT);
172 	oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
173 	oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS, GPIO_LINE_MUTE);
174 	msleep(300);
175 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE);
176 	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
177 
178 	snd_component_add(chip->card, "PCM1796");
179 	snd_component_add(chip->card, "CS5381");
180 }
181 
182 static void xonar_cleanup(struct oxygen *chip)
183 {
184 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
185 }
186 
187 static void set_pcm1796_params(struct oxygen *chip,
188 			       struct snd_pcm_hw_params *params)
189 {
190 #if 0
191 	unsigned int i;
192 	u8 value;
193 
194 	value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
195 	for (i = 0; i < 4; ++i)
196 		pcm1796_write(chip, i, 20, value);
197 #endif
198 }
199 
200 static void update_pcm1796_volume(struct oxygen *chip)
201 {
202 	unsigned int i;
203 
204 	for (i = 0; i < 4; ++i) {
205 		pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
206 		pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
207 	}
208 }
209 
210 static void update_pcm1796_mute(struct oxygen *chip)
211 {
212 	unsigned int i;
213 	u8 value;
214 
215 	value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
216 	if (chip->dac_mute)
217 		value |= PCM1796_MUTE;
218 	for (i = 0; i < 4; ++i)
219 		pcm1796_write(chip, i, 18, value);
220 }
221 
222 static void set_cs5381_params(struct oxygen *chip,
223 			      struct snd_pcm_hw_params *params)
224 {
225 	unsigned int value;
226 
227 	if (params_rate(params) <= 54000)
228 		value = GPIO_CS5381_M_SINGLE;
229 	else if (params_rate(params) <= 108000)
230 		value = GPIO_CS5381_M_DOUBLE;
231 	else
232 		value = GPIO_CS5381_M_QUAD;
233 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
234 			      value, GPIO_CS5381_M_MASK);
235 }
236 
237 static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
238 {
239 	unsigned int index = chip->controls[control]->private_value & 0xff;
240 	u16 value;
241 
242 	value = oxygen_read_ac97(chip, 0, index);
243 	if (!(value & 0x8000)) {
244 		oxygen_write_ac97(chip, 0, index, value | 0x8000);
245 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
246 			       &chip->controls[control]->id);
247 	}
248 }
249 
250 static void xonar_ac97_switch_hook(struct oxygen *chip, unsigned int codec,
251 				   unsigned int reg, int mute)
252 {
253 	if (codec != 0)
254 		return;
255 	/* line-in is exclusive */
256 	switch (reg) {
257 	case AC97_LINE:
258 		oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
259 					 mute ? GPIO_LINE_MUTE : 0,
260 					 GPIO_LINE_MUTE);
261 		if (!mute) {
262 			mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
263 			mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
264 			mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
265 		}
266 		break;
267 	case AC97_MIC:
268 	case AC97_CD:
269 	case AC97_VIDEO:
270 	case AC97_AUX:
271 		if (!mute) {
272 			oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_STATUS,
273 					     GPIO_LINE_MUTE);
274 			mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
275 		}
276 		break;
277 	}
278 }
279 
280 static int pcm1796_volume_info(struct snd_kcontrol *ctl,
281 			       struct snd_ctl_elem_info *info)
282 {
283 	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
284 	info->count = 8;
285 	info->value.integer.min = 0x0f;
286 	info->value.integer.max = 0xff;
287 	return 0;
288 }
289 
290 static int alt_switch_get(struct snd_kcontrol *ctl,
291 			  struct snd_ctl_elem_value *value)
292 {
293 	struct oxygen *chip = ctl->private_data;
294 
295 	value->value.integer.value[0] =
296 		!!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_ALT);
297 	return 0;
298 }
299 
300 static int alt_switch_put(struct snd_kcontrol *ctl,
301 			  struct snd_ctl_elem_value *value)
302 {
303 	struct oxygen *chip = ctl->private_data;
304 	u16 old_bits, new_bits;
305 	int changed;
306 
307 	spin_lock_irq(&chip->reg_lock);
308 	old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
309 	if (value->value.integer.value[0])
310 		new_bits = old_bits | GPIO_ALT;
311 	else
312 		new_bits = old_bits & ~GPIO_ALT;
313 	changed = new_bits != old_bits;
314 	if (changed)
315 		oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
316 	spin_unlock_irq(&chip->reg_lock);
317 	return changed;
318 }
319 
320 static const struct snd_kcontrol_new alt_switch = {
321 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
322 	.name = "Analog Loopback Switch",
323 	.info = snd_ctl_boolean_mono_info,
324 	.get = alt_switch_get,
325 	.put = alt_switch_put,
326 };
327 
328 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
329 
330 static int xonar_control_filter(struct snd_kcontrol_new *template)
331 {
332 	if (!strcmp(template->name, "Master Playback Volume")) {
333 		template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
334 		template->info = pcm1796_volume_info,
335 		template->tlv.p = pcm1796_db_scale;
336 	} else if (!strncmp(template->name, "CD Capture ", 11)) {
337 		/* CD in is actually connected to the video in pin */
338 		template->private_value ^= AC97_CD ^ AC97_VIDEO;
339 	} else if (!strcmp(template->name, "Line Capture Volume")) {
340 		return 1; /* line-in bypasses the AC'97 mixer */
341 	}
342 	return 0;
343 }
344 
345 static int xonar_mixer_init(struct oxygen *chip)
346 {
347 	return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
348 }
349 
350 static const struct oxygen_model model_xonar = {
351 	.shortname = "Asus AV200",
352 	.longname = "Asus Virtuoso 200",
353 	.chip = "AV200",
354 	.init = xonar_init,
355 	.control_filter = xonar_control_filter,
356 	.mixer_init = xonar_mixer_init,
357 	.cleanup = xonar_cleanup,
358 	.set_dac_params = set_pcm1796_params,
359 	.set_adc_params = set_cs5381_params,
360 	.update_dac_volume = update_pcm1796_volume,
361 	.update_dac_mute = update_pcm1796_mute,
362 	.ac97_switch_hook = xonar_ac97_switch_hook,
363 	.dac_channels = 8,
364 	.used_channels = OXYGEN_CHANNEL_B |
365 			 OXYGEN_CHANNEL_C |
366 			 OXYGEN_CHANNEL_SPDIF |
367 			 OXYGEN_CHANNEL_MULTICH,
368 	.function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
369 	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
370 	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
371 };
372 
373 static int __devinit xonar_probe(struct pci_dev *pci,
374 				 const struct pci_device_id *pci_id)
375 {
376 	static int dev;
377 	int err;
378 
379 	if (dev >= SNDRV_CARDS)
380 		return -ENODEV;
381 	if (!enable[dev]) {
382 		++dev;
383 		return -ENOENT;
384 	}
385 	err = oxygen_pci_probe(pci, index[dev], id[dev], 1, &model_xonar);
386 	if (err >= 0)
387 		++dev;
388 	return err;
389 }
390 
391 static struct pci_driver xonar_driver = {
392 	.name = "AV200",
393 	.id_table = xonar_ids,
394 	.probe = xonar_probe,
395 	.remove = __devexit_p(oxygen_pci_remove),
396 };
397 
398 static int __init alsa_card_xonar_init(void)
399 {
400 	return pci_register_driver(&xonar_driver);
401 }
402 
403 static void __exit alsa_card_xonar_exit(void)
404 {
405 	pci_unregister_driver(&xonar_driver);
406 }
407 
408 module_init(alsa_card_xonar_init)
409 module_exit(alsa_card_xonar_exit)
410