2 * Driver for Digigram VX soundcards
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/device.h>
28 #include <linux/firmware.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/asoundef.h>
32 #include <sound/info.h>
34 #include <sound/vx_core.h>
37 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
38 MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
39 MODULE_LICENSE("GPL");
43 * vx_check_reg_bit - wait for the specified bit is set/reset on a register
44 * @reg: register to check
46 * @bit: resultant bit to be checked
47 * @time: time-out of loop in msec
49 * returns zero if a bit matches, or a negative error code.
51 int snd_vx_check_reg_bit(struct vx_core *chip, int reg, int mask, int bit, int time)
53 unsigned long end_time = jiffies + (time * HZ + 999) / 1000;
54 #ifdef CONFIG_SND_DEBUG
55 static char *reg_names[VX_REG_MAX] = {
56 "ICR", "CVR", "ISR", "IVR", "RXH", "RXM", "RXL",
57 "DMA", "CDSP", "RFREQ", "RUER/V2", "DATA", "MEMIRQ",
58 "ACQ", "BIT0", "BIT1", "MIC0", "MIC1", "MIC2",
59 "MIC3", "INTCSR", "CNTRL", "GPIOC",
60 "LOFREQ", "HIFREQ", "CSUER", "RUER"
64 if ((snd_vx_inb(chip, reg) & mask) == bit)
67 } while (time_after_eq(end_time, jiffies));
68 snd_printd(KERN_DEBUG "vx_check_reg_bit: timeout, reg=%s, mask=0x%x, val=0x%x\n", reg_names[reg], mask, snd_vx_inb(chip, reg));
72 EXPORT_SYMBOL(snd_vx_check_reg_bit);
75 * vx_send_irq_dsp - set command irq bit
76 * @num: the requested IRQ type, IRQ_XXX
78 * this triggers the specified IRQ request
79 * returns 0 if successful, or a negative error code.
82 static int vx_send_irq_dsp(struct vx_core *chip, int num)
87 if (snd_vx_check_reg_bit(chip, VX_CVR, CVR_HC, 0, 200) < 0)
91 if (vx_has_new_dsp(chip))
92 nirq += VXP_IRQ_OFFSET;
93 vx_outb(chip, CVR, (nirq >> 1) | CVR_HC);
99 * vx_reset_chk - reset CHK bit on ISR
101 * returns 0 if successful, or a negative error code.
103 static int vx_reset_chk(struct vx_core *chip)
106 if (vx_send_irq_dsp(chip, IRQ_RESET_CHK) < 0)
108 /* Wait until CHK = 0 */
109 if (vx_check_isr(chip, ISR_CHK, 0, 200) < 0)
115 * vx_transfer_end - terminate message transfer
116 * @cmd: IRQ message to send (IRQ_MESS_XXX_END)
118 * returns 0 if successful, or a negative error code.
119 * the error code can be VX-specific, retrieved via vx_get_error().
120 * NB: call with spinlock held!
122 static int vx_transfer_end(struct vx_core *chip, int cmd)
126 if ((err = vx_reset_chk(chip)) < 0)
129 /* irq MESS_READ/WRITE_END */
130 if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
134 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
137 /* If error, Read RX */
138 if ((err = vx_inb(chip, ISR)) & ISR_ERR) {
139 if ((err = vx_wait_for_rx_full(chip)) < 0) {
140 snd_printd(KERN_DEBUG "transfer_end: error in rx_full\n");
143 err = vx_inb(chip, RXH) << 16;
144 err |= vx_inb(chip, RXM) << 8;
145 err |= vx_inb(chip, RXL);
146 snd_printd(KERN_DEBUG "transfer_end: error = 0x%x\n", err);
147 return -(VX_ERR_MASK | err);
153 * vx_read_status - return the status rmh
154 * @rmh: rmh record to store the status
156 * returns 0 if successful, or a negative error code.
157 * the error code can be VX-specific, retrieved via vx_get_error().
158 * NB: call with spinlock held!
160 static int vx_read_status(struct vx_core *chip, struct vx_rmh *rmh)
162 int i, err, val, size;
164 /* no read necessary? */
165 if (rmh->DspStat == RMH_SSIZE_FIXED && rmh->LgStat == 0)
168 /* Wait for RX full (with timeout protection)
169 * The first word of status is in RX
171 err = vx_wait_for_rx_full(chip);
176 val = vx_inb(chip, RXH) << 16;
177 val |= vx_inb(chip, RXM) << 8;
178 val |= vx_inb(chip, RXL);
180 /* If status given by DSP, let's decode its size */
181 switch (rmh->DspStat) {
184 rmh->Stat[0] = val & 0xffff00;
185 rmh->LgStat = size + 1;
188 /* Let's count the arg numbers from a mask */
196 rmh->LgStat = size + 1;
199 /* else retrieve the status length given by the driver */
201 rmh->Stat[0] = val; /* Val is the status 1st word */
202 size--; /* hence adjust remaining length */
208 if (snd_BUG_ON(size > SIZE_MAX_STATUS))
211 for (i = 1; i <= size; i++) {
212 /* trigger an irq MESS_WRITE_NEXT */
213 err = vx_send_irq_dsp(chip, IRQ_MESS_WRITE_NEXT);
216 /* Wait for RX full (with timeout protection) */
217 err = vx_wait_for_rx_full(chip);
220 rmh->Stat[i] = vx_inb(chip, RXH) << 16;
221 rmh->Stat[i] |= vx_inb(chip, RXM) << 8;
222 rmh->Stat[i] |= vx_inb(chip, RXL);
225 return vx_transfer_end(chip, IRQ_MESS_WRITE_END);
229 #define MASK_MORE_THAN_1_WORD_COMMAND 0x00008000
230 #define MASK_1_WORD_COMMAND 0x00ff7fff
233 * vx_send_msg_nolock - send a DSP message and read back the status
234 * @rmh: the rmh record to send and receive
236 * returns 0 if successful, or a negative error code.
237 * the error code can be VX-specific, retrieved via vx_get_error().
239 * this function doesn't call spinlock at all.
241 int vx_send_msg_nolock(struct vx_core *chip, struct vx_rmh *rmh)
245 if (chip->chip_status & VX_STAT_IS_STALE)
248 if ((err = vx_reset_chk(chip)) < 0) {
249 snd_printd(KERN_DEBUG "vx_send_msg: vx_reset_chk error\n");
254 printk(KERN_DEBUG "rmh: cmd = 0x%06x, length = %d, stype = %d\n",
255 rmh->Cmd[0], rmh->LgCmd, rmh->DspStat);
256 if (rmh->LgCmd > 1) {
257 printk(KERN_DEBUG " ");
258 for (i = 1; i < rmh->LgCmd; i++)
259 printk("0x%06x ", rmh->Cmd[i]);
263 /* Check bit M is set according to length of the command */
265 rmh->Cmd[0] |= MASK_MORE_THAN_1_WORD_COMMAND;
267 rmh->Cmd[0] &= MASK_1_WORD_COMMAND;
269 /* Wait for TX empty */
270 if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
271 snd_printd(KERN_DEBUG "vx_send_msg: wait tx empty error\n");
276 vx_outb(chip, TXH, (rmh->Cmd[0] >> 16) & 0xff);
277 vx_outb(chip, TXM, (rmh->Cmd[0] >> 8) & 0xff);
278 vx_outb(chip, TXL, rmh->Cmd[0] & 0xff);
280 /* Trigger irq MESSAGE */
281 if ((err = vx_send_irq_dsp(chip, IRQ_MESSAGE)) < 0) {
282 snd_printd(KERN_DEBUG "vx_send_msg: send IRQ_MESSAGE error\n");
286 /* Wait for CHK = 1 */
287 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
290 /* If error, get error value from RX */
291 if (vx_inb(chip, ISR) & ISR_ERR) {
292 if ((err = vx_wait_for_rx_full(chip)) < 0) {
293 snd_printd(KERN_DEBUG "vx_send_msg: rx_full read error\n");
296 err = vx_inb(chip, RXH) << 16;
297 err |= vx_inb(chip, RXM) << 8;
298 err |= vx_inb(chip, RXL);
299 snd_printd(KERN_DEBUG "msg got error = 0x%x at cmd[0]\n", err);
300 err = -(VX_ERR_MASK | err);
304 /* Send the other words */
305 if (rmh->LgCmd > 1) {
306 for (i = 1; i < rmh->LgCmd; i++) {
307 /* Wait for TX ready */
308 if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
309 snd_printd(KERN_DEBUG "vx_send_msg: tx_ready error\n");
314 vx_outb(chip, TXH, (rmh->Cmd[i] >> 16) & 0xff);
315 vx_outb(chip, TXM, (rmh->Cmd[i] >> 8) & 0xff);
316 vx_outb(chip, TXL, rmh->Cmd[i] & 0xff);
318 /* Trigger irq MESS_READ_NEXT */
319 if ((err = vx_send_irq_dsp(chip, IRQ_MESS_READ_NEXT)) < 0) {
320 snd_printd(KERN_DEBUG "vx_send_msg: IRQ_READ_NEXT error\n");
324 /* Wait for TX empty */
325 if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
326 snd_printd(KERN_DEBUG "vx_send_msg: TX_READY error\n");
329 /* End of transfer */
330 err = vx_transfer_end(chip, IRQ_MESS_READ_END);
335 return vx_read_status(chip, rmh);
340 * vx_send_msg - send a DSP message with spinlock
341 * @rmh: the rmh record to send and receive
343 * returns 0 if successful, or a negative error code.
344 * see vx_send_msg_nolock().
346 int vx_send_msg(struct vx_core *chip, struct vx_rmh *rmh)
351 spin_lock_irqsave(&chip->lock, flags);
352 err = vx_send_msg_nolock(chip, rmh);
353 spin_unlock_irqrestore(&chip->lock, flags);
359 * vx_send_rih_nolock - send an RIH to xilinx
360 * @cmd: the command to send
362 * returns 0 if successful, or a negative error code.
363 * the error code can be VX-specific, retrieved via vx_get_error().
365 * this function doesn't call spinlock at all.
367 * unlike RMH, no command is sent to DSP.
369 int vx_send_rih_nolock(struct vx_core *chip, int cmd)
373 if (chip->chip_status & VX_STAT_IS_STALE)
377 printk(KERN_DEBUG "send_rih: cmd = 0x%x\n", cmd);
379 if ((err = vx_reset_chk(chip)) < 0)
382 if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
385 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
387 /* If error, read RX */
388 if (vx_inb(chip, ISR) & ISR_ERR) {
389 if ((err = vx_wait_for_rx_full(chip)) < 0)
391 err = vx_inb(chip, RXH) << 16;
392 err |= vx_inb(chip, RXM) << 8;
393 err |= vx_inb(chip, RXL);
394 return -(VX_ERR_MASK | err);
401 * vx_send_rih - send an RIH with spinlock
402 * @cmd: the command to send
404 * see vx_send_rih_nolock().
406 int vx_send_rih(struct vx_core *chip, int cmd)
411 spin_lock_irqsave(&chip->lock, flags);
412 err = vx_send_rih_nolock(chip, cmd);
413 spin_unlock_irqrestore(&chip->lock, flags);
417 #define END_OF_RESET_WAIT_TIME 500 /* us */
420 * snd_vx_boot_xilinx - boot up the xilinx interface
421 * @boot: the boot record to load
423 int snd_vx_load_boot_image(struct vx_core *chip, const struct firmware *boot)
426 int no_fillup = vx_has_new_dsp(chip);
428 /* check the length of boot image */
435 /* more strict check */
436 unsigned int c = ((u32)boot->data[0] << 16) | ((u32)boot->data[1] << 8) | boot->data[2];
437 if (boot->size != (c + 2) * 3)
445 udelay(END_OF_RESET_WAIT_TIME); /* another wait? */
447 /* download boot strap */
448 for (i = 0; i < 0x600; i += 3) {
449 if (i >= boot->size) {
452 if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
453 snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
456 vx_outb(chip, TXH, 0);
457 vx_outb(chip, TXM, 0);
458 vx_outb(chip, TXL, 0);
460 const unsigned char *image = boot->data + i;
461 if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
462 snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
465 vx_outb(chip, TXH, image[0]);
466 vx_outb(chip, TXM, image[1]);
467 vx_outb(chip, TXL, image[2]);
473 EXPORT_SYMBOL(snd_vx_load_boot_image);
476 * vx_test_irq_src - query the source of interrupts
478 * called from irq handler only
480 static int vx_test_irq_src(struct vx_core *chip, unsigned int *ret)
484 vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
485 spin_lock(&chip->lock);
486 err = vx_send_msg_nolock(chip, &chip->irq_rmh);
490 *ret = chip->irq_rmh.Stat[0];
491 spin_unlock(&chip->lock);
497 * vx_interrupt - soft irq handler
499 static void vx_interrupt(unsigned long private_data)
501 struct vx_core *chip = (struct vx_core *) private_data;
504 if (chip->chip_status & VX_STAT_IS_STALE)
507 if (vx_test_irq_src(chip, &events) < 0)
511 if (events & 0x000800)
512 printk(KERN_ERR "DSP Stream underrun ! IRQ events = 0x%x\n", events);
514 // printk(KERN_DEBUG "IRQ events = 0x%x\n", events);
516 /* We must prevent any application using this DSP
517 * and block any further request until the application
518 * either unregisters or reloads the DSP
520 if (events & FATAL_DSP_ERROR) {
521 snd_printk(KERN_ERR "vx_core: fatal DSP error!!\n");
525 /* The start on time code conditions are filled (ie the time code
526 * received by the board is equal to one of those given to it).
528 if (events & TIME_CODE_EVENT_PENDING)
529 ; /* so far, nothing to do yet */
531 /* The frequency has changed on the board (UER mode). */
532 if (events & FREQUENCY_CHANGE_EVENT_PENDING)
533 vx_change_frequency(chip);
535 /* update the pcm streams */
536 vx_pcm_update_intr(chip, events);
541 * snd_vx_irq_handler - interrupt handler
543 irqreturn_t snd_vx_irq_handler(int irq, void *dev)
545 struct vx_core *chip = dev;
547 if (! (chip->chip_status & VX_STAT_CHIP_INIT) ||
548 (chip->chip_status & VX_STAT_IS_STALE))
550 if (! vx_test_and_ack(chip))
551 tasklet_hi_schedule(&chip->tq);
555 EXPORT_SYMBOL(snd_vx_irq_handler);
559 static void vx_reset_board(struct vx_core *chip, int cold_reset)
561 if (snd_BUG_ON(!chip->ops->reset_board))
564 /* current source, later sync'ed with target */
565 chip->audio_source = VX_AUDIO_SRC_LINE;
567 chip->audio_source_target = chip->audio_source;
568 chip->clock_source = INTERNAL_QUARTZ;
569 chip->clock_mode = VX_CLOCK_MODE_AUTO;
571 chip->uer_detected = VX_UER_MODE_NOT_PRESENT;
572 chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
575 chip->ops->reset_board(chip, cold_reset);
577 vx_reset_codec(chip, cold_reset);
579 vx_set_internal_clock(chip, chip->freq);
584 if (vx_is_pcmcia(chip)) {
585 /* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
586 vx_test_and_ack(chip);
587 vx_validate_irq(chip, 1);
591 vx_set_iec958_status(chip, chip->uer_bits);
599 static void vx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
601 struct vx_core *chip = entry->private_data;
602 static char *audio_src_vxp[] = { "Line", "Mic", "Digital" };
603 static char *audio_src_vx2[] = { "Analog", "Analog", "Digital" };
604 static char *clock_mode[] = { "Auto", "Internal", "External" };
605 static char *clock_src[] = { "Internal", "External" };
606 static char *uer_type[] = { "Consumer", "Professional", "Not Present" };
608 snd_iprintf(buffer, "%s\n", chip->card->longname);
609 snd_iprintf(buffer, "Xilinx Firmware: %s\n",
610 chip->chip_status & VX_STAT_XILINX_LOADED ? "Loaded" : "No");
611 snd_iprintf(buffer, "Device Initialized: %s\n",
612 chip->chip_status & VX_STAT_DEVICE_INIT ? "Yes" : "No");
613 snd_iprintf(buffer, "DSP audio info:");
614 if (chip->audio_info & VX_AUDIO_INFO_REAL_TIME)
615 snd_iprintf(buffer, " realtime");
616 if (chip->audio_info & VX_AUDIO_INFO_OFFLINE)
617 snd_iprintf(buffer, " offline");
618 if (chip->audio_info & VX_AUDIO_INFO_MPEG1)
619 snd_iprintf(buffer, " mpeg1");
620 if (chip->audio_info & VX_AUDIO_INFO_MPEG2)
621 snd_iprintf(buffer, " mpeg2");
622 if (chip->audio_info & VX_AUDIO_INFO_LINEAR_8)
623 snd_iprintf(buffer, " linear8");
624 if (chip->audio_info & VX_AUDIO_INFO_LINEAR_16)
625 snd_iprintf(buffer, " linear16");
626 if (chip->audio_info & VX_AUDIO_INFO_LINEAR_24)
627 snd_iprintf(buffer, " linear24");
628 snd_iprintf(buffer, "\n");
629 snd_iprintf(buffer, "Input Source: %s\n", vx_is_pcmcia(chip) ?
630 audio_src_vxp[chip->audio_source] :
631 audio_src_vx2[chip->audio_source]);
632 snd_iprintf(buffer, "Clock Mode: %s\n", clock_mode[chip->clock_mode]);
633 snd_iprintf(buffer, "Clock Source: %s\n", clock_src[chip->clock_source]);
634 snd_iprintf(buffer, "Frequency: %d\n", chip->freq);
635 snd_iprintf(buffer, "Detected Frequency: %d\n", chip->freq_detected);
636 snd_iprintf(buffer, "Detected UER type: %s\n", uer_type[chip->uer_detected]);
637 snd_iprintf(buffer, "Min/Max/Cur IBL: %d/%d/%d (granularity=%d)\n",
638 chip->ibl.min_size, chip->ibl.max_size, chip->ibl.size,
639 chip->ibl.granularity);
642 static void vx_proc_init(struct vx_core *chip)
644 struct snd_info_entry *entry;
646 if (! snd_card_proc_new(chip->card, "vx-status", &entry))
647 snd_info_set_text_ops(entry, chip, vx_proc_read);
652 * snd_vx_dsp_boot - load the DSP boot
654 int snd_vx_dsp_boot(struct vx_core *chip, const struct firmware *boot)
657 int cold_reset = !(chip->chip_status & VX_STAT_DEVICE_INIT);
659 vx_reset_board(chip, cold_reset);
660 vx_validate_irq(chip, 0);
662 if ((err = snd_vx_load_boot_image(chip, boot)) < 0)
669 EXPORT_SYMBOL(snd_vx_dsp_boot);
672 * snd_vx_dsp_load - load the DSP image
674 int snd_vx_dsp_load(struct vx_core *chip, const struct firmware *dsp)
678 unsigned int csum = 0;
679 const unsigned char *image, *cptr;
684 vx_toggle_dac_mute(chip, 1);
686 /* Transfert data buffer from PC to DSP */
687 for (i = 0; i < dsp->size; i += 3) {
688 image = dsp->data + i;
689 /* Wait DSP ready for a new read */
690 if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
691 printk("dsp loading error at position %d\n", i);
696 csum = (csum >> 24) | (csum << 8);
697 vx_outb(chip, TXH, *cptr++);
699 csum = (csum >> 24) | (csum << 8);
700 vx_outb(chip, TXM, *cptr++);
702 csum = (csum >> 24) | (csum << 8);
703 vx_outb(chip, TXL, *cptr++);
705 snd_printdd(KERN_DEBUG "checksum = 0x%08x\n", csum);
709 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
712 vx_toggle_dac_mute(chip, 0);
714 vx_test_and_ack(chip);
715 vx_validate_irq(chip, 1);
720 EXPORT_SYMBOL(snd_vx_dsp_load);
726 int snd_vx_suspend(struct vx_core *chip, pm_message_t state)
730 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
731 chip->chip_status |= VX_STAT_IN_SUSPEND;
732 for (i = 0; i < chip->hw->num_codecs; i++)
733 snd_pcm_suspend_all(chip->pcm[i]);
738 EXPORT_SYMBOL(snd_vx_suspend);
743 int snd_vx_resume(struct vx_core *chip)
747 chip->chip_status &= ~VX_STAT_CHIP_INIT;
749 for (i = 0; i < 4; i++) {
750 if (! chip->firmware[i])
752 err = chip->ops->load_dsp(chip, i, chip->firmware[i]);
754 snd_printk(KERN_ERR "vx: firmware resume error at DSP %d\n", i);
759 chip->chip_status |= VX_STAT_CHIP_INIT;
760 chip->chip_status &= ~VX_STAT_IN_SUSPEND;
762 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
766 EXPORT_SYMBOL(snd_vx_resume);
770 * snd_vx_create - constructor for struct vx_core
771 * @hw: hardware specific record
773 * this function allocates the instance and prepare for the hardware
776 * return the instance pointer if successful, NULL in error.
778 struct vx_core *snd_vx_create(struct snd_card *card, struct snd_vx_hardware *hw,
779 struct snd_vx_ops *ops,
782 struct vx_core *chip;
784 if (snd_BUG_ON(!card || !hw || !ops))
787 chip = kzalloc(sizeof(*chip) + extra_size, GFP_KERNEL);
789 snd_printk(KERN_ERR "vx_core: no memory\n");
792 spin_lock_init(&chip->lock);
793 spin_lock_init(&chip->irq_lock);
796 chip->type = hw->type;
798 tasklet_init(&chip->tq, vx_interrupt, (unsigned long)chip);
799 mutex_init(&chip->mixer_mutex);
802 card->private_data = chip;
803 strcpy(card->driver, hw->name);
804 sprintf(card->shortname, "Digigram %s", hw->name);
811 EXPORT_SYMBOL(snd_vx_create);
816 static int __init alsa_vx_core_init(void)
821 static void __exit alsa_vx_core_exit(void)
825 module_init(alsa_vx_core_init)
826 module_exit(alsa_vx_core_exit)