blob: f9855073a0a971203a199b0805bfa9fdf239931b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Creative Labs, Inc.
4 * Routines for control of EMU10K1 chips
5 *
6 * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
7 * Added support for Audigy 2 Value.
8 *
9 *
10 * BUGS:
11 * --
12 *
13 * TODO:
14 * --
15 *
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.
20 *
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.
25 *
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
29 *
30 */
31
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>
39
40#include <sound/core.h>
41#include <sound/emu10k1.h>
42#include "p16v.h"
James Courtier-Duttone2b15f82005-11-11 23:39:05 +010043#include "tina2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#if 0
46MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Creative Labs, Inc.");
47MODULE_DESCRIPTION("Routines for control of EMU10K1 chips");
48MODULE_LICENSE("GPL");
49#endif
50
51/*************************************************************************
52 * EMU10K1 init / done
53 *************************************************************************/
54
Takashi Iwaieb4698f2005-11-17 14:50:13 +010055void snd_emu10k1_voice_init(struct snd_emu10k1 * emu, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
58 snd_emu10k1_ptr_write(emu, IP, ch, 0);
59 snd_emu10k1_ptr_write(emu, VTFT, ch, 0xffff);
60 snd_emu10k1_ptr_write(emu, CVCF, ch, 0xffff);
61 snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
62 snd_emu10k1_ptr_write(emu, CPF, ch, 0);
63 snd_emu10k1_ptr_write(emu, CCR, ch, 0);
64
65 snd_emu10k1_ptr_write(emu, PSST, ch, 0);
66 snd_emu10k1_ptr_write(emu, DSL, ch, 0x10);
67 snd_emu10k1_ptr_write(emu, CCCA, ch, 0);
68 snd_emu10k1_ptr_write(emu, Z1, ch, 0);
69 snd_emu10k1_ptr_write(emu, Z2, ch, 0);
70 snd_emu10k1_ptr_write(emu, FXRT, ch, 0x32100000);
71
72 snd_emu10k1_ptr_write(emu, ATKHLDM, ch, 0);
73 snd_emu10k1_ptr_write(emu, DCYSUSM, ch, 0);
74 snd_emu10k1_ptr_write(emu, IFATN, ch, 0xffff);
75 snd_emu10k1_ptr_write(emu, PEFE, ch, 0);
76 snd_emu10k1_ptr_write(emu, FMMOD, ch, 0);
77 snd_emu10k1_ptr_write(emu, TREMFRQ, ch, 24); /* 1 Hz */
78 snd_emu10k1_ptr_write(emu, FM2FRQ2, ch, 24); /* 1 Hz */
79 snd_emu10k1_ptr_write(emu, TEMPENV, ch, 0);
80
81 /*** these are last so OFF prevents writing ***/
82 snd_emu10k1_ptr_write(emu, LFOVAL2, ch, 0);
83 snd_emu10k1_ptr_write(emu, LFOVAL1, ch, 0);
84 snd_emu10k1_ptr_write(emu, ATKHLDV, ch, 0);
85 snd_emu10k1_ptr_write(emu, ENVVOL, ch, 0);
86 snd_emu10k1_ptr_write(emu, ENVVAL, ch, 0);
87
88 /* Audigy extra stuffs */
89 if (emu->audigy) {
90 snd_emu10k1_ptr_write(emu, 0x4c, ch, 0); /* ?? */
91 snd_emu10k1_ptr_write(emu, 0x4d, ch, 0); /* ?? */
92 snd_emu10k1_ptr_write(emu, 0x4e, ch, 0); /* ?? */
93 snd_emu10k1_ptr_write(emu, 0x4f, ch, 0); /* ?? */
94 snd_emu10k1_ptr_write(emu, A_FXRT1, ch, 0x03020100);
95 snd_emu10k1_ptr_write(emu, A_FXRT2, ch, 0x3f3f3f3f);
96 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, ch, 0);
97 }
98}
99
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100100static int __devinit snd_emu10k1_init(struct snd_emu10k1 * emu, int enable_ir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 int ch, idx, err;
103 unsigned int silent_page;
104
105 emu->fx8010.itram_size = (16 * 1024)/2;
106 emu->fx8010.etram_pages.area = NULL;
107 emu->fx8010.etram_pages.bytes = 0;
108
109 /* disable audio and lock cache */
110 outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, emu->port + HCFG);
111
112 /* reset recording buffers */
113 snd_emu10k1_ptr_write(emu, MICBS, 0, ADCBS_BUFSIZE_NONE);
114 snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
115 snd_emu10k1_ptr_write(emu, FXBS, 0, ADCBS_BUFSIZE_NONE);
116 snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
117 snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
118 snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
119
120 /* disable channel interrupt */
121 outl(0, emu->port + INTE);
122 snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
123 snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
124 snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
125 snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
126
127 if (emu->audigy){
128 /* set SPDIF bypass mode */
129 snd_emu10k1_ptr_write(emu, SPBYPASS, 0, SPBYPASS_FORMAT);
130 /* enable rear left + rear right AC97 slots */
131 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_REAR_RIGHT | AC97SLOT_REAR_LEFT);
132 }
133
134 /* init envelope engine */
135 for (ch = 0; ch < NUM_G; ch++) {
136 emu->voices[ch].emu = emu;
137 emu->voices[ch].number = ch;
138 snd_emu10k1_voice_init(emu, ch);
139 }
140
141 /*
142 * Init to 0x02109204 :
143 * Clock accuracy = 0 (1000ppm)
144 * Sample Rate = 2 (48kHz)
145 * Audio Channel = 1 (Left of 2)
146 * Source Number = 0 (Unspecified)
147 * Generation Status = 1 (Original for Cat Code 12)
148 * Cat Code = 12 (Digital Signal Mixer)
149 * Mode = 0 (Mode 0)
150 * Emphasis = 0 (None)
151 * CP = 1 (Copyright unasserted)
152 * AN = 0 (Audio data)
153 * P = 0 (Consumer)
154 */
155 snd_emu10k1_ptr_write(emu, SPCS0, 0,
156 emu->spdif_bits[0] =
157 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
158 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
159 SPCS_GENERATIONSTATUS | 0x00001200 |
160 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
161 snd_emu10k1_ptr_write(emu, SPCS1, 0,
162 emu->spdif_bits[1] =
163 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
164 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
165 SPCS_GENERATIONSTATUS | 0x00001200 |
166 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
167 snd_emu10k1_ptr_write(emu, SPCS2, 0,
168 emu->spdif_bits[2] =
169 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
170 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
171 SPCS_GENERATIONSTATUS | 0x00001200 |
172 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
173
Lee Revell2b637da2005-03-30 13:51:18 +0200174 if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Hacks for Alice3 to work independent of haP16V driver */
176 u32 tmp;
177
178 //Setup SRCMulti_I2S SamplingRate
179 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
180 tmp &= 0xfffff1ff;
181 tmp |= (0x2<<9);
182 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
183
184 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
185 snd_emu10k1_ptr20_write(emu, SRCSel, 0, 0x14);
186 /* Setup SRCMulti Input Audio Enable */
187 /* Use 0xFFFFFFFF to enable P16V sounds. */
188 snd_emu10k1_ptr20_write(emu, SRCMULTI_ENABLE, 0, 0xFFFFFFFF);
189
190 /* Enabled Phased (8-channel) P16V playback */
191 outl(0x0201, emu->port + HCFG2);
192 /* Set playback routing. */
James Courtier-Duttonfd9a98e2005-04-10 15:43:35 +0200193 snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, 0x78e4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
James Courtier-Duttone0474e52005-07-02 16:33:34 +0200195 if (emu->card_capabilities->ca0108_chip) { /* audigy2 Value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Hacks for Alice3 to work independent of haP16V driver */
197 u32 tmp;
198
199 snd_printk(KERN_ERR "Audigy2 value:Special config.\n");
200 //Setup SRCMulti_I2S SamplingRate
201 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
202 tmp &= 0xfffff1ff;
203 tmp |= (0x2<<9);
204 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
205
206 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
207 outl(0x600000, emu->port + 0x20);
208 outl(0x14, emu->port + 0x24);
209
210 /* Setup SRCMulti Input Audio Enable */
211 outl(0x7b0000, emu->port + 0x20);
212 outl(0xFF000000, emu->port + 0x24);
213
214 /* Setup SPDIF Out Audio Enable */
215 /* The Audigy 2 Value has a separate SPDIF out,
216 * so no need for a mixer switch
217 */
218 outl(0x7a0000, emu->port + 0x20);
219 outl(0xFF000000, emu->port + 0x24);
220 tmp = inl(emu->port + A_IOCFG) & ~0x8; /* Clear bit 3 */
221 outl(tmp, emu->port + A_IOCFG);
222 }
223
224
225 /*
226 * Clear page with silence & setup all pointers to this page
227 */
228 memset(emu->silent_page.area, 0, PAGE_SIZE);
229 silent_page = emu->silent_page.addr << 1;
230 for (idx = 0; idx < MAXPAGES; idx++)
231 ((u32 *)emu->ptb_pages.area)[idx] = cpu_to_le32(silent_page | idx);
232 snd_emu10k1_ptr_write(emu, PTB, 0, emu->ptb_pages.addr);
233 snd_emu10k1_ptr_write(emu, TCB, 0, 0); /* taken from original driver */
234 snd_emu10k1_ptr_write(emu, TCBS, 0, 4); /* taken from original driver */
235
236 silent_page = (emu->silent_page.addr << 1) | MAP_PTI_MASK;
237 for (ch = 0; ch < NUM_G; ch++) {
238 snd_emu10k1_ptr_write(emu, MAPA, ch, silent_page);
239 snd_emu10k1_ptr_write(emu, MAPB, ch, silent_page);
240 }
241
242 /*
243 * Hokay, setup HCFG
244 * Mute Disable Audio = 0
245 * Lock Tank Memory = 1
246 * Lock Sound Memory = 0
247 * Auto Mute = 1
248 */
249 if (emu->audigy) {
250 if (emu->revision == 4) /* audigy2 */
251 outl(HCFG_AUDIOENABLE |
252 HCFG_AC3ENABLE_CDSPDIF |
253 HCFG_AC3ENABLE_GPSPDIF |
254 HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
255 else
256 outl(HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
James Courtier-Duttone0474e52005-07-02 16:33:34 +0200257 /* FIXME: Remove all these emu->model and replace it with a card recognition parameter,
258 * e.g. card_capabilities->joystick */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 } else if (emu->model == 0x20 ||
260 emu->model == 0xc400 ||
261 (emu->model == 0x21 && emu->revision < 6))
262 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE, emu->port + HCFG);
263 else
264 // With on-chip joystick
265 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
266
267 if (enable_ir) { /* enable IR for SB Live */
268 if (emu->audigy) {
269 unsigned int reg = inl(emu->port + A_IOCFG);
270 outl(reg | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
271 udelay(500);
272 outl(reg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
273 udelay(100);
274 outl(reg, emu->port + A_IOCFG);
275 } else {
276 unsigned int reg = inl(emu->port + HCFG);
277 outl(reg | HCFG_GPOUT2, emu->port + HCFG);
278 udelay(500);
279 outl(reg | HCFG_GPOUT1 | HCFG_GPOUT2, emu->port + HCFG);
280 udelay(100);
281 outl(reg, emu->port + HCFG);
282 }
283 }
284
285 if (emu->audigy) { /* enable analog output */
286 unsigned int reg = inl(emu->port + A_IOCFG);
287 outl(reg | A_IOCFG_GPOUT0, emu->port + A_IOCFG);
288 }
289
290 /*
291 * Initialize the effect engine
292 */
293 if ((err = snd_emu10k1_init_efx(emu)) < 0)
294 return err;
295
296 /*
297 * Enable the audio bit
298 */
299 outl(inl(emu->port + HCFG) | HCFG_AUDIOENABLE, emu->port + HCFG);
300
301 /* Enable analog/digital outs on audigy */
302 if (emu->audigy) {
303 outl(inl(emu->port + A_IOCFG) & ~0x44, emu->port + A_IOCFG);
304
James Courtier-Duttone0474e52005-07-02 16:33:34 +0200305 if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* Unmute Analog now. Set GPO6 to 1 for Apollo.
307 * This has to be done after init ALice3 I2SOut beyond 48KHz.
308 * So, sequence is important. */
309 outl(inl(emu->port + A_IOCFG) | 0x0040, emu->port + A_IOCFG);
James Courtier-Duttone0474e52005-07-02 16:33:34 +0200310 } else if (emu->card_capabilities->ca0108_chip) { /* audigy2 value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* Unmute Analog now. */
312 outl(inl(emu->port + A_IOCFG) | 0x0060, emu->port + A_IOCFG);
313 } else {
314 /* Disable routing from AC97 line out to Front speakers */
315 outl(inl(emu->port + A_IOCFG) | 0x0080, emu->port + A_IOCFG);
316 }
317 }
318
319#if 0
320 {
321 unsigned int tmp;
322 /* FIXME: the following routine disables LiveDrive-II !! */
323 // TOSLink detection
324 emu->tos_link = 0;
325 tmp = inl(emu->port + HCFG);
326 if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
327 outl(tmp|0x800, emu->port + HCFG);
328 udelay(50);
329 if (tmp != (inl(emu->port + HCFG) & ~0x800)) {
330 emu->tos_link = 1;
331 outl(tmp, emu->port + HCFG);
332 }
333 }
334 }
335#endif
336
337 snd_emu10k1_intr_enable(emu, INTE_PCIERRORENABLE);
338
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100339 emu->reserved_page = (struct snd_emu10k1_memblk *)snd_emu10k1_synth_alloc(emu, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (emu->reserved_page)
341 emu->reserved_page->map_locked = 1;
342
343 return 0;
344}
345
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100346static int snd_emu10k1_done(struct snd_emu10k1 * emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int ch;
349
350 outl(0, emu->port + INTE);
351
352 /*
353 * Shutdown the chip
354 */
355 for (ch = 0; ch < NUM_G; ch++)
356 snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
357 for (ch = 0; ch < NUM_G; ch++) {
358 snd_emu10k1_ptr_write(emu, VTFT, ch, 0);
359 snd_emu10k1_ptr_write(emu, CVCF, ch, 0);
360 snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
361 snd_emu10k1_ptr_write(emu, CPF, ch, 0);
362 }
363
364 /* reset recording buffers */
365 snd_emu10k1_ptr_write(emu, MICBS, 0, 0);
366 snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
367 snd_emu10k1_ptr_write(emu, FXBS, 0, 0);
368 snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
369 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
370 snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
371 snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
372 snd_emu10k1_ptr_write(emu, TCBS, 0, TCBS_BUFFSIZE_16K);
373 snd_emu10k1_ptr_write(emu, TCB, 0, 0);
374 if (emu->audigy)
375 snd_emu10k1_ptr_write(emu, A_DBG, 0, A_DBG_SINGLE_STEP);
376 else
377 snd_emu10k1_ptr_write(emu, DBG, 0, EMU10K1_DBG_SINGLE_STEP);
378
379 /* disable channel interrupt */
380 snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
381 snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
382 snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
383 snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
384
385 /* remove reserved page */
386 if (emu->reserved_page != NULL) {
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100387 snd_emu10k1_synth_free(emu, (struct snd_util_memblk *)emu->reserved_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 emu->reserved_page = NULL;
389 }
390
391 /* disable audio and lock cache */
392 outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, emu->port + HCFG);
393 snd_emu10k1_ptr_write(emu, PTB, 0, 0);
394
395 snd_emu10k1_free_efx(emu);
396
397 return 0;
398}
399
400/*************************************************************************
401 * ECARD functional implementation
402 *************************************************************************/
403
404/* In A1 Silicon, these bits are in the HC register */
405#define HOOKN_BIT (1L << 12)
406#define HANDN_BIT (1L << 11)
407#define PULSEN_BIT (1L << 10)
408
409#define EC_GDI1 (1 << 13)
410#define EC_GDI0 (1 << 14)
411
412#define EC_NUM_CONTROL_BITS 20
413
414#define EC_AC3_DATA_SELN 0x0001L
415#define EC_EE_DATA_SEL 0x0002L
416#define EC_EE_CNTRL_SELN 0x0004L
417#define EC_EECLK 0x0008L
418#define EC_EECS 0x0010L
419#define EC_EESDO 0x0020L
420#define EC_TRIM_CSN 0x0040L
421#define EC_TRIM_SCLK 0x0080L
422#define EC_TRIM_SDATA 0x0100L
423#define EC_TRIM_MUTEN 0x0200L
424#define EC_ADCCAL 0x0400L
425#define EC_ADCRSTN 0x0800L
426#define EC_DACCAL 0x1000L
427#define EC_DACMUTEN 0x2000L
428#define EC_LEDN 0x4000L
429
430#define EC_SPDIF0_SEL_SHIFT 15
431#define EC_SPDIF1_SEL_SHIFT 17
432#define EC_SPDIF0_SEL_MASK (0x3L << EC_SPDIF0_SEL_SHIFT)
433#define EC_SPDIF1_SEL_MASK (0x7L << EC_SPDIF1_SEL_SHIFT)
434#define EC_SPDIF0_SELECT(_x) (((_x) << EC_SPDIF0_SEL_SHIFT) & EC_SPDIF0_SEL_MASK)
435#define EC_SPDIF1_SELECT(_x) (((_x) << EC_SPDIF1_SEL_SHIFT) & EC_SPDIF1_SEL_MASK)
436#define EC_CURRENT_PROM_VERSION 0x01 /* Self-explanatory. This should
437 * be incremented any time the EEPROM's
438 * format is changed. */
439
440#define EC_EEPROM_SIZE 0x40 /* ECARD EEPROM has 64 16-bit words */
441
442/* Addresses for special values stored in to EEPROM */
443#define EC_PROM_VERSION_ADDR 0x20 /* Address of the current prom version */
444#define EC_BOARDREV0_ADDR 0x21 /* LSW of board rev */
445#define EC_BOARDREV1_ADDR 0x22 /* MSW of board rev */
446
447#define EC_LAST_PROMFILE_ADDR 0x2f
448
449#define EC_SERIALNUM_ADDR 0x30 /* First word of serial number. The
450 * can be up to 30 characters in length
451 * and is stored as a NULL-terminated
452 * ASCII string. Any unused bytes must be
453 * filled with zeros */
454#define EC_CHECKSUM_ADDR 0x3f /* Location at which checksum is stored */
455
456
457/* Most of this stuff is pretty self-evident. According to the hardware
458 * dudes, we need to leave the ADCCAL bit low in order to avoid a DC
459 * offset problem. Weird.
460 */
461#define EC_RAW_RUN_MODE (EC_DACMUTEN | EC_ADCRSTN | EC_TRIM_MUTEN | \
462 EC_TRIM_CSN)
463
464
465#define EC_DEFAULT_ADC_GAIN 0xC4C4
466#define EC_DEFAULT_SPDIF0_SEL 0x0
467#define EC_DEFAULT_SPDIF1_SEL 0x4
468
469/**************************************************************************
470 * @func Clock bits into the Ecard's control latch. The Ecard uses a
471 * control latch will is loaded bit-serially by toggling the Modem control
472 * lines from function 2 on the E8010. This function hides these details
473 * and presents the illusion that we are actually writing to a distinct
474 * register.
475 */
476
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100477static void snd_emu10k1_ecard_write(struct snd_emu10k1 * emu, unsigned int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 unsigned short count;
480 unsigned int data;
481 unsigned long hc_port;
482 unsigned int hc_value;
483
484 hc_port = emu->port + HCFG;
485 hc_value = inl(hc_port) & ~(HOOKN_BIT | HANDN_BIT | PULSEN_BIT);
486 outl(hc_value, hc_port);
487
488 for (count = 0; count < EC_NUM_CONTROL_BITS; count++) {
489
490 /* Set up the value */
491 data = ((value & 0x1) ? PULSEN_BIT : 0);
492 value >>= 1;
493
494 outl(hc_value | data, hc_port);
495
496 /* Clock the shift register */
497 outl(hc_value | data | HANDN_BIT, hc_port);
498 outl(hc_value | data, hc_port);
499 }
500
501 /* Latch the bits */
502 outl(hc_value | HOOKN_BIT, hc_port);
503 outl(hc_value, hc_port);
504}
505
506/**************************************************************************
507 * @func Set the gain of the ECARD's CS3310 Trim/gain controller. The
508 * trim value consists of a 16bit value which is composed of two
509 * 8 bit gain/trim values, one for the left channel and one for the
510 * right channel. The following table maps from the Gain/Attenuation
511 * value in decibels into the corresponding bit pattern for a single
512 * channel.
513 */
514
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100515static void snd_emu10k1_ecard_setadcgain(struct snd_emu10k1 * emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned short gain)
517{
518 unsigned int bit;
519
520 /* Enable writing to the TRIM registers */
521 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
522
523 /* Do it again to insure that we meet hold time requirements */
524 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
525
526 for (bit = (1 << 15); bit; bit >>= 1) {
527 unsigned int value;
528
529 value = emu->ecard_ctrl & ~(EC_TRIM_CSN | EC_TRIM_SDATA);
530
531 if (gain & bit)
532 value |= EC_TRIM_SDATA;
533
534 /* Clock the bit */
535 snd_emu10k1_ecard_write(emu, value);
536 snd_emu10k1_ecard_write(emu, value | EC_TRIM_SCLK);
537 snd_emu10k1_ecard_write(emu, value);
538 }
539
540 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
541}
542
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100543static int __devinit snd_emu10k1_ecard_init(struct snd_emu10k1 * emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 unsigned int hc_value;
546
547 /* Set up the initial settings */
548 emu->ecard_ctrl = EC_RAW_RUN_MODE |
549 EC_SPDIF0_SELECT(EC_DEFAULT_SPDIF0_SEL) |
550 EC_SPDIF1_SELECT(EC_DEFAULT_SPDIF1_SEL);
551
552 /* Step 0: Set the codec type in the hardware control register
553 * and enable audio output */
554 hc_value = inl(emu->port + HCFG);
555 outl(hc_value | HCFG_AUDIOENABLE | HCFG_CODECFORMAT_I2S, emu->port + HCFG);
556 inl(emu->port + HCFG);
557
558 /* Step 1: Turn off the led and deassert TRIM_CS */
559 snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
560
561 /* Step 2: Calibrate the ADC and DAC */
562 snd_emu10k1_ecard_write(emu, EC_DACCAL | EC_LEDN | EC_TRIM_CSN);
563
564 /* Step 3: Wait for awhile; XXX We can't get away with this
565 * under a real operating system; we'll need to block and wait that
566 * way. */
567 snd_emu10k1_wait(emu, 48000);
568
569 /* Step 4: Switch off the DAC and ADC calibration. Note
570 * That ADC_CAL is actually an inverted signal, so we assert
571 * it here to stop calibration. */
572 snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
573
574 /* Step 4: Switch into run mode */
575 snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
576
577 /* Step 5: Set the analog input gain */
578 snd_emu10k1_ecard_setadcgain(emu, EC_DEFAULT_ADC_GAIN);
579
580 return 0;
581}
582
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100583static int __devinit snd_emu10k1_cardbus_init(struct snd_emu10k1 * emu)
James Courtier-Duttond83c6712005-10-31 10:27:41 +0000584{
585 unsigned long special_port;
586 unsigned int value;
587
588 /* Special initialisation routine
589 * before the rest of the IO-Ports become active.
590 */
591 special_port = emu->port + 0x38;
592 value = inl(special_port);
593 outl(0x00d00000, special_port);
594 value = inl(special_port);
595 outl(0x00d00001, special_port);
596 value = inl(special_port);
597 outl(0x00d0005f, special_port);
598 value = inl(special_port);
599 outl(0x00d0007f, special_port);
600 value = inl(special_port);
601 outl(0x0090007f, special_port);
602 value = inl(special_port);
603
James Courtier-Duttone2b15f82005-11-11 23:39:05 +0100604 snd_emu10k1_ptr20_write(emu, TINA2_VOLUME, 0, 0xfefefefe); /* Defaults to 0x30303030 */
James Courtier-Duttond83c6712005-10-31 10:27:41 +0000605 return 0;
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * Create the EMU10K1 instance
610 */
611
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100612static int snd_emu10k1_free(struct snd_emu10k1 *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 if (emu->port) { /* avoid access to already used hardware */
615 snd_emu10k1_fx8010_tram_setup(emu, 0);
616 snd_emu10k1_done(emu);
617 }
618 if (emu->memhdr)
619 snd_util_memhdr_free(emu->memhdr);
620 if (emu->silent_page.area)
621 snd_dma_free_pages(&emu->silent_page);
622 if (emu->ptb_pages.area)
623 snd_dma_free_pages(&emu->ptb_pages);
624 vfree(emu->page_ptr_table);
625 vfree(emu->page_addr_table);
626 if (emu->irq >= 0)
627 free_irq(emu->irq, (void *)emu);
628 if (emu->port)
629 pci_release_regions(emu->pci);
630 pci_disable_device(emu->pci);
Lee Revell2b637da2005-03-30 13:51:18 +0200631 if (emu->card_capabilities->ca0151_chip) /* P16V */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 snd_p16v_free(emu);
633 kfree(emu);
634 return 0;
635}
636
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100637static int snd_emu10k1_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100639 struct snd_emu10k1 *emu = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return snd_emu10k1_free(emu);
641}
642
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100643static struct snd_emu_chip_details emu_chip_details[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* Audigy 2 Value AC3 out does not work yet. Need to find out how to turn off interpolators.*/
James Courtier-Dutton88dc0e52005-07-03 12:54:29 +0200645 /* Tested by James@superbug.co.uk 3rd July 2005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x10011102,
647 .driver = "Audigy2", .name = "Audigy 2 Value [SB0400]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200648 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .emu10k2_chip = 1,
650 .ca0108_chip = 1,
Peter Zubaj26689072005-04-01 11:15:07 +0200651 .spk71 = 1,
652 .ac97_chip = 1} ,
James Courtier-Duttond83c6712005-10-31 10:27:41 +0000653 /* Audigy 2 ZS Notebook Cardbus card.*/
654 /* Tested by James@superbug.co.uk 30th October 2005 */
655 /* Not working yet, but progressing. */
656 {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x20011102,
657 .driver = "Audigy2", .name = "Audigy 2 ZS Notebook [SB0530]",
658 .id = "Audigy2",
659 .emu10k2_chip = 1,
660 .ca0108_chip = 1,
661 .ca_cardbus_chip = 1,
662 .spk71 = 1} ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 {.vendor = 0x1102, .device = 0x0008,
664 .driver = "Audigy2", .name = "Audigy 2 Value [Unknown]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200665 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .emu10k2_chip = 1,
Peter Zubaj26689072005-04-01 11:15:07 +0200667 .ca0108_chip = 1,
668 .ac97_chip = 1} ,
James Courtier-Dutton7c1d5492005-07-10 11:50:36 +0200669 /* Tested by James@superbug.co.uk 8th July 2005. No sound available yet. */
670 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x40011102,
671 .driver = "Audigy2", .name = "E-mu 1212m [4001]",
672 .id = "EMU1212m",
673 .emu10k2_chip = 1,
674 .ca0102_chip = 1,
675 .ecard = 1} ,
James Courtier-Dutton88dc0e52005-07-03 12:54:29 +0200676 /* Tested by James@superbug.co.uk 3rd July 2005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20071102,
678 .driver = "Audigy2", .name = "Audigy 4 PRO [SB0380]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200679 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 .emu10k2_chip = 1,
681 .ca0102_chip = 1,
682 .ca0151_chip = 1,
683 .spk71 = 1,
684 .spdif_bug = 1,
685 .ac97_chip = 1} ,
Lee Revellf6f8bb62005-11-07 14:59:19 +0100686 /* Tested by shane-alsa@cm.nu 5th Nov 2005 */
687 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20061102,
688 .driver = "Audigy2", .name = "Audigy 2 [2006]",
689 .id = "Audigy2",
690 .emu10k2_chip = 1,
691 .ca0102_chip = 1,
692 .ca0151_chip = 1,
693 .spk71 = 1,
694 .spdif_bug = 1,
695 .ac97_chip = 1} ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20021102,
697 .driver = "Audigy2", .name = "Audigy 2 ZS [SB0350]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200698 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 .emu10k2_chip = 1,
700 .ca0102_chip = 1,
701 .ca0151_chip = 1,
702 .spk71 = 1,
703 .spdif_bug = 1,
704 .ac97_chip = 1} ,
705 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20011102,
706 .driver = "Audigy2", .name = "Audigy 2 ZS [2001]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200707 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 .emu10k2_chip = 1,
709 .ca0102_chip = 1,
710 .ca0151_chip = 1,
711 .spk71 = 1,
712 .spdif_bug = 1,
713 .ac97_chip = 1} ,
714 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10071102,
715 .driver = "Audigy2", .name = "Audigy 2 [SB0240]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200716 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 .emu10k2_chip = 1,
718 .ca0102_chip = 1,
719 .ca0151_chip = 1,
720 .spk71 = 1,
721 .spdif_bug = 1,
722 .ac97_chip = 1} ,
723 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10051102,
724 .driver = "Audigy2", .name = "Audigy 2 EX [1005]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200725 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 .emu10k2_chip = 1,
727 .ca0102_chip = 1,
728 .ca0151_chip = 1,
Lee Revell2f020aa2005-11-07 14:54:24 +0100729 .spk71 = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 .spdif_bug = 1} ,
731 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10021102,
732 .driver = "Audigy2", .name = "Audigy 2 Platinum [SB0240P]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200733 .id = "Audigy2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 .emu10k2_chip = 1,
735 .ca0102_chip = 1,
736 .ca0151_chip = 1,
737 .spk71 = 1,
738 .spdif_bug = 1,
739 .ac97_chip = 1} ,
Takashi Iwaibdaed502005-04-07 15:48:42 +0200740 {.vendor = 0x1102, .device = 0x0004, .revision = 0x04,
741 .driver = "Audigy2", .name = "Audigy 2 [Unknown]",
742 .id = "Audigy2",
743 .emu10k2_chip = 1,
744 .ca0102_chip = 1,
745 .ca0151_chip = 1,
746 .spdif_bug = 1,
747 .ac97_chip = 1} ,
Peter Zubaj26689072005-04-01 11:15:07 +0200748 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00531102,
749 .driver = "Audigy", .name = "Audigy 1 [SB0090]",
750 .id = "Audigy",
751 .emu10k2_chip = 1,
752 .ca0102_chip = 1,
753 .ac97_chip = 1} ,
James Courtier-Duttonae3a72d2005-07-06 22:36:18 +0200754 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00521102,
755 .driver = "Audigy", .name = "Audigy 1 ES [SB0160]",
756 .id = "Audigy",
757 .emu10k2_chip = 1,
758 .ca0102_chip = 1,
759 .spdif_bug = 1,
760 .ac97_chip = 1} ,
Arnaud Patarda6c17ec2005-05-27 12:31:34 +0200761 {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00511102,
762 .driver = "Audigy", .name = "Audigy 1 [SB0090]",
763 .id = "Audigy",
764 .emu10k2_chip = 1,
765 .ca0102_chip = 1,
766 .ac97_chip = 1} ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 {.vendor = 0x1102, .device = 0x0004,
Takashi Iwaibdaed502005-04-07 15:48:42 +0200768 .driver = "Audigy", .name = "Audigy 1 [Unknown]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200769 .id = "Audigy",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 .emu10k2_chip = 1,
771 .ca0102_chip = 1,
Peter Zubaj26689072005-04-01 11:15:07 +0200772 .ac97_chip = 1} ,
James Courtier-Duttona6f61922005-07-03 12:32:40 +0200773 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806B1102,
774 .driver = "EMU10K1", .name = "SBLive! [SB0105]",
James Courtier-Dutton2b6b22f2005-06-18 13:50:22 +0200775 .id = "Live",
776 .emu10k1_chip = 1,
777 .ac97_chip = 1,
778 .sblive51 = 1} ,
779 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806A1102,
780 .driver = "EMU10K1", .name = "SBLive! Value [SB0103]",
781 .id = "Live",
782 .emu10k1_chip = 1,
783 .ac97_chip = 1,
784 .sblive51 = 1} ,
James Courtier-Duttona6f61922005-07-03 12:32:40 +0200785 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80691102,
786 .driver = "EMU10K1", .name = "SBLive! Value [SB0101]",
787 .id = "Live",
788 .emu10k1_chip = 1,
789 .ac97_chip = 1,
790 .sblive51 = 1} ,
Lee Revellc6c0b842005-08-29 17:42:00 +0200791 /* Tested by Thomas Zehetbauer 27th Aug 2005 */
792 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80651102,
793 .driver = "EMU10K1", .name = "SB Live 5.1 [SB0220]",
794 .id = "Live",
795 .emu10k1_chip = 1,
796 .ac97_chip = 1,
797 .sblive51 = 1} ,
James Courtier-Duttona6f61922005-07-03 12:32:40 +0200798 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80641102,
799 .driver = "EMU10K1", .name = "SB Live 5.1",
800 .id = "Live",
801 .emu10k1_chip = 1,
802 .ac97_chip = 1,
803 .sblive51 = 1} ,
James Courtier-Duttonafe0f1f2005-09-10 10:24:10 +0200804 /* Tested by alsa bugtrack user "hus" bug #1297 12th Aug 2005 */
James Courtier-Duttona6f61922005-07-03 12:32:40 +0200805 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80611102,
Takashi Iwaif12aa402005-09-30 16:56:59 +0200806 .driver = "EMU10K1", .name = "SBLive 5.1 [SB0060]",
James Courtier-Duttona6f61922005-07-03 12:32:40 +0200807 .id = "Live",
808 .emu10k1_chip = 1,
Takashi Iwaif12aa402005-09-30 16:56:59 +0200809 .ac97_chip = 2, /* ac97 is optional; both SBLive 5.1 and platinum
810 * share the same IDs!
811 */
James Courtier-Duttona6f61922005-07-03 12:32:40 +0200812 .sblive51 = 1} ,
813 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80511102,
814 .driver = "EMU10K1", .name = "SBLive! Value [CT4850]",
815 .id = "Live",
816 .emu10k1_chip = 1,
817 .ac97_chip = 1,
818 .sblive51 = 1} ,
819 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80401102,
820 .driver = "EMU10K1", .name = "SBLive! Platinum [CT4760P]",
821 .id = "Live",
822 .emu10k1_chip = 1,
823 .ac97_chip = 1} ,
824 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80321102,
825 .driver = "EMU10K1", .name = "SBLive! Value [CT4871]",
826 .id = "Live",
827 .emu10k1_chip = 1,
828 .ac97_chip = 1,
829 .sblive51 = 1} ,
830 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80311102,
831 .driver = "EMU10K1", .name = "SBLive! Value [CT4831]",
832 .id = "Live",
833 .emu10k1_chip = 1,
834 .ac97_chip = 1,
835 .sblive51 = 1} ,
836 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80281102,
837 .driver = "EMU10K1", .name = "SBLive! Value [CT4870]",
838 .id = "Live",
839 .emu10k1_chip = 1,
840 .ac97_chip = 1,
841 .sblive51 = 1} ,
James Courtier-Dutton88dc0e52005-07-03 12:54:29 +0200842 /* Tested by James@superbug.co.uk 3rd July 2005 */
James Courtier-Duttona6f61922005-07-03 12:32:40 +0200843 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80271102,
844 .driver = "EMU10K1", .name = "SBLive! Value [CT4832]",
845 .id = "Live",
846 .emu10k1_chip = 1,
847 .ac97_chip = 1,
848 .sblive51 = 1} ,
849 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80261102,
850 .driver = "EMU10K1", .name = "SBLive! Value [CT4830]",
851 .id = "Live",
852 .emu10k1_chip = 1,
853 .ac97_chip = 1,
854 .sblive51 = 1} ,
855 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80231102,
856 .driver = "EMU10K1", .name = "SB PCI512 [CT4790]",
857 .id = "Live",
858 .emu10k1_chip = 1,
859 .ac97_chip = 1,
860 .sblive51 = 1} ,
861 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80221102,
862 .driver = "EMU10K1", .name = "SBLive! Value [CT4780]",
863 .id = "Live",
864 .emu10k1_chip = 1,
865 .ac97_chip = 1,
866 .sblive51 = 1} ,
867 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x40011102,
868 .driver = "EMU10K1", .name = "E-mu APS [4001]",
869 .id = "APS",
870 .emu10k1_chip = 1,
871 .ecard = 1} ,
872 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00211102,
873 .driver = "EMU10K1", .name = "SBLive! [CT4620]",
874 .id = "Live",
875 .emu10k1_chip = 1,
876 .ac97_chip = 1,
877 .sblive51 = 1} ,
878 {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00201102,
879 .driver = "EMU10K1", .name = "SBLive! Value [CT4670]",
James Courtier-Dutton2b6b22f2005-06-18 13:50:22 +0200880 .id = "Live",
881 .emu10k1_chip = 1,
882 .ac97_chip = 1,
883 .sblive51 = 1} ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 {.vendor = 0x1102, .device = 0x0002,
885 .driver = "EMU10K1", .name = "SB Live [Unknown]",
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200886 .id = "Live",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 .emu10k1_chip = 1,
Lee Revell2b637da2005-03-30 13:51:18 +0200888 .ac97_chip = 1,
889 .sblive51 = 1} ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 { } /* terminator */
891};
892
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100893int __devinit snd_emu10k1_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct pci_dev * pci,
895 unsigned short extin_mask,
896 unsigned short extout_mask,
897 long max_cache_bytes,
898 int enable_ir,
James Courtier-Duttone66bc8b2005-07-06 22:21:51 +0200899 uint subsystem,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100900 struct snd_emu10k1 ** remu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100902 struct snd_emu10k1 *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 int err;
904 int is_audigy;
905 unsigned char revision;
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100906 const struct snd_emu_chip_details *c;
907 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 .dev_free = snd_emu10k1_dev_free,
909 };
910
911 *remu = NULL;
912
913 /* enable PCI device */
914 if ((err = pci_enable_device(pci)) < 0)
915 return err;
916
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200917 emu = kzalloc(sizeof(*emu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (emu == NULL) {
919 pci_disable_device(pci);
920 return -ENOMEM;
921 }
922 emu->card = card;
923 spin_lock_init(&emu->reg_lock);
924 spin_lock_init(&emu->emu_lock);
925 spin_lock_init(&emu->voice_lock);
926 spin_lock_init(&emu->synth_lock);
927 spin_lock_init(&emu->memblk_lock);
928 init_MUTEX(&emu->ptb_lock);
929 init_MUTEX(&emu->fx8010.lock);
930 INIT_LIST_HEAD(&emu->mapped_link_head);
931 INIT_LIST_HEAD(&emu->mapped_order_link_head);
932 emu->pci = pci;
933 emu->irq = -1;
934 emu->synth = NULL;
935 emu->get_synth_voice = NULL;
936 /* read revision & serial */
937 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
938 emu->revision = revision;
939 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &emu->serial);
940 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &emu->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 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);
942
943 for (c = emu_chip_details; c->vendor; c++) {
944 if (c->vendor == pci->vendor && c->device == pci->device) {
James Courtier-Duttone66bc8b2005-07-06 22:21:51 +0200945 if (subsystem) {
946 if (c->subsystem && (c->subsystem == subsystem) ) {
947 break;
948 } else continue;
949 } else {
950 if (c->subsystem && (c->subsystem != emu->serial) )
951 continue;
952 if (c->revision && c->revision != emu->revision)
953 continue;
954 }
Takashi Iwaibdaed502005-04-07 15:48:42 +0200955 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 }
958 if (c->vendor == 0) {
959 snd_printk(KERN_ERR "emu10k1: Card not recognised\n");
960 kfree(emu);
961 pci_disable_device(pci);
962 return -ENOENT;
963 }
964 emu->card_capabilities = c;
James Courtier-Duttone66bc8b2005-07-06 22:21:51 +0200965 if (c->subsystem && !subsystem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 snd_printdd("Sound card name=%s\n", c->name);
James Courtier-Duttone66bc8b2005-07-06 22:21:51 +0200967 else if (subsystem)
968 snd_printdd("Sound card name=%s, vendor=0x%x, device=0x%x, subsystem=0x%x. Forced to subsytem=0x%x\n",
969 c->name, pci->vendor, pci->device, emu->serial, c->subsystem);
970 else
971 snd_printdd("Sound card name=%s, vendor=0x%x, device=0x%x, subsystem=0x%x.\n",
972 c->name, pci->vendor, pci->device, emu->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Takashi Iwai85a655d2005-03-30 14:40:25 +0200974 if (!*card->id && c->id) {
975 int i, n = 0;
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200976 strlcpy(card->id, c->id, sizeof(card->id));
Takashi Iwai85a655d2005-03-30 14:40:25 +0200977 for (;;) {
978 for (i = 0; i < snd_ecards_limit; i++) {
979 if (snd_cards[i] && !strcmp(snd_cards[i]->id, card->id))
980 break;
981 }
982 if (i >= snd_ecards_limit)
983 break;
984 n++;
985 if (n >= SNDRV_CARDS)
986 break;
987 snprintf(card->id, sizeof(card->id), "%s_%d", c->id, n);
988 }
989 }
Takashi Iwaiaec72e02005-03-30 14:22:25 +0200990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 is_audigy = emu->audigy = c->emu10k2_chip;
992
993 /* set the DMA transfer mask */
994 emu->dma_mask = is_audigy ? AUDIGY_DMA_MASK : EMU10K1_DMA_MASK;
995 if (pci_set_dma_mask(pci, emu->dma_mask) < 0 ||
996 pci_set_consistent_dma_mask(pci, emu->dma_mask) < 0) {
997 snd_printk(KERN_ERR "architecture does not support PCI busmaster DMA with mask 0x%lx\n", emu->dma_mask);
998 kfree(emu);
999 pci_disable_device(pci);
1000 return -ENXIO;
1001 }
1002 if (is_audigy)
1003 emu->gpr_base = A_FXGPREGBASE;
1004 else
1005 emu->gpr_base = FXGPREGBASE;
1006
1007 if ((err = pci_request_regions(pci, "EMU10K1")) < 0) {
1008 kfree(emu);
1009 pci_disable_device(pci);
1010 return err;
1011 }
1012 emu->port = pci_resource_start(pci, 0);
1013
1014 if (request_irq(pci->irq, snd_emu10k1_interrupt, SA_INTERRUPT|SA_SHIRQ, "EMU10K1", (void *)emu)) {
1015 snd_emu10k1_free(emu);
1016 return -EBUSY;
1017 }
1018 emu->irq = pci->irq;
1019
1020 emu->max_cache_pages = max_cache_bytes >> PAGE_SHIFT;
1021 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1022 32 * 1024, &emu->ptb_pages) < 0) {
1023 snd_emu10k1_free(emu);
1024 return -ENOMEM;
1025 }
1026
1027 emu->page_ptr_table = (void **)vmalloc(emu->max_cache_pages * sizeof(void*));
1028 emu->page_addr_table = (unsigned long*)vmalloc(emu->max_cache_pages * sizeof(unsigned long));
1029 if (emu->page_ptr_table == NULL || emu->page_addr_table == NULL) {
1030 snd_emu10k1_free(emu);
1031 return -ENOMEM;
1032 }
1033
1034 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1035 EMUPAGESIZE, &emu->silent_page) < 0) {
1036 snd_emu10k1_free(emu);
1037 return -ENOMEM;
1038 }
1039 emu->memhdr = snd_util_memhdr_new(emu->max_cache_pages * PAGE_SIZE);
1040 if (emu->memhdr == NULL) {
1041 snd_emu10k1_free(emu);
1042 return -ENOMEM;
1043 }
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001044 emu->memhdr->block_extra_size = sizeof(struct snd_emu10k1_memblk) -
1045 sizeof(struct snd_util_memblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 pci_set_master(pci);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 emu->fx8010.fxbus_mask = 0x303f;
1050 if (extin_mask == 0)
1051 extin_mask = 0x3fcf;
1052 if (extout_mask == 0)
1053 extout_mask = 0x7fff;
1054 emu->fx8010.extin_mask = extin_mask;
1055 emu->fx8010.extout_mask = extout_mask;
1056
Lee Revell2b637da2005-03-30 13:51:18 +02001057 if (emu->card_capabilities->ecard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if ((err = snd_emu10k1_ecard_init(emu)) < 0) {
1059 snd_emu10k1_free(emu);
1060 return err;
1061 }
James Courtier-Duttond83c6712005-10-31 10:27:41 +00001062 } else if (emu->card_capabilities->ca_cardbus_chip) {
1063 if ((err = snd_emu10k1_cardbus_init(emu)) < 0) {
1064 snd_emu10k1_free(emu);
1065 return err;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 } else {
1068 /* 5.1: Enable the additional AC97 Slots. If the emu10k1 version
1069 does not support this, it shouldn't do any harm */
1070 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
1071 }
1072
1073 if ((err = snd_emu10k1_init(emu, enable_ir)) < 0) {
1074 snd_emu10k1_free(emu);
1075 return err;
1076 }
1077
1078 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, emu, &ops)) < 0) {
1079 snd_emu10k1_free(emu);
1080 return err;
1081 }
1082
1083 snd_emu10k1_proc_init(emu);
1084
1085 snd_card_set_dev(card, &pci->dev);
1086 *remu = emu;
1087 return 0;
1088}
1089
1090/* memory.c */
1091EXPORT_SYMBOL(snd_emu10k1_synth_alloc);
1092EXPORT_SYMBOL(snd_emu10k1_synth_free);
1093EXPORT_SYMBOL(snd_emu10k1_synth_bzero);
1094EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user);
1095EXPORT_SYMBOL(snd_emu10k1_memblk_map);
1096/* voice.c */
1097EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
1098EXPORT_SYMBOL(snd_emu10k1_voice_free);
1099/* io.c */
1100EXPORT_SYMBOL(snd_emu10k1_ptr_read);
1101EXPORT_SYMBOL(snd_emu10k1_ptr_write);