2 * Copyright (c) by Jaroslav Kysela <perex@perex.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>
56 #include <linux/mutex.h>
59 #include <sound/core.h>
60 #include <sound/control.h>
61 #include <sound/info.h>
62 #include <sound/pcm.h>
63 #include <sound/pcm_params.h>
64 #include <sound/cs46xx.h>
68 #include "cs46xx_lib.h"
71 static void amp_voyetra(struct snd_cs46xx *chip, int change);
73 #ifdef CONFIG_SND_CS46XX_NEW_DSP
74 static struct snd_pcm_ops snd_cs46xx_playback_rear_ops;
75 static struct snd_pcm_ops snd_cs46xx_playback_indirect_rear_ops;
76 static struct snd_pcm_ops snd_cs46xx_playback_clfe_ops;
77 static struct snd_pcm_ops snd_cs46xx_playback_indirect_clfe_ops;
78 static struct snd_pcm_ops snd_cs46xx_playback_iec958_ops;
79 static struct snd_pcm_ops snd_cs46xx_playback_indirect_iec958_ops;
82 static struct snd_pcm_ops snd_cs46xx_playback_ops;
83 static struct snd_pcm_ops snd_cs46xx_playback_indirect_ops;
84 static struct snd_pcm_ops snd_cs46xx_capture_ops;
85 static struct snd_pcm_ops snd_cs46xx_capture_indirect_ops;
87 static unsigned short snd_cs46xx_codec_read(struct snd_cs46xx *chip,
92 unsigned short result,tmp;
94 snd_assert ( (codec_index == CS46XX_PRIMARY_CODEC_INDEX) ||
95 (codec_index == CS46XX_SECONDARY_CODEC_INDEX),
98 chip->active_ctrl(chip, 1);
100 if (codec_index == CS46XX_SECONDARY_CODEC_INDEX)
101 offset = CS46XX_SECONDARY_CODEC_OFFSET;
104 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
105 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
106 * 3. Write ACCTL = Control Register = 460h for initiating the write7---55
107 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
108 * 5. if DCV not cleared, break and return error
109 * 6. Read ACSTS = Status Register = 464h, check VSTS bit
112 snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
114 tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL);
115 if ((tmp & ACCTL_VFRM) == 0) {
116 snd_printk(KERN_WARNING "cs46xx: ACCTL_VFRM not set 0x%x\n",tmp);
117 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, (tmp & (~ACCTL_ESYN)) | ACCTL_VFRM );
119 tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL + offset);
120 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, tmp | ACCTL_ESYN | ACCTL_VFRM );
125 * Setup the AC97 control registers on the CS461x to send the
126 * appropriate command to the AC97 to perform the read.
127 * ACCAD = Command Address Register = 46Ch
128 * ACCDA = Command Data Register = 470h
129 * ACCTL = Control Register = 460h
130 * set DCV - will clear when process completed
131 * set CRW - Read command
132 * set VFRM - valid frame enabled
133 * set ESYN - ASYNC generation enabled
134 * set RSTN - ARST# inactive, AC97 codec not reset
137 snd_cs46xx_pokeBA0(chip, BA0_ACCAD, reg);
138 snd_cs46xx_pokeBA0(chip, BA0_ACCDA, 0);
139 if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
140 snd_cs46xx_pokeBA0(chip, BA0_ACCTL,/* clear ACCTL_DCV */ ACCTL_CRW |
141 ACCTL_VFRM | ACCTL_ESYN |
143 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
144 ACCTL_VFRM | ACCTL_ESYN |
147 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
148 ACCTL_CRW | ACCTL_VFRM | ACCTL_ESYN |
153 * Wait for the read to occur.
155 for (count = 0; count < 1000; count++) {
157 * First, we want to wait for a short time.
161 * Now, check to see if the read has completed.
162 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
164 if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV))
168 snd_printk(KERN_ERR "AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg);
174 * Wait for the valid status bit to go active.
176 for (count = 0; count < 100; count++) {
178 * Read the AC97 status register.
179 * ACSTS = Status Register = 464h
180 * VSTS - Valid Status
182 if (snd_cs46xx_peekBA0(chip, BA0_ACSTS + offset) & ACSTS_VSTS)
187 snd_printk(KERN_ERR "AC'97 read problem (ACSTS_VSTS), codec_index %d, reg = 0x%x\n", codec_index, reg);
193 * Read the data returned from the AC97 register.
194 * ACSDA = Status Data Register = 474h
197 printk("e) reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", reg,
198 snd_cs46xx_peekBA0(chip, BA0_ACSDA),
199 snd_cs46xx_peekBA0(chip, BA0_ACCAD));
202 //snd_cs46xx_peekBA0(chip, BA0_ACCAD);
203 result = snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
205 chip->active_ctrl(chip, -1);
209 static unsigned short snd_cs46xx_ac97_read(struct snd_ac97 * ac97,
212 struct snd_cs46xx *chip = ac97->private_data;
214 int codec_index = ac97->num;
216 snd_assert(codec_index == CS46XX_PRIMARY_CODEC_INDEX ||
217 codec_index == CS46XX_SECONDARY_CODEC_INDEX,
220 val = snd_cs46xx_codec_read(chip, reg, codec_index);
226 static void snd_cs46xx_codec_write(struct snd_cs46xx *chip,
233 snd_assert ((codec_index == CS46XX_PRIMARY_CODEC_INDEX) ||
234 (codec_index == CS46XX_SECONDARY_CODEC_INDEX),
237 chip->active_ctrl(chip, 1);
240 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
241 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
242 * 3. Write ACCTL = Control Register = 460h for initiating the write
243 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
244 * 5. if DCV not cleared, break and return error
248 * Setup the AC97 control registers on the CS461x to send the
249 * appropriate command to the AC97 to perform the read.
250 * ACCAD = Command Address Register = 46Ch
251 * ACCDA = Command Data Register = 470h
252 * ACCTL = Control Register = 460h
253 * set DCV - will clear when process completed
254 * reset CRW - Write command
255 * set VFRM - valid frame enabled
256 * set ESYN - ASYNC generation enabled
257 * set RSTN - ARST# inactive, AC97 codec not reset
259 snd_cs46xx_pokeBA0(chip, BA0_ACCAD , reg);
260 snd_cs46xx_pokeBA0(chip, BA0_ACCDA , val);
261 snd_cs46xx_peekBA0(chip, BA0_ACCTL);
263 if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
264 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, /* clear ACCTL_DCV */ ACCTL_VFRM |
265 ACCTL_ESYN | ACCTL_RSTN);
266 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
267 ACCTL_ESYN | ACCTL_RSTN);
269 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
270 ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
273 for (count = 0; count < 4000; count++) {
275 * First, we want to wait for a short time.
279 * Now, check to see if the write has completed.
280 * ACCTL = 460h, DCV should be reset by now and 460h = 07h
282 if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV)) {
286 snd_printk(KERN_ERR "AC'97 write problem, codec_index = %d, reg = 0x%x, val = 0x%x\n", codec_index, reg, val);
288 chip->active_ctrl(chip, -1);
291 static void snd_cs46xx_ac97_write(struct snd_ac97 *ac97,
295 struct snd_cs46xx *chip = ac97->private_data;
296 int codec_index = ac97->num;
298 snd_assert(codec_index == CS46XX_PRIMARY_CODEC_INDEX ||
299 codec_index == CS46XX_SECONDARY_CODEC_INDEX,
302 snd_cs46xx_codec_write(chip, reg, val, codec_index);
307 * Chip initialization
310 int snd_cs46xx_download(struct snd_cs46xx *chip,
312 unsigned long offset,
316 unsigned int bank = offset >> 16;
317 offset = offset & 0xffff;
319 snd_assert(!(offset & 3) && !(len & 3), return -EINVAL);
320 dst = chip->region.idx[bank+1].remap_addr + offset;
323 /* writel already converts 32-bit value to right endianess */
331 #ifdef CONFIG_SND_CS46XX_NEW_DSP
333 #include "imgs/cwc4630.h"
334 #include "imgs/cwcasync.h"
335 #include "imgs/cwcsnoop.h"
336 #include "imgs/cwcbinhack.h"
337 #include "imgs/cwcdma.h"
339 int snd_cs46xx_clear_BA1(struct snd_cs46xx *chip,
340 unsigned long offset,
344 unsigned int bank = offset >> 16;
345 offset = offset & 0xffff;
347 snd_assert(!(offset & 3) && !(len & 3), return -EINVAL);
348 dst = chip->region.idx[bank+1].remap_addr + offset;
351 /* writel already converts 32-bit value to right endianess */
359 #else /* old DSP image */
361 #include "cs46xx_image.h"
363 int snd_cs46xx_download_image(struct snd_cs46xx *chip)
366 unsigned long offset = 0;
368 for (idx = 0; idx < BA1_MEMORY_COUNT; idx++) {
369 if ((err = snd_cs46xx_download(chip,
370 &BA1Struct.map[offset],
371 BA1Struct.memory[idx].offset,
372 BA1Struct.memory[idx].size)) < 0)
374 offset += BA1Struct.memory[idx].size >> 2;
378 #endif /* CONFIG_SND_CS46XX_NEW_DSP */
384 static void snd_cs46xx_reset(struct snd_cs46xx *chip)
389 * Write the reset bit of the SP control register.
391 snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RSTSP);
394 * Write the control register.
396 snd_cs46xx_poke(chip, BA1_SPCR, SPCR_DRQEN);
399 * Clear the trap registers.
401 for (idx = 0; idx < 8; idx++) {
402 snd_cs46xx_poke(chip, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
403 snd_cs46xx_poke(chip, BA1_TWPR, 0xFFFF);
405 snd_cs46xx_poke(chip, BA1_DREG, 0);
408 * Set the frame timer to reflect the number of cycles per frame.
410 snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
413 static int cs46xx_wait_for_fifo(struct snd_cs46xx * chip,int retry_timeout)
417 * Make sure the previous FIFO write operation has completed.
419 for(i = 0; i < 50; i++){
420 status = snd_cs46xx_peekBA0(chip, BA0_SERBST);
422 if( !(status & SERBST_WBSY) )
425 mdelay(retry_timeout);
428 if(status & SERBST_WBSY) {
429 snd_printk( KERN_ERR "cs46xx: failure waiting for FIFO command to complete\n");
437 static void snd_cs46xx_clear_serial_FIFOs(struct snd_cs46xx *chip)
439 int idx, powerdown = 0;
443 * See if the devices are powered down. If so, we must power them up first
444 * or they will not respond.
446 tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
447 if (!(tmp & CLKCR1_SWCE)) {
448 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
453 * We want to clear out the serial port FIFOs so we don't end up playing
454 * whatever random garbage happens to be in them. We fill the sample FIFOS
455 * with zero (silence).
457 snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0);
460 * Fill all 256 sample FIFO locations.
462 for (idx = 0; idx < 0xFF; idx++) {
464 * Make sure the previous FIFO write operation has completed.
466 if (cs46xx_wait_for_fifo(chip,1)) {
467 snd_printdd ("failed waiting for FIFO at addr (%02X)\n",idx);
470 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
475 * Write the serial port FIFO index.
477 snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
479 * Tell the serial port to load the new value into the FIFO location.
481 snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
484 * Now, if we powered up the devices, then power them back down again.
485 * This is kinda ugly, but should never happen.
488 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
491 static void snd_cs46xx_proc_start(struct snd_cs46xx *chip)
496 * Set the frame timer to reflect the number of cycles per frame.
498 snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
500 * Turn on the run, run at frame, and DMA enable bits in the local copy of
501 * the SP control register.
503 snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
505 * Wait until the run at frame bit resets itself in the SP control
508 for (cnt = 0; cnt < 25; cnt++) {
510 if (!(snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR))
514 if (snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR)
515 snd_printk(KERN_ERR "SPCR_RUNFR never reset\n");
518 static void snd_cs46xx_proc_stop(struct snd_cs46xx *chip)
521 * Turn off the run, run at frame, and DMA enable bits in the local copy of
522 * the SP control register.
524 snd_cs46xx_poke(chip, BA1_SPCR, 0);
528 * Sample rate routines
531 #define GOF_PER_SEC 200
533 static void snd_cs46xx_set_play_sample_rate(struct snd_cs46xx *chip, unsigned int rate)
536 unsigned int tmp1, tmp2;
537 unsigned int phiIncr;
538 unsigned int correctionPerGOF, correctionPerSec;
541 * Compute the values used to drive the actual sample rate conversion.
542 * The following formulas are being computed, using inline assembly
543 * since we need to use 64 bit arithmetic to compute the values:
545 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
546 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
548 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
549 * GOF_PER_SEC * correctionPerGOF
553 * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
554 * correctionPerGOF:correctionPerSec =
555 * dividend:remainder(ulOther / GOF_PER_SEC)
558 phiIncr = tmp1 / 48000;
559 tmp1 -= phiIncr * 48000;
564 tmp1 -= tmp2 * 48000;
565 correctionPerGOF = tmp1 / GOF_PER_SEC;
566 tmp1 -= correctionPerGOF * GOF_PER_SEC;
567 correctionPerSec = tmp1;
570 * Fill in the SampleRateConverter control block.
572 spin_lock_irqsave(&chip->reg_lock, flags);
573 snd_cs46xx_poke(chip, BA1_PSRC,
574 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
575 snd_cs46xx_poke(chip, BA1_PPI, phiIncr);
576 spin_unlock_irqrestore(&chip->reg_lock, flags);
579 static void snd_cs46xx_set_capture_sample_rate(struct snd_cs46xx *chip, unsigned int rate)
582 unsigned int phiIncr, coeffIncr, tmp1, tmp2;
583 unsigned int correctionPerGOF, correctionPerSec, initialDelay;
584 unsigned int frameGroupLength, cnt;
587 * We can only decimate by up to a factor of 1/9th the hardware rate.
588 * Correct the value if an attempt is made to stray outside that limit.
590 if ((rate * 9) < 48000)
594 * We can not capture at at rate greater than the Input Rate (48000).
595 * Return an error if an attempt is made to stray outside that limit.
601 * Compute the values used to drive the actual sample rate conversion.
602 * The following formulas are being computed, using inline assembly
603 * since we need to use 64 bit arithmetic to compute the values:
605 * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
606 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
607 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
609 * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
610 * GOF_PER_SEC * correctionPerGOF
611 * initialDelay = ceil((24 * Fs,in) / Fs,out)
615 * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
616 * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
617 * correctionPerGOF:correctionPerSec =
618 * dividend:remainder(ulOther / GOF_PER_SEC)
619 * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
623 coeffIncr = tmp1 / 48000;
624 tmp1 -= coeffIncr * 48000;
627 coeffIncr += tmp1 / 48000;
628 coeffIncr ^= 0xFFFFFFFF;
631 phiIncr = tmp1 / rate;
632 tmp1 -= phiIncr * rate;
638 correctionPerGOF = tmp1 / GOF_PER_SEC;
639 tmp1 -= correctionPerGOF * GOF_PER_SEC;
640 correctionPerSec = tmp1;
641 initialDelay = ((48000 * 24) + rate - 1) / rate;
644 * Fill in the VariDecimate control block.
646 spin_lock_irqsave(&chip->reg_lock, flags);
647 snd_cs46xx_poke(chip, BA1_CSRC,
648 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
649 snd_cs46xx_poke(chip, BA1_CCI, coeffIncr);
650 snd_cs46xx_poke(chip, BA1_CD,
651 (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
652 snd_cs46xx_poke(chip, BA1_CPI, phiIncr);
653 spin_unlock_irqrestore(&chip->reg_lock, flags);
656 * Figure out the frame group length for the write back task. Basically,
657 * this is just the factors of 24000 (2^6*3*5^3) that are not present in
658 * the output sample rate.
660 frameGroupLength = 1;
661 for (cnt = 2; cnt <= 64; cnt *= 2) {
662 if (((rate / cnt) * cnt) != rate)
663 frameGroupLength *= 2;
665 if (((rate / 3) * 3) != rate) {
666 frameGroupLength *= 3;
668 for (cnt = 5; cnt <= 125; cnt *= 5) {
669 if (((rate / cnt) * cnt) != rate)
670 frameGroupLength *= 5;
674 * Fill in the WriteBack control block.
676 spin_lock_irqsave(&chip->reg_lock, flags);
677 snd_cs46xx_poke(chip, BA1_CFG1, frameGroupLength);
678 snd_cs46xx_poke(chip, BA1_CFG2, (0x00800000 | frameGroupLength));
679 snd_cs46xx_poke(chip, BA1_CCST, 0x0000FFFF);
680 snd_cs46xx_poke(chip, BA1_CSPB, ((65536 * rate) / 24000));
681 snd_cs46xx_poke(chip, (BA1_CSPB + 4), 0x0000FFFF);
682 spin_unlock_irqrestore(&chip->reg_lock, flags);
689 static void snd_cs46xx_pb_trans_copy(struct snd_pcm_substream *substream,
690 struct snd_pcm_indirect *rec, size_t bytes)
692 struct snd_pcm_runtime *runtime = substream->runtime;
693 struct snd_cs46xx_pcm * cpcm = runtime->private_data;
694 memcpy(cpcm->hw_buf.area + rec->hw_data, runtime->dma_area + rec->sw_data, bytes);
697 static int snd_cs46xx_playback_transfer(struct snd_pcm_substream *substream)
699 struct snd_pcm_runtime *runtime = substream->runtime;
700 struct snd_cs46xx_pcm * cpcm = runtime->private_data;
701 snd_pcm_indirect_playback_transfer(substream, &cpcm->pcm_rec, snd_cs46xx_pb_trans_copy);
705 static void snd_cs46xx_cp_trans_copy(struct snd_pcm_substream *substream,
706 struct snd_pcm_indirect *rec, size_t bytes)
708 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
709 struct snd_pcm_runtime *runtime = substream->runtime;
710 memcpy(runtime->dma_area + rec->sw_data,
711 chip->capt.hw_buf.area + rec->hw_data, bytes);
714 static int snd_cs46xx_capture_transfer(struct snd_pcm_substream *substream)
716 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
717 snd_pcm_indirect_capture_transfer(substream, &chip->capt.pcm_rec, snd_cs46xx_cp_trans_copy);
721 static snd_pcm_uframes_t snd_cs46xx_playback_direct_pointer(struct snd_pcm_substream *substream)
723 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
725 struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
726 snd_assert (cpcm->pcm_channel,return -ENXIO);
728 #ifdef CONFIG_SND_CS46XX_NEW_DSP
729 ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
731 ptr = snd_cs46xx_peek(chip, BA1_PBA);
733 ptr -= cpcm->hw_buf.addr;
734 return ptr >> cpcm->shift;
737 static snd_pcm_uframes_t snd_cs46xx_playback_indirect_pointer(struct snd_pcm_substream *substream)
739 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
741 struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
743 #ifdef CONFIG_SND_CS46XX_NEW_DSP
744 snd_assert (cpcm->pcm_channel,return -ENXIO);
745 ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
747 ptr = snd_cs46xx_peek(chip, BA1_PBA);
749 ptr -= cpcm->hw_buf.addr;
750 return snd_pcm_indirect_playback_pointer(substream, &cpcm->pcm_rec, ptr);
753 static snd_pcm_uframes_t snd_cs46xx_capture_direct_pointer(struct snd_pcm_substream *substream)
755 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
756 size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
757 return ptr >> chip->capt.shift;
760 static snd_pcm_uframes_t snd_cs46xx_capture_indirect_pointer(struct snd_pcm_substream *substream)
762 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
763 size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
764 return snd_pcm_indirect_capture_pointer(substream, &chip->capt.pcm_rec, ptr);
767 static int snd_cs46xx_playback_trigger(struct snd_pcm_substream *substream,
770 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
771 /*struct snd_pcm_runtime *runtime = substream->runtime;*/
774 #ifdef CONFIG_SND_CS46XX_NEW_DSP
775 struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
776 if (! cpcm->pcm_channel) {
781 case SNDRV_PCM_TRIGGER_START:
782 case SNDRV_PCM_TRIGGER_RESUME:
783 #ifdef CONFIG_SND_CS46XX_NEW_DSP
784 /* magic value to unmute PCM stream playback volume */
785 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
786 SCBVolumeCtrl) << 2, 0x80008000);
788 if (cpcm->pcm_channel->unlinked)
789 cs46xx_dsp_pcm_link(chip,cpcm->pcm_channel);
791 if (substream->runtime->periods != CS46XX_FRAGS)
792 snd_cs46xx_playback_transfer(substream);
794 spin_lock(&chip->reg_lock);
795 if (substream->runtime->periods != CS46XX_FRAGS)
796 snd_cs46xx_playback_transfer(substream);
798 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
800 snd_cs46xx_poke(chip, BA1_PCTL, chip->play_ctl | tmp);
802 spin_unlock(&chip->reg_lock);
805 case SNDRV_PCM_TRIGGER_STOP:
806 case SNDRV_PCM_TRIGGER_SUSPEND:
807 #ifdef CONFIG_SND_CS46XX_NEW_DSP
808 /* magic mute channel */
809 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
810 SCBVolumeCtrl) << 2, 0xffffffff);
812 if (!cpcm->pcm_channel->unlinked)
813 cs46xx_dsp_pcm_unlink(chip,cpcm->pcm_channel);
815 spin_lock(&chip->reg_lock);
817 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
819 snd_cs46xx_poke(chip, BA1_PCTL, tmp);
821 spin_unlock(&chip->reg_lock);
832 static int snd_cs46xx_capture_trigger(struct snd_pcm_substream *substream,
835 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
839 spin_lock(&chip->reg_lock);
841 case SNDRV_PCM_TRIGGER_START:
842 case SNDRV_PCM_TRIGGER_RESUME:
843 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
845 snd_cs46xx_poke(chip, BA1_CCTL, chip->capt.ctl | tmp);
847 case SNDRV_PCM_TRIGGER_STOP:
848 case SNDRV_PCM_TRIGGER_SUSPEND:
849 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
851 snd_cs46xx_poke(chip, BA1_CCTL, tmp);
857 spin_unlock(&chip->reg_lock);
862 #ifdef CONFIG_SND_CS46XX_NEW_DSP
863 static int _cs46xx_adjust_sample_rate (struct snd_cs46xx *chip, struct snd_cs46xx_pcm *cpcm,
867 /* If PCMReaderSCB and SrcTaskSCB not created yet ... */
868 if ( cpcm->pcm_channel == NULL) {
869 cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate,
870 cpcm, cpcm->hw_buf.addr,cpcm->pcm_channel_id);
871 if (cpcm->pcm_channel == NULL) {
872 snd_printk(KERN_ERR "cs46xx: failed to create virtual PCM channel\n");
875 cpcm->pcm_channel->sample_rate = sample_rate;
877 /* if sample rate is changed */
878 if ((int)cpcm->pcm_channel->sample_rate != sample_rate) {
879 int unlinked = cpcm->pcm_channel->unlinked;
880 cs46xx_dsp_destroy_pcm_channel (chip,cpcm->pcm_channel);
882 if ( (cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate, cpcm,
884 cpcm->pcm_channel_id)) == NULL) {
885 snd_printk(KERN_ERR "cs46xx: failed to re-create virtual PCM channel\n");
889 if (!unlinked) cs46xx_dsp_pcm_link (chip,cpcm->pcm_channel);
890 cpcm->pcm_channel->sample_rate = sample_rate;
898 static int snd_cs46xx_playback_hw_params(struct snd_pcm_substream *substream,
899 struct snd_pcm_hw_params *hw_params)
901 struct snd_pcm_runtime *runtime = substream->runtime;
902 struct snd_cs46xx_pcm *cpcm;
904 #ifdef CONFIG_SND_CS46XX_NEW_DSP
905 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
906 int sample_rate = params_rate(hw_params);
907 int period_size = params_period_bytes(hw_params);
909 cpcm = runtime->private_data;
911 #ifdef CONFIG_SND_CS46XX_NEW_DSP
912 snd_assert (sample_rate != 0, return -ENXIO);
914 mutex_lock(&chip->spos_mutex);
916 if (_cs46xx_adjust_sample_rate (chip,cpcm,sample_rate)) {
917 mutex_unlock(&chip->spos_mutex);
921 snd_assert (cpcm->pcm_channel != NULL);
922 if (!cpcm->pcm_channel) {
923 mutex_unlock(&chip->spos_mutex);
928 if (cs46xx_dsp_pcm_channel_set_period (chip,cpcm->pcm_channel,period_size)) {
929 mutex_unlock(&chip->spos_mutex);
933 snd_printdd ("period_size (%d), periods (%d) buffer_size(%d)\n",
934 period_size, params_periods(hw_params),
935 params_buffer_bytes(hw_params));
938 if (params_periods(hw_params) == CS46XX_FRAGS) {
939 if (runtime->dma_area != cpcm->hw_buf.area)
940 snd_pcm_lib_free_pages(substream);
941 runtime->dma_area = cpcm->hw_buf.area;
942 runtime->dma_addr = cpcm->hw_buf.addr;
943 runtime->dma_bytes = cpcm->hw_buf.bytes;
946 #ifdef CONFIG_SND_CS46XX_NEW_DSP
947 if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
948 substream->ops = &snd_cs46xx_playback_ops;
949 } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
950 substream->ops = &snd_cs46xx_playback_rear_ops;
951 } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
952 substream->ops = &snd_cs46xx_playback_clfe_ops;
953 } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
954 substream->ops = &snd_cs46xx_playback_iec958_ops;
959 substream->ops = &snd_cs46xx_playback_ops;
963 if (runtime->dma_area == cpcm->hw_buf.area) {
964 runtime->dma_area = NULL;
965 runtime->dma_addr = 0;
966 runtime->dma_bytes = 0;
968 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) {
969 #ifdef CONFIG_SND_CS46XX_NEW_DSP
970 mutex_unlock(&chip->spos_mutex);
975 #ifdef CONFIG_SND_CS46XX_NEW_DSP
976 if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
977 substream->ops = &snd_cs46xx_playback_indirect_ops;
978 } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
979 substream->ops = &snd_cs46xx_playback_indirect_rear_ops;
980 } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
981 substream->ops = &snd_cs46xx_playback_indirect_clfe_ops;
982 } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
983 substream->ops = &snd_cs46xx_playback_indirect_iec958_ops;
988 substream->ops = &snd_cs46xx_playback_indirect_ops;
993 #ifdef CONFIG_SND_CS46XX_NEW_DSP
994 mutex_unlock(&chip->spos_mutex);
1000 static int snd_cs46xx_playback_hw_free(struct snd_pcm_substream *substream)
1002 /*struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);*/
1003 struct snd_pcm_runtime *runtime = substream->runtime;
1004 struct snd_cs46xx_pcm *cpcm;
1006 cpcm = runtime->private_data;
1008 /* if play_back open fails, then this function
1009 is called and cpcm can actually be NULL here */
1010 if (!cpcm) return -ENXIO;
1012 if (runtime->dma_area != cpcm->hw_buf.area)
1013 snd_pcm_lib_free_pages(substream);
1015 runtime->dma_area = NULL;
1016 runtime->dma_addr = 0;
1017 runtime->dma_bytes = 0;
1022 static int snd_cs46xx_playback_prepare(struct snd_pcm_substream *substream)
1026 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1027 struct snd_pcm_runtime *runtime = substream->runtime;
1028 struct snd_cs46xx_pcm *cpcm;
1030 cpcm = runtime->private_data;
1032 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1033 snd_assert (cpcm->pcm_channel != NULL, return -ENXIO);
1035 pfie = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2 );
1036 pfie &= ~0x0000f03f;
1039 pfie = snd_cs46xx_peek(chip, BA1_PFIE);
1040 pfie &= ~0x0000f03f;
1044 /* if to convert from stereo to mono */
1045 if (runtime->channels == 1) {
1049 /* if to convert from 8 bit to 16 bit */
1050 if (snd_pcm_format_width(runtime->format) == 8) {
1054 /* if to convert to unsigned */
1055 if (snd_pcm_format_unsigned(runtime->format))
1058 /* Never convert byte order when sample stream is 8 bit */
1059 if (snd_pcm_format_width(runtime->format) != 8) {
1060 /* convert from big endian to little endian */
1061 if (snd_pcm_format_big_endian(runtime->format))
1065 memset(&cpcm->pcm_rec, 0, sizeof(cpcm->pcm_rec));
1066 cpcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1067 cpcm->pcm_rec.hw_buffer_size = runtime->period_size * CS46XX_FRAGS << cpcm->shift;
1069 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1071 tmp = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2);
1073 tmp |= (4 << cpcm->shift) - 1;
1074 /* playback transaction count register */
1075 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2, tmp);
1077 /* playback format && interrupt enable */
1078 snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2, pfie | cpcm->pcm_channel->pcm_slot);
1080 snd_cs46xx_poke(chip, BA1_PBA, cpcm->hw_buf.addr);
1081 tmp = snd_cs46xx_peek(chip, BA1_PDTC);
1083 tmp |= (4 << cpcm->shift) - 1;
1084 snd_cs46xx_poke(chip, BA1_PDTC, tmp);
1085 snd_cs46xx_poke(chip, BA1_PFIE, pfie);
1086 snd_cs46xx_set_play_sample_rate(chip, runtime->rate);
1092 static int snd_cs46xx_capture_hw_params(struct snd_pcm_substream *substream,
1093 struct snd_pcm_hw_params *hw_params)
1095 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1096 struct snd_pcm_runtime *runtime = substream->runtime;
1099 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1100 cs46xx_dsp_pcm_ostream_set_period (chip, params_period_bytes(hw_params));
1102 if (runtime->periods == CS46XX_FRAGS) {
1103 if (runtime->dma_area != chip->capt.hw_buf.area)
1104 snd_pcm_lib_free_pages(substream);
1105 runtime->dma_area = chip->capt.hw_buf.area;
1106 runtime->dma_addr = chip->capt.hw_buf.addr;
1107 runtime->dma_bytes = chip->capt.hw_buf.bytes;
1108 substream->ops = &snd_cs46xx_capture_ops;
1110 if (runtime->dma_area == chip->capt.hw_buf.area) {
1111 runtime->dma_area = NULL;
1112 runtime->dma_addr = 0;
1113 runtime->dma_bytes = 0;
1115 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1117 substream->ops = &snd_cs46xx_capture_indirect_ops;
1123 static int snd_cs46xx_capture_hw_free(struct snd_pcm_substream *substream)
1125 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1126 struct snd_pcm_runtime *runtime = substream->runtime;
1128 if (runtime->dma_area != chip->capt.hw_buf.area)
1129 snd_pcm_lib_free_pages(substream);
1130 runtime->dma_area = NULL;
1131 runtime->dma_addr = 0;
1132 runtime->dma_bytes = 0;
1137 static int snd_cs46xx_capture_prepare(struct snd_pcm_substream *substream)
1139 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1140 struct snd_pcm_runtime *runtime = substream->runtime;
1142 snd_cs46xx_poke(chip, BA1_CBA, chip->capt.hw_buf.addr);
1143 chip->capt.shift = 2;
1144 memset(&chip->capt.pcm_rec, 0, sizeof(chip->capt.pcm_rec));
1145 chip->capt.pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1146 chip->capt.pcm_rec.hw_buffer_size = runtime->period_size * CS46XX_FRAGS << 2;
1147 snd_cs46xx_set_capture_sample_rate(chip, runtime->rate);
1152 static irqreturn_t snd_cs46xx_interrupt(int irq, void *dev_id)
1154 struct snd_cs46xx *chip = dev_id;
1156 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1157 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1160 struct snd_cs46xx_pcm *cpcm = NULL;
1164 * Read the Interrupt Status Register to clear the interrupt
1166 status1 = snd_cs46xx_peekBA0(chip, BA0_HISR);
1167 if ((status1 & 0x7fffffff) == 0) {
1168 snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
1172 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1173 status2 = snd_cs46xx_peekBA0(chip, BA0_HSR0);
1175 for (i = 0; i < DSP_MAX_PCM_CHANNELS; ++i) {
1177 if ( status1 & (1 << i) ) {
1178 if (i == CS46XX_DSP_CAPTURE_CHANNEL) {
1179 if (chip->capt.substream)
1180 snd_pcm_period_elapsed(chip->capt.substream);
1182 if (ins->pcm_channels[i].active &&
1183 ins->pcm_channels[i].private_data &&
1184 !ins->pcm_channels[i].unlinked) {
1185 cpcm = ins->pcm_channels[i].private_data;
1186 snd_pcm_period_elapsed(cpcm->substream);
1191 if ( status2 & (1 << (i - 16))) {
1192 if (ins->pcm_channels[i].active &&
1193 ins->pcm_channels[i].private_data &&
1194 !ins->pcm_channels[i].unlinked) {
1195 cpcm = ins->pcm_channels[i].private_data;
1196 snd_pcm_period_elapsed(cpcm->substream);
1204 if ((status1 & HISR_VC0) && chip->playback_pcm) {
1205 if (chip->playback_pcm->substream)
1206 snd_pcm_period_elapsed(chip->playback_pcm->substream);
1208 if ((status1 & HISR_VC1) && chip->pcm) {
1209 if (chip->capt.substream)
1210 snd_pcm_period_elapsed(chip->capt.substream);
1214 if ((status1 & HISR_MIDI) && chip->rmidi) {
1217 spin_lock(&chip->reg_lock);
1218 while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_RBE) == 0) {
1219 c = snd_cs46xx_peekBA0(chip, BA0_MIDRP);
1220 if ((chip->midcr & MIDCR_RIE) == 0)
1222 snd_rawmidi_receive(chip->midi_input, &c, 1);
1224 while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
1225 if ((chip->midcr & MIDCR_TIE) == 0)
1227 if (snd_rawmidi_transmit(chip->midi_output, &c, 1) != 1) {
1228 chip->midcr &= ~MIDCR_TIE;
1229 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
1232 snd_cs46xx_pokeBA0(chip, BA0_MIDWP, c);
1234 spin_unlock(&chip->reg_lock);
1237 * EOI to the PCI part....reenables interrupts
1239 snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
1244 static struct snd_pcm_hardware snd_cs46xx_playback =
1246 .info = (SNDRV_PCM_INFO_MMAP |
1247 SNDRV_PCM_INFO_INTERLEAVED |
1248 SNDRV_PCM_INFO_BLOCK_TRANSFER /*|*/
1249 /*SNDRV_PCM_INFO_RESUME*/),
1250 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
1251 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
1252 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE),
1253 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1258 .buffer_bytes_max = (256 * 1024),
1259 .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
1260 .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
1261 .periods_min = CS46XX_FRAGS,
1262 .periods_max = 1024,
1266 static struct snd_pcm_hardware snd_cs46xx_capture =
1268 .info = (SNDRV_PCM_INFO_MMAP |
1269 SNDRV_PCM_INFO_INTERLEAVED |
1270 SNDRV_PCM_INFO_BLOCK_TRANSFER /*|*/
1271 /*SNDRV_PCM_INFO_RESUME*/),
1272 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1273 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1278 .buffer_bytes_max = (256 * 1024),
1279 .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
1280 .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
1281 .periods_min = CS46XX_FRAGS,
1282 .periods_max = 1024,
1286 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1288 static unsigned int period_sizes[] = { 32, 64, 128, 256, 512, 1024, 2048 };
1290 static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
1291 .count = ARRAY_SIZE(period_sizes),
1292 .list = period_sizes,
1298 static void snd_cs46xx_pcm_free_substream(struct snd_pcm_runtime *runtime)
1300 kfree(runtime->private_data);
1303 static int _cs46xx_playback_open_channel (struct snd_pcm_substream *substream,int pcm_channel_id)
1305 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1306 struct snd_cs46xx_pcm * cpcm;
1307 struct snd_pcm_runtime *runtime = substream->runtime;
1309 cpcm = kzalloc(sizeof(*cpcm), GFP_KERNEL);
1312 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1313 PAGE_SIZE, &cpcm->hw_buf) < 0) {
1318 runtime->hw = snd_cs46xx_playback;
1319 runtime->private_data = cpcm;
1320 runtime->private_free = snd_cs46xx_pcm_free_substream;
1322 cpcm->substream = substream;
1323 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1324 mutex_lock(&chip->spos_mutex);
1325 cpcm->pcm_channel = NULL;
1326 cpcm->pcm_channel_id = pcm_channel_id;
1329 snd_pcm_hw_constraint_list(runtime, 0,
1330 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1331 &hw_constraints_period_sizes);
1333 mutex_unlock(&chip->spos_mutex);
1335 chip->playback_pcm = cpcm; /* HACK */
1338 if (chip->accept_valid)
1339 substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
1340 chip->active_ctrl(chip, 1);
1345 static int snd_cs46xx_playback_open(struct snd_pcm_substream *substream)
1347 snd_printdd("open front channel\n");
1348 return _cs46xx_playback_open_channel(substream,DSP_PCM_MAIN_CHANNEL);
1351 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1352 static int snd_cs46xx_playback_open_rear(struct snd_pcm_substream *substream)
1354 snd_printdd("open rear channel\n");
1356 return _cs46xx_playback_open_channel(substream,DSP_PCM_REAR_CHANNEL);
1359 static int snd_cs46xx_playback_open_clfe(struct snd_pcm_substream *substream)
1361 snd_printdd("open center - LFE channel\n");
1363 return _cs46xx_playback_open_channel(substream,DSP_PCM_CENTER_LFE_CHANNEL);
1366 static int snd_cs46xx_playback_open_iec958(struct snd_pcm_substream *substream)
1368 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1370 snd_printdd("open raw iec958 channel\n");
1372 mutex_lock(&chip->spos_mutex);
1373 cs46xx_iec958_pre_open (chip);
1374 mutex_unlock(&chip->spos_mutex);
1376 return _cs46xx_playback_open_channel(substream,DSP_IEC958_CHANNEL);
1379 static int snd_cs46xx_playback_close(struct snd_pcm_substream *substream);
1381 static int snd_cs46xx_playback_close_iec958(struct snd_pcm_substream *substream)
1384 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1386 snd_printdd("close raw iec958 channel\n");
1388 err = snd_cs46xx_playback_close(substream);
1390 mutex_lock(&chip->spos_mutex);
1391 cs46xx_iec958_post_close (chip);
1392 mutex_unlock(&chip->spos_mutex);
1398 static int snd_cs46xx_capture_open(struct snd_pcm_substream *substream)
1400 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1402 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1403 PAGE_SIZE, &chip->capt.hw_buf) < 0)
1405 chip->capt.substream = substream;
1406 substream->runtime->hw = snd_cs46xx_capture;
1408 if (chip->accept_valid)
1409 substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
1411 chip->active_ctrl(chip, 1);
1413 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1414 snd_pcm_hw_constraint_list(substream->runtime, 0,
1415 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1416 &hw_constraints_period_sizes);
1421 static int snd_cs46xx_playback_close(struct snd_pcm_substream *substream)
1423 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1424 struct snd_pcm_runtime *runtime = substream->runtime;
1425 struct snd_cs46xx_pcm * cpcm;
1427 cpcm = runtime->private_data;
1429 /* when playback_open fails, then cpcm can be NULL */
1430 if (!cpcm) return -ENXIO;
1432 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1433 mutex_lock(&chip->spos_mutex);
1434 if (cpcm->pcm_channel) {
1435 cs46xx_dsp_destroy_pcm_channel(chip,cpcm->pcm_channel);
1436 cpcm->pcm_channel = NULL;
1438 mutex_unlock(&chip->spos_mutex);
1440 chip->playback_pcm = NULL;
1443 cpcm->substream = NULL;
1444 snd_dma_free_pages(&cpcm->hw_buf);
1445 chip->active_ctrl(chip, -1);
1450 static int snd_cs46xx_capture_close(struct snd_pcm_substream *substream)
1452 struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
1454 chip->capt.substream = NULL;
1455 snd_dma_free_pages(&chip->capt.hw_buf);
1456 chip->active_ctrl(chip, -1);
1461 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1462 static struct snd_pcm_ops snd_cs46xx_playback_rear_ops = {
1463 .open = snd_cs46xx_playback_open_rear,
1464 .close = snd_cs46xx_playback_close,
1465 .ioctl = snd_pcm_lib_ioctl,
1466 .hw_params = snd_cs46xx_playback_hw_params,
1467 .hw_free = snd_cs46xx_playback_hw_free,
1468 .prepare = snd_cs46xx_playback_prepare,
1469 .trigger = snd_cs46xx_playback_trigger,
1470 .pointer = snd_cs46xx_playback_direct_pointer,
1473 static struct snd_pcm_ops snd_cs46xx_playback_indirect_rear_ops = {
1474 .open = snd_cs46xx_playback_open_rear,
1475 .close = snd_cs46xx_playback_close,
1476 .ioctl = snd_pcm_lib_ioctl,
1477 .hw_params = snd_cs46xx_playback_hw_params,
1478 .hw_free = snd_cs46xx_playback_hw_free,
1479 .prepare = snd_cs46xx_playback_prepare,
1480 .trigger = snd_cs46xx_playback_trigger,
1481 .pointer = snd_cs46xx_playback_indirect_pointer,
1482 .ack = snd_cs46xx_playback_transfer,
1485 static struct snd_pcm_ops snd_cs46xx_playback_clfe_ops = {
1486 .open = snd_cs46xx_playback_open_clfe,
1487 .close = snd_cs46xx_playback_close,
1488 .ioctl = snd_pcm_lib_ioctl,
1489 .hw_params = snd_cs46xx_playback_hw_params,
1490 .hw_free = snd_cs46xx_playback_hw_free,
1491 .prepare = snd_cs46xx_playback_prepare,
1492 .trigger = snd_cs46xx_playback_trigger,
1493 .pointer = snd_cs46xx_playback_direct_pointer,
1496 static struct snd_pcm_ops snd_cs46xx_playback_indirect_clfe_ops = {
1497 .open = snd_cs46xx_playback_open_clfe,
1498 .close = snd_cs46xx_playback_close,
1499 .ioctl = snd_pcm_lib_ioctl,
1500 .hw_params = snd_cs46xx_playback_hw_params,
1501 .hw_free = snd_cs46xx_playback_hw_free,
1502 .prepare = snd_cs46xx_playback_prepare,
1503 .trigger = snd_cs46xx_playback_trigger,
1504 .pointer = snd_cs46xx_playback_indirect_pointer,
1505 .ack = snd_cs46xx_playback_transfer,
1508 static struct snd_pcm_ops snd_cs46xx_playback_iec958_ops = {
1509 .open = snd_cs46xx_playback_open_iec958,
1510 .close = snd_cs46xx_playback_close_iec958,
1511 .ioctl = snd_pcm_lib_ioctl,
1512 .hw_params = snd_cs46xx_playback_hw_params,
1513 .hw_free = snd_cs46xx_playback_hw_free,
1514 .prepare = snd_cs46xx_playback_prepare,
1515 .trigger = snd_cs46xx_playback_trigger,
1516 .pointer = snd_cs46xx_playback_direct_pointer,
1519 static struct snd_pcm_ops snd_cs46xx_playback_indirect_iec958_ops = {
1520 .open = snd_cs46xx_playback_open_iec958,
1521 .close = snd_cs46xx_playback_close_iec958,
1522 .ioctl = snd_pcm_lib_ioctl,
1523 .hw_params = snd_cs46xx_playback_hw_params,
1524 .hw_free = snd_cs46xx_playback_hw_free,
1525 .prepare = snd_cs46xx_playback_prepare,
1526 .trigger = snd_cs46xx_playback_trigger,
1527 .pointer = snd_cs46xx_playback_indirect_pointer,
1528 .ack = snd_cs46xx_playback_transfer,
1533 static struct snd_pcm_ops snd_cs46xx_playback_ops = {
1534 .open = snd_cs46xx_playback_open,
1535 .close = snd_cs46xx_playback_close,
1536 .ioctl = snd_pcm_lib_ioctl,
1537 .hw_params = snd_cs46xx_playback_hw_params,
1538 .hw_free = snd_cs46xx_playback_hw_free,
1539 .prepare = snd_cs46xx_playback_prepare,
1540 .trigger = snd_cs46xx_playback_trigger,
1541 .pointer = snd_cs46xx_playback_direct_pointer,
1544 static struct snd_pcm_ops snd_cs46xx_playback_indirect_ops = {
1545 .open = snd_cs46xx_playback_open,
1546 .close = snd_cs46xx_playback_close,
1547 .ioctl = snd_pcm_lib_ioctl,
1548 .hw_params = snd_cs46xx_playback_hw_params,
1549 .hw_free = snd_cs46xx_playback_hw_free,
1550 .prepare = snd_cs46xx_playback_prepare,
1551 .trigger = snd_cs46xx_playback_trigger,
1552 .pointer = snd_cs46xx_playback_indirect_pointer,
1553 .ack = snd_cs46xx_playback_transfer,
1556 static struct snd_pcm_ops snd_cs46xx_capture_ops = {
1557 .open = snd_cs46xx_capture_open,
1558 .close = snd_cs46xx_capture_close,
1559 .ioctl = snd_pcm_lib_ioctl,
1560 .hw_params = snd_cs46xx_capture_hw_params,
1561 .hw_free = snd_cs46xx_capture_hw_free,
1562 .prepare = snd_cs46xx_capture_prepare,
1563 .trigger = snd_cs46xx_capture_trigger,
1564 .pointer = snd_cs46xx_capture_direct_pointer,
1567 static struct snd_pcm_ops snd_cs46xx_capture_indirect_ops = {
1568 .open = snd_cs46xx_capture_open,
1569 .close = snd_cs46xx_capture_close,
1570 .ioctl = snd_pcm_lib_ioctl,
1571 .hw_params = snd_cs46xx_capture_hw_params,
1572 .hw_free = snd_cs46xx_capture_hw_free,
1573 .prepare = snd_cs46xx_capture_prepare,
1574 .trigger = snd_cs46xx_capture_trigger,
1575 .pointer = snd_cs46xx_capture_indirect_pointer,
1576 .ack = snd_cs46xx_capture_transfer,
1579 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1580 #define MAX_PLAYBACK_CHANNELS (DSP_MAX_PCM_CHANNELS - 1)
1582 #define MAX_PLAYBACK_CHANNELS 1
1585 int __devinit snd_cs46xx_pcm(struct snd_cs46xx *chip, int device, struct snd_pcm ** rpcm)
1587 struct snd_pcm *pcm;
1592 if ((err = snd_pcm_new(chip->card, "CS46xx", device, MAX_PLAYBACK_CHANNELS, 1, &pcm)) < 0)
1595 pcm->private_data = chip;
1597 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_ops);
1598 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs46xx_capture_ops);
1601 pcm->info_flags = 0;
1602 strcpy(pcm->name, "CS46xx");
1605 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1606 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1615 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1616 int __devinit snd_cs46xx_pcm_rear(struct snd_cs46xx *chip, int device, struct snd_pcm ** rpcm)
1618 struct snd_pcm *pcm;
1624 if ((err = snd_pcm_new(chip->card, "CS46xx - Rear", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
1627 pcm->private_data = chip;
1629 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_rear_ops);
1632 pcm->info_flags = 0;
1633 strcpy(pcm->name, "CS46xx - Rear");
1634 chip->pcm_rear = pcm;
1636 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1637 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1645 int __devinit snd_cs46xx_pcm_center_lfe(struct snd_cs46xx *chip, int device, struct snd_pcm ** rpcm)
1647 struct snd_pcm *pcm;
1653 if ((err = snd_pcm_new(chip->card, "CS46xx - Center LFE", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
1656 pcm->private_data = chip;
1658 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_clfe_ops);
1661 pcm->info_flags = 0;
1662 strcpy(pcm->name, "CS46xx - Center LFE");
1663 chip->pcm_center_lfe = pcm;
1665 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1666 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1674 int __devinit snd_cs46xx_pcm_iec958(struct snd_cs46xx *chip, int device, struct snd_pcm ** rpcm)
1676 struct snd_pcm *pcm;
1682 if ((err = snd_pcm_new(chip->card, "CS46xx - IEC958", device, 1, 0, &pcm)) < 0)
1685 pcm->private_data = chip;
1687 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_iec958_ops);
1690 pcm->info_flags = 0;
1691 strcpy(pcm->name, "CS46xx - IEC958");
1692 chip->pcm_rear = pcm;
1694 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1695 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1707 static void snd_cs46xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1709 struct snd_cs46xx *chip = bus->private_data;
1711 chip->ac97_bus = NULL;
1714 static void snd_cs46xx_mixer_free_ac97(struct snd_ac97 *ac97)
1716 struct snd_cs46xx *chip = ac97->private_data;
1718 snd_assert ((ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) ||
1719 (ac97 == chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]),
1722 if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) {
1723 chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = NULL;
1724 chip->eapd_switch = NULL;
1727 chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = NULL;
1730 static int snd_cs46xx_vol_info(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_info *uinfo)
1733 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1735 uinfo->value.integer.min = 0;
1736 uinfo->value.integer.max = 0x7fff;
1740 static int snd_cs46xx_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1742 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1743 int reg = kcontrol->private_value;
1744 unsigned int val = snd_cs46xx_peek(chip, reg);
1745 ucontrol->value.integer.value[0] = 0xffff - (val >> 16);
1746 ucontrol->value.integer.value[1] = 0xffff - (val & 0xffff);
1750 static int snd_cs46xx_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1752 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1753 int reg = kcontrol->private_value;
1754 unsigned int val = ((0xffff - ucontrol->value.integer.value[0]) << 16 |
1755 (0xffff - ucontrol->value.integer.value[1]));
1756 unsigned int old = snd_cs46xx_peek(chip, reg);
1757 int change = (old != val);
1760 snd_cs46xx_poke(chip, reg, val);
1766 #ifdef CONFIG_SND_CS46XX_NEW_DSP
1768 static int snd_cs46xx_vol_dac_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1770 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1772 ucontrol->value.integer.value[0] = chip->dsp_spos_instance->dac_volume_left;
1773 ucontrol->value.integer.value[1] = chip->dsp_spos_instance->dac_volume_right;
1778 static int snd_cs46xx_vol_dac_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1780 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1783 if (chip->dsp_spos_instance->dac_volume_right != ucontrol->value.integer.value[0] ||
1784 chip->dsp_spos_instance->dac_volume_left != ucontrol->value.integer.value[1]) {
1785 cs46xx_dsp_set_dac_volume(chip,
1786 ucontrol->value.integer.value[0],
1787 ucontrol->value.integer.value[1]);
1795 static int snd_cs46xx_vol_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1797 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1799 ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_input_volume_left;
1800 ucontrol->value.integer.value[1] = chip->dsp_spos_instance->spdif_input_volume_right;
1804 static int snd_cs46xx_vol_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1806 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1809 if (chip->dsp_spos_instance->spdif_input_volume_left != ucontrol->value.integer.value[0] ||
1810 chip->dsp_spos_instance->spdif_input_volume_right!= ucontrol->value.integer.value[1]) {
1811 cs46xx_dsp_set_iec958_volume (chip,
1812 ucontrol->value.integer.value[0],
1813 ucontrol->value.integer.value[1]);
1821 #define snd_mixer_boolean_info snd_ctl_boolean_mono_info
1823 static int snd_cs46xx_iec958_get(struct snd_kcontrol *kcontrol,
1824 struct snd_ctl_elem_value *ucontrol)
1826 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1827 int reg = kcontrol->private_value;
1829 if (reg == CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT)
1830 ucontrol->value.integer.value[0] = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
1832 ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_status_in;
1837 static int snd_cs46xx_iec958_put(struct snd_kcontrol *kcontrol,
1838 struct snd_ctl_elem_value *ucontrol)
1840 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1843 switch (kcontrol->private_value) {
1844 case CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT:
1845 mutex_lock(&chip->spos_mutex);
1846 change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
1847 if (ucontrol->value.integer.value[0] && !change)
1848 cs46xx_dsp_enable_spdif_out(chip);
1849 else if (change && !ucontrol->value.integer.value[0])
1850 cs46xx_dsp_disable_spdif_out(chip);
1852 res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED));
1853 mutex_unlock(&chip->spos_mutex);
1855 case CS46XX_MIXER_SPDIF_INPUT_ELEMENT:
1856 change = chip->dsp_spos_instance->spdif_status_in;
1857 if (ucontrol->value.integer.value[0] && !change) {
1858 cs46xx_dsp_enable_spdif_in(chip);
1859 /* restore volume */
1861 else if (change && !ucontrol->value.integer.value[0])
1862 cs46xx_dsp_disable_spdif_in(chip);
1864 res = (change != chip->dsp_spos_instance->spdif_status_in);
1868 snd_assert(0, (void)0);
1874 static int snd_cs46xx_adc_capture_get(struct snd_kcontrol *kcontrol,
1875 struct snd_ctl_elem_value *ucontrol)
1877 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1878 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1880 if (ins->adc_input != NULL)
1881 ucontrol->value.integer.value[0] = 1;
1883 ucontrol->value.integer.value[0] = 0;
1888 static int snd_cs46xx_adc_capture_put(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *ucontrol)
1891 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1892 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1895 if (ucontrol->value.integer.value[0] && !ins->adc_input) {
1896 cs46xx_dsp_enable_adc_capture(chip);
1898 } else if (!ucontrol->value.integer.value[0] && ins->adc_input) {
1899 cs46xx_dsp_disable_adc_capture(chip);
1905 static int snd_cs46xx_pcm_capture_get(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_value *ucontrol)
1908 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1909 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1911 if (ins->pcm_input != NULL)
1912 ucontrol->value.integer.value[0] = 1;
1914 ucontrol->value.integer.value[0] = 0;
1920 static int snd_cs46xx_pcm_capture_put(struct snd_kcontrol *kcontrol,
1921 struct snd_ctl_elem_value *ucontrol)
1923 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1924 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1927 if (ucontrol->value.integer.value[0] && !ins->pcm_input) {
1928 cs46xx_dsp_enable_pcm_capture(chip);
1930 } else if (!ucontrol->value.integer.value[0] && ins->pcm_input) {
1931 cs46xx_dsp_disable_pcm_capture(chip);
1938 static int snd_herc_spdif_select_get(struct snd_kcontrol *kcontrol,
1939 struct snd_ctl_elem_value *ucontrol)
1941 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1943 int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
1945 if (val1 & EGPIODR_GPOE0)
1946 ucontrol->value.integer.value[0] = 1;
1948 ucontrol->value.integer.value[0] = 0;
1954 * Game Theatre XP card - EGPIO[0] is used to select SPDIF input optical or coaxial.
1956 static int snd_herc_spdif_select_put(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_value *ucontrol)
1959 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1960 int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
1961 int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
1963 if (ucontrol->value.integer.value[0]) {
1964 /* optical is default */
1965 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
1966 EGPIODR_GPOE0 | val1); /* enable EGPIO0 output */
1967 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
1968 EGPIOPTR_GPPT0 | val2); /* open-drain on output */
1971 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE0); /* disable */
1972 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT0); /* disable */
1975 /* checking diff from the EGPIO direction register
1977 return (val1 != (int)snd_cs46xx_peekBA0(chip, BA0_EGPIODR));
1981 static int snd_cs46xx_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1983 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1988 static int snd_cs46xx_spdif_default_get(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
1991 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
1992 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1994 mutex_lock(&chip->spos_mutex);
1995 ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_default >> 24) & 0xff);
1996 ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_default >> 16) & 0xff);
1997 ucontrol->value.iec958.status[2] = 0;
1998 ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_default) & 0xff);
1999 mutex_unlock(&chip->spos_mutex);
2004 static int snd_cs46xx_spdif_default_put(struct snd_kcontrol *kcontrol,
2005 struct snd_ctl_elem_value *ucontrol)
2007 struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
2008 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
2012 mutex_lock(&chip->spos_mutex);
2013 val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
2014 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[2]) << 16) |
2015 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
2016 /* left and right validity bit */
2017 (1 << 13) | (1 << 12);
2020 change = (unsigned int)ins->spdif_csuv_default != val;
2021 ins->spdif_csuv_default = val;
2023 if ( !(ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN) )
2024 cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
2026 mutex_unlock(&chip->spos_mutex);
2031 static int snd_cs46xx_spdif_mask_get(struct snd_kcontrol *kcontrol,
2032 struct snd_ctl_elem_value *ucontrol)
2034 ucontrol->value.iec958.status[0] = 0xff;
2035 ucontrol->value.iec958.status[1] = 0xff;
2036 ucontrol->value.iec958.status[2] = 0x00;
2037 ucontrol->value.iec958.status[3] = 0xff;
2041 static int snd_cs46xx_spdif_stream_get(struct snd_kcontrol *kcontrol,
2042 struct snd_ctl_elem_value *ucontrol)
2044 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
2045 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
2047 mutex_lock(&chip->spos_mutex);
2048 ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_stream >> 24) & 0xff);
2049 ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_stream >> 16) & 0xff);
2050 ucontrol->value.iec958.status[2] = 0;
2051 ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_stream) & 0xff);
2052 mutex_unlock(&chip->spos_mutex);
2057 static int snd_cs46xx_spdif_stream_put(struct snd_kcontrol *kcontrol,
2058 struct snd_ctl_elem_value *ucontrol)
2060 struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
2061 struct dsp_spos_instance * ins = chip->dsp_spos_instance;
2065 mutex_lock(&chip->spos_mutex);
2066 val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
2067 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[1]) << 16) |
2068 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
2069 /* left and right validity bit */
2070 (1 << 13) | (1 << 12);
2073 change = ins->spdif_csuv_stream != val;
2074 ins->spdif_csuv_stream = val;
2076 if ( ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN )
2077 cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
2079 mutex_unlock(&chip->spos_mutex);
2084 #endif /* CONFIG_SND_CS46XX_NEW_DSP */
2087 #ifdef CONFIG_SND_CS46XX_DEBUG_GPIO
2088 static int snd_cs46xx_egpio_select_info(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_info *uinfo)
2091 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2093 uinfo->value.integer.min = 0;
2094 uinfo->value.integer.max = 8;
2098 static int snd_cs46xx_egpio_select_get(struct snd_kcontrol *kcontrol,
2099 struct snd_ctl_elem_value *ucontrol)
2101 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
2102 ucontrol->value.integer.value[0] = chip->current_gpio;
2107 static int snd_cs46xx_egpio_select_put(struct snd_kcontrol *kcontrol,
2108 struct snd_ctl_elem_value *ucontrol)
2110 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
2111 int change = (chip->current_gpio != ucontrol->value.integer.value[0]);
2112 chip->current_gpio = ucontrol->value.integer.value[0];
2118 static int snd_cs46xx_egpio_get(struct snd_kcontrol *kcontrol,
2119 struct snd_ctl_elem_value *ucontrol)
2121 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
2122 int reg = kcontrol->private_value;
2124 snd_printdd ("put: reg = %04x, gpio %02x\n",reg,chip->current_gpio);
2125 ucontrol->value.integer.value[0] =
2126 (snd_cs46xx_peekBA0(chip, reg) & (1 << chip->current_gpio)) ? 1 : 0;
2131 static int snd_cs46xx_egpio_put(struct snd_kcontrol *kcontrol,
2132 struct snd_ctl_elem_value *ucontrol)
2134 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
2135 int reg = kcontrol->private_value;
2136 int val = snd_cs46xx_peekBA0(chip, reg);
2138 snd_printdd ("put: reg = %04x, gpio %02x\n",reg,chip->current_gpio);
2140 if (ucontrol->value.integer.value[0])
2141 val |= (1 << chip->current_gpio);
2143 val &= ~(1 << chip->current_gpio);
2145 snd_cs46xx_pokeBA0(chip, reg,val);
2146 snd_printdd ("put: val %08x oldval %08x\n",val,oldval);
2148 return (oldval != val);
2150 #endif /* CONFIG_SND_CS46XX_DEBUG_GPIO */
2152 static struct snd_kcontrol_new snd_cs46xx_controls[] __devinitdata = {
2154 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2155 .name = "DAC Volume",
2156 .info = snd_cs46xx_vol_info,
2157 #ifndef CONFIG_SND_CS46XX_NEW_DSP
2158 .get = snd_cs46xx_vol_get,
2159 .put = snd_cs46xx_vol_put,
2160 .private_value = BA1_PVOL,
2162 .get = snd_cs46xx_vol_dac_get,
2163 .put = snd_cs46xx_vol_dac_put,
2168 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2169 .name = "ADC Volume",
2170 .info = snd_cs46xx_vol_info,
2171 .get = snd_cs46xx_vol_get,
2172 .put = snd_cs46xx_vol_put,
2173 #ifndef CONFIG_SND_CS46XX_NEW_DSP
2174 .private_value = BA1_CVOL,
2176 .private_value = (VARIDECIMATE_SCB_ADDR + 0xE) << 2,
2179 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2181 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2182 .name = "ADC Capture Switch",
2183 .info = snd_mixer_boolean_info,
2184 .get = snd_cs46xx_adc_capture_get,
2185 .put = snd_cs46xx_adc_capture_put
2188 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2189 .name = "DAC Capture Switch",
2190 .info = snd_mixer_boolean_info,
2191 .get = snd_cs46xx_pcm_capture_get,
2192 .put = snd_cs46xx_pcm_capture_put
2195 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2196 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
2197 .info = snd_mixer_boolean_info,
2198 .get = snd_cs46xx_iec958_get,
2199 .put = snd_cs46xx_iec958_put,
2200 .private_value = CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT,
2203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2204 .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,SWITCH),
2205 .info = snd_mixer_boolean_info,
2206 .get = snd_cs46xx_iec958_get,
2207 .put = snd_cs46xx_iec958_put,
2208 .private_value = CS46XX_MIXER_SPDIF_INPUT_ELEMENT,
2211 /* Input IEC958 volume does not work for the moment. (Benny) */
2213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2214 .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,VOLUME),
2215 .info = snd_cs46xx_vol_info,
2216 .get = snd_cs46xx_vol_iec958_get,
2217 .put = snd_cs46xx_vol_iec958_put,
2218 .private_value = (ASYNCRX_SCB_ADDR + 0xE) << 2,
2222 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2223 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2224 .info = snd_cs46xx_spdif_info,
2225 .get = snd_cs46xx_spdif_default_get,
2226 .put = snd_cs46xx_spdif_default_put,
2229 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2230 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2231 .info = snd_cs46xx_spdif_info,
2232 .get = snd_cs46xx_spdif_mask_get,
2233 .access = SNDRV_CTL_ELEM_ACCESS_READ
2236 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2237 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2238 .info = snd_cs46xx_spdif_info,
2239 .get = snd_cs46xx_spdif_stream_get,
2240 .put = snd_cs46xx_spdif_stream_put
2244 #ifdef CONFIG_SND_CS46XX_DEBUG_GPIO
2246 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2247 .name = "EGPIO select",
2248 .info = snd_cs46xx_egpio_select_info,
2249 .get = snd_cs46xx_egpio_select_get,
2250 .put = snd_cs46xx_egpio_select_put,
2254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2255 .name = "EGPIO Input/Output",
2256 .info = snd_mixer_boolean_info,
2257 .get = snd_cs46xx_egpio_get,
2258 .put = snd_cs46xx_egpio_put,
2259 .private_value = BA0_EGPIODR,
2262 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2263 .name = "EGPIO CMOS/Open drain",
2264 .info = snd_mixer_boolean_info,
2265 .get = snd_cs46xx_egpio_get,
2266 .put = snd_cs46xx_egpio_put,
2267 .private_value = BA0_EGPIOPTR,
2270 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2271 .name = "EGPIO On/Off",
2272 .info = snd_mixer_boolean_info,
2273 .get = snd_cs46xx_egpio_get,
2274 .put = snd_cs46xx_egpio_put,
2275 .private_value = BA0_EGPIOSR,
2280 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2281 /* set primary cs4294 codec into Extended Audio Mode */
2282 static int snd_cs46xx_front_dup_get(struct snd_kcontrol *kcontrol,
2283 struct snd_ctl_elem_value *ucontrol)
2285 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
2287 val = snd_ac97_read(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX], AC97_CSR_ACMODE);
2288 ucontrol->value.integer.value[0] = (val & 0x200) ? 0 : 1;
2292 static int snd_cs46xx_front_dup_put(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_value *ucontrol)
2295 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
2296 return snd_ac97_update_bits(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
2297 AC97_CSR_ACMODE, 0x200,
2298 ucontrol->value.integer.value[0] ? 0 : 0x200);
2301 static struct snd_kcontrol_new snd_cs46xx_front_dup_ctl = {
2302 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2303 .name = "Duplicate Front",
2304 .info = snd_mixer_boolean_info,
2305 .get = snd_cs46xx_front_dup_get,
2306 .put = snd_cs46xx_front_dup_put,
2310 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2311 /* Only available on the Hercules Game Theater XP soundcard */
2312 static struct snd_kcontrol_new snd_hercules_controls[] = {
2314 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2315 .name = "Optical/Coaxial SPDIF Input Switch",
2316 .info = snd_mixer_boolean_info,
2317 .get = snd_herc_spdif_select_get,
2318 .put = snd_herc_spdif_select_put,
2323 static void snd_cs46xx_codec_reset (struct snd_ac97 * ac97)
2325 unsigned long end_time;
2328 /* reset to defaults */
2329 snd_ac97_write(ac97, AC97_RESET, 0);
2331 /* set the desired CODEC mode */
2332 if (ac97->num == CS46XX_PRIMARY_CODEC_INDEX) {
2333 snd_printdd("cs46xx: CODOEC1 mode %04x\n",0x0);
2334 snd_cs46xx_ac97_write(ac97,AC97_CSR_ACMODE,0x0);
2335 } else if (ac97->num == CS46XX_SECONDARY_CODEC_INDEX) {
2336 snd_printdd("cs46xx: CODOEC2 mode %04x\n",0x3);
2337 snd_cs46xx_ac97_write(ac97,AC97_CSR_ACMODE,0x3);
2339 snd_assert(0); /* should never happen ... */
2344 /* it's necessary to wait awhile until registers are accessible after RESET */
2345 /* because the PCM or MASTER volume registers can be modified, */
2346 /* the REC_GAIN register is used for tests */
2347 end_time = jiffies + HZ;
2349 unsigned short ext_mid;
2351 /* use preliminary reads to settle the communication */
2352 snd_ac97_read(ac97, AC97_RESET);
2353 snd_ac97_read(ac97, AC97_VENDOR_ID1);
2354 snd_ac97_read(ac97, AC97_VENDOR_ID2);
2356 ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2357 if (ext_mid != 0xffff && (ext_mid & 1) != 0)
2360 /* test if we can write to the record gain volume register */
2361 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
2362 if ((err = snd_ac97_read(ac97, AC97_REC_GAIN)) == 0x8a05)
2366 } while (time_after_eq(end_time, jiffies));
2368 snd_printk(KERN_ERR "CS46xx secondary codec doesn't respond!\n");
2372 static int __devinit cs46xx_detect_codec(struct snd_cs46xx *chip, int codec)
2375 struct snd_ac97_template ac97;
2377 memset(&ac97, 0, sizeof(ac97));
2378 ac97.private_data = chip;
2379 ac97.private_free = snd_cs46xx_mixer_free_ac97;
2381 if (chip->amplifier_ctrl == amp_voyetra)
2382 ac97.scaps = AC97_SCAP_INV_EAPD;
2384 if (codec == CS46XX_SECONDARY_CODEC_INDEX) {
2385 snd_cs46xx_codec_write(chip, AC97_RESET, 0, codec);
2387 if (snd_cs46xx_codec_read(chip, AC97_RESET, codec) & 0x8000) {
2388 snd_printdd("snd_cs46xx: seconadry codec not present\n");
2393 snd_cs46xx_codec_write(chip, AC97_MASTER, 0x8000, codec);
2394 for (idx = 0; idx < 100; ++idx) {
2395 if (snd_cs46xx_codec_read(chip, AC97_MASTER, codec) == 0x8000) {
2396 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97[codec]);
2401 snd_printdd("snd_cs46xx: codec %d detection timeout\n", codec);
2405 int __devinit snd_cs46xx_mixer(struct snd_cs46xx *chip, int spdif_device)
2407 struct snd_card *card = chip->card;
2408 struct snd_ctl_elem_id id;
2411 static struct snd_ac97_bus_ops ops = {
2412 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2413 .reset = snd_cs46xx_codec_reset,
2415 .write = snd_cs46xx_ac97_write,
2416 .read = snd_cs46xx_ac97_read,
2419 /* detect primary codec */
2420 chip->nr_ac97_codecs = 0;
2421 snd_printdd("snd_cs46xx: detecting primary codec\n");
2422 if ((err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus)) < 0)
2424 chip->ac97_bus->private_free = snd_cs46xx_mixer_free_ac97_bus;
2426 if (cs46xx_detect_codec(chip, CS46XX_PRIMARY_CODEC_INDEX) < 0)
2428 chip->nr_ac97_codecs = 1;
2430 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2431 snd_printdd("snd_cs46xx: detecting seconadry codec\n");
2432 /* try detect a secondary codec */
2433 if (! cs46xx_detect_codec(chip, CS46XX_SECONDARY_CODEC_INDEX))
2434 chip->nr_ac97_codecs = 2;
2435 #endif /* CONFIG_SND_CS46XX_NEW_DSP */
2437 /* add cs4630 mixer controls */
2438 for (idx = 0; idx < ARRAY_SIZE(snd_cs46xx_controls); idx++) {
2439 struct snd_kcontrol *kctl;
2440 kctl = snd_ctl_new1(&snd_cs46xx_controls[idx], chip);
2441 if (kctl && kctl->id.iface == SNDRV_CTL_ELEM_IFACE_PCM)
2442 kctl->id.device = spdif_device;
2443 if ((err = snd_ctl_add(card, kctl)) < 0)
2447 /* get EAPD mixer switch (for voyetra hack) */
2448 memset(&id, 0, sizeof(id));
2449 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2450 strcpy(id.name, "External Amplifier");
2451 chip->eapd_switch = snd_ctl_find_id(chip->card, &id);
2453 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2454 if (chip->nr_ac97_codecs == 1) {
2455 unsigned int id2 = chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]->id & 0xffff;
2456 if (id2 == 0x592b || id2 == 0x592d) {
2457 err = snd_ctl_add(card, snd_ctl_new1(&snd_cs46xx_front_dup_ctl, chip));
2460 snd_ac97_write_cache(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
2461 AC97_CSR_ACMODE, 0x200);
2464 /* do soundcard specific mixer setup */
2465 if (chip->mixer_init) {
2466 snd_printdd ("calling chip->mixer_init(chip);\n");
2467 chip->mixer_init(chip);
2471 /* turn on amplifier */
2472 chip->amplifier_ctrl(chip, 1);
2481 static void snd_cs46xx_midi_reset(struct snd_cs46xx *chip)
2483 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, MIDCR_MRST);
2485 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2488 static int snd_cs46xx_midi_input_open(struct snd_rawmidi_substream *substream)
2490 struct snd_cs46xx *chip = substream->rmidi->private_data;
2492 chip->active_ctrl(chip, 1);
2493 spin_lock_irq(&chip->reg_lock);
2494 chip->uartm |= CS46XX_MODE_INPUT;
2495 chip->midcr |= MIDCR_RXE;
2496 chip->midi_input = substream;
2497 if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
2498 snd_cs46xx_midi_reset(chip);
2500 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2502 spin_unlock_irq(&chip->reg_lock);
2506 static int snd_cs46xx_midi_input_close(struct snd_rawmidi_substream *substream)
2508 struct snd_cs46xx *chip = substream->rmidi->private_data;
2510 spin_lock_irq(&chip->reg_lock);
2511 chip->midcr &= ~(MIDCR_RXE | MIDCR_RIE);
2512 chip->midi_input = NULL;
2513 if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
2514 snd_cs46xx_midi_reset(chip);
2516 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2518 chip->uartm &= ~CS46XX_MODE_INPUT;
2519 spin_unlock_irq(&chip->reg_lock);
2520 chip->active_ctrl(chip, -1);
2524 static int snd_cs46xx_midi_output_open(struct snd_rawmidi_substream *substream)
2526 struct snd_cs46xx *chip = substream->rmidi->private_data;
2528 chip->active_ctrl(chip, 1);
2530 spin_lock_irq(&chip->reg_lock);
2531 chip->uartm |= CS46XX_MODE_OUTPUT;
2532 chip->midcr |= MIDCR_TXE;
2533 chip->midi_output = substream;
2534 if (!(chip->uartm & CS46XX_MODE_INPUT)) {
2535 snd_cs46xx_midi_reset(chip);
2537 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2539 spin_unlock_irq(&chip->reg_lock);
2543 static int snd_cs46xx_midi_output_close(struct snd_rawmidi_substream *substream)
2545 struct snd_cs46xx *chip = substream->rmidi->private_data;
2547 spin_lock_irq(&chip->reg_lock);
2548 chip->midcr &= ~(MIDCR_TXE | MIDCR_TIE);
2549 chip->midi_output = NULL;
2550 if (!(chip->uartm & CS46XX_MODE_INPUT)) {
2551 snd_cs46xx_midi_reset(chip);
2553 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2555 chip->uartm &= ~CS46XX_MODE_OUTPUT;
2556 spin_unlock_irq(&chip->reg_lock);
2557 chip->active_ctrl(chip, -1);
2561 static void snd_cs46xx_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
2563 unsigned long flags;
2564 struct snd_cs46xx *chip = substream->rmidi->private_data;
2566 spin_lock_irqsave(&chip->reg_lock, flags);
2568 if ((chip->midcr & MIDCR_RIE) == 0) {
2569 chip->midcr |= MIDCR_RIE;
2570 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2573 if (chip->midcr & MIDCR_RIE) {
2574 chip->midcr &= ~MIDCR_RIE;
2575 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2578 spin_unlock_irqrestore(&chip->reg_lock, flags);
2581 static void snd_cs46xx_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
2583 unsigned long flags;
2584 struct snd_cs46xx *chip = substream->rmidi->private_data;
2587 spin_lock_irqsave(&chip->reg_lock, flags);
2589 if ((chip->midcr & MIDCR_TIE) == 0) {
2590 chip->midcr |= MIDCR_TIE;
2591 /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
2592 while ((chip->midcr & MIDCR_TIE) &&
2593 (snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
2594 if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
2595 chip->midcr &= ~MIDCR_TIE;
2597 snd_cs46xx_pokeBA0(chip, BA0_MIDWP, byte);
2600 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2603 if (chip->midcr & MIDCR_TIE) {
2604 chip->midcr &= ~MIDCR_TIE;
2605 snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
2608 spin_unlock_irqrestore(&chip->reg_lock, flags);
2611 static struct snd_rawmidi_ops snd_cs46xx_midi_output =
2613 .open = snd_cs46xx_midi_output_open,
2614 .close = snd_cs46xx_midi_output_close,
2615 .trigger = snd_cs46xx_midi_output_trigger,
2618 static struct snd_rawmidi_ops snd_cs46xx_midi_input =
2620 .open = snd_cs46xx_midi_input_open,
2621 .close = snd_cs46xx_midi_input_close,
2622 .trigger = snd_cs46xx_midi_input_trigger,
2625 int __devinit snd_cs46xx_midi(struct snd_cs46xx *chip, int device, struct snd_rawmidi **rrawmidi)
2627 struct snd_rawmidi *rmidi;
2632 if ((err = snd_rawmidi_new(chip->card, "CS46XX", device, 1, 1, &rmidi)) < 0)
2634 strcpy(rmidi->name, "CS46XX");
2635 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs46xx_midi_output);
2636 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs46xx_midi_input);
2637 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2638 rmidi->private_data = chip;
2639 chip->rmidi = rmidi;
2647 * gameport interface
2650 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
2652 static void snd_cs46xx_gameport_trigger(struct gameport *gameport)
2654 struct snd_cs46xx *chip = gameport_get_port_data(gameport);
2656 snd_assert(chip, return);
2657 snd_cs46xx_pokeBA0(chip, BA0_JSPT, 0xFF); //outb(gameport->io, 0xFF);
2660 static unsigned char snd_cs46xx_gameport_read(struct gameport *gameport)
2662 struct snd_cs46xx *chip = gameport_get_port_data(gameport);
2664 snd_assert(chip, return 0);
2665 return snd_cs46xx_peekBA0(chip, BA0_JSPT); //inb(gameport->io);
2668 static int snd_cs46xx_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
2670 struct snd_cs46xx *chip = gameport_get_port_data(gameport);
2671 unsigned js1, js2, jst;
2673 snd_assert(chip, return 0);
2675 js1 = snd_cs46xx_peekBA0(chip, BA0_JSC1);
2676 js2 = snd_cs46xx_peekBA0(chip, BA0_JSC2);
2677 jst = snd_cs46xx_peekBA0(chip, BA0_JSPT);
2679 *buttons = (~jst >> 4) & 0x0F;
2681 axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
2682 axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
2683 axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
2684 axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
2686 for(jst=0;jst<4;++jst)
2687 if(axes[jst]==0xFFFF) axes[jst] = -1;
2691 static int snd_cs46xx_gameport_open(struct gameport *gameport, int mode)
2694 case GAMEPORT_MODE_COOKED:
2696 case GAMEPORT_MODE_RAW:
2704 int __devinit snd_cs46xx_gameport(struct snd_cs46xx *chip)
2706 struct gameport *gp;
2708 chip->gameport = gp = gameport_allocate_port();
2710 printk(KERN_ERR "cs46xx: cannot allocate memory for gameport\n");
2714 gameport_set_name(gp, "CS46xx Gameport");
2715 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
2716 gameport_set_dev_parent(gp, &chip->pci->dev);
2717 gameport_set_port_data(gp, chip);
2719 gp->open = snd_cs46xx_gameport_open;
2720 gp->read = snd_cs46xx_gameport_read;
2721 gp->trigger = snd_cs46xx_gameport_trigger;
2722 gp->cooked_read = snd_cs46xx_gameport_cooked_read;
2724 snd_cs46xx_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
2725 snd_cs46xx_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
2727 gameport_register_port(gp);
2732 static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip)
2734 if (chip->gameport) {
2735 gameport_unregister_port(chip->gameport);
2736 chip->gameport = NULL;
2740 int __devinit snd_cs46xx_gameport(struct snd_cs46xx *chip) { return -ENOSYS; }
2741 static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip) { }
2742 #endif /* CONFIG_GAMEPORT */
2744 #ifdef CONFIG_PROC_FS
2749 static long snd_cs46xx_io_read(struct snd_info_entry *entry, void *file_private_data,
2750 struct file *file, char __user *buf,
2751 unsigned long count, unsigned long pos)
2754 struct snd_cs46xx_region *region = entry->private_data;
2757 if (pos + (size_t)size > region->size)
2758 size = region->size - pos;
2760 if (copy_to_user_fromio(buf, region->remap_addr + pos, size))
2766 static struct snd_info_entry_ops snd_cs46xx_proc_io_ops = {
2767 .read = snd_cs46xx_io_read,
2770 static int __devinit snd_cs46xx_proc_init(struct snd_card *card, struct snd_cs46xx *chip)
2772 struct snd_info_entry *entry;
2775 for (idx = 0; idx < 5; idx++) {
2776 struct snd_cs46xx_region *region = &chip->region.idx[idx];
2777 if (! snd_card_proc_new(card, region->name, &entry)) {
2778 entry->content = SNDRV_INFO_CONTENT_DATA;
2779 entry->private_data = chip;
2780 entry->c.ops = &snd_cs46xx_proc_io_ops;
2781 entry->size = region->size;
2782 entry->mode = S_IFREG | S_IRUSR;
2785 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2786 cs46xx_dsp_proc_init(card, chip);
2791 static int snd_cs46xx_proc_done(struct snd_cs46xx *chip)
2793 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2794 cs46xx_dsp_proc_done(chip);
2798 #else /* !CONFIG_PROC_FS */
2799 #define snd_cs46xx_proc_init(card, chip)
2800 #define snd_cs46xx_proc_done(chip)
2806 static void snd_cs46xx_hw_stop(struct snd_cs46xx *chip)
2810 tmp = snd_cs46xx_peek(chip, BA1_PFIE);
2813 snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt disable */
2815 tmp = snd_cs46xx_peek(chip, BA1_CIE);
2818 snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt disable */
2821 * Stop playback DMA.
2823 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
2824 snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
2829 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
2830 snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
2833 * Reset the processor.
2835 snd_cs46xx_reset(chip);
2837 snd_cs46xx_proc_stop(chip);
2840 * Power down the PLL.
2842 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
2845 * Turn off the Processor by turning off the software clock enable flag in
2846 * the clock control register.
2848 tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE;
2849 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
2853 static int snd_cs46xx_free(struct snd_cs46xx *chip)
2857 snd_assert(chip != NULL, return -EINVAL);
2859 if (chip->active_ctrl)
2860 chip->active_ctrl(chip, 1);
2862 snd_cs46xx_remove_gameport(chip);
2864 if (chip->amplifier_ctrl)
2865 chip->amplifier_ctrl(chip, -chip->amplifier); /* force to off */
2867 snd_cs46xx_proc_done(chip);
2869 if (chip->region.idx[0].resource)
2870 snd_cs46xx_hw_stop(chip);
2873 free_irq(chip->irq, chip);
2875 for (idx = 0; idx < 5; idx++) {
2876 struct snd_cs46xx_region *region = &chip->region.idx[idx];
2877 if (region->remap_addr)
2878 iounmap(region->remap_addr);
2879 release_and_free_resource(region->resource);
2882 if (chip->active_ctrl)
2883 chip->active_ctrl(chip, -chip->amplifier);
2885 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2886 if (chip->dsp_spos_instance) {
2887 cs46xx_dsp_spos_destroy(chip);
2888 chip->dsp_spos_instance = NULL;
2893 kfree(chip->saved_regs);
2896 pci_disable_device(chip->pci);
2901 static int snd_cs46xx_dev_free(struct snd_device *device)
2903 struct snd_cs46xx *chip = device->device_data;
2904 return snd_cs46xx_free(chip);
2910 static int snd_cs46xx_chip_init(struct snd_cs46xx *chip)
2915 * First, blast the clock control register to zero so that the PLL starts
2916 * out in a known state, and blast the master serial port control register
2917 * to zero so that the serial ports also start out in a known state.
2919 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
2920 snd_cs46xx_pokeBA0(chip, BA0_SERMC1, 0);
2923 * If we are in AC97 mode, then we must set the part to a host controlled
2924 * AC-link. Otherwise, we won't be able to bring up the link.
2926 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2927 snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0 |
2928 SERACC_TWO_CODECS); /* 2.00 dual codecs */
2929 /* snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0); */ /* 2.00 codec */
2931 snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_1_03); /* 1.03 codec */
2935 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
2936 * spec) and then drive it high. This is done for non AC97 modes since
2937 * there might be logic external to the CS461x that uses the ARST# line
2940 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, 0);
2941 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2942 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, 0);
2945 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_RSTN);
2946 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2947 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_RSTN);
2951 * The first thing we do here is to enable sync generation. As soon
2952 * as we start receiving bit clock, we'll start producing the SYNC
2955 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
2956 #ifdef CONFIG_SND_CS46XX_NEW_DSP
2957 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_ESYN | ACCTL_RSTN);
2961 * Now wait for a short while to allow the AC97 part to start
2962 * generating bit clock (so we don't try to start the PLL without an
2968 * Set the serial port timing configuration, so that
2969 * the clock control circuit gets its clock from the correct place.
2971 snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97);
2974 * Write the selected clock control setup to the hardware. Do not turn on
2975 * SWCE yet (if requested), so that the devices clocked by the output of
2976 * PLL are not clocked until the PLL is stable.
2978 snd_cs46xx_pokeBA0(chip, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
2979 snd_cs46xx_pokeBA0(chip, BA0_PLLM, 0x3a);
2980 snd_cs46xx_pokeBA0(chip, BA0_CLKCR2, CLKCR2_PDIVS_8);
2985 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP);
2988 * Wait until the PLL has stabilized.
2993 * Turn on clocking of the core so that we can setup the serial ports.
2995 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP | CLKCR1_SWCE);
2998 * Enable FIFO Host Bypass
3000 snd_cs46xx_pokeBA0(chip, BA0_SERBCF, SERBCF_HBP);
3003 * Fill the serial port FIFOs with silence.
3005 snd_cs46xx_clear_serial_FIFOs(chip);
3008 * Set the serial port FIFO pointer to the first sample in the FIFO.
3010 /* snd_cs46xx_pokeBA0(chip, BA0_SERBSP, 0); */
3013 * Write the serial port configuration to the part. The master
3014 * enable bit is not set until all other values have been written.
3016 snd_cs46xx_pokeBA0(chip, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
3017 snd_cs46xx_pokeBA0(chip, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
3018 snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
3021 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3022 snd_cs46xx_pokeBA0(chip, BA0_SERC7, SERC7_ASDI2EN);
3023 snd_cs46xx_pokeBA0(chip, BA0_SERC3, 0);
3024 snd_cs46xx_pokeBA0(chip, BA0_SERC4, 0);
3025 snd_cs46xx_pokeBA0(chip, BA0_SERC5, 0);
3026 snd_cs46xx_pokeBA0(chip, BA0_SERC6, 1);
3033 * Wait for the codec ready signal from the AC97 codec.
3036 while (timeout-- > 0) {
3038 * Read the AC97 status register to see if we've seen a CODEC READY
3039 * signal from the AC97 codec.
3041 if (snd_cs46xx_peekBA0(chip, BA0_ACSTS) & ACSTS_CRDY)
3047 snd_printk(KERN_ERR "create - never read codec ready from AC'97\n");
3048 snd_printk(KERN_ERR "it is not probably bug, try to use CS4236 driver\n");
3051 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3054 for (count = 0; count < 150; count++) {
3055 /* First, we want to wait for a short time. */
3058 if (snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY)
3063 * Make sure CODEC is READY.
3065 if (!(snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY))
3066 snd_printdd("cs46xx: never read card ready from secondary AC'97\n");
3071 * Assert the vaid frame signal so that we can start sending commands
3072 * to the AC97 codec.
3074 snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3075 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3076 snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3081 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
3082 * the codec is pumping ADC data across the AC-link.
3085 while (timeout-- > 0) {
3087 * Read the input slot valid register and see if input slots 3 and
3090 if ((snd_cs46xx_peekBA0(chip, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
3095 #ifndef CONFIG_SND_CS46XX_NEW_DSP
3096 snd_printk(KERN_ERR "create - never read ISV3 & ISV4 from AC'97\n");
3099 /* This may happen on a cold boot with a Terratec SiXPack 5.1.
3100 Reloading the driver may help, if there's other soundcards
3101 with the same problem I would like to know. (Benny) */
3103 snd_printk(KERN_ERR "ERROR: snd-cs46xx: never read ISV3 & ISV4 from AC'97\n");
3104 snd_printk(KERN_ERR " Try reloading the ALSA driver, if you find something\n");
3105 snd_printk(KERN_ERR " broken or not working on your soundcard upon\n");
3106 snd_printk(KERN_ERR " this message please report to alsa-devel@alsa-project.org\n");
3113 * Now, assert valid frame and the slot 3 and 4 valid bits. This will
3114 * commense the transfer of digital audio data to the AC97 codec.
3117 snd_cs46xx_pokeBA0(chip, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
3121 * Power down the DAC and ADC. We will power them up (if) when we need
3124 /* snd_cs46xx_pokeBA0(chip, BA0_AC97_POWERDOWN, 0x300); */
3127 * Turn off the Processor by turning off the software clock enable flag in
3128 * the clock control register.
3130 /* tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE; */
3131 /* snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp); */
3137 * start and load DSP
3140 static void cs46xx_enable_stream_irqs(struct snd_cs46xx *chip)
3144 snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_IEV | HICR_CHGM);
3146 tmp = snd_cs46xx_peek(chip, BA1_PFIE);
3148 snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt enable */
3150 tmp = snd_cs46xx_peek(chip, BA1_CIE);
3153 snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt enable */
3156 int __devinit snd_cs46xx_start_dsp(struct snd_cs46xx *chip)
3160 * Reset the processor.
3162 snd_cs46xx_reset(chip);
3164 * Download the image to the processor.
3166 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3168 if (cs46xx_dsp_load_module(chip, &cwcemb80_module) < 0) {
3169 snd_printk(KERN_ERR "image download error\n");
3174 if (cs46xx_dsp_load_module(chip, &cwc4630_module) < 0) {
3175 snd_printk(KERN_ERR "image download error [cwc4630]\n");
3179 if (cs46xx_dsp_load_module(chip, &cwcasync_module) < 0) {
3180 snd_printk(KERN_ERR "image download error [cwcasync]\n");
3184 if (cs46xx_dsp_load_module(chip, &cwcsnoop_module) < 0) {
3185 snd_printk(KERN_ERR "image download error [cwcsnoop]\n");
3189 if (cs46xx_dsp_load_module(chip, &cwcbinhack_module) < 0) {
3190 snd_printk(KERN_ERR "image download error [cwcbinhack]\n");
3194 if (cs46xx_dsp_load_module(chip, &cwcdma_module) < 0) {
3195 snd_printk(KERN_ERR "image download error [cwcdma]\n");
3199 if (cs46xx_dsp_scb_and_task_init(chip) < 0)
3203 if (snd_cs46xx_download_image(chip) < 0) {
3204 snd_printk(KERN_ERR "image download error\n");
3209 * Stop playback DMA.
3211 tmp = snd_cs46xx_peek(chip, BA1_PCTL);
3212 chip->play_ctl = tmp & 0xffff0000;
3213 snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
3219 tmp = snd_cs46xx_peek(chip, BA1_CCTL);
3220 chip->capt.ctl = tmp & 0x0000ffff;
3221 snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
3225 snd_cs46xx_set_play_sample_rate(chip, 8000);
3226 snd_cs46xx_set_capture_sample_rate(chip, 8000);
3228 snd_cs46xx_proc_start(chip);
3230 cs46xx_enable_stream_irqs(chip);
3232 #ifndef CONFIG_SND_CS46XX_NEW_DSP
3233 /* set the attenuation to 0dB */
3234 snd_cs46xx_poke(chip, BA1_PVOL, 0x80008000);
3235 snd_cs46xx_poke(chip, BA1_CVOL, 0x80008000);
3243 * AMP control - null AMP
3246 static void amp_none(struct snd_cs46xx *chip, int change)
3250 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3251 static int voyetra_setup_eapd_slot(struct snd_cs46xx *chip)
3254 u32 idx, valid_slots,tmp,powerdown = 0;
3255 u16 modem_power,pin_config,logic_type;
3257 snd_printdd ("cs46xx: cs46xx_setup_eapd_slot()+\n");
3260 * See if the devices are powered down. If so, we must power them up first
3261 * or they will not respond.
3263 tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
3265 if (!(tmp & CLKCR1_SWCE)) {
3266 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
3271 * Clear PRA. The Bonzo chip will be used for GPIO not for modem
3274 if(chip->nr_ac97_codecs != 2) {
3275 snd_printk (KERN_ERR "cs46xx: cs46xx_setup_eapd_slot() - no secondary codec configured\n");
3279 modem_power = snd_cs46xx_codec_read (chip,
3280 AC97_EXTENDED_MSTATUS,
3281 CS46XX_SECONDARY_CODEC_INDEX);
3282 modem_power &=0xFEFF;
3284 snd_cs46xx_codec_write(chip,
3285 AC97_EXTENDED_MSTATUS, modem_power,
3286 CS46XX_SECONDARY_CODEC_INDEX);
3289 * Set GPIO pin's 7 and 8 so that they are configured for output.
3291 pin_config = snd_cs46xx_codec_read (chip,
3293 CS46XX_SECONDARY_CODEC_INDEX);
3296 snd_cs46xx_codec_write(chip,
3297 AC97_GPIO_CFG, pin_config,
3298 CS46XX_SECONDARY_CODEC_INDEX);
3301 * Set GPIO pin's 7 and 8 so that they are compatible with CMOS logic.
3304 logic_type = snd_cs46xx_codec_read(chip, AC97_GPIO_POLARITY,
3305 CS46XX_SECONDARY_CODEC_INDEX);
3308 snd_cs46xx_codec_write (chip, AC97_GPIO_POLARITY, logic_type,
3309 CS46XX_SECONDARY_CODEC_INDEX);
3311 valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
3312 valid_slots |= 0x200;
3313 snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
3315 if ( cs46xx_wait_for_fifo(chip,1) ) {
3316 snd_printdd("FIFO is busy\n");
3322 * Fill slots 12 with the correct value for the GPIO pins.
3324 for(idx = 0x90; idx <= 0x9F; idx++) {
3326 * Initialize the fifo so that bits 7 and 8 are on.
3328 * Remember that the GPIO pins in bonzo are shifted by 4 bits to
3329 * the left. 0x1800 corresponds to bits 7 and 8.
3331 snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0x1800);
3334 * Wait for command to complete
3336 if ( cs46xx_wait_for_fifo(chip,200) ) {
3337 snd_printdd("failed waiting for FIFO at addr (%02X)\n",idx);
3343 * Write the serial port FIFO index.
3345 snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
3348 * Tell the serial port to load the new value into the FIFO location.
3350 snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
3353 /* wait for last command to complete */
3354 cs46xx_wait_for_fifo(chip,200);
3357 * Now, if we powered up the devices, then power them back down again.
3358 * This is kinda ugly, but should never happen.
3361 snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
3371 static void amp_voyetra(struct snd_cs46xx *chip, int change)
3373 /* Manage the EAPD bit on the Crystal 4297
3374 and the Analog AD1885 */
3376 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3377 int old = chip->amplifier;
3381 chip->amplifier += change;
3382 oval = snd_cs46xx_codec_read(chip, AC97_POWERDOWN,
3383 CS46XX_PRIMARY_CODEC_INDEX);
3385 if (chip->amplifier) {
3386 /* Turn the EAPD amp on */
3389 /* Turn the EAPD amp off */
3393 snd_cs46xx_codec_write(chip, AC97_POWERDOWN, val,
3394 CS46XX_PRIMARY_CODEC_INDEX);
3395 if (chip->eapd_switch)
3396 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3397 &chip->eapd_switch->id);
3400 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3401 if (chip->amplifier && !old) {
3402 voyetra_setup_eapd_slot(chip);
3407 static void hercules_init(struct snd_cs46xx *chip)
3409 /* default: AMP off, and SPDIF input optical */
3410 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
3411 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
3416 * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
3418 static void amp_hercules(struct snd_cs46xx *chip, int change)
3420 int old = chip->amplifier;
3421 int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
3422 int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
3424 chip->amplifier += change;
3425 if (chip->amplifier && !old) {
3426 snd_printdd ("Hercules amplifier ON\n");
3428 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
3429 EGPIODR_GPOE2 | val1); /* enable EGPIO2 output */
3430 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
3431 EGPIOPTR_GPPT2 | val2); /* open-drain on output */
3432 } else if (old && !chip->amplifier) {
3433 snd_printdd ("Hercules amplifier OFF\n");
3434 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE2); /* disable */
3435 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT2); /* disable */
3439 static void voyetra_mixer_init (struct snd_cs46xx *chip)
3441 snd_printdd ("initializing Voyetra mixer\n");
3443 /* Enable SPDIF out */
3444 snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
3445 snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
3448 static void hercules_mixer_init (struct snd_cs46xx *chip)
3450 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3453 struct snd_card *card = chip->card;
3456 /* set EGPIO to default */
3457 hercules_init(chip);
3459 snd_printdd ("initializing Hercules mixer\n");
3461 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3462 if (chip->in_suspend)
3465 for (idx = 0 ; idx < ARRAY_SIZE(snd_hercules_controls); idx++) {
3466 struct snd_kcontrol *kctl;
3468 kctl = snd_ctl_new1(&snd_hercules_controls[idx], chip);
3469 if ((err = snd_ctl_add(card, kctl)) < 0) {
3470 printk (KERN_ERR "cs46xx: failed to initialize Hercules mixer (%d)\n",err);
3483 static void amp_voyetra_4294(struct snd_cs46xx *chip, int change)
3485 chip->amplifier += change;
3487 if (chip->amplifier) {
3488 /* Switch the GPIO pins 7 and 8 to open drain */
3489 snd_cs46xx_codec_write(chip, 0x4C,
3490 snd_cs46xx_codec_read(chip, 0x4C) & 0xFE7F);
3491 snd_cs46xx_codec_write(chip, 0x4E,
3492 snd_cs46xx_codec_read(chip, 0x4E) | 0x0180);
3493 /* Now wake the AMP (this might be backwards) */
3494 snd_cs46xx_codec_write(chip, 0x54,
3495 snd_cs46xx_codec_read(chip, 0x54) & ~0x0180);
3497 snd_cs46xx_codec_write(chip, 0x54,
3498 snd_cs46xx_codec_read(chip, 0x54) | 0x0180);
3505 * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
3506 * whenever we need to beat on the chip.
3508 * The original idea and code for this hack comes from David Kaiser at
3509 * Linuxcare. Perhaps one day Crystal will document their chips well
3510 * enough to make them useful.
3513 static void clkrun_hack(struct snd_cs46xx *chip, int change)
3517 if (!chip->acpi_port)
3520 chip->amplifier += change;
3522 /* Read ACPI port */
3523 nval = control = inw(chip->acpi_port + 0x10);
3525 /* Flip CLKRUN off while running */
3526 if (! chip->amplifier)
3530 if (nval != control)
3531 outw(nval, chip->acpi_port + 0x10);
3536 * detect intel piix4
3538 static void clkrun_init(struct snd_cs46xx *chip)
3540 struct pci_dev *pdev;
3543 chip->acpi_port = 0;
3545 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
3546 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3548 return; /* Not a thinkpad thats for sure */
3550 /* Find the control port */
3551 pci_read_config_byte(pdev, 0x41, &pp);
3552 chip->acpi_port = pp << 8;
3566 void (*init)(struct snd_cs46xx *);
3567 void (*amp)(struct snd_cs46xx *, int);
3568 void (*active)(struct snd_cs46xx *, int);
3569 void (*mixer_init)(struct snd_cs46xx *);
3572 static struct cs_card_type __devinitdata cards[] = {
3576 .name = "Genius Soundmaker 128 value",
3577 /* nothing special */
3584 .mixer_init = voyetra_mixer_init,
3589 .name = "Mitac MI6020/21",
3595 .name = "Hercules Game Theatre XP",
3596 .amp = amp_hercules,
3597 .mixer_init = hercules_mixer_init,
3602 .name = "Hercules Game Theatre XP",
3603 .amp = amp_hercules,
3604 .mixer_init = hercules_mixer_init,
3609 .name = "Hercules Game Theatre XP",
3610 .amp = amp_hercules,
3611 .mixer_init = hercules_mixer_init,
3617 .name = "Hercules Game Theatre XP",
3618 .amp = amp_hercules,
3619 .mixer_init = hercules_mixer_init,
3624 .name = "Hercules Game Theatre XP",
3625 .amp = amp_hercules,
3626 .mixer_init = hercules_mixer_init,
3631 .name = "Hercules Game Theatre XP",
3632 .amp = amp_hercules,
3633 .mixer_init = hercules_mixer_init,
3639 .name = "Terratec SiXPack 5.1",
3641 /* Not sure if the 570 needs the clkrun hack */
3643 .vendor = PCI_VENDOR_ID_IBM,
3645 .name = "Thinkpad 570",
3646 .init = clkrun_init,
3647 .active = clkrun_hack,
3650 .vendor = PCI_VENDOR_ID_IBM,
3652 .name = "Thinkpad 600X/A20/T20",
3653 .init = clkrun_init,
3654 .active = clkrun_hack,
3657 .vendor = PCI_VENDOR_ID_IBM,
3659 .name = "Thinkpad 600E (unsupported)",
3669 static unsigned int saved_regs[] = {
3677 int snd_cs46xx_suspend(struct pci_dev *pci, pm_message_t state)
3679 struct snd_card *card = pci_get_drvdata(pci);
3680 struct snd_cs46xx *chip = card->private_data;
3683 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3684 chip->in_suspend = 1;
3685 snd_pcm_suspend_all(chip->pcm);
3686 // chip->ac97_powerdown = snd_cs46xx_codec_read(chip, AC97_POWER_CONTROL);
3687 // chip->ac97_general_purpose = snd_cs46xx_codec_read(chip, BA0_AC97_GENERAL_PURPOSE);
3689 snd_ac97_suspend(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
3690 snd_ac97_suspend(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
3692 /* save some registers */
3693 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3694 chip->saved_regs[i] = snd_cs46xx_peekBA0(chip, saved_regs[i]);
3696 amp_saved = chip->amplifier;
3698 chip->amplifier_ctrl(chip, -chip->amplifier);
3699 snd_cs46xx_hw_stop(chip);
3700 /* disable CLKRUN */
3701 chip->active_ctrl(chip, -chip->amplifier);
3702 chip->amplifier = amp_saved; /* restore the status */
3704 pci_disable_device(pci);
3705 pci_save_state(pci);
3706 pci_set_power_state(pci, pci_choose_state(pci, state));
3710 int snd_cs46xx_resume(struct pci_dev *pci)
3712 struct snd_card *card = pci_get_drvdata(pci);
3713 struct snd_cs46xx *chip = card->private_data;
3716 pci_set_power_state(pci, PCI_D0);
3717 pci_restore_state(pci);
3718 if (pci_enable_device(pci) < 0) {
3719 printk(KERN_ERR "cs46xx: pci_enable_device failed, "
3720 "disabling device\n");
3721 snd_card_disconnect(card);
3724 pci_set_master(pci);
3726 amp_saved = chip->amplifier;
3727 chip->amplifier = 0;
3728 chip->active_ctrl(chip, 1); /* force to on */
3730 snd_cs46xx_chip_init(chip);
3732 snd_cs46xx_reset(chip);
3733 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3734 cs46xx_dsp_resume(chip);
3735 /* restore some registers */
3736 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3737 snd_cs46xx_pokeBA0(chip, saved_regs[i], chip->saved_regs[i]);
3739 snd_cs46xx_download_image(chip);
3743 snd_cs46xx_codec_write(chip, BA0_AC97_GENERAL_PURPOSE,
3744 chip->ac97_general_purpose);
3745 snd_cs46xx_codec_write(chip, AC97_POWER_CONTROL,
3746 chip->ac97_powerdown);
3748 snd_cs46xx_codec_write(chip, BA0_AC97_POWERDOWN,
3749 chip->ac97_powerdown);
3753 snd_ac97_resume(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
3754 snd_ac97_resume(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
3756 /* reset playback/capture */
3757 snd_cs46xx_set_play_sample_rate(chip, 8000);
3758 snd_cs46xx_set_capture_sample_rate(chip, 8000);
3759 snd_cs46xx_proc_start(chip);
3761 cs46xx_enable_stream_irqs(chip);
3764 chip->amplifier_ctrl(chip, 1); /* turn amp on */
3766 chip->active_ctrl(chip, -1); /* disable CLKRUN */
3767 chip->amplifier = amp_saved;
3768 chip->in_suspend = 0;
3769 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3772 #endif /* CONFIG_PM */
3778 int __devinit snd_cs46xx_create(struct snd_card *card,
3779 struct pci_dev * pci,
3780 int external_amp, int thinkpad,
3781 struct snd_cs46xx ** rchip)
3783 struct snd_cs46xx *chip;
3785 struct snd_cs46xx_region *region;
3786 struct cs_card_type *cp;
3787 u16 ss_card, ss_vendor;
3788 static struct snd_device_ops ops = {
3789 .dev_free = snd_cs46xx_dev_free,
3794 /* enable PCI device */
3795 if ((err = pci_enable_device(pci)) < 0)
3798 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
3800 pci_disable_device(pci);
3803 spin_lock_init(&chip->reg_lock);
3804 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3805 mutex_init(&chip->spos_mutex);
3810 chip->ba0_addr = pci_resource_start(pci, 0);
3811 chip->ba1_addr = pci_resource_start(pci, 1);
3812 if (chip->ba0_addr == 0 || chip->ba0_addr == (unsigned long)~0 ||
3813 chip->ba1_addr == 0 || chip->ba1_addr == (unsigned long)~0) {
3814 snd_printk(KERN_ERR "wrong address(es) - ba0 = 0x%lx, ba1 = 0x%lx\n",
3815 chip->ba0_addr, chip->ba1_addr);
3816 snd_cs46xx_free(chip);
3820 region = &chip->region.name.ba0;
3821 strcpy(region->name, "CS46xx_BA0");
3822 region->base = chip->ba0_addr;
3823 region->size = CS46XX_BA0_SIZE;
3825 region = &chip->region.name.data0;
3826 strcpy(region->name, "CS46xx_BA1_data0");
3827 region->base = chip->ba1_addr + BA1_SP_DMEM0;
3828 region->size = CS46XX_BA1_DATA0_SIZE;
3830 region = &chip->region.name.data1;
3831 strcpy(region->name, "CS46xx_BA1_data1");
3832 region->base = chip->ba1_addr + BA1_SP_DMEM1;
3833 region->size = CS46XX_BA1_DATA1_SIZE;
3835 region = &chip->region.name.pmem;
3836 strcpy(region->name, "CS46xx_BA1_pmem");
3837 region->base = chip->ba1_addr + BA1_SP_PMEM;
3838 region->size = CS46XX_BA1_PRG_SIZE;
3840 region = &chip->region.name.reg;
3841 strcpy(region->name, "CS46xx_BA1_reg");
3842 region->base = chip->ba1_addr + BA1_SP_REG;
3843 region->size = CS46XX_BA1_REG_SIZE;
3845 /* set up amp and clkrun hack */
3846 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
3847 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &ss_card);
3849 for (cp = &cards[0]; cp->name; cp++) {
3850 if (cp->vendor == ss_vendor && cp->id == ss_card) {
3851 snd_printdd ("hack for %s enabled\n", cp->name);
3853 chip->amplifier_ctrl = cp->amp;
3854 chip->active_ctrl = cp->active;
3855 chip->mixer_init = cp->mixer_init;
3864 snd_printk(KERN_INFO "Crystal EAPD support forced on.\n");
3865 chip->amplifier_ctrl = amp_voyetra;
3869 snd_printk(KERN_INFO "Activating CLKRUN hack for Thinkpad.\n");
3870 chip->active_ctrl = clkrun_hack;
3874 if (chip->amplifier_ctrl == NULL)
3875 chip->amplifier_ctrl = amp_none;
3876 if (chip->active_ctrl == NULL)
3877 chip->active_ctrl = amp_none;
3879 chip->active_ctrl(chip, 1); /* enable CLKRUN */
3881 pci_set_master(pci);
3883 for (idx = 0; idx < 5; idx++) {
3884 region = &chip->region.idx[idx];
3885 if ((region->resource = request_mem_region(region->base, region->size,
3886 region->name)) == NULL) {
3887 snd_printk(KERN_ERR "unable to request memory region 0x%lx-0x%lx\n",
3888 region->base, region->base + region->size - 1);
3889 snd_cs46xx_free(chip);
3892 region->remap_addr = ioremap_nocache(region->base, region->size);
3893 if (region->remap_addr == NULL) {
3894 snd_printk(KERN_ERR "%s ioremap problem\n", region->name);
3895 snd_cs46xx_free(chip);
3900 if (request_irq(pci->irq, snd_cs46xx_interrupt, IRQF_SHARED,
3902 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
3903 snd_cs46xx_free(chip);
3906 chip->irq = pci->irq;
3908 #ifdef CONFIG_SND_CS46XX_NEW_DSP
3909 chip->dsp_spos_instance = cs46xx_dsp_spos_create(chip);
3910 if (chip->dsp_spos_instance == NULL) {
3911 snd_cs46xx_free(chip);
3916 err = snd_cs46xx_chip_init(chip);
3918 snd_cs46xx_free(chip);
3922 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3923 snd_cs46xx_free(chip);
3927 snd_cs46xx_proc_init(card, chip);
3930 chip->saved_regs = kmalloc(sizeof(*chip->saved_regs) *
3931 ARRAY_SIZE(saved_regs), GFP_KERNEL);
3932 if (!chip->saved_regs) {
3933 snd_cs46xx_free(chip);
3938 chip->active_ctrl(chip, -1); /* disable CLKRUN */
3940 snd_card_set_dev(card, &pci->dev);