2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Abramo Bagnara <abramo@alsa-project.org>
5 * Routines for control of Cirrus Logic CS461x chips
8 * - Sometimes the SPDIF input DSP tasks get's unsynchronized
9 * and the SPDIF get somewhat "distorcionated", or/and left right channel
10 * are swapped. To get around this problem when it happens, mute and unmute
11 * the SPDIF input mixer controll.
12 * - On the Hercules Game Theater XP the amplifier are sometimes turned
13 * off on inadecuate moments which causes distorcions on sound.
16 * - Secondary CODEC on some soundcards
17 * - SPDIF input support for other sample rates then 48khz
18 * - Posibility to mix the SPDIF output with analog sources.
19 * - PCM channels for Center and LFE on secondary codec
21 * NOTE: with CONFIG_SND_CS46XX_NEW_DSP unset uses old DSP image (which
22 * is default configuration), no SPDIF, no secondary codec, no
23 * multi channel PCM. But known to work.
25 * FINALLY: A credit to the developers Tom and Jordan
26 * at Cirrus for have helping me out with the DSP, however we
27 * still don't have sufficient documentation and technical
28 * references to be able to implement all fancy feutures
29 * supported by the cs46xx DSP's.
30 * Benny <benny@hostmobility.com>
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48 #include <sound/driver.h>
49 #include <linux/delay.h>
50 #include <linux/pci.h>
52 #include <linux/init.h>
53 #include <linux/interrupt.h>
54 #include <linux/slab.h>
55 #include <linux/gameport.h>
57 #include <sound/core.h>
58 #include <sound/control.h>
59 #include <sound/info.h>
60 #include <sound/pcm.h>
61 #include <sound/pcm_params.h>
62 #include <sound/cs46xx.h>
66 #include "cs46xx_lib.h"
69 static void amp_voyetra(cs46xx_t *chip, int change);
71 static unsigned short snd_cs46xx_codec_read(cs46xx_t *chip,
76 unsigned short result,tmp;
78 snd_assert ( (codec_index == CS46XX_PRIMARY_CODEC_INDEX) ||
79 (codec_index == CS46XX_SECONDARY_CODEC_INDEX),
82 if (codec_index == CS46XX_SECONDARY_CODEC_INDEX)
83 offset = CS46XX_SECONDARY_CODEC_OFFSET;
86 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
87 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
88 * 3. Write ACCTL = Control Register = 460h for initiating the write7---55
89 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
90 * 5. if DCV not cleared, break and return error
91 * 6. Read ACSTS = Status Register = 464h, check VSTS bit
94 snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
96 tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL);
97 if ((tmp & ACCTL_VFRM) == 0) {
98 snd_printk(KERN_WARNING "cs46xx: ACCTL_VFRM not set 0x%x\n",tmp);
99 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, (tmp & (~ACCTL_ESYN)) | ACCTL_VFRM );
101 tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL + offset);
102 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, tmp | ACCTL_ESYN | ACCTL_VFRM );
107 * Setup the AC97 control registers on the CS461x to send the
108 * appropriate command to the AC97 to perform the read.
109 * ACCAD = Command Address Register = 46Ch
110 * ACCDA = Command Data Register = 470h
111 * ACCTL = Control Register = 460h
112 * set DCV - will clear when process completed
113 * set CRW - Read command
114 * set VFRM - valid frame enabled
115 * set ESYN - ASYNC generation enabled
116 * set RSTN - ARST# inactive, AC97 codec not reset
119 snd_cs46xx_pokeBA0(chip, BA0_ACCAD, reg);
120 snd_cs46xx_pokeBA0(chip, BA0_ACCDA, 0);
121 if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
122 snd_cs46xx_pokeBA0(chip, BA0_ACCTL,/* clear ACCTL_DCV */ ACCTL_CRW |
123 ACCTL_VFRM | ACCTL_ESYN |
125 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
126 ACCTL_VFRM | ACCTL_ESYN |
129 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
130 ACCTL_CRW | ACCTL_VFRM | ACCTL_ESYN |
135 * Wait for the read to occur.
137 for (count = 0; count < 1000; count++) {
139 * First, we want to wait for a short time.
143 * Now, check to see if the read has completed.
144 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
146 if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV))
150 snd_printk("AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg);
156 * Wait for the valid status bit to go active.
158 for (count = 0; count < 100; count++) {
160 * Read the AC97 status register.
161 * ACSTS = Status Register = 464h
162 * VSTS - Valid Status
164 if (snd_cs46xx_peekBA0(chip, BA0_ACSTS + offset) & ACSTS_VSTS)
169 snd_printk("AC'97 read problem (ACSTS_VSTS), codec_index %d, reg = 0x%x\n", codec_index, reg);
175 * Read the data returned from the AC97 register.
176 * ACSDA = Status Data Register = 474h
179 printk("e) reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", reg,
180 snd_cs46xx_peekBA0(chip, BA0_ACSDA),
181 snd_cs46xx_peekBA0(chip, BA0_ACCAD));
184 //snd_cs46xx_peekBA0(chip, BA0_ACCAD);
185 result = snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
190 static unsigned short snd_cs46xx_ac97_read(ac97_t * ac97,
193 cs46xx_t *chip = snd_magic_cast(cs46xx_t, ac97->private_data, return -ENXIO);
195 int codec_index = -1;
197 /* UGGLY: nr_ac97_codecs == 0 primery codec detection is in progress */
198 if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] || chip->nr_ac97_codecs == 0)
199 codec_index = CS46XX_PRIMARY_CODEC_INDEX;
200 /* UGGLY: nr_ac97_codecs == 1 secondary codec detection is in progress */
201 else if (ac97 == chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] || chip->nr_ac97_codecs == 1)
202 codec_index = CS46XX_SECONDARY_CODEC_INDEX;
204 snd_assert(0, return 0xffff);
205 chip->active_ctrl(chip, 1);
206 val = snd_cs46xx_codec_read(chip, reg, codec_index);
207 chip->active_ctrl(chip, -1);
209 /* HACK: voyetra uses EAPD bit in the reverse way.
210 * we flip the bit to show the mixer status correctly
212 if (reg == AC97_POWERDOWN && chip->amplifier_ctrl == amp_voyetra)
219 static void snd_cs46xx_codec_write(cs46xx_t *chip,
226 snd_assert ((codec_index == CS46XX_PRIMARY_CODEC_INDEX) ||
227 (codec_index == CS46XX_SECONDARY_CODEC_INDEX),
231 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
232 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
233 * 3. Write ACCTL = Control Register = 460h for initiating the write
234 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
235 * 5. if DCV not cleared, break and return error
239 * Setup the AC97 control registers on the CS461x to send the
240 * appropriate command to the AC97 to perform the read.
241 * ACCAD = Command Address Register = 46Ch
242 * ACCDA = Command Data Register = 470h
243 * ACCTL = Control Register = 460h
244 * set DCV - will clear when process completed
245 * reset CRW - Write command
246 * set VFRM - valid frame enabled
247 * set ESYN - ASYNC generation enabled
248 * set RSTN - ARST# inactive, AC97 codec not reset
250 snd_cs46xx_pokeBA0(chip, BA0_ACCAD , reg);
251 snd_cs46xx_pokeBA0(chip, BA0_ACCDA , val);
252 snd_cs46xx_peekBA0(chip, BA0_ACCTL);
254 if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
255 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, /* clear ACCTL_DCV */ ACCTL_VFRM |
256 ACCTL_ESYN | ACCTL_RSTN);
257 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
258 ACCTL_ESYN | ACCTL_RSTN);
260 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
261 ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
264 for (count = 0; count < 4000; count++) {
266 * First, we want to wait for a short time.
270 * Now, check to see if the write has completed.
271 * ACCTL = 460h, DCV should be reset by now and 460h = 07h
273 if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV)) {
277 snd_printk("AC'97 write problem, codec_index = %d, reg = 0x%x, val = 0x%x\n", codec_index, reg, val);
280 static void snd_cs46xx_ac97_write(ac97_t *ac97,
284 cs46xx_t *chip = snd_magic_cast(cs46xx_t, ac97->private_data, return);
285 int codec_index = -1;
287 /* UGGLY: nr_ac97_codecs == 0 primery codec detection is in progress */
288 if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] || chip->nr_ac97_codecs == 0)
289 codec_index = CS46XX_PRIMARY_CODEC_INDEX;
290 /* UGGLY: nr_ac97_codecs == 1 secondary codec detection is in progress */
291 else if (ac97 == chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] || chip->nr_ac97_codecs == 1)
292 codec_index = CS46XX_SECONDARY_CODEC_INDEX;
294 snd_assert(0,return);
296 /* HACK: voyetra uses EAPD bit in the reverse way.
297 * we flip the bit to show the mixer status correctly
299 if (reg == AC97_POWERDOWN && chip->amplifier_ctrl == amp_voyetra)
302 chip->active_ctrl(chip, 1);
303 snd_cs46xx_codec_write(chip, reg, val, codec_index);
304 chip->active_ctrl(chip, -1);
309 * Chip initialization
312 int snd_cs46xx_download(cs46xx_t *chip,
314 unsigned long offset,
318 unsigned int bank = offset >> 16;
319 offset = offset & 0xffff;
321 snd_assert(!(offset & 3) && !(len & 3), return -EINVAL);
322 dst = chip->region.idx[bank+1].remap_addr + offset;
325 /* writel already converts 32-bit value to right endianess */
333 #ifdef CONFIG_SND_CS46XX_NEW_DSP
335 #include "imgs/cwc4630.h"
336 #include "imgs/cwcasync.h"
337 #include "imgs/cwcsnoop.h"
338 #include "imgs/cwcbinhack.h"
339 #include "imgs/cwcdma.h"
341 int snd_cs46xx_clear_BA1(cs46xx_t *chip,
342 unsigned long offset,
346 unsigned int bank = offset >> 16;
347 offset = offset & 0xffff;
349 snd_assert(!(offset & 3) && !(len & 3), return -EINVAL);
350 dst = chip->region.idx[bank+1].remap_addr + offset;
353 /* writel already converts 32-bit value to right endianess */
361 #else /* old DSP image */
363 #include "cs46xx_image.h"
365 int snd_cs46xx_download_image(cs46xx_t *chip)
368 unsigned long offset = 0;
370 for (idx = 0; idx < BA1_MEMORY_COUNT; idx++) {
371 if ((err = snd_cs46xx_download(chip,
372 &BA1Struct.map[offset],
373 BA1Struct.memory[idx].offset,
374 BA1Struct.memory[idx].size)) < 0)
376 offset += BA1Struct.memory[idx].size >> 2;
380 #endif /* CONFIG_SND_CS46XX_NEW_DSP */
386 static void snd_cs46xx_reset(cs46xx_t *chip)
391 * Write the reset bit of the SP control register.
393 snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RSTSP);
396 * Write the control register.
398 snd_cs46xx_poke(chip, BA1_SPCR, SPCR_DRQEN);
401 * Clear the trap registers.
403 for (idx = 0; idx < 8; idx++) {
404 snd_cs46xx_poke(chip, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
405 snd_cs46xx_poke(chip, BA1_TWPR, 0xFFFF);
407 snd_cs46xx_poke(chip, BA1_DREG, 0);
410 * Set the frame timer to reflect the number of cycles per frame.
412 snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
415 static int cs46xx_wait_for_fifo(cs46xx_t * chip,int retry_timeout)
419 * Make sure the previous FIFO write operation has completed.
421 for(i = 0; i < 50; i++){
422 status = snd_cs46xx_peekBA0(chip, BA0_SERBST);
424 if( !(status & SERBST_WBSY) )
427 mdelay(retry_timeout);
430 if(status & SERBST_WBSY) {
431 snd_printk( KERN_ERR "cs46xx: failure waiting for FIFO command to complete\n");
439 static void snd_cs46xx_clear_serial_FIFOs(cs46xx_t *chip)
441 int idx, powerdown = 0;
445 * See if the devices are powered down. If so, we must power them up first
446 * or they will not respond.
448 tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
449 if (!(tmp & CLKCR1_SWCE)) {
450 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
455 * We want to clear out the serial port FIFOs so we don't end up playing
456 * whatever random garbage happens to be in them. We fill the sample FIFOS
457 * with zero (silence).
459 snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0);
462 * Fill all 256 sample FIFO locations.
464 for (idx = 0; idx < 0xFF; idx++) {
466 * Make sure the previous FIFO write operation has completed.
468 if (cs46xx_wait_for_fifo(chip,1)) {
469 snd_printdd ("failed waiting for FIFO at addr (%02X)\n",idx);
472 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
477 * Write the serial port FIFO index.
479 snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
481 * Tell the serial port to load the new value into the FIFO location.
483 snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
486 * Now, if we powered up the devices, then power them back down again.
487 * This is kinda ugly, but should never happen.
490 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
493 static void snd_cs46xx_proc_start(cs46xx_t *chip)
498 * Set the frame timer to reflect the number of cycles per frame.
500 snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
502 * Turn on the run, run at frame, and DMA enable bits in the local copy of
503 * the SP control register.
505 snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
507 * Wait until the run at frame bit resets itself in the SP control
510 for (cnt = 0; cnt < 25; cnt++) {
512 if (!(snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR))
516 if (snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR)
517 snd_printk("SPCR_RUNFR never reset\n");
520 static void snd_cs46xx_proc_stop(cs46xx_t *chip)
523 * Turn off the run, run at frame, and DMA enable bits in the local copy of
524 * the SP control register.
526 snd_cs46xx_poke(chip, BA1_SPCR, 0);
530 * Sample rate routines
533 #define GOF_PER_SEC 200
535 static void snd_cs46xx_set_play_sample_rate(cs46xx_t *chip, unsigned int rate)
538 unsigned int tmp1, tmp2;
539 unsigned int phiIncr;
540 unsigned int correctionPerGOF, correctionPerSec;
543 * Compute the values used to drive the actual sample rate conversion.
544 * The following formulas are being computed, using inline assembly
545 * since we need to use 64 bit arithmetic to compute the values:
547 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
548 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
550 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
551 * GOF_PER_SEC * correctionPerGOF
555 * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
556 * correctionPerGOF:correctionPerSec =
557 * dividend:remainder(ulOther / GOF_PER_SEC)
560 phiIncr = tmp1 / 48000;
561 tmp1 -= phiIncr * 48000;
566 tmp1 -= tmp2 * 48000;
567 correctionPerGOF = tmp1 / GOF_PER_SEC;
568 tmp1 -= correctionPerGOF * GOF_PER_SEC;
569 correctionPerSec = tmp1;
572 * Fill in the SampleRateConverter control block.
574 spin_lock_irqsave(&chip->reg_lock, flags);
575 snd_cs46xx_poke(chip, BA1_PSRC,
576 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
577 snd_cs46xx_poke(chip, BA1_PPI, phiIncr);
578 spin_unlock_irqrestore(&chip->reg_lock, flags);
581 static void snd_cs46xx_set_capture_sample_rate(cs46xx_t *chip, unsigned int rate)
584 unsigned int phiIncr, coeffIncr, tmp1, tmp2;
585 unsigned int correctionPerGOF, correctionPerSec, initialDelay;
586 unsigned int frameGroupLength, cnt;
589 * We can only decimate by up to a factor of 1/9th the hardware rate.
590 * Correct the value if an attempt is made to stray outside that limit.
592 if ((rate * 9) < 48000)
596 * We can not capture at at rate greater than the Input Rate (48000).
597 * Return an error if an attempt is made to stray outside that limit.
603 * Compute the values used to drive the actual sample rate conversion.
604 * The following formulas are being computed, using inline assembly
605 * since we need to use 64 bit arithmetic to compute the values:
607 * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
608 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
609 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
611 * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
612 * GOF_PER_SEC * correctionPerGOF
613 * initialDelay = ceil((24 * Fs,in) / Fs,out)
617 * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
618 * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
619 * correctionPerGOF:correctionPerSec =
620 * dividend:remainder(ulOther / GOF_PER_SEC)
621 * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
625 coeffIncr = tmp1 / 48000;
626 tmp1 -= coeffIncr * 48000;
629 coeffIncr += tmp1 / 48000;
630 coeffIncr ^= 0xFFFFFFFF;
633 phiIncr = tmp1 / rate;
634 tmp1 -= phiIncr * rate;
640 correctionPerGOF = tmp1 / GOF_PER_SEC;
641 tmp1 -= correctionPerGOF * GOF_PER_SEC;
642 correctionPerSec = tmp1;
643 initialDelay = ((48000 * 24) + rate - 1) / rate;
646 * Fill in the VariDecimate control block.
648 spin_lock_irqsave(&chip->reg_lock, flags);
649 snd_cs46xx_poke(chip, BA1_CSRC,
650 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
651 snd_cs46xx_poke(chip, BA1_CCI, coeffIncr);
652 snd_cs46xx_poke(chip, BA1_CD,
653 (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
654 snd_cs46xx_poke(chip, BA1_CPI, phiIncr);
655 spin_unlock_irqrestore(&chip->reg_lock, flags);
658 * Figure out the frame group length for the write back task. Basically,
659 * this is just the factors of 24000 (2^6*3*5^3) that are not present in
660 * the output sample rate.
662 frameGroupLength = 1;
663 for (cnt = 2; cnt <= 64; cnt *= 2) {
664 if (((rate / cnt) * cnt) != rate)
665 frameGroupLength *= 2;
667 if (((rate / 3) * 3) != rate) {
668 frameGroupLength *= 3;
670 for (cnt = 5; cnt <= 125; cnt *= 5) {
671 if (((rate / cnt) * cnt) != rate)
672 frameGroupLength *= 5;
676 * Fill in the WriteBack control block.
678 spin_lock_irqsave(&chip->reg_lock, flags);
679 snd_cs46xx_poke(chip, BA1_CFG1, frameGroupLength);
680 snd_cs46xx_poke(chip, BA1_CFG2, (0x00800000 | frameGroupLength));
681 snd_cs46xx_poke(chip, BA1_CCST, 0x0000FFFF);
682 snd_cs46xx_poke(chip, BA1_CSPB, ((65536 * rate) / 24000));
683 snd_cs46xx_poke(chip, (BA1_CSPB + 4), 0x0000FFFF);
684 spin_unlock_irqrestore(&chip->reg_lock, flags);
691 static int snd_cs46xx_playback_transfer(snd_pcm_substream_t *substream)
693 /* cs46xx_t *chip = snd_pcm_substream_chip(substream); */
694 snd_pcm_runtime_t *runtime = substream->runtime;
695 cs46xx_pcm_t * cpcm = snd_magic_cast(cs46xx_pcm_t, runtime->private_data, return -ENXIO);
696 snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
697 snd_pcm_sframes_t diff = appl_ptr - cpcm->appl_ptr;
698 int buffer_size = runtime->period_size * CS46XX_FRAGS << cpcm->shift;
701 if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
702 diff += runtime->boundary;
703 cpcm->sw_ready += diff * (1 << cpcm->shift);
704 cpcm->appl_ptr = appl_ptr;
706 while (cpcm->hw_ready < buffer_size &&
707 cpcm->sw_ready > 0) {
708 size_t hw_to_end = buffer_size - cpcm->hw_data;
709 size_t sw_to_end = cpcm->sw_bufsize - cpcm->sw_data;
710 size_t bytes = buffer_size - cpcm->hw_ready;
711 if (cpcm->sw_ready < (int)bytes)
712 bytes = cpcm->sw_ready;
713 if (hw_to_end < bytes)
715 if (sw_to_end < bytes)
717 memcpy(cpcm->hw_buf.area + cpcm->hw_data,
718 runtime->dma_area + cpcm->sw_data,
720 cpcm->hw_data += bytes;
721 if ((int)cpcm->hw_data == buffer_size)
723 cpcm->sw_data += bytes;
724 if (cpcm->sw_data == cpcm->sw_bufsize)
726 cpcm->hw_ready += bytes;
727 cpcm->sw_ready -= bytes;
732 static int snd_cs46xx_capture_transfer(snd_pcm_substream_t *substream)
734 cs46xx_t *chip = snd_pcm_substream_chip(substream);
735 snd_pcm_runtime_t *runtime = substream->runtime;
736 snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
737 snd_pcm_sframes_t diff = appl_ptr - chip->capt.appl_ptr;
738 int buffer_size = runtime->period_size * CS46XX_FRAGS << chip->capt.shift;
741 if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
742 diff += runtime->boundary;
743 chip->capt.sw_ready -= diff * (1 << chip->capt.shift);
744 chip->capt.appl_ptr = appl_ptr;
746 while (chip->capt.hw_ready > 0 &&
747 chip->capt.sw_ready < (int)chip->capt.sw_bufsize) {
748 size_t hw_to_end = buffer_size - chip->capt.hw_data;
749 size_t sw_to_end = chip->capt.sw_bufsize - chip->capt.sw_data;
750 size_t bytes = chip->capt.sw_bufsize - chip->capt.sw_ready;
751 if (chip->capt.hw_ready < (int)bytes)
752 bytes = chip->capt.hw_ready;
753 if (hw_to_end < bytes)
755 if (sw_to_end < bytes)
757 memcpy(runtime->dma_area + chip->capt.sw_data,
758 chip->capt.hw_buf.area + chip->capt.hw_data,
760 chip->capt.hw_data += bytes;
761 if ((int)chip->capt.hw_data == buffer_size)
762 chip->capt.hw_data = 0;
763 chip->capt.sw_data += bytes;
764 if (chip->capt.sw_data == chip->capt.sw_bufsize)
765 chip->capt.sw_data = 0;
766 chip->capt.hw_ready -= bytes;
767 chip->capt.sw_ready += bytes;
772 static snd_pcm_uframes_t snd_cs46xx_playback_direct_pointer(snd_pcm_substream_t * substream)
774 cs46xx_t *chip = snd_pcm_substream_chip(substream);
776 cs46xx_pcm_t *cpcm = snd_magic_cast(cs46xx_pcm_t, substream->runtime->private_data, return -ENXIO);
777 snd_assert (cpcm->pcm_channel,return -ENXIO);
779 #ifdef CONFIG_SND_CS46XX_NEW_DSP
780 ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
782 ptr = snd_cs46xx_peek(chip, BA1_PBA);
784 ptr -= cpcm->hw_buf.addr;
785 return ptr >> cpcm->shift;
788 static snd_pcm_uframes_t snd_cs46xx_playback_indirect_pointer(snd_pcm_substream_t * substream)
790 cs46xx_t *chip = snd_pcm_substream_chip(substream);
792 cs46xx_pcm_t *cpcm = snd_magic_cast(cs46xx_pcm_t, substream->runtime->private_data, return -ENXIO);
794 int buffer_size = substream->runtime->period_size * CS46XX_FRAGS << cpcm->shift;
796 #ifdef CONFIG_SND_CS46XX_NEW_DSP
797 snd_assert (cpcm->pcm_channel,return -ENXIO);
798 ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
800 ptr = snd_cs46xx_peek(chip, BA1_PBA);
802 ptr -= cpcm->hw_buf.addr;
804 bytes = ptr - cpcm->hw_io;
807 bytes += buffer_size;
809 cpcm->hw_ready -= bytes;
810 cpcm->sw_io += bytes;
811 if (cpcm->sw_io >= cpcm->sw_bufsize)
812 cpcm->sw_io -= cpcm->sw_bufsize;
813 snd_cs46xx_playback_transfer(substream);
814 return cpcm->sw_io >> cpcm->shift;
817 static snd_pcm_uframes_t snd_cs46xx_capture_direct_pointer(snd_pcm_substream_t * substream)
819 cs46xx_t *chip = snd_pcm_substream_chip(substream);
820 size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
821 return ptr >> chip->capt.shift;
824 static snd_pcm_uframes_t snd_cs46xx_capture_indirect_pointer(snd_pcm_substream_t * substream)
826 cs46xx_t *chip = snd_pcm_substream_chip(substream);
827 size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
828 ssize_t bytes = ptr - chip->capt.hw_io;
829 int buffer_size = substream->runtime->period_size * CS46XX_FRAGS << chip->capt.shift;
832 bytes += buffer_size;
833 chip->capt.hw_io = ptr;
834 chip->capt.hw_ready += bytes;
835 chip->capt.sw_io += bytes;
836 if (chip->capt.sw_io >= chip->capt.sw_bufsize)
837 chip->capt.sw_io -= chip->capt.sw_bufsize;
838 snd_cs46xx_capture_transfer(substream);
839 return chip->capt.sw_io >> chip->capt.shift;
842 static int snd_cs46xx_playback_trigger(snd_pcm_substream_t * substream,
845 cs46xx_t *chip = snd_pcm_substream_chip(substream);
846 /*snd_pcm_runtime_t *runtime = substream->runtime;*/
849 #ifdef CONFIG_SND_CS46XX_NEW_DSP
850 cs46xx_pcm_t *cpcm = snd_magic_cast(cs46xx_pcm_t, substream->runtime->private_data, return -ENXIO);
852 spin_lock(&chip->reg_lock);
855 #ifdef CONFIG_SND_CS46XX_NEW_DSP
857 if (! cpcm->pcm_channel) {
862 case SNDRV_PCM_TRIGGER_START:
863 case SNDRV_PCM_TRIGGER_RESUME:
864 #ifdef CONFIG_SND_CS46XX_NEW_DSP
865 /* magic value to unmute PCM stream playback volume */
866 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
867 SCBVolumeCtrl) << 2, 0x80008000);
869 if (cpcm->pcm_channel->unlinked)
870 cs46xx_dsp_pcm_link(chip,cpcm->pcm_channel);
872 if (substream->runtime->periods != CS46XX_FRAGS)
873 snd_cs46xx_playback_transfer(substream);
875 if (substream->runtime->periods != CS46XX_FRAGS)
876 snd_cs46xx_playback_transfer(substream);
878 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
880 snd_cs46xx_poke(chip, BA1_PCTL, chip->play_ctl | tmp);
884 case SNDRV_PCM_TRIGGER_STOP:
885 case SNDRV_PCM_TRIGGER_SUSPEND:
886 #ifdef CONFIG_SND_CS46XX_NEW_DSP
887 /* magic mute channel */
888 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
889 SCBVolumeCtrl) << 2, 0xffffffff);
891 if (!cpcm->pcm_channel->unlinked)
892 cs46xx_dsp_pcm_unlink(chip,cpcm->pcm_channel);
895 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
897 snd_cs46xx_poke(chip, BA1_PCTL, tmp);
906 #ifndef CONFIG_SND_CS46XX_NEW_DSP
907 spin_unlock(&chip->reg_lock);
913 static int snd_cs46xx_capture_trigger(snd_pcm_substream_t * substream,
916 cs46xx_t *chip = snd_pcm_substream_chip(substream);
920 spin_lock(&chip->reg_lock);
922 case SNDRV_PCM_TRIGGER_START:
923 case SNDRV_PCM_TRIGGER_RESUME:
924 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
926 snd_cs46xx_poke(chip, BA1_CCTL, chip->capt.ctl | tmp);
928 case SNDRV_PCM_TRIGGER_STOP:
929 case SNDRV_PCM_TRIGGER_SUSPEND:
930 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
932 snd_cs46xx_poke(chip, BA1_CCTL, tmp);
938 spin_unlock(&chip->reg_lock);
943 #ifdef CONFIG_SND_CS46XX_NEW_DSP
944 static int _cs46xx_adjust_sample_rate (cs46xx_t *chip, cs46xx_pcm_t *cpcm,
948 /* If PCMReaderSCB and SrcTaskSCB not created yet ... */
949 if ( cpcm->pcm_channel == NULL) {
950 cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate,
951 cpcm, cpcm->hw_buf.addr,cpcm->pcm_channel_id);
952 if (cpcm->pcm_channel == NULL) {
953 snd_printk(KERN_ERR "cs46xx: failed to create virtual PCM channel\n");
956 cpcm->pcm_channel->sample_rate = sample_rate;
958 /* if sample rate is changed */
959 if ((int)cpcm->pcm_channel->sample_rate != sample_rate) {
960 int unlinked = cpcm->pcm_channel->unlinked;
961 cs46xx_dsp_destroy_pcm_channel (chip,cpcm->pcm_channel);
963 if ( (cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate, cpcm,
965 cpcm->pcm_channel_id)) == NULL) {
966 snd_printk(KERN_ERR "cs46xx: failed to re-create virtual PCM channel\n");
970 if (!unlinked) cs46xx_dsp_pcm_link (chip,cpcm->pcm_channel);
971 cpcm->pcm_channel->sample_rate = sample_rate;
979 static int snd_cs46xx_playback_hw_params(snd_pcm_substream_t * substream,
980 snd_pcm_hw_params_t * hw_params)
982 snd_pcm_runtime_t *runtime = substream->runtime;
985 #ifdef CONFIG_SND_CS46XX_NEW_DSP
986 cs46xx_t *chip = snd_pcm_substream_chip(substream);
987 int sample_rate = params_rate(hw_params);
988 int period_size = params_period_bytes(hw_params);
990 cpcm = snd_magic_cast(cs46xx_pcm_t, runtime->private_data, return -ENXIO);
992 #ifdef CONFIG_SND_CS46XX_NEW_DSP
993 snd_assert (sample_rate != 0, return -ENXIO);
995 down (&chip->spos_mutex);
997 if (_cs46xx_adjust_sample_rate (chip,cpcm,sample_rate)) {
998 up (&chip->spos_mutex);
1002 snd_assert (cpcm->pcm_channel != NULL);
1003 if (!cpcm->pcm_channel) {
1004 up (&chip->spos_mutex);
1009 if (cs46xx_dsp_pcm_channel_set_period (chip,cpcm->pcm_channel,period_size)) {
1010 up (&chip->spos_mutex);
1014 snd_printdd ("period_size (%d), periods (%d) buffer_size(%d)\n",
1015 period_size, params_periods(hw_params),
1016 params_buffer_bytes(hw_params));
1019 if (params_periods(hw_params) == CS46XX_FRAGS) {
1020 if (runtime->dma_area != cpcm->hw_buf.area)
1021 snd_pcm_lib_free_pages(substream);
1022 runtime->dma_area = cpcm->hw_buf.area;
1023 runtime->dma_addr = cpcm->hw_buf.addr;
1024 runtime->dma_bytes = cpcm->hw_buf.bytes;
1027 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1028 if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
1029 substream->ops = &snd_cs46xx_playback_ops;
1030 } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
1031 substream->ops = &snd_cs46xx_playback_rear_ops;
1032 } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
1033 substream->ops = &snd_cs46xx_playback_clfe_ops;
1034 } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
1035 substream->ops = &snd_cs46xx_playback_iec958_ops;
1040 substream->ops = &snd_cs46xx_playback_ops;
1044 if (runtime->dma_area == cpcm->hw_buf.area) {
1045 runtime->dma_area = NULL;
1046 runtime->dma_addr = 0;
1047 runtime->dma_bytes = 0;
1049 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) {
1050 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1051 up (&chip->spos_mutex);
1056 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1057 if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
1058 substream->ops = &snd_cs46xx_playback_indirect_ops;
1059 } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
1060 substream->ops = &snd_cs46xx_playback_indirect_rear_ops;
1061 } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
1062 substream->ops = &snd_cs46xx_playback_indirect_clfe_ops;
1063 } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
1064 substream->ops = &snd_cs46xx_playback_indirect_iec958_ops;
1069 substream->ops = &snd_cs46xx_playback_indirect_ops;
1074 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1075 up (&chip->spos_mutex);
1081 static int snd_cs46xx_playback_hw_free(snd_pcm_substream_t * substream)
1083 /*cs46xx_t *chip = snd_pcm_substream_chip(substream);*/
1084 snd_pcm_runtime_t *runtime = substream->runtime;
1087 cpcm = snd_magic_cast(cs46xx_pcm_t, runtime->private_data, return -ENXIO);
1089 /* if play_back open fails, then this function
1090 is called and cpcm can actually be NULL here */
1091 if (!cpcm) return -ENXIO;
1093 if (runtime->dma_area != cpcm->hw_buf.area)
1094 snd_pcm_lib_free_pages(substream);
1096 runtime->dma_area = NULL;
1097 runtime->dma_addr = 0;
1098 runtime->dma_bytes = 0;
1103 static int snd_cs46xx_playback_prepare(snd_pcm_substream_t * substream)
1107 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1108 snd_pcm_runtime_t *runtime = substream->runtime;
1111 cpcm = snd_magic_cast(cs46xx_pcm_t, runtime->private_data, return -ENXIO);
1113 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1114 snd_assert (cpcm->pcm_channel != NULL, return -ENXIO);
1116 pfie = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2 );
1117 pfie &= ~0x0000f03f;
1120 pfie = snd_cs46xx_peek(chip, BA1_PFIE);
1121 pfie &= ~0x0000f03f;
1125 /* if to convert from stereo to mono */
1126 if (runtime->channels == 1) {
1130 /* if to convert from 8 bit to 16 bit */
1131 if (snd_pcm_format_width(runtime->format) == 8) {
1135 /* if to convert to unsigned */
1136 if (snd_pcm_format_unsigned(runtime->format))
1139 /* Never convert byte order when sample stream is 8 bit */
1140 if (snd_pcm_format_width(runtime->format) != 8) {
1141 /* convert from big endian to little endian */
1142 if (snd_pcm_format_big_endian(runtime->format))
1146 cpcm->sw_bufsize = snd_pcm_lib_buffer_bytes(substream);
1147 cpcm->sw_data = cpcm->sw_io = cpcm->sw_ready = 0;
1148 cpcm->hw_data = cpcm->hw_io = cpcm->hw_ready = 0;
1151 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1153 tmp = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2);
1155 tmp |= (4 << cpcm->shift) - 1;
1156 /* playback transaction count register */
1157 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2, tmp);
1159 /* playback format && interrupt enable */
1160 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2, pfie | cpcm->pcm_channel->pcm_slot);
1162 snd_cs46xx_poke(chip, BA1_PBA, cpcm->hw_buf.addr);
1163 tmp = snd_cs46xx_peek(chip, BA1_PDTC);
1165 tmp |= (4 << cpcm->shift) - 1;
1166 snd_cs46xx_poke(chip, BA1_PDTC, tmp);
1167 snd_cs46xx_poke(chip, BA1_PFIE, pfie);
1168 snd_cs46xx_set_play_sample_rate(chip, runtime->rate);
1174 static int snd_cs46xx_capture_hw_params(snd_pcm_substream_t * substream,
1175 snd_pcm_hw_params_t * hw_params)
1177 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1178 snd_pcm_runtime_t *runtime = substream->runtime;
1181 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1182 cs46xx_dsp_pcm_ostream_set_period (chip, params_period_bytes(hw_params));
1184 if (runtime->periods == CS46XX_FRAGS) {
1185 if (runtime->dma_area != chip->capt.hw_buf.area)
1186 snd_pcm_lib_free_pages(substream);
1187 runtime->dma_area = chip->capt.hw_buf.area;
1188 runtime->dma_addr = chip->capt.hw_buf.addr;
1189 runtime->dma_bytes = chip->capt.hw_buf.bytes;
1190 substream->ops = &snd_cs46xx_capture_ops;
1192 if (runtime->dma_area == chip->capt.hw_buf.area) {
1193 runtime->dma_area = NULL;
1194 runtime->dma_addr = 0;
1195 runtime->dma_bytes = 0;
1197 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1199 substream->ops = &snd_cs46xx_capture_indirect_ops;
1205 static int snd_cs46xx_capture_hw_free(snd_pcm_substream_t * substream)
1207 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1208 snd_pcm_runtime_t *runtime = substream->runtime;
1210 if (runtime->dma_area != chip->capt.hw_buf.area)
1211 snd_pcm_lib_free_pages(substream);
1212 runtime->dma_area = NULL;
1213 runtime->dma_addr = 0;
1214 runtime->dma_bytes = 0;
1219 static int snd_cs46xx_capture_prepare(snd_pcm_substream_t * substream)
1221 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1222 snd_pcm_runtime_t *runtime = substream->runtime;
1224 snd_cs46xx_poke(chip, BA1_CBA, chip->capt.hw_buf.addr);
1225 chip->capt.shift = 2;
1226 chip->capt.sw_bufsize = snd_pcm_lib_buffer_bytes(substream);
1227 chip->capt.sw_data = chip->capt.sw_io = chip->capt.sw_ready = 0;
1228 chip->capt.hw_data = chip->capt.hw_io = chip->capt.hw_ready = 0;
1229 chip->capt.appl_ptr = 0;
1230 snd_cs46xx_set_capture_sample_rate(chip, runtime->rate);
1235 static irqreturn_t snd_cs46xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1237 cs46xx_t *chip = snd_magic_cast(cs46xx_t, dev_id, return IRQ_NONE);
1239 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1240 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
1243 cs46xx_pcm_t *cpcm = NULL;
1247 * Read the Interrupt Status Register to clear the interrupt
1249 status1 = snd_cs46xx_peekBA0(chip, BA0_HISR);
1250 if ((status1 & 0x7fffffff) == 0) {
1251 snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
1255 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1256 status2 = snd_cs46xx_peekBA0(chip, BA0_HSR0);
1258 for (i = 0; i < DSP_MAX_PCM_CHANNELS; ++i) {
1260 if ( status1 & (1 << i) ) {
1261 if (i == CS46XX_DSP_CAPTURE_CHANNEL) {
1262 if (chip->capt.substream)
1263 snd_pcm_period_elapsed(chip->capt.substream);
1265 if (ins->pcm_channels[i].active &&
1266 ins->pcm_channels[i].private_data &&
1267 !ins->pcm_channels[i].unlinked) {
1268 cpcm = snd_magic_cast(cs46xx_pcm_t, ins->pcm_channels[i].private_data, continue);
1269 snd_pcm_period_elapsed(cpcm->substream);
1274 if ( status2 & (1 << (i - 16))) {
1275 if (ins->pcm_channels[i].active &&
1276 ins->pcm_channels[i].private_data &&
1277 !ins->pcm_channels[i].unlinked) {
1278 cpcm = snd_magic_cast(cs46xx_pcm_t, ins->pcm_channels[i].private_data, continue);
1279 snd_pcm_period_elapsed(cpcm->substream);
1287 if ((status1 & HISR_VC0) && chip->playback_pcm) {
1288 if (chip->playback_pcm->substream)
1289 snd_pcm_period_elapsed(chip->playback_pcm->substream);
1291 if ((status1 & HISR_VC1) && chip->pcm) {
1292 if (chip->capt.substream)
1293 snd_pcm_period_elapsed(chip->capt.substream);
1297 if ((status1 & HISR_MIDI) && chip->rmidi) {
1300 spin_lock(&chip->reg_lock);
1301 while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_RBE) == 0) {
1302 c = snd_cs46xx_peekBA0(chip, BA0_MIDRP);
1303 if ((chip->midcr & MIDCR_RIE) == 0)
1305 snd_rawmidi_receive(chip->midi_input, &c, 1);
1307 while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
1308 if ((chip->midcr & MIDCR_TIE) == 0)
1310 if (snd_rawmidi_transmit(chip->midi_output, &c, 1) != 1) {
1311 chip->midcr &= ~MIDCR_TIE;
1312 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
1315 snd_cs46xx_pokeBA0(chip, BA0_MIDWP, c);
1317 spin_unlock(&chip->reg_lock);
1320 * EOI to the PCI part....reenables interrupts
1322 snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
1327 static snd_pcm_hardware_t snd_cs46xx_playback =
1329 .info = (SNDRV_PCM_INFO_MMAP |
1330 SNDRV_PCM_INFO_INTERLEAVED |
1331 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1332 SNDRV_PCM_INFO_RESUME),
1333 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
1334 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
1335 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE),
1336 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1341 .buffer_bytes_max = (256 * 1024),
1342 .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
1343 .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
1344 .periods_min = CS46XX_FRAGS,
1345 .periods_max = 1024,
1349 static snd_pcm_hardware_t snd_cs46xx_capture =
1351 .info = (SNDRV_PCM_INFO_MMAP |
1352 SNDRV_PCM_INFO_INTERLEAVED |
1353 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1354 SNDRV_PCM_INFO_RESUME),
1355 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1356 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1361 .buffer_bytes_max = (256 * 1024),
1362 .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
1363 .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
1364 .periods_min = CS46XX_FRAGS,
1365 .periods_max = 1024,
1369 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1371 static unsigned int period_sizes[] = { 32, 64, 128, 256, 512, 1024, 2048 };
1373 #define PERIOD_SIZES sizeof(period_sizes) / sizeof(period_sizes[0])
1375 static snd_pcm_hw_constraint_list_t hw_constraints_period_sizes = {
1376 .count = PERIOD_SIZES,
1377 .list = period_sizes,
1383 static void snd_cs46xx_pcm_free_substream(snd_pcm_runtime_t *runtime)
1385 cs46xx_pcm_t * cpcm = snd_magic_cast(cs46xx_pcm_t, runtime->private_data, return);
1388 snd_magic_kfree(cpcm);
1391 static int _cs46xx_playback_open_channel (snd_pcm_substream_t * substream,int pcm_channel_id)
1393 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1394 cs46xx_pcm_t * cpcm;
1395 snd_pcm_runtime_t *runtime = substream->runtime;
1397 cpcm = snd_magic_kcalloc(cs46xx_pcm_t, 0, GFP_KERNEL);
1400 if (snd_dma_alloc_pages(&chip->dma_dev, PAGE_SIZE, &cpcm->hw_buf) < 0) {
1401 snd_magic_kfree(cpcm);
1405 runtime->hw = snd_cs46xx_playback;
1406 runtime->private_data = cpcm;
1407 runtime->private_free = snd_cs46xx_pcm_free_substream;
1409 cpcm->substream = substream;
1410 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1411 down (&chip->spos_mutex);
1412 cpcm->pcm_channel = NULL;
1413 cpcm->pcm_channel_id = pcm_channel_id;
1416 snd_pcm_hw_constraint_list(runtime, 0,
1417 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1418 &hw_constraints_period_sizes);
1420 up (&chip->spos_mutex);
1422 chip->playback_pcm = cpcm; /* HACK */
1425 if (chip->accept_valid)
1426 substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
1427 chip->active_ctrl(chip, 1);
1432 static int snd_cs46xx_playback_open(snd_pcm_substream_t * substream)
1434 snd_printdd("open front channel\n");
1435 return _cs46xx_playback_open_channel(substream,DSP_PCM_MAIN_CHANNEL);
1438 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1439 static int snd_cs46xx_playback_open_rear(snd_pcm_substream_t * substream)
1441 snd_printdd("open rear channel\n");
1443 return _cs46xx_playback_open_channel(substream,DSP_PCM_REAR_CHANNEL);
1446 static int snd_cs46xx_playback_open_clfe(snd_pcm_substream_t * substream)
1448 snd_printdd("open center - LFE channel\n");
1450 return _cs46xx_playback_open_channel(substream,DSP_PCM_CENTER_LFE_CHANNEL);
1453 static int snd_cs46xx_playback_open_iec958(snd_pcm_substream_t * substream)
1455 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1457 snd_printdd("open raw iec958 channel\n");
1459 down (&chip->spos_mutex);
1460 cs46xx_iec958_pre_open (chip);
1461 up (&chip->spos_mutex);
1463 return _cs46xx_playback_open_channel(substream,DSP_IEC958_CHANNEL);
1466 static int snd_cs46xx_playback_close(snd_pcm_substream_t * substream);
1468 static int snd_cs46xx_playback_close_iec958(snd_pcm_substream_t * substream)
1471 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1473 snd_printdd("close raw iec958 channel\n");
1475 err = snd_cs46xx_playback_close(substream);
1477 down (&chip->spos_mutex);
1478 cs46xx_iec958_post_close (chip);
1479 up (&chip->spos_mutex);
1485 static int snd_cs46xx_capture_open(snd_pcm_substream_t * substream)
1487 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1489 if (snd_dma_alloc_pages(&chip->dma_dev, PAGE_SIZE, &chip->capt.hw_buf) < 0)
1491 chip->capt.substream = substream;
1492 substream->runtime->hw = snd_cs46xx_capture;
1494 if (chip->accept_valid)
1495 substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
1497 chip->active_ctrl(chip, 1);
1499 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1500 snd_pcm_hw_constraint_list(substream->runtime, 0,
1501 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1502 &hw_constraints_period_sizes);
1507 static int snd_cs46xx_playback_close(snd_pcm_substream_t * substream)
1509 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1510 snd_pcm_runtime_t *runtime = substream->runtime;
1511 cs46xx_pcm_t * cpcm;
1513 cpcm = snd_magic_cast(cs46xx_pcm_t, runtime->private_data, return -ENXIO);
1515 /* when playback_open fails, then cpcm can be NULL */
1516 if (!cpcm) return -ENXIO;
1518 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1519 down (&chip->spos_mutex);
1520 if (cpcm->pcm_channel) {
1521 cs46xx_dsp_destroy_pcm_channel(chip,cpcm->pcm_channel);
1522 cpcm->pcm_channel = NULL;
1524 up (&chip->spos_mutex);
1526 chip->playback_pcm = NULL;
1529 cpcm->substream = NULL;
1530 snd_dma_free_pages(&chip->dma_dev, &cpcm->hw_buf);
1531 chip->active_ctrl(chip, -1);
1536 static int snd_cs46xx_capture_close(snd_pcm_substream_t * substream)
1538 cs46xx_t *chip = snd_pcm_substream_chip(substream);
1540 chip->capt.substream = NULL;
1541 snd_dma_free_pages(&chip->dma_dev, &chip->capt.hw_buf);
1542 chip->active_ctrl(chip, -1);
1547 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1548 snd_pcm_ops_t snd_cs46xx_playback_rear_ops = {
1549 .open = snd_cs46xx_playback_open_rear,
1550 .close = snd_cs46xx_playback_close,
1551 .ioctl = snd_pcm_lib_ioctl,
1552 .hw_params = snd_cs46xx_playback_hw_params,
1553 .hw_free = snd_cs46xx_playback_hw_free,
1554 .prepare = snd_cs46xx_playback_prepare,
1555 .trigger = snd_cs46xx_playback_trigger,
1556 .pointer = snd_cs46xx_playback_direct_pointer,
1559 snd_pcm_ops_t snd_cs46xx_playback_indirect_rear_ops = {
1560 .open = snd_cs46xx_playback_open_rear,
1561 .close = snd_cs46xx_playback_close,
1562 .ioctl = snd_pcm_lib_ioctl,
1563 .hw_params = snd_cs46xx_playback_hw_params,
1564 .hw_free = snd_cs46xx_playback_hw_free,
1565 .prepare = snd_cs46xx_playback_prepare,
1566 .trigger = snd_cs46xx_playback_trigger,
1567 .pointer = snd_cs46xx_playback_indirect_pointer,
1568 .ack = snd_cs46xx_playback_transfer,
1571 snd_pcm_ops_t snd_cs46xx_playback_clfe_ops = {
1572 .open = snd_cs46xx_playback_open_clfe,
1573 .close = snd_cs46xx_playback_close,
1574 .ioctl = snd_pcm_lib_ioctl,
1575 .hw_params = snd_cs46xx_playback_hw_params,
1576 .hw_free = snd_cs46xx_playback_hw_free,
1577 .prepare = snd_cs46xx_playback_prepare,
1578 .trigger = snd_cs46xx_playback_trigger,
1579 .pointer = snd_cs46xx_playback_direct_pointer,
1582 snd_pcm_ops_t snd_cs46xx_playback_indirect_clfe_ops = {
1583 .open = snd_cs46xx_playback_open_clfe,
1584 .close = snd_cs46xx_playback_close,
1585 .ioctl = snd_pcm_lib_ioctl,
1586 .hw_params = snd_cs46xx_playback_hw_params,
1587 .hw_free = snd_cs46xx_playback_hw_free,
1588 .prepare = snd_cs46xx_playback_prepare,
1589 .trigger = snd_cs46xx_playback_trigger,
1590 .pointer = snd_cs46xx_playback_indirect_pointer,
1591 .ack = snd_cs46xx_playback_transfer,
1594 snd_pcm_ops_t snd_cs46xx_playback_iec958_ops = {
1595 .open = snd_cs46xx_playback_open_iec958,
1596 .close = snd_cs46xx_playback_close_iec958,
1597 .ioctl = snd_pcm_lib_ioctl,
1598 .hw_params = snd_cs46xx_playback_hw_params,
1599 .hw_free = snd_cs46xx_playback_hw_free,
1600 .prepare = snd_cs46xx_playback_prepare,
1601 .trigger = snd_cs46xx_playback_trigger,
1602 .pointer = snd_cs46xx_playback_direct_pointer,
1605 snd_pcm_ops_t snd_cs46xx_playback_indirect_iec958_ops = {
1606 .open = snd_cs46xx_playback_open_iec958,
1607 .close = snd_cs46xx_playback_close_iec958,
1608 .ioctl = snd_pcm_lib_ioctl,
1609 .hw_params = snd_cs46xx_playback_hw_params,
1610 .hw_free = snd_cs46xx_playback_hw_free,
1611 .prepare = snd_cs46xx_playback_prepare,
1612 .trigger = snd_cs46xx_playback_trigger,
1613 .pointer = snd_cs46xx_playback_indirect_pointer,
1614 .ack = snd_cs46xx_playback_transfer,
1619 snd_pcm_ops_t snd_cs46xx_playback_ops = {
1620 .open = snd_cs46xx_playback_open,
1621 .close = snd_cs46xx_playback_close,
1622 .ioctl = snd_pcm_lib_ioctl,
1623 .hw_params = snd_cs46xx_playback_hw_params,
1624 .hw_free = snd_cs46xx_playback_hw_free,
1625 .prepare = snd_cs46xx_playback_prepare,
1626 .trigger = snd_cs46xx_playback_trigger,
1627 .pointer = snd_cs46xx_playback_direct_pointer,
1630 snd_pcm_ops_t snd_cs46xx_playback_indirect_ops = {
1631 .open = snd_cs46xx_playback_open,
1632 .close = snd_cs46xx_playback_close,
1633 .ioctl = snd_pcm_lib_ioctl,
1634 .hw_params = snd_cs46xx_playback_hw_params,
1635 .hw_free = snd_cs46xx_playback_hw_free,
1636 .prepare = snd_cs46xx_playback_prepare,
1637 .trigger = snd_cs46xx_playback_trigger,
1638 .pointer = snd_cs46xx_playback_indirect_pointer,
1639 .ack = snd_cs46xx_playback_transfer,
1642 snd_pcm_ops_t snd_cs46xx_capture_ops = {
1643 .open = snd_cs46xx_capture_open,
1644 .close = snd_cs46xx_capture_close,
1645 .ioctl = snd_pcm_lib_ioctl,
1646 .hw_params = snd_cs46xx_capture_hw_params,
1647 .hw_free = snd_cs46xx_capture_hw_free,
1648 .prepare = snd_cs46xx_capture_prepare,
1649 .trigger = snd_cs46xx_capture_trigger,
1650 .pointer = snd_cs46xx_capture_direct_pointer,
1653 snd_pcm_ops_t snd_cs46xx_capture_indirect_ops = {
1654 .open = snd_cs46xx_capture_open,
1655 .close = snd_cs46xx_capture_close,
1656 .ioctl = snd_pcm_lib_ioctl,
1657 .hw_params = snd_cs46xx_capture_hw_params,
1658 .hw_free = snd_cs46xx_capture_hw_free,
1659 .prepare = snd_cs46xx_capture_prepare,
1660 .trigger = snd_cs46xx_capture_trigger,
1661 .pointer = snd_cs46xx_capture_indirect_pointer,
1662 .ack = snd_cs46xx_capture_transfer,
1665 static void snd_cs46xx_pcm_free(snd_pcm_t *pcm)
1667 cs46xx_t *chip = snd_magic_cast(cs46xx_t, pcm->private_data, return);
1669 snd_pcm_lib_preallocate_free_for_all(pcm);
1672 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1673 static void snd_cs46xx_pcm_rear_free(snd_pcm_t *pcm)
1675 cs46xx_t *chip = snd_magic_cast(cs46xx_t, pcm->private_data, return);
1676 chip->pcm_rear = NULL;
1677 snd_pcm_lib_preallocate_free_for_all(pcm);
1680 static void snd_cs46xx_pcm_center_lfe_free(snd_pcm_t *pcm)
1682 cs46xx_t *chip = snd_magic_cast(cs46xx_t, pcm->private_data, return);
1683 chip->pcm_center_lfe = NULL;
1684 snd_pcm_lib_preallocate_free_for_all(pcm);
1687 static void snd_cs46xx_pcm_iec958_free(snd_pcm_t *pcm)
1689 cs46xx_t *chip = snd_magic_cast(cs46xx_t, pcm->private_data, return);
1690 chip->pcm_iec958 = NULL;
1691 snd_pcm_lib_preallocate_free_for_all(pcm);
1694 #define MAX_PLAYBACK_CHANNELS (DSP_MAX_PCM_CHANNELS - 1)
1696 #define MAX_PLAYBACK_CHANNELS 1
1699 int __devinit snd_cs46xx_pcm(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
1706 if ((err = snd_pcm_new(chip->card, "CS46xx", device, MAX_PLAYBACK_CHANNELS, 1, &pcm)) < 0)
1709 pcm->private_data = chip;
1710 pcm->private_free = snd_cs46xx_pcm_free;
1712 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_ops);
1713 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs46xx_capture_ops);
1716 pcm->info_flags = 0;
1717 strcpy(pcm->name, "CS46xx");
1720 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1721 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1730 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1731 int __devinit snd_cs46xx_pcm_rear(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
1739 if ((err = snd_pcm_new(chip->card, "CS46xx - Rear", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
1742 pcm->private_data = chip;
1743 pcm->private_free = snd_cs46xx_pcm_rear_free;
1745 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_rear_ops);
1748 pcm->info_flags = 0;
1749 strcpy(pcm->name, "CS46xx - Rear");
1750 chip->pcm_rear = pcm;
1752 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1753 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1761 int __devinit snd_cs46xx_pcm_center_lfe(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
1769 if ((err = snd_pcm_new(chip->card, "CS46xx - Center LFE", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
1772 pcm->private_data = chip;
1773 pcm->private_free = snd_cs46xx_pcm_center_lfe_free;
1775 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_clfe_ops);
1778 pcm->info_flags = 0;
1779 strcpy(pcm->name, "CS46xx - Center LFE");
1780 chip->pcm_center_lfe = pcm;
1782 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1783 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1791 int __devinit snd_cs46xx_pcm_iec958(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
1799 if ((err = snd_pcm_new(chip->card, "CS46xx - IEC958", device, 1, 0, &pcm)) < 0)
1802 pcm->private_data = chip;
1803 pcm->private_free = snd_cs46xx_pcm_iec958_free;
1805 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_iec958_ops);
1808 pcm->info_flags = 0;
1809 strcpy(pcm->name, "CS46xx - IEC958");
1810 chip->pcm_rear = pcm;
1812 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1813 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1825 static void snd_cs46xx_mixer_free_ac97_bus(ac97_bus_t *bus)
1827 cs46xx_t *chip = snd_magic_cast(cs46xx_t, bus->private_data, return);
1829 chip->ac97_bus = NULL;
1832 static void snd_cs46xx_mixer_free_ac97(ac97_t *ac97)
1834 cs46xx_t *chip = snd_magic_cast(cs46xx_t, ac97->private_data, return);
1836 snd_assert ((ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) ||
1837 (ac97 == chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]),
1840 if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) {
1841 chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = NULL;
1842 chip->eapd_switch = NULL;
1845 chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = NULL;
1848 static int snd_cs46xx_vol_info(snd_kcontrol_t *kcontrol,
1849 snd_ctl_elem_info_t *uinfo)
1851 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1853 uinfo->value.integer.min = 0;
1854 uinfo->value.integer.max = 0x7fff;
1858 static int snd_cs46xx_vol_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1860 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1861 int reg = kcontrol->private_value;
1862 unsigned int val = snd_cs46xx_peek(chip, reg);
1863 ucontrol->value.integer.value[0] = 0xffff - (val >> 16);
1864 ucontrol->value.integer.value[1] = 0xffff - (val & 0xffff);
1868 static int snd_cs46xx_vol_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1870 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1871 int reg = kcontrol->private_value;
1872 unsigned int val = ((0xffff - ucontrol->value.integer.value[0]) << 16 |
1873 (0xffff - ucontrol->value.integer.value[1]));
1874 unsigned int old = snd_cs46xx_peek(chip, reg);
1875 int change = (old != val);
1878 snd_cs46xx_poke(chip, reg, val);
1884 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1886 static int snd_cs46xx_vol_dac_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1888 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1890 ucontrol->value.integer.value[0] = chip->dsp_spos_instance->dac_volume_left;
1891 ucontrol->value.integer.value[1] = chip->dsp_spos_instance->dac_volume_right;
1896 static int snd_cs46xx_vol_dac_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1898 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1901 if (chip->dsp_spos_instance->dac_volume_right != ucontrol->value.integer.value[0] ||
1902 chip->dsp_spos_instance->dac_volume_left != ucontrol->value.integer.value[1]) {
1903 cs46xx_dsp_set_dac_volume(chip,
1904 ucontrol->value.integer.value[0],
1905 ucontrol->value.integer.value[1]);
1913 static int snd_cs46xx_vol_iec958_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1915 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1917 ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_input_volume_left;
1918 ucontrol->value.integer.value[1] = chip->dsp_spos_instance->spdif_input_volume_right;
1922 static int snd_cs46xx_vol_iec958_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1924 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1927 if (chip->dsp_spos_instance->spdif_input_volume_left != ucontrol->value.integer.value[0] ||
1928 chip->dsp_spos_instance->spdif_input_volume_right!= ucontrol->value.integer.value[1]) {
1929 cs46xx_dsp_set_iec958_volume (chip,
1930 ucontrol->value.integer.value[0],
1931 ucontrol->value.integer.value[1]);
1939 static int snd_mixer_boolean_info(snd_kcontrol_t *kcontrol,
1940 snd_ctl_elem_info_t *uinfo)
1942 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1944 uinfo->value.integer.min = 0;
1945 uinfo->value.integer.max = 1;
1949 static int snd_cs46xx_iec958_get(snd_kcontrol_t *kcontrol,
1950 snd_ctl_elem_value_t *ucontrol)
1952 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1953 int reg = kcontrol->private_value;
1955 if (reg == CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT)
1956 ucontrol->value.integer.value[0] = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
1958 ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_status_in;
1963 static int snd_cs46xx_iec958_put(snd_kcontrol_t *kcontrol,
1964 snd_ctl_elem_value_t *ucontrol)
1966 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
1969 switch (kcontrol->private_value) {
1970 case CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT:
1971 down (&chip->spos_mutex);
1972 change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
1973 if (ucontrol->value.integer.value[0] && !change)
1974 cs46xx_dsp_enable_spdif_out(chip);
1975 else if (change && !ucontrol->value.integer.value[0])
1976 cs46xx_dsp_disable_spdif_out(chip);
1978 res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED));
1979 up (&chip->spos_mutex);
1981 case CS46XX_MIXER_SPDIF_INPUT_ELEMENT:
1982 change = chip->dsp_spos_instance->spdif_status_in;
1983 if (ucontrol->value.integer.value[0] && !change) {
1984 cs46xx_dsp_enable_spdif_in(chip);
1985 /* restore volume */
1987 else if (change && !ucontrol->value.integer.value[0])
1988 cs46xx_dsp_disable_spdif_in(chip);
1990 res = (change != chip->dsp_spos_instance->spdif_status_in);
1994 snd_assert(0, (void)0);
2000 static int snd_cs46xx_adc_capture_get(snd_kcontrol_t *kcontrol,
2001 snd_ctl_elem_value_t *ucontrol)
2003 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2004 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2006 if (ins->adc_input != NULL)
2007 ucontrol->value.integer.value[0] = 1;
2009 ucontrol->value.integer.value[0] = 0;
2014 static int snd_cs46xx_adc_capture_put(snd_kcontrol_t *kcontrol,
2015 snd_ctl_elem_value_t *ucontrol)
2017 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2018 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2021 if (ucontrol->value.integer.value[0] && !ins->adc_input) {
2022 cs46xx_dsp_enable_adc_capture(chip);
2024 } else if (!ucontrol->value.integer.value[0] && ins->adc_input) {
2025 cs46xx_dsp_disable_adc_capture(chip);
2031 static int snd_cs46xx_pcm_capture_get(snd_kcontrol_t *kcontrol,
2032 snd_ctl_elem_value_t *ucontrol)
2034 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2035 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2037 if (ins->pcm_input != NULL)
2038 ucontrol->value.integer.value[0] = 1;
2040 ucontrol->value.integer.value[0] = 0;
2046 static int snd_cs46xx_pcm_capture_put(snd_kcontrol_t *kcontrol,
2047 snd_ctl_elem_value_t *ucontrol)
2049 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2050 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2053 if (ucontrol->value.integer.value[0] && !ins->pcm_input) {
2054 cs46xx_dsp_enable_pcm_capture(chip);
2056 } else if (!ucontrol->value.integer.value[0] && ins->pcm_input) {
2057 cs46xx_dsp_disable_pcm_capture(chip);
2064 static int snd_herc_spdif_select_get(snd_kcontrol_t *kcontrol,
2065 snd_ctl_elem_value_t *ucontrol)
2067 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2069 int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
2071 if (val1 & EGPIODR_GPOE0)
2072 ucontrol->value.integer.value[0] = 1;
2074 ucontrol->value.integer.value[0] = 0;
2080 * Game Theatre XP card - EGPIO[0] is used to select SPDIF input optical or coaxial.
2082 static int snd_herc_spdif_select_put(snd_kcontrol_t *kcontrol,
2083 snd_ctl_elem_value_t *ucontrol)
2085 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2086 int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
2087 int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
2089 if (ucontrol->value.integer.value[0]) {
2090 /* optical is default */
2091 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
2092 EGPIODR_GPOE0 | val1); /* enable EGPIO0 output */
2093 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
2094 EGPIOPTR_GPPT0 | val2); /* open-drain on output */
2097 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE0); /* disable */
2098 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT0); /* disable */
2101 /* checking diff from the EGPIO direction register
2103 return (val1 != (int)snd_cs46xx_peekBA0(chip, BA0_EGPIODR));
2107 static int snd_cs46xx_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2109 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2114 static int snd_cs46xx_spdif_default_get(snd_kcontrol_t * kcontrol,
2115 snd_ctl_elem_value_t * ucontrol)
2117 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2118 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2120 down (&chip->spos_mutex);
2121 ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_default >> 24) & 0xff);
2122 ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_default >> 16) & 0xff);
2123 ucontrol->value.iec958.status[2] = 0;
2124 ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_default) & 0xff);
2125 up (&chip->spos_mutex);
2130 static int snd_cs46xx_spdif_default_put(snd_kcontrol_t * kcontrol,
2131 snd_ctl_elem_value_t * ucontrol)
2133 cs46xx_t * chip = snd_kcontrol_chip(kcontrol);
2134 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2138 down (&chip->spos_mutex);
2139 val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
2140 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[2]) << 16) |
2141 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
2142 /* left and right validity bit */
2143 (1 << 13) | (1 << 12);
2146 change = (unsigned int)ins->spdif_csuv_default != val;
2147 ins->spdif_csuv_default = val;
2149 if ( !(ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN) )
2150 cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
2152 up (&chip->spos_mutex);
2157 static int snd_cs46xx_spdif_mask_get(snd_kcontrol_t * kcontrol,
2158 snd_ctl_elem_value_t * ucontrol)
2160 ucontrol->value.iec958.status[0] = 0xff;
2161 ucontrol->value.iec958.status[1] = 0xff;
2162 ucontrol->value.iec958.status[2] = 0x00;
2163 ucontrol->value.iec958.status[3] = 0xff;
2167 static int snd_cs46xx_spdif_stream_get(snd_kcontrol_t * kcontrol,
2168 snd_ctl_elem_value_t * ucontrol)
2170 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2171 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2173 down (&chip->spos_mutex);
2174 ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_stream >> 24) & 0xff);
2175 ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_stream >> 16) & 0xff);
2176 ucontrol->value.iec958.status[2] = 0;
2177 ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_stream) & 0xff);
2178 up (&chip->spos_mutex);
2183 static int snd_cs46xx_spdif_stream_put(snd_kcontrol_t * kcontrol,
2184 snd_ctl_elem_value_t * ucontrol)
2186 cs46xx_t * chip = snd_kcontrol_chip(kcontrol);
2187 dsp_spos_instance_t * ins = chip->dsp_spos_instance;
2191 down (&chip->spos_mutex);
2192 val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
2193 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[1]) << 16) |
2194 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
2195 /* left and right validity bit */
2196 (1 << 13) | (1 << 12);
2199 change = ins->spdif_csuv_stream != val;
2200 ins->spdif_csuv_stream = val;
2202 if ( ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN )
2203 cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
2205 up (&chip->spos_mutex);
2210 #endif /* CONFIG_SND_CS46XX_NEW_DSP */
2213 #ifdef CONFIG_SND_CS46XX_DEBUG_GPIO
2214 static int snd_cs46xx_egpio_select_info(snd_kcontrol_t *kcontrol,
2215 snd_ctl_elem_info_t *uinfo)
2217 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2219 uinfo->value.integer.min = 0;
2220 uinfo->value.integer.max = 8;
2224 static int snd_cs46xx_egpio_select_get(snd_kcontrol_t *kcontrol,
2225 snd_ctl_elem_value_t *ucontrol)
2227 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2228 ucontrol->value.integer.value[0] = chip->current_gpio;
2233 static int snd_cs46xx_egpio_select_put(snd_kcontrol_t *kcontrol,
2234 snd_ctl_elem_value_t *ucontrol)
2236 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2237 int change = (chip->current_gpio != ucontrol->value.integer.value[0]);
2238 chip->current_gpio = ucontrol->value.integer.value[0];
2244 static int snd_cs46xx_egpio_get(snd_kcontrol_t *kcontrol,
2245 snd_ctl_elem_value_t *ucontrol)
2247 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2248 int reg = kcontrol->private_value;
2250 snd_printdd ("put: reg = %04x, gpio %02x\n",reg,chip->current_gpio);
2251 ucontrol->value.integer.value[0] =
2252 (snd_cs46xx_peekBA0(chip, reg) & (1 << chip->current_gpio)) ? 1 : 0;
2257 static int snd_cs46xx_egpio_put(snd_kcontrol_t *kcontrol,
2258 snd_ctl_elem_value_t *ucontrol)
2260 cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
2261 int reg = kcontrol->private_value;
2262 int val = snd_cs46xx_peekBA0(chip, reg);
2264 snd_printdd ("put: reg = %04x, gpio %02x\n",reg,chip->current_gpio);
2266 if (ucontrol->value.integer.value[0])
2267 val |= (1 << chip->current_gpio);
2269 val &= ~(1 << chip->current_gpio);
2271 snd_cs46xx_pokeBA0(chip, reg,val);
2272 snd_printdd ("put: val %08x oldval %08x\n",val,oldval);
2274 return (oldval != val);
2276 #endif /* CONFIG_SND_CS46XX_DEBUG_GPIO */
2278 static snd_kcontrol_new_t snd_cs46xx_controls[] __devinitdata = {
2280 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2281 .name = "DAC Volume",
2282 .info = snd_cs46xx_vol_info,
2283 #ifndef CONFIG_SND_CS46XX_NEW_DSP
2284 .get = snd_cs46xx_vol_get,
2285 .put = snd_cs46xx_vol_put,
2286 .private_value = BA1_PVOL,
2288 .get = snd_cs46xx_vol_dac_get,
2289 .put = snd_cs46xx_vol_dac_put,
2294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2295 .name = "ADC Volume",
2296 .info = snd_cs46xx_vol_info,
2297 .get = snd_cs46xx_vol_get,
2298 .put = snd_cs46xx_vol_put,
2299 #ifndef CONFIG_SND_CS46XX_NEW_DSP
2300 .private_value = BA1_CVOL,
2302 .private_value = (VARIDECIMATE_SCB_ADDR + 0xE) << 2,
2305 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2308 .name = "ADC Capture Switch",
2309 .info = snd_mixer_boolean_info,
2310 .get = snd_cs46xx_adc_capture_get,
2311 .put = snd_cs46xx_adc_capture_put
2314 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2315 .name = "DAC Capture Switch",
2316 .info = snd_mixer_boolean_info,
2317 .get = snd_cs46xx_pcm_capture_get,
2318 .put = snd_cs46xx_pcm_capture_put
2321 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2322 .name = "IEC958 Output Switch",
2323 .info = snd_mixer_boolean_info,
2324 .get = snd_cs46xx_iec958_get,
2325 .put = snd_cs46xx_iec958_put,
2326 .private_value = CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT,
2329 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2330 .name = "IEC958 Input Switch",
2331 .info = snd_mixer_boolean_info,
2332 .get = snd_cs46xx_iec958_get,
2333 .put = snd_cs46xx_iec958_put,
2334 .private_value = CS46XX_MIXER_SPDIF_INPUT_ELEMENT,
2337 /* Input IEC958 volume does not work for the moment. (Benny) */
2339 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2340 .name = "IEC958 Input Volume",
2341 .info = snd_cs46xx_vol_info,
2342 .get = snd_cs46xx_vol_iec958_get,
2343 .put = snd_cs46xx_vol_iec958_put,
2344 .private_value = (ASYNCRX_SCB_ADDR + 0xE) << 2,
2348 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2349 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2350 .info = snd_cs46xx_spdif_info,
2351 .get = snd_cs46xx_spdif_default_get,
2352 .put = snd_cs46xx_spdif_default_put,
2355 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2356 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2357 .info = snd_cs46xx_spdif_info,
2358 .get = snd_cs46xx_spdif_mask_get,
2359 .access = SNDRV_CTL_ELEM_ACCESS_READ
2362 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2363 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2364 .info = snd_cs46xx_spdif_info,
2365 .get = snd_cs46xx_spdif_stream_get,
2366 .put = snd_cs46xx_spdif_stream_put
2370 #ifdef CONFIG_SND_CS46XX_DEBUG_GPIO
2372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2373 .name = "EGPIO select",
2374 .info = snd_cs46xx_egpio_select_info,
2375 .get = snd_cs46xx_egpio_select_get,
2376 .put = snd_cs46xx_egpio_select_put,
2380 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2381 .name = "EGPIO Input/Output",
2382 .info = snd_mixer_boolean_info,
2383 .get = snd_cs46xx_egpio_get,
2384 .put = snd_cs46xx_egpio_put,
2385 .private_value = BA0_EGPIODR,
2388 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2389 .name = "EGPIO CMOS/Open drain",
2390 .info = snd_mixer_boolean_info,
2391 .get = snd_cs46xx_egpio_get,
2392 .put = snd_cs46xx_egpio_put,
2393 .private_value = BA0_EGPIOPTR,
2396 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2397 .name = "EGPIO On/Off",
2398 .info = snd_mixer_boolean_info,
2399 .get = snd_cs46xx_egpio_get,
2400 .put = snd_cs46xx_egpio_put,
2401 .private_value = BA0_EGPIOSR,
2406 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2407 /* Only available on the Hercules Game Theater XP soundcard */
2408 static snd_kcontrol_new_t snd_hercules_controls[] __devinitdata = {
2410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2411 .name = "Optical/Coaxial SPDIF Input Switch",
2412 .info = snd_mixer_boolean_info,
2413 .get = snd_herc_spdif_select_get,
2414 .put = snd_herc_spdif_select_put,
2419 static void snd_cs46xx_codec_reset (ac97_t * ac97)
2421 unsigned long end_time;
2423 cs46xx_t * chip = snd_magic_cast(cs46xx_t,ac97->private_data,return /* -ENXIO */);
2425 /* reset to defaults */
2426 snd_ac97_write(ac97, AC97_RESET, 0);
2428 /* set the desired CODEC mode */
2429 if (chip->nr_ac97_codecs == 0) {
2430 snd_printdd("cs46xx: CODOEC1 mode %04x\n",0x0);
2431 snd_cs46xx_ac97_write(ac97,AC97_CSR_ACMODE,0x0);
2432 } else if (chip->nr_ac97_codecs == 1) {
2433 snd_printdd("cs46xx: CODOEC2 mode %04x\n",0x3);
2434 snd_cs46xx_ac97_write(ac97,AC97_CSR_ACMODE,0x3);
2436 snd_assert(0); /* should never happen ... */
2441 /* it's necessary to wait awhile until registers are accessible after RESET */
2442 /* because the PCM or MASTER volume registers can be modified, */
2443 /* the REC_GAIN register is used for tests */
2444 end_time = jiffies + HZ;
2446 unsigned short ext_mid;
2448 /* use preliminary reads to settle the communication */
2449 snd_ac97_read(ac97, AC97_RESET);
2450 snd_ac97_read(ac97, AC97_VENDOR_ID1);
2451 snd_ac97_read(ac97, AC97_VENDOR_ID2);
2453 ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2454 if (ext_mid != 0xffff && (ext_mid & 1) != 0)
2457 /* test if we can write to the record gain volume register */
2458 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
2459 if ((err = snd_ac97_read(ac97, AC97_REC_GAIN)) == 0x8a05)
2462 set_current_state(TASK_UNINTERRUPTIBLE);
2463 schedule_timeout(HZ/100);
2464 } while (time_after_eq(end_time, jiffies));
2466 snd_printk("CS46xx secondary codec dont respond!\n");
2470 int __devinit snd_cs46xx_mixer(cs46xx_t *chip)
2472 snd_card_t *card = chip->card;
2475 snd_ctl_elem_id_t id;
2479 /* detect primary codec */
2480 chip->nr_ac97_codecs = 0;
2481 snd_printdd("snd_cs46xx: detecting primary codec\n");
2482 memset(&bus, 0, sizeof(bus));
2483 bus.write = snd_cs46xx_ac97_write;
2484 bus.read = snd_cs46xx_ac97_read;
2485 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2486 bus.reset = snd_cs46xx_codec_reset;
2488 bus.private_data = chip;
2489 bus.private_free = snd_cs46xx_mixer_free_ac97_bus;
2490 if ((err = snd_ac97_bus(card, &bus, &chip->ac97_bus)) < 0)
2493 memset(&ac97, 0, sizeof(ac97));
2494 ac97.private_data = chip;
2495 ac97.private_free = snd_cs46xx_mixer_free_ac97;
2496 chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = &ac97;
2498 snd_cs46xx_ac97_write(&ac97, AC97_MASTER, 0x8000);
2499 for (idx = 0; idx < 100; ++idx) {
2500 if (snd_cs46xx_ac97_read(&ac97, AC97_MASTER) == 0x8000)
2502 set_current_state(TASK_INTERRUPTIBLE);
2503 schedule_timeout(HZ/100);
2505 chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = NULL;
2509 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97[CS46XX_PRIMARY_CODEC_INDEX])) < 0)
2511 snd_printdd("snd_cs46xx: primary codec phase one\n");
2512 chip->nr_ac97_codecs = 1;
2514 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2515 snd_printdd("snd_cs46xx: detecting seconadry codec\n");
2516 /* try detect a secondary codec */
2517 memset(&ac97, 0, sizeof(ac97));
2518 ac97.private_data = chip;
2519 ac97.private_free = snd_cs46xx_mixer_free_ac97;
2520 ac97.num = CS46XX_SECONDARY_CODEC_INDEX;
2522 snd_cs46xx_ac97_write(&ac97, AC97_RESET, 0);
2525 if (snd_cs46xx_ac97_read(&ac97, AC97_RESET) & 0x8000) {
2526 snd_printdd("snd_cs46xx: seconadry codec not present\n");
2530 chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = &ac97;
2531 snd_cs46xx_ac97_write(&ac97, AC97_MASTER, 0x8000);
2532 for (idx = 0; idx < 100; ++idx) {
2533 if (snd_cs46xx_ac97_read(&ac97, AC97_MASTER) == 0x8000) {
2536 set_current_state(TASK_INTERRUPTIBLE);
2537 schedule_timeout(HZ/100);
2541 snd_printdd("snd_cs46xx: secondary codec did not respond ...\n");
2543 chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = NULL;
2544 chip->nr_ac97_codecs = 1;
2546 /* well, one codec only ... */
2549 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97[CS46XX_SECONDARY_CODEC_INDEX])) < 0)
2551 chip->nr_ac97_codecs = 2;
2555 #endif /* CONFIG_SND_CS46XX_NEW_DSP */
2557 /* add cs4630 mixer controls */
2558 for (idx = 0; idx < ARRAY_SIZE(snd_cs46xx_controls); idx++) {
2559 snd_kcontrol_t *kctl;
2560 kctl = snd_ctl_new1(&snd_cs46xx_controls[idx], chip);
2561 if ((err = snd_ctl_add(card, kctl)) < 0)
2565 /* get EAPD mixer switch (for voyetra hack) */
2566 memset(&id, 0, sizeof(id));
2567 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2568 strcpy(id.name, "External Amplifier");
2569 chip->eapd_switch = snd_ctl_find_id(chip->card, &id);
2571 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2572 if (chip->nr_ac97_codecs == 1 &&
2573 (snd_cs46xx_codec_read(chip, AC97_VENDOR_ID2,
2574 CS46XX_PRIMARY_CODEC_INDEX) == 0x592b ||
2575 snd_cs46xx_codec_read(chip, AC97_VENDOR_ID2,
2576 CS46XX_PRIMARY_CODEC_INDEX) == 0x592d)) {
2577 /* set primary cs4294 codec into Extended Audio Mode */
2578 snd_printdd("setting EAM bit on cs4294 CODEC\n");
2579 snd_cs46xx_codec_write(chip, AC97_CSR_ACMODE, 0x200,
2580 CS46XX_PRIMARY_CODEC_INDEX);
2582 /* do soundcard specific mixer setup */
2583 if (chip->mixer_init) {
2584 snd_printdd ("calling chip->mixer_init(chip);\n");
2585 chip->mixer_init(chip);
2589 /* turn on amplifier */
2590 chip->amplifier_ctrl(chip, 1);
2599 static void snd_cs46xx_midi_reset(cs46xx_t *chip)
2601 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, MIDCR_MRST);
2603 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2606 static int snd_cs46xx_midi_input_open(snd_rawmidi_substream_t * substream)
2608 unsigned long flags;
2609 cs46xx_t *chip = snd_magic_cast(cs46xx_t, substream->rmidi->private_data, return -ENXIO);
2611 chip->active_ctrl(chip, 1);
2612 spin_lock_irqsave(&chip->reg_lock, flags);
2613 chip->uartm |= CS46XX_MODE_INPUT;
2614 chip->midcr |= MIDCR_RXE;
2615 chip->midi_input = substream;
2616 if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
2617 snd_cs46xx_midi_reset(chip);
2619 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2621 spin_unlock_irqrestore(&chip->reg_lock, flags);
2625 static int snd_cs46xx_midi_input_close(snd_rawmidi_substream_t * substream)
2627 unsigned long flags;
2628 cs46xx_t *chip = snd_magic_cast(cs46xx_t, substream->rmidi->private_data, return -ENXIO);
2630 spin_lock_irqsave(&chip->reg_lock, flags);
2631 chip->midcr &= ~(MIDCR_RXE | MIDCR_RIE);
2632 chip->midi_input = NULL;
2633 if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
2634 snd_cs46xx_midi_reset(chip);
2636 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2638 chip->uartm &= ~CS46XX_MODE_INPUT;
2639 spin_unlock_irqrestore(&chip->reg_lock, flags);
2640 chip->active_ctrl(chip, -1);
2644 static int snd_cs46xx_midi_output_open(snd_rawmidi_substream_t * substream)
2646 unsigned long flags;
2647 cs46xx_t *chip = snd_magic_cast(cs46xx_t, substream->rmidi->private_data, return -ENXIO);
2649 chip->active_ctrl(chip, 1);
2651 spin_lock_irqsave(&chip->reg_lock, flags);
2652 chip->uartm |= CS46XX_MODE_OUTPUT;
2653 chip->midcr |= MIDCR_TXE;
2654 chip->midi_output = substream;
2655 if (!(chip->uartm & CS46XX_MODE_INPUT)) {
2656 snd_cs46xx_midi_reset(chip);
2658 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2660 spin_unlock_irqrestore(&chip->reg_lock, flags);
2664 static int snd_cs46xx_midi_output_close(snd_rawmidi_substream_t * substream)
2666 unsigned long flags;
2667 cs46xx_t *chip = snd_magic_cast(cs46xx_t, substream->rmidi->private_data, return -ENXIO);
2669 spin_lock_irqsave(&chip->reg_lock, flags);
2670 chip->midcr &= ~(MIDCR_TXE | MIDCR_TIE);
2671 chip->midi_output = NULL;
2672 if (!(chip->uartm & CS46XX_MODE_INPUT)) {
2673 snd_cs46xx_midi_reset(chip);
2675 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2677 chip->uartm &= ~CS46XX_MODE_OUTPUT;
2678 spin_unlock_irqrestore(&chip->reg_lock, flags);
2679 chip->active_ctrl(chip, -1);
2683 static void snd_cs46xx_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
2685 unsigned long flags;
2686 cs46xx_t *chip = snd_magic_cast(cs46xx_t, substream->rmidi->private_data, return);
2688 spin_lock_irqsave(&chip->reg_lock, flags);
2690 if ((chip->midcr & MIDCR_RIE) == 0) {
2691 chip->midcr |= MIDCR_RIE;
2692 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2695 if (chip->midcr & MIDCR_RIE) {
2696 chip->midcr &= ~MIDCR_RIE;
2697 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2700 spin_unlock_irqrestore(&chip->reg_lock, flags);
2703 static void snd_cs46xx_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
2705 unsigned long flags;
2706 cs46xx_t *chip = snd_magic_cast(cs46xx_t, substream->rmidi->private_data, return);
2709 spin_lock_irqsave(&chip->reg_lock, flags);
2711 if ((chip->midcr & MIDCR_TIE) == 0) {
2712 chip->midcr |= MIDCR_TIE;
2713 /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
2714 while ((chip->midcr & MIDCR_TIE) &&
2715 (snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
2716 if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
2717 chip->midcr &= ~MIDCR_TIE;
2719 snd_cs46xx_pokeBA0(chip, BA0_MIDWP, byte);
2722 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2725 if (chip->midcr & MIDCR_TIE) {
2726 chip->midcr &= ~MIDCR_TIE;
2727 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2730 spin_unlock_irqrestore(&chip->reg_lock, flags);
2733 static snd_rawmidi_ops_t snd_cs46xx_midi_output =
2735 .open = snd_cs46xx_midi_output_open,
2736 .close = snd_cs46xx_midi_output_close,
2737 .trigger = snd_cs46xx_midi_output_trigger,
2740 static snd_rawmidi_ops_t snd_cs46xx_midi_input =
2742 .open = snd_cs46xx_midi_input_open,
2743 .close = snd_cs46xx_midi_input_close,
2744 .trigger = snd_cs46xx_midi_input_trigger,
2747 int __devinit snd_cs46xx_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
2749 snd_rawmidi_t *rmidi;
2754 if ((err = snd_rawmidi_new(chip->card, "CS46XX", device, 1, 1, &rmidi)) < 0)
2756 strcpy(rmidi->name, "CS46XX");
2757 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs46xx_midi_output);
2758 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs46xx_midi_input);
2759 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2760 rmidi->private_data = chip;
2761 chip->rmidi = rmidi;
2769 * gameport interface
2772 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
2774 typedef struct snd_cs46xx_gameport {
2775 struct gameport info;
2777 } cs46xx_gameport_t;
2779 static void snd_cs46xx_gameport_trigger(struct gameport *gameport)
2781 cs46xx_gameport_t *gp = (cs46xx_gameport_t *)gameport;
2783 snd_assert(gp, return);
2784 chip = snd_magic_cast(cs46xx_t, gp->chip, return);
2785 snd_cs46xx_pokeBA0(chip, BA0_JSPT, 0xFF); //outb(gameport->io, 0xFF);
2788 static unsigned char snd_cs46xx_gameport_read(struct gameport *gameport)
2790 cs46xx_gameport_t *gp = (cs46xx_gameport_t *)gameport;
2792 snd_assert(gp, return 0);
2793 chip = snd_magic_cast(cs46xx_t, gp->chip, return 0);
2794 return snd_cs46xx_peekBA0(chip, BA0_JSPT); //inb(gameport->io);
2797 static int snd_cs46xx_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
2799 cs46xx_gameport_t *gp = (cs46xx_gameport_t *)gameport;
2801 unsigned js1, js2, jst;
2803 snd_assert(gp, return 0);
2804 chip = snd_magic_cast(cs46xx_t, gp->chip, return 0);
2806 js1 = snd_cs46xx_peekBA0(chip, BA0_JSC1);
2807 js2 = snd_cs46xx_peekBA0(chip, BA0_JSC2);
2808 jst = snd_cs46xx_peekBA0(chip, BA0_JSPT);
2810 *buttons = (~jst >> 4) & 0x0F;
2812 axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
2813 axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
2814 axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
2815 axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
2817 for(jst=0;jst<4;++jst)
2818 if(axes[jst]==0xFFFF) axes[jst] = -1;
2822 static int snd_cs46xx_gameport_open(struct gameport *gameport, int mode)
2825 case GAMEPORT_MODE_COOKED:
2827 case GAMEPORT_MODE_RAW:
2835 void __devinit snd_cs46xx_gameport(cs46xx_t *chip)
2837 cs46xx_gameport_t *gp;
2838 gp = kmalloc(sizeof(*gp), GFP_KERNEL);
2840 snd_printk("cannot allocate gameport area\n");
2843 memset(gp, 0, sizeof(*gp));
2844 gp->info.open = snd_cs46xx_gameport_open;
2845 gp->info.read = snd_cs46xx_gameport_read;
2846 gp->info.trigger = snd_cs46xx_gameport_trigger;
2847 gp->info.cooked_read = snd_cs46xx_gameport_cooked_read;
2849 chip->gameport = gp;
2851 snd_cs46xx_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
2852 snd_cs46xx_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
2853 gameport_register_port(&gp->info);
2858 void __devinit snd_cs46xx_gameport(cs46xx_t *chip)
2862 #endif /* CONFIG_GAMEPORT */
2868 static long snd_cs46xx_io_read(snd_info_entry_t *entry, void *file_private_data,
2869 struct file *file, char __user *buf, long count)
2872 snd_cs46xx_region_t *region = (snd_cs46xx_region_t *)entry->private_data;
2875 if (file->f_pos + (size_t)size > region->size)
2876 size = region->size - file->f_pos;
2878 if (copy_to_user_fromio(buf, region->remap_addr + file->f_pos, size))
2880 file->f_pos += size;
2885 static struct snd_info_entry_ops snd_cs46xx_proc_io_ops = {
2886 .read = snd_cs46xx_io_read,
2889 static int __devinit snd_cs46xx_proc_init(snd_card_t * card, cs46xx_t *chip)
2891 snd_info_entry_t *entry;
2894 for (idx = 0; idx < 5; idx++) {
2895 snd_cs46xx_region_t *region = &chip->region.idx[idx];
2896 if (! snd_card_proc_new(card, region->name, &entry)) {
2897 entry->content = SNDRV_INFO_CONTENT_DATA;
2898 entry->private_data = chip;
2899 entry->c.ops = &snd_cs46xx_proc_io_ops;
2900 entry->size = region->size;
2901 entry->mode = S_IFREG | S_IRUSR;
2904 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2905 cs46xx_dsp_proc_init(card, chip);
2910 static int snd_cs46xx_proc_done(cs46xx_t *chip)
2912 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2913 cs46xx_dsp_proc_done(chip);
2921 static void snd_cs46xx_hw_stop(cs46xx_t *chip)
2925 tmp = snd_cs46xx_peek(chip, BA1_PFIE);
2928 snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt disable */
2930 tmp = snd_cs46xx_peek(chip, BA1_CIE);
2933 snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt disable */
2936 * Stop playback DMA.
2938 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
2939 snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
2944 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
2945 snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
2948 * Reset the processor.
2950 snd_cs46xx_reset(chip);
2952 snd_cs46xx_proc_stop(chip);
2955 * Power down the PLL.
2957 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
2960 * Turn off the Processor by turning off the software clock enable flag in
2961 * the clock control register.
2963 tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE;
2964 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
2968 static int snd_cs46xx_free(cs46xx_t *chip)
2972 snd_assert(chip != NULL, return -EINVAL);
2974 if (chip->active_ctrl)
2975 chip->active_ctrl(chip, 1);
2977 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
2978 if (chip->gameport) {
2979 gameport_unregister_port(&chip->gameport->info);
2980 kfree(chip->gameport);
2984 if (chip->amplifier_ctrl)
2985 chip->amplifier_ctrl(chip, -chip->amplifier); /* force to off */
2987 snd_cs46xx_proc_done(chip);
2989 if (chip->region.idx[0].resource)
2990 snd_cs46xx_hw_stop(chip);
2992 for (idx = 0; idx < 5; idx++) {
2993 snd_cs46xx_region_t *region = &chip->region.idx[idx];
2994 if (region->remap_addr)
2995 iounmap((void *) region->remap_addr);
2996 if (region->resource) {
2997 release_resource(region->resource);
2998 kfree_nocheck(region->resource);
3002 free_irq(chip->irq, (void *)chip);
3004 if (chip->active_ctrl)
3005 chip->active_ctrl(chip, -chip->amplifier);
3007 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3008 if (chip->dsp_spos_instance) {
3009 cs46xx_dsp_spos_destroy(chip);
3010 chip->dsp_spos_instance = NULL;
3014 snd_magic_kfree(chip);
3018 static int snd_cs46xx_dev_free(snd_device_t *device)
3020 cs46xx_t *chip = snd_magic_cast(cs46xx_t, device->device_data, return -ENXIO);
3021 return snd_cs46xx_free(chip);
3027 static int snd_cs46xx_chip_init(cs46xx_t *chip)
3032 * First, blast the clock control register to zero so that the PLL starts
3033 * out in a known state, and blast the master serial port control register
3034 * to zero so that the serial ports also start out in a known state.
3036 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
3037 snd_cs46xx_pokeBA0(chip, BA0_SERMC1, 0);
3040 * If we are in AC97 mode, then we must set the part to a host controlled
3041 * AC-link. Otherwise, we won't be able to bring up the link.
3043 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3044 snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0 |
3045 SERACC_TWO_CODECS); /* 2.00 dual codecs */
3046 /* snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0); */ /* 2.00 codec */
3048 snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_1_03); /* 1.03 codec */
3052 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
3053 * spec) and then drive it high. This is done for non AC97 modes since
3054 * there might be logic external to the CS461x that uses the ARST# line
3057 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, 0);
3058 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3059 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, 0);
3062 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_RSTN);
3063 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3064 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_RSTN);
3068 * The first thing we do here is to enable sync generation. As soon
3069 * as we start receiving bit clock, we'll start producing the SYNC
3072 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
3073 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3074 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_ESYN | ACCTL_RSTN);
3078 * Now wait for a short while to allow the AC97 part to start
3079 * generating bit clock (so we don't try to start the PLL without an
3085 * Set the serial port timing configuration, so that
3086 * the clock control circuit gets its clock from the correct place.
3088 snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97);
3091 * Write the selected clock control setup to the hardware. Do not turn on
3092 * SWCE yet (if requested), so that the devices clocked by the output of
3093 * PLL are not clocked until the PLL is stable.
3095 snd_cs46xx_pokeBA0(chip, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
3096 snd_cs46xx_pokeBA0(chip, BA0_PLLM, 0x3a);
3097 snd_cs46xx_pokeBA0(chip, BA0_CLKCR2, CLKCR2_PDIVS_8);
3102 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP);
3105 * Wait until the PLL has stabilized.
3107 set_current_state(TASK_UNINTERRUPTIBLE);
3108 schedule_timeout(HZ/10); /* 100ms */
3111 * Turn on clocking of the core so that we can setup the serial ports.
3113 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP | CLKCR1_SWCE);
3116 * Enable FIFO Host Bypass
3118 snd_cs46xx_pokeBA0(chip, BA0_SERBCF, SERBCF_HBP);
3121 * Fill the serial port FIFOs with silence.
3123 snd_cs46xx_clear_serial_FIFOs(chip);
3126 * Set the serial port FIFO pointer to the first sample in the FIFO.
3128 /* snd_cs46xx_pokeBA0(chip, BA0_SERBSP, 0); */
3131 * Write the serial port configuration to the part. The master
3132 * enable bit is not set until all other values have been written.
3134 snd_cs46xx_pokeBA0(chip, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
3135 snd_cs46xx_pokeBA0(chip, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
3136 snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
3139 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3140 snd_cs46xx_pokeBA0(chip, BA0_SERC7, SERC7_ASDI2EN);
3141 snd_cs46xx_pokeBA0(chip, BA0_SERC3, 0);
3142 snd_cs46xx_pokeBA0(chip, BA0_SERC4, 0);
3143 snd_cs46xx_pokeBA0(chip, BA0_SERC5, 0);
3144 snd_cs46xx_pokeBA0(chip, BA0_SERC6, 1);
3151 * Wait for the codec ready signal from the AC97 codec.
3154 while (timeout-- > 0) {
3156 * Read the AC97 status register to see if we've seen a CODEC READY
3157 * signal from the AC97 codec.
3159 if (snd_cs46xx_peekBA0(chip, BA0_ACSTS) & ACSTS_CRDY)
3161 set_current_state(TASK_UNINTERRUPTIBLE);
3162 schedule_timeout((HZ+99)/100);
3166 snd_printk("create - never read codec ready from AC'97\n");
3167 snd_printk("it is not probably bug, try to use CS4236 driver\n");
3170 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3173 for (count = 0; count < 150; count++) {
3174 /* First, we want to wait for a short time. */
3177 if (snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY)
3182 * Make sure CODEC is READY.
3184 if (!(snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY))
3185 snd_printdd("cs46xx: never read card ready from secondary AC'97\n");
3190 * Assert the vaid frame signal so that we can start sending commands
3191 * to the AC97 codec.
3193 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3194 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3195 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3200 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
3201 * the codec is pumping ADC data across the AC-link.
3204 while (timeout-- > 0) {
3206 * Read the input slot valid register and see if input slots 3 and
3209 if ((snd_cs46xx_peekBA0(chip, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
3211 set_current_state(TASK_UNINTERRUPTIBLE);
3212 schedule_timeout((HZ+99)/100);
3215 #ifndef CONFIG_SND_CS46XX_NEW_DSP
3216 snd_printk("create - never read ISV3 & ISV4 from AC'97\n");
3219 /* This may happen on a cold boot with a Terratec SiXPack 5.1.
3220 Reloading the driver may help, if there's other soundcards
3221 with the same problem I would like to know. (Benny) */
3223 snd_printk("ERROR: snd-cs46xx: never read ISV3 & ISV4 from AC'97\n");
3224 snd_printk(" Try reloading the ALSA driver, if you find something\n");
3225 snd_printk(" broken or not working on your soundcard upon\n");
3226 snd_printk(" this message please report to alsa-devel@lists.sourceforge.net\n");
3233 * Now, assert valid frame and the slot 3 and 4 valid bits. This will
3234 * commense the transfer of digital audio data to the AC97 codec.
3237 snd_cs46xx_pokeBA0(chip, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
3241 * Power down the DAC and ADC. We will power them up (if) when we need
3244 /* snd_cs46xx_pokeBA0(chip, BA0_AC97_POWERDOWN, 0x300); */
3247 * Turn off the Processor by turning off the software clock enable flag in
3248 * the clock control register.
3250 /* tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE; */
3251 /* snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp); */
3257 * start and load DSP
3259 int __devinit snd_cs46xx_start_dsp(cs46xx_t *chip)
3263 * Reset the processor.
3265 snd_cs46xx_reset(chip);
3267 * Download the image to the processor.
3269 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3271 if (cs46xx_dsp_load_module(chip, &cwcemb80_module) < 0) {
3272 snd_printk(KERN_ERR "image download error\n");
3277 if (cs46xx_dsp_load_module(chip, &cwc4630_module) < 0) {
3278 snd_printk(KERN_ERR "image download error [cwc4630]\n");
3282 if (cs46xx_dsp_load_module(chip, &cwcasync_module) < 0) {
3283 snd_printk(KERN_ERR "image download error [cwcasync]\n");
3287 if (cs46xx_dsp_load_module(chip, &cwcsnoop_module) < 0) {
3288 snd_printk(KERN_ERR "image download error [cwcsnoop]\n");
3292 if (cs46xx_dsp_load_module(chip, &cwcbinhack_module) < 0) {
3293 snd_printk(KERN_ERR "image download error [cwcbinhack]\n");
3297 if (cs46xx_dsp_load_module(chip, &cwcdma_module) < 0) {
3298 snd_printk(KERN_ERR "image download error [cwcdma]\n");
3302 if (cs46xx_dsp_scb_and_task_init(chip) < 0)
3306 if (snd_cs46xx_download_image(chip) < 0) {
3307 snd_printk("image download error\n");
3312 * Stop playback DMA.
3314 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
3315 chip->play_ctl = tmp & 0xffff0000;
3316 snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
3322 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
3323 chip->capt.ctl = tmp & 0x0000ffff;
3324 snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
3328 snd_cs46xx_set_play_sample_rate(chip, 8000);
3329 snd_cs46xx_set_capture_sample_rate(chip, 8000);
3331 snd_cs46xx_proc_start(chip);
3334 * Enable interrupts on the part.
3336 snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_IEV | HICR_CHGM);
3338 tmp = snd_cs46xx_peek(chip, BA1_PFIE);
3340 snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt enable */
3342 tmp = snd_cs46xx_peek(chip, BA1_CIE);
3345 snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt enable */
3347 #ifndef CONFIG_SND_CS46XX_NEW_DSP
3348 /* set the attenuation to 0dB */
3349 snd_cs46xx_poke(chip, BA1_PVOL, 0x80008000);
3350 snd_cs46xx_poke(chip, BA1_CVOL, 0x80008000);
3358 * AMP control - null AMP
3361 static void amp_none(cs46xx_t *chip, int change)
3365 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3366 static int voyetra_setup_eapd_slot(cs46xx_t *chip)
3369 u32 idx, valid_slots,tmp,powerdown = 0;
3370 u16 modem_power,pin_config,logic_type;
3372 snd_printdd ("cs46xx: cs46xx_setup_eapd_slot()+\n");
3375 * See if the devices are powered down. If so, we must power them up first
3376 * or they will not respond.
3378 tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
3380 if (!(tmp & CLKCR1_SWCE)) {
3381 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
3386 * Clear PRA. The Bonzo chip will be used for GPIO not for modem
3389 if(chip->nr_ac97_codecs != 2) {
3390 snd_printk (KERN_ERR "cs46xx: cs46xx_setup_eapd_slot() - no secondary codec configured\n");
3394 modem_power = snd_cs46xx_codec_read (chip,
3395 AC97_EXTENDED_MSTATUS,
3396 CS46XX_SECONDARY_CODEC_INDEX);
3397 modem_power &=0xFEFF;
3399 snd_cs46xx_codec_write(chip,
3400 AC97_EXTENDED_MSTATUS, modem_power,
3401 CS46XX_SECONDARY_CODEC_INDEX);
3404 * Set GPIO pin's 7 and 8 so that they are configured for output.
3406 pin_config = snd_cs46xx_codec_read (chip,
3408 CS46XX_SECONDARY_CODEC_INDEX);
3411 snd_cs46xx_codec_write(chip,
3412 AC97_GPIO_CFG, pin_config,
3413 CS46XX_SECONDARY_CODEC_INDEX);
3416 * Set GPIO pin's 7 and 8 so that they are compatible with CMOS logic.
3419 logic_type = snd_cs46xx_codec_read(chip, AC97_GPIO_POLARITY,
3420 CS46XX_SECONDARY_CODEC_INDEX);
3423 snd_cs46xx_codec_write (chip, AC97_GPIO_POLARITY, logic_type,
3424 CS46XX_SECONDARY_CODEC_INDEX);
3426 valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
3427 valid_slots |= 0x200;
3428 snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
3430 if ( cs46xx_wait_for_fifo(chip,1) ) {
3431 snd_printdd("FIFO is busy\n");
3437 * Fill slots 12 with the correct value for the GPIO pins.
3439 for(idx = 0x90; idx <= 0x9F; idx++) {
3441 * Initialize the fifo so that bits 7 and 8 are on.
3443 * Remember that the GPIO pins in bonzo are shifted by 4 bits to
3444 * the left. 0x1800 corresponds to bits 7 and 8.
3446 snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0x1800);
3449 * Wait for command to complete
3451 if ( cs46xx_wait_for_fifo(chip,200) ) {
3452 snd_printdd("failed waiting for FIFO at addr (%02X)\n",idx);
3458 * Write the serial port FIFO index.
3460 snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
3463 * Tell the serial port to load the new value into the FIFO location.
3465 snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
3468 /* wait for last command to complete */
3469 cs46xx_wait_for_fifo(chip,200);
3472 * Now, if we powered up the devices, then power them back down again.
3473 * This is kinda ugly, but should never happen.
3476 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
3486 static void amp_voyetra(cs46xx_t *chip, int change)
3488 /* Manage the EAPD bit on the Crystal 4297
3489 and the Analog AD1885 */
3491 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3492 int old = chip->amplifier;
3496 chip->amplifier += change;
3497 oval = snd_cs46xx_codec_read(chip, AC97_POWERDOWN,
3498 CS46XX_PRIMARY_CODEC_INDEX);
3500 if (chip->amplifier) {
3501 /* Turn the EAPD amp on */
3504 /* Turn the EAPD amp off */
3508 snd_cs46xx_codec_write(chip, AC97_POWERDOWN, val,
3509 CS46XX_PRIMARY_CODEC_INDEX);
3510 if (chip->eapd_switch)
3511 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3512 &chip->eapd_switch->id);
3515 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3516 if (chip->amplifier && !old) {
3517 voyetra_setup_eapd_slot(chip);
3522 static void hercules_init(cs46xx_t *chip)
3524 /* default: AMP off, and SPDIF input optical */
3525 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
3526 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
3531 * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
3533 static void amp_hercules(cs46xx_t *chip, int change)
3535 int old = chip->amplifier;
3536 int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
3537 int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
3539 chip->amplifier += change;
3540 if (chip->amplifier && !old) {
3541 snd_printdd ("Hercules amplifier ON\n");
3543 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
3544 EGPIODR_GPOE2 | val1); /* enable EGPIO2 output */
3545 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
3546 EGPIOPTR_GPPT2 | val2); /* open-drain on output */
3547 } else if (old && !chip->amplifier) {
3548 snd_printdd ("Hercules amplifier OFF\n");
3549 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE2); /* disable */
3550 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT2); /* disable */
3554 static void voyetra_mixer_init (cs46xx_t *chip)
3556 snd_printdd ("initializing Voyetra mixer\n");
3558 /* Enable SPDIF out */
3559 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
3560 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
3563 static void hercules_mixer_init (cs46xx_t *chip)
3565 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3568 snd_card_t *card = chip->card;
3571 /* set EGPIO to default */
3572 hercules_init(chip);
3574 snd_printdd ("initializing Hercules mixer\n");
3576 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3577 for (idx = 0 ; idx < ARRAY_SIZE(snd_hercules_controls); idx++) {
3578 snd_kcontrol_t *kctl;
3580 kctl = snd_ctl_new1(&snd_hercules_controls[idx], chip);
3581 if ((err = snd_ctl_add(card, kctl)) < 0) {
3582 printk (KERN_ERR "cs46xx: failed to initialize Hercules mixer (%d)\n",err);
3595 static void amp_voyetra_4294(cs46xx_t *chip, int change)
3597 chip->amplifier += change;
3599 if (chip->amplifier) {
3600 /* Switch the GPIO pins 7 and 8 to open drain */
3601 snd_cs46xx_codec_write(chip, 0x4C,
3602 snd_cs46xx_codec_read(chip, 0x4C) & 0xFE7F);
3603 snd_cs46xx_codec_write(chip, 0x4E,
3604 snd_cs46xx_codec_read(chip, 0x4E) | 0x0180);
3605 /* Now wake the AMP (this might be backwards) */
3606 snd_cs46xx_codec_write(chip, 0x54,
3607 snd_cs46xx_codec_read(chip, 0x54) & ~0x0180);
3609 snd_cs46xx_codec_write(chip, 0x54,
3610 snd_cs46xx_codec_read(chip, 0x54) | 0x0180);
3619 #ifndef PCI_VENDOR_ID_INTEL
3620 #define PCI_VENDOR_ID_INTEL 0x8086
3621 #endif /* PCI_VENDOR_ID_INTEL */
3623 #ifndef PCI_DEVICE_ID_INTEL_82371AB_3
3624 #define PCI_DEVICE_ID_INTEL_82371AB_3 0x7113
3625 #endif /* PCI_DEVICE_ID_INTEL_82371AB_3 */
3628 * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
3629 * whenever we need to beat on the chip.
3631 * The original idea and code for this hack comes from David Kaiser at
3632 * Linuxcare. Perhaps one day Crystal will document their chips well
3633 * enough to make them useful.
3636 static void clkrun_hack(cs46xx_t *chip, int change)
3640 if (chip->acpi_dev == NULL)
3643 chip->amplifier += change;
3645 /* Read ACPI port */
3646 nval = control = inw(chip->acpi_port + 0x10);
3648 /* Flip CLKRUN off while running */
3649 if (! chip->amplifier)
3653 if (nval != control)
3654 outw(nval, chip->acpi_port + 0x10);
3659 * detect intel piix4
3661 static void clkrun_init(cs46xx_t *chip)
3665 chip->acpi_dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3666 if (chip->acpi_dev == NULL)
3667 return; /* Not a thinkpad thats for sure */
3669 /* Find the control port */
3670 pci_read_config_byte(chip->acpi_dev, 0x41, &pp);
3671 chip->acpi_port = pp << 8;
3684 void (*init)(cs46xx_t *);
3685 void (*amp)(cs46xx_t *, int);
3686 void (*active)(cs46xx_t *, int);
3687 void (*mixer_init)(cs46xx_t *);
3690 static struct cs_card_type __devinitdata cards[] = {
3694 .name = "Genius Soundmaker 128 value",
3695 /* nothing special */
3702 .mixer_init = voyetra_mixer_init,
3707 .name = "Mitac MI6020/21",
3713 .name = "Hercules Game Theatre XP",
3714 .amp = amp_hercules,
3715 .mixer_init = hercules_mixer_init,
3720 .name = "Hercules Game Theatre XP",
3721 .amp = amp_hercules,
3722 .mixer_init = hercules_mixer_init,
3727 .name = "Hercules Game Theatre XP",
3728 .amp = amp_hercules,
3729 .mixer_init = hercules_mixer_init,
3735 .name = "Hercules Game Theatre XP",
3736 .amp = amp_hercules,
3737 .mixer_init = hercules_mixer_init,
3742 .name = "Hercules Game Theatre XP",
3743 .amp = amp_hercules,
3744 .mixer_init = hercules_mixer_init,
3749 .name = "Hercules Game Theatre XP",
3750 .amp = amp_hercules,
3751 .mixer_init = hercules_mixer_init,
3757 .name = "Terratec SiXPack 5.1",
3759 /* Not sure if the 570 needs the clkrun hack */
3761 .vendor = PCI_VENDOR_ID_IBM,
3763 .name = "Thinkpad 570",
3764 .init = clkrun_init,
3765 .active = clkrun_hack,
3768 .vendor = PCI_VENDOR_ID_IBM,
3770 .name = "Thinkpad 600X/A20/T20",
3771 .init = clkrun_init,
3772 .active = clkrun_hack,
3775 .vendor = PCI_VENDOR_ID_IBM,
3777 .name = "Thinkpad 600E (unsupported)",
3787 static int snd_cs46xx_suspend(snd_card_t *card, unsigned int state)
3789 cs46xx_t *chip = snd_magic_cast(cs46xx_t, card->pm_private_data, return -EINVAL);
3792 snd_pcm_suspend_all(chip->pcm);
3793 // chip->ac97_powerdown = snd_cs46xx_codec_read(chip, AC97_POWER_CONTROL);
3794 // chip->ac97_general_purpose = snd_cs46xx_codec_read(chip, BA0_AC97_GENERAL_PURPOSE);
3796 snd_ac97_suspend(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
3797 if (chip->ac97[CS46XX_SECONDARY_CODEC_INDEX])
3798 snd_ac97_suspend(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
3800 amp_saved = chip->amplifier;
3802 chip->amplifier_ctrl(chip, -chip->amplifier);
3803 snd_cs46xx_hw_stop(chip);
3804 /* disable CLKRUN */
3805 chip->active_ctrl(chip, -chip->amplifier);
3806 chip->amplifier = amp_saved; /* restore the status */
3807 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3811 static int snd_cs46xx_resume(snd_card_t *card, unsigned int state)
3813 cs46xx_t *chip = snd_magic_cast(cs46xx_t, card->pm_private_data, return -EINVAL);
3816 pci_enable_device(chip->pci);
3817 amp_saved = chip->amplifier;
3818 chip->amplifier = 0;
3819 chip->active_ctrl(chip, 1); /* force to on */
3821 snd_cs46xx_chip_init(chip);
3824 snd_cs46xx_codec_write(chip, BA0_AC97_GENERAL_PURPOSE,
3825 chip->ac97_general_purpose);
3826 snd_cs46xx_codec_write(chip, AC97_POWER_CONTROL,
3827 chip->ac97_powerdown);
3829 snd_cs46xx_codec_write(chip, BA0_AC97_POWERDOWN,
3830 chip->ac97_powerdown);
3834 snd_ac97_resume(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
3835 if (chip->ac97[CS46XX_SECONDARY_CODEC_INDEX])
3836 snd_ac97_resume(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
3839 chip->amplifier_ctrl(chip, 1); /* turn amp on */
3841 chip->active_ctrl(chip, -1); /* disable CLKRUN */
3842 chip->amplifier = amp_saved;
3843 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3846 #endif /* CONFIG_PM */
3852 int __devinit snd_cs46xx_create(snd_card_t * card,
3853 struct pci_dev * pci,
3854 int external_amp, int thinkpad,
3859 snd_cs46xx_region_t *region;
3860 struct cs_card_type *cp;
3861 u16 ss_card, ss_vendor;
3862 static snd_device_ops_t ops = {
3863 .dev_free = snd_cs46xx_dev_free,
3868 /* enable PCI device */
3869 if ((err = pci_enable_device(pci)) < 0)
3872 chip = snd_magic_kcalloc(cs46xx_t, 0, GFP_KERNEL);
3875 spin_lock_init(&chip->reg_lock);
3876 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3877 init_MUTEX(&chip->spos_mutex);
3882 chip->ba0_addr = pci_resource_start(pci, 0);
3883 chip->ba1_addr = pci_resource_start(pci, 1);
3884 if (chip->ba0_addr == 0 || chip->ba0_addr == (unsigned long)~0 ||
3885 chip->ba1_addr == 0 || chip->ba1_addr == (unsigned long)~0) {
3886 snd_printk("wrong address(es) - ba0 = 0x%lx, ba1 = 0x%lx\n", chip->ba0_addr, chip->ba1_addr);
3887 snd_cs46xx_free(chip);
3891 region = &chip->region.name.ba0;
3892 strcpy(region->name, "CS46xx_BA0");
3893 region->base = chip->ba0_addr;
3894 region->size = CS46XX_BA0_SIZE;
3896 region = &chip->region.name.data0;
3897 strcpy(region->name, "CS46xx_BA1_data0");
3898 region->base = chip->ba1_addr + BA1_SP_DMEM0;
3899 region->size = CS46XX_BA1_DATA0_SIZE;
3901 region = &chip->region.name.data1;
3902 strcpy(region->name, "CS46xx_BA1_data1");
3903 region->base = chip->ba1_addr + BA1_SP_DMEM1;
3904 region->size = CS46XX_BA1_DATA1_SIZE;
3906 region = &chip->region.name.pmem;
3907 strcpy(region->name, "CS46xx_BA1_pmem");
3908 region->base = chip->ba1_addr + BA1_SP_PMEM;
3909 region->size = CS46XX_BA1_PRG_SIZE;
3911 region = &chip->region.name.reg;
3912 strcpy(region->name, "CS46xx_BA1_reg");
3913 region->base = chip->ba1_addr + BA1_SP_REG;
3914 region->size = CS46XX_BA1_REG_SIZE;
3916 memset(&chip->dma_dev, 0, sizeof(chip->dma_dev));
3917 chip->dma_dev.type = SNDRV_DMA_TYPE_DEV;
3918 chip->dma_dev.dev = snd_dma_pci_data(pci);
3920 /* set up amp and clkrun hack */
3921 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
3922 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &ss_card);
3924 for (cp = &cards[0]; cp->name; cp++) {
3925 if (cp->vendor == ss_vendor && cp->id == ss_card) {
3926 snd_printdd ("hack for %s enabled\n", cp->name);
3928 chip->amplifier_ctrl = cp->amp;
3929 chip->active_ctrl = cp->active;
3930 chip->mixer_init = cp->mixer_init;
3939 snd_printk("Crystal EAPD support forced on.\n");
3940 chip->amplifier_ctrl = amp_voyetra;
3944 snd_printk("Activating CLKRUN hack for Thinkpad.\n");
3945 chip->active_ctrl = clkrun_hack;
3949 if (chip->amplifier_ctrl == NULL)
3950 chip->amplifier_ctrl = amp_none;
3951 if (chip->active_ctrl == NULL)
3952 chip->active_ctrl = amp_none;
3954 chip->active_ctrl(chip, 1); /* enable CLKRUN */
3956 pci_set_master(pci);
3958 for (idx = 0; idx < 5; idx++) {
3959 region = &chip->region.idx[idx];
3960 if ((region->resource = request_mem_region(region->base, region->size, region->name)) == NULL) {
3961 snd_printk("unable to request memory region 0x%lx-0x%lx\n", region->base, region->base + region->size - 1);
3962 snd_cs46xx_free(chip);
3965 region->remap_addr = (unsigned long) ioremap_nocache(region->base, region->size);
3966 if (region->remap_addr == 0) {
3967 snd_printk("%s ioremap problem\n", region->name);
3968 snd_cs46xx_free(chip);
3973 if (request_irq(pci->irq, snd_cs46xx_interrupt, SA_INTERRUPT|SA_SHIRQ, "CS46XX", (void *) chip)) {
3974 snd_printk("unable to grab IRQ %d\n", pci->irq);
3975 snd_cs46xx_free(chip);
3978 chip->irq = pci->irq;
3980 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3981 chip->dsp_spos_instance = cs46xx_dsp_spos_create(chip);
3982 if (chip->dsp_spos_instance == NULL) {
3983 snd_cs46xx_free(chip);
3988 err = snd_cs46xx_chip_init(chip);
3990 snd_cs46xx_free(chip);
3994 snd_cs46xx_proc_init(card, chip);
3996 snd_card_set_pm_callback(card, snd_cs46xx_suspend, snd_cs46xx_resume, chip);
3998 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3999 snd_cs46xx_free(chip);
4003 chip->active_ctrl(chip, -1); /* disable CLKRUN */
4005 snd_card_set_dev(card, &pci->dev);