2 * Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
3 * Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
6 * FEATURES currently supported:
7 * See ca0106_main.c for features.
10 * Support interrupts per period.
11 * Removed noise from Center/LFE channel when in Analog mode.
12 * Rename and remove mixer controls.
14 * Use separate card based DMA buffer for periods table list.
16 * Change remove and rename ctrls into lists.
18 * Try to fix capture sources.
21 * Enable S32_LE format support.
23 * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
25 * Add Model name recognition.
27 * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
28 * Remove redundent "voice" handling.
30 * Single trigger call for multi channels.
32 * Set limits based on what the sound card hardware can do.
33 * playback periods_min=2, periods_max=8
34 * capture hw constraints require period_size = n * 64 bytes.
35 * playback hw constraints require period_size = n * 64 bytes.
37 * Separate ca0106.c into separate functional .c files.
39 * Modified Copyright message.
41 * Add iec958 file in proc file system to show status of SPDIF in.
43 * Implement support for Line-in capture on SB Live 24bit.
45 * This code was initally based on code from ALSA's emu10k1x.c which is:
46 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
48 * This program is free software; you can redistribute it and/or modify
49 * it under the terms of the GNU General Public License as published by
50 * the Free Software Foundation; either version 2 of the License, or
51 * (at your option) any later version.
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * GNU General Public License for more details.
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
63 #include <sound/driver.h>
64 #include <linux/delay.h>
65 #include <linux/init.h>
66 #include <linux/interrupt.h>
67 #include <linux/pci.h>
68 #include <linux/slab.h>
69 #include <linux/moduleparam.h>
70 #include <sound/core.h>
71 #include <sound/initval.h>
72 #include <sound/pcm.h>
73 #include <sound/ac97_codec.h>
74 #include <sound/info.h>
75 #include <sound/asoundef.h>
80 struct snd_ca0106_category_str {
85 static struct snd_ca0106_category_str snd_ca0106_con_category[] = {
86 { IEC958_AES1_CON_DAT, "DAT" },
87 { IEC958_AES1_CON_VCR, "VCR" },
88 { IEC958_AES1_CON_MICROPHONE, "microphone" },
89 { IEC958_AES1_CON_SYNTHESIZER, "synthesizer" },
90 { IEC958_AES1_CON_RATE_CONVERTER, "rate converter" },
91 { IEC958_AES1_CON_MIXER, "mixer" },
92 { IEC958_AES1_CON_SAMPLER, "sampler" },
93 { IEC958_AES1_CON_PCM_CODER, "PCM coder" },
94 { IEC958_AES1_CON_IEC908_CD, "CD" },
95 { IEC958_AES1_CON_NON_IEC908_CD, "non-IEC908 CD" },
96 { IEC958_AES1_CON_GENERAL, "general" },
100 static void snd_ca0106_proc_dump_iec958( snd_info_buffer_t *buffer, u32 value)
104 status[0] = value & 0xff;
105 status[1] = (value >> 8) & 0xff;
106 status[2] = (value >> 16) & 0xff;
107 status[3] = (value >> 24) & 0xff;
109 if (! (status[0] & IEC958_AES0_PROFESSIONAL)) {
111 snd_iprintf(buffer, "Mode: consumer\n");
112 snd_iprintf(buffer, "Data: ");
113 if (!(status[0] & IEC958_AES0_NONAUDIO)) {
114 snd_iprintf(buffer, "audio\n");
116 snd_iprintf(buffer, "non-audio\n");
118 snd_iprintf(buffer, "Rate: ");
119 switch (status[3] & IEC958_AES3_CON_FS) {
120 case IEC958_AES3_CON_FS_44100:
121 snd_iprintf(buffer, "44100 Hz\n");
123 case IEC958_AES3_CON_FS_48000:
124 snd_iprintf(buffer, "48000 Hz\n");
126 case IEC958_AES3_CON_FS_32000:
127 snd_iprintf(buffer, "32000 Hz\n");
130 snd_iprintf(buffer, "unknown\n");
133 snd_iprintf(buffer, "Copyright: ");
134 if (status[0] & IEC958_AES0_CON_NOT_COPYRIGHT) {
135 snd_iprintf(buffer, "permitted\n");
137 snd_iprintf(buffer, "protected\n");
139 snd_iprintf(buffer, "Emphasis: ");
140 if ((status[0] & IEC958_AES0_CON_EMPHASIS) != IEC958_AES0_CON_EMPHASIS_5015) {
141 snd_iprintf(buffer, "none\n");
143 snd_iprintf(buffer, "50/15us\n");
145 snd_iprintf(buffer, "Category: ");
146 for (i = 0; i < ARRAY_SIZE(snd_ca0106_con_category); i++) {
147 if ((status[1] & IEC958_AES1_CON_CATEGORY) == snd_ca0106_con_category[i].val) {
148 snd_iprintf(buffer, "%s\n", snd_ca0106_con_category[i].name);
152 if (i >= ARRAY_SIZE(snd_ca0106_con_category)) {
153 snd_iprintf(buffer, "unknown 0x%x\n", status[1] & IEC958_AES1_CON_CATEGORY);
155 snd_iprintf(buffer, "Original: ");
156 if (status[1] & IEC958_AES1_CON_ORIGINAL) {
157 snd_iprintf(buffer, "original\n");
159 snd_iprintf(buffer, "1st generation\n");
161 snd_iprintf(buffer, "Clock: ");
162 switch (status[3] & IEC958_AES3_CON_CLOCK) {
163 case IEC958_AES3_CON_CLOCK_1000PPM:
164 snd_iprintf(buffer, "1000 ppm\n");
166 case IEC958_AES3_CON_CLOCK_50PPM:
167 snd_iprintf(buffer, "50 ppm\n");
169 case IEC958_AES3_CON_CLOCK_VARIABLE:
170 snd_iprintf(buffer, "variable pitch\n");
173 snd_iprintf(buffer, "unknown\n");
177 snd_iprintf(buffer, "Mode: professional\n");
178 snd_iprintf(buffer, "Data: ");
179 if (!(status[0] & IEC958_AES0_NONAUDIO)) {
180 snd_iprintf(buffer, "audio\n");
182 snd_iprintf(buffer, "non-audio\n");
184 snd_iprintf(buffer, "Rate: ");
185 switch (status[0] & IEC958_AES0_PRO_FS) {
186 case IEC958_AES0_PRO_FS_44100:
187 snd_iprintf(buffer, "44100 Hz\n");
189 case IEC958_AES0_PRO_FS_48000:
190 snd_iprintf(buffer, "48000 Hz\n");
192 case IEC958_AES0_PRO_FS_32000:
193 snd_iprintf(buffer, "32000 Hz\n");
196 snd_iprintf(buffer, "unknown\n");
199 snd_iprintf(buffer, "Rate Locked: ");
200 if (status[0] & IEC958_AES0_PRO_FREQ_UNLOCKED)
201 snd_iprintf(buffer, "no\n");
203 snd_iprintf(buffer, "yes\n");
204 snd_iprintf(buffer, "Emphasis: ");
205 switch (status[0] & IEC958_AES0_PRO_EMPHASIS) {
206 case IEC958_AES0_PRO_EMPHASIS_CCITT:
207 snd_iprintf(buffer, "CCITT J.17\n");
209 case IEC958_AES0_PRO_EMPHASIS_NONE:
210 snd_iprintf(buffer, "none\n");
212 case IEC958_AES0_PRO_EMPHASIS_5015:
213 snd_iprintf(buffer, "50/15us\n");
215 case IEC958_AES0_PRO_EMPHASIS_NOTID:
217 snd_iprintf(buffer, "unknown\n");
220 snd_iprintf(buffer, "Stereophonic: ");
221 if ((status[1] & IEC958_AES1_PRO_MODE) == IEC958_AES1_PRO_MODE_STEREOPHONIC) {
222 snd_iprintf(buffer, "stereo\n");
224 snd_iprintf(buffer, "not indicated\n");
226 snd_iprintf(buffer, "Userbits: ");
227 switch (status[1] & IEC958_AES1_PRO_USERBITS) {
228 case IEC958_AES1_PRO_USERBITS_192:
229 snd_iprintf(buffer, "192bit\n");
231 case IEC958_AES1_PRO_USERBITS_UDEF:
232 snd_iprintf(buffer, "user-defined\n");
235 snd_iprintf(buffer, "unkown\n");
238 snd_iprintf(buffer, "Sample Bits: ");
239 switch (status[2] & IEC958_AES2_PRO_SBITS) {
240 case IEC958_AES2_PRO_SBITS_20:
241 snd_iprintf(buffer, "20 bit\n");
243 case IEC958_AES2_PRO_SBITS_24:
244 snd_iprintf(buffer, "24 bit\n");
246 case IEC958_AES2_PRO_SBITS_UDEF:
247 snd_iprintf(buffer, "user defined\n");
250 snd_iprintf(buffer, "unknown\n");
253 snd_iprintf(buffer, "Word Length: ");
254 switch (status[2] & IEC958_AES2_PRO_WORDLEN) {
255 case IEC958_AES2_PRO_WORDLEN_22_18:
256 snd_iprintf(buffer, "22 bit or 18 bit\n");
258 case IEC958_AES2_PRO_WORDLEN_23_19:
259 snd_iprintf(buffer, "23 bit or 19 bit\n");
261 case IEC958_AES2_PRO_WORDLEN_24_20:
262 snd_iprintf(buffer, "24 bit or 20 bit\n");
264 case IEC958_AES2_PRO_WORDLEN_20_16:
265 snd_iprintf(buffer, "20 bit or 16 bit\n");
268 snd_iprintf(buffer, "unknown\n");
274 static void snd_ca0106_proc_iec958(snd_info_entry_t *entry,
275 snd_info_buffer_t * buffer)
277 ca0106_t *emu = entry->private_data;
280 value = snd_ca0106_ptr_read(emu, SAMPLE_RATE_TRACKER_STATUS, 0);
281 snd_iprintf(buffer, "Status: %s, %s, %s\n",
282 (value & 0x100000) ? "Rate Locked" : "Not Rate Locked",
283 (value & 0x200000) ? "SPDIF Locked" : "No SPDIF Lock",
284 (value & 0x400000) ? "Audio Valid" : "No valid audio" );
285 snd_iprintf(buffer, "Estimated sample rate: %u\n",
286 ((value & 0xfffff) * 48000) / 0x8000 );
287 if (value & 0x200000) {
288 snd_iprintf(buffer, "IEC958/SPDIF input status:\n");
289 value = snd_ca0106_ptr_read(emu, SPDIF_INPUT_STATUS, 0);
290 snd_ca0106_proc_dump_iec958(buffer, value);
293 snd_iprintf(buffer, "\n");
296 static void snd_ca0106_proc_reg_write32(snd_info_entry_t *entry,
297 snd_info_buffer_t * buffer)
299 ca0106_t *emu = entry->private_data;
303 while (!snd_info_get_line(buffer, line, sizeof(line))) {
304 if (sscanf(line, "%x %x", ®, &val) != 2)
306 if ((reg < 0x40) && (reg >=0) && (val <= 0xffffffff) ) {
307 spin_lock_irqsave(&emu->emu_lock, flags);
308 outl(val, emu->port + (reg & 0xfffffffc));
309 spin_unlock_irqrestore(&emu->emu_lock, flags);
314 static void snd_ca0106_proc_reg_read32(snd_info_entry_t *entry,
315 snd_info_buffer_t * buffer)
317 ca0106_t *emu = entry->private_data;
321 snd_iprintf(buffer, "Registers:\n\n");
322 for(i = 0; i < 0x20; i+=4) {
323 spin_lock_irqsave(&emu->emu_lock, flags);
324 value = inl(emu->port + i);
325 spin_unlock_irqrestore(&emu->emu_lock, flags);
326 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
330 static void snd_ca0106_proc_reg_read16(snd_info_entry_t *entry,
331 snd_info_buffer_t * buffer)
333 ca0106_t *emu = entry->private_data;
337 snd_iprintf(buffer, "Registers:\n\n");
338 for(i = 0; i < 0x20; i+=2) {
339 spin_lock_irqsave(&emu->emu_lock, flags);
340 value = inw(emu->port + i);
341 spin_unlock_irqrestore(&emu->emu_lock, flags);
342 snd_iprintf(buffer, "Register %02X: %04X\n", i, value);
346 static void snd_ca0106_proc_reg_read8(snd_info_entry_t *entry,
347 snd_info_buffer_t * buffer)
349 ca0106_t *emu = entry->private_data;
353 snd_iprintf(buffer, "Registers:\n\n");
354 for(i = 0; i < 0x20; i+=1) {
355 spin_lock_irqsave(&emu->emu_lock, flags);
356 value = inb(emu->port + i);
357 spin_unlock_irqrestore(&emu->emu_lock, flags);
358 snd_iprintf(buffer, "Register %02X: %02X\n", i, value);
362 static void snd_ca0106_proc_reg_read1(snd_info_entry_t *entry,
363 snd_info_buffer_t * buffer)
365 ca0106_t *emu = entry->private_data;
369 snd_iprintf(buffer, "Registers\n");
370 for(i = 0; i < 0x40; i++) {
371 snd_iprintf(buffer, "%02X: ",i);
372 for (j = 0; j < 4; j++) {
373 value = snd_ca0106_ptr_read(emu, i, j);
374 snd_iprintf(buffer, "%08lX ", value);
376 snd_iprintf(buffer, "\n");
380 static void snd_ca0106_proc_reg_read2(snd_info_entry_t *entry,
381 snd_info_buffer_t * buffer)
383 ca0106_t *emu = entry->private_data;
387 snd_iprintf(buffer, "Registers\n");
388 for(i = 0x40; i < 0x80; i++) {
389 snd_iprintf(buffer, "%02X: ",i);
390 for (j = 0; j < 4; j++) {
391 value = snd_ca0106_ptr_read(emu, i, j);
392 snd_iprintf(buffer, "%08lX ", value);
394 snd_iprintf(buffer, "\n");
398 static void snd_ca0106_proc_reg_write(snd_info_entry_t *entry,
399 snd_info_buffer_t * buffer)
401 ca0106_t *emu = entry->private_data;
403 unsigned int reg, channel_id , val;
404 while (!snd_info_get_line(buffer, line, sizeof(line))) {
405 if (sscanf(line, "%x %x %x", ®, &channel_id, &val) != 3)
407 if ((reg < 0x80) && (reg >=0) && (val <= 0xffffffff) && (channel_id >=0) && (channel_id <= 3) )
408 snd_ca0106_ptr_write(emu, reg, channel_id, val);
412 static void snd_ca0106_proc_i2c_write(snd_info_entry_t *entry,
413 snd_info_buffer_t * buffer)
415 ca0106_t *emu = entry->private_data;
417 unsigned int reg, val;
418 while (!snd_info_get_line(buffer, line, sizeof(line))) {
419 if (sscanf(line, "%x %x", ®, &val) != 2)
421 if ((reg <= 0x7f) || (val <= 0x1ff)) {
422 snd_ca0106_i2c_write(emu, reg, val);
427 int __devinit snd_ca0106_proc_init(ca0106_t * emu)
429 snd_info_entry_t *entry;
431 if(! snd_card_proc_new(emu->card, "iec958", &entry))
432 snd_info_set_text_ops(entry, emu, 1024, snd_ca0106_proc_iec958);
433 if(! snd_card_proc_new(emu->card, "ca0106_reg32", &entry)) {
434 snd_info_set_text_ops(entry, emu, 1024, snd_ca0106_proc_reg_read32);
435 entry->c.text.write_size = 64;
436 entry->c.text.write = snd_ca0106_proc_reg_write32;
437 entry->mode |= S_IWUSR;
439 if(! snd_card_proc_new(emu->card, "ca0106_reg16", &entry))
440 snd_info_set_text_ops(entry, emu, 1024, snd_ca0106_proc_reg_read16);
441 if(! snd_card_proc_new(emu->card, "ca0106_reg8", &entry))
442 snd_info_set_text_ops(entry, emu, 1024, snd_ca0106_proc_reg_read8);
443 if(! snd_card_proc_new(emu->card, "ca0106_regs1", &entry)) {
444 snd_info_set_text_ops(entry, emu, 1024, snd_ca0106_proc_reg_read1);
445 entry->c.text.write_size = 64;
446 entry->c.text.write = snd_ca0106_proc_reg_write;
447 entry->mode |= S_IWUSR;
448 // entry->private_data = emu;
450 if(! snd_card_proc_new(emu->card, "ca0106_i2c", &entry)) {
451 snd_info_set_text_ops(entry, emu, 1024, snd_ca0106_proc_i2c_write);
452 entry->c.text.write_size = 64;
453 entry->c.text.write = snd_ca0106_proc_i2c_write;
454 entry->mode |= S_IWUSR;
455 // entry->private_data = emu;
457 if(! snd_card_proc_new(emu->card, "ca0106_regs2", &entry))
458 snd_info_set_text_ops(entry, emu, 1024, snd_ca0106_proc_reg_read2);