xref: /openbmc/linux/sound/pci/oxygen/virtuoso.c (revision 80647ee2)
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 
35 #include <linux/pci.h>
36 #include <linux/delay.h>
37 #include <linux/mutex.h>
38 #include <sound/ac97_codec.h>
39 #include <sound/control.h>
40 #include <sound/core.h>
41 #include <sound/initval.h>
42 #include <sound/pcm.h>
43 #include <sound/tlv.h>
44 #include "oxygen.h"
45 #include "cm9780.h"
46 #include "pcm1796.h"
47 
48 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
49 MODULE_DESCRIPTION("Asus AV200 driver");
50 MODULE_LICENSE("GPL");
51 MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
52 
53 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
54 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
55 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
56 
57 module_param_array(index, int, NULL, 0444);
58 MODULE_PARM_DESC(index, "card index");
59 module_param_array(id, charp, NULL, 0444);
60 MODULE_PARM_DESC(id, "ID string");
61 module_param_array(enable, bool, NULL, 0444);
62 MODULE_PARM_DESC(enable, "enable card");
63 
64 enum {
65 	MODEL_D2,
66 	MODEL_D2X,
67 };
68 
69 static struct pci_device_id xonar_ids[] __devinitdata = {
70 	{ OXYGEN_PCI_SUBID(0x1043, 0x8269), .driver_data = MODEL_D2 },
71 	{ OXYGEN_PCI_SUBID(0x1043, 0x82b7), .driver_data = MODEL_D2X },
72 	{ }
73 };
74 MODULE_DEVICE_TABLE(pci, xonar_ids);
75 
76 
77 #define GPIO_CS53x1_M_MASK	0x000c
78 #define GPIO_CS53x1_M_SINGLE	0x0000
79 #define GPIO_CS53x1_M_DOUBLE	0x0004
80 #define GPIO_CS53x1_M_QUAD	0x0008
81 
82 #define GPIO_D2X_EXT_POWER	0x0020
83 #define GPIO_D2_ALT		0x0080
84 #define GPIO_D2_OUTPUT_ENABLE	0x0100
85 
86 struct xonar_data {
87 	unsigned int anti_pop_delay;
88 	u16 output_enable_bit;
89 	u8 ext_power_reg;
90 	u8 ext_power_int_reg;
91 	u8 ext_power_bit;
92 	u8 has_power;
93 };
94 
95 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
96 			  u8 reg, u8 value)
97 {
98 	/* maps ALSA channel pair number to SPI output */
99 	static const u8 codec_map[4] = {
100 		0, 1, 2, 4
101 	};
102 	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER  |
103 			 OXYGEN_SPI_DATA_LENGTH_2 |
104 			 OXYGEN_SPI_CLOCK_160 |
105 			 (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
106 			 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
107 			 (reg << 8) | value);
108 }
109 
110 static void xonar_common_init(struct oxygen *chip)
111 {
112 	struct xonar_data *data = chip->model_data;
113 
114 	if (data->ext_power_reg) {
115 		oxygen_set_bits8(chip, data->ext_power_int_reg,
116 				 data->ext_power_bit);
117 		chip->interrupt_mask |= OXYGEN_INT_GPIO;
118 		data->has_power = !!(oxygen_read8(chip, data->ext_power_reg)
119 				     & data->ext_power_bit);
120 	}
121 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_CS53x1_M_MASK);
122 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
123 			      GPIO_CS53x1_M_SINGLE, GPIO_CS53x1_M_MASK);
124 	oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
125 	msleep(data->anti_pop_delay);
126 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, data->output_enable_bit);
127 	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
128 }
129 
130 static void xonar_d2_init(struct oxygen *chip)
131 {
132 	struct xonar_data *data = chip->model_data;
133 	unsigned int i;
134 
135 	data->anti_pop_delay = 300;
136 	data->output_enable_bit = GPIO_D2_OUTPUT_ENABLE;
137 
138 	for (i = 0; i < 4; ++i) {
139 		pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
140 		pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
141 		pcm1796_write(chip, i, 20, PCM1796_OS_64);
142 		pcm1796_write(chip, i, 21, 0);
143 		pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
144 		pcm1796_write(chip, i, 17, 0xff);
145 	}
146 
147 	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT);
148 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT);
149 
150 	xonar_common_init(chip);
151 
152 	snd_component_add(chip->card, "PCM1796");
153 	snd_component_add(chip->card, "CS5381");
154 }
155 
156 static void xonar_d2x_init(struct oxygen *chip)
157 {
158 	struct xonar_data *data = chip->model_data;
159 
160 	data->ext_power_reg = OXYGEN_GPIO_DATA;
161 	data->ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK;
162 	data->ext_power_bit = GPIO_D2X_EXT_POWER;
163 	oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2X_EXT_POWER);
164 	xonar_d2_init(chip);
165 }
166 
167 static void xonar_cleanup(struct oxygen *chip)
168 {
169 	struct xonar_data *data = chip->model_data;
170 
171 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, data->output_enable_bit);
172 }
173 
174 static void set_pcm1796_params(struct oxygen *chip,
175 			       struct snd_pcm_hw_params *params)
176 {
177 	unsigned int i;
178 	u8 value;
179 
180 	value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
181 	for (i = 0; i < 4; ++i)
182 		pcm1796_write(chip, i, 20, value);
183 }
184 
185 static void update_pcm1796_volume(struct oxygen *chip)
186 {
187 	unsigned int i;
188 
189 	for (i = 0; i < 4; ++i) {
190 		pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
191 		pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
192 	}
193 }
194 
195 static void update_pcm1796_mute(struct oxygen *chip)
196 {
197 	unsigned int i;
198 	u8 value;
199 
200 	value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
201 	if (chip->dac_mute)
202 		value |= PCM1796_MUTE;
203 	for (i = 0; i < 4; ++i)
204 		pcm1796_write(chip, i, 18, value);
205 }
206 
207 static void set_cs53x1_params(struct oxygen *chip,
208 			      struct snd_pcm_hw_params *params)
209 {
210 	unsigned int value;
211 
212 	if (params_rate(params) <= 54000)
213 		value = GPIO_CS53x1_M_SINGLE;
214 	else if (params_rate(params) <= 108000)
215 		value = GPIO_CS53x1_M_DOUBLE;
216 	else
217 		value = GPIO_CS53x1_M_QUAD;
218 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
219 			      value, GPIO_CS53x1_M_MASK);
220 }
221 
222 static void xonar_gpio_changed(struct oxygen *chip)
223 {
224 	struct xonar_data *data = chip->model_data;
225 	u8 has_power;
226 
227 	has_power = !!(oxygen_read8(chip, data->ext_power_reg)
228 		       & data->ext_power_bit);
229 	if (has_power != data->has_power) {
230 		data->has_power = has_power;
231 		if (has_power) {
232 			snd_printk(KERN_NOTICE "power restored\n");
233 		} else {
234 			snd_printk(KERN_CRIT
235 				   "Hey! Don't unplug the power cable!\n");
236 			/* TODO: stop PCMs */
237 		}
238 	}
239 }
240 
241 static int pcm1796_volume_info(struct snd_kcontrol *ctl,
242 			       struct snd_ctl_elem_info *info)
243 {
244 	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
245 	info->count = 8;
246 	info->value.integer.min = 0x0f;
247 	info->value.integer.max = 0xff;
248 	return 0;
249 }
250 
251 static int alt_switch_get(struct snd_kcontrol *ctl,
252 			  struct snd_ctl_elem_value *value)
253 {
254 	struct oxygen *chip = ctl->private_data;
255 
256 	value->value.integer.value[0] =
257 		!!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_D2_ALT);
258 	return 0;
259 }
260 
261 static int alt_switch_put(struct snd_kcontrol *ctl,
262 			  struct snd_ctl_elem_value *value)
263 {
264 	struct oxygen *chip = ctl->private_data;
265 	u16 old_bits, new_bits;
266 	int changed;
267 
268 	spin_lock_irq(&chip->reg_lock);
269 	old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
270 	if (value->value.integer.value[0])
271 		new_bits = old_bits | GPIO_D2_ALT;
272 	else
273 		new_bits = old_bits & ~GPIO_D2_ALT;
274 	changed = new_bits != old_bits;
275 	if (changed)
276 		oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
277 	spin_unlock_irq(&chip->reg_lock);
278 	return changed;
279 }
280 
281 static const struct snd_kcontrol_new alt_switch = {
282 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
283 	.name = "Analog Loopback Switch",
284 	.info = snd_ctl_boolean_mono_info,
285 	.get = alt_switch_get,
286 	.put = alt_switch_put,
287 };
288 
289 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
290 
291 static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
292 {
293 	if (!strcmp(template->name, "Master Playback Volume")) {
294 		template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
295 		template->info = pcm1796_volume_info;
296 		template->tlv.p = pcm1796_db_scale;
297 	} else if (!strncmp(template->name, "CD Capture ", 11)) {
298 		/* CD in is actually connected to the video in pin */
299 		template->private_value ^= AC97_CD ^ AC97_VIDEO;
300 	}
301 	return 0;
302 }
303 
304 static int xonar_mixer_init(struct oxygen *chip)
305 {
306 	return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
307 }
308 
309 static const struct oxygen_model xonar_models[] = {
310 	[MODEL_D2] = {
311 		.shortname = "Xonar D2",
312 		.longname = "Asus Virtuoso 200",
313 		.chip = "AV200",
314 		.owner = THIS_MODULE,
315 		.init = xonar_d2_init,
316 		.control_filter = xonar_d2_control_filter,
317 		.mixer_init = xonar_mixer_init,
318 		.cleanup = xonar_cleanup,
319 		.set_dac_params = set_pcm1796_params,
320 		.set_adc_params = set_cs53x1_params,
321 		.update_dac_volume = update_pcm1796_volume,
322 		.update_dac_mute = update_pcm1796_mute,
323 		.model_data_size = sizeof(struct xonar_data),
324 		.pcm_dev_cfg = PLAYBACK_0_TO_I2S |
325 			       PLAYBACK_1_TO_SPDIF |
326 			       CAPTURE_0_FROM_I2S_2 |
327 			       CAPTURE_1_FROM_SPDIF,
328 		.dac_channels = 8,
329 		.misc_flags = OXYGEN_MISC_MIDI,
330 		.function_flags = OXYGEN_FUNCTION_SPI |
331 				  OXYGEN_FUNCTION_ENABLE_SPI_4_5,
332 		.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
333 		.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
334 	},
335 	[MODEL_D2X] = {
336 		.shortname = "Xonar D2X",
337 		.longname = "Asus Virtuoso 200",
338 		.chip = "AV200",
339 		.owner = THIS_MODULE,
340 		.init = xonar_d2x_init,
341 		.control_filter = xonar_d2_control_filter,
342 		.mixer_init = xonar_mixer_init,
343 		.cleanup = xonar_cleanup,
344 		.set_dac_params = set_pcm1796_params,
345 		.set_adc_params = set_cs53x1_params,
346 		.update_dac_volume = update_pcm1796_volume,
347 		.update_dac_mute = update_pcm1796_mute,
348 		.gpio_changed = xonar_gpio_changed,
349 		.model_data_size = sizeof(struct xonar_data),
350 		.pcm_dev_cfg = PLAYBACK_0_TO_I2S |
351 			       PLAYBACK_1_TO_SPDIF |
352 			       CAPTURE_0_FROM_I2S_2 |
353 			       CAPTURE_1_FROM_SPDIF,
354 		.dac_channels = 8,
355 		.misc_flags = OXYGEN_MISC_MIDI,
356 		.function_flags = OXYGEN_FUNCTION_SPI |
357 				  OXYGEN_FUNCTION_ENABLE_SPI_4_5,
358 		.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
359 		.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
360 	},
361 };
362 
363 static int __devinit xonar_probe(struct pci_dev *pci,
364 				 const struct pci_device_id *pci_id)
365 {
366 	static int dev;
367 	int err;
368 
369 	if (dev >= SNDRV_CARDS)
370 		return -ENODEV;
371 	if (!enable[dev]) {
372 		++dev;
373 		return -ENOENT;
374 	}
375 	err = oxygen_pci_probe(pci, index[dev], id[dev],
376 			       &xonar_models[pci_id->driver_data]);
377 	if (err >= 0)
378 		++dev;
379 	return err;
380 }
381 
382 static struct pci_driver xonar_driver = {
383 	.name = "AV200",
384 	.id_table = xonar_ids,
385 	.probe = xonar_probe,
386 	.remove = __devexit_p(oxygen_pci_remove),
387 };
388 
389 static int __init alsa_card_xonar_init(void)
390 {
391 	return pci_register_driver(&xonar_driver);
392 }
393 
394 static void __exit alsa_card_xonar_exit(void)
395 {
396 	pci_unregister_driver(&xonar_driver);
397 }
398 
399 module_init(alsa_card_xonar_init)
400 module_exit(alsa_card_xonar_exit)
401