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 <sound/driver.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/asoundef.h>
31 #include <sound/info.h>
33 #include <sound/vx_core.h>
36 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
37 MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
38 MODULE_LICENSE("GPL");
42 * snd_vx_delay - delay for the specified time
43 * @xmsec: the time to delay in msec
45 void snd_vx_delay(vx_core_t *chip, int xmsec)
47 if (! in_interrupt() && xmsec >= 1000 / HZ) {
48 set_current_state(TASK_UNINTERRUPTIBLE);
49 schedule_timeout((xmsec * HZ + 999) / 1000);
56 * vx_check_reg_bit - wait for the specified bit is set/reset on a register
57 * @reg: register to check
59 * @bit: resultant bit to be checked
60 * @time: time-out of loop in msec
62 * returns zero if a bit matches, or a negative error code.
64 int snd_vx_check_reg_bit(vx_core_t *chip, int reg, int mask, int bit, int time)
66 unsigned long end_time = jiffies + (time * HZ + 999) / 1000;
67 #ifdef CONFIG_SND_DEBUG
68 static char *reg_names[VX_REG_MAX] = {
69 "ICR", "CVR", "ISR", "IVR", "RXH", "RXM", "RXL",
70 "DMA", "CDSP", "RFREQ", "RUER/V2", "DATA", "MEMIRQ",
71 "ACQ", "BIT0", "BIT1", "MIC0", "MIC1", "MIC2",
72 "MIC3", "INTCSR", "CNTRL", "GPIOC",
73 "LOFREQ", "HIFREQ", "CSUER", "RUER"
77 if ((snd_vx_inb(chip, reg) & mask) == bit)
79 //snd_vx_delay(chip, 10);
80 } while (time_after_eq(end_time, jiffies));
81 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));
86 * vx_send_irq_dsp - set command irq bit
87 * @num: the requested IRQ type, IRQ_XXX
89 * this triggers the specified IRQ request
90 * returns 0 if successful, or a negative error code.
93 static int vx_send_irq_dsp(vx_core_t *chip, int num)
98 if (snd_vx_check_reg_bit(chip, VX_CVR, CVR_HC, 0, 200) < 0)
102 if (vx_has_new_dsp(chip))
103 nirq += VXP_IRQ_OFFSET;
104 vx_outb(chip, CVR, (nirq >> 1) | CVR_HC);
110 * vx_reset_chk - reset CHK bit on ISR
112 * returns 0 if successful, or a negative error code.
114 static int vx_reset_chk(vx_core_t *chip)
117 if (vx_send_irq_dsp(chip, IRQ_RESET_CHK) < 0)
119 /* Wait until CHK = 0 */
120 if (vx_check_isr(chip, ISR_CHK, 0, 200) < 0)
126 * vx_transfer_end - terminate message transfer
127 * @cmd: IRQ message to send (IRQ_MESS_XXX_END)
129 * returns 0 if successful, or a negative error code.
130 * the error code can be VX-specific, retrieved via vx_get_error().
131 * NB: call with spinlock held!
133 static int vx_transfer_end(vx_core_t *chip, int cmd)
137 if ((err = vx_reset_chk(chip)) < 0)
140 /* irq MESS_READ/WRITE_END */
141 if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
145 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
148 /* If error, Read RX */
149 if ((err = vx_inb(chip, ISR)) & ISR_ERR) {
150 if ((err = vx_wait_for_rx_full(chip)) < 0) {
151 snd_printd(KERN_DEBUG "transfer_end: error in rx_full\n");
154 err = vx_inb(chip, RXH) << 16;
155 err |= vx_inb(chip, RXM) << 8;
156 err |= vx_inb(chip, RXL);
157 snd_printd(KERN_DEBUG "transfer_end: error = 0x%x\n", err);
158 return -(VX_ERR_MASK | err);
164 * vx_read_status - return the status rmh
165 * @rmh: rmh record to store the status
167 * returns 0 if successful, or a negative error code.
168 * the error code can be VX-specific, retrieved via vx_get_error().
169 * NB: call with spinlock held!
171 static int vx_read_status(vx_core_t *chip, struct vx_rmh *rmh)
173 int i, err, val, size;
175 /* no read necessary? */
176 if (rmh->DspStat == RMH_SSIZE_FIXED && rmh->LgStat == 0)
179 /* Wait for RX full (with timeout protection)
180 * The first word of status is in RX
182 err = vx_wait_for_rx_full(chip);
187 val = vx_inb(chip, RXH) << 16;
188 val |= vx_inb(chip, RXM) << 8;
189 val |= vx_inb(chip, RXL);
191 /* If status given by DSP, let's decode its size */
192 switch (rmh->DspStat) {
195 rmh->Stat[0] = val & 0xffff00;
196 rmh->LgStat = size + 1;
199 /* Let's count the arg numbers from a mask */
207 rmh->LgStat = size + 1;
210 /* else retrieve the status length given by the driver */
212 rmh->Stat[0] = val; /* Val is the status 1st word */
213 size--; /* hence adjust remaining length */
219 snd_assert(size <= SIZE_MAX_STATUS, return -EINVAL);
221 for (i = 1; i <= size; i++) {
222 /* trigger an irq MESS_WRITE_NEXT */
223 err = vx_send_irq_dsp(chip, IRQ_MESS_WRITE_NEXT);
226 /* Wait for RX full (with timeout protection) */
227 err = vx_wait_for_rx_full(chip);
230 rmh->Stat[i] = vx_inb(chip, RXH) << 16;
231 rmh->Stat[i] |= vx_inb(chip, RXM) << 8;
232 rmh->Stat[i] |= vx_inb(chip, RXL);
235 return vx_transfer_end(chip, IRQ_MESS_WRITE_END);
239 #define MASK_MORE_THAN_1_WORD_COMMAND 0x00008000
240 #define MASK_1_WORD_COMMAND 0x00ff7fff
243 * vx_send_msg_nolock - send a DSP message and read back the status
244 * @rmh: the rmh record to send and receive
246 * returns 0 if successful, or a negative error code.
247 * the error code can be VX-specific, retrieved via vx_get_error().
249 * this function doesn't call spinlock at all.
251 int vx_send_msg_nolock(vx_core_t *chip, struct vx_rmh *rmh)
255 if (chip->chip_status & VX_STAT_IS_STALE)
258 if ((err = vx_reset_chk(chip)) < 0) {
259 snd_printd(KERN_DEBUG "vx_send_msg: vx_reset_chk error\n");
264 printk(KERN_DEBUG "rmh: cmd = 0x%06x, length = %d, stype = %d\n",
265 rmh->Cmd[0], rmh->LgCmd, rmh->DspStat);
266 if (rmh->LgCmd > 1) {
267 printk(KERN_DEBUG " ");
268 for (i = 1; i < rmh->LgCmd; i++)
269 printk("0x%06x ", rmh->Cmd[i]);
273 /* Check bit M is set according to length of the command */
275 rmh->Cmd[0] |= MASK_MORE_THAN_1_WORD_COMMAND;
277 rmh->Cmd[0] &= MASK_1_WORD_COMMAND;
279 /* Wait for TX empty */
280 if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
281 snd_printd(KERN_DEBUG "vx_send_msg: wait tx empty error\n");
286 vx_outb(chip, TXH, (rmh->Cmd[0] >> 16) & 0xff);
287 vx_outb(chip, TXM, (rmh->Cmd[0] >> 8) & 0xff);
288 vx_outb(chip, TXL, rmh->Cmd[0] & 0xff);
290 /* Trigger irq MESSAGE */
291 if ((err = vx_send_irq_dsp(chip, IRQ_MESSAGE)) < 0) {
292 snd_printd(KERN_DEBUG "vx_send_msg: send IRQ_MESSAGE error\n");
296 /* Wait for CHK = 1 */
297 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
300 /* If error, get error value from RX */
301 if (vx_inb(chip, ISR) & ISR_ERR) {
302 if ((err = vx_wait_for_rx_full(chip)) < 0) {
303 snd_printd(KERN_DEBUG "vx_send_msg: rx_full read error\n");
306 err = vx_inb(chip, RXH) << 16;
307 err |= vx_inb(chip, RXM) << 8;
308 err |= vx_inb(chip, RXL);
309 snd_printd(KERN_DEBUG "msg got error = 0x%x at cmd[0]\n", err);
310 err = -(VX_ERR_MASK | err);
314 /* Send the other words */
315 if (rmh->LgCmd > 1) {
316 for (i = 1; i < rmh->LgCmd; i++) {
317 /* Wait for TX ready */
318 if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
319 snd_printd(KERN_DEBUG "vx_send_msg: tx_ready error\n");
324 vx_outb(chip, TXH, (rmh->Cmd[i] >> 16) & 0xff);
325 vx_outb(chip, TXM, (rmh->Cmd[i] >> 8) & 0xff);
326 vx_outb(chip, TXL, rmh->Cmd[i] & 0xff);
328 /* Trigger irq MESS_READ_NEXT */
329 if ((err = vx_send_irq_dsp(chip, IRQ_MESS_READ_NEXT)) < 0) {
330 snd_printd(KERN_DEBUG "vx_send_msg: IRQ_READ_NEXT error\n");
334 /* Wait for TX empty */
335 if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
336 snd_printd(KERN_DEBUG "vx_send_msg: TX_READY error\n");
339 /* End of transfer */
340 err = vx_transfer_end(chip, IRQ_MESS_READ_END);
345 return vx_read_status(chip, rmh);
350 * vx_send_msg - send a DSP message with spinlock
351 * @rmh: the rmh record to send and receive
353 * returns 0 if successful, or a negative error code.
354 * see vx_send_msg_nolock().
356 int vx_send_msg(vx_core_t *chip, struct vx_rmh *rmh)
361 spin_lock_irqsave(&chip->lock, flags);
362 err = vx_send_msg_nolock(chip, rmh);
363 spin_unlock_irqrestore(&chip->lock, flags);
369 * vx_send_rih_nolock - send an RIH to xilinx
370 * @cmd: the command to send
372 * returns 0 if successful, or a negative error code.
373 * the error code can be VX-specific, retrieved via vx_get_error().
375 * this function doesn't call spinlock at all.
377 * unlike RMH, no command is sent to DSP.
379 int vx_send_rih_nolock(vx_core_t *chip, int cmd)
383 if (chip->chip_status & VX_STAT_IS_STALE)
387 printk(KERN_DEBUG "send_rih: cmd = 0x%x\n", cmd);
389 if ((err = vx_reset_chk(chip)) < 0)
392 if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
395 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
397 /* If error, read RX */
398 if (vx_inb(chip, ISR) & ISR_ERR) {
399 if ((err = vx_wait_for_rx_full(chip)) < 0)
401 err = vx_inb(chip, RXH) << 16;
402 err |= vx_inb(chip, RXM) << 8;
403 err |= vx_inb(chip, RXL);
404 return -(VX_ERR_MASK | err);
411 * vx_send_rih - send an RIH with spinlock
412 * @cmd: the command to send
414 * see vx_send_rih_nolock().
416 int vx_send_rih(vx_core_t *chip, int cmd)
421 spin_lock_irqsave(&chip->lock, flags);
422 err = vx_send_rih_nolock(chip, cmd);
423 spin_unlock_irqrestore(&chip->lock, flags);
427 #define END_OF_RESET_WAIT_TIME 500 /* us */
430 * snd_vx_boot_xilinx - boot up the xilinx interface
431 * @boot: the boot record to load
433 int snd_vx_load_boot_image(vx_core_t *chip, const snd_hwdep_dsp_image_t *boot)
436 int no_fillup = vx_has_new_dsp(chip);
438 /* check the length of boot image */
439 snd_assert(boot->length > 0, return -EINVAL);
440 snd_assert(boot->length % 3 == 0, return -EINVAL);
441 snd_assert(boot->image, return -EINVAL);
444 /* more strict check */
445 unsigned int c = ((u32)boot->image[0] << 16) | ((u32)boot->image[1] << 8) | boot->image[2];
446 snd_assert(boot->length == (c + 2) * 3, return -EINVAL);
453 udelay(END_OF_RESET_WAIT_TIME); /* another wait? */
455 /* download boot strap */
456 for (i = 0; i < 0x600; i += 3) {
457 if (i >= boot->length) {
460 if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
461 snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
464 vx_outb(chip, TXH, 0);
465 vx_outb(chip, TXM, 0);
466 vx_outb(chip, TXL, 0);
468 unsigned char image[3];
469 if (copy_from_user(image, boot->image + i, 3))
471 if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
472 snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
475 vx_outb(chip, TXH, image[0]);
476 vx_outb(chip, TXM, image[1]);
477 vx_outb(chip, TXL, image[2]);
484 * vx_test_irq_src - query the source of interrupts
486 * called from irq handler only
488 static int vx_test_irq_src(vx_core_t *chip, unsigned int *ret)
492 vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
493 spin_lock(&chip->lock);
494 err = vx_send_msg_nolock(chip, &chip->irq_rmh);
498 *ret = chip->irq_rmh.Stat[0];
499 spin_unlock(&chip->lock);
505 * vx_interrupt - soft irq handler
507 static void vx_interrupt(unsigned long private_data)
509 vx_core_t *chip = (vx_core_t *) private_data;
512 if (chip->chip_status & VX_STAT_IS_STALE)
515 if (vx_test_irq_src(chip, &events) < 0)
519 if (events & 0x000800)
520 printk(KERN_ERR "DSP Stream underrun ! IRQ events = 0x%x\n", events);
522 // printk(KERN_DEBUG "IRQ events = 0x%x\n", events);
524 /* We must prevent any application using this DSP
525 * and block any further request until the application
526 * either unregisters or reloads the DSP
528 if (events & FATAL_DSP_ERROR) {
529 snd_printk(KERN_ERR "vx_core: fatal DSP error!!\n");
533 /* The start on time code conditions are filled (ie the time code
534 * received by the board is equal to one of those given to it).
536 if (events & TIME_CODE_EVENT_PENDING)
537 ; /* so far, nothing to do yet */
539 /* The frequency has changed on the board (UER mode). */
540 if (events & FREQUENCY_CHANGE_EVENT_PENDING)
541 vx_change_frequency(chip);
543 /* update the pcm streams */
544 vx_pcm_update_intr(chip, events);
549 * snd_vx_irq_handler - interrupt handler
551 irqreturn_t snd_vx_irq_handler(int irq, void *dev, struct pt_regs *regs)
553 vx_core_t *chip = dev;
555 if (! (chip->chip_status & VX_STAT_CHIP_INIT) ||
556 (chip->chip_status & VX_STAT_IS_STALE))
558 if (! vx_test_and_ack(chip))
559 tasklet_hi_schedule(&chip->tq);
566 static void vx_reset_board(vx_core_t *chip, int cold_reset)
568 snd_assert(chip->ops->reset_board, return);
570 /* current source, later sync'ed with target */
571 chip->audio_source = VX_AUDIO_SRC_LINE;
573 chip->audio_source_target = chip->audio_source;
574 chip->clock_source = INTERNAL_QUARTZ;
575 chip->clock_mode = VX_CLOCK_MODE_AUTO;
577 chip->uer_detected = VX_UER_MODE_NOT_PRESENT;
578 chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
581 chip->ops->reset_board(chip, cold_reset);
583 vx_reset_codec(chip, cold_reset);
585 vx_set_internal_clock(chip, chip->freq);
590 if (vx_is_pcmcia(chip)) {
591 /* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
592 vx_test_and_ack(chip);
593 vx_validate_irq(chip, 1);
597 vx_set_iec958_status(chip, chip->uer_bits);
605 static void vx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
607 vx_core_t *chip = entry->private_data;
608 static char *audio_src_vxp[] = { "Line", "Mic", "Digital" };
609 static char *audio_src_vx2[] = { "Analog", "Analog", "Digital" };
610 static char *clock_mode[] = { "Auto", "Internal", "External" };
611 static char *clock_src[] = { "Internal", "External" };
612 static char *uer_type[] = { "Consumer", "Professional", "Not Present" };
614 snd_iprintf(buffer, "%s\n", chip->card->longname);
615 snd_iprintf(buffer, "DSP audio info:");
616 if (chip->audio_info & VX_AUDIO_INFO_REAL_TIME)
617 snd_iprintf(buffer, " realtime");
618 if (chip->audio_info & VX_AUDIO_INFO_OFFLINE)
619 snd_iprintf(buffer, " offline");
620 if (chip->audio_info & VX_AUDIO_INFO_MPEG1)
621 snd_iprintf(buffer, " mpeg1");
622 if (chip->audio_info & VX_AUDIO_INFO_MPEG2)
623 snd_iprintf(buffer, " mpeg2");
624 if (chip->audio_info & VX_AUDIO_INFO_LINEAR_8)
625 snd_iprintf(buffer, " linear8");
626 if (chip->audio_info & VX_AUDIO_INFO_LINEAR_16)
627 snd_iprintf(buffer, " linear16");
628 if (chip->audio_info & VX_AUDIO_INFO_LINEAR_24)
629 snd_iprintf(buffer, " linear24");
630 snd_iprintf(buffer, "\n");
631 snd_iprintf(buffer, "Input Source: %s\n", vx_is_pcmcia(chip) ?
632 audio_src_vxp[chip->audio_source] :
633 audio_src_vx2[chip->audio_source]);
634 snd_iprintf(buffer, "Clock Mode: %s\n", clock_mode[chip->clock_mode]);
635 snd_iprintf(buffer, "Clock Source: %s\n", clock_src[chip->clock_source]);
636 snd_iprintf(buffer, "Frequency: %d\n", chip->freq);
637 snd_iprintf(buffer, "Detected Frequency: %d\n", chip->freq_detected);
638 snd_iprintf(buffer, "Detected UER type: %s\n", uer_type[chip->uer_detected]);
639 snd_iprintf(buffer, "Min/Max/Cur IBL: %d/%d/%d (granularity=%d)\n",
640 chip->ibl.min_size, chip->ibl.max_size, chip->ibl.size,
641 chip->ibl.granularity);
644 static void vx_proc_init(vx_core_t *chip)
646 snd_info_entry_t *entry;
648 if (! snd_card_proc_new(chip->card, "vx-status", &entry))
649 snd_info_set_text_ops(entry, chip, 1024, vx_proc_read);
654 * snd_vx_dsp_boot - load the DSP boot
656 int snd_vx_dsp_boot(vx_core_t *chip, const snd_hwdep_dsp_image_t *boot)
659 int cold_reset = !(chip->chip_status & VX_STAT_DEVICE_INIT);
661 vx_reset_board(chip, cold_reset);
662 vx_validate_irq(chip, 0);
664 if ((err = snd_vx_load_boot_image(chip, boot)) < 0)
666 snd_vx_delay(chip, 10);
672 * snd_vx_dsp_load - load the DSP image
674 int snd_vx_dsp_load(vx_core_t *chip, const snd_hwdep_dsp_image_t *dsp)
678 unsigned int csum = 0;
679 unsigned char image[3], *cptr;
681 snd_assert(dsp->length % 3 == 0, return -EINVAL);
683 vx_toggle_dac_mute(chip, 1);
685 /* Transfert data buffer from PC to DSP */
686 for (i = 0; i < dsp->length; i += 3) {
687 if (copy_from_user(image, dsp->image + i, 3))
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);
707 snd_vx_delay(chip, 200);
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);
721 * snd_vx_create - constructor for vx_core_t
722 * @hw: hardware specific record
724 * this function allocates the instance and prepare for the hardware
727 * return the instance pointer if successful, NULL in error.
729 vx_core_t *snd_vx_create(snd_card_t *card, struct snd_vx_hardware *hw,
730 struct snd_vx_ops *ops,
735 snd_assert(card && hw && ops, return NULL);
737 chip = kcalloc(1, sizeof(chip) + extra_size, GFP_KERNEL);
739 snd_printk(KERN_ERR "vx_core: no memory\n");
742 spin_lock_init(&chip->lock);
743 spin_lock_init(&chip->irq_lock);
746 chip->type = hw->type;
748 tasklet_init(&chip->tq, vx_interrupt, (unsigned long)chip);
749 init_MUTEX(&chip->mixer_mutex);
752 card->private_data = chip;
753 strcpy(card->driver, hw->name);
754 sprintf(card->shortname, "Digigram %s", hw->name);
765 void snd_vx_suspend(vx_core_t *chip)
769 chip->chip_status |= VX_STAT_IN_SUSPEND;
770 for (i = 0; i < chip->hw->num_codecs; i++)
771 snd_pcm_suspend_all(chip->pcm[i]);
773 chip->hwdep->dsp_loaded = 0;
779 void snd_vx_resume(vx_core_t *chip)
781 /* clear all stuff... */
782 chip->chip_status &= ~(VX_STAT_IN_SUSPEND|VX_STAT_CHIP_INIT);
790 static int __init alsa_vx_core_init(void)
795 static void __exit alsa_vx_core_exit(void)
799 module_init(alsa_vx_core_init)
800 module_exit(alsa_vx_core_exit)
805 EXPORT_SYMBOL(snd_vx_check_reg_bit);
806 EXPORT_SYMBOL(snd_vx_create);
807 EXPORT_SYMBOL(snd_vx_hwdep_new);
808 EXPORT_SYMBOL(snd_vx_irq_handler);
809 EXPORT_SYMBOL(snd_vx_delay);
810 EXPORT_SYMBOL(snd_vx_dsp_boot);
811 EXPORT_SYMBOL(snd_vx_dsp_load);
812 EXPORT_SYMBOL(snd_vx_load_boot_image);
814 EXPORT_SYMBOL(snd_vx_suspend);
815 EXPORT_SYMBOL(snd_vx_resume);