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.
84 * Some stability problems when unloading the snd-ca0106 kernel module.
88 * 4 Capture channels, only one implemented so far.
89 * Other capture rates apart from 48khz not implemented.
94 * P17 Chip: CA0106-DAT
95 * AC97 Codec: STAC 9721
96 * ADC: Philips 1361T (Stereo 24bit)
97 * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
101 * P17 Chip: CA0106-DAT
103 * ADC: WM8775EDS (4 Channel)
104 * DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
105 * SPDIF Out control switches between Mic in and SPDIF out.
106 * No sound out or mic input working yet.
110 * P17 Chip: CA0106-DAT
114 * Trying to handle it like the SB0410.
116 * This code was initally based on code from ALSA's emu10k1x.c which is:
117 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
119 * This program is free software; you can redistribute it and/or modify
120 * it under the terms of the GNU General Public License as published by
121 * the Free Software Foundation; either version 2 of the License, or
122 * (at your option) any later version.
124 * This program is distributed in the hope that it will be useful,
125 * but WITHOUT ANY WARRANTY; without even the implied warranty of
126 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
127 * GNU General Public License for more details.
129 * You should have received a copy of the GNU General Public License
130 * along with this program; if not, write to the Free Software
131 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
134 #include <sound/driver.h>
135 #include <linux/delay.h>
136 #include <linux/init.h>
137 #include <linux/interrupt.h>
138 #include <linux/pci.h>
139 #include <linux/slab.h>
140 #include <linux/moduleparam.h>
141 #include <linux/dma-mapping.h>
142 #include <sound/core.h>
143 #include <sound/initval.h>
144 #include <sound/pcm.h>
145 #include <sound/ac97_codec.h>
146 #include <sound/info.h>
148 MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
149 MODULE_DESCRIPTION("CA0106");
150 MODULE_LICENSE("GPL");
151 MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
153 // module parameters (see "Module Parameters")
154 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
155 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
;
156 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
157 static uint subsystem
[SNDRV_CARDS
]; /* Force card subsystem model */
159 module_param_array(index
, int, NULL
, 0444);
160 MODULE_PARM_DESC(index
, "Index value for the CA0106 soundcard.");
161 module_param_array(id
, charp
, NULL
, 0444);
162 MODULE_PARM_DESC(id
, "ID string for the CA0106 soundcard.");
163 module_param_array(enable
, bool, NULL
, 0444);
164 MODULE_PARM_DESC(enable
, "Enable the CA0106 soundcard.");
165 module_param_array(subsystem
, uint
, NULL
, 0444);
166 MODULE_PARM_DESC(subsystem
, "Force card subsystem model.");
170 static struct snd_ca0106_details ca0106_chip_details
[] = {
171 /* AudigyLS[SB0310] */
172 { .serial
= 0x10021102,
173 .name
= "AudigyLS [SB0310]",
175 /* Unknown AudigyLS that also says SB0310 on it */
176 { .serial
= 0x10051102,
177 .name
= "AudigyLS [SB0310b]",
179 /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
180 { .serial
= 0x10061102,
181 .name
= "Live! 7.1 24bit [SB0410]",
184 /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
185 { .serial
= 0x10071102,
186 .name
= "Live! 7.1 24bit [SB0413]",
189 /* New Audigy SE. Has a different DAC. */
195 { .serial
= 0x100a1102,
196 .name
= "Audigy SE [SB0570]",
200 /* New Audigy LS. Has a different DAC. */
206 { .serial
= 0x10111102,
207 .name
= "Audigy SE OEM [SB0570a]",
211 /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
217 { .serial
= 0x10091462,
218 .name
= "MSI K8N Diamond MB [SB0438]",
221 /* Shuttle XPC SD31P which has an onboard Creative Labs
222 * Sound Blaster Live! 24-bit EAX
223 * high-definition 7.1 audio processor".
224 * Added using info from andrewvegan in alsa bug #1298
226 { .serial
= 0x30381297,
227 .name
= "Shuttle XPC SD31P [SD31P]",
230 /* Shuttle XPC SD11G5 which has an onboard Creative Labs
231 * Sound Blaster Live! 24-bit EAX
232 * high-definition 7.1 audio processor".
233 * Fixes ALSA bug#1600
235 { .serial
= 0x30411297,
236 .name
= "Shuttle XPC SD11G5 [SD11G5]",
240 .name
= "AudigyLS [Unknown]" }
243 /* hardware definition */
244 static struct snd_pcm_hardware snd_ca0106_playback_hw
= {
245 .info
= (SNDRV_PCM_INFO_MMAP
|
246 SNDRV_PCM_INFO_INTERLEAVED
|
247 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
248 SNDRV_PCM_INFO_MMAP_VALID
),
249 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S32_LE
,
250 .rates
= (SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_96000
|
251 SNDRV_PCM_RATE_192000
),
254 .channels_min
= 2, //1,
255 .channels_max
= 2, //6,
256 .buffer_bytes_max
= ((65536 - 64) * 8),
257 .period_bytes_min
= 64,
258 .period_bytes_max
= (65536 - 64),
264 static struct snd_pcm_hardware snd_ca0106_capture_hw
= {
265 .info
= (SNDRV_PCM_INFO_MMAP
|
266 SNDRV_PCM_INFO_INTERLEAVED
|
267 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
268 SNDRV_PCM_INFO_MMAP_VALID
),
269 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S32_LE
,
270 .rates
= (SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
|
271 SNDRV_PCM_RATE_96000
| SNDRV_PCM_RATE_192000
),
276 .buffer_bytes_max
= ((65536 - 64) * 8),
277 .period_bytes_min
= 64,
278 .period_bytes_max
= (65536 - 64),
284 unsigned int snd_ca0106_ptr_read(struct snd_ca0106
* emu
,
289 unsigned int regptr
, val
;
291 regptr
= (reg
<< 16) | chn
;
293 spin_lock_irqsave(&emu
->emu_lock
, flags
);
294 outl(regptr
, emu
->port
+ PTR
);
295 val
= inl(emu
->port
+ DATA
);
296 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
300 void snd_ca0106_ptr_write(struct snd_ca0106
*emu
,
308 regptr
= (reg
<< 16) | chn
;
310 spin_lock_irqsave(&emu
->emu_lock
, flags
);
311 outl(regptr
, emu
->port
+ PTR
);
312 outl(data
, emu
->port
+ DATA
);
313 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
316 int snd_ca0106_spi_write(struct snd_ca0106
* emu
,
319 unsigned int reset
, set
;
320 unsigned int reg
, tmp
;
323 if (data
> 0xffff) /* Only 16bit values allowed */
325 tmp
= snd_ca0106_ptr_read(emu
, reg
, 0);
326 reset
= (tmp
& ~0x3ffff) | 0x20000; /* Set xxx20000 */
327 set
= reset
| 0x10000; /* Set xxx1xxxx */
328 snd_ca0106_ptr_write(emu
, reg
, 0, reset
| data
);
329 tmp
= snd_ca0106_ptr_read(emu
, reg
, 0); /* write post */
330 snd_ca0106_ptr_write(emu
, reg
, 0, set
| data
);
332 /* Wait for status bit to return to 0 */
333 for (n
= 0; n
< 100; n
++) {
335 tmp
= snd_ca0106_ptr_read(emu
, reg
, 0);
336 if (!(tmp
& 0x10000)) {
341 if (result
) /* Timed out */
343 snd_ca0106_ptr_write(emu
, reg
, 0, reset
| data
);
344 tmp
= snd_ca0106_ptr_read(emu
, reg
, 0); /* Write post */
348 /* The ADC does not support i2c read, so only write is implemented */
349 int snd_ca0106_i2c_write(struct snd_ca0106
*emu
,
357 if ((reg
> 0x7f) || (value
> 0x1ff)) {
358 snd_printk(KERN_ERR
"i2c_write: invalid values.\n");
362 tmp
= reg
<< 25 | value
<< 16;
363 // snd_printk("I2C-write:reg=0x%x, value=0x%x\n", reg, value);
364 /* Not sure what this I2C channel controls. */
365 /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
367 /* This controls the I2C connected to the WM8775 ADC Codec */
368 snd_ca0106_ptr_write(emu
, I2C_D1
, 0, tmp
);
370 for (retry
= 0; retry
< 10; retry
++) {
371 /* Send the data to i2c */
372 //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
373 //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
375 tmp
= tmp
| (I2C_A_ADC_LAST
|I2C_A_ADC_START
|I2C_A_ADC_ADD
);
376 snd_ca0106_ptr_write(emu
, I2C_A
, 0, tmp
);
378 /* Wait till the transaction ends */
380 status
= snd_ca0106_ptr_read(emu
, I2C_A
, 0);
381 //snd_printk("I2C:status=0x%x\n", status);
383 if ((status
& I2C_A_ADC_START
) == 0)
389 //Read back and see if the transaction is successful
390 if ((status
& I2C_A_ADC_ABORT
) == 0)
395 snd_printk(KERN_ERR
"Writing to ADC failed!\n");
403 static void snd_ca0106_intr_enable(struct snd_ca0106
*emu
, unsigned int intrenb
)
408 spin_lock_irqsave(&emu
->emu_lock
, flags
);
409 enable
= inl(emu
->port
+ INTE
) | intrenb
;
410 outl(enable
, emu
->port
+ INTE
);
411 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
414 static void snd_ca0106_intr_disable(struct snd_ca0106
*emu
, unsigned int intrenb
)
419 spin_lock_irqsave(&emu
->emu_lock
, flags
);
420 enable
= inl(emu
->port
+ INTE
) & ~intrenb
;
421 outl(enable
, emu
->port
+ INTE
);
422 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
426 static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime
*runtime
)
428 kfree(runtime
->private_data
);
431 /* open_playback callback */
432 static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream
*substream
,
435 struct snd_ca0106
*chip
= snd_pcm_substream_chip(substream
);
436 struct snd_ca0106_channel
*channel
= &(chip
->playback_channels
[channel_id
]);
437 struct snd_ca0106_pcm
*epcm
;
438 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
441 epcm
= kzalloc(sizeof(*epcm
), GFP_KERNEL
);
446 epcm
->substream
= substream
;
447 epcm
->channel_id
=channel_id
;
449 runtime
->private_data
= epcm
;
450 runtime
->private_free
= snd_ca0106_pcm_free_substream
;
452 runtime
->hw
= snd_ca0106_playback_hw
;
455 channel
->number
= channel_id
;
458 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
459 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
460 channel
->epcm
= epcm
;
461 if ((err
= snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
)) < 0)
463 if ((err
= snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 64)) < 0)
469 static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream
*substream
)
471 struct snd_ca0106
*chip
= snd_pcm_substream_chip(substream
);
472 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
473 struct snd_ca0106_pcm
*epcm
= runtime
->private_data
;
474 chip
->playback_channels
[epcm
->channel_id
].use
= 0;
475 /* FIXME: maybe zero others */
479 static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream
*substream
)
481 return snd_ca0106_pcm_open_playback_channel(substream
, PCM_FRONT_CHANNEL
);
484 static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream
*substream
)
486 return snd_ca0106_pcm_open_playback_channel(substream
, PCM_CENTER_LFE_CHANNEL
);
489 static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream
*substream
)
491 return snd_ca0106_pcm_open_playback_channel(substream
, PCM_UNKNOWN_CHANNEL
);
494 static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream
*substream
)
496 return snd_ca0106_pcm_open_playback_channel(substream
, PCM_REAR_CHANNEL
);
499 /* open_capture callback */
500 static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream
*substream
,
503 struct snd_ca0106
*chip
= snd_pcm_substream_chip(substream
);
504 struct snd_ca0106_channel
*channel
= &(chip
->capture_channels
[channel_id
]);
505 struct snd_ca0106_pcm
*epcm
;
506 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
509 epcm
= kzalloc(sizeof(*epcm
), GFP_KERNEL
);
511 snd_printk(KERN_ERR
"open_capture_channel: failed epcm alloc\n");
515 epcm
->substream
= substream
;
516 epcm
->channel_id
=channel_id
;
518 runtime
->private_data
= epcm
;
519 runtime
->private_free
= snd_ca0106_pcm_free_substream
;
521 runtime
->hw
= snd_ca0106_capture_hw
;
524 channel
->number
= channel_id
;
527 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
528 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
529 channel
->epcm
= epcm
;
530 if ((err
= snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
)) < 0)
532 //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
533 if ((err
= snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 64)) < 0)
539 static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream
*substream
)
541 struct snd_ca0106
*chip
= snd_pcm_substream_chip(substream
);
542 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
543 struct snd_ca0106_pcm
*epcm
= runtime
->private_data
;
544 chip
->capture_channels
[epcm
->channel_id
].use
= 0;
545 /* FIXME: maybe zero others */
549 static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream
*substream
)
551 return snd_ca0106_pcm_open_capture_channel(substream
, 0);
554 static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream
*substream
)
556 return snd_ca0106_pcm_open_capture_channel(substream
, 1);
559 static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream
*substream
)
561 return snd_ca0106_pcm_open_capture_channel(substream
, 2);
564 static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream
*substream
)
566 return snd_ca0106_pcm_open_capture_channel(substream
, 3);
569 /* hw_params callback */
570 static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream
*substream
,
571 struct snd_pcm_hw_params
*hw_params
)
573 return snd_pcm_lib_malloc_pages(substream
,
574 params_buffer_bytes(hw_params
));
577 /* hw_free callback */
578 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream
*substream
)
580 return snd_pcm_lib_free_pages(substream
);
583 /* hw_params callback */
584 static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream
*substream
,
585 struct snd_pcm_hw_params
*hw_params
)
587 return snd_pcm_lib_malloc_pages(substream
,
588 params_buffer_bytes(hw_params
));
591 /* hw_free callback */
592 static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream
*substream
)
594 return snd_pcm_lib_free_pages(substream
);
597 /* prepare playback callback */
598 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream
*substream
)
600 struct snd_ca0106
*emu
= snd_pcm_substream_chip(substream
);
601 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
602 struct snd_ca0106_pcm
*epcm
= runtime
->private_data
;
603 int channel
= epcm
->channel_id
;
604 u32
*table_base
= (u32
*)(emu
->buffer
.area
+(8*16*channel
));
605 u32 period_size_bytes
= frames_to_bytes(runtime
, runtime
->period_size
);
606 u32 hcfg_mask
= HCFG_PLAYBACK_S32_LE
;
607 u32 hcfg_set
= 0x00000000;
609 u32 reg40_mask
= 0x30000 << (channel
<<1);
612 /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
613 u32 reg71_mask
= 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
618 //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));
619 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
620 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
621 /* Rate can be set per channel. */
622 /* reg40 control host to fifo */
623 /* reg71 controls DAC rate. */
624 switch (runtime
->rate
) {
626 reg40_set
= 0x10000 << (channel
<<1);
627 reg71_set
= 0x01010000;
634 reg40_set
= 0x20000 << (channel
<<1);
635 reg71_set
= 0x02020000;
638 reg40_set
= 0x30000 << (channel
<<1);
639 reg71_set
= 0x03030000;
646 /* Format is a global setting */
647 /* FIXME: Only let the first channel accessed set this. */
648 switch (runtime
->format
) {
649 case SNDRV_PCM_FORMAT_S16_LE
:
652 case SNDRV_PCM_FORMAT_S32_LE
:
653 hcfg_set
= HCFG_PLAYBACK_S32_LE
;
659 hcfg
= inl(emu
->port
+ HCFG
) ;
660 hcfg
= (hcfg
& ~hcfg_mask
) | hcfg_set
;
661 outl(hcfg
, emu
->port
+ HCFG
);
662 reg40
= snd_ca0106_ptr_read(emu
, 0x40, 0);
663 reg40
= (reg40
& ~reg40_mask
) | reg40_set
;
664 snd_ca0106_ptr_write(emu
, 0x40, 0, reg40
);
665 reg71
= snd_ca0106_ptr_read(emu
, 0x71, 0);
666 reg71
= (reg71
& ~reg71_mask
) | reg71_set
;
667 snd_ca0106_ptr_write(emu
, 0x71, 0, reg71
);
669 /* FIXME: Check emu->buffer.size before actually writing to it. */
670 for(i
=0; i
< runtime
->periods
; i
++) {
671 table_base
[i
*2] = runtime
->dma_addr
+ (i
* period_size_bytes
);
672 table_base
[i
*2+1] = period_size_bytes
<< 16;
675 snd_ca0106_ptr_write(emu
, PLAYBACK_LIST_ADDR
, channel
, emu
->buffer
.addr
+(8*16*channel
));
676 snd_ca0106_ptr_write(emu
, PLAYBACK_LIST_SIZE
, channel
, (runtime
->periods
- 1) << 19);
677 snd_ca0106_ptr_write(emu
, PLAYBACK_LIST_PTR
, channel
, 0);
678 snd_ca0106_ptr_write(emu
, PLAYBACK_DMA_ADDR
, channel
, runtime
->dma_addr
);
679 snd_ca0106_ptr_write(emu
, PLAYBACK_PERIOD_SIZE
, channel
, frames_to_bytes(runtime
, runtime
->period_size
)<<16); // buffer size in bytes
680 /* FIXME test what 0 bytes does. */
681 snd_ca0106_ptr_write(emu
, PLAYBACK_PERIOD_SIZE
, channel
, 0); // buffer size in bytes
682 snd_ca0106_ptr_write(emu
, PLAYBACK_POINTER
, channel
, 0);
683 snd_ca0106_ptr_write(emu
, 0x07, channel
, 0x0);
684 snd_ca0106_ptr_write(emu
, 0x08, channel
, 0);
685 snd_ca0106_ptr_write(emu
, PLAYBACK_MUTE
, 0x0, 0x0); /* Unmute output */
687 snd_ca0106_ptr_write(emu
, SPCS0
, 0,
688 SPCS_CLKACCY_1000PPM
| SPCS_SAMPLERATE_48
|
689 SPCS_CHANNELNUM_LEFT
| SPCS_SOURCENUM_UNSPEC
|
690 SPCS_GENERATIONSTATUS
| 0x00001200 |
691 0x00000000 | SPCS_EMPHASIS_NONE
| SPCS_COPYRIGHT
);
698 /* prepare capture callback */
699 static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream
*substream
)
701 struct snd_ca0106
*emu
= snd_pcm_substream_chip(substream
);
702 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
703 struct snd_ca0106_pcm
*epcm
= runtime
->private_data
;
704 int channel
= epcm
->channel_id
;
705 u32 hcfg_mask
= HCFG_CAPTURE_S32_LE
;
706 u32 hcfg_set
= 0x00000000;
708 u32 over_sampling
=0x2;
709 u32 reg71_mask
= 0x0000c000 ; /* Global. Set ADC rate. */
713 //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));
714 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
715 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
716 /* reg71 controls ADC rate. */
717 switch (runtime
->rate
) {
719 reg71_set
= 0x00004000;
725 reg71_set
= 0x00008000;
729 reg71_set
= 0x0000c000;
736 /* Format is a global setting */
737 /* FIXME: Only let the first channel accessed set this. */
738 switch (runtime
->format
) {
739 case SNDRV_PCM_FORMAT_S16_LE
:
742 case SNDRV_PCM_FORMAT_S32_LE
:
743 hcfg_set
= HCFG_CAPTURE_S32_LE
;
749 hcfg
= inl(emu
->port
+ HCFG
) ;
750 hcfg
= (hcfg
& ~hcfg_mask
) | hcfg_set
;
751 outl(hcfg
, emu
->port
+ HCFG
);
752 reg71
= snd_ca0106_ptr_read(emu
, 0x71, 0);
753 reg71
= (reg71
& ~reg71_mask
) | reg71_set
;
754 snd_ca0106_ptr_write(emu
, 0x71, 0, reg71
);
755 if (emu
->details
->i2c_adc
== 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
756 snd_ca0106_i2c_write(emu
, ADC_MASTER
, over_sampling
); /* Adjust the over sampler to better suit the capture rate. */
760 //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));
761 snd_ca0106_ptr_write(emu
, 0x13, channel
, 0);
762 snd_ca0106_ptr_write(emu
, CAPTURE_DMA_ADDR
, channel
, runtime
->dma_addr
);
763 snd_ca0106_ptr_write(emu
, CAPTURE_BUFFER_SIZE
, channel
, frames_to_bytes(runtime
, runtime
->buffer_size
)<<16); // buffer size in bytes
764 snd_ca0106_ptr_write(emu
, CAPTURE_POINTER
, channel
, 0);
769 /* trigger_playback callback */
770 static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream
*substream
,
773 struct snd_ca0106
*emu
= snd_pcm_substream_chip(substream
);
774 struct snd_pcm_runtime
*runtime
;
775 struct snd_ca0106_pcm
*epcm
;
778 struct list_head
*pos
;
779 struct snd_pcm_substream
*s
;
785 case SNDRV_PCM_TRIGGER_START
:
788 case SNDRV_PCM_TRIGGER_STOP
:
793 snd_pcm_group_for_each(pos
, substream
) {
794 s
= snd_pcm_group_substream_entry(pos
);
795 runtime
= s
->runtime
;
796 epcm
= runtime
->private_data
;
797 channel
= epcm
->channel_id
;
798 //snd_printk("channel=%d\n",channel);
799 epcm
->running
= running
;
800 basic
|= (0x1<<channel
);
801 extended
|= (0x10<<channel
);
802 snd_pcm_trigger_done(s
, substream
);
804 //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
807 case SNDRV_PCM_TRIGGER_START
:
808 snd_ca0106_ptr_write(emu
, EXTENDED_INT_MASK
, 0, snd_ca0106_ptr_read(emu
, EXTENDED_INT_MASK
, 0) | (extended
));
809 snd_ca0106_ptr_write(emu
, BASIC_INTERRUPT
, 0, snd_ca0106_ptr_read(emu
, BASIC_INTERRUPT
, 0)|(basic
));
811 case SNDRV_PCM_TRIGGER_STOP
:
812 snd_ca0106_ptr_write(emu
, BASIC_INTERRUPT
, 0, snd_ca0106_ptr_read(emu
, BASIC_INTERRUPT
, 0) & ~(basic
));
813 snd_ca0106_ptr_write(emu
, EXTENDED_INT_MASK
, 0, snd_ca0106_ptr_read(emu
, EXTENDED_INT_MASK
, 0) & ~(extended
));
822 /* trigger_capture callback */
823 static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream
*substream
,
826 struct snd_ca0106
*emu
= snd_pcm_substream_chip(substream
);
827 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
828 struct snd_ca0106_pcm
*epcm
= runtime
->private_data
;
829 int channel
= epcm
->channel_id
;
833 case SNDRV_PCM_TRIGGER_START
:
834 snd_ca0106_ptr_write(emu
, EXTENDED_INT_MASK
, 0, snd_ca0106_ptr_read(emu
, EXTENDED_INT_MASK
, 0) | (0x110000<<channel
));
835 snd_ca0106_ptr_write(emu
, BASIC_INTERRUPT
, 0, snd_ca0106_ptr_read(emu
, BASIC_INTERRUPT
, 0)|(0x100<<channel
));
838 case SNDRV_PCM_TRIGGER_STOP
:
839 snd_ca0106_ptr_write(emu
, BASIC_INTERRUPT
, 0, snd_ca0106_ptr_read(emu
, BASIC_INTERRUPT
, 0) & ~(0x100<<channel
));
840 snd_ca0106_ptr_write(emu
, EXTENDED_INT_MASK
, 0, snd_ca0106_ptr_read(emu
, EXTENDED_INT_MASK
, 0) & ~(0x110000<<channel
));
850 /* pointer_playback callback */
851 static snd_pcm_uframes_t
852 snd_ca0106_pcm_pointer_playback(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 snd_pcm_uframes_t ptr
, ptr1
, ptr2
,ptr3
,ptr4
= 0;
858 int channel
= epcm
->channel_id
;
863 ptr3
= snd_ca0106_ptr_read(emu
, PLAYBACK_LIST_PTR
, channel
);
864 ptr1
= snd_ca0106_ptr_read(emu
, PLAYBACK_POINTER
, channel
);
865 ptr4
= snd_ca0106_ptr_read(emu
, PLAYBACK_LIST_PTR
, channel
);
866 if (ptr3
!= ptr4
) ptr1
= snd_ca0106_ptr_read(emu
, PLAYBACK_POINTER
, channel
);
867 ptr2
= bytes_to_frames(runtime
, ptr1
);
868 ptr2
+= (ptr4
>> 3) * runtime
->period_size
;
870 if (ptr
>= runtime
->buffer_size
)
871 ptr
-= runtime
->buffer_size
;
872 //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);
877 /* pointer_capture callback */
878 static snd_pcm_uframes_t
879 snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream
*substream
)
881 struct snd_ca0106
*emu
= snd_pcm_substream_chip(substream
);
882 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
883 struct snd_ca0106_pcm
*epcm
= runtime
->private_data
;
884 snd_pcm_uframes_t ptr
, ptr1
, ptr2
= 0;
885 int channel
= channel
=epcm
->channel_id
;
890 ptr1
= snd_ca0106_ptr_read(emu
, CAPTURE_POINTER
, channel
);
891 ptr2
= bytes_to_frames(runtime
, ptr1
);
893 if (ptr
>= runtime
->buffer_size
)
894 ptr
-= runtime
->buffer_size
;
895 //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);
901 static struct snd_pcm_ops snd_ca0106_playback_front_ops
= {
902 .open
= snd_ca0106_pcm_open_playback_front
,
903 .close
= snd_ca0106_pcm_close_playback
,
904 .ioctl
= snd_pcm_lib_ioctl
,
905 .hw_params
= snd_ca0106_pcm_hw_params_playback
,
906 .hw_free
= snd_ca0106_pcm_hw_free_playback
,
907 .prepare
= snd_ca0106_pcm_prepare_playback
,
908 .trigger
= snd_ca0106_pcm_trigger_playback
,
909 .pointer
= snd_ca0106_pcm_pointer_playback
,
912 static struct snd_pcm_ops snd_ca0106_capture_0_ops
= {
913 .open
= snd_ca0106_pcm_open_0_capture
,
914 .close
= snd_ca0106_pcm_close_capture
,
915 .ioctl
= snd_pcm_lib_ioctl
,
916 .hw_params
= snd_ca0106_pcm_hw_params_capture
,
917 .hw_free
= snd_ca0106_pcm_hw_free_capture
,
918 .prepare
= snd_ca0106_pcm_prepare_capture
,
919 .trigger
= snd_ca0106_pcm_trigger_capture
,
920 .pointer
= snd_ca0106_pcm_pointer_capture
,
923 static struct snd_pcm_ops snd_ca0106_capture_1_ops
= {
924 .open
= snd_ca0106_pcm_open_1_capture
,
925 .close
= snd_ca0106_pcm_close_capture
,
926 .ioctl
= snd_pcm_lib_ioctl
,
927 .hw_params
= snd_ca0106_pcm_hw_params_capture
,
928 .hw_free
= snd_ca0106_pcm_hw_free_capture
,
929 .prepare
= snd_ca0106_pcm_prepare_capture
,
930 .trigger
= snd_ca0106_pcm_trigger_capture
,
931 .pointer
= snd_ca0106_pcm_pointer_capture
,
934 static struct snd_pcm_ops snd_ca0106_capture_2_ops
= {
935 .open
= snd_ca0106_pcm_open_2_capture
,
936 .close
= snd_ca0106_pcm_close_capture
,
937 .ioctl
= snd_pcm_lib_ioctl
,
938 .hw_params
= snd_ca0106_pcm_hw_params_capture
,
939 .hw_free
= snd_ca0106_pcm_hw_free_capture
,
940 .prepare
= snd_ca0106_pcm_prepare_capture
,
941 .trigger
= snd_ca0106_pcm_trigger_capture
,
942 .pointer
= snd_ca0106_pcm_pointer_capture
,
945 static struct snd_pcm_ops snd_ca0106_capture_3_ops
= {
946 .open
= snd_ca0106_pcm_open_3_capture
,
947 .close
= snd_ca0106_pcm_close_capture
,
948 .ioctl
= snd_pcm_lib_ioctl
,
949 .hw_params
= snd_ca0106_pcm_hw_params_capture
,
950 .hw_free
= snd_ca0106_pcm_hw_free_capture
,
951 .prepare
= snd_ca0106_pcm_prepare_capture
,
952 .trigger
= snd_ca0106_pcm_trigger_capture
,
953 .pointer
= snd_ca0106_pcm_pointer_capture
,
956 static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops
= {
957 .open
= snd_ca0106_pcm_open_playback_center_lfe
,
958 .close
= snd_ca0106_pcm_close_playback
,
959 .ioctl
= snd_pcm_lib_ioctl
,
960 .hw_params
= snd_ca0106_pcm_hw_params_playback
,
961 .hw_free
= snd_ca0106_pcm_hw_free_playback
,
962 .prepare
= snd_ca0106_pcm_prepare_playback
,
963 .trigger
= snd_ca0106_pcm_trigger_playback
,
964 .pointer
= snd_ca0106_pcm_pointer_playback
,
967 static struct snd_pcm_ops snd_ca0106_playback_unknown_ops
= {
968 .open
= snd_ca0106_pcm_open_playback_unknown
,
969 .close
= snd_ca0106_pcm_close_playback
,
970 .ioctl
= snd_pcm_lib_ioctl
,
971 .hw_params
= snd_ca0106_pcm_hw_params_playback
,
972 .hw_free
= snd_ca0106_pcm_hw_free_playback
,
973 .prepare
= snd_ca0106_pcm_prepare_playback
,
974 .trigger
= snd_ca0106_pcm_trigger_playback
,
975 .pointer
= snd_ca0106_pcm_pointer_playback
,
978 static struct snd_pcm_ops snd_ca0106_playback_rear_ops
= {
979 .open
= snd_ca0106_pcm_open_playback_rear
,
980 .close
= snd_ca0106_pcm_close_playback
,
981 .ioctl
= snd_pcm_lib_ioctl
,
982 .hw_params
= snd_ca0106_pcm_hw_params_playback
,
983 .hw_free
= snd_ca0106_pcm_hw_free_playback
,
984 .prepare
= snd_ca0106_pcm_prepare_playback
,
985 .trigger
= snd_ca0106_pcm_trigger_playback
,
986 .pointer
= snd_ca0106_pcm_pointer_playback
,
990 static unsigned short snd_ca0106_ac97_read(struct snd_ac97
*ac97
,
993 struct snd_ca0106
*emu
= ac97
->private_data
;
997 spin_lock_irqsave(&emu
->emu_lock
, flags
);
998 outb(reg
, emu
->port
+ AC97ADDRESS
);
999 val
= inw(emu
->port
+ AC97DATA
);
1000 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
1004 static void snd_ca0106_ac97_write(struct snd_ac97
*ac97
,
1005 unsigned short reg
, unsigned short val
)
1007 struct snd_ca0106
*emu
= ac97
->private_data
;
1008 unsigned long flags
;
1010 spin_lock_irqsave(&emu
->emu_lock
, flags
);
1011 outb(reg
, emu
->port
+ AC97ADDRESS
);
1012 outw(val
, emu
->port
+ AC97DATA
);
1013 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
1016 static int snd_ca0106_ac97(struct snd_ca0106
*chip
)
1018 struct snd_ac97_bus
*pbus
;
1019 struct snd_ac97_template ac97
;
1021 static struct snd_ac97_bus_ops ops
= {
1022 .write
= snd_ca0106_ac97_write
,
1023 .read
= snd_ca0106_ac97_read
,
1026 if ((err
= snd_ac97_bus(chip
->card
, 0, &ops
, NULL
, &pbus
)) < 0)
1028 pbus
->no_vra
= 1; /* we don't need VRA */
1030 memset(&ac97
, 0, sizeof(ac97
));
1031 ac97
.private_data
= chip
;
1032 ac97
.scaps
= AC97_SCAP_NO_SPDIF
;
1033 return snd_ac97_mixer(pbus
, &ac97
, &chip
->ac97
);
1036 static int snd_ca0106_free(struct snd_ca0106
*chip
)
1038 if (chip
->res_port
!= NULL
) { /* avoid access to already used hardware */
1039 // disable interrupts
1040 snd_ca0106_ptr_write(chip
, BASIC_INTERRUPT
, 0, 0);
1041 outl(0, chip
->port
+ INTE
);
1042 snd_ca0106_ptr_write(chip
, EXTENDED_INT_MASK
, 0, 0);
1045 //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
1046 outl(0, chip
->port
+ HCFG
);
1047 /* FIXME: We need to stop and DMA transfers here.
1048 * But as I am not sure how yet, we cannot from the dma pages.
1049 * So we can fix: snd-malloc: Memory leak? pages not freed = 8
1054 if (chip
->buffer
.area
)
1055 snd_dma_free_pages(&chip
->buffer
);
1058 // release the i/o port
1059 release_and_free_resource(chip
->res_port
);
1063 free_irq(chip
->irq
, chip
);
1064 pci_disable_device(chip
->pci
);
1069 static int snd_ca0106_dev_free(struct snd_device
*device
)
1071 struct snd_ca0106
*chip
= device
->device_data
;
1072 return snd_ca0106_free(chip
);
1075 static irqreturn_t
snd_ca0106_interrupt(int irq
, void *dev_id
)
1077 unsigned int status
;
1079 struct snd_ca0106
*chip
= dev_id
;
1082 unsigned int stat76
;
1083 struct snd_ca0106_channel
*pchannel
;
1085 status
= inl(chip
->port
+ IPR
);
1089 stat76
= snd_ca0106_ptr_read(chip
, EXTENDED_INT
, 0);
1090 //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
1091 //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
1092 mask
= 0x11; /* 0x1 for one half, 0x10 for the other half period. */
1093 for(i
= 0; i
< 4; i
++) {
1094 pchannel
= &(chip
->playback_channels
[i
]);
1095 if (stat76
& mask
) {
1096 /* FIXME: Select the correct substream for period elapsed */
1098 snd_pcm_period_elapsed(pchannel
->epcm
->substream
);
1099 //printk(KERN_INFO "interrupt [%d] used\n", i);
1102 //printk(KERN_INFO "channel=%p\n",pchannel);
1103 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1106 mask
= 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
1107 for(i
= 0; i
< 4; i
++) {
1108 pchannel
= &(chip
->capture_channels
[i
]);
1109 if (stat76
& mask
) {
1110 /* FIXME: Select the correct substream for period elapsed */
1112 snd_pcm_period_elapsed(pchannel
->epcm
->substream
);
1113 //printk(KERN_INFO "interrupt [%d] used\n", i);
1116 //printk(KERN_INFO "channel=%p\n",pchannel);
1117 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1121 snd_ca0106_ptr_write(chip
, EXTENDED_INT
, 0, stat76
);
1123 if (chip
->midi
.dev_id
&&
1124 (status
& (chip
->midi
.ipr_tx
|chip
->midi
.ipr_rx
))) {
1125 if (chip
->midi
.interrupt
)
1126 chip
->midi
.interrupt(&chip
->midi
, status
);
1128 chip
->midi
.interrupt_disable(&chip
->midi
, chip
->midi
.tx_enable
| chip
->midi
.rx_enable
);
1131 // acknowledge the interrupt if necessary
1132 outl(status
, chip
->port
+IPR
);
1137 static int __devinit
snd_ca0106_pcm(struct snd_ca0106
*emu
, int device
, struct snd_pcm
**rpcm
)
1139 struct snd_pcm
*pcm
;
1140 struct snd_pcm_substream
*substream
;
1145 if ((err
= snd_pcm_new(emu
->card
, "ca0106", device
, 1, 1, &pcm
)) < 0)
1148 pcm
->private_data
= emu
;
1152 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_ca0106_playback_front_ops
);
1153 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_ca0106_capture_0_ops
);
1156 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_ca0106_playback_rear_ops
);
1157 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_ca0106_capture_1_ops
);
1160 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_ca0106_playback_center_lfe_ops
);
1161 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_ca0106_capture_2_ops
);
1164 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_ca0106_playback_unknown_ops
);
1165 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_ca0106_capture_3_ops
);
1169 pcm
->info_flags
= 0;
1170 pcm
->dev_subclass
= SNDRV_PCM_SUBCLASS_GENERIC_MIX
;
1171 strcpy(pcm
->name
, "CA0106");
1174 for(substream
= pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
;
1176 substream
= substream
->next
) {
1177 if ((err
= snd_pcm_lib_preallocate_pages(substream
,
1179 snd_dma_pci_data(emu
->pci
),
1180 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
1184 for (substream
= pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
;
1186 substream
= substream
->next
) {
1187 if ((err
= snd_pcm_lib_preallocate_pages(substream
,
1189 snd_dma_pci_data(emu
->pci
),
1190 64*1024, 64*1024)) < 0)
1200 static unsigned int spi_dac_init
[] = {
1205 0x0620, /* Set 24 bit. Was 0x0600 */
1224 static unsigned int i2c_adc_init
[][2] = {
1225 { 0x17, 0x00 }, /* Reset */
1226 { 0x07, 0x00 }, /* Timeout */
1227 { 0x0b, 0x22 }, /* Interface control */
1228 { 0x0c, 0x22 }, /* Master mode control */
1229 { 0x0d, 0x08 }, /* Powerdown control */
1230 { 0x0e, 0xcf }, /* Attenuation Left 0x01 = -103dB, 0xff = 24dB */
1231 { 0x0f, 0xcf }, /* Attenuation Right 0.5dB steps */
1232 { 0x10, 0x7b }, /* ALC Control 1 */
1233 { 0x11, 0x00 }, /* ALC Control 2 */
1234 { 0x12, 0x32 }, /* ALC Control 3 */
1235 { 0x13, 0x00 }, /* Noise gate control */
1236 { 0x14, 0xa6 }, /* Limiter control */
1237 { 0x15, ADC_MUX_LINEIN
}, /* ADC Mixer control */
1240 static int __devinit
snd_ca0106_create(int dev
, struct snd_card
*card
,
1241 struct pci_dev
*pci
,
1242 struct snd_ca0106
**rchip
)
1244 struct snd_ca0106
*chip
;
1245 struct snd_ca0106_details
*c
;
1248 static struct snd_device_ops ops
= {
1249 .dev_free
= snd_ca0106_dev_free
,
1254 if ((err
= pci_enable_device(pci
)) < 0)
1256 if (pci_set_dma_mask(pci
, DMA_32BIT_MASK
) < 0 ||
1257 pci_set_consistent_dma_mask(pci
, DMA_32BIT_MASK
) < 0) {
1258 printk(KERN_ERR
"error to set 32bit mask DMA\n");
1259 pci_disable_device(pci
);
1263 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1265 pci_disable_device(pci
);
1273 spin_lock_init(&chip
->emu_lock
);
1275 chip
->port
= pci_resource_start(pci
, 0);
1276 if ((chip
->res_port
= request_region(chip
->port
, 0x20,
1277 "snd_ca0106")) == NULL
) {
1278 snd_ca0106_free(chip
);
1279 printk(KERN_ERR
"cannot allocate the port\n");
1283 if (request_irq(pci
->irq
, snd_ca0106_interrupt
,
1284 IRQF_SHARED
, "snd_ca0106", chip
)) {
1285 snd_ca0106_free(chip
);
1286 printk(KERN_ERR
"cannot grab irq\n");
1289 chip
->irq
= pci
->irq
;
1291 /* This stores the periods table. */
1292 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(pci
), 1024, &chip
->buffer
) < 0) {
1293 snd_ca0106_free(chip
);
1297 pci_set_master(pci
);
1298 /* read revision & serial */
1299 pci_read_config_byte(pci
, PCI_REVISION_ID
, &chip
->revision
);
1300 pci_read_config_dword(pci
, PCI_SUBSYSTEM_VENDOR_ID
, &chip
->serial
);
1301 pci_read_config_word(pci
, PCI_SUBSYSTEM_ID
, &chip
->model
);
1303 printk(KERN_INFO
"snd-ca0106: Model %04x Rev %08x Serial %08x\n", chip
->model
,
1304 chip
->revision
, chip
->serial
);
1306 strcpy(card
->driver
, "CA0106");
1307 strcpy(card
->shortname
, "CA0106");
1309 for (c
= ca0106_chip_details
; c
->serial
; c
++) {
1310 if (subsystem
[dev
]) {
1311 if (c
->serial
== subsystem
[dev
])
1313 } else if (c
->serial
== chip
->serial
)
1317 if (subsystem
[dev
]) {
1318 printk(KERN_INFO
"snd-ca0106: Sound card name=%s, subsystem=0x%x. Forced to subsystem=0x%x\n",
1319 c
->name
, chip
->serial
, subsystem
[dev
]);
1322 sprintf(card
->longname
, "%s at 0x%lx irq %i",
1323 c
->name
, chip
->port
, chip
->irq
);
1325 outl(0, chip
->port
+ INTE
);
1328 * Init to 0x02109204 :
1329 * Clock accuracy = 0 (1000ppm)
1330 * Sample Rate = 2 (48kHz)
1331 * Audio Channel = 1 (Left of 2)
1332 * Source Number = 0 (Unspecified)
1333 * Generation Status = 1 (Original for Cat Code 12)
1334 * Cat Code = 12 (Digital Signal Mixer)
1336 * Emphasis = 0 (None)
1337 * CP = 1 (Copyright unasserted)
1338 * AN = 0 (Audio data)
1341 snd_ca0106_ptr_write(chip
, SPCS0
, 0,
1342 chip
->spdif_bits
[0] =
1343 SPCS_CLKACCY_1000PPM
| SPCS_SAMPLERATE_48
|
1344 SPCS_CHANNELNUM_LEFT
| SPCS_SOURCENUM_UNSPEC
|
1345 SPCS_GENERATIONSTATUS
| 0x00001200 |
1346 0x00000000 | SPCS_EMPHASIS_NONE
| SPCS_COPYRIGHT
);
1347 /* Only SPCS1 has been tested */
1348 snd_ca0106_ptr_write(chip
, SPCS1
, 0,
1349 chip
->spdif_bits
[1] =
1350 SPCS_CLKACCY_1000PPM
| SPCS_SAMPLERATE_48
|
1351 SPCS_CHANNELNUM_LEFT
| SPCS_SOURCENUM_UNSPEC
|
1352 SPCS_GENERATIONSTATUS
| 0x00001200 |
1353 0x00000000 | SPCS_EMPHASIS_NONE
| SPCS_COPYRIGHT
);
1354 snd_ca0106_ptr_write(chip
, SPCS2
, 0,
1355 chip
->spdif_bits
[2] =
1356 SPCS_CLKACCY_1000PPM
| SPCS_SAMPLERATE_48
|
1357 SPCS_CHANNELNUM_LEFT
| SPCS_SOURCENUM_UNSPEC
|
1358 SPCS_GENERATIONSTATUS
| 0x00001200 |
1359 0x00000000 | SPCS_EMPHASIS_NONE
| SPCS_COPYRIGHT
);
1360 snd_ca0106_ptr_write(chip
, SPCS3
, 0,
1361 chip
->spdif_bits
[3] =
1362 SPCS_CLKACCY_1000PPM
| SPCS_SAMPLERATE_48
|
1363 SPCS_CHANNELNUM_LEFT
| SPCS_SOURCENUM_UNSPEC
|
1364 SPCS_GENERATIONSTATUS
| 0x00001200 |
1365 0x00000000 | SPCS_EMPHASIS_NONE
| SPCS_COPYRIGHT
);
1367 snd_ca0106_ptr_write(chip
, PLAYBACK_MUTE
, 0, 0x00fc0000);
1368 snd_ca0106_ptr_write(chip
, CAPTURE_MUTE
, 0, 0x00fc0000);
1370 /* Write 0x8000 to AC97_REC_GAIN to mute it. */
1371 outb(AC97_REC_GAIN
, chip
->port
+ AC97ADDRESS
);
1372 outw(0x8000, chip
->port
+ AC97DATA
);
1374 snd_ca0106_ptr_write(chip
, SPCS0
, 0, 0x2108006);
1375 snd_ca0106_ptr_write(chip
, 0x42, 0, 0x2108006);
1376 snd_ca0106_ptr_write(chip
, 0x43, 0, 0x2108006);
1377 snd_ca0106_ptr_write(chip
, 0x44, 0, 0x2108006);
1380 //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
1381 /* Analog or Digital output */
1382 snd_ca0106_ptr_write(chip
, SPDIF_SELECT1
, 0, 0xf);
1383 snd_ca0106_ptr_write(chip
, SPDIF_SELECT2
, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
1384 chip
->spdif_enable
= 0; /* Set digital SPDIF output off */
1385 //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
1386 //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
1388 snd_ca0106_ptr_write(chip
, CAPTURE_CONTROL
, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
1389 snd_ca0106_ptr_write(chip
, CAPTURE_CONTROL
, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
1390 snd_ca0106_ptr_write(chip
, CAPTURE_CONTROL
, 2, 0x30300000); /* SPDIF IN Volume */
1391 snd_ca0106_ptr_write(chip
, CAPTURE_CONTROL
, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
1392 snd_ca0106_ptr_write(chip
, PLAYBACK_ROUTING1
, 0, 0x32765410);
1393 snd_ca0106_ptr_write(chip
, PLAYBACK_ROUTING2
, 0, 0x76767676);
1394 snd_ca0106_ptr_write(chip
, CAPTURE_ROUTING1
, 0, 0x32765410);
1395 snd_ca0106_ptr_write(chip
, CAPTURE_ROUTING2
, 0, 0x76767676);
1396 for(ch
= 0; ch
< 4; ch
++) {
1397 snd_ca0106_ptr_write(chip
, CAPTURE_VOLUME1
, ch
, 0x30303030); /* Only high 16 bits matter */
1398 snd_ca0106_ptr_write(chip
, CAPTURE_VOLUME2
, ch
, 0x30303030);
1399 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
1400 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
1401 snd_ca0106_ptr_write(chip
, PLAYBACK_VOLUME1
, ch
, 0xffffffff); /* Mute */
1402 snd_ca0106_ptr_write(chip
, PLAYBACK_VOLUME2
, ch
, 0xffffffff); /* Mute */
1404 if (chip
->details
->i2c_adc
== 1) {
1405 /* Select MIC, Line in, TAD in, AUX in */
1406 snd_ca0106_ptr_write(chip
, CAPTURE_SOURCE
, 0x0, 0x333300e4);
1407 /* Default to CAPTURE_SOURCE to i2s in */
1408 chip
->capture_source
= 3;
1409 } else if (chip
->details
->ac97
== 1) {
1410 /* Default to AC97 in */
1411 snd_ca0106_ptr_write(chip
, CAPTURE_SOURCE
, 0x0, 0x444400e4);
1412 /* Default to CAPTURE_SOURCE to AC97 in */
1413 chip
->capture_source
= 4;
1415 /* Select MIC, Line in, TAD in, AUX in */
1416 snd_ca0106_ptr_write(chip
, CAPTURE_SOURCE
, 0x0, 0x333300e4);
1417 /* Default to Set CAPTURE_SOURCE to i2s in */
1418 chip
->capture_source
= 3;
1421 if (chip
->details
->gpio_type
== 2) { /* The SB0438 use GPIO differently. */
1422 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1423 outl(0x0, chip
->port
+GPIO
);
1424 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1425 outl(0x005f5301, chip
->port
+GPIO
); /* Analog */
1426 } else if (chip
->details
->gpio_type
== 1) { /* The SB0410 and SB0413 use GPIO differently. */
1427 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1428 outl(0x0, chip
->port
+GPIO
);
1429 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1430 outl(0x005f5301, chip
->port
+GPIO
); /* Analog */
1432 outl(0x0, chip
->port
+GPIO
);
1433 outl(0x005f03a3, chip
->port
+GPIO
); /* Analog */
1434 //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
1436 snd_ca0106_intr_enable(chip
, 0x105); /* Win2000 uses 0x1e0 */
1438 //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1439 //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
1440 //outl(0x00000009, chip->port+HCFG);
1441 outl(HCFG_AC97
| HCFG_AUDIOENABLE
, chip
->port
+HCFG
); /* AC97 2.0, Enable outputs. */
1443 if (chip
->details
->i2c_adc
== 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
1446 size
= ARRAY_SIZE(i2c_adc_init
);
1447 //snd_printk("I2C:array size=0x%x\n", size);
1448 for (n
=0; n
< size
; n
++) {
1449 snd_ca0106_i2c_write(chip
, i2c_adc_init
[n
][0], i2c_adc_init
[n
][1]);
1451 for (n
=0; n
< 4; n
++) {
1452 chip
->i2c_capture_volume
[n
][0]= 0xcf;
1453 chip
->i2c_capture_volume
[n
][1]= 0xcf;
1455 chip
->i2c_capture_source
=2; /* Line in */
1456 //snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
1458 if (chip
->details
->spi_dac
== 1) { /* The SB0570 use SPI to control DAC. */
1461 size
= ARRAY_SIZE(spi_dac_init
);
1462 for (n
=0; n
< size
; n
++)
1463 snd_ca0106_spi_write(chip
, spi_dac_init
[n
]);
1466 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
,
1468 snd_ca0106_free(chip
);
1476 static void ca0106_midi_interrupt_enable(struct snd_ca_midi
*midi
, int intr
)
1478 snd_ca0106_intr_enable((struct snd_ca0106
*)(midi
->dev_id
), intr
);
1481 static void ca0106_midi_interrupt_disable(struct snd_ca_midi
*midi
, int intr
)
1483 snd_ca0106_intr_disable((struct snd_ca0106
*)(midi
->dev_id
), intr
);
1486 static unsigned char ca0106_midi_read(struct snd_ca_midi
*midi
, int idx
)
1488 return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106
*)(midi
->dev_id
),
1489 midi
->port
+ idx
, 0);
1492 static void ca0106_midi_write(struct snd_ca_midi
*midi
, int data
, int idx
)
1494 snd_ca0106_ptr_write((struct snd_ca0106
*)(midi
->dev_id
), midi
->port
+ idx
, 0, data
);
1497 static struct snd_card
*ca0106_dev_id_card(void *dev_id
)
1499 return ((struct snd_ca0106
*)dev_id
)->card
;
1502 static int ca0106_dev_id_port(void *dev_id
)
1504 return ((struct snd_ca0106
*)dev_id
)->port
;
1507 static int __devinit
snd_ca0106_midi(struct snd_ca0106
*chip
, unsigned int channel
)
1509 struct snd_ca_midi
*midi
;
1513 if (channel
== CA0106_MIDI_CHAN_B
) {
1514 name
= "CA0106 MPU-401 (UART) B";
1515 midi
= &chip
->midi2
;
1516 midi
->tx_enable
= INTE_MIDI_TX_B
;
1517 midi
->rx_enable
= INTE_MIDI_RX_B
;
1518 midi
->ipr_tx
= IPR_MIDI_TX_B
;
1519 midi
->ipr_rx
= IPR_MIDI_RX_B
;
1520 midi
->port
= MIDI_UART_B_DATA
;
1522 name
= "CA0106 MPU-401 (UART)";
1524 midi
->tx_enable
= INTE_MIDI_TX_A
;
1525 midi
->rx_enable
= INTE_MIDI_TX_B
;
1526 midi
->ipr_tx
= IPR_MIDI_TX_A
;
1527 midi
->ipr_rx
= IPR_MIDI_RX_A
;
1528 midi
->port
= MIDI_UART_A_DATA
;
1531 midi
->reset
= CA0106_MPU401_RESET
;
1532 midi
->enter_uart
= CA0106_MPU401_ENTER_UART
;
1533 midi
->ack
= CA0106_MPU401_ACK
;
1535 midi
->input_avail
= CA0106_MIDI_INPUT_AVAIL
;
1536 midi
->output_ready
= CA0106_MIDI_OUTPUT_READY
;
1538 midi
->channel
= channel
;
1540 midi
->interrupt_enable
= ca0106_midi_interrupt_enable
;
1541 midi
->interrupt_disable
= ca0106_midi_interrupt_disable
;
1543 midi
->read
= ca0106_midi_read
;
1544 midi
->write
= ca0106_midi_write
;
1546 midi
->get_dev_id_card
= ca0106_dev_id_card
;
1547 midi
->get_dev_id_port
= ca0106_dev_id_port
;
1549 midi
->dev_id
= chip
;
1551 if ((err
= ca_midi_init(chip
, midi
, 0, name
)) < 0)
1558 static int __devinit
snd_ca0106_probe(struct pci_dev
*pci
,
1559 const struct pci_device_id
*pci_id
)
1562 struct snd_card
*card
;
1563 struct snd_ca0106
*chip
;
1566 if (dev
>= SNDRV_CARDS
)
1573 card
= snd_card_new(index
[dev
], id
[dev
], THIS_MODULE
, 0);
1577 if ((err
= snd_ca0106_create(dev
, card
, pci
, &chip
)) < 0) {
1578 snd_card_free(card
);
1582 if ((err
= snd_ca0106_pcm(chip
, 0, NULL
)) < 0) {
1583 snd_card_free(card
);
1586 if ((err
= snd_ca0106_pcm(chip
, 1, NULL
)) < 0) {
1587 snd_card_free(card
);
1590 if ((err
= snd_ca0106_pcm(chip
, 2, NULL
)) < 0) {
1591 snd_card_free(card
);
1594 if ((err
= snd_ca0106_pcm(chip
, 3, NULL
)) < 0) {
1595 snd_card_free(card
);
1598 if (chip
->details
->ac97
== 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
1599 if ((err
= snd_ca0106_ac97(chip
)) < 0) {
1600 snd_card_free(card
);
1604 if ((err
= snd_ca0106_mixer(chip
)) < 0) {
1605 snd_card_free(card
);
1609 snd_printdd("ca0106: probe for MIDI channel A ...");
1610 if ((err
= snd_ca0106_midi(chip
,CA0106_MIDI_CHAN_A
)) < 0) {
1611 snd_card_free(card
);
1612 snd_printdd(" failed, err=0x%x\n",err
);
1615 snd_printdd(" done.\n");
1617 #ifdef CONFIG_PROC_FS
1618 snd_ca0106_proc_init(chip
);
1621 if ((err
= snd_card_register(card
)) < 0) {
1622 snd_card_free(card
);
1626 pci_set_drvdata(pci
, card
);
1631 static void __devexit
snd_ca0106_remove(struct pci_dev
*pci
)
1633 snd_card_free(pci_get_drvdata(pci
));
1634 pci_set_drvdata(pci
, NULL
);
1638 static struct pci_device_id snd_ca0106_ids
[] = {
1639 { 0x1102, 0x0007, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0 }, /* Audigy LS or Live 24bit */
1642 MODULE_DEVICE_TABLE(pci
, snd_ca0106_ids
);
1644 // pci_driver definition
1645 static struct pci_driver driver
= {
1647 .id_table
= snd_ca0106_ids
,
1648 .probe
= snd_ca0106_probe
,
1649 .remove
= __devexit_p(snd_ca0106_remove
),
1652 // initialization of the module
1653 static int __init
alsa_card_ca0106_init(void)
1655 return pci_register_driver(&driver
);
1658 // clean up the module
1659 static void __exit
alsa_card_ca0106_exit(void)
1661 pci_unregister_driver(&driver
);
1664 module_init(alsa_card_ca0106_init
)
1665 module_exit(alsa_card_ca0106_exit
)