[ALSA] Remove xxx_t typedefs: PCI AU88x0
[deliverable/linux.git] / sound / pci / ca0106 / ca0106_main.c
CommitLineData
1da177e4
LT
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
7199acdc 4 * Version: 0.0.23
1da177e4
LT
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
7199acdc
JCD
80 * 0.0.23
81 * Implement support for Line-in capture on SB Live 24bit.
1da177e4
LT
82 *
83 * BUGS:
84 * Some stability problems when unloading the snd-ca0106 kernel module.
85 * --
86 *
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)
98 *
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.
107 *
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.
115 *
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>
118 *
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.
123 *
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.
128 *
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
132 *
133 */
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>
299676b1 141#include <linux/dma-mapping.h>
1da177e4
LT
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>
147
148MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
149MODULE_DESCRIPTION("CA0106");
150MODULE_LICENSE("GPL");
151MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
152
153// module parameters (see "Module Parameters")
154static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
155static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
156static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
157
158module_param_array(index, int, NULL, 0444);
159MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
160module_param_array(id, charp, NULL, 0444);
161MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
162module_param_array(enable, bool, NULL, 0444);
163MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
164
165#include "ca0106.h"
166
1baa705b
JCD
167static ca0106_details_t 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]",
7199acdc
JCD
179 .gpio_type = 1,
180 .i2c_adc = 1 } ,
1baa705b
JCD
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]",
7199acdc
JCD
184 .gpio_type = 1,
185 .i2c_adc = 1 } ,
1baa705b
JCD
186 /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
187 { .serial = 0x10091462,
188 .name = "MSI K8N Diamond MB [SB0438]",
7199acdc
JCD
189 .gpio_type = 1,
190 .i2c_adc = 1 } ,
1b05962e
JCD
191 /* Shuttle XPC SD31P which has an onboard Creative Labs Sound Blaster Live! 24-bit EAX
192 * high-definition 7.1 audio processor".
193 * Added using info from andrewvegan in alsa bug #1298
194 */
195 { .serial = 0x30381297,
196 .name = "Shuttle XPC SD31P [SD31P]",
197 .gpio_type = 1,
198 .i2c_adc = 1 } ,
1baa705b
JCD
199 { .serial = 0,
200 .name = "AudigyLS [Unknown]" }
1da177e4
LT
201};
202
203/* hardware definition */
204static snd_pcm_hardware_t snd_ca0106_playback_hw = {
205 .info = (SNDRV_PCM_INFO_MMAP |
206 SNDRV_PCM_INFO_INTERLEAVED |
207 SNDRV_PCM_INFO_BLOCK_TRANSFER |
208 SNDRV_PCM_INFO_MMAP_VALID),
209 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
95a98265
TI
210 .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
211 SNDRV_PCM_RATE_192000),
1da177e4
LT
212 .rate_min = 48000,
213 .rate_max = 192000,
214 .channels_min = 2, //1,
215 .channels_max = 2, //6,
216 .buffer_bytes_max = ((65536 - 64) * 8),
217 .period_bytes_min = 64,
218 .period_bytes_max = (65536 - 64),
219 .periods_min = 2,
220 .periods_max = 8,
221 .fifo_size = 0,
222};
223
224static snd_pcm_hardware_t snd_ca0106_capture_hw = {
225 .info = (SNDRV_PCM_INFO_MMAP |
226 SNDRV_PCM_INFO_INTERLEAVED |
227 SNDRV_PCM_INFO_BLOCK_TRANSFER |
228 SNDRV_PCM_INFO_MMAP_VALID),
883130b4 229 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
95a98265
TI
230 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
231 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
883130b4
JCD
232 .rate_min = 44100,
233 .rate_max = 192000,
1da177e4
LT
234 .channels_min = 2,
235 .channels_max = 2,
236 .buffer_bytes_max = ((65536 - 64) * 8),
237 .period_bytes_min = 64,
238 .period_bytes_max = (65536 - 64),
239 .periods_min = 2,
240 .periods_max = 2,
241 .fifo_size = 0,
242};
243
244unsigned int snd_ca0106_ptr_read(ca0106_t * emu,
245 unsigned int reg,
246 unsigned int chn)
247{
248 unsigned long flags;
249 unsigned int regptr, val;
250
251 regptr = (reg << 16) | chn;
252
253 spin_lock_irqsave(&emu->emu_lock, flags);
254 outl(regptr, emu->port + PTR);
255 val = inl(emu->port + DATA);
256 spin_unlock_irqrestore(&emu->emu_lock, flags);
257 return val;
258}
259
260void snd_ca0106_ptr_write(ca0106_t *emu,
261 unsigned int reg,
262 unsigned int chn,
263 unsigned int data)
264{
265 unsigned int regptr;
266 unsigned long flags;
267
268 regptr = (reg << 16) | chn;
269
270 spin_lock_irqsave(&emu->emu_lock, flags);
271 outl(regptr, emu->port + PTR);
272 outl(data, emu->port + DATA);
273 spin_unlock_irqrestore(&emu->emu_lock, flags);
274}
275
7199acdc
JCD
276int snd_ca0106_i2c_write(ca0106_t *emu,
277 u32 reg,
278 u32 value)
279{
280 u32 tmp;
95a98265 281 int timeout = 0;
7199acdc
JCD
282 int status;
283 int retry;
95a98265 284 if ((reg > 0x7f) || (value > 0x1ff)) {
99b359ba 285 snd_printk(KERN_ERR "i2c_write: invalid values.\n");
7199acdc
JCD
286 return -EINVAL;
287 }
288
289 tmp = reg << 25 | value << 16;
8fabab15
JCD
290 /* Not sure what this I2C channel controls. */
291 /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
292
293 /* This controls the I2C connected to the WM8775 ADC Codec */
7199acdc
JCD
294 snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
295
95a98265 296 for (retry = 0; retry < 10; retry++) {
7199acdc
JCD
297 /* Send the data to i2c */
298 tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
299 tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
300 tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
301 snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
302
303 /* Wait till the transaction ends */
95a98265 304 while (1) {
7199acdc
JCD
305 status = snd_ca0106_ptr_read(emu, I2C_A, 0);
306 //snd_printk("I2C:status=0x%x\n", status);
307 timeout++;
95a98265 308 if ((status & I2C_A_ADC_START) == 0)
7199acdc
JCD
309 break;
310
95a98265 311 if (timeout > 1000)
7199acdc
JCD
312 break;
313 }
314 //Read back and see if the transaction is successful
95a98265 315 if ((status & I2C_A_ADC_ABORT) == 0)
7199acdc
JCD
316 break;
317 }
318
95a98265 319 if (retry == 10) {
99b359ba 320 snd_printk(KERN_ERR "Writing to ADC failed!\n");
7199acdc
JCD
321 return -EINVAL;
322 }
323
324 return 0;
325}
326
327
1da177e4
LT
328static void snd_ca0106_intr_enable(ca0106_t *emu, unsigned int intrenb)
329{
330 unsigned long flags;
331 unsigned int enable;
332
333 spin_lock_irqsave(&emu->emu_lock, flags);
334 enable = inl(emu->port + INTE) | intrenb;
335 outl(enable, emu->port + INTE);
336 spin_unlock_irqrestore(&emu->emu_lock, flags);
337}
338
8a5afd29
JCD
339static void snd_ca0106_intr_disable(ca0106_t *emu, unsigned int intrenb)
340{
341 unsigned long flags;
342 unsigned int enable;
343
344 spin_lock_irqsave(&emu->emu_lock, flags);
345 enable = inl(emu->port + INTE) & ~intrenb;
346 outl(enable, emu->port + INTE);
347 spin_unlock_irqrestore(&emu->emu_lock, flags);
348}
349
350
1da177e4
LT
351static void snd_ca0106_pcm_free_substream(snd_pcm_runtime_t *runtime)
352{
4d572776 353 kfree(runtime->private_data);
1da177e4
LT
354}
355
356/* open_playback callback */
357static int snd_ca0106_pcm_open_playback_channel(snd_pcm_substream_t *substream, int channel_id)
358{
359 ca0106_t *chip = snd_pcm_substream_chip(substream);
360 ca0106_channel_t *channel = &(chip->playback_channels[channel_id]);
361 ca0106_pcm_t *epcm;
362 snd_pcm_runtime_t *runtime = substream->runtime;
363 int err;
364
e560d8d8 365 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
366
367 if (epcm == NULL)
368 return -ENOMEM;
369 epcm->emu = chip;
370 epcm->substream = substream;
371 epcm->channel_id=channel_id;
372
373 runtime->private_data = epcm;
374 runtime->private_free = snd_ca0106_pcm_free_substream;
375
376 runtime->hw = snd_ca0106_playback_hw;
377
378 channel->emu = chip;
379 channel->number = channel_id;
380
95a98265 381 channel->use = 1;
1da177e4
LT
382 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
383 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
95a98265 384 channel->epcm = epcm;
1da177e4
LT
385 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
386 return err;
387 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
388 return err;
389 return 0;
390}
391
392/* close callback */
393static int snd_ca0106_pcm_close_playback(snd_pcm_substream_t *substream)
394{
395 ca0106_t *chip = snd_pcm_substream_chip(substream);
396 snd_pcm_runtime_t *runtime = substream->runtime;
397 ca0106_pcm_t *epcm = runtime->private_data;
398 chip->playback_channels[epcm->channel_id].use=0;
399/* FIXME: maybe zero others */
400 return 0;
401}
402
403static int snd_ca0106_pcm_open_playback_front(snd_pcm_substream_t *substream)
404{
405 return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
406}
407
408static int snd_ca0106_pcm_open_playback_center_lfe(snd_pcm_substream_t *substream)
409{
410 return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
411}
412
413static int snd_ca0106_pcm_open_playback_unknown(snd_pcm_substream_t *substream)
414{
415 return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
416}
417
418static int snd_ca0106_pcm_open_playback_rear(snd_pcm_substream_t *substream)
419{
420 return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
421}
422
423/* open_capture callback */
424static int snd_ca0106_pcm_open_capture_channel(snd_pcm_substream_t *substream, int channel_id)
425{
426 ca0106_t *chip = snd_pcm_substream_chip(substream);
427 ca0106_channel_t *channel = &(chip->capture_channels[channel_id]);
428 ca0106_pcm_t *epcm;
429 snd_pcm_runtime_t *runtime = substream->runtime;
430 int err;
431
e560d8d8 432 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4 433 if (epcm == NULL) {
99b359ba 434 snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
1da177e4
LT
435 return -ENOMEM;
436 }
437 epcm->emu = chip;
438 epcm->substream = substream;
439 epcm->channel_id=channel_id;
440
441 runtime->private_data = epcm;
442 runtime->private_free = snd_ca0106_pcm_free_substream;
443
444 runtime->hw = snd_ca0106_capture_hw;
445
446 channel->emu = chip;
447 channel->number = channel_id;
448
95a98265 449 channel->use = 1;
1da177e4
LT
450 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
451 //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
95a98265 452 channel->epcm = epcm;
1da177e4
LT
453 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
454 return err;
455 //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
456 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
457 return err;
458 return 0;
459}
460
461/* close callback */
462static int snd_ca0106_pcm_close_capture(snd_pcm_substream_t *substream)
463{
464 ca0106_t *chip = snd_pcm_substream_chip(substream);
465 snd_pcm_runtime_t *runtime = substream->runtime;
466 ca0106_pcm_t *epcm = runtime->private_data;
467 chip->capture_channels[epcm->channel_id].use=0;
468/* FIXME: maybe zero others */
469 return 0;
470}
471
472static int snd_ca0106_pcm_open_0_capture(snd_pcm_substream_t *substream)
473{
474 return snd_ca0106_pcm_open_capture_channel(substream, 0);
475}
476
477static int snd_ca0106_pcm_open_1_capture(snd_pcm_substream_t *substream)
478{
479 return snd_ca0106_pcm_open_capture_channel(substream, 1);
480}
481
482static int snd_ca0106_pcm_open_2_capture(snd_pcm_substream_t *substream)
483{
484 return snd_ca0106_pcm_open_capture_channel(substream, 2);
485}
486
487static int snd_ca0106_pcm_open_3_capture(snd_pcm_substream_t *substream)
488{
489 return snd_ca0106_pcm_open_capture_channel(substream, 3);
490}
491
492/* hw_params callback */
493static int snd_ca0106_pcm_hw_params_playback(snd_pcm_substream_t *substream,
494 snd_pcm_hw_params_t * hw_params)
495{
496 return snd_pcm_lib_malloc_pages(substream,
497 params_buffer_bytes(hw_params));
498}
499
500/* hw_free callback */
501static int snd_ca0106_pcm_hw_free_playback(snd_pcm_substream_t *substream)
502{
503 return snd_pcm_lib_free_pages(substream);
504}
505
506/* hw_params callback */
507static int snd_ca0106_pcm_hw_params_capture(snd_pcm_substream_t *substream,
508 snd_pcm_hw_params_t * hw_params)
509{
510 return snd_pcm_lib_malloc_pages(substream,
511 params_buffer_bytes(hw_params));
512}
513
514/* hw_free callback */
515static int snd_ca0106_pcm_hw_free_capture(snd_pcm_substream_t *substream)
516{
517 return snd_pcm_lib_free_pages(substream);
518}
519
520/* prepare playback callback */
521static int snd_ca0106_pcm_prepare_playback(snd_pcm_substream_t *substream)
522{
523 ca0106_t *emu = snd_pcm_substream_chip(substream);
524 snd_pcm_runtime_t *runtime = substream->runtime;
525 ca0106_pcm_t *epcm = runtime->private_data;
526 int channel = epcm->channel_id;
527 u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
528 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
529 u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
530 u32 hcfg_set = 0x00000000;
531 u32 hcfg;
532 u32 reg40_mask = 0x30000 << (channel<<1);
533 u32 reg40_set = 0;
534 u32 reg40;
535 /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
536 u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
537 u32 reg71_set = 0;
538 u32 reg71;
539 int i;
540
541 //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));
542 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
543 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
544 /* Rate can be set per channel. */
545 /* reg40 control host to fifo */
546 /* reg71 controls DAC rate. */
547 switch (runtime->rate) {
548 case 44100:
549 reg40_set = 0x10000 << (channel<<1);
550 reg71_set = 0x01010000;
551 break;
552 case 48000:
553 reg40_set = 0;
554 reg71_set = 0;
555 break;
556 case 96000:
557 reg40_set = 0x20000 << (channel<<1);
558 reg71_set = 0x02020000;
559 break;
560 case 192000:
561 reg40_set = 0x30000 << (channel<<1);
562 reg71_set = 0x03030000;
563 break;
564 default:
565 reg40_set = 0;
566 reg71_set = 0;
567 break;
568 }
569 /* Format is a global setting */
570 /* FIXME: Only let the first channel accessed set this. */
571 switch (runtime->format) {
572 case SNDRV_PCM_FORMAT_S16_LE:
573 hcfg_set = 0;
574 break;
575 case SNDRV_PCM_FORMAT_S32_LE:
576 hcfg_set = HCFG_PLAYBACK_S32_LE;
577 break;
578 default:
579 hcfg_set = 0;
580 break;
581 }
582 hcfg = inl(emu->port + HCFG) ;
583 hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
584 outl(hcfg, emu->port + HCFG);
585 reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
586 reg40 = (reg40 & ~reg40_mask) | reg40_set;
587 snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
588 reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
589 reg71 = (reg71 & ~reg71_mask) | reg71_set;
590 snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
591
592 /* FIXME: Check emu->buffer.size before actually writing to it. */
593 for(i=0; i < runtime->periods; i++) {
95a98265
TI
594 table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
595 table_base[i*2+1] = period_size_bytes << 16;
1da177e4
LT
596 }
597
598 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
599 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
600 snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
601 snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
602 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
603 /* FIXME test what 0 bytes does. */
604 snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
605 snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
606 snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
607 snd_ca0106_ptr_write(emu, 0x08, channel, 0);
608 snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
609#if 0
610 snd_ca0106_ptr_write(emu, SPCS0, 0,
611 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
612 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
613 SPCS_GENERATIONSTATUS | 0x00001200 |
614 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
615 }
616#endif
617
618 return 0;
619}
620
621/* prepare capture callback */
622static int snd_ca0106_pcm_prepare_capture(snd_pcm_substream_t *substream)
623{
624 ca0106_t *emu = snd_pcm_substream_chip(substream);
625 snd_pcm_runtime_t *runtime = substream->runtime;
626 ca0106_pcm_t *epcm = runtime->private_data;
627 int channel = epcm->channel_id;
883130b4
JCD
628 u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
629 u32 hcfg_set = 0x00000000;
630 u32 hcfg;
631 u32 over_sampling=0x2;
632 u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
633 u32 reg71_set = 0;
634 u32 reg71;
635
636 //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));
637 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
638 //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
639 /* reg71 controls ADC rate. */
640 switch (runtime->rate) {
641 case 44100:
642 reg71_set = 0x00004000;
643 break;
644 case 48000:
645 reg71_set = 0;
646 break;
647 case 96000:
648 reg71_set = 0x00008000;
649 over_sampling=0xa;
650 break;
651 case 192000:
652 reg71_set = 0x0000c000;
653 over_sampling=0xa;
654 break;
655 default:
656 reg71_set = 0;
657 break;
658 }
659 /* Format is a global setting */
660 /* FIXME: Only let the first channel accessed set this. */
661 switch (runtime->format) {
662 case SNDRV_PCM_FORMAT_S16_LE:
663 hcfg_set = 0;
664 break;
665 case SNDRV_PCM_FORMAT_S32_LE:
666 hcfg_set = HCFG_CAPTURE_S32_LE;
667 break;
668 default:
669 hcfg_set = 0;
670 break;
671 }
672 hcfg = inl(emu->port + HCFG) ;
673 hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
674 outl(hcfg, emu->port + HCFG);
675 reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
676 reg71 = (reg71 & ~reg71_mask) | reg71_set;
677 snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
678 if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
679 snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
680 }
681
682
1da177e4
LT
683 //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));
684 snd_ca0106_ptr_write(emu, 0x13, channel, 0);
685 snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
686 snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
687 snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
688
689 return 0;
690}
691
692/* trigger_playback callback */
693static int snd_ca0106_pcm_trigger_playback(snd_pcm_substream_t *substream,
694 int cmd)
695{
696 ca0106_t *emu = snd_pcm_substream_chip(substream);
697 snd_pcm_runtime_t *runtime;
698 ca0106_pcm_t *epcm;
699 int channel;
700 int result = 0;
701 struct list_head *pos;
702 snd_pcm_substream_t *s;
703 u32 basic = 0;
704 u32 extended = 0;
705 int running=0;
706
707 switch (cmd) {
708 case SNDRV_PCM_TRIGGER_START:
709 running=1;
710 break;
711 case SNDRV_PCM_TRIGGER_STOP:
712 default:
713 running=0;
714 break;
715 }
716 snd_pcm_group_for_each(pos, substream) {
717 s = snd_pcm_group_substream_entry(pos);
718 runtime = s->runtime;
719 epcm = runtime->private_data;
720 channel = epcm->channel_id;
721 //snd_printk("channel=%d\n",channel);
722 epcm->running = running;
723 basic |= (0x1<<channel);
724 extended |= (0x10<<channel);
725 snd_pcm_trigger_done(s, substream);
726 }
727 //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
728
729 switch (cmd) {
730 case SNDRV_PCM_TRIGGER_START:
731 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
732 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
733 break;
734 case SNDRV_PCM_TRIGGER_STOP:
735 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
736 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
737 break;
738 default:
739 result = -EINVAL;
740 break;
741 }
742 return result;
743}
744
745/* trigger_capture callback */
746static int snd_ca0106_pcm_trigger_capture(snd_pcm_substream_t *substream,
747 int cmd)
748{
749 ca0106_t *emu = snd_pcm_substream_chip(substream);
750 snd_pcm_runtime_t *runtime = substream->runtime;
751 ca0106_pcm_t *epcm = runtime->private_data;
752 int channel = epcm->channel_id;
753 int result = 0;
754
755 switch (cmd) {
756 case SNDRV_PCM_TRIGGER_START:
757 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
758 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
759 epcm->running = 1;
760 break;
761 case SNDRV_PCM_TRIGGER_STOP:
762 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
763 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
764 epcm->running = 0;
765 break;
766 default:
767 result = -EINVAL;
768 break;
769 }
770 return result;
771}
772
773/* pointer_playback callback */
774static snd_pcm_uframes_t
775snd_ca0106_pcm_pointer_playback(snd_pcm_substream_t *substream)
776{
777 ca0106_t *emu = snd_pcm_substream_chip(substream);
778 snd_pcm_runtime_t *runtime = substream->runtime;
779 ca0106_pcm_t *epcm = runtime->private_data;
780 snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
781 int channel = epcm->channel_id;
782
783 if (!epcm->running)
784 return 0;
785
786 ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
787 ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
788 ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
789 if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
790 ptr2 = bytes_to_frames(runtime, ptr1);
791 ptr2+= (ptr4 >> 3) * runtime->period_size;
792 ptr=ptr2;
793 if (ptr >= runtime->buffer_size)
794 ptr -= runtime->buffer_size;
795 //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);
796
797 return ptr;
798}
799
800/* pointer_capture callback */
801static snd_pcm_uframes_t
802snd_ca0106_pcm_pointer_capture(snd_pcm_substream_t *substream)
803{
804 ca0106_t *emu = snd_pcm_substream_chip(substream);
805 snd_pcm_runtime_t *runtime = substream->runtime;
806 ca0106_pcm_t *epcm = runtime->private_data;
807 snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
808 int channel = channel=epcm->channel_id;
809
810 if (!epcm->running)
811 return 0;
812
813 ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
814 ptr2 = bytes_to_frames(runtime, ptr1);
815 ptr=ptr2;
816 if (ptr >= runtime->buffer_size)
817 ptr -= runtime->buffer_size;
818 //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);
819
820 return ptr;
821}
822
823/* operators */
824static snd_pcm_ops_t snd_ca0106_playback_front_ops = {
825 .open = snd_ca0106_pcm_open_playback_front,
826 .close = snd_ca0106_pcm_close_playback,
827 .ioctl = snd_pcm_lib_ioctl,
828 .hw_params = snd_ca0106_pcm_hw_params_playback,
829 .hw_free = snd_ca0106_pcm_hw_free_playback,
830 .prepare = snd_ca0106_pcm_prepare_playback,
831 .trigger = snd_ca0106_pcm_trigger_playback,
832 .pointer = snd_ca0106_pcm_pointer_playback,
833};
834
835static snd_pcm_ops_t snd_ca0106_capture_0_ops = {
836 .open = snd_ca0106_pcm_open_0_capture,
837 .close = snd_ca0106_pcm_close_capture,
838 .ioctl = snd_pcm_lib_ioctl,
839 .hw_params = snd_ca0106_pcm_hw_params_capture,
840 .hw_free = snd_ca0106_pcm_hw_free_capture,
841 .prepare = snd_ca0106_pcm_prepare_capture,
842 .trigger = snd_ca0106_pcm_trigger_capture,
843 .pointer = snd_ca0106_pcm_pointer_capture,
844};
845
846static snd_pcm_ops_t snd_ca0106_capture_1_ops = {
847 .open = snd_ca0106_pcm_open_1_capture,
848 .close = snd_ca0106_pcm_close_capture,
849 .ioctl = snd_pcm_lib_ioctl,
850 .hw_params = snd_ca0106_pcm_hw_params_capture,
851 .hw_free = snd_ca0106_pcm_hw_free_capture,
852 .prepare = snd_ca0106_pcm_prepare_capture,
853 .trigger = snd_ca0106_pcm_trigger_capture,
854 .pointer = snd_ca0106_pcm_pointer_capture,
855};
856
857static snd_pcm_ops_t snd_ca0106_capture_2_ops = {
858 .open = snd_ca0106_pcm_open_2_capture,
859 .close = snd_ca0106_pcm_close_capture,
860 .ioctl = snd_pcm_lib_ioctl,
861 .hw_params = snd_ca0106_pcm_hw_params_capture,
862 .hw_free = snd_ca0106_pcm_hw_free_capture,
863 .prepare = snd_ca0106_pcm_prepare_capture,
864 .trigger = snd_ca0106_pcm_trigger_capture,
865 .pointer = snd_ca0106_pcm_pointer_capture,
866};
867
868static snd_pcm_ops_t snd_ca0106_capture_3_ops = {
869 .open = snd_ca0106_pcm_open_3_capture,
870 .close = snd_ca0106_pcm_close_capture,
871 .ioctl = snd_pcm_lib_ioctl,
872 .hw_params = snd_ca0106_pcm_hw_params_capture,
873 .hw_free = snd_ca0106_pcm_hw_free_capture,
874 .prepare = snd_ca0106_pcm_prepare_capture,
875 .trigger = snd_ca0106_pcm_trigger_capture,
876 .pointer = snd_ca0106_pcm_pointer_capture,
877};
878
879static snd_pcm_ops_t snd_ca0106_playback_center_lfe_ops = {
880 .open = snd_ca0106_pcm_open_playback_center_lfe,
881 .close = snd_ca0106_pcm_close_playback,
882 .ioctl = snd_pcm_lib_ioctl,
883 .hw_params = snd_ca0106_pcm_hw_params_playback,
884 .hw_free = snd_ca0106_pcm_hw_free_playback,
885 .prepare = snd_ca0106_pcm_prepare_playback,
886 .trigger = snd_ca0106_pcm_trigger_playback,
887 .pointer = snd_ca0106_pcm_pointer_playback,
888};
889
890static snd_pcm_ops_t snd_ca0106_playback_unknown_ops = {
891 .open = snd_ca0106_pcm_open_playback_unknown,
892 .close = snd_ca0106_pcm_close_playback,
893 .ioctl = snd_pcm_lib_ioctl,
894 .hw_params = snd_ca0106_pcm_hw_params_playback,
895 .hw_free = snd_ca0106_pcm_hw_free_playback,
896 .prepare = snd_ca0106_pcm_prepare_playback,
897 .trigger = snd_ca0106_pcm_trigger_playback,
898 .pointer = snd_ca0106_pcm_pointer_playback,
899};
900
901static snd_pcm_ops_t snd_ca0106_playback_rear_ops = {
902 .open = snd_ca0106_pcm_open_playback_rear,
903 .close = snd_ca0106_pcm_close_playback,
904 .ioctl = snd_pcm_lib_ioctl,
905 .hw_params = snd_ca0106_pcm_hw_params_playback,
906 .hw_free = snd_ca0106_pcm_hw_free_playback,
907 .prepare = snd_ca0106_pcm_prepare_playback,
908 .trigger = snd_ca0106_pcm_trigger_playback,
909 .pointer = snd_ca0106_pcm_pointer_playback,
910};
911
912
913static unsigned short snd_ca0106_ac97_read(ac97_t *ac97,
914 unsigned short reg)
915{
916 ca0106_t *emu = ac97->private_data;
917 unsigned long flags;
918 unsigned short val;
919
920 spin_lock_irqsave(&emu->emu_lock, flags);
921 outb(reg, emu->port + AC97ADDRESS);
922 val = inw(emu->port + AC97DATA);
923 spin_unlock_irqrestore(&emu->emu_lock, flags);
924 return val;
925}
926
927static void snd_ca0106_ac97_write(ac97_t *ac97,
928 unsigned short reg, unsigned short val)
929{
930 ca0106_t *emu = ac97->private_data;
931 unsigned long flags;
932
933 spin_lock_irqsave(&emu->emu_lock, flags);
934 outb(reg, emu->port + AC97ADDRESS);
935 outw(val, emu->port + AC97DATA);
936 spin_unlock_irqrestore(&emu->emu_lock, flags);
937}
938
939static int snd_ca0106_ac97(ca0106_t *chip)
940{
941 ac97_bus_t *pbus;
942 ac97_template_t ac97;
943 int err;
944 static ac97_bus_ops_t ops = {
945 .write = snd_ca0106_ac97_write,
946 .read = snd_ca0106_ac97_read,
947 };
948
949 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
950 return err;
951 pbus->no_vra = 1; /* we don't need VRA */
952
953 memset(&ac97, 0, sizeof(ac97));
954 ac97.private_data = chip;
36c4dc42 955 ac97.scaps = AC97_SCAP_NO_SPDIF;
1da177e4
LT
956 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
957}
958
959static int snd_ca0106_free(ca0106_t *chip)
960{
961 if (chip->res_port != NULL) { /* avoid access to already used hardware */
962 // disable interrupts
963 snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
964 outl(0, chip->port + INTE);
965 snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
966 udelay(1000);
967 // disable audio
968 //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
969 outl(0, chip->port + HCFG);
970 /* FIXME: We need to stop and DMA transfers here.
971 * But as I am not sure how yet, we cannot from the dma pages.
972 * So we can fix: snd-malloc: Memory leak? pages not freed = 8
973 */
974 }
975 // release the data
976#if 1
977 if (chip->buffer.area)
978 snd_dma_free_pages(&chip->buffer);
979#endif
980
981 // release the i/o port
b1d5776d
TI
982 release_and_free_resource(chip->res_port);
983
1da177e4
LT
984 // release the irq
985 if (chip->irq >= 0)
986 free_irq(chip->irq, (void *)chip);
987 pci_disable_device(chip->pci);
988 kfree(chip);
989 return 0;
990}
991
992static int snd_ca0106_dev_free(snd_device_t *device)
993{
994 ca0106_t *chip = device->device_data;
995 return snd_ca0106_free(chip);
996}
997
998static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id,
999 struct pt_regs *regs)
1000{
1001 unsigned int status;
1002
1003 ca0106_t *chip = dev_id;
1004 int i;
1005 int mask;
1006 unsigned int stat76;
1007 ca0106_channel_t *pchannel;
1008
1da177e4
LT
1009 status = inl(chip->port + IPR);
1010
1da177e4
LT
1011 if (! status)
1012 return IRQ_NONE;
1013
1014 stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
1015 //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
1016 //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
1017 mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
1018 for(i = 0; i < 4; i++) {
1019 pchannel = &(chip->playback_channels[i]);
95a98265 1020 if (stat76 & mask) {
1da177e4
LT
1021/* FIXME: Select the correct substream for period elapsed */
1022 if(pchannel->use) {
95a98265
TI
1023 snd_pcm_period_elapsed(pchannel->epcm->substream);
1024 //printk(KERN_INFO "interrupt [%d] used\n", i);
1da177e4
LT
1025 }
1026 }
1027 //printk(KERN_INFO "channel=%p\n",pchannel);
1028 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1029 mask <<= 1;
1030 }
1031 mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
1032 for(i = 0; i < 4; i++) {
1033 pchannel = &(chip->capture_channels[i]);
95a98265 1034 if (stat76 & mask) {
1da177e4
LT
1035/* FIXME: Select the correct substream for period elapsed */
1036 if(pchannel->use) {
95a98265
TI
1037 snd_pcm_period_elapsed(pchannel->epcm->substream);
1038 //printk(KERN_INFO "interrupt [%d] used\n", i);
1da177e4
LT
1039 }
1040 }
1041 //printk(KERN_INFO "channel=%p\n",pchannel);
1042 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1043 mask <<= 1;
1044 }
1045
1046 snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
8a5afd29
JCD
1047
1048 if (chip->midi.dev_id &&
95a98265 1049 (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
8a5afd29
JCD
1050 if (chip->midi.interrupt)
1051 chip->midi.interrupt(&chip->midi, status);
1052 else
1053 chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
1054 }
1055
1da177e4
LT
1056 // acknowledge the interrupt if necessary
1057 outl(status, chip->port+IPR);
1058
1da177e4
LT
1059 return IRQ_HANDLED;
1060}
1061
1da177e4
LT
1062static int __devinit snd_ca0106_pcm(ca0106_t *emu, int device, snd_pcm_t **rpcm)
1063{
1064 snd_pcm_t *pcm;
1065 snd_pcm_substream_t *substream;
1066 int err;
1067
1068 if (rpcm)
1069 *rpcm = NULL;
1070 if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
1071 return err;
1072
1073 pcm->private_data = emu;
1da177e4
LT
1074
1075 switch (device) {
1076 case 0:
1077 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
1078 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
1079 break;
1080 case 1:
1081 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
1082 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
1083 break;
1084 case 2:
1085 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
1086 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
1087 break;
1088 case 3:
1089 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
1090 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
1091 break;
1092 }
1093
1094 pcm->info_flags = 0;
1095 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1096 strcpy(pcm->name, "CA0106");
1097 emu->pcm = pcm;
1098
1099 for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1100 substream;
1101 substream = substream->next) {
1102 if ((err = snd_pcm_lib_preallocate_pages(substream,
1103 SNDRV_DMA_TYPE_DEV,
1104 snd_dma_pci_data(emu->pci),
1105 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
1106 return err;
1107 }
1108
1109 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
1110 substream;
1111 substream = substream->next) {
1112 if ((err = snd_pcm_lib_preallocate_pages(substream,
1113 SNDRV_DMA_TYPE_DEV,
1114 snd_dma_pci_data(emu->pci),
1115 64*1024, 64*1024)) < 0)
1116 return err;
1117 }
1118
1119 if (rpcm)
1120 *rpcm = pcm;
1121
1122 return 0;
1123}
1124
1125static int __devinit snd_ca0106_create(snd_card_t *card,
1126 struct pci_dev *pci,
1127 ca0106_t **rchip)
1128{
1129 ca0106_t *chip;
1baa705b 1130 ca0106_details_t *c;
1da177e4
LT
1131 int err;
1132 int ch;
1133 static snd_device_ops_t ops = {
1134 .dev_free = snd_ca0106_dev_free,
1135 };
1136
1137 *rchip = NULL;
1138
1139 if ((err = pci_enable_device(pci)) < 0)
1140 return err;
299676b1
TK
1141 if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
1142 pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
1da177e4
LT
1143 printk(KERN_ERR "error to set 32bit mask DMA\n");
1144 pci_disable_device(pci);
1145 return -ENXIO;
1146 }
1147
e560d8d8 1148 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4
LT
1149 if (chip == NULL) {
1150 pci_disable_device(pci);
1151 return -ENOMEM;
1152 }
1153
1154 chip->card = card;
1155 chip->pci = pci;
1156 chip->irq = -1;
1157
1158 spin_lock_init(&chip->emu_lock);
1159
1160 chip->port = pci_resource_start(pci, 0);
1161 if ((chip->res_port = request_region(chip->port, 0x20,
1162 "snd_ca0106")) == NULL) {
1163 snd_ca0106_free(chip);
1164 printk(KERN_ERR "cannot allocate the port\n");
1165 return -EBUSY;
1166 }
1167
1168 if (request_irq(pci->irq, snd_ca0106_interrupt,
1169 SA_INTERRUPT|SA_SHIRQ, "snd_ca0106",
1170 (void *)chip)) {
1171 snd_ca0106_free(chip);
1172 printk(KERN_ERR "cannot grab irq\n");
1173 return -EBUSY;
1174 }
1175 chip->irq = pci->irq;
1176
1177 /* This stores the periods table. */
1178 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
1179 snd_ca0106_free(chip);
1180 return -ENOMEM;
1181 }
1182
1183 pci_set_master(pci);
1184 /* read revision & serial */
1185 pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
1186 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
1187 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
1188#if 1
1189 printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
1190 chip->revision, chip->serial);
1191#endif
1baa705b
JCD
1192 strcpy(card->driver, "CA0106");
1193 strcpy(card->shortname, "CA0106");
1194
95a98265
TI
1195 for (c = ca0106_chip_details; c->serial; c++) {
1196 if (c->serial == chip->serial)
1197 break;
1baa705b
JCD
1198 }
1199 chip->details = c;
1200 sprintf(card->longname, "%s at 0x%lx irq %i",
1201 c->name, chip->port, chip->irq);
1da177e4
LT
1202
1203 outl(0, chip->port + INTE);
1204
1205 /*
1206 * Init to 0x02109204 :
1207 * Clock accuracy = 0 (1000ppm)
1208 * Sample Rate = 2 (48kHz)
1209 * Audio Channel = 1 (Left of 2)
1210 * Source Number = 0 (Unspecified)
1211 * Generation Status = 1 (Original for Cat Code 12)
1212 * Cat Code = 12 (Digital Signal Mixer)
1213 * Mode = 0 (Mode 0)
1214 * Emphasis = 0 (None)
1215 * CP = 1 (Copyright unasserted)
1216 * AN = 0 (Audio data)
1217 * P = 0 (Consumer)
1218 */
1219 snd_ca0106_ptr_write(chip, SPCS0, 0,
1220 chip->spdif_bits[0] =
1221 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1222 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1223 SPCS_GENERATIONSTATUS | 0x00001200 |
1224 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1225 /* Only SPCS1 has been tested */
1226 snd_ca0106_ptr_write(chip, SPCS1, 0,
1227 chip->spdif_bits[1] =
1228 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1229 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1230 SPCS_GENERATIONSTATUS | 0x00001200 |
1231 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1232 snd_ca0106_ptr_write(chip, SPCS2, 0,
1233 chip->spdif_bits[2] =
1234 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1235 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1236 SPCS_GENERATIONSTATUS | 0x00001200 |
1237 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1238 snd_ca0106_ptr_write(chip, SPCS3, 0,
1239 chip->spdif_bits[3] =
1240 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1241 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1242 SPCS_GENERATIONSTATUS | 0x00001200 |
1243 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1244
1245 snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
1246 snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
1247
1248 /* Write 0x8000 to AC97_REC_GAIN to mute it. */
1249 outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
1250 outw(0x8000, chip->port + AC97DATA);
1251#if 0
1252 snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
1253 snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
1254 snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
1255 snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
1256#endif
1257
1258 //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
1259 /* Analog or Digital output */
1260 snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
8f55fbb0 1261 snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
1da177e4
LT
1262 chip->spdif_enable = 0; /* Set digital SPDIF output off */
1263 chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1264 //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
1265 //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
1266
1267 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
1268 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
1269 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
1270 snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
1271 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
1272 snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
1273 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
1274 snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
1275 for(ch = 0; ch < 4; ch++) {
1276 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
1277 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
1278 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
1279 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
1280 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
1281 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
1282 }
1283 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC, Line in, TAD in, AUX in */
1284 chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1285
1baa705b 1286 if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
1da177e4
LT
1287 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1288 outl(0x0, chip->port+GPIO);
1289 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
c82bf829 1290 outl(0x005f5301, chip->port+GPIO); /* Analog */
1da177e4
LT
1291 } else {
1292 outl(0x0, chip->port+GPIO);
1293 outl(0x005f03a3, chip->port+GPIO); /* Analog */
1294 //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
1295 }
1296 snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
1297
1298 //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1299 //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
1300 //outl(0x00000009, chip->port+HCFG);
1301 outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
1302
7199acdc
JCD
1303 if (chip->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
1304 snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
1305 }
1306
1da177e4
LT
1307 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1308 chip, &ops)) < 0) {
1309 snd_ca0106_free(chip);
1310 return err;
1311 }
1312 *rchip = chip;
1313 return 0;
1314}
1315
8a5afd29 1316
7cf4783b
JCD
1317static void ca0106_midi_interrupt_enable(ca_midi_t *midi, int intr)
1318{
8a5afd29
JCD
1319 snd_ca0106_intr_enable((ca0106_t *)(midi->dev_id), intr);
1320}
1321
7cf4783b
JCD
1322static void ca0106_midi_interrupt_disable(ca_midi_t *midi, int intr)
1323{
8a5afd29
JCD
1324 snd_ca0106_intr_disable((ca0106_t *)(midi->dev_id), intr);
1325}
1326
7cf4783b
JCD
1327static unsigned char ca0106_midi_read(ca_midi_t *midi, int idx)
1328{
8a5afd29
JCD
1329 return (unsigned char)snd_ca0106_ptr_read((ca0106_t *)(midi->dev_id), midi->port + idx, 0);
1330}
1331
7cf4783b
JCD
1332static void ca0106_midi_write(ca_midi_t *midi, int data, int idx)
1333{
8a5afd29
JCD
1334 snd_ca0106_ptr_write((ca0106_t *)(midi->dev_id), midi->port + idx, 0, data);
1335}
1336
7cf4783b
JCD
1337static snd_card_t *ca0106_dev_id_card(void *dev_id)
1338{
8a5afd29
JCD
1339 return ((ca0106_t *)dev_id)->card;
1340}
1341
7cf4783b
JCD
1342static int ca0106_dev_id_port(void *dev_id)
1343{
8a5afd29
JCD
1344 return ((ca0106_t *)dev_id)->port;
1345}
1346
1347static int __devinit snd_ca0106_midi(ca0106_t *chip, unsigned int channel)
1348{
1349 ca_midi_t *midi;
1350 char *name;
1351 int err;
1352
95a98265 1353 if (channel == CA0106_MIDI_CHAN_B) {
8a5afd29
JCD
1354 name = "CA0106 MPU-401 (UART) B";
1355 midi = &chip->midi2;
1356 midi->tx_enable = INTE_MIDI_TX_B;
1357 midi->rx_enable = INTE_MIDI_RX_B;
1358 midi->ipr_tx = IPR_MIDI_TX_B;
1359 midi->ipr_rx = IPR_MIDI_RX_B;
1360 midi->port = MIDI_UART_B_DATA;
1361 } else {
1362 name = "CA0106 MPU-401 (UART)";
1363 midi = &chip->midi;
1364 midi->tx_enable = INTE_MIDI_TX_A;
1365 midi->rx_enable = INTE_MIDI_TX_B;
1366 midi->ipr_tx = IPR_MIDI_TX_A;
1367 midi->ipr_rx = IPR_MIDI_RX_A;
1368 midi->port = MIDI_UART_A_DATA;
1369 }
1370
1371 midi->reset = CA0106_MPU401_RESET;
1372 midi->enter_uart = CA0106_MPU401_ENTER_UART;
1373 midi->ack = CA0106_MPU401_ACK;
1374
1375 midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
1376 midi->output_ready = CA0106_MIDI_OUTPUT_READY;
1377
1378 midi->channel = channel;
1379
1380 midi->interrupt_enable = ca0106_midi_interrupt_enable;
1381 midi->interrupt_disable = ca0106_midi_interrupt_disable;
1382
1383 midi->read = ca0106_midi_read;
1384 midi->write = ca0106_midi_write;
1385
1386 midi->get_dev_id_card = ca0106_dev_id_card;
1387 midi->get_dev_id_port = ca0106_dev_id_port;
1388
1389 midi->dev_id = chip;
1390
1391 if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
1392 return err;
1393
1394 return 0;
1395}
1396
1397
1da177e4
LT
1398static int __devinit snd_ca0106_probe(struct pci_dev *pci,
1399 const struct pci_device_id *pci_id)
1400{
1401 static int dev;
1402 snd_card_t *card;
1403 ca0106_t *chip;
1da177e4
LT
1404 int err;
1405
1406 if (dev >= SNDRV_CARDS)
1407 return -ENODEV;
1408 if (!enable[dev]) {
1409 dev++;
1410 return -ENOENT;
1411 }
1412
1413 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1414 if (card == NULL)
1415 return -ENOMEM;
1416
1417 if ((err = snd_ca0106_create(card, pci, &chip)) < 0) {
1418 snd_card_free(card);
1419 return err;
1420 }
1421
1422 if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
1423 snd_card_free(card);
1424 return err;
1425 }
1426 if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
1427 snd_card_free(card);
1428 return err;
1429 }
1430 if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
1431 snd_card_free(card);
1432 return err;
1433 }
1434 if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
1435 snd_card_free(card);
1436 return err;
1437 }
1baa705b 1438 if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
1da177e4
LT
1439 if ((err = snd_ca0106_ac97(chip)) < 0) {
1440 snd_card_free(card);
1441 return err;
1442 }
1443 }
1444 if ((err = snd_ca0106_mixer(chip)) < 0) {
1445 snd_card_free(card);
1446 return err;
1447 }
1448
7cf4783b 1449 snd_printdd("ca0106: probe for MIDI channel A ...");
8a5afd29
JCD
1450 if ((err = snd_ca0106_midi(chip,CA0106_MIDI_CHAN_A)) < 0) {
1451 snd_card_free(card);
7cf4783b 1452 snd_printdd(" failed, err=0x%x\n",err);
8a5afd29
JCD
1453 return err;
1454 }
7cf4783b 1455 snd_printdd(" done.\n");
8a5afd29 1456
1da177e4
LT
1457 snd_ca0106_proc_init(chip);
1458
1da177e4
LT
1459 if ((err = snd_card_register(card)) < 0) {
1460 snd_card_free(card);
1461 return err;
1462 }
1463
1464 pci_set_drvdata(pci, card);
1465 dev++;
1466 return 0;
1467}
1468
1469static void __devexit snd_ca0106_remove(struct pci_dev *pci)
1470{
1471 snd_card_free(pci_get_drvdata(pci));
1472 pci_set_drvdata(pci, NULL);
1473}
1474
1475// PCI IDs
1476static struct pci_device_id snd_ca0106_ids[] = {
1477 { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
1478 { 0, }
1479};
1480MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
1481
1482// pci_driver definition
1483static struct pci_driver driver = {
1484 .name = "CA0106",
1485 .id_table = snd_ca0106_ids,
1486 .probe = snd_ca0106_probe,
1487 .remove = __devexit_p(snd_ca0106_remove),
1488};
1489
1490// initialization of the module
1491static int __init alsa_card_ca0106_init(void)
1492{
95a98265 1493 return pci_register_driver(&driver);
1da177e4
LT
1494}
1495
1496// clean up the module
1497static void __exit alsa_card_ca0106_exit(void)
1498{
1499 pci_unregister_driver(&driver);
1500}
1501
1502module_init(alsa_card_ca0106_init)
1503module_exit(alsa_card_ca0106_exit)
This page took 0.145937 seconds and 5 git commands to generate.