blob: a56e68ea87bc3147a8efda8917fef4bbfe504ac6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.22
5 *
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.
21 *
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
29 *
30 * For capture from Mic in or Line in.
31 * Digital/Analog ( switch must be in Analog mode for CAPTURE. )
32 *
33 * CAPTURE feedback into PLAYBACK
34 *
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 *
81 * BUGS:
82 * Some stability problems when unloading the snd-ca0106 kernel module.
83 * --
84 *
85 * TODO:
86 * 4 Capture channels, only one implemented so far.
87 * Other capture rates apart from 48khz not implemented.
88 * MIDI
89 * --
90 * GENERAL INFO:
91 * Model: SB0310
92 * P17 Chip: CA0106-DAT
93 * AC97 Codec: STAC 9721
94 * ADC: Philips 1361T (Stereo 24bit)
95 * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
96 *
97 * GENERAL INFO:
98 * Model: SB0410
99 * P17 Chip: CA0106-DAT
100 * AC97 Codec: None
101 * ADC: WM8775EDS (4 Channel)
102 * DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
103 * SPDIF Out control switches between Mic in and SPDIF out.
104 * No sound out or mic input working yet.
105 *
106 * GENERAL INFO:
107 * Model: SB0413
108 * P17 Chip: CA0106-DAT
109 * AC97 Codec: None.
110 * ADC: Unknown
111 * DAC: Unknown
112 * Trying to handle it like the SB0410.
113 *
114 * This code was initally based on code from ALSA's emu10k1x.c which is:
115 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
116 *
117 * This program is free software; you can redistribute it and/or modify
118 * it under the terms of the GNU General Public License as published by
119 * the Free Software Foundation; either version 2 of the License, or
120 * (at your option) any later version.
121 *
122 * This program is distributed in the hope that it will be useful,
123 * but WITHOUT ANY WARRANTY; without even the implied warranty of
124 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
125 * GNU General Public License for more details.
126 *
127 * You should have received a copy of the GNU General Public License
128 * along with this program; if not, write to the Free Software
129 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
130 *
131 */
132#include <sound/driver.h>
133#include <linux/delay.h>
134#include <linux/init.h>
135#include <linux/interrupt.h>
136#include <linux/pci.h>
137#include <linux/slab.h>
138#include <linux/moduleparam.h>
139#include <sound/core.h>
140#include <sound/initval.h>
141#include <sound/pcm.h>
142#include <sound/ac97_codec.h>
143#include <sound/info.h>
144
145MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
146MODULE_DESCRIPTION("CA0106");
147MODULE_LICENSE("GPL");
148MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
149
150// module parameters (see "Module Parameters")
151static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
152static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
153static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
154
155module_param_array(index, int, NULL, 0444);
156MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
157module_param_array(id, charp, NULL, 0444);
158MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
159module_param_array(enable, bool, NULL, 0444);
160MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
161
162#include "ca0106.h"
163
James Courtier-Dutton1baa7052005-05-21 22:35:58 +0200164static ca0106_details_t ca0106_chip_details[] = {
165 /* AudigyLS[SB0310] */
166 { .serial = 0x10021102,
167 .name = "AudigyLS [SB0310]",
168 .ac97 = 1 } ,
169 /* Unknown AudigyLS that also says SB0310 on it */
170 { .serial = 0x10051102,
171 .name = "AudigyLS [SB0310b]",
172 .ac97 = 1 } ,
173 /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
174 { .serial = 0x10061102,
175 .name = "Live! 7.1 24bit [SB0410]",
176 .gpio_type = 1 } ,
177 /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
178 { .serial = 0x10071102,
179 .name = "Live! 7.1 24bit [SB0413]",
180 .gpio_type = 1 } ,
181 /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
182 { .serial = 0x10091462,
183 .name = "MSI K8N Diamond MB [SB0438]",
184 .gpio_type = 1 } ,
185 { .serial = 0,
186 .name = "AudigyLS [Unknown]" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
189/* hardware definition */
190static snd_pcm_hardware_t snd_ca0106_playback_hw = {
191 .info = (SNDRV_PCM_INFO_MMAP |
192 SNDRV_PCM_INFO_INTERLEAVED |
193 SNDRV_PCM_INFO_BLOCK_TRANSFER |
194 SNDRV_PCM_INFO_MMAP_VALID),
195 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
196 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000,
197 .rate_min = 48000,
198 .rate_max = 192000,
199 .channels_min = 2, //1,
200 .channels_max = 2, //6,
201 .buffer_bytes_max = ((65536 - 64) * 8),
202 .period_bytes_min = 64,
203 .period_bytes_max = (65536 - 64),
204 .periods_min = 2,
205 .periods_max = 8,
206 .fifo_size = 0,
207};
208
209static snd_pcm_hardware_t snd_ca0106_capture_hw = {
210 .info = (SNDRV_PCM_INFO_MMAP |
211 SNDRV_PCM_INFO_INTERLEAVED |
212 SNDRV_PCM_INFO_BLOCK_TRANSFER |
213 SNDRV_PCM_INFO_MMAP_VALID),
214 .formats = SNDRV_PCM_FMTBIT_S16_LE,
215 .rates = SNDRV_PCM_RATE_48000,
216 .rate_min = 48000,
217 .rate_max = 48000,
218 .channels_min = 2,
219 .channels_max = 2,
220 .buffer_bytes_max = ((65536 - 64) * 8),
221 .period_bytes_min = 64,
222 .period_bytes_max = (65536 - 64),
223 .periods_min = 2,
224 .periods_max = 2,
225 .fifo_size = 0,
226};
227
228unsigned int snd_ca0106_ptr_read(ca0106_t * emu,
229 unsigned int reg,
230 unsigned int chn)
231{
232 unsigned long flags;
233 unsigned int regptr, val;
234
235 regptr = (reg << 16) | chn;
236
237 spin_lock_irqsave(&emu->emu_lock, flags);
238 outl(regptr, emu->port + PTR);
239 val = inl(emu->port + DATA);
240 spin_unlock_irqrestore(&emu->emu_lock, flags);
241 return val;
242}
243
244void snd_ca0106_ptr_write(ca0106_t *emu,
245 unsigned int reg,
246 unsigned int chn,
247 unsigned int data)
248{
249 unsigned int regptr;
250 unsigned long flags;
251
252 regptr = (reg << 16) | chn;
253
254 spin_lock_irqsave(&emu->emu_lock, flags);
255 outl(regptr, emu->port + PTR);
256 outl(data, emu->port + DATA);
257 spin_unlock_irqrestore(&emu->emu_lock, flags);
258}
259
260static void snd_ca0106_intr_enable(ca0106_t *emu, unsigned int intrenb)
261{
262 unsigned long flags;
263 unsigned int enable;
264
265 spin_lock_irqsave(&emu->emu_lock, flags);
266 enable = inl(emu->port + INTE) | intrenb;
267 outl(enable, emu->port + INTE);
268 spin_unlock_irqrestore(&emu->emu_lock, flags);
269}
270
271static void snd_ca0106_pcm_free_substream(snd_pcm_runtime_t *runtime)
272{
273 ca0106_pcm_t *epcm = runtime->private_data;
274
275 if (epcm) {
276 kfree(epcm);
277 }
278}
279
280/* open_playback callback */
281static int snd_ca0106_pcm_open_playback_channel(snd_pcm_substream_t *substream, int channel_id)
282{
283 ca0106_t *chip = snd_pcm_substream_chip(substream);
284 ca0106_channel_t *channel = &(chip->playback_channels[channel_id]);
285 ca0106_pcm_t *epcm;
286 snd_pcm_runtime_t *runtime = substream->runtime;
287 int err;
288
289 epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
290
291 if (epcm == NULL)
292 return -ENOMEM;
293 epcm->emu = chip;
294 epcm->substream = substream;
295 epcm->channel_id=channel_id;
296
297 runtime->private_data = epcm;
298 runtime->private_free = snd_ca0106_pcm_free_substream;
299
300 runtime->hw = snd_ca0106_playback_hw;
301
302 channel->emu = chip;
303 channel->number = channel_id;
304
305 channel->use=1;
306 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
307 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
308 channel->epcm=epcm;
309 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
310 return err;
311 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
312 return err;
313 return 0;
314}
315
316/* close callback */
317static int snd_ca0106_pcm_close_playback(snd_pcm_substream_t *substream)
318{
319 ca0106_t *chip = snd_pcm_substream_chip(substream);
320 snd_pcm_runtime_t *runtime = substream->runtime;
321 ca0106_pcm_t *epcm = runtime->private_data;
322 chip->playback_channels[epcm->channel_id].use=0;
323/* FIXME: maybe zero others */
324 return 0;
325}
326
327static int snd_ca0106_pcm_open_playback_front(snd_pcm_substream_t *substream)
328{
329 return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
330}
331
332static int snd_ca0106_pcm_open_playback_center_lfe(snd_pcm_substream_t *substream)
333{
334 return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
335}
336
337static int snd_ca0106_pcm_open_playback_unknown(snd_pcm_substream_t *substream)
338{
339 return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
340}
341
342static int snd_ca0106_pcm_open_playback_rear(snd_pcm_substream_t *substream)
343{
344 return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
345}
346
347/* open_capture callback */
348static int snd_ca0106_pcm_open_capture_channel(snd_pcm_substream_t *substream, int channel_id)
349{
350 ca0106_t *chip = snd_pcm_substream_chip(substream);
351 ca0106_channel_t *channel = &(chip->capture_channels[channel_id]);
352 ca0106_pcm_t *epcm;
353 snd_pcm_runtime_t *runtime = substream->runtime;
354 int err;
355
356 epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
357 if (epcm == NULL) {
358 snd_printk("open_capture_channel: failed epcm alloc\n");
359 return -ENOMEM;
360 }
361 epcm->emu = chip;
362 epcm->substream = substream;
363 epcm->channel_id=channel_id;
364
365 runtime->private_data = epcm;
366 runtime->private_free = snd_ca0106_pcm_free_substream;
367
368 runtime->hw = snd_ca0106_capture_hw;
369
370 channel->emu = chip;
371 channel->number = channel_id;
372
373 channel->use=1;
374 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
375 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
376 channel->epcm=epcm;
377 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
378 return err;
379 //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
380 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
381 return err;
382 return 0;
383}
384
385/* close callback */
386static int snd_ca0106_pcm_close_capture(snd_pcm_substream_t *substream)
387{
388 ca0106_t *chip = snd_pcm_substream_chip(substream);
389 snd_pcm_runtime_t *runtime = substream->runtime;
390 ca0106_pcm_t *epcm = runtime->private_data;
391 chip->capture_channels[epcm->channel_id].use=0;
392/* FIXME: maybe zero others */
393 return 0;
394}
395
396static int snd_ca0106_pcm_open_0_capture(snd_pcm_substream_t *substream)
397{
398 return snd_ca0106_pcm_open_capture_channel(substream, 0);
399}
400
401static int snd_ca0106_pcm_open_1_capture(snd_pcm_substream_t *substream)
402{
403 return snd_ca0106_pcm_open_capture_channel(substream, 1);
404}
405
406static int snd_ca0106_pcm_open_2_capture(snd_pcm_substream_t *substream)
407{
408 return snd_ca0106_pcm_open_capture_channel(substream, 2);
409}
410
411static int snd_ca0106_pcm_open_3_capture(snd_pcm_substream_t *substream)
412{
413 return snd_ca0106_pcm_open_capture_channel(substream, 3);
414}
415
416/* hw_params callback */
417static int snd_ca0106_pcm_hw_params_playback(snd_pcm_substream_t *substream,
418 snd_pcm_hw_params_t * hw_params)
419{
420 return snd_pcm_lib_malloc_pages(substream,
421 params_buffer_bytes(hw_params));
422}
423
424/* hw_free callback */
425static int snd_ca0106_pcm_hw_free_playback(snd_pcm_substream_t *substream)
426{
427 return snd_pcm_lib_free_pages(substream);
428}
429
430/* hw_params callback */
431static int snd_ca0106_pcm_hw_params_capture(snd_pcm_substream_t *substream,
432 snd_pcm_hw_params_t * hw_params)
433{
434 return snd_pcm_lib_malloc_pages(substream,
435 params_buffer_bytes(hw_params));
436}
437
438/* hw_free callback */
439static int snd_ca0106_pcm_hw_free_capture(snd_pcm_substream_t *substream)
440{
441 return snd_pcm_lib_free_pages(substream);
442}
443
444/* prepare playback callback */
445static int snd_ca0106_pcm_prepare_playback(snd_pcm_substream_t *substream)
446{
447 ca0106_t *emu = snd_pcm_substream_chip(substream);
448 snd_pcm_runtime_t *runtime = substream->runtime;
449 ca0106_pcm_t *epcm = runtime->private_data;
450 int channel = epcm->channel_id;
451 u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
452 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
453 u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
454 u32 hcfg_set = 0x00000000;
455 u32 hcfg;
456 u32 reg40_mask = 0x30000 << (channel<<1);
457 u32 reg40_set = 0;
458 u32 reg40;
459 /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
460 u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
461 u32 reg71_set = 0;
462 u32 reg71;
463 int i;
464
465 //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));
466 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
467 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
468 /* Rate can be set per channel. */
469 /* reg40 control host to fifo */
470 /* reg71 controls DAC rate. */
471 switch (runtime->rate) {
472 case 44100:
473 reg40_set = 0x10000 << (channel<<1);
474 reg71_set = 0x01010000;
475 break;
476 case 48000:
477 reg40_set = 0;
478 reg71_set = 0;
479 break;
480 case 96000:
481 reg40_set = 0x20000 << (channel<<1);
482 reg71_set = 0x02020000;
483 break;
484 case 192000:
485 reg40_set = 0x30000 << (channel<<1);
486 reg71_set = 0x03030000;
487 break;
488 default:
489 reg40_set = 0;
490 reg71_set = 0;
491 break;
492 }
493 /* Format is a global setting */
494 /* FIXME: Only let the first channel accessed set this. */
495 switch (runtime->format) {
496 case SNDRV_PCM_FORMAT_S16_LE:
497 hcfg_set = 0;
498 break;
499 case SNDRV_PCM_FORMAT_S32_LE:
500 hcfg_set = HCFG_PLAYBACK_S32_LE;
501 break;
502 default:
503 hcfg_set = 0;
504 break;
505 }
506 hcfg = inl(emu->port + HCFG) ;
507 hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
508 outl(hcfg, emu->port + HCFG);
509 reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
510 reg40 = (reg40 & ~reg40_mask) | reg40_set;
511 snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
512 reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
513 reg71 = (reg71 & ~reg71_mask) | reg71_set;
514 snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
515
516 /* FIXME: Check emu->buffer.size before actually writing to it. */
517 for(i=0; i < runtime->periods; i++) {
518 table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
519 table_base[(i*2)+1]=period_size_bytes<<16;
520 }
521
522 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
523 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
524 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
525 snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
526 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
527 /* FIXME test what 0 bytes does. */
528 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
529 snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
530 snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
531 snd_ca0106_ptr_write(emu, 0x08, channel, 0);
532 snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
533#if 0
534 snd_ca0106_ptr_write(emu, SPCS0, 0,
535 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
536 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
537 SPCS_GENERATIONSTATUS | 0x00001200 |
538 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
539 }
540#endif
541
542 return 0;
543}
544
545/* prepare capture callback */
546static int snd_ca0106_pcm_prepare_capture(snd_pcm_substream_t *substream)
547{
548 ca0106_t *emu = snd_pcm_substream_chip(substream);
549 snd_pcm_runtime_t *runtime = substream->runtime;
550 ca0106_pcm_t *epcm = runtime->private_data;
551 int channel = epcm->channel_id;
552 //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));
553 snd_ca0106_ptr_write(emu, 0x13, channel, 0);
554 snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
555 snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
556 snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
557
558 return 0;
559}
560
561/* trigger_playback callback */
562static int snd_ca0106_pcm_trigger_playback(snd_pcm_substream_t *substream,
563 int cmd)
564{
565 ca0106_t *emu = snd_pcm_substream_chip(substream);
566 snd_pcm_runtime_t *runtime;
567 ca0106_pcm_t *epcm;
568 int channel;
569 int result = 0;
570 struct list_head *pos;
571 snd_pcm_substream_t *s;
572 u32 basic = 0;
573 u32 extended = 0;
574 int running=0;
575
576 switch (cmd) {
577 case SNDRV_PCM_TRIGGER_START:
578 running=1;
579 break;
580 case SNDRV_PCM_TRIGGER_STOP:
581 default:
582 running=0;
583 break;
584 }
585 snd_pcm_group_for_each(pos, substream) {
586 s = snd_pcm_group_substream_entry(pos);
587 runtime = s->runtime;
588 epcm = runtime->private_data;
589 channel = epcm->channel_id;
590 //snd_printk("channel=%d\n",channel);
591 epcm->running = running;
592 basic |= (0x1<<channel);
593 extended |= (0x10<<channel);
594 snd_pcm_trigger_done(s, substream);
595 }
596 //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
597
598 switch (cmd) {
599 case SNDRV_PCM_TRIGGER_START:
600 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
601 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
602 break;
603 case SNDRV_PCM_TRIGGER_STOP:
604 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
605 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
606 break;
607 default:
608 result = -EINVAL;
609 break;
610 }
611 return result;
612}
613
614/* trigger_capture callback */
615static int snd_ca0106_pcm_trigger_capture(snd_pcm_substream_t *substream,
616 int cmd)
617{
618 ca0106_t *emu = snd_pcm_substream_chip(substream);
619 snd_pcm_runtime_t *runtime = substream->runtime;
620 ca0106_pcm_t *epcm = runtime->private_data;
621 int channel = epcm->channel_id;
622 int result = 0;
623
624 switch (cmd) {
625 case SNDRV_PCM_TRIGGER_START:
626 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
627 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
628 epcm->running = 1;
629 break;
630 case SNDRV_PCM_TRIGGER_STOP:
631 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
632 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
633 epcm->running = 0;
634 break;
635 default:
636 result = -EINVAL;
637 break;
638 }
639 return result;
640}
641
642/* pointer_playback callback */
643static snd_pcm_uframes_t
644snd_ca0106_pcm_pointer_playback(snd_pcm_substream_t *substream)
645{
646 ca0106_t *emu = snd_pcm_substream_chip(substream);
647 snd_pcm_runtime_t *runtime = substream->runtime;
648 ca0106_pcm_t *epcm = runtime->private_data;
649 snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
650 int channel = epcm->channel_id;
651
652 if (!epcm->running)
653 return 0;
654
655 ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
656 ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
657 ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
658 if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
659 ptr2 = bytes_to_frames(runtime, ptr1);
660 ptr2+= (ptr4 >> 3) * runtime->period_size;
661 ptr=ptr2;
662 if (ptr >= runtime->buffer_size)
663 ptr -= runtime->buffer_size;
664 //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);
665
666 return ptr;
667}
668
669/* pointer_capture callback */
670static snd_pcm_uframes_t
671snd_ca0106_pcm_pointer_capture(snd_pcm_substream_t *substream)
672{
673 ca0106_t *emu = snd_pcm_substream_chip(substream);
674 snd_pcm_runtime_t *runtime = substream->runtime;
675 ca0106_pcm_t *epcm = runtime->private_data;
676 snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
677 int channel = channel=epcm->channel_id;
678
679 if (!epcm->running)
680 return 0;
681
682 ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
683 ptr2 = bytes_to_frames(runtime, ptr1);
684 ptr=ptr2;
685 if (ptr >= runtime->buffer_size)
686 ptr -= runtime->buffer_size;
687 //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);
688
689 return ptr;
690}
691
692/* operators */
693static snd_pcm_ops_t snd_ca0106_playback_front_ops = {
694 .open = snd_ca0106_pcm_open_playback_front,
695 .close = snd_ca0106_pcm_close_playback,
696 .ioctl = snd_pcm_lib_ioctl,
697 .hw_params = snd_ca0106_pcm_hw_params_playback,
698 .hw_free = snd_ca0106_pcm_hw_free_playback,
699 .prepare = snd_ca0106_pcm_prepare_playback,
700 .trigger = snd_ca0106_pcm_trigger_playback,
701 .pointer = snd_ca0106_pcm_pointer_playback,
702};
703
704static snd_pcm_ops_t snd_ca0106_capture_0_ops = {
705 .open = snd_ca0106_pcm_open_0_capture,
706 .close = snd_ca0106_pcm_close_capture,
707 .ioctl = snd_pcm_lib_ioctl,
708 .hw_params = snd_ca0106_pcm_hw_params_capture,
709 .hw_free = snd_ca0106_pcm_hw_free_capture,
710 .prepare = snd_ca0106_pcm_prepare_capture,
711 .trigger = snd_ca0106_pcm_trigger_capture,
712 .pointer = snd_ca0106_pcm_pointer_capture,
713};
714
715static snd_pcm_ops_t snd_ca0106_capture_1_ops = {
716 .open = snd_ca0106_pcm_open_1_capture,
717 .close = snd_ca0106_pcm_close_capture,
718 .ioctl = snd_pcm_lib_ioctl,
719 .hw_params = snd_ca0106_pcm_hw_params_capture,
720 .hw_free = snd_ca0106_pcm_hw_free_capture,
721 .prepare = snd_ca0106_pcm_prepare_capture,
722 .trigger = snd_ca0106_pcm_trigger_capture,
723 .pointer = snd_ca0106_pcm_pointer_capture,
724};
725
726static snd_pcm_ops_t snd_ca0106_capture_2_ops = {
727 .open = snd_ca0106_pcm_open_2_capture,
728 .close = snd_ca0106_pcm_close_capture,
729 .ioctl = snd_pcm_lib_ioctl,
730 .hw_params = snd_ca0106_pcm_hw_params_capture,
731 .hw_free = snd_ca0106_pcm_hw_free_capture,
732 .prepare = snd_ca0106_pcm_prepare_capture,
733 .trigger = snd_ca0106_pcm_trigger_capture,
734 .pointer = snd_ca0106_pcm_pointer_capture,
735};
736
737static snd_pcm_ops_t snd_ca0106_capture_3_ops = {
738 .open = snd_ca0106_pcm_open_3_capture,
739 .close = snd_ca0106_pcm_close_capture,
740 .ioctl = snd_pcm_lib_ioctl,
741 .hw_params = snd_ca0106_pcm_hw_params_capture,
742 .hw_free = snd_ca0106_pcm_hw_free_capture,
743 .prepare = snd_ca0106_pcm_prepare_capture,
744 .trigger = snd_ca0106_pcm_trigger_capture,
745 .pointer = snd_ca0106_pcm_pointer_capture,
746};
747
748static snd_pcm_ops_t snd_ca0106_playback_center_lfe_ops = {
749 .open = snd_ca0106_pcm_open_playback_center_lfe,
750 .close = snd_ca0106_pcm_close_playback,
751 .ioctl = snd_pcm_lib_ioctl,
752 .hw_params = snd_ca0106_pcm_hw_params_playback,
753 .hw_free = snd_ca0106_pcm_hw_free_playback,
754 .prepare = snd_ca0106_pcm_prepare_playback,
755 .trigger = snd_ca0106_pcm_trigger_playback,
756 .pointer = snd_ca0106_pcm_pointer_playback,
757};
758
759static snd_pcm_ops_t snd_ca0106_playback_unknown_ops = {
760 .open = snd_ca0106_pcm_open_playback_unknown,
761 .close = snd_ca0106_pcm_close_playback,
762 .ioctl = snd_pcm_lib_ioctl,
763 .hw_params = snd_ca0106_pcm_hw_params_playback,
764 .hw_free = snd_ca0106_pcm_hw_free_playback,
765 .prepare = snd_ca0106_pcm_prepare_playback,
766 .trigger = snd_ca0106_pcm_trigger_playback,
767 .pointer = snd_ca0106_pcm_pointer_playback,
768};
769
770static snd_pcm_ops_t snd_ca0106_playback_rear_ops = {
771 .open = snd_ca0106_pcm_open_playback_rear,
772 .close = snd_ca0106_pcm_close_playback,
773 .ioctl = snd_pcm_lib_ioctl,
774 .hw_params = snd_ca0106_pcm_hw_params_playback,
775 .hw_free = snd_ca0106_pcm_hw_free_playback,
776 .prepare = snd_ca0106_pcm_prepare_playback,
777 .trigger = snd_ca0106_pcm_trigger_playback,
778 .pointer = snd_ca0106_pcm_pointer_playback,
779};
780
781
782static unsigned short snd_ca0106_ac97_read(ac97_t *ac97,
783 unsigned short reg)
784{
785 ca0106_t *emu = ac97->private_data;
786 unsigned long flags;
787 unsigned short val;
788
789 spin_lock_irqsave(&emu->emu_lock, flags);
790 outb(reg, emu->port + AC97ADDRESS);
791 val = inw(emu->port + AC97DATA);
792 spin_unlock_irqrestore(&emu->emu_lock, flags);
793 return val;
794}
795
796static void snd_ca0106_ac97_write(ac97_t *ac97,
797 unsigned short reg, unsigned short val)
798{
799 ca0106_t *emu = ac97->private_data;
800 unsigned long flags;
801
802 spin_lock_irqsave(&emu->emu_lock, flags);
803 outb(reg, emu->port + AC97ADDRESS);
804 outw(val, emu->port + AC97DATA);
805 spin_unlock_irqrestore(&emu->emu_lock, flags);
806}
807
808static int snd_ca0106_ac97(ca0106_t *chip)
809{
810 ac97_bus_t *pbus;
811 ac97_template_t ac97;
812 int err;
813 static ac97_bus_ops_t ops = {
814 .write = snd_ca0106_ac97_write,
815 .read = snd_ca0106_ac97_read,
816 };
817
818 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
819 return err;
820 pbus->no_vra = 1; /* we don't need VRA */
821
822 memset(&ac97, 0, sizeof(ac97));
823 ac97.private_data = chip;
Takashi Iwai36c4dc42005-03-24 17:48:30 +0100824 ac97.scaps = AC97_SCAP_NO_SPDIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
826}
827
828static int snd_ca0106_free(ca0106_t *chip)
829{
830 if (chip->res_port != NULL) { /* avoid access to already used hardware */
831 // disable interrupts
832 snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
833 outl(0, chip->port + INTE);
834 snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
835 udelay(1000);
836 // disable audio
837 //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
838 outl(0, chip->port + HCFG);
839 /* FIXME: We need to stop and DMA transfers here.
840 * But as I am not sure how yet, we cannot from the dma pages.
841 * So we can fix: snd-malloc: Memory leak? pages not freed = 8
842 */
843 }
844 // release the data
845#if 1
846 if (chip->buffer.area)
847 snd_dma_free_pages(&chip->buffer);
848#endif
849
850 // release the i/o port
851 if (chip->res_port) {
852 release_resource(chip->res_port);
853 kfree_nocheck(chip->res_port);
854 }
855 // release the irq
856 if (chip->irq >= 0)
857 free_irq(chip->irq, (void *)chip);
858 pci_disable_device(chip->pci);
859 kfree(chip);
860 return 0;
861}
862
863static int snd_ca0106_dev_free(snd_device_t *device)
864{
865 ca0106_t *chip = device->device_data;
866 return snd_ca0106_free(chip);
867}
868
869static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id,
870 struct pt_regs *regs)
871{
872 unsigned int status;
873
874 ca0106_t *chip = dev_id;
875 int i;
876 int mask;
877 unsigned int stat76;
878 ca0106_channel_t *pchannel;
879
880 spin_lock(&chip->emu_lock);
881
882 status = inl(chip->port + IPR);
883
884 // call updater, unlock before it
885 spin_unlock(&chip->emu_lock);
886
887 if (! status)
888 return IRQ_NONE;
889
890 stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
891 //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
892 //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
893 mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
894 for(i = 0; i < 4; i++) {
895 pchannel = &(chip->playback_channels[i]);
896 if(stat76 & mask) {
897/* FIXME: Select the correct substream for period elapsed */
898 if(pchannel->use) {
899 snd_pcm_period_elapsed(pchannel->epcm->substream);
900 //printk(KERN_INFO "interrupt [%d] used\n", i);
901 }
902 }
903 //printk(KERN_INFO "channel=%p\n",pchannel);
904 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
905 mask <<= 1;
906 }
907 mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
908 for(i = 0; i < 4; i++) {
909 pchannel = &(chip->capture_channels[i]);
910 if(stat76 & mask) {
911/* FIXME: Select the correct substream for period elapsed */
912 if(pchannel->use) {
913 snd_pcm_period_elapsed(pchannel->epcm->substream);
914 //printk(KERN_INFO "interrupt [%d] used\n", i);
915 }
916 }
917 //printk(KERN_INFO "channel=%p\n",pchannel);
918 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
919 mask <<= 1;
920 }
921
922 snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
923 spin_lock(&chip->emu_lock);
924 // acknowledge the interrupt if necessary
925 outl(status, chip->port+IPR);
926
927 spin_unlock(&chip->emu_lock);
928
929 return IRQ_HANDLED;
930}
931
932static void snd_ca0106_pcm_free(snd_pcm_t *pcm)
933{
934 ca0106_t *emu = pcm->private_data;
935 emu->pcm = NULL;
936 snd_pcm_lib_preallocate_free_for_all(pcm);
937}
938
939static int __devinit snd_ca0106_pcm(ca0106_t *emu, int device, snd_pcm_t **rpcm)
940{
941 snd_pcm_t *pcm;
942 snd_pcm_substream_t *substream;
943 int err;
944
945 if (rpcm)
946 *rpcm = NULL;
947 if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
948 return err;
949
950 pcm->private_data = emu;
951 pcm->private_free = snd_ca0106_pcm_free;
952
953 switch (device) {
954 case 0:
955 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
956 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
957 break;
958 case 1:
959 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
960 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
961 break;
962 case 2:
963 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
964 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
965 break;
966 case 3:
967 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
968 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
969 break;
970 }
971
972 pcm->info_flags = 0;
973 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
974 strcpy(pcm->name, "CA0106");
975 emu->pcm = pcm;
976
977 for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
978 substream;
979 substream = substream->next) {
980 if ((err = snd_pcm_lib_preallocate_pages(substream,
981 SNDRV_DMA_TYPE_DEV,
982 snd_dma_pci_data(emu->pci),
983 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
984 return err;
985 }
986
987 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
988 substream;
989 substream = substream->next) {
990 if ((err = snd_pcm_lib_preallocate_pages(substream,
991 SNDRV_DMA_TYPE_DEV,
992 snd_dma_pci_data(emu->pci),
993 64*1024, 64*1024)) < 0)
994 return err;
995 }
996
997 if (rpcm)
998 *rpcm = pcm;
999
1000 return 0;
1001}
1002
1003static int __devinit snd_ca0106_create(snd_card_t *card,
1004 struct pci_dev *pci,
1005 ca0106_t **rchip)
1006{
1007 ca0106_t *chip;
James Courtier-Dutton1baa7052005-05-21 22:35:58 +02001008 ca0106_details_t *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int err;
1010 int ch;
1011 static snd_device_ops_t ops = {
1012 .dev_free = snd_ca0106_dev_free,
1013 };
1014
1015 *rchip = NULL;
1016
1017 if ((err = pci_enable_device(pci)) < 0)
1018 return err;
1019 if (pci_set_dma_mask(pci, 0xffffffffUL) < 0 ||
1020 pci_set_consistent_dma_mask(pci, 0xffffffffUL) < 0) {
1021 printk(KERN_ERR "error to set 32bit mask DMA\n");
1022 pci_disable_device(pci);
1023 return -ENXIO;
1024 }
1025
1026 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1027 if (chip == NULL) {
1028 pci_disable_device(pci);
1029 return -ENOMEM;
1030 }
1031
1032 chip->card = card;
1033 chip->pci = pci;
1034 chip->irq = -1;
1035
1036 spin_lock_init(&chip->emu_lock);
1037
1038 chip->port = pci_resource_start(pci, 0);
1039 if ((chip->res_port = request_region(chip->port, 0x20,
1040 "snd_ca0106")) == NULL) {
1041 snd_ca0106_free(chip);
1042 printk(KERN_ERR "cannot allocate the port\n");
1043 return -EBUSY;
1044 }
1045
1046 if (request_irq(pci->irq, snd_ca0106_interrupt,
1047 SA_INTERRUPT|SA_SHIRQ, "snd_ca0106",
1048 (void *)chip)) {
1049 snd_ca0106_free(chip);
1050 printk(KERN_ERR "cannot grab irq\n");
1051 return -EBUSY;
1052 }
1053 chip->irq = pci->irq;
1054
1055 /* This stores the periods table. */
1056 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
1057 snd_ca0106_free(chip);
1058 return -ENOMEM;
1059 }
1060
1061 pci_set_master(pci);
1062 /* read revision & serial */
1063 pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
1064 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
1065 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
1066#if 1
1067 printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
1068 chip->revision, chip->serial);
1069#endif
James Courtier-Dutton1baa7052005-05-21 22:35:58 +02001070 strcpy(card->driver, "CA0106");
1071 strcpy(card->shortname, "CA0106");
1072
1073 for (c=ca0106_chip_details; c->serial; c++) {
1074 if (c->serial == chip->serial) break;
1075 }
1076 chip->details = c;
1077 sprintf(card->longname, "%s at 0x%lx irq %i",
1078 c->name, chip->port, chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 outl(0, chip->port + INTE);
1081
1082 /*
1083 * Init to 0x02109204 :
1084 * Clock accuracy = 0 (1000ppm)
1085 * Sample Rate = 2 (48kHz)
1086 * Audio Channel = 1 (Left of 2)
1087 * Source Number = 0 (Unspecified)
1088 * Generation Status = 1 (Original for Cat Code 12)
1089 * Cat Code = 12 (Digital Signal Mixer)
1090 * Mode = 0 (Mode 0)
1091 * Emphasis = 0 (None)
1092 * CP = 1 (Copyright unasserted)
1093 * AN = 0 (Audio data)
1094 * P = 0 (Consumer)
1095 */
1096 snd_ca0106_ptr_write(chip, SPCS0, 0,
1097 chip->spdif_bits[0] =
1098 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1099 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1100 SPCS_GENERATIONSTATUS | 0x00001200 |
1101 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1102 /* Only SPCS1 has been tested */
1103 snd_ca0106_ptr_write(chip, SPCS1, 0,
1104 chip->spdif_bits[1] =
1105 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1106 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1107 SPCS_GENERATIONSTATUS | 0x00001200 |
1108 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1109 snd_ca0106_ptr_write(chip, SPCS2, 0,
1110 chip->spdif_bits[2] =
1111 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1112 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1113 SPCS_GENERATIONSTATUS | 0x00001200 |
1114 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1115 snd_ca0106_ptr_write(chip, SPCS3, 0,
1116 chip->spdif_bits[3] =
1117 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1118 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1119 SPCS_GENERATIONSTATUS | 0x00001200 |
1120 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1121
1122 snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
1123 snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
1124
1125 /* Write 0x8000 to AC97_REC_GAIN to mute it. */
1126 outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
1127 outw(0x8000, chip->port + AC97DATA);
1128#if 0
1129 snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
1130 snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
1131 snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
1132 snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
1133#endif
1134
1135 //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
1136 /* Analog or Digital output */
1137 snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
James Courtier-Dutton8f55fbb2005-05-21 16:45:58 +02001138 snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 chip->spdif_enable = 0; /* Set digital SPDIF output off */
1140 chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1141 //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
1142 //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
1143
1144 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
1145 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
1146 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
1147 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
1148 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
1149 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
1150 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
1151 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
1152 for(ch = 0; ch < 4; ch++) {
1153 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
1154 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
1155 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
1156 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
1157 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
1158 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
1159 }
1160 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC, Line in, TAD in, AUX in */
1161 chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1162
James Courtier-Dutton1baa7052005-05-21 22:35:58 +02001163 if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1165 outl(0x0, chip->port+GPIO);
1166 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
James Courtier-Dutton8f55fbb2005-05-21 16:45:58 +02001167 outl(0x005f4301, chip->port+GPIO); /* Analog */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 } else {
1169 outl(0x0, chip->port+GPIO);
1170 outl(0x005f03a3, chip->port+GPIO); /* Analog */
1171 //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
1172 }
1173 snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
1174
1175 //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1176 //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
1177 //outl(0x00000009, chip->port+HCFG);
1178 outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
1179
1180 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1181 chip, &ops)) < 0) {
1182 snd_ca0106_free(chip);
1183 return err;
1184 }
1185 *rchip = chip;
1186 return 0;
1187}
1188
1189static int __devinit snd_ca0106_probe(struct pci_dev *pci,
1190 const struct pci_device_id *pci_id)
1191{
1192 static int dev;
1193 snd_card_t *card;
1194 ca0106_t *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int err;
1196
1197 if (dev >= SNDRV_CARDS)
1198 return -ENODEV;
1199 if (!enable[dev]) {
1200 dev++;
1201 return -ENOENT;
1202 }
1203
1204 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1205 if (card == NULL)
1206 return -ENOMEM;
1207
1208 if ((err = snd_ca0106_create(card, pci, &chip)) < 0) {
1209 snd_card_free(card);
1210 return err;
1211 }
1212
1213 if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
1214 snd_card_free(card);
1215 return err;
1216 }
1217 if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
1218 snd_card_free(card);
1219 return err;
1220 }
1221 if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
1222 snd_card_free(card);
1223 return err;
1224 }
1225 if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
1226 snd_card_free(card);
1227 return err;
1228 }
James Courtier-Dutton1baa7052005-05-21 22:35:58 +02001229 if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if ((err = snd_ca0106_ac97(chip)) < 0) {
1231 snd_card_free(card);
1232 return err;
1233 }
1234 }
1235 if ((err = snd_ca0106_mixer(chip)) < 0) {
1236 snd_card_free(card);
1237 return err;
1238 }
1239
1240 snd_ca0106_proc_init(chip);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if ((err = snd_card_register(card)) < 0) {
1243 snd_card_free(card);
1244 return err;
1245 }
1246
1247 pci_set_drvdata(pci, card);
1248 dev++;
1249 return 0;
1250}
1251
1252static void __devexit snd_ca0106_remove(struct pci_dev *pci)
1253{
1254 snd_card_free(pci_get_drvdata(pci));
1255 pci_set_drvdata(pci, NULL);
1256}
1257
1258// PCI IDs
1259static struct pci_device_id snd_ca0106_ids[] = {
1260 { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
1261 { 0, }
1262};
1263MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
1264
1265// pci_driver definition
1266static struct pci_driver driver = {
1267 .name = "CA0106",
1268 .id_table = snd_ca0106_ids,
1269 .probe = snd_ca0106_probe,
1270 .remove = __devexit_p(snd_ca0106_remove),
1271};
1272
1273// initialization of the module
1274static int __init alsa_card_ca0106_init(void)
1275{
1276 int err;
1277
Takashi Iwai01d25d42005-04-11 16:58:24 +02001278 if ((err = pci_register_driver(&driver)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return err;
1280
1281 return 0;
1282}
1283
1284// clean up the module
1285static void __exit alsa_card_ca0106_exit(void)
1286{
1287 pci_unregister_driver(&driver);
1288}
1289
1290module_init(alsa_card_ca0106_init)
1291module_exit(alsa_card_ca0106_exit)