2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 * Routines for control of EMU10K1 chips
6 * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
7 * Added support for Audigy 2 Value.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sound/driver.h>
33 #include <linux/delay.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/pci.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
40 #include <sound/core.h>
41 #include <sound/emu10k1.h>
45 /*************************************************************************
47 *************************************************************************/
49 void snd_emu10k1_voice_init(struct snd_emu10k1 * emu, int ch)
51 snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
52 snd_emu10k1_ptr_write(emu, IP, ch, 0);
53 snd_emu10k1_ptr_write(emu, VTFT, ch, 0xffff);
54 snd_emu10k1_ptr_write(emu, CVCF, ch, 0xffff);
55 snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
56 snd_emu10k1_ptr_write(emu, CPF, ch, 0);
57 snd_emu10k1_ptr_write(emu, CCR, ch, 0);
59 snd_emu10k1_ptr_write(emu, PSST, ch, 0);
60 snd_emu10k1_ptr_write(emu, DSL, ch, 0x10);
61 snd_emu10k1_ptr_write(emu, CCCA, ch, 0);
62 snd_emu10k1_ptr_write(emu, Z1, ch, 0);
63 snd_emu10k1_ptr_write(emu, Z2, ch, 0);
64 snd_emu10k1_ptr_write(emu, FXRT, ch, 0x32100000);
66 snd_emu10k1_ptr_write(emu, ATKHLDM, ch, 0);
67 snd_emu10k1_ptr_write(emu, DCYSUSM, ch, 0);
68 snd_emu10k1_ptr_write(emu, IFATN, ch, 0xffff);
69 snd_emu10k1_ptr_write(emu, PEFE, ch, 0);
70 snd_emu10k1_ptr_write(emu, FMMOD, ch, 0);
71 snd_emu10k1_ptr_write(emu, TREMFRQ, ch, 24); /* 1 Hz */
72 snd_emu10k1_ptr_write(emu, FM2FRQ2, ch, 24); /* 1 Hz */
73 snd_emu10k1_ptr_write(emu, TEMPENV, ch, 0);
75 /*** these are last so OFF prevents writing ***/
76 snd_emu10k1_ptr_write(emu, LFOVAL2, ch, 0);
77 snd_emu10k1_ptr_write(emu, LFOVAL1, ch, 0);
78 snd_emu10k1_ptr_write(emu, ATKHLDV, ch, 0);
79 snd_emu10k1_ptr_write(emu, ENVVOL, ch, 0);
80 snd_emu10k1_ptr_write(emu, ENVVAL, ch, 0);
82 /* Audigy extra stuffs */
84 snd_emu10k1_ptr_write(emu, 0x4c, ch, 0); /* ?? */
85 snd_emu10k1_ptr_write(emu, 0x4d, ch, 0); /* ?? */
86 snd_emu10k1_ptr_write(emu, 0x4e, ch, 0); /* ?? */
87 snd_emu10k1_ptr_write(emu, 0x4f, ch, 0); /* ?? */
88 snd_emu10k1_ptr_write(emu, A_FXRT1, ch, 0x03020100);
89 snd_emu10k1_ptr_write(emu, A_FXRT2, ch, 0x3f3f3f3f);
90 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, ch, 0);
94 static int snd_emu10k1_init(struct snd_emu10k1 *emu, int enable_ir, int resume)
96 unsigned int silent_page;
99 /* disable audio and lock cache */
100 outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE,
103 /* reset recording buffers */
104 snd_emu10k1_ptr_write(emu, MICBS, 0, ADCBS_BUFSIZE_NONE);
105 snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
106 snd_emu10k1_ptr_write(emu, FXBS, 0, ADCBS_BUFSIZE_NONE);
107 snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
108 snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
109 snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
111 /* disable channel interrupt */
112 outl(0, emu->port + INTE);
113 snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
114 snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
115 snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
116 snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
119 /* set SPDIF bypass mode */
120 snd_emu10k1_ptr_write(emu, SPBYPASS, 0, SPBYPASS_FORMAT);
121 /* enable rear left + rear right AC97 slots */
122 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_REAR_RIGHT |
126 /* init envelope engine */
127 for (ch = 0; ch < NUM_G; ch++)
128 snd_emu10k1_voice_init(emu, ch);
130 snd_emu10k1_ptr_write(emu, SPCS0, 0, emu->spdif_bits[0]);
131 snd_emu10k1_ptr_write(emu, SPCS1, 0, emu->spdif_bits[1]);
132 snd_emu10k1_ptr_write(emu, SPCS2, 0, emu->spdif_bits[2]);
134 if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
135 /* Hacks for Alice3 to work independent of haP16V driver */
138 //Setup SRCMulti_I2S SamplingRate
139 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
142 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
144 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
145 snd_emu10k1_ptr20_write(emu, SRCSel, 0, 0x14);
146 /* Setup SRCMulti Input Audio Enable */
147 /* Use 0xFFFFFFFF to enable P16V sounds. */
148 snd_emu10k1_ptr20_write(emu, SRCMULTI_ENABLE, 0, 0xFFFFFFFF);
150 /* Enabled Phased (8-channel) P16V playback */
151 outl(0x0201, emu->port + HCFG2);
152 /* Set playback routing. */
153 snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, 0x78e4);
155 if (emu->card_capabilities->ca0108_chip) { /* audigy2 Value */
156 /* Hacks for Alice3 to work independent of haP16V driver */
159 snd_printk(KERN_INFO "Audigy2 value: Special config.\n");
160 //Setup SRCMulti_I2S SamplingRate
161 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
164 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
166 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
167 outl(0x600000, emu->port + 0x20);
168 outl(0x14, emu->port + 0x24);
170 /* Setup SRCMulti Input Audio Enable */
171 outl(0x7b0000, emu->port + 0x20);
172 outl(0xFF000000, emu->port + 0x24);
174 /* Setup SPDIF Out Audio Enable */
175 /* The Audigy 2 Value has a separate SPDIF out,
176 * so no need for a mixer switch
178 outl(0x7a0000, emu->port + 0x20);
179 outl(0xFF000000, emu->port + 0x24);
180 tmp = inl(emu->port + A_IOCFG) & ~0x8; /* Clear bit 3 */
181 outl(tmp, emu->port + A_IOCFG);
184 snd_emu10k1_ptr_write(emu, PTB, 0, emu->ptb_pages.addr);
185 snd_emu10k1_ptr_write(emu, TCB, 0, 0); /* taken from original driver */
186 snd_emu10k1_ptr_write(emu, TCBS, 0, 4); /* taken from original driver */
188 silent_page = (emu->silent_page.addr << 1) | MAP_PTI_MASK;
189 for (ch = 0; ch < NUM_G; ch++) {
190 snd_emu10k1_ptr_write(emu, MAPA, ch, silent_page);
191 snd_emu10k1_ptr_write(emu, MAPB, ch, silent_page);
196 * Mute Disable Audio = 0
197 * Lock Tank Memory = 1
198 * Lock Sound Memory = 0
202 if (emu->revision == 4) /* audigy2 */
203 outl(HCFG_AUDIOENABLE |
204 HCFG_AC3ENABLE_CDSPDIF |
205 HCFG_AC3ENABLE_GPSPDIF |
206 HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
208 outl(HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
209 /* FIXME: Remove all these emu->model and replace it with a card recognition parameter,
210 * e.g. card_capabilities->joystick */
211 } else if (emu->model == 0x20 ||
212 emu->model == 0xc400 ||
213 (emu->model == 0x21 && emu->revision < 6))
214 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE, emu->port + HCFG);
216 // With on-chip joystick
217 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
219 if (enable_ir) { /* enable IR for SB Live */
221 unsigned int reg = inl(emu->port + A_IOCFG);
222 outl(reg | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
224 outl(reg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
226 outl(reg, emu->port + A_IOCFG);
228 unsigned int reg = inl(emu->port + HCFG);
229 outl(reg | HCFG_GPOUT2, emu->port + HCFG);
231 outl(reg | HCFG_GPOUT1 | HCFG_GPOUT2, emu->port + HCFG);
233 outl(reg, emu->port + HCFG);
237 if (emu->audigy) { /* enable analog output */
238 unsigned int reg = inl(emu->port + A_IOCFG);
239 outl(reg | A_IOCFG_GPOUT0, emu->port + A_IOCFG);
245 static void snd_emu10k1_audio_enable(struct snd_emu10k1 *emu)
248 * Enable the audio bit
250 outl(inl(emu->port + HCFG) | HCFG_AUDIOENABLE, emu->port + HCFG);
252 /* Enable analog/digital outs on audigy */
254 outl(inl(emu->port + A_IOCFG) & ~0x44, emu->port + A_IOCFG);
256 if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
257 /* Unmute Analog now. Set GPO6 to 1 for Apollo.
258 * This has to be done after init ALice3 I2SOut beyond 48KHz.
259 * So, sequence is important. */
260 outl(inl(emu->port + A_IOCFG) | 0x0040, emu->port + A_IOCFG);
261 } else if (emu->card_capabilities->ca0108_chip) { /* audigy2 value */
262 /* Unmute Analog now. */
263 outl(inl(emu->port + A_IOCFG) | 0x0060, emu->port + A_IOCFG);
265 /* Disable routing from AC97 line out to Front speakers */
266 outl(inl(emu->port + A_IOCFG) | 0x0080, emu->port + A_IOCFG);
273 /* FIXME: the following routine disables LiveDrive-II !! */
276 tmp = inl(emu->port + HCFG);
277 if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
278 outl(tmp|0x800, emu->port + HCFG);
280 if (tmp != (inl(emu->port + HCFG) & ~0x800)) {
282 outl(tmp, emu->port + HCFG);
288 snd_emu10k1_intr_enable(emu, INTE_PCIERRORENABLE);
291 int snd_emu10k1_done(struct snd_emu10k1 * emu)
295 outl(0, emu->port + INTE);
300 for (ch = 0; ch < NUM_G; ch++)
301 snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
302 for (ch = 0; ch < NUM_G; ch++) {
303 snd_emu10k1_ptr_write(emu, VTFT, ch, 0);
304 snd_emu10k1_ptr_write(emu, CVCF, ch, 0);
305 snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
306 snd_emu10k1_ptr_write(emu, CPF, ch, 0);
309 /* reset recording buffers */
310 snd_emu10k1_ptr_write(emu, MICBS, 0, 0);
311 snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
312 snd_emu10k1_ptr_write(emu, FXBS, 0, 0);
313 snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
314 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
315 snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
316 snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
317 snd_emu10k1_ptr_write(emu, TCBS, 0, TCBS_BUFFSIZE_16K);
318 snd_emu10k1_ptr_write(emu, TCB, 0, 0);
320 snd_emu10k1_ptr_write(emu, A_DBG, 0, A_DBG_SINGLE_STEP);
322 snd_emu10k1_ptr_write(emu, DBG, 0, EMU10K1_DBG_SINGLE_STEP);
324 /* disable channel interrupt */
325 snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
326 snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
327 snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
328 snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
330 /* disable audio and lock cache */
331 outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, emu->port + HCFG);
332 snd_emu10k1_ptr_write(emu, PTB, 0, 0);
337 /*************************************************************************
338 * ECARD functional implementation
339 *************************************************************************/
341 /* In A1 Silicon, these bits are in the HC register */
342 #define HOOKN_BIT (1L << 12)
343 #define HANDN_BIT (1L << 11)
344 #define PULSEN_BIT (1L << 10)
346 #define EC_GDI1 (1 << 13)
347 #define EC_GDI0 (1 << 14)
349 #define EC_NUM_CONTROL_BITS 20
351 #define EC_AC3_DATA_SELN 0x0001L
352 #define EC_EE_DATA_SEL 0x0002L
353 #define EC_EE_CNTRL_SELN 0x0004L
354 #define EC_EECLK 0x0008L
355 #define EC_EECS 0x0010L
356 #define EC_EESDO 0x0020L
357 #define EC_TRIM_CSN 0x0040L
358 #define EC_TRIM_SCLK 0x0080L
359 #define EC_TRIM_SDATA 0x0100L
360 #define EC_TRIM_MUTEN 0x0200L
361 #define EC_ADCCAL 0x0400L
362 #define EC_ADCRSTN 0x0800L
363 #define EC_DACCAL 0x1000L
364 #define EC_DACMUTEN 0x2000L
365 #define EC_LEDN 0x4000L
367 #define EC_SPDIF0_SEL_SHIFT 15
368 #define EC_SPDIF1_SEL_SHIFT 17
369 #define EC_SPDIF0_SEL_MASK (0x3L << EC_SPDIF0_SEL_SHIFT)
370 #define EC_SPDIF1_SEL_MASK (0x7L << EC_SPDIF1_SEL_SHIFT)
371 #define EC_SPDIF0_SELECT(_x) (((_x) << EC_SPDIF0_SEL_SHIFT) & EC_SPDIF0_SEL_MASK)
372 #define EC_SPDIF1_SELECT(_x) (((_x) << EC_SPDIF1_SEL_SHIFT) & EC_SPDIF1_SEL_MASK)
373 #define EC_CURRENT_PROM_VERSION 0x01 /* Self-explanatory. This should
374 * be incremented any time the EEPROM's
375 * format is changed. */
377 #define EC_EEPROM_SIZE 0x40 /* ECARD EEPROM has 64 16-bit words */
379 /* Addresses for special values stored in to EEPROM */
380 #define EC_PROM_VERSION_ADDR 0x20 /* Address of the current prom version */
381 #define EC_BOARDREV0_ADDR 0x21 /* LSW of board rev */
382 #define EC_BOARDREV1_ADDR 0x22 /* MSW of board rev */
384 #define EC_LAST_PROMFILE_ADDR 0x2f
386 #define EC_SERIALNUM_ADDR 0x30 /* First word of serial number. The
387 * can be up to 30 characters in length
388 * and is stored as a NULL-terminated
389 * ASCII string. Any unused bytes must be
390 * filled with zeros */
391 #define EC_CHECKSUM_ADDR 0x3f /* Location at which checksum is stored */
394 /* Most of this stuff is pretty self-evident. According to the hardware
395 * dudes, we need to leave the ADCCAL bit low in order to avoid a DC
396 * offset problem. Weird.
398 #define EC_RAW_RUN_MODE (EC_DACMUTEN | EC_ADCRSTN | EC_TRIM_MUTEN | \
402 #define EC_DEFAULT_ADC_GAIN 0xC4C4
403 #define EC_DEFAULT_SPDIF0_SEL 0x0
404 #define EC_DEFAULT_SPDIF1_SEL 0x4
406 /**************************************************************************
407 * @func Clock bits into the Ecard's control latch. The Ecard uses a
408 * control latch will is loaded bit-serially by toggling the Modem control
409 * lines from function 2 on the E8010. This function hides these details
410 * and presents the illusion that we are actually writing to a distinct
414 static void snd_emu10k1_ecard_write(struct snd_emu10k1 * emu, unsigned int value)
416 unsigned short count;
418 unsigned long hc_port;
419 unsigned int hc_value;
421 hc_port = emu->port + HCFG;
422 hc_value = inl(hc_port) & ~(HOOKN_BIT | HANDN_BIT | PULSEN_BIT);
423 outl(hc_value, hc_port);
425 for (count = 0; count < EC_NUM_CONTROL_BITS; count++) {
427 /* Set up the value */
428 data = ((value & 0x1) ? PULSEN_BIT : 0);
431 outl(hc_value | data, hc_port);
433 /* Clock the shift register */
434 outl(hc_value | data | HANDN_BIT, hc_port);
435 outl(hc_value | data, hc_port);
439 outl(hc_value | HOOKN_BIT, hc_port);
440 outl(hc_value, hc_port);
443 /**************************************************************************
444 * @func Set the gain of the ECARD's CS3310 Trim/gain controller. The
445 * trim value consists of a 16bit value which is composed of two
446 * 8 bit gain/trim values, one for the left channel and one for the
447 * right channel. The following table maps from the Gain/Attenuation
448 * value in decibels into the corresponding bit pattern for a single
452 static void snd_emu10k1_ecard_setadcgain(struct snd_emu10k1 * emu,
457 /* Enable writing to the TRIM registers */
458 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
460 /* Do it again to insure that we meet hold time requirements */
461 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
463 for (bit = (1 << 15); bit; bit >>= 1) {
466 value = emu->ecard_ctrl & ~(EC_TRIM_CSN | EC_TRIM_SDATA);
469 value |= EC_TRIM_SDATA;
472 snd_emu10k1_ecard_write(emu, value);
473 snd_emu10k1_ecard_write(emu, value | EC_TRIM_SCLK);
474 snd_emu10k1_ecard_write(emu, value);
477 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
480 static int __devinit snd_emu10k1_ecard_init(struct snd_emu10k1 * emu)
482 unsigned int hc_value;
484 /* Set up the initial settings */
485 emu->ecard_ctrl = EC_RAW_RUN_MODE |
486 EC_SPDIF0_SELECT(EC_DEFAULT_SPDIF0_SEL) |
487 EC_SPDIF1_SELECT(EC_DEFAULT_SPDIF1_SEL);
489 /* Step 0: Set the codec type in the hardware control register
490 * and enable audio output */
491 hc_value = inl(emu->port + HCFG);
492 outl(hc_value | HCFG_AUDIOENABLE | HCFG_CODECFORMAT_I2S, emu->port + HCFG);
493 inl(emu->port + HCFG);
495 /* Step 1: Turn off the led and deassert TRIM_CS */
496 snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
498 /* Step 2: Calibrate the ADC and DAC */
499 snd_emu10k1_ecard_write(emu, EC_DACCAL | EC_LEDN | EC_TRIM_CSN);
501 /* Step 3: Wait for awhile; XXX We can't get away with this
502 * under a real operating system; we'll need to block and wait that
504 snd_emu10k1_wait(emu, 48000);
506 /* Step 4: Switch off the DAC and ADC calibration. Note
507 * That ADC_CAL is actually an inverted signal, so we assert
508 * it here to stop calibration. */
509 snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
511 /* Step 4: Switch into run mode */
512 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
514 /* Step 5: Set the analog input gain */
515 snd_emu10k1_ecard_setadcgain(emu, EC_DEFAULT_ADC_GAIN);
520 static int __devinit snd_emu10k1_cardbus_init(struct snd_emu10k1 * emu)
522 unsigned long special_port;
525 /* Special initialisation routine
526 * before the rest of the IO-Ports become active.
528 special_port = emu->port + 0x38;
529 value = inl(special_port);
530 outl(0x00d00000, special_port);
531 value = inl(special_port);
532 outl(0x00d00001, special_port);
533 value = inl(special_port);
534 outl(0x00d0005f, special_port);
535 value = inl(special_port);
536 outl(0x00d0007f, special_port);
537 value = inl(special_port);
538 outl(0x0090007f, special_port);
539 value = inl(special_port);
541 snd_emu10k1_ptr20_write(emu, TINA2_VOLUME, 0, 0xfefefefe); /* Defaults to 0x30303030 */
546 * Create the EMU10K1 instance
550 static int alloc_pm_buffer(struct snd_emu10k1 *emu);
551 static void free_pm_buffer(struct snd_emu10k1 *emu);
554 static int snd_emu10k1_free(struct snd_emu10k1 *emu)
556 if (emu->port) { /* avoid access to already used hardware */
557 snd_emu10k1_fx8010_tram_setup(emu, 0);
558 snd_emu10k1_done(emu);
559 /* remove reserved page */
560 if (emu->reserved_page) {
561 snd_emu10k1_synth_free(emu, (struct snd_util_memblk *)emu->reserved_page);
562 emu->reserved_page = NULL;
564 snd_emu10k1_free_efx(emu);
567 snd_util_memhdr_free(emu->memhdr);
568 if (emu->silent_page.area)
569 snd_dma_free_pages(&emu->silent_page);
570 if (emu->ptb_pages.area)
571 snd_dma_free_pages(&emu->ptb_pages);
572 vfree(emu->page_ptr_table);
573 vfree(emu->page_addr_table);
578 free_irq(emu->irq, (void *)emu);
580 pci_release_regions(emu->pci);
581 if (emu->card_capabilities->ca0151_chip) /* P16V */
583 pci_disable_device(emu->pci);
588 static int snd_emu10k1_dev_free(struct snd_device *device)
590 struct snd_emu10k1 *emu = device->device_data;
591 return snd_emu10k1_free(emu);
594 static struct snd_emu_chip_details emu_chip_details[] = {
595 /* Audigy 2 Value AC3 out does not work yet. Need to find out how to turn off interpolators.*/
596 /* Tested by James@superbug.co.uk 3rd July 2005 */
597 {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x10011102,
598 .driver = "Audigy2", .name = "Audigy 2 Value [SB0400]",
604 /* Audigy 2 ZS Notebook Cardbus card.*/
605 /* Tested by James@superbug.co.uk 30th October 2005 */
606 /* Not working yet, but progressing. */
607 {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x20011102,
608 .driver = "Audigy2", .name = "Audigy 2 ZS Notebook [SB0530]",
612 .ca_cardbus_chip = 1,
614 {.vendor = 0x1102, .device = 0x0008,
615 .driver = "Audigy2", .name = "Audigy 2 Value [Unknown]",
620 /* Tested by James@superbug.co.uk 8th July 2005. No sound available yet. */
621 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x40011102,
622 .driver = "Audigy2", .name = "E-mu 1212m [4001]",
627 /* Tested by James@superbug.co.uk 3rd July 2005 */
628 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20071102,
629 .driver = "Audigy2", .name = "Audigy 4 PRO [SB0380]",
637 /* Tested by shane-alsa@cm.nu 5th Nov 2005 */
638 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20061102,
639 .driver = "Audigy2", .name = "Audigy 2 [2006]",
647 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20021102,
648 .driver = "Audigy2", .name = "Audigy 2 ZS [SB0350]",
656 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20011102,
657 .driver = "Audigy2", .name = "Audigy 2 ZS [2001]",
665 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10071102,
666 .driver = "Audigy2", .name = "Audigy 2 [SB0240]",
674 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10051102,
675 .driver = "Audigy2", .name = "Audigy 2 EX [1005]",
682 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10021102,
683 .driver = "Audigy2", .name = "Audigy 2 Platinum [SB0240P]",
691 {.vendor = 0x1102, .device = 0x0004, .revision = 0x04,
692 .driver = "Audigy2", .name = "Audigy 2 [Unknown]",
699 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00531102,
700 .driver = "Audigy", .name = "Audigy 1 [SB0090]",
705 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00521102,
706 .driver = "Audigy", .name = "Audigy 1 ES [SB0160]",
712 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00511102,
713 .driver = "Audigy", .name = "Audigy 1 [SB0090]",
718 {.vendor = 0x1102, .device = 0x0004,
719 .driver = "Audigy", .name = "Audigy 1 [Unknown]",
724 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806B1102,
725 .driver = "EMU10K1", .name = "SBLive! [SB0105]",
730 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806A1102,
731 .driver = "EMU10K1", .name = "SBLive! Value [SB0103]",
736 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80691102,
737 .driver = "EMU10K1", .name = "SBLive! Value [SB0101]",
742 /* Tested by Thomas Zehetbauer 27th Aug 2005 */
743 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80651102,
744 .driver = "EMU10K1", .name = "SB Live 5.1 [SB0220]",
749 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80641102,
750 .driver = "EMU10K1", .name = "SB Live 5.1",
755 /* Tested by alsa bugtrack user "hus" bug #1297 12th Aug 2005 */
756 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80611102,
757 .driver = "EMU10K1", .name = "SBLive 5.1 [SB0060]",
760 .ac97_chip = 2, /* ac97 is optional; both SBLive 5.1 and platinum
761 * share the same IDs!
764 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80511102,
765 .driver = "EMU10K1", .name = "SBLive! Value [CT4850]",
770 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80401102,
771 .driver = "EMU10K1", .name = "SBLive! Platinum [CT4760P]",
775 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80321102,
776 .driver = "EMU10K1", .name = "SBLive! Value [CT4871]",
781 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80311102,
782 .driver = "EMU10K1", .name = "SBLive! Value [CT4831]",
787 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80281102,
788 .driver = "EMU10K1", .name = "SBLive! Value [CT4870]",
793 /* Tested by James@superbug.co.uk 3rd July 2005 */
794 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80271102,
795 .driver = "EMU10K1", .name = "SBLive! Value [CT4832]",
800 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80261102,
801 .driver = "EMU10K1", .name = "SBLive! Value [CT4830]",
806 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80231102,
807 .driver = "EMU10K1", .name = "SB PCI512 [CT4790]",
812 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80221102,
813 .driver = "EMU10K1", .name = "SBLive! Value [CT4780]",
818 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x40011102,
819 .driver = "EMU10K1", .name = "E-mu APS [4001]",
823 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00211102,
824 .driver = "EMU10K1", .name = "SBLive! [CT4620]",
829 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00201102,
830 .driver = "EMU10K1", .name = "SBLive! Value [CT4670]",
835 {.vendor = 0x1102, .device = 0x0002,
836 .driver = "EMU10K1", .name = "SB Live [Unknown]",
844 int __devinit snd_emu10k1_create(struct snd_card *card,
845 struct pci_dev * pci,
846 unsigned short extin_mask,
847 unsigned short extout_mask,
848 long max_cache_bytes,
851 struct snd_emu10k1 ** remu)
853 struct snd_emu10k1 *emu;
856 unsigned char revision;
857 unsigned int silent_page;
858 const struct snd_emu_chip_details *c;
859 static struct snd_device_ops ops = {
860 .dev_free = snd_emu10k1_dev_free,
865 /* enable PCI device */
866 if ((err = pci_enable_device(pci)) < 0)
869 emu = kzalloc(sizeof(*emu), GFP_KERNEL);
871 pci_disable_device(pci);
875 spin_lock_init(&emu->reg_lock);
876 spin_lock_init(&emu->emu_lock);
877 spin_lock_init(&emu->voice_lock);
878 spin_lock_init(&emu->synth_lock);
879 spin_lock_init(&emu->memblk_lock);
880 init_MUTEX(&emu->ptb_lock);
881 init_MUTEX(&emu->fx8010.lock);
882 INIT_LIST_HEAD(&emu->mapped_link_head);
883 INIT_LIST_HEAD(&emu->mapped_order_link_head);
887 emu->get_synth_voice = NULL;
888 /* read revision & serial */
889 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
890 emu->revision = revision;
891 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &emu->serial);
892 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &emu->model);
893 snd_printdd("vendor=0x%x, device=0x%x, subsystem_vendor_id=0x%x, subsystem_id=0x%x\n",pci->vendor, pci->device, emu->serial, emu->model);
895 for (c = emu_chip_details; c->vendor; c++) {
896 if (c->vendor == pci->vendor && c->device == pci->device) {
898 if (c->subsystem && (c->subsystem == subsystem) ) {
902 if (c->subsystem && (c->subsystem != emu->serial) )
904 if (c->revision && c->revision != emu->revision)
910 if (c->vendor == 0) {
911 snd_printk(KERN_ERR "emu10k1: Card not recognised\n");
913 pci_disable_device(pci);
916 emu->card_capabilities = c;
917 if (c->subsystem && !subsystem)
918 snd_printdd("Sound card name=%s\n", c->name);
920 snd_printdd("Sound card name=%s, vendor=0x%x, device=0x%x, subsystem=0x%x. Forced to subsytem=0x%x\n",
921 c->name, pci->vendor, pci->device, emu->serial, c->subsystem);
923 snd_printdd("Sound card name=%s, vendor=0x%x, device=0x%x, subsystem=0x%x.\n",
924 c->name, pci->vendor, pci->device, emu->serial);
926 if (!*card->id && c->id) {
928 strlcpy(card->id, c->id, sizeof(card->id));
930 for (i = 0; i < snd_ecards_limit; i++) {
931 if (snd_cards[i] && !strcmp(snd_cards[i]->id, card->id))
934 if (i >= snd_ecards_limit)
937 if (n >= SNDRV_CARDS)
939 snprintf(card->id, sizeof(card->id), "%s_%d", c->id, n);
943 is_audigy = emu->audigy = c->emu10k2_chip;
945 /* set the DMA transfer mask */
946 emu->dma_mask = is_audigy ? AUDIGY_DMA_MASK : EMU10K1_DMA_MASK;
947 if (pci_set_dma_mask(pci, emu->dma_mask) < 0 ||
948 pci_set_consistent_dma_mask(pci, emu->dma_mask) < 0) {
949 snd_printk(KERN_ERR "architecture does not support PCI busmaster DMA with mask 0x%lx\n", emu->dma_mask);
951 pci_disable_device(pci);
955 emu->gpr_base = A_FXGPREGBASE;
957 emu->gpr_base = FXGPREGBASE;
959 if ((err = pci_request_regions(pci, "EMU10K1")) < 0) {
961 pci_disable_device(pci);
964 emu->port = pci_resource_start(pci, 0);
966 if (request_irq(pci->irq, snd_emu10k1_interrupt, SA_INTERRUPT|SA_SHIRQ, "EMU10K1", (void *)emu)) {
972 emu->max_cache_pages = max_cache_bytes >> PAGE_SHIFT;
973 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
974 32 * 1024, &emu->ptb_pages) < 0) {
979 emu->page_ptr_table = (void **)vmalloc(emu->max_cache_pages * sizeof(void*));
980 emu->page_addr_table = (unsigned long*)vmalloc(emu->max_cache_pages * sizeof(unsigned long));
981 if (emu->page_ptr_table == NULL || emu->page_addr_table == NULL) {
986 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
987 EMUPAGESIZE, &emu->silent_page) < 0) {
991 emu->memhdr = snd_util_memhdr_new(emu->max_cache_pages * PAGE_SIZE);
992 if (emu->memhdr == NULL) {
996 emu->memhdr->block_extra_size = sizeof(struct snd_emu10k1_memblk) -
997 sizeof(struct snd_util_memblk);
1001 emu->fx8010.fxbus_mask = 0x303f;
1002 if (extin_mask == 0)
1003 extin_mask = 0x3fcf;
1004 if (extout_mask == 0)
1005 extout_mask = 0x7fff;
1006 emu->fx8010.extin_mask = extin_mask;
1007 emu->fx8010.extout_mask = extout_mask;
1008 emu->enable_ir = enable_ir;
1010 if (emu->card_capabilities->ecard) {
1011 if ((err = snd_emu10k1_ecard_init(emu)) < 0)
1013 } else if (emu->card_capabilities->ca_cardbus_chip) {
1014 if ((err = snd_emu10k1_cardbus_init(emu)) < 0)
1017 /* 5.1: Enable the additional AC97 Slots. If the emu10k1 version
1018 does not support this, it shouldn't do any harm */
1019 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
1022 /* initialize TRAM setup */
1023 emu->fx8010.itram_size = (16 * 1024)/2;
1024 emu->fx8010.etram_pages.area = NULL;
1025 emu->fx8010.etram_pages.bytes = 0;
1028 * Init to 0x02109204 :
1029 * Clock accuracy = 0 (1000ppm)
1030 * Sample Rate = 2 (48kHz)
1031 * Audio Channel = 1 (Left of 2)
1032 * Source Number = 0 (Unspecified)
1033 * Generation Status = 1 (Original for Cat Code 12)
1034 * Cat Code = 12 (Digital Signal Mixer)
1036 * Emphasis = 0 (None)
1037 * CP = 1 (Copyright unasserted)
1038 * AN = 0 (Audio data)
1041 emu->spdif_bits[0] = emu->spdif_bits[1] =
1042 emu->spdif_bits[2] = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1043 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1044 SPCS_GENERATIONSTATUS | 0x00001200 |
1045 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
1047 emu->reserved_page = (struct snd_emu10k1_memblk *)
1048 snd_emu10k1_synth_alloc(emu, 4096);
1049 if (emu->reserved_page)
1050 emu->reserved_page->map_locked = 1;
1052 /* Clear silent pages and set up pointers */
1053 memset(emu->silent_page.area, 0, PAGE_SIZE);
1054 silent_page = emu->silent_page.addr << 1;
1055 for (idx = 0; idx < MAXPAGES; idx++)
1056 ((u32 *)emu->ptb_pages.area)[idx] = cpu_to_le32(silent_page | idx);
1058 /* set up voice indices */
1059 for (idx = 0; idx < NUM_G; idx++) {
1060 emu->voices[idx].emu = emu;
1061 emu->voices[idx].number = idx;
1064 if ((err = snd_emu10k1_init(emu, enable_ir, 0)) < 0)
1067 if ((err = alloc_pm_buffer(emu)) < 0)
1071 /* Initialize the effect engine */
1072 if ((err = snd_emu10k1_init_efx(emu)) < 0)
1074 snd_emu10k1_audio_enable(emu);
1076 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, emu, &ops)) < 0)
1079 snd_emu10k1_proc_init(emu);
1081 snd_card_set_dev(card, &pci->dev);
1086 snd_emu10k1_free(emu);
1091 static unsigned char saved_regs[] = {
1092 CPF, PTRX, CVCF, VTFT, Z1, Z2, PSST, DSL, CCCA, CCR, CLP,
1093 FXRT, MAPA, MAPB, ENVVOL, ATKHLDV, DCYSUSV, LFOVAL1, ENVVAL,
1094 ATKHLDM, DCYSUSM, LFOVAL2, IP, IFATN, PEFE, FMMOD, TREMFRQ, FM2FRQ2,
1095 TEMPENV, ADCCR, FXWC, MICBA, ADCBA, FXBA,
1096 MICBS, ADCBS, FXBS, CDCS, GPSCS, SPCS0, SPCS1, SPCS2,
1097 SPBYPASS, AC97SLOT, CDSRCS, GPSRCS, ZVSRCS, MICIDX, ADCIDX, FXIDX,
1100 static unsigned char saved_regs_audigy[] = {
1101 A_ADCIDX, A_MICIDX, A_FXWC1, A_FXWC2, A_SAMPLE_RATE,
1102 A_FXRT2, A_SENDAMOUNTS, A_FXRT1,
1106 static int __devinit alloc_pm_buffer(struct snd_emu10k1 *emu)
1110 size = ARRAY_SIZE(saved_regs);
1112 size += ARRAY_SIZE(saved_regs_audigy);
1113 emu->saved_ptr = vmalloc(4 * NUM_G * size);
1114 if (! emu->saved_ptr)
1116 if (snd_emu10k1_efx_alloc_pm_buffer(emu) < 0)
1118 if (emu->card_capabilities->ca0151_chip &&
1119 snd_p16v_alloc_pm_buffer(emu) < 0)
1124 static void free_pm_buffer(struct snd_emu10k1 *emu)
1126 vfree(emu->saved_ptr);
1127 snd_emu10k1_efx_free_pm_buffer(emu);
1128 if (emu->card_capabilities->ca0151_chip)
1129 snd_p16v_free_pm_buffer(emu);
1132 void snd_emu10k1_suspend_regs(struct snd_emu10k1 *emu)
1138 val = emu->saved_ptr;
1139 for (reg = saved_regs; *reg != 0xff; reg++)
1140 for (i = 0; i < NUM_G; i++, val++)
1141 *val = snd_emu10k1_ptr_read(emu, *reg, i);
1143 for (reg = saved_regs_audigy; *reg != 0xff; reg++)
1144 for (i = 0; i < NUM_G; i++, val++)
1145 *val = snd_emu10k1_ptr_read(emu, *reg, i);
1148 emu->saved_a_iocfg = inl(emu->port + A_IOCFG);
1149 emu->saved_hcfg = inl(emu->port + HCFG);
1152 void snd_emu10k1_resume_init(struct snd_emu10k1 *emu)
1154 if (emu->card_capabilities->ecard)
1155 snd_emu10k1_ecard_init(emu);
1157 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
1158 snd_emu10k1_init(emu, emu->enable_ir, 1);
1161 void snd_emu10k1_resume_regs(struct snd_emu10k1 *emu)
1167 snd_emu10k1_audio_enable(emu);
1169 /* resore for spdif */
1171 outl(emu->port + A_IOCFG, emu->saved_a_iocfg);
1172 outl(emu->port + HCFG, emu->saved_hcfg);
1174 val = emu->saved_ptr;
1175 for (reg = saved_regs; *reg != 0xff; reg++)
1176 for (i = 0; i < NUM_G; i++, val++)
1177 snd_emu10k1_ptr_write(emu, *reg, i, *val);
1179 for (reg = saved_regs_audigy; *reg != 0xff; reg++)
1180 for (i = 0; i < NUM_G; i++, val++)
1181 snd_emu10k1_ptr_write(emu, *reg, i, *val);
1187 EXPORT_SYMBOL(snd_emu10k1_synth_alloc);
1188 EXPORT_SYMBOL(snd_emu10k1_synth_free);
1189 EXPORT_SYMBOL(snd_emu10k1_synth_bzero);
1190 EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user);
1191 EXPORT_SYMBOL(snd_emu10k1_memblk_map);
1193 EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
1194 EXPORT_SYMBOL(snd_emu10k1_voice_free);
1196 EXPORT_SYMBOL(snd_emu10k1_ptr_read);
1197 EXPORT_SYMBOL(snd_emu10k1_ptr_write);