xref: /openbmc/linux/sound/pci/oxygen/virtuoso.c (revision af9af174)
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 #if 0
178 	unsigned int i;
179 	u8 value;
180 
181 	value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
182 	for (i = 0; i < 4; ++i)
183 		pcm1796_write(chip, i, 20, value);
184 #endif
185 }
186 
187 static void update_pcm1796_volume(struct oxygen *chip)
188 {
189 	unsigned int i;
190 
191 	for (i = 0; i < 4; ++i) {
192 		pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
193 		pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
194 	}
195 }
196 
197 static void update_pcm1796_mute(struct oxygen *chip)
198 {
199 	unsigned int i;
200 	u8 value;
201 
202 	value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
203 	if (chip->dac_mute)
204 		value |= PCM1796_MUTE;
205 	for (i = 0; i < 4; ++i)
206 		pcm1796_write(chip, i, 18, value);
207 }
208 
209 static void set_cs53x1_params(struct oxygen *chip,
210 			      struct snd_pcm_hw_params *params)
211 {
212 	unsigned int value;
213 
214 	if (params_rate(params) <= 54000)
215 		value = GPIO_CS53x1_M_SINGLE;
216 	else if (params_rate(params) <= 108000)
217 		value = GPIO_CS53x1_M_DOUBLE;
218 	else
219 		value = GPIO_CS53x1_M_QUAD;
220 	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
221 			      value, GPIO_CS53x1_M_MASK);
222 }
223 
224 static void xonar_gpio_changed(struct oxygen *chip)
225 {
226 	struct xonar_data *data = chip->model_data;
227 	u8 has_power;
228 
229 	has_power = !!(oxygen_read8(chip, data->ext_power_reg)
230 		       & data->ext_power_bit);
231 	if (has_power != data->has_power) {
232 		data->has_power = has_power;
233 		if (has_power) {
234 			snd_printk(KERN_NOTICE "power restored\n");
235 		} else {
236 			snd_printk(KERN_CRIT
237 				   "Hey! Don't unplug the power cable!\n");
238 			/* TODO: stop PCMs */
239 		}
240 	}
241 }
242 
243 static int pcm1796_volume_info(struct snd_kcontrol *ctl,
244 			       struct snd_ctl_elem_info *info)
245 {
246 	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
247 	info->count = 8;
248 	info->value.integer.min = 0x0f;
249 	info->value.integer.max = 0xff;
250 	return 0;
251 }
252 
253 static int alt_switch_get(struct snd_kcontrol *ctl,
254 			  struct snd_ctl_elem_value *value)
255 {
256 	struct oxygen *chip = ctl->private_data;
257 
258 	value->value.integer.value[0] =
259 		!!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_D2_ALT);
260 	return 0;
261 }
262 
263 static int alt_switch_put(struct snd_kcontrol *ctl,
264 			  struct snd_ctl_elem_value *value)
265 {
266 	struct oxygen *chip = ctl->private_data;
267 	u16 old_bits, new_bits;
268 	int changed;
269 
270 	spin_lock_irq(&chip->reg_lock);
271 	old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
272 	if (value->value.integer.value[0])
273 		new_bits = old_bits | GPIO_D2_ALT;
274 	else
275 		new_bits = old_bits & ~GPIO_D2_ALT;
276 	changed = new_bits != old_bits;
277 	if (changed)
278 		oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
279 	spin_unlock_irq(&chip->reg_lock);
280 	return changed;
281 }
282 
283 static const struct snd_kcontrol_new alt_switch = {
284 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
285 	.name = "Analog Loopback Switch",
286 	.info = snd_ctl_boolean_mono_info,
287 	.get = alt_switch_get,
288 	.put = alt_switch_put,
289 };
290 
291 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
292 
293 static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
294 {
295 	if (!strcmp(template->name, "Master Playback Volume")) {
296 		template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
297 		template->info = pcm1796_volume_info,
298 		template->tlv.p = pcm1796_db_scale;
299 	} else if (!strncmp(template->name, "CD Capture ", 11)) {
300 		/* CD in is actually connected to the video in pin */
301 		template->private_value ^= AC97_CD ^ AC97_VIDEO;
302 	}
303 	return 0;
304 }
305 
306 static int xonar_mixer_init(struct oxygen *chip)
307 {
308 	return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
309 }
310 
311 static const struct oxygen_model xonar_models[] = {
312 	[MODEL_D2] = {
313 		.shortname = "Asus AV200",
314 		.longname = "Asus Virtuoso 200",
315 		.chip = "AV200",
316 		.owner = THIS_MODULE,
317 		.init = xonar_d2_init,
318 		.control_filter = xonar_d2_control_filter,
319 		.mixer_init = xonar_mixer_init,
320 		.cleanup = xonar_cleanup,
321 		.set_dac_params = set_pcm1796_params,
322 		.set_adc_params = set_cs53x1_params,
323 		.update_dac_volume = update_pcm1796_volume,
324 		.update_dac_mute = update_pcm1796_mute,
325 		.model_data_size = sizeof(struct xonar_data),
326 		.pcm_dev_cfg = PLAYBACK_0_TO_I2S |
327 			       PLAYBACK_1_TO_SPDIF |
328 			       CAPTURE_0_FROM_I2S_2 |
329 			       CAPTURE_1_FROM_SPDIF,
330 		.dac_channels = 8,
331 		.misc_flags = OXYGEN_MISC_MIDI,
332 		.function_flags = OXYGEN_FUNCTION_SPI |
333 				  OXYGEN_FUNCTION_ENABLE_SPI_4_5,
334 		.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
335 		.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
336 	},
337 	[MODEL_D2X] = {
338 		.shortname = "Asus AV200",
339 		.longname = "Asus Virtuoso 200",
340 		.chip = "AV200",
341 		.owner = THIS_MODULE,
342 		.init = xonar_d2x_init,
343 		.control_filter = xonar_d2_control_filter,
344 		.mixer_init = xonar_mixer_init,
345 		.cleanup = xonar_cleanup,
346 		.set_dac_params = set_pcm1796_params,
347 		.set_adc_params = set_cs53x1_params,
348 		.update_dac_volume = update_pcm1796_volume,
349 		.update_dac_mute = update_pcm1796_mute,
350 		.gpio_changed = xonar_gpio_changed,
351 		.model_data_size = sizeof(struct xonar_data),
352 		.pcm_dev_cfg = PLAYBACK_0_TO_I2S |
353 			       PLAYBACK_1_TO_SPDIF |
354 			       CAPTURE_0_FROM_I2S_2 |
355 			       CAPTURE_1_FROM_SPDIF,
356 		.dac_channels = 8,
357 		.misc_flags = OXYGEN_MISC_MIDI,
358 		.function_flags = OXYGEN_FUNCTION_SPI |
359 				  OXYGEN_FUNCTION_ENABLE_SPI_4_5,
360 		.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
361 		.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
362 	},
363 };
364 
365 static int __devinit xonar_probe(struct pci_dev *pci,
366 				 const struct pci_device_id *pci_id)
367 {
368 	static int dev;
369 	int err;
370 
371 	if (dev >= SNDRV_CARDS)
372 		return -ENODEV;
373 	if (!enable[dev]) {
374 		++dev;
375 		return -ENOENT;
376 	}
377 	err = oxygen_pci_probe(pci, index[dev], id[dev],
378 			       &xonar_models[pci_id->driver_data]);
379 	if (err >= 0)
380 		++dev;
381 	return err;
382 }
383 
384 static struct pci_driver xonar_driver = {
385 	.name = "AV200",
386 	.id_table = xonar_ids,
387 	.probe = xonar_probe,
388 	.remove = __devexit_p(oxygen_pci_remove),
389 };
390 
391 static int __init alsa_card_xonar_init(void)
392 {
393 	return pci_register_driver(&xonar_driver);
394 }
395 
396 static void __exit alsa_card_xonar_exit(void)
397 {
398 	pci_unregister_driver(&xonar_driver);
399 }
400 
401 module_init(alsa_card_xonar_init)
402 module_exit(alsa_card_xonar_exit)
403