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 * Front, Rear and Center/LFE.
8 * Surround40 and Surround51.
9 * Capture from MIC an LINE IN input.
10 * SPDIF digital playback of PCM stereo and AC3/DTS works.
11 * (One can use a standard mono mini-jack to one RCA plugs cable.
12 * or one can use a standard stereo mini-jack to two RCA plugs cable.
13 * Plug one of the RCA plugs into the Coax input of the external decoder/receiver.)
14 * ( In theory one could output 3 different AC3 streams at once, to 3 different SPDIF outputs. )
15 * Notes on how to capture sound:
16 * The AC97 is used in the PLAYBACK direction.
17 * The output from the AC97 chip, instead of reaching the speakers, is fed into the Philips 1361T ADC.
18 * So, to record from the MIC, set the MIC Playback volume to max,
19 * unmute the MIC and turn up the MASTER Playback volume.
20 * So, to prevent feedback when capturing, minimise the "Capture feedback into Playback" volume.
22 * The only playback controls that currently do anything are: -
30 * For capture from Mic in or Line in.
31 * Digital/Analog ( switch must be in Analog mode for CAPTURE. )
33 * CAPTURE feedback into PLAYBACK
36 * Support interrupts per period.
37 * Removed noise from Center/LFE channel when in Analog mode.
38 * Rename and remove mixer controls.
40 * Use separate card based DMA buffer for periods table list.
42 * Change remove and rename ctrls into lists.
44 * Try to fix capture sources.
47 * Enable S32_LE format support.
49 * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
51 * Add Model name recognition.
53 * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
54 * Remove redundent "voice" handling.
56 * Single trigger call for multi channels.
58 * Set limits based on what the sound card hardware can do.
59 * playback periods_min=2, periods_max=8
60 * capture hw constraints require period_size = n * 64 bytes.
61 * playback hw constraints require period_size = n * 64 bytes.
65 * Implement 192000 sample rate.
67 * Add support for SB0410 and SB0413.
69 * Modified Copyright message.
71 * Finally fix support for SB Live 24 bit. SB0410 and SB0413.
72 * The output codec needs resetting, otherwise all output is muted.
74 * Merge "pci_disable_device(pci);" fixes.
76 * Add 4 capture channels. (SPDIF only comes in on channel 0. )
77 * Add SPDIF capture using optional digital I/O module for SB Live 24bit. (Analog capture does not yet work.)
79 * Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
81 * Implement support for Line-in capture on SB Live 24bit.
83 * Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
86 * Some stability problems when unloading the snd-ca0106 kernel module.
90 * 4 Capture channels, only one implemented so far.
91 * Other capture rates apart from 48khz not implemented.
96 * P17 Chip: CA0106-DAT
97 * AC97 Codec: STAC 9721
98 * ADC: Philips 1361T (Stereo 24bit)
99 * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
103 * P17 Chip: CA0106-DAT
105 * ADC: WM8775EDS (4 Channel)
106 * DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
107 * SPDIF Out control switches between Mic in and SPDIF out.
108 * No sound out or mic input working yet.
112 * P17 Chip: CA0106-DAT
116 * Trying to handle it like the SB0410.
118 * This code was initally based on code from ALSA's emu10k1x.c which is:
119 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
121 * This program is free software; you can redistribute it and/or modify
122 * it under the terms of the GNU General Public License as published by
123 * the Free Software Foundation; either version 2 of the License, or
124 * (at your option) any later version.
126 * This program is distributed in the hope that it will be useful,
127 * but WITHOUT ANY WARRANTY; without even the implied warranty of
128 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
129 * GNU General Public License for more details.
131 * You should have received a copy of the GNU General Public License
132 * along with this program; if not, write to the Free Software
133 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
136 #include <sound/driver.h>
137 #include <linux/delay.h>
138 #include <linux/init.h>
139 #include <linux/interrupt.h>
140 #include <linux/pci.h>
141 #include <linux/slab.h>
142 #include <linux/moduleparam.h>
143 #include <linux/dma-mapping.h>
144 #include <sound/core.h>
145 #include <sound/initval.h>
146 #include <sound/pcm.h>
147 #include <sound/ac97_codec.h>
148 #include <sound/info.h>
150 MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
151 MODULE_DESCRIPTION("CA0106");
152 MODULE_LICENSE("GPL");
153 MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
155 // module parameters (see "Module Parameters")
156 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
157 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
158 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
159 static uint subsystem[SNDRV_CARDS]; /* Force card subsystem model */
161 module_param_array(index, int, NULL, 0444);
162 MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
163 module_param_array(id, charp, NULL, 0444);
164 MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
165 module_param_array(enable, bool, NULL, 0444);
166 MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
167 module_param_array(subsystem, uint, NULL, 0444);
168 MODULE_PARM_DESC(subsystem, "Force card subsystem model.");
172 static struct snd_ca0106_details ca0106_chip_details[] = {
173 /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
174 /* It is really just a normal SB Live 24bit. */
178 { .serial = 0x10131102,
179 .name = "X-Fi Extreme Audio [SBxxxx]",
182 /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
183 /* It is really just a normal SB Live 24bit. */
190 * Playback on front, rear, center/lfe speakers
191 * Capture from Mic in.
193 * Capture from Line in.
194 * Playback to digital out.
196 { .serial = 0x10121102,
197 .name = "X-Fi Extreme Audio [SB0790]",
200 /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
201 /* AudigyLS[SB0310] */
202 { .serial = 0x10021102,
203 .name = "AudigyLS [SB0310]",
205 /* Unknown AudigyLS that also says SB0310 on it */
206 { .serial = 0x10051102,
207 .name = "AudigyLS [SB0310b]",
209 /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
210 { .serial = 0x10061102,
211 .name = "Live! 7.1 24bit [SB0410]",
214 /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
215 { .serial = 0x10071102,
216 .name = "Live! 7.1 24bit [SB0413]",
219 /* New Audigy SE. Has a different DAC. */
225 { .serial = 0x100a1102,
226 .name = "Audigy SE [SB0570]",
230 /* New Audigy LS. Has a different DAC. */
236 { .serial = 0x10111102,
237 .name = "Audigy SE OEM [SB0570a]",
241 /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
247 { .serial = 0x10091462,
248 .name = "MSI K8N Diamond MB [SB0438]",
251 /* Shuttle XPC SD31P which has an onboard Creative Labs
252 * Sound Blaster Live! 24-bit EAX
253 * high-definition 7.1 audio processor".
254 * Added using info from andrewvegan in alsa bug #1298
256 { .serial = 0x30381297,
257 .name = "Shuttle XPC SD31P [SD31P]",
260 /* Shuttle XPC SD11G5 which has an onboard Creative Labs
261 * Sound Blaster Live! 24-bit EAX
262 * high-definition 7.1 audio processor".
263 * Fixes ALSA bug#1600
265 { .serial = 0x30411297,
266 .name = "Shuttle XPC SD11G5 [SD11G5]",
270 .name = "AudigyLS [Unknown]" }
273 /* hardware definition */
274 static struct snd_pcm_hardware snd_ca0106_playback_hw = {
275 .info = (SNDRV_PCM_INFO_MMAP |
276 SNDRV_PCM_INFO_INTERLEAVED |
277 SNDRV_PCM_INFO_BLOCK_TRANSFER |
278 SNDRV_PCM_INFO_MMAP_VALID),
279 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
280 .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
281 SNDRV_PCM_RATE_192000),
284 .channels_min = 2, //1,
285 .channels_max = 2, //6,
286 .buffer_bytes_max = ((65536 - 64) * 8),
287 .period_bytes_min = 64,
288 .period_bytes_max = (65536 - 64),
294 static struct snd_pcm_hardware snd_ca0106_capture_hw = {
295 .info = (SNDRV_PCM_INFO_MMAP |
296 SNDRV_PCM_INFO_INTERLEAVED |
297 SNDRV_PCM_INFO_BLOCK_TRANSFER |
298 SNDRV_PCM_INFO_MMAP_VALID),
299 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
300 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
301 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
306 .buffer_bytes_max = ((65536 - 64) * 8),
307 .period_bytes_min = 64,
308 .period_bytes_max = (65536 - 64),
314 unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
319 unsigned int regptr, val;
321 regptr = (reg << 16) | chn;
323 spin_lock_irqsave(&emu->emu_lock, flags);
324 outl(regptr, emu->port + PTR);
325 val = inl(emu->port + DATA);
326 spin_unlock_irqrestore(&emu->emu_lock, flags);
330 void snd_ca0106_ptr_write(struct snd_ca0106 *emu,
338 regptr = (reg << 16) | chn;
340 spin_lock_irqsave(&emu->emu_lock, flags);
341 outl(regptr, emu->port + PTR);
342 outl(data, emu->port + DATA);
343 spin_unlock_irqrestore(&emu->emu_lock, flags);
346 int snd_ca0106_spi_write(struct snd_ca0106 * emu,
349 unsigned int reset, set;
350 unsigned int reg, tmp;
353 if (data > 0xffff) /* Only 16bit values allowed */
355 tmp = snd_ca0106_ptr_read(emu, reg, 0);
356 reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
357 set = reset | 0x10000; /* Set xxx1xxxx */
358 snd_ca0106_ptr_write(emu, reg, 0, reset | data);
359 tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
360 snd_ca0106_ptr_write(emu, reg, 0, set | data);
362 /* Wait for status bit to return to 0 */
363 for (n = 0; n < 100; n++) {
365 tmp = snd_ca0106_ptr_read(emu, reg, 0);
366 if (!(tmp & 0x10000)) {
371 if (result) /* Timed out */
373 snd_ca0106_ptr_write(emu, reg, 0, reset | data);
374 tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
378 /* The ADC does not support i2c read, so only write is implemented */
379 int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
387 if ((reg > 0x7f) || (value > 0x1ff)) {
388 snd_printk(KERN_ERR "i2c_write: invalid values.\n");
392 tmp = reg << 25 | value << 16;
393 // snd_printk("I2C-write:reg=0x%x, value=0x%x\n", reg, value);
394 /* Not sure what this I2C channel controls. */
395 /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
397 /* This controls the I2C connected to the WM8775 ADC Codec */
398 snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
400 for (retry = 0; retry < 10; retry++) {
401 /* Send the data to i2c */
402 //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
403 //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
405 tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
406 snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
408 /* Wait till the transaction ends */
410 status = snd_ca0106_ptr_read(emu, I2C_A, 0);
411 //snd_printk("I2C:status=0x%x\n", status);
413 if ((status & I2C_A_ADC_START) == 0)
419 //Read back and see if the transaction is successful
420 if ((status & I2C_A_ADC_ABORT) == 0)
425 snd_printk(KERN_ERR "Writing to ADC failed!\n");
433 static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
438 spin_lock_irqsave(&emu->emu_lock, flags);
439 enable = inl(emu->port + INTE) | intrenb;
440 outl(enable, emu->port + INTE);
441 spin_unlock_irqrestore(&emu->emu_lock, flags);
444 static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
449 spin_lock_irqsave(&emu->emu_lock, flags);
450 enable = inl(emu->port + INTE) & ~intrenb;
451 outl(enable, emu->port + INTE);
452 spin_unlock_irqrestore(&emu->emu_lock, flags);
456 static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
458 kfree(runtime->private_data);
461 /* open_playback callback */
462 static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
465 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
466 struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
467 struct snd_ca0106_pcm *epcm;
468 struct snd_pcm_runtime *runtime = substream->runtime;
471 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
476 epcm->substream = substream;
477 epcm->channel_id=channel_id;
479 runtime->private_data = epcm;
480 runtime->private_free = snd_ca0106_pcm_free_substream;
482 runtime->hw = snd_ca0106_playback_hw;
485 channel->number = channel_id;
488 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
489 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
490 channel->epcm = epcm;
491 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
493 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
499 static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
501 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
502 struct snd_pcm_runtime *runtime = substream->runtime;
503 struct snd_ca0106_pcm *epcm = runtime->private_data;
504 chip->playback_channels[epcm->channel_id].use = 0;
505 /* FIXME: maybe zero others */
509 static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
511 return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
514 static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
516 return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
519 static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
521 return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
524 static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
526 return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
529 /* open_capture callback */
530 static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
533 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
534 struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
535 struct snd_ca0106_pcm *epcm;
536 struct snd_pcm_runtime *runtime = substream->runtime;
539 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
541 snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
545 epcm->substream = substream;
546 epcm->channel_id=channel_id;
548 runtime->private_data = epcm;
549 runtime->private_free = snd_ca0106_pcm_free_substream;
551 runtime->hw = snd_ca0106_capture_hw;
554 channel->number = channel_id;
557 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
558 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
559 channel->epcm = epcm;
560 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
562 //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
563 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
569 static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
571 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
572 struct snd_pcm_runtime *runtime = substream->runtime;
573 struct snd_ca0106_pcm *epcm = runtime->private_data;
574 chip->capture_channels[epcm->channel_id].use = 0;
575 /* FIXME: maybe zero others */
579 static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
581 return snd_ca0106_pcm_open_capture_channel(substream, 0);
584 static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
586 return snd_ca0106_pcm_open_capture_channel(substream, 1);
589 static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
591 return snd_ca0106_pcm_open_capture_channel(substream, 2);
594 static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
596 return snd_ca0106_pcm_open_capture_channel(substream, 3);
599 /* hw_params callback */
600 static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
601 struct snd_pcm_hw_params *hw_params)
603 return snd_pcm_lib_malloc_pages(substream,
604 params_buffer_bytes(hw_params));
607 /* hw_free callback */
608 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
610 return snd_pcm_lib_free_pages(substream);
613 /* hw_params callback */
614 static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
615 struct snd_pcm_hw_params *hw_params)
617 return snd_pcm_lib_malloc_pages(substream,
618 params_buffer_bytes(hw_params));
621 /* hw_free callback */
622 static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
624 return snd_pcm_lib_free_pages(substream);
627 /* prepare playback callback */
628 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
630 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
631 struct snd_pcm_runtime *runtime = substream->runtime;
632 struct snd_ca0106_pcm *epcm = runtime->private_data;
633 int channel = epcm->channel_id;
634 u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
635 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
636 u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
637 u32 hcfg_set = 0x00000000;
639 u32 reg40_mask = 0x30000 << (channel<<1);
642 /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
643 u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
648 //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
649 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
650 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
651 /* Rate can be set per channel. */
652 /* reg40 control host to fifo */
653 /* reg71 controls DAC rate. */
654 switch (runtime->rate) {
656 reg40_set = 0x10000 << (channel<<1);
657 reg71_set = 0x01010000;
664 reg40_set = 0x20000 << (channel<<1);
665 reg71_set = 0x02020000;
668 reg40_set = 0x30000 << (channel<<1);
669 reg71_set = 0x03030000;
676 /* Format is a global setting */
677 /* FIXME: Only let the first channel accessed set this. */
678 switch (runtime->format) {
679 case SNDRV_PCM_FORMAT_S16_LE:
682 case SNDRV_PCM_FORMAT_S32_LE:
683 hcfg_set = HCFG_PLAYBACK_S32_LE;
689 hcfg = inl(emu->port + HCFG) ;
690 hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
691 outl(hcfg, emu->port + HCFG);
692 reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
693 reg40 = (reg40 & ~reg40_mask) | reg40_set;
694 snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
695 reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
696 reg71 = (reg71 & ~reg71_mask) | reg71_set;
697 snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
699 /* FIXME: Check emu->buffer.size before actually writing to it. */
700 for(i=0; i < runtime->periods; i++) {
701 table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
702 table_base[i*2+1] = period_size_bytes << 16;
705 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
706 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
707 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
708 snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
709 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
710 /* FIXME test what 0 bytes does. */
711 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
712 snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
713 snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
714 snd_ca0106_ptr_write(emu, 0x08, channel, 0);
715 snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
717 snd_ca0106_ptr_write(emu, SPCS0, 0,
718 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
719 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
720 SPCS_GENERATIONSTATUS | 0x00001200 |
721 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
728 /* prepare capture callback */
729 static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
731 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
732 struct snd_pcm_runtime *runtime = substream->runtime;
733 struct snd_ca0106_pcm *epcm = runtime->private_data;
734 int channel = epcm->channel_id;
735 u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
736 u32 hcfg_set = 0x00000000;
738 u32 over_sampling=0x2;
739 u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
743 //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
744 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
745 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
746 /* reg71 controls ADC rate. */
747 switch (runtime->rate) {
749 reg71_set = 0x00004000;
755 reg71_set = 0x00008000;
759 reg71_set = 0x0000c000;
766 /* Format is a global setting */
767 /* FIXME: Only let the first channel accessed set this. */
768 switch (runtime->format) {
769 case SNDRV_PCM_FORMAT_S16_LE:
772 case SNDRV_PCM_FORMAT_S32_LE:
773 hcfg_set = HCFG_CAPTURE_S32_LE;
779 hcfg = inl(emu->port + HCFG) ;
780 hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
781 outl(hcfg, emu->port + HCFG);
782 reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
783 reg71 = (reg71 & ~reg71_mask) | reg71_set;
784 snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
785 if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
786 snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
790 //printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, frames_to_bytes(runtime, 1));
791 snd_ca0106_ptr_write(emu, 0x13, channel, 0);
792 snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
793 snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
794 snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
799 /* trigger_playback callback */
800 static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
803 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
804 struct snd_pcm_runtime *runtime;
805 struct snd_ca0106_pcm *epcm;
808 struct snd_pcm_substream *s;
814 case SNDRV_PCM_TRIGGER_START:
817 case SNDRV_PCM_TRIGGER_STOP:
822 snd_pcm_group_for_each_entry(s, substream) {
823 runtime = s->runtime;
824 epcm = runtime->private_data;
825 channel = epcm->channel_id;
826 //snd_printk("channel=%d\n",channel);
827 epcm->running = running;
828 basic |= (0x1<<channel);
829 extended |= (0x10<<channel);
830 snd_pcm_trigger_done(s, substream);
832 //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
835 case SNDRV_PCM_TRIGGER_START:
836 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
837 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
839 case SNDRV_PCM_TRIGGER_STOP:
840 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
841 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
850 /* trigger_capture callback */
851 static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
854 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
855 struct snd_pcm_runtime *runtime = substream->runtime;
856 struct snd_ca0106_pcm *epcm = runtime->private_data;
857 int channel = epcm->channel_id;
861 case SNDRV_PCM_TRIGGER_START:
862 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
863 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
866 case SNDRV_PCM_TRIGGER_STOP:
867 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
868 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
878 /* pointer_playback callback */
879 static snd_pcm_uframes_t
880 snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
882 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct snd_ca0106_pcm *epcm = runtime->private_data;
885 snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
886 int channel = epcm->channel_id;
891 ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
892 ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
893 ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
894 if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
895 ptr2 = bytes_to_frames(runtime, ptr1);
896 ptr2+= (ptr4 >> 3) * runtime->period_size;
898 if (ptr >= runtime->buffer_size)
899 ptr -= runtime->buffer_size;
900 //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
905 /* pointer_capture callback */
906 static snd_pcm_uframes_t
907 snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
909 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
910 struct snd_pcm_runtime *runtime = substream->runtime;
911 struct snd_ca0106_pcm *epcm = runtime->private_data;
912 snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
913 int channel = channel=epcm->channel_id;
918 ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
919 ptr2 = bytes_to_frames(runtime, ptr1);
921 if (ptr >= runtime->buffer_size)
922 ptr -= runtime->buffer_size;
923 //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
929 static struct snd_pcm_ops snd_ca0106_playback_front_ops = {
930 .open = snd_ca0106_pcm_open_playback_front,
931 .close = snd_ca0106_pcm_close_playback,
932 .ioctl = snd_pcm_lib_ioctl,
933 .hw_params = snd_ca0106_pcm_hw_params_playback,
934 .hw_free = snd_ca0106_pcm_hw_free_playback,
935 .prepare = snd_ca0106_pcm_prepare_playback,
936 .trigger = snd_ca0106_pcm_trigger_playback,
937 .pointer = snd_ca0106_pcm_pointer_playback,
940 static struct snd_pcm_ops snd_ca0106_capture_0_ops = {
941 .open = snd_ca0106_pcm_open_0_capture,
942 .close = snd_ca0106_pcm_close_capture,
943 .ioctl = snd_pcm_lib_ioctl,
944 .hw_params = snd_ca0106_pcm_hw_params_capture,
945 .hw_free = snd_ca0106_pcm_hw_free_capture,
946 .prepare = snd_ca0106_pcm_prepare_capture,
947 .trigger = snd_ca0106_pcm_trigger_capture,
948 .pointer = snd_ca0106_pcm_pointer_capture,
951 static struct snd_pcm_ops snd_ca0106_capture_1_ops = {
952 .open = snd_ca0106_pcm_open_1_capture,
953 .close = snd_ca0106_pcm_close_capture,
954 .ioctl = snd_pcm_lib_ioctl,
955 .hw_params = snd_ca0106_pcm_hw_params_capture,
956 .hw_free = snd_ca0106_pcm_hw_free_capture,
957 .prepare = snd_ca0106_pcm_prepare_capture,
958 .trigger = snd_ca0106_pcm_trigger_capture,
959 .pointer = snd_ca0106_pcm_pointer_capture,
962 static struct snd_pcm_ops snd_ca0106_capture_2_ops = {
963 .open = snd_ca0106_pcm_open_2_capture,
964 .close = snd_ca0106_pcm_close_capture,
965 .ioctl = snd_pcm_lib_ioctl,
966 .hw_params = snd_ca0106_pcm_hw_params_capture,
967 .hw_free = snd_ca0106_pcm_hw_free_capture,
968 .prepare = snd_ca0106_pcm_prepare_capture,
969 .trigger = snd_ca0106_pcm_trigger_capture,
970 .pointer = snd_ca0106_pcm_pointer_capture,
973 static struct snd_pcm_ops snd_ca0106_capture_3_ops = {
974 .open = snd_ca0106_pcm_open_3_capture,
975 .close = snd_ca0106_pcm_close_capture,
976 .ioctl = snd_pcm_lib_ioctl,
977 .hw_params = snd_ca0106_pcm_hw_params_capture,
978 .hw_free = snd_ca0106_pcm_hw_free_capture,
979 .prepare = snd_ca0106_pcm_prepare_capture,
980 .trigger = snd_ca0106_pcm_trigger_capture,
981 .pointer = snd_ca0106_pcm_pointer_capture,
984 static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
985 .open = snd_ca0106_pcm_open_playback_center_lfe,
986 .close = snd_ca0106_pcm_close_playback,
987 .ioctl = snd_pcm_lib_ioctl,
988 .hw_params = snd_ca0106_pcm_hw_params_playback,
989 .hw_free = snd_ca0106_pcm_hw_free_playback,
990 .prepare = snd_ca0106_pcm_prepare_playback,
991 .trigger = snd_ca0106_pcm_trigger_playback,
992 .pointer = snd_ca0106_pcm_pointer_playback,
995 static struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
996 .open = snd_ca0106_pcm_open_playback_unknown,
997 .close = snd_ca0106_pcm_close_playback,
998 .ioctl = snd_pcm_lib_ioctl,
999 .hw_params = snd_ca0106_pcm_hw_params_playback,
1000 .hw_free = snd_ca0106_pcm_hw_free_playback,
1001 .prepare = snd_ca0106_pcm_prepare_playback,
1002 .trigger = snd_ca0106_pcm_trigger_playback,
1003 .pointer = snd_ca0106_pcm_pointer_playback,
1006 static struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
1007 .open = snd_ca0106_pcm_open_playback_rear,
1008 .close = snd_ca0106_pcm_close_playback,
1009 .ioctl = snd_pcm_lib_ioctl,
1010 .hw_params = snd_ca0106_pcm_hw_params_playback,
1011 .hw_free = snd_ca0106_pcm_hw_free_playback,
1012 .prepare = snd_ca0106_pcm_prepare_playback,
1013 .trigger = snd_ca0106_pcm_trigger_playback,
1014 .pointer = snd_ca0106_pcm_pointer_playback,
1018 static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
1021 struct snd_ca0106 *emu = ac97->private_data;
1022 unsigned long flags;
1025 spin_lock_irqsave(&emu->emu_lock, flags);
1026 outb(reg, emu->port + AC97ADDRESS);
1027 val = inw(emu->port + AC97DATA);
1028 spin_unlock_irqrestore(&emu->emu_lock, flags);
1032 static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
1033 unsigned short reg, unsigned short val)
1035 struct snd_ca0106 *emu = ac97->private_data;
1036 unsigned long flags;
1038 spin_lock_irqsave(&emu->emu_lock, flags);
1039 outb(reg, emu->port + AC97ADDRESS);
1040 outw(val, emu->port + AC97DATA);
1041 spin_unlock_irqrestore(&emu->emu_lock, flags);
1044 static int snd_ca0106_ac97(struct snd_ca0106 *chip)
1046 struct snd_ac97_bus *pbus;
1047 struct snd_ac97_template ac97;
1049 static struct snd_ac97_bus_ops ops = {
1050 .write = snd_ca0106_ac97_write,
1051 .read = snd_ca0106_ac97_read,
1054 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1056 pbus->no_vra = 1; /* we don't need VRA */
1058 memset(&ac97, 0, sizeof(ac97));
1059 ac97.private_data = chip;
1060 ac97.scaps = AC97_SCAP_NO_SPDIF;
1061 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1064 static int snd_ca0106_free(struct snd_ca0106 *chip)
1066 if (chip->res_port != NULL) { /* avoid access to already used hardware */
1067 // disable interrupts
1068 snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
1069 outl(0, chip->port + INTE);
1070 snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
1073 //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
1074 outl(0, chip->port + HCFG);
1075 /* FIXME: We need to stop and DMA transfers here.
1076 * But as I am not sure how yet, we cannot from the dma pages.
1077 * So we can fix: snd-malloc: Memory leak? pages not freed = 8
1082 if (chip->buffer.area)
1083 snd_dma_free_pages(&chip->buffer);
1086 // release the i/o port
1087 release_and_free_resource(chip->res_port);
1091 free_irq(chip->irq, chip);
1092 pci_disable_device(chip->pci);
1097 static int snd_ca0106_dev_free(struct snd_device *device)
1099 struct snd_ca0106 *chip = device->device_data;
1100 return snd_ca0106_free(chip);
1103 static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
1105 unsigned int status;
1107 struct snd_ca0106 *chip = dev_id;
1110 unsigned int stat76;
1111 struct snd_ca0106_channel *pchannel;
1113 status = inl(chip->port + IPR);
1117 stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
1118 //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
1119 //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
1120 mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
1121 for(i = 0; i < 4; i++) {
1122 pchannel = &(chip->playback_channels[i]);
1123 if (stat76 & mask) {
1124 /* FIXME: Select the correct substream for period elapsed */
1126 snd_pcm_period_elapsed(pchannel->epcm->substream);
1127 //printk(KERN_INFO "interrupt [%d] used\n", i);
1130 //printk(KERN_INFO "channel=%p\n",pchannel);
1131 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1134 mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
1135 for(i = 0; i < 4; i++) {
1136 pchannel = &(chip->capture_channels[i]);
1137 if (stat76 & mask) {
1138 /* FIXME: Select the correct substream for period elapsed */
1140 snd_pcm_period_elapsed(pchannel->epcm->substream);
1141 //printk(KERN_INFO "interrupt [%d] used\n", i);
1144 //printk(KERN_INFO "channel=%p\n",pchannel);
1145 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1149 snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
1151 if (chip->midi.dev_id &&
1152 (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
1153 if (chip->midi.interrupt)
1154 chip->midi.interrupt(&chip->midi, status);
1156 chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
1159 // acknowledge the interrupt if necessary
1160 outl(status, chip->port+IPR);
1165 static int __devinit snd_ca0106_pcm(struct snd_ca0106 *emu, int device, struct snd_pcm **rpcm)
1167 struct snd_pcm *pcm;
1168 struct snd_pcm_substream *substream;
1173 if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
1176 pcm->private_data = emu;
1180 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
1181 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
1184 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
1185 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
1188 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
1189 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
1192 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
1193 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
1197 pcm->info_flags = 0;
1198 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1199 strcpy(pcm->name, "CA0106");
1202 for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1204 substream = substream->next) {
1205 if ((err = snd_pcm_lib_preallocate_pages(substream,
1207 snd_dma_pci_data(emu->pci),
1208 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
1212 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
1214 substream = substream->next) {
1215 if ((err = snd_pcm_lib_preallocate_pages(substream,
1217 snd_dma_pci_data(emu->pci),
1218 64*1024, 64*1024)) < 0)
1228 static unsigned int spi_dac_init[] = {
1233 0x0620, /* Set 24 bit. Was 0x0600 */
1252 static unsigned int i2c_adc_init[][2] = {
1253 { 0x17, 0x00 }, /* Reset */
1254 { 0x07, 0x00 }, /* Timeout */
1255 { 0x0b, 0x22 }, /* Interface control */
1256 { 0x0c, 0x22 }, /* Master mode control */
1257 { 0x0d, 0x08 }, /* Powerdown control */
1258 { 0x0e, 0xcf }, /* Attenuation Left 0x01 = -103dB, 0xff = 24dB */
1259 { 0x0f, 0xcf }, /* Attenuation Right 0.5dB steps */
1260 { 0x10, 0x7b }, /* ALC Control 1 */
1261 { 0x11, 0x00 }, /* ALC Control 2 */
1262 { 0x12, 0x32 }, /* ALC Control 3 */
1263 { 0x13, 0x00 }, /* Noise gate control */
1264 { 0x14, 0xa6 }, /* Limiter control */
1265 { 0x15, ADC_MUX_LINEIN }, /* ADC Mixer control */
1268 static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
1269 struct pci_dev *pci,
1270 struct snd_ca0106 **rchip)
1272 struct snd_ca0106 *chip;
1273 struct snd_ca0106_details *c;
1276 static struct snd_device_ops ops = {
1277 .dev_free = snd_ca0106_dev_free,
1282 if ((err = pci_enable_device(pci)) < 0)
1284 if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
1285 pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
1286 printk(KERN_ERR "error to set 32bit mask DMA\n");
1287 pci_disable_device(pci);
1291 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1293 pci_disable_device(pci);
1301 spin_lock_init(&chip->emu_lock);
1303 chip->port = pci_resource_start(pci, 0);
1304 if ((chip->res_port = request_region(chip->port, 0x20,
1305 "snd_ca0106")) == NULL) {
1306 snd_ca0106_free(chip);
1307 printk(KERN_ERR "cannot allocate the port\n");
1311 if (request_irq(pci->irq, snd_ca0106_interrupt,
1312 IRQF_SHARED, "snd_ca0106", chip)) {
1313 snd_ca0106_free(chip);
1314 printk(KERN_ERR "cannot grab irq\n");
1317 chip->irq = pci->irq;
1319 /* This stores the periods table. */
1320 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
1321 snd_ca0106_free(chip);
1325 pci_set_master(pci);
1327 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
1328 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
1330 printk(KERN_INFO "snd-ca0106: Model %04x Rev %08x Serial %08x\n", chip->model,
1331 pci->revision, chip->serial);
1333 strcpy(card->driver, "CA0106");
1334 strcpy(card->shortname, "CA0106");
1336 for (c = ca0106_chip_details; c->serial; c++) {
1337 if (subsystem[dev]) {
1338 if (c->serial == subsystem[dev])
1340 } else if (c->serial == chip->serial)
1344 if (subsystem[dev]) {
1345 printk(KERN_INFO "snd-ca0106: Sound card name=%s, subsystem=0x%x. Forced to subsystem=0x%x\n",
1346 c->name, chip->serial, subsystem[dev]);
1349 sprintf(card->longname, "%s at 0x%lx irq %i",
1350 c->name, chip->port, chip->irq);
1352 outl(0, chip->port + INTE);
1355 * Init to 0x02109204 :
1356 * Clock accuracy = 0 (1000ppm)
1357 * Sample Rate = 2 (48kHz)
1358 * Audio Channel = 1 (Left of 2)
1359 * Source Number = 0 (Unspecified)
1360 * Generation Status = 1 (Original for Cat Code 12)
1361 * Cat Code = 12 (Digital Signal Mixer)
1363 * Emphasis = 0 (None)
1364 * CP = 1 (Copyright unasserted)
1365 * AN = 0 (Audio data)
1368 snd_ca0106_ptr_write(chip, SPCS0, 0,
1369 chip->spdif_bits[0] =
1370 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1371 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1372 SPCS_GENERATIONSTATUS | 0x00001200 |
1373 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1374 /* Only SPCS1 has been tested */
1375 snd_ca0106_ptr_write(chip, SPCS1, 0,
1376 chip->spdif_bits[1] =
1377 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1378 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1379 SPCS_GENERATIONSTATUS | 0x00001200 |
1380 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1381 snd_ca0106_ptr_write(chip, SPCS2, 0,
1382 chip->spdif_bits[2] =
1383 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1384 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1385 SPCS_GENERATIONSTATUS | 0x00001200 |
1386 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1387 snd_ca0106_ptr_write(chip, SPCS3, 0,
1388 chip->spdif_bits[3] =
1389 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1390 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1391 SPCS_GENERATIONSTATUS | 0x00001200 |
1392 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1394 snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
1395 snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
1397 /* Write 0x8000 to AC97_REC_GAIN to mute it. */
1398 outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
1399 outw(0x8000, chip->port + AC97DATA);
1401 snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
1402 snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
1403 snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
1404 snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
1407 //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
1408 /* Analog or Digital output */
1409 snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
1410 snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
1411 chip->spdif_enable = 0; /* Set digital SPDIF output off */
1412 //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
1413 //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
1415 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
1416 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
1417 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
1418 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
1419 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
1420 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
1421 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
1422 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
1423 for(ch = 0; ch < 4; ch++) {
1424 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
1425 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
1426 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
1427 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
1428 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
1429 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
1431 if (chip->details->i2c_adc == 1) {
1432 /* Select MIC, Line in, TAD in, AUX in */
1433 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
1434 /* Default to CAPTURE_SOURCE to i2s in */
1435 chip->capture_source = 3;
1436 } else if (chip->details->ac97 == 1) {
1437 /* Default to AC97 in */
1438 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x444400e4);
1439 /* Default to CAPTURE_SOURCE to AC97 in */
1440 chip->capture_source = 4;
1442 /* Select MIC, Line in, TAD in, AUX in */
1443 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
1444 /* Default to Set CAPTURE_SOURCE to i2s in */
1445 chip->capture_source = 3;
1448 if (chip->details->gpio_type == 2) { /* The SB0438 use GPIO differently. */
1449 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1450 outl(0x0, chip->port+GPIO);
1451 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1452 outl(0x005f5301, chip->port+GPIO); /* Analog */
1453 } else if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
1454 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1455 outl(0x0, chip->port+GPIO);
1456 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1457 outl(0x005f5301, chip->port+GPIO); /* Analog */
1459 outl(0x0, chip->port+GPIO);
1460 outl(0x005f03a3, chip->port+GPIO); /* Analog */
1461 //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
1463 snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
1465 //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1466 //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
1467 //outl(0x00000009, chip->port+HCFG);
1468 outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
1470 if (chip->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
1473 size = ARRAY_SIZE(i2c_adc_init);
1474 //snd_printk("I2C:array size=0x%x\n", size);
1475 for (n=0; n < size; n++) {
1476 snd_ca0106_i2c_write(chip, i2c_adc_init[n][0], i2c_adc_init[n][1]);
1478 for (n=0; n < 4; n++) {
1479 chip->i2c_capture_volume[n][0]= 0xcf;
1480 chip->i2c_capture_volume[n][1]= 0xcf;
1482 chip->i2c_capture_source=2; /* Line in */
1483 //snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
1485 if (chip->details->spi_dac == 1) { /* The SB0570 use SPI to control DAC. */
1488 size = ARRAY_SIZE(spi_dac_init);
1489 for (n = 0; n < size; n++) {
1490 int reg = spi_dac_init[n] >> SPI_REG_SHIFT;
1492 snd_ca0106_spi_write(chip, spi_dac_init[n]);
1493 if (reg < ARRAY_SIZE(chip->spi_dac_reg))
1494 chip->spi_dac_reg[reg] = spi_dac_init[n];
1498 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1500 snd_ca0106_free(chip);
1508 static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
1510 snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
1513 static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
1515 snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
1518 static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
1520 return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
1521 midi->port + idx, 0);
1524 static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
1526 snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
1529 static struct snd_card *ca0106_dev_id_card(void *dev_id)
1531 return ((struct snd_ca0106 *)dev_id)->card;
1534 static int ca0106_dev_id_port(void *dev_id)
1536 return ((struct snd_ca0106 *)dev_id)->port;
1539 static int __devinit snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
1541 struct snd_ca_midi *midi;
1545 if (channel == CA0106_MIDI_CHAN_B) {
1546 name = "CA0106 MPU-401 (UART) B";
1547 midi = &chip->midi2;
1548 midi->tx_enable = INTE_MIDI_TX_B;
1549 midi->rx_enable = INTE_MIDI_RX_B;
1550 midi->ipr_tx = IPR_MIDI_TX_B;
1551 midi->ipr_rx = IPR_MIDI_RX_B;
1552 midi->port = MIDI_UART_B_DATA;
1554 name = "CA0106 MPU-401 (UART)";
1556 midi->tx_enable = INTE_MIDI_TX_A;
1557 midi->rx_enable = INTE_MIDI_TX_B;
1558 midi->ipr_tx = IPR_MIDI_TX_A;
1559 midi->ipr_rx = IPR_MIDI_RX_A;
1560 midi->port = MIDI_UART_A_DATA;
1563 midi->reset = CA0106_MPU401_RESET;
1564 midi->enter_uart = CA0106_MPU401_ENTER_UART;
1565 midi->ack = CA0106_MPU401_ACK;
1567 midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
1568 midi->output_ready = CA0106_MIDI_OUTPUT_READY;
1570 midi->channel = channel;
1572 midi->interrupt_enable = ca0106_midi_interrupt_enable;
1573 midi->interrupt_disable = ca0106_midi_interrupt_disable;
1575 midi->read = ca0106_midi_read;
1576 midi->write = ca0106_midi_write;
1578 midi->get_dev_id_card = ca0106_dev_id_card;
1579 midi->get_dev_id_port = ca0106_dev_id_port;
1581 midi->dev_id = chip;
1583 if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
1590 static int __devinit snd_ca0106_probe(struct pci_dev *pci,
1591 const struct pci_device_id *pci_id)
1594 struct snd_card *card;
1595 struct snd_ca0106 *chip;
1598 if (dev >= SNDRV_CARDS)
1605 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1609 if ((err = snd_ca0106_create(dev, card, pci, &chip)) < 0) {
1610 snd_card_free(card);
1614 if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
1615 snd_card_free(card);
1618 if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
1619 snd_card_free(card);
1622 if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
1623 snd_card_free(card);
1626 if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
1627 snd_card_free(card);
1630 if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
1631 if ((err = snd_ca0106_ac97(chip)) < 0) {
1632 snd_card_free(card);
1636 if ((err = snd_ca0106_mixer(chip)) < 0) {
1637 snd_card_free(card);
1641 snd_printdd("ca0106: probe for MIDI channel A ...");
1642 if ((err = snd_ca0106_midi(chip,CA0106_MIDI_CHAN_A)) < 0) {
1643 snd_card_free(card);
1644 snd_printdd(" failed, err=0x%x\n",err);
1647 snd_printdd(" done.\n");
1649 #ifdef CONFIG_PROC_FS
1650 snd_ca0106_proc_init(chip);
1653 snd_card_set_dev(card, &pci->dev);
1655 if ((err = snd_card_register(card)) < 0) {
1656 snd_card_free(card);
1660 pci_set_drvdata(pci, card);
1665 static void __devexit snd_ca0106_remove(struct pci_dev *pci)
1667 snd_card_free(pci_get_drvdata(pci));
1668 pci_set_drvdata(pci, NULL);
1672 static struct pci_device_id snd_ca0106_ids[] = {
1673 { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
1676 MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
1678 // pci_driver definition
1679 static struct pci_driver driver = {
1681 .id_table = snd_ca0106_ids,
1682 .probe = snd_ca0106_probe,
1683 .remove = __devexit_p(snd_ca0106_remove),
1686 // initialization of the module
1687 static int __init alsa_card_ca0106_init(void)
1689 return pci_register_driver(&driver);
1692 // clean up the module
1693 static void __exit alsa_card_ca0106_exit(void)
1695 pci_unregister_driver(&driver);
1698 module_init(alsa_card_ca0106_init)
1699 module_exit(alsa_card_ca0106_exit)