[ALSA] snd-ca0106: Fix SPI driver code. Fixes speaker output.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / pci / ca0106 / ca0106_main.c
blob6ed7c0bfa091c3c3790a48cb1e4cf3fe458c4b02
1 /*
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
4 * Version: 0.0.23
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: -
23 * Analog Front
24 * Analog Rear
25 * Analog Center/LFE
26 * SPDIF Front
27 * SPDIF Rear
28 * SPDIF Center/LFE
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
35 * Changelog:
36 * Support interrupts per period.
37 * Removed noise from Center/LFE channel when in Analog mode.
38 * Rename and remove mixer controls.
39 * 0.0.6
40 * Use separate card based DMA buffer for periods table list.
41 * 0.0.7
42 * Change remove and rename ctrls into lists.
43 * 0.0.8
44 * Try to fix capture sources.
45 * 0.0.9
46 * Fix AC3 output.
47 * Enable S32_LE format support.
48 * 0.0.10
49 * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
50 * 0.0.11
51 * Add Model name recognition.
52 * 0.0.12
53 * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
54 * Remove redundent "voice" handling.
55 * 0.0.13
56 * Single trigger call for multi channels.
57 * 0.0.14
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.
62 * 0.0.15
63 * Minor updates.
64 * 0.0.16
65 * Implement 192000 sample rate.
66 * 0.0.17
67 * Add support for SB0410 and SB0413.
68 * 0.0.18
69 * Modified Copyright message.
70 * 0.0.19
71 * Finally fix support for SB Live 24 bit. SB0410 and SB0413.
72 * The output codec needs resetting, otherwise all output is muted.
73 * 0.0.20
74 * Merge "pci_disable_device(pci);" fixes.
75 * 0.0.21
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.)
78 * 0.0.22
79 * Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
80 * 0.0.23
81 * Implement support for Line-in capture on SB Live 24bit.
83 * BUGS:
84 * Some stability problems when unloading the snd-ca0106 kernel module.
85 * --
87 * TODO:
88 * 4 Capture channels, only one implemented so far.
89 * Other capture rates apart from 48khz not implemented.
90 * MIDI
91 * --
92 * GENERAL INFO:
93 * Model: SB0310
94 * P17 Chip: CA0106-DAT
95 * AC97 Codec: STAC 9721
96 * ADC: Philips 1361T (Stereo 24bit)
97 * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
99 * GENERAL INFO:
100 * Model: SB0410
101 * P17 Chip: CA0106-DAT
102 * AC97 Codec: None
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.
108 * GENERAL INFO:
109 * Model: SB0413
110 * P17 Chip: CA0106-DAT
111 * AC97 Codec: None.
112 * ADC: Unknown
113 * DAC: Unknown
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;
158 module_param_array(index, int, NULL, 0444);
159 MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
160 module_param_array(id, charp, NULL, 0444);
161 MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
162 module_param_array(enable, bool, NULL, 0444);
163 MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
165 #include "ca0106.h"
167 static struct snd_ca0106_details ca0106_chip_details[] = {
168 /* AudigyLS[SB0310] */
169 { .serial = 0x10021102,
170 .name = "AudigyLS [SB0310]",
171 .ac97 = 1 } ,
172 /* Unknown AudigyLS that also says SB0310 on it */
173 { .serial = 0x10051102,
174 .name = "AudigyLS [SB0310b]",
175 .ac97 = 1 } ,
176 /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
177 { .serial = 0x10061102,
178 .name = "Live! 7.1 24bit [SB0410]",
179 .gpio_type = 1,
180 .i2c_adc = 1 } ,
181 /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
182 { .serial = 0x10071102,
183 .name = "Live! 7.1 24bit [SB0413]",
184 .gpio_type = 1,
185 .i2c_adc = 1 } ,
186 /* New Audigy SE. Has a different DAC. */
187 /* SB0570:
188 * CTRL:CA0106-DAT
189 * ADC: WM8768GEDS
190 * DAC: WM8775EDS
192 { .serial = 0x100a1102,
193 .name = "Audigy SE [SB0570]",
194 .gpio_type = 1,
195 .i2c_adc = 1,
196 .spi_dac = 1 } ,
197 /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
198 { .serial = 0x10091462,
199 .name = "MSI K8N Diamond MB [SB0438]",
200 .gpio_type = 1,
201 .i2c_adc = 1 } ,
202 /* Shuttle XPC SD31P which has an onboard Creative Labs Sound Blaster Live! 24-bit EAX
203 * high-definition 7.1 audio processor".
204 * Added using info from andrewvegan in alsa bug #1298
206 { .serial = 0x30381297,
207 .name = "Shuttle XPC SD31P [SD31P]",
208 .gpio_type = 1,
209 .i2c_adc = 1 } ,
210 { .serial = 0,
211 .name = "AudigyLS [Unknown]" }
214 /* hardware definition */
215 static struct snd_pcm_hardware snd_ca0106_playback_hw = {
216 .info = (SNDRV_PCM_INFO_MMAP |
217 SNDRV_PCM_INFO_INTERLEAVED |
218 SNDRV_PCM_INFO_BLOCK_TRANSFER |
219 SNDRV_PCM_INFO_MMAP_VALID),
220 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
221 .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
222 SNDRV_PCM_RATE_192000),
223 .rate_min = 48000,
224 .rate_max = 192000,
225 .channels_min = 2, //1,
226 .channels_max = 2, //6,
227 .buffer_bytes_max = ((65536 - 64) * 8),
228 .period_bytes_min = 64,
229 .period_bytes_max = (65536 - 64),
230 .periods_min = 2,
231 .periods_max = 8,
232 .fifo_size = 0,
235 static struct snd_pcm_hardware snd_ca0106_capture_hw = {
236 .info = (SNDRV_PCM_INFO_MMAP |
237 SNDRV_PCM_INFO_INTERLEAVED |
238 SNDRV_PCM_INFO_BLOCK_TRANSFER |
239 SNDRV_PCM_INFO_MMAP_VALID),
240 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
241 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
242 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
243 .rate_min = 44100,
244 .rate_max = 192000,
245 .channels_min = 2,
246 .channels_max = 2,
247 .buffer_bytes_max = ((65536 - 64) * 8),
248 .period_bytes_min = 64,
249 .period_bytes_max = (65536 - 64),
250 .periods_min = 2,
251 .periods_max = 2,
252 .fifo_size = 0,
255 unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
256 unsigned int reg,
257 unsigned int chn)
259 unsigned long flags;
260 unsigned int regptr, val;
262 regptr = (reg << 16) | chn;
264 spin_lock_irqsave(&emu->emu_lock, flags);
265 outl(regptr, emu->port + PTR);
266 val = inl(emu->port + DATA);
267 spin_unlock_irqrestore(&emu->emu_lock, flags);
268 return val;
271 void snd_ca0106_ptr_write(struct snd_ca0106 *emu,
272 unsigned int reg,
273 unsigned int chn,
274 unsigned int data)
276 unsigned int regptr;
277 unsigned long flags;
279 regptr = (reg << 16) | chn;
281 spin_lock_irqsave(&emu->emu_lock, flags);
282 outl(regptr, emu->port + PTR);
283 outl(data, emu->port + DATA);
284 spin_unlock_irqrestore(&emu->emu_lock, flags);
287 int snd_ca0106_spi_write(struct snd_ca0106 * emu,
288 unsigned int data)
290 unsigned int reset, set;
291 unsigned int reg, tmp;
292 int n, result;
293 reg = SPI;
294 if (data > 0xffff) /* Only 16bit values allowed */
295 return 1;
296 tmp = snd_ca0106_ptr_read(emu, reg, 0);
297 reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
298 set = reset | 0x10000; /* Set xxx1xxxx */
299 snd_ca0106_ptr_write(emu, reg, 0, reset | data);
300 tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
301 snd_ca0106_ptr_write(emu, reg, 0, set | data);
302 result = 1;
303 /* Wait for status bit to return to 0 */
304 for (n = 0; n < 100; n++) {
305 udelay(10);
306 tmp = snd_ca0106_ptr_read(emu, reg, 0);
307 if (!(tmp & 0x10000)) {
308 result = 0;
309 break;
312 if (result) /* Timed out */
313 return 1;
314 snd_ca0106_ptr_write(emu, reg, 0, reset | data);
315 tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
316 return 0;
319 int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
320 u32 reg,
321 u32 value)
323 u32 tmp;
324 int timeout = 0;
325 int status;
326 int retry;
327 if ((reg > 0x7f) || (value > 0x1ff)) {
328 snd_printk(KERN_ERR "i2c_write: invalid values.\n");
329 return -EINVAL;
332 tmp = reg << 25 | value << 16;
333 /* Not sure what this I2C channel controls. */
334 /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
336 /* This controls the I2C connected to the WM8775 ADC Codec */
337 snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
339 for (retry = 0; retry < 10; retry++) {
340 /* Send the data to i2c */
341 tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
342 tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
343 tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
344 snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
346 /* Wait till the transaction ends */
347 while (1) {
348 status = snd_ca0106_ptr_read(emu, I2C_A, 0);
349 //snd_printk("I2C:status=0x%x\n", status);
350 timeout++;
351 if ((status & I2C_A_ADC_START) == 0)
352 break;
354 if (timeout > 1000)
355 break;
357 //Read back and see if the transaction is successful
358 if ((status & I2C_A_ADC_ABORT) == 0)
359 break;
362 if (retry == 10) {
363 snd_printk(KERN_ERR "Writing to ADC failed!\n");
364 return -EINVAL;
367 return 0;
371 static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
373 unsigned long flags;
374 unsigned int enable;
376 spin_lock_irqsave(&emu->emu_lock, flags);
377 enable = inl(emu->port + INTE) | intrenb;
378 outl(enable, emu->port + INTE);
379 spin_unlock_irqrestore(&emu->emu_lock, flags);
382 static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
384 unsigned long flags;
385 unsigned int enable;
387 spin_lock_irqsave(&emu->emu_lock, flags);
388 enable = inl(emu->port + INTE) & ~intrenb;
389 outl(enable, emu->port + INTE);
390 spin_unlock_irqrestore(&emu->emu_lock, flags);
394 static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
396 kfree(runtime->private_data);
399 /* open_playback callback */
400 static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
401 int channel_id)
403 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
404 struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
405 struct snd_ca0106_pcm *epcm;
406 struct snd_pcm_runtime *runtime = substream->runtime;
407 int err;
409 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
411 if (epcm == NULL)
412 return -ENOMEM;
413 epcm->emu = chip;
414 epcm->substream = substream;
415 epcm->channel_id=channel_id;
417 runtime->private_data = epcm;
418 runtime->private_free = snd_ca0106_pcm_free_substream;
420 runtime->hw = snd_ca0106_playback_hw;
422 channel->emu = chip;
423 channel->number = channel_id;
425 channel->use = 1;
426 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
427 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
428 channel->epcm = epcm;
429 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
430 return err;
431 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
432 return err;
433 return 0;
436 /* close callback */
437 static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
439 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
440 struct snd_pcm_runtime *runtime = substream->runtime;
441 struct snd_ca0106_pcm *epcm = runtime->private_data;
442 chip->playback_channels[epcm->channel_id].use = 0;
443 /* FIXME: maybe zero others */
444 return 0;
447 static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
449 return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
452 static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
454 return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
457 static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
459 return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
462 static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
464 return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
467 /* open_capture callback */
468 static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
469 int channel_id)
471 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
472 struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
473 struct snd_ca0106_pcm *epcm;
474 struct snd_pcm_runtime *runtime = substream->runtime;
475 int err;
477 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
478 if (epcm == NULL) {
479 snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
480 return -ENOMEM;
482 epcm->emu = chip;
483 epcm->substream = substream;
484 epcm->channel_id=channel_id;
486 runtime->private_data = epcm;
487 runtime->private_free = snd_ca0106_pcm_free_substream;
489 runtime->hw = snd_ca0106_capture_hw;
491 channel->emu = chip;
492 channel->number = channel_id;
494 channel->use = 1;
495 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
496 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
497 channel->epcm = epcm;
498 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
499 return err;
500 //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
501 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
502 return err;
503 return 0;
506 /* close callback */
507 static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
509 struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
510 struct snd_pcm_runtime *runtime = substream->runtime;
511 struct snd_ca0106_pcm *epcm = runtime->private_data;
512 chip->capture_channels[epcm->channel_id].use = 0;
513 /* FIXME: maybe zero others */
514 return 0;
517 static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
519 return snd_ca0106_pcm_open_capture_channel(substream, 0);
522 static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
524 return snd_ca0106_pcm_open_capture_channel(substream, 1);
527 static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
529 return snd_ca0106_pcm_open_capture_channel(substream, 2);
532 static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
534 return snd_ca0106_pcm_open_capture_channel(substream, 3);
537 /* hw_params callback */
538 static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
539 struct snd_pcm_hw_params *hw_params)
541 return snd_pcm_lib_malloc_pages(substream,
542 params_buffer_bytes(hw_params));
545 /* hw_free callback */
546 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
548 return snd_pcm_lib_free_pages(substream);
551 /* hw_params callback */
552 static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
553 struct snd_pcm_hw_params *hw_params)
555 return snd_pcm_lib_malloc_pages(substream,
556 params_buffer_bytes(hw_params));
559 /* hw_free callback */
560 static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
562 return snd_pcm_lib_free_pages(substream);
565 /* prepare playback callback */
566 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
568 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
569 struct snd_pcm_runtime *runtime = substream->runtime;
570 struct snd_ca0106_pcm *epcm = runtime->private_data;
571 int channel = epcm->channel_id;
572 u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
573 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
574 u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
575 u32 hcfg_set = 0x00000000;
576 u32 hcfg;
577 u32 reg40_mask = 0x30000 << (channel<<1);
578 u32 reg40_set = 0;
579 u32 reg40;
580 /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
581 u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
582 u32 reg71_set = 0;
583 u32 reg71;
584 int i;
586 //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));
587 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
588 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
589 /* Rate can be set per channel. */
590 /* reg40 control host to fifo */
591 /* reg71 controls DAC rate. */
592 switch (runtime->rate) {
593 case 44100:
594 reg40_set = 0x10000 << (channel<<1);
595 reg71_set = 0x01010000;
596 break;
597 case 48000:
598 reg40_set = 0;
599 reg71_set = 0;
600 break;
601 case 96000:
602 reg40_set = 0x20000 << (channel<<1);
603 reg71_set = 0x02020000;
604 break;
605 case 192000:
606 reg40_set = 0x30000 << (channel<<1);
607 reg71_set = 0x03030000;
608 break;
609 default:
610 reg40_set = 0;
611 reg71_set = 0;
612 break;
614 /* Format is a global setting */
615 /* FIXME: Only let the first channel accessed set this. */
616 switch (runtime->format) {
617 case SNDRV_PCM_FORMAT_S16_LE:
618 hcfg_set = 0;
619 break;
620 case SNDRV_PCM_FORMAT_S32_LE:
621 hcfg_set = HCFG_PLAYBACK_S32_LE;
622 break;
623 default:
624 hcfg_set = 0;
625 break;
627 hcfg = inl(emu->port + HCFG) ;
628 hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
629 outl(hcfg, emu->port + HCFG);
630 reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
631 reg40 = (reg40 & ~reg40_mask) | reg40_set;
632 snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
633 reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
634 reg71 = (reg71 & ~reg71_mask) | reg71_set;
635 snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
637 /* FIXME: Check emu->buffer.size before actually writing to it. */
638 for(i=0; i < runtime->periods; i++) {
639 table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
640 table_base[i*2+1] = period_size_bytes << 16;
643 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
644 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
645 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
646 snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
647 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
648 /* FIXME test what 0 bytes does. */
649 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
650 snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
651 snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
652 snd_ca0106_ptr_write(emu, 0x08, channel, 0);
653 snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
654 #if 0
655 snd_ca0106_ptr_write(emu, SPCS0, 0,
656 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
657 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
658 SPCS_GENERATIONSTATUS | 0x00001200 |
659 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
661 #endif
663 return 0;
666 /* prepare capture callback */
667 static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
669 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
670 struct snd_pcm_runtime *runtime = substream->runtime;
671 struct snd_ca0106_pcm *epcm = runtime->private_data;
672 int channel = epcm->channel_id;
673 u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
674 u32 hcfg_set = 0x00000000;
675 u32 hcfg;
676 u32 over_sampling=0x2;
677 u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
678 u32 reg71_set = 0;
679 u32 reg71;
681 //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));
682 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
683 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
684 /* reg71 controls ADC rate. */
685 switch (runtime->rate) {
686 case 44100:
687 reg71_set = 0x00004000;
688 break;
689 case 48000:
690 reg71_set = 0;
691 break;
692 case 96000:
693 reg71_set = 0x00008000;
694 over_sampling=0xa;
695 break;
696 case 192000:
697 reg71_set = 0x0000c000;
698 over_sampling=0xa;
699 break;
700 default:
701 reg71_set = 0;
702 break;
704 /* Format is a global setting */
705 /* FIXME: Only let the first channel accessed set this. */
706 switch (runtime->format) {
707 case SNDRV_PCM_FORMAT_S16_LE:
708 hcfg_set = 0;
709 break;
710 case SNDRV_PCM_FORMAT_S32_LE:
711 hcfg_set = HCFG_CAPTURE_S32_LE;
712 break;
713 default:
714 hcfg_set = 0;
715 break;
717 hcfg = inl(emu->port + HCFG) ;
718 hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
719 outl(hcfg, emu->port + HCFG);
720 reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
721 reg71 = (reg71 & ~reg71_mask) | reg71_set;
722 snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
723 if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
724 snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
728 //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));
729 snd_ca0106_ptr_write(emu, 0x13, channel, 0);
730 snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
731 snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
732 snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
734 return 0;
737 /* trigger_playback callback */
738 static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
739 int cmd)
741 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
742 struct snd_pcm_runtime *runtime;
743 struct snd_ca0106_pcm *epcm;
744 int channel;
745 int result = 0;
746 struct list_head *pos;
747 struct snd_pcm_substream *s;
748 u32 basic = 0;
749 u32 extended = 0;
750 int running=0;
752 switch (cmd) {
753 case SNDRV_PCM_TRIGGER_START:
754 running=1;
755 break;
756 case SNDRV_PCM_TRIGGER_STOP:
757 default:
758 running=0;
759 break;
761 snd_pcm_group_for_each(pos, substream) {
762 s = snd_pcm_group_substream_entry(pos);
763 runtime = s->runtime;
764 epcm = runtime->private_data;
765 channel = epcm->channel_id;
766 //snd_printk("channel=%d\n",channel);
767 epcm->running = running;
768 basic |= (0x1<<channel);
769 extended |= (0x10<<channel);
770 snd_pcm_trigger_done(s, substream);
772 //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
774 switch (cmd) {
775 case SNDRV_PCM_TRIGGER_START:
776 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
777 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
778 break;
779 case SNDRV_PCM_TRIGGER_STOP:
780 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
781 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
782 break;
783 default:
784 result = -EINVAL;
785 break;
787 return result;
790 /* trigger_capture callback */
791 static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
792 int cmd)
794 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
795 struct snd_pcm_runtime *runtime = substream->runtime;
796 struct snd_ca0106_pcm *epcm = runtime->private_data;
797 int channel = epcm->channel_id;
798 int result = 0;
800 switch (cmd) {
801 case SNDRV_PCM_TRIGGER_START:
802 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
803 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
804 epcm->running = 1;
805 break;
806 case SNDRV_PCM_TRIGGER_STOP:
807 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
808 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
809 epcm->running = 0;
810 break;
811 default:
812 result = -EINVAL;
813 break;
815 return result;
818 /* pointer_playback callback */
819 static snd_pcm_uframes_t
820 snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
822 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
823 struct snd_pcm_runtime *runtime = substream->runtime;
824 struct snd_ca0106_pcm *epcm = runtime->private_data;
825 snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
826 int channel = epcm->channel_id;
828 if (!epcm->running)
829 return 0;
831 ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
832 ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
833 ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
834 if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
835 ptr2 = bytes_to_frames(runtime, ptr1);
836 ptr2+= (ptr4 >> 3) * runtime->period_size;
837 ptr=ptr2;
838 if (ptr >= runtime->buffer_size)
839 ptr -= runtime->buffer_size;
840 //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);
842 return ptr;
845 /* pointer_capture callback */
846 static snd_pcm_uframes_t
847 snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
849 struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
850 struct snd_pcm_runtime *runtime = substream->runtime;
851 struct snd_ca0106_pcm *epcm = runtime->private_data;
852 snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
853 int channel = channel=epcm->channel_id;
855 if (!epcm->running)
856 return 0;
858 ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
859 ptr2 = bytes_to_frames(runtime, ptr1);
860 ptr=ptr2;
861 if (ptr >= runtime->buffer_size)
862 ptr -= runtime->buffer_size;
863 //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);
865 return ptr;
868 /* operators */
869 static struct snd_pcm_ops snd_ca0106_playback_front_ops = {
870 .open = snd_ca0106_pcm_open_playback_front,
871 .close = snd_ca0106_pcm_close_playback,
872 .ioctl = snd_pcm_lib_ioctl,
873 .hw_params = snd_ca0106_pcm_hw_params_playback,
874 .hw_free = snd_ca0106_pcm_hw_free_playback,
875 .prepare = snd_ca0106_pcm_prepare_playback,
876 .trigger = snd_ca0106_pcm_trigger_playback,
877 .pointer = snd_ca0106_pcm_pointer_playback,
880 static struct snd_pcm_ops snd_ca0106_capture_0_ops = {
881 .open = snd_ca0106_pcm_open_0_capture,
882 .close = snd_ca0106_pcm_close_capture,
883 .ioctl = snd_pcm_lib_ioctl,
884 .hw_params = snd_ca0106_pcm_hw_params_capture,
885 .hw_free = snd_ca0106_pcm_hw_free_capture,
886 .prepare = snd_ca0106_pcm_prepare_capture,
887 .trigger = snd_ca0106_pcm_trigger_capture,
888 .pointer = snd_ca0106_pcm_pointer_capture,
891 static struct snd_pcm_ops snd_ca0106_capture_1_ops = {
892 .open = snd_ca0106_pcm_open_1_capture,
893 .close = snd_ca0106_pcm_close_capture,
894 .ioctl = snd_pcm_lib_ioctl,
895 .hw_params = snd_ca0106_pcm_hw_params_capture,
896 .hw_free = snd_ca0106_pcm_hw_free_capture,
897 .prepare = snd_ca0106_pcm_prepare_capture,
898 .trigger = snd_ca0106_pcm_trigger_capture,
899 .pointer = snd_ca0106_pcm_pointer_capture,
902 static struct snd_pcm_ops snd_ca0106_capture_2_ops = {
903 .open = snd_ca0106_pcm_open_2_capture,
904 .close = snd_ca0106_pcm_close_capture,
905 .ioctl = snd_pcm_lib_ioctl,
906 .hw_params = snd_ca0106_pcm_hw_params_capture,
907 .hw_free = snd_ca0106_pcm_hw_free_capture,
908 .prepare = snd_ca0106_pcm_prepare_capture,
909 .trigger = snd_ca0106_pcm_trigger_capture,
910 .pointer = snd_ca0106_pcm_pointer_capture,
913 static struct snd_pcm_ops snd_ca0106_capture_3_ops = {
914 .open = snd_ca0106_pcm_open_3_capture,
915 .close = snd_ca0106_pcm_close_capture,
916 .ioctl = snd_pcm_lib_ioctl,
917 .hw_params = snd_ca0106_pcm_hw_params_capture,
918 .hw_free = snd_ca0106_pcm_hw_free_capture,
919 .prepare = snd_ca0106_pcm_prepare_capture,
920 .trigger = snd_ca0106_pcm_trigger_capture,
921 .pointer = snd_ca0106_pcm_pointer_capture,
924 static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
925 .open = snd_ca0106_pcm_open_playback_center_lfe,
926 .close = snd_ca0106_pcm_close_playback,
927 .ioctl = snd_pcm_lib_ioctl,
928 .hw_params = snd_ca0106_pcm_hw_params_playback,
929 .hw_free = snd_ca0106_pcm_hw_free_playback,
930 .prepare = snd_ca0106_pcm_prepare_playback,
931 .trigger = snd_ca0106_pcm_trigger_playback,
932 .pointer = snd_ca0106_pcm_pointer_playback,
935 static struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
936 .open = snd_ca0106_pcm_open_playback_unknown,
937 .close = snd_ca0106_pcm_close_playback,
938 .ioctl = snd_pcm_lib_ioctl,
939 .hw_params = snd_ca0106_pcm_hw_params_playback,
940 .hw_free = snd_ca0106_pcm_hw_free_playback,
941 .prepare = snd_ca0106_pcm_prepare_playback,
942 .trigger = snd_ca0106_pcm_trigger_playback,
943 .pointer = snd_ca0106_pcm_pointer_playback,
946 static struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
947 .open = snd_ca0106_pcm_open_playback_rear,
948 .close = snd_ca0106_pcm_close_playback,
949 .ioctl = snd_pcm_lib_ioctl,
950 .hw_params = snd_ca0106_pcm_hw_params_playback,
951 .hw_free = snd_ca0106_pcm_hw_free_playback,
952 .prepare = snd_ca0106_pcm_prepare_playback,
953 .trigger = snd_ca0106_pcm_trigger_playback,
954 .pointer = snd_ca0106_pcm_pointer_playback,
958 static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
959 unsigned short reg)
961 struct snd_ca0106 *emu = ac97->private_data;
962 unsigned long flags;
963 unsigned short val;
965 spin_lock_irqsave(&emu->emu_lock, flags);
966 outb(reg, emu->port + AC97ADDRESS);
967 val = inw(emu->port + AC97DATA);
968 spin_unlock_irqrestore(&emu->emu_lock, flags);
969 return val;
972 static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
973 unsigned short reg, unsigned short val)
975 struct snd_ca0106 *emu = ac97->private_data;
976 unsigned long flags;
978 spin_lock_irqsave(&emu->emu_lock, flags);
979 outb(reg, emu->port + AC97ADDRESS);
980 outw(val, emu->port + AC97DATA);
981 spin_unlock_irqrestore(&emu->emu_lock, flags);
984 static int snd_ca0106_ac97(struct snd_ca0106 *chip)
986 struct snd_ac97_bus *pbus;
987 struct snd_ac97_template ac97;
988 int err;
989 static struct snd_ac97_bus_ops ops = {
990 .write = snd_ca0106_ac97_write,
991 .read = snd_ca0106_ac97_read,
994 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
995 return err;
996 pbus->no_vra = 1; /* we don't need VRA */
998 memset(&ac97, 0, sizeof(ac97));
999 ac97.private_data = chip;
1000 ac97.scaps = AC97_SCAP_NO_SPDIF;
1001 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1004 static int snd_ca0106_free(struct snd_ca0106 *chip)
1006 if (chip->res_port != NULL) { /* avoid access to already used hardware */
1007 // disable interrupts
1008 snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
1009 outl(0, chip->port + INTE);
1010 snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
1011 udelay(1000);
1012 // disable audio
1013 //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
1014 outl(0, chip->port + HCFG);
1015 /* FIXME: We need to stop and DMA transfers here.
1016 * But as I am not sure how yet, we cannot from the dma pages.
1017 * So we can fix: snd-malloc: Memory leak? pages not freed = 8
1020 // release the data
1021 #if 1
1022 if (chip->buffer.area)
1023 snd_dma_free_pages(&chip->buffer);
1024 #endif
1026 // release the i/o port
1027 release_and_free_resource(chip->res_port);
1029 // release the irq
1030 if (chip->irq >= 0)
1031 free_irq(chip->irq, (void *)chip);
1032 pci_disable_device(chip->pci);
1033 kfree(chip);
1034 return 0;
1037 static int snd_ca0106_dev_free(struct snd_device *device)
1039 struct snd_ca0106 *chip = device->device_data;
1040 return snd_ca0106_free(chip);
1043 static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id,
1044 struct pt_regs *regs)
1046 unsigned int status;
1048 struct snd_ca0106 *chip = dev_id;
1049 int i;
1050 int mask;
1051 unsigned int stat76;
1052 struct snd_ca0106_channel *pchannel;
1054 status = inl(chip->port + IPR);
1055 if (! status)
1056 return IRQ_NONE;
1058 stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
1059 //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
1060 //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
1061 mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
1062 for(i = 0; i < 4; i++) {
1063 pchannel = &(chip->playback_channels[i]);
1064 if (stat76 & mask) {
1065 /* FIXME: Select the correct substream for period elapsed */
1066 if(pchannel->use) {
1067 snd_pcm_period_elapsed(pchannel->epcm->substream);
1068 //printk(KERN_INFO "interrupt [%d] used\n", i);
1071 //printk(KERN_INFO "channel=%p\n",pchannel);
1072 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1073 mask <<= 1;
1075 mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
1076 for(i = 0; i < 4; i++) {
1077 pchannel = &(chip->capture_channels[i]);
1078 if (stat76 & mask) {
1079 /* FIXME: Select the correct substream for period elapsed */
1080 if(pchannel->use) {
1081 snd_pcm_period_elapsed(pchannel->epcm->substream);
1082 //printk(KERN_INFO "interrupt [%d] used\n", i);
1085 //printk(KERN_INFO "channel=%p\n",pchannel);
1086 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1087 mask <<= 1;
1090 snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
1092 if (chip->midi.dev_id &&
1093 (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
1094 if (chip->midi.interrupt)
1095 chip->midi.interrupt(&chip->midi, status);
1096 else
1097 chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
1100 // acknowledge the interrupt if necessary
1101 outl(status, chip->port+IPR);
1103 return IRQ_HANDLED;
1106 static int __devinit snd_ca0106_pcm(struct snd_ca0106 *emu, int device, struct snd_pcm **rpcm)
1108 struct snd_pcm *pcm;
1109 struct snd_pcm_substream *substream;
1110 int err;
1112 if (rpcm)
1113 *rpcm = NULL;
1114 if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
1115 return err;
1117 pcm->private_data = emu;
1119 switch (device) {
1120 case 0:
1121 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
1122 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
1123 break;
1124 case 1:
1125 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
1126 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
1127 break;
1128 case 2:
1129 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
1130 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
1131 break;
1132 case 3:
1133 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
1134 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
1135 break;
1138 pcm->info_flags = 0;
1139 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1140 strcpy(pcm->name, "CA0106");
1141 emu->pcm = pcm;
1143 for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1144 substream;
1145 substream = substream->next) {
1146 if ((err = snd_pcm_lib_preallocate_pages(substream,
1147 SNDRV_DMA_TYPE_DEV,
1148 snd_dma_pci_data(emu->pci),
1149 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
1150 return err;
1153 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
1154 substream;
1155 substream = substream->next) {
1156 if ((err = snd_pcm_lib_preallocate_pages(substream,
1157 SNDRV_DMA_TYPE_DEV,
1158 snd_dma_pci_data(emu->pci),
1159 64*1024, 64*1024)) < 0)
1160 return err;
1163 if (rpcm)
1164 *rpcm = pcm;
1166 return 0;
1169 static unsigned int spi_dac_init[] = {
1170 0x00ff,
1171 0x02ff,
1172 0x0400,
1173 0x0520,
1174 0x0600,
1175 0x08ff,
1176 0x0aff,
1177 0x0cff,
1178 0x0eff,
1179 0x10ff,
1180 0x1200,
1181 0x1400,
1182 0x1480,
1183 0x1800,
1184 0x1aff,
1185 0x1cff,
1186 0x1e00,
1187 0x0530,
1188 0x0602,
1189 0x0622,
1190 0x1400,
1193 static int __devinit snd_ca0106_create(struct snd_card *card,
1194 struct pci_dev *pci,
1195 struct snd_ca0106 **rchip)
1197 struct snd_ca0106 *chip;
1198 struct snd_ca0106_details *c;
1199 int err;
1200 int ch;
1201 static struct snd_device_ops ops = {
1202 .dev_free = snd_ca0106_dev_free,
1205 *rchip = NULL;
1207 if ((err = pci_enable_device(pci)) < 0)
1208 return err;
1209 if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
1210 pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
1211 printk(KERN_ERR "error to set 32bit mask DMA\n");
1212 pci_disable_device(pci);
1213 return -ENXIO;
1216 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1217 if (chip == NULL) {
1218 pci_disable_device(pci);
1219 return -ENOMEM;
1222 chip->card = card;
1223 chip->pci = pci;
1224 chip->irq = -1;
1226 spin_lock_init(&chip->emu_lock);
1228 chip->port = pci_resource_start(pci, 0);
1229 if ((chip->res_port = request_region(chip->port, 0x20,
1230 "snd_ca0106")) == NULL) {
1231 snd_ca0106_free(chip);
1232 printk(KERN_ERR "cannot allocate the port\n");
1233 return -EBUSY;
1236 if (request_irq(pci->irq, snd_ca0106_interrupt,
1237 SA_INTERRUPT|SA_SHIRQ, "snd_ca0106",
1238 (void *)chip)) {
1239 snd_ca0106_free(chip);
1240 printk(KERN_ERR "cannot grab irq\n");
1241 return -EBUSY;
1243 chip->irq = pci->irq;
1245 /* This stores the periods table. */
1246 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
1247 snd_ca0106_free(chip);
1248 return -ENOMEM;
1251 pci_set_master(pci);
1252 /* read revision & serial */
1253 pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
1254 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
1255 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
1256 #if 1
1257 printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
1258 chip->revision, chip->serial);
1259 #endif
1260 strcpy(card->driver, "CA0106");
1261 strcpy(card->shortname, "CA0106");
1263 for (c = ca0106_chip_details; c->serial; c++) {
1264 if (c->serial == chip->serial)
1265 break;
1267 chip->details = c;
1268 sprintf(card->longname, "%s at 0x%lx irq %i",
1269 c->name, chip->port, chip->irq);
1271 outl(0, chip->port + INTE);
1274 * Init to 0x02109204 :
1275 * Clock accuracy = 0 (1000ppm)
1276 * Sample Rate = 2 (48kHz)
1277 * Audio Channel = 1 (Left of 2)
1278 * Source Number = 0 (Unspecified)
1279 * Generation Status = 1 (Original for Cat Code 12)
1280 * Cat Code = 12 (Digital Signal Mixer)
1281 * Mode = 0 (Mode 0)
1282 * Emphasis = 0 (None)
1283 * CP = 1 (Copyright unasserted)
1284 * AN = 0 (Audio data)
1285 * P = 0 (Consumer)
1287 snd_ca0106_ptr_write(chip, SPCS0, 0,
1288 chip->spdif_bits[0] =
1289 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1290 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1291 SPCS_GENERATIONSTATUS | 0x00001200 |
1292 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1293 /* Only SPCS1 has been tested */
1294 snd_ca0106_ptr_write(chip, SPCS1, 0,
1295 chip->spdif_bits[1] =
1296 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1297 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1298 SPCS_GENERATIONSTATUS | 0x00001200 |
1299 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1300 snd_ca0106_ptr_write(chip, SPCS2, 0,
1301 chip->spdif_bits[2] =
1302 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1303 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1304 SPCS_GENERATIONSTATUS | 0x00001200 |
1305 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1306 snd_ca0106_ptr_write(chip, SPCS3, 0,
1307 chip->spdif_bits[3] =
1308 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1309 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1310 SPCS_GENERATIONSTATUS | 0x00001200 |
1311 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1313 snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
1314 snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
1316 /* Write 0x8000 to AC97_REC_GAIN to mute it. */
1317 outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
1318 outw(0x8000, chip->port + AC97DATA);
1319 #if 0
1320 snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
1321 snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
1322 snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
1323 snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
1324 #endif
1326 //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
1327 /* Analog or Digital output */
1328 snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
1329 snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
1330 chip->spdif_enable = 0; /* Set digital SPDIF output off */
1331 chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1332 //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
1333 //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
1335 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
1336 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
1337 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
1338 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
1339 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
1340 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
1341 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
1342 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
1343 for(ch = 0; ch < 4; ch++) {
1344 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
1345 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
1346 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
1347 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
1348 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
1349 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
1351 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC, Line in, TAD in, AUX in */
1352 chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1354 if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
1355 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1356 outl(0x0, chip->port+GPIO);
1357 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1358 outl(0x005f5301, chip->port+GPIO); /* Analog */
1359 } else {
1360 outl(0x0, chip->port+GPIO);
1361 outl(0x005f03a3, chip->port+GPIO); /* Analog */
1362 //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
1364 snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
1366 //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1367 //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
1368 //outl(0x00000009, chip->port+HCFG);
1369 outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
1371 if (chip->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
1372 snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
1374 if (chip->details->spi_dac == 1) { /* The SB0570 use SPI to control DAC. */
1375 int size, n;
1377 size = ARRAY_SIZE(spi_dac_init);
1378 for (n=0; n < size; n++)
1379 snd_ca0106_spi_write(chip, spi_dac_init[n]);
1382 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1383 chip, &ops)) < 0) {
1384 snd_ca0106_free(chip);
1385 return err;
1387 *rchip = chip;
1388 return 0;
1392 static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
1394 snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
1397 static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
1399 snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
1402 static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
1404 return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
1405 midi->port + idx, 0);
1408 static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
1410 snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
1413 static struct snd_card *ca0106_dev_id_card(void *dev_id)
1415 return ((struct snd_ca0106 *)dev_id)->card;
1418 static int ca0106_dev_id_port(void *dev_id)
1420 return ((struct snd_ca0106 *)dev_id)->port;
1423 static int __devinit snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
1425 struct snd_ca_midi *midi;
1426 char *name;
1427 int err;
1429 if (channel == CA0106_MIDI_CHAN_B) {
1430 name = "CA0106 MPU-401 (UART) B";
1431 midi = &chip->midi2;
1432 midi->tx_enable = INTE_MIDI_TX_B;
1433 midi->rx_enable = INTE_MIDI_RX_B;
1434 midi->ipr_tx = IPR_MIDI_TX_B;
1435 midi->ipr_rx = IPR_MIDI_RX_B;
1436 midi->port = MIDI_UART_B_DATA;
1437 } else {
1438 name = "CA0106 MPU-401 (UART)";
1439 midi = &chip->midi;
1440 midi->tx_enable = INTE_MIDI_TX_A;
1441 midi->rx_enable = INTE_MIDI_TX_B;
1442 midi->ipr_tx = IPR_MIDI_TX_A;
1443 midi->ipr_rx = IPR_MIDI_RX_A;
1444 midi->port = MIDI_UART_A_DATA;
1447 midi->reset = CA0106_MPU401_RESET;
1448 midi->enter_uart = CA0106_MPU401_ENTER_UART;
1449 midi->ack = CA0106_MPU401_ACK;
1451 midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
1452 midi->output_ready = CA0106_MIDI_OUTPUT_READY;
1454 midi->channel = channel;
1456 midi->interrupt_enable = ca0106_midi_interrupt_enable;
1457 midi->interrupt_disable = ca0106_midi_interrupt_disable;
1459 midi->read = ca0106_midi_read;
1460 midi->write = ca0106_midi_write;
1462 midi->get_dev_id_card = ca0106_dev_id_card;
1463 midi->get_dev_id_port = ca0106_dev_id_port;
1465 midi->dev_id = chip;
1467 if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
1468 return err;
1470 return 0;
1474 static int __devinit snd_ca0106_probe(struct pci_dev *pci,
1475 const struct pci_device_id *pci_id)
1477 static int dev;
1478 struct snd_card *card;
1479 struct snd_ca0106 *chip;
1480 int err;
1482 if (dev >= SNDRV_CARDS)
1483 return -ENODEV;
1484 if (!enable[dev]) {
1485 dev++;
1486 return -ENOENT;
1489 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1490 if (card == NULL)
1491 return -ENOMEM;
1493 if ((err = snd_ca0106_create(card, pci, &chip)) < 0) {
1494 snd_card_free(card);
1495 return err;
1498 if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
1499 snd_card_free(card);
1500 return err;
1502 if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
1503 snd_card_free(card);
1504 return err;
1506 if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
1507 snd_card_free(card);
1508 return err;
1510 if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
1511 snd_card_free(card);
1512 return err;
1514 if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
1515 if ((err = snd_ca0106_ac97(chip)) < 0) {
1516 snd_card_free(card);
1517 return err;
1520 if ((err = snd_ca0106_mixer(chip)) < 0) {
1521 snd_card_free(card);
1522 return err;
1525 snd_printdd("ca0106: probe for MIDI channel A ...");
1526 if ((err = snd_ca0106_midi(chip,CA0106_MIDI_CHAN_A)) < 0) {
1527 snd_card_free(card);
1528 snd_printdd(" failed, err=0x%x\n",err);
1529 return err;
1531 snd_printdd(" done.\n");
1533 #ifdef CONFIG_PROC_FS
1534 snd_ca0106_proc_init(chip);
1535 #endif
1537 if ((err = snd_card_register(card)) < 0) {
1538 snd_card_free(card);
1539 return err;
1542 pci_set_drvdata(pci, card);
1543 dev++;
1544 return 0;
1547 static void __devexit snd_ca0106_remove(struct pci_dev *pci)
1549 snd_card_free(pci_get_drvdata(pci));
1550 pci_set_drvdata(pci, NULL);
1553 // PCI IDs
1554 static struct pci_device_id snd_ca0106_ids[] = {
1555 { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
1556 { 0, }
1558 MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
1560 // pci_driver definition
1561 static struct pci_driver driver = {
1562 .name = "CA0106",
1563 .id_table = snd_ca0106_ids,
1564 .probe = snd_ca0106_probe,
1565 .remove = __devexit_p(snd_ca0106_remove),
1568 // initialization of the module
1569 static int __init alsa_card_ca0106_init(void)
1571 return pci_register_driver(&driver);
1574 // clean up the module
1575 static void __exit alsa_card_ca0106_exit(void)
1577 pci_unregister_driver(&driver);
1580 module_init(alsa_card_ca0106_init)
1581 module_exit(alsa_card_ca0106_exit)