xref: /openbmc/linux/sound/drivers/vx/vx_core.c (revision db0a5214)
1 /*
2  * Driver for Digigram VX soundcards
3  *
4  * Hardware core part
5  *
6  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7  *
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.
12  *
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.
17  *
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
21  */
22 
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 <linux/module.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/asoundef.h>
33 #include <sound/info.h>
34 #include <asm/io.h>
35 #include <sound/vx_core.h>
36 #include "vx_cmd.h"
37 
38 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
39 MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
40 MODULE_LICENSE("GPL");
41 
42 
43 /*
44  * vx_check_reg_bit - wait for the specified bit is set/reset on a register
45  * @reg: register to check
46  * @mask: bit mask
47  * @bit: resultant bit to be checked
48  * @time: time-out of loop in msec
49  *
50  * returns zero if a bit matches, or a negative error code.
51  */
52 int snd_vx_check_reg_bit(struct vx_core *chip, int reg, int mask, int bit, int time)
53 {
54 	unsigned long end_time = jiffies + (time * HZ + 999) / 1000;
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"
61 	};
62 
63 	do {
64 		if ((snd_vx_inb(chip, reg) & mask) == bit)
65 			return 0;
66 		//msleep(10);
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));
69 	return -EIO;
70 }
71 
72 EXPORT_SYMBOL(snd_vx_check_reg_bit);
73 
74 /*
75  * vx_send_irq_dsp - set command irq bit
76  * @num: the requested IRQ type, IRQ_XXX
77  *
78  * this triggers the specified IRQ request
79  * returns 0 if successful, or a negative error code.
80  *
81  */
82 static int vx_send_irq_dsp(struct vx_core *chip, int num)
83 {
84 	int nirq;
85 
86 	/* wait for Hc = 0 */
87 	if (snd_vx_check_reg_bit(chip, VX_CVR, CVR_HC, 0, 200) < 0)
88 		return -EIO;
89 
90 	nirq = num;
91 	if (vx_has_new_dsp(chip))
92 		nirq += VXP_IRQ_OFFSET;
93 	vx_outb(chip, CVR, (nirq >> 1) | CVR_HC);
94 	return 0;
95 }
96 
97 
98 /*
99  * vx_reset_chk - reset CHK bit on ISR
100  *
101  * returns 0 if successful, or a negative error code.
102  */
103 static int vx_reset_chk(struct vx_core *chip)
104 {
105 	/* Reset irq CHK */
106 	if (vx_send_irq_dsp(chip, IRQ_RESET_CHK) < 0)
107 		return -EIO;
108 	/* Wait until CHK = 0 */
109 	if (vx_check_isr(chip, ISR_CHK, 0, 200) < 0)
110 		return -EIO;
111 	return 0;
112 }
113 
114 /*
115  * vx_transfer_end - terminate message transfer
116  * @cmd: IRQ message to send (IRQ_MESS_XXX_END)
117  *
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 mutex held!
121  */
122 static int vx_transfer_end(struct vx_core *chip, int cmd)
123 {
124 	int err;
125 
126 	if ((err = vx_reset_chk(chip)) < 0)
127 		return err;
128 
129 	/* irq MESS_READ/WRITE_END */
130 	if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
131 		return err;
132 
133 	/* Wait CHK = 1 */
134 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
135 		return err;
136 
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");
141 			return err;
142 		}
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);
148 	}
149 	return 0;
150 }
151 
152 /*
153  * vx_read_status - return the status rmh
154  * @rmh: rmh record to store the status
155  *
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 mutex held!
159  */
160 static int vx_read_status(struct vx_core *chip, struct vx_rmh *rmh)
161 {
162 	int i, err, val, size;
163 
164 	/* no read necessary? */
165 	if (rmh->DspStat == RMH_SSIZE_FIXED && rmh->LgStat == 0)
166 		return 0;
167 
168 	/* Wait for RX full (with timeout protection)
169 	 * The first word of status is in RX
170 	 */
171 	err = vx_wait_for_rx_full(chip);
172 	if (err < 0)
173 		return err;
174 
175 	/* Read RX */
176 	val = vx_inb(chip, RXH) << 16;
177 	val |= vx_inb(chip, RXM) << 8;
178 	val |= vx_inb(chip, RXL);
179 
180 	/* If status given by DSP, let's decode its size */
181 	switch (rmh->DspStat) {
182 	case RMH_SSIZE_ARG:
183 		size = val & 0xff;
184 		rmh->Stat[0] = val & 0xffff00;
185 		rmh->LgStat = size + 1;
186 		break;
187 	case RMH_SSIZE_MASK:
188 		/* Let's count the arg numbers from a mask */
189 		rmh->Stat[0] = val;
190 		size = 0;
191 		while (val) {
192 			if (val & 0x01)
193 				size++;
194 			val >>= 1;
195 		}
196 		rmh->LgStat = size + 1;
197 		break;
198 	default:
199 		/* else retrieve the status length given by the driver */
200 		size = rmh->LgStat;
201 		rmh->Stat[0] = val;  /* Val is the status 1st word */
202 		size--;              /* hence adjust remaining length */
203 		break;
204         }
205 
206 	if (size < 1)
207 		return 0;
208 	if (snd_BUG_ON(size >= SIZE_MAX_STATUS))
209 		return -EINVAL;
210 
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);
214 		if (err < 0)
215 			return err;
216 		/* Wait for RX full (with timeout protection) */
217 		err = vx_wait_for_rx_full(chip);
218 		if (err < 0)
219 			return err;
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);
223 	}
224 
225 	return vx_transfer_end(chip, IRQ_MESS_WRITE_END);
226 }
227 
228 
229 #define MASK_MORE_THAN_1_WORD_COMMAND   0x00008000
230 #define MASK_1_WORD_COMMAND             0x00ff7fff
231 
232 /*
233  * vx_send_msg_nolock - send a DSP message and read back the status
234  * @rmh: the rmh record to send and receive
235  *
236  * returns 0 if successful, or a negative error code.
237  * the error code can be VX-specific, retrieved via vx_get_error().
238  *
239  * this function doesn't call mutex lock at all.
240  */
241 int vx_send_msg_nolock(struct vx_core *chip, struct vx_rmh *rmh)
242 {
243 	int i, err;
244 
245 	if (chip->chip_status & VX_STAT_IS_STALE)
246 		return -EBUSY;
247 
248 	if ((err = vx_reset_chk(chip)) < 0) {
249 		snd_printd(KERN_DEBUG "vx_send_msg: vx_reset_chk error\n");
250 		return err;
251 	}
252 
253 #if 0
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]);
260 		printk("\n");
261 	}
262 #endif
263 	/* Check bit M is set according to length of the command */
264 	if (rmh->LgCmd > 1)
265 		rmh->Cmd[0] |= MASK_MORE_THAN_1_WORD_COMMAND;
266 	else
267 		rmh->Cmd[0] &= MASK_1_WORD_COMMAND;
268 
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");
272 		return err;
273 	}
274 
275 	/* Write Cmd[0] */
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);
279 
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");
283 		return err;
284 	}
285 
286 	/* Wait for CHK = 1 */
287 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
288 		return err;
289 
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");
294 			return err;
295 		}
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);
301 		return err;
302 	}
303 
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");
310 				return err;
311 			}
312 
313 			/* Write Cmd[i] */
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);
317 
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");
321 				return err;
322 			}
323 		}
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");
327 			return err;
328 		}
329 		/* End of transfer */
330 		err = vx_transfer_end(chip, IRQ_MESS_READ_END);
331 		if (err < 0)
332 			return err;
333 	}
334 
335 	return vx_read_status(chip, rmh);
336 }
337 
338 
339 /*
340  * vx_send_msg - send a DSP message with mutex
341  * @rmh: the rmh record to send and receive
342  *
343  * returns 0 if successful, or a negative error code.
344  * see vx_send_msg_nolock().
345  */
346 int vx_send_msg(struct vx_core *chip, struct vx_rmh *rmh)
347 {
348 	int err;
349 
350 	mutex_lock(&chip->lock);
351 	err = vx_send_msg_nolock(chip, rmh);
352 	mutex_unlock(&chip->lock);
353 	return err;
354 }
355 
356 
357 /*
358  * vx_send_rih_nolock - send an RIH to xilinx
359  * @cmd: the command to send
360  *
361  * returns 0 if successful, or a negative error code.
362  * the error code can be VX-specific, retrieved via vx_get_error().
363  *
364  * this function doesn't call mutex at all.
365  *
366  * unlike RMH, no command is sent to DSP.
367  */
368 int vx_send_rih_nolock(struct vx_core *chip, int cmd)
369 {
370 	int err;
371 
372 	if (chip->chip_status & VX_STAT_IS_STALE)
373 		return -EBUSY;
374 
375 #if 0
376 	printk(KERN_DEBUG "send_rih: cmd = 0x%x\n", cmd);
377 #endif
378 	if ((err = vx_reset_chk(chip)) < 0)
379 		return err;
380 	/* send the IRQ */
381 	if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
382 		return err;
383 	/* Wait CHK = 1 */
384 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
385 		return err;
386 	/* If error, read RX */
387 	if (vx_inb(chip, ISR) & ISR_ERR) {
388 		if ((err = vx_wait_for_rx_full(chip)) < 0)
389 			return err;
390 		err = vx_inb(chip, RXH) << 16;
391 		err |= vx_inb(chip, RXM) << 8;
392 		err |= vx_inb(chip, RXL);
393 		return -(VX_ERR_MASK | err);
394 	}
395 	return 0;
396 }
397 
398 
399 /*
400  * vx_send_rih - send an RIH with mutex
401  * @cmd: the command to send
402  *
403  * see vx_send_rih_nolock().
404  */
405 int vx_send_rih(struct vx_core *chip, int cmd)
406 {
407 	int err;
408 
409 	mutex_lock(&chip->lock);
410 	err = vx_send_rih_nolock(chip, cmd);
411 	mutex_unlock(&chip->lock);
412 	return err;
413 }
414 
415 #define END_OF_RESET_WAIT_TIME		500	/* us */
416 
417 /**
418  * snd_vx_boot_xilinx - boot up the xilinx interface
419  * @boot: the boot record to load
420  */
421 int snd_vx_load_boot_image(struct vx_core *chip, const struct firmware *boot)
422 {
423 	unsigned int i;
424 	int no_fillup = vx_has_new_dsp(chip);
425 
426 	/* check the length of boot image */
427 	if (boot->size <= 0)
428 		return -EINVAL;
429 	if (boot->size % 3)
430 		return -EINVAL;
431 #if 0
432 	{
433 		/* more strict check */
434 		unsigned int c = ((u32)boot->data[0] << 16) | ((u32)boot->data[1] << 8) | boot->data[2];
435 		if (boot->size != (c + 2) * 3)
436 			return -EINVAL;
437 	}
438 #endif
439 
440 	/* reset dsp */
441 	vx_reset_dsp(chip);
442 
443 	udelay(END_OF_RESET_WAIT_TIME); /* another wait? */
444 
445 	/* download boot strap */
446 	for (i = 0; i < 0x600; i += 3) {
447 		if (i >= boot->size) {
448 			if (no_fillup)
449 				break;
450 			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
451 				snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
452 				return -EIO;
453 			}
454 			vx_outb(chip, TXH, 0);
455 			vx_outb(chip, TXM, 0);
456 			vx_outb(chip, TXL, 0);
457 		} else {
458 			const unsigned char *image = boot->data + i;
459 			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
460 				snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
461 				return -EIO;
462 			}
463 			vx_outb(chip, TXH, image[0]);
464 			vx_outb(chip, TXM, image[1]);
465 			vx_outb(chip, TXL, image[2]);
466 		}
467 	}
468 	return 0;
469 }
470 
471 EXPORT_SYMBOL(snd_vx_load_boot_image);
472 
473 /*
474  * vx_test_irq_src - query the source of interrupts
475  *
476  * called from irq handler only
477  */
478 static int vx_test_irq_src(struct vx_core *chip, unsigned int *ret)
479 {
480 	int err;
481 
482 	vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
483 	mutex_lock(&chip->lock);
484 	err = vx_send_msg_nolock(chip, &chip->irq_rmh);
485 	if (err < 0)
486 		*ret = 0;
487 	else
488 		*ret = chip->irq_rmh.Stat[0];
489 	mutex_unlock(&chip->lock);
490 	return err;
491 }
492 
493 
494 /*
495  * snd_vx_threaded_irq_handler - threaded irq handler
496  */
497 irqreturn_t snd_vx_threaded_irq_handler(int irq, void *dev)
498 {
499 	struct vx_core *chip = dev;
500 	unsigned int events;
501 
502 	if (chip->chip_status & VX_STAT_IS_STALE)
503 		return IRQ_HANDLED;
504 
505 	if (vx_test_irq_src(chip, &events) < 0)
506 		return IRQ_HANDLED;
507 
508 #if 0
509 	if (events & 0x000800)
510 		printk(KERN_ERR "DSP Stream underrun ! IRQ events = 0x%x\n", events);
511 #endif
512 	// printk(KERN_DEBUG "IRQ events = 0x%x\n", events);
513 
514 	/* We must prevent any application using this DSP
515 	 * and block any further request until the application
516 	 * either unregisters or reloads the DSP
517 	 */
518 	if (events & FATAL_DSP_ERROR) {
519 		snd_printk(KERN_ERR "vx_core: fatal DSP error!!\n");
520 		return IRQ_HANDLED;
521 	}
522 
523 	/* The start on time code conditions are filled (ie the time code
524 	 * received by the board is equal to one of those given to it).
525 	 */
526 	if (events & TIME_CODE_EVENT_PENDING)
527 		; /* so far, nothing to do yet */
528 
529 	/* The frequency has changed on the board (UER mode). */
530 	if (events & FREQUENCY_CHANGE_EVENT_PENDING)
531 		vx_change_frequency(chip);
532 
533 	/* update the pcm streams */
534 	vx_pcm_update_intr(chip, events);
535 	return IRQ_HANDLED;
536 }
537 EXPORT_SYMBOL(snd_vx_threaded_irq_handler);
538 
539 /**
540  * snd_vx_irq_handler - interrupt handler
541  */
542 irqreturn_t snd_vx_irq_handler(int irq, void *dev)
543 {
544 	struct vx_core *chip = dev;
545 
546 	if (! (chip->chip_status & VX_STAT_CHIP_INIT) ||
547 	    (chip->chip_status & VX_STAT_IS_STALE))
548 		return IRQ_NONE;
549 	if (! vx_test_and_ack(chip))
550 		return IRQ_WAKE_THREAD;
551 	return IRQ_NONE;
552 }
553 
554 EXPORT_SYMBOL(snd_vx_irq_handler);
555 
556 /*
557  */
558 static void vx_reset_board(struct vx_core *chip, int cold_reset)
559 {
560 	if (snd_BUG_ON(!chip->ops->reset_board))
561 		return;
562 
563 	/* current source, later sync'ed with target */
564 	chip->audio_source = VX_AUDIO_SRC_LINE;
565 	if (cold_reset) {
566 		chip->audio_source_target = chip->audio_source;
567 		chip->clock_source = INTERNAL_QUARTZ;
568 		chip->clock_mode = VX_CLOCK_MODE_AUTO;
569 		chip->freq = 48000;
570 		chip->uer_detected = VX_UER_MODE_NOT_PRESENT;
571 		chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
572 	}
573 
574 	chip->ops->reset_board(chip, cold_reset);
575 
576 	vx_reset_codec(chip, cold_reset);
577 
578 	vx_set_internal_clock(chip, chip->freq);
579 
580 	/* Reset the DSP */
581 	vx_reset_dsp(chip);
582 
583 	if (vx_is_pcmcia(chip)) {
584 		/* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
585 		vx_test_and_ack(chip);
586 		vx_validate_irq(chip, 1);
587 	}
588 
589 	/* init CBits */
590 	vx_set_iec958_status(chip, chip->uer_bits);
591 }
592 
593 
594 /*
595  * proc interface
596  */
597 
598 static void vx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
599 {
600 	struct vx_core *chip = entry->private_data;
601 	static char *audio_src_vxp[] = { "Line", "Mic", "Digital" };
602 	static char *audio_src_vx2[] = { "Analog", "Analog", "Digital" };
603 	static char *clock_mode[] = { "Auto", "Internal", "External" };
604 	static char *clock_src[] = { "Internal", "External" };
605 	static char *uer_type[] = { "Consumer", "Professional", "Not Present" };
606 
607 	snd_iprintf(buffer, "%s\n", chip->card->longname);
608 	snd_iprintf(buffer, "Xilinx Firmware: %s\n",
609 		    chip->chip_status & VX_STAT_XILINX_LOADED ? "Loaded" : "No");
610 	snd_iprintf(buffer, "Device Initialized: %s\n",
611 		    chip->chip_status & VX_STAT_DEVICE_INIT ? "Yes" : "No");
612 	snd_iprintf(buffer, "DSP audio info:");
613 	if (chip->audio_info & VX_AUDIO_INFO_REAL_TIME)
614 		snd_iprintf(buffer, " realtime");
615 	if (chip->audio_info & VX_AUDIO_INFO_OFFLINE)
616 		snd_iprintf(buffer, " offline");
617 	if (chip->audio_info & VX_AUDIO_INFO_MPEG1)
618 		snd_iprintf(buffer, " mpeg1");
619 	if (chip->audio_info & VX_AUDIO_INFO_MPEG2)
620 		snd_iprintf(buffer, " mpeg2");
621 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_8)
622 		snd_iprintf(buffer, " linear8");
623 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_16)
624 		snd_iprintf(buffer, " linear16");
625 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_24)
626 		snd_iprintf(buffer, " linear24");
627 	snd_iprintf(buffer, "\n");
628 	snd_iprintf(buffer, "Input Source: %s\n", vx_is_pcmcia(chip) ?
629 		    audio_src_vxp[chip->audio_source] :
630 		    audio_src_vx2[chip->audio_source]);
631 	snd_iprintf(buffer, "Clock Mode: %s\n", clock_mode[chip->clock_mode]);
632 	snd_iprintf(buffer, "Clock Source: %s\n", clock_src[chip->clock_source]);
633 	snd_iprintf(buffer, "Frequency: %d\n", chip->freq);
634 	snd_iprintf(buffer, "Detected Frequency: %d\n", chip->freq_detected);
635 	snd_iprintf(buffer, "Detected UER type: %s\n", uer_type[chip->uer_detected]);
636 	snd_iprintf(buffer, "Min/Max/Cur IBL: %d/%d/%d (granularity=%d)\n",
637 		    chip->ibl.min_size, chip->ibl.max_size, chip->ibl.size,
638 		    chip->ibl.granularity);
639 }
640 
641 static void vx_proc_init(struct vx_core *chip)
642 {
643 	struct snd_info_entry *entry;
644 
645 	if (! snd_card_proc_new(chip->card, "vx-status", &entry))
646 		snd_info_set_text_ops(entry, chip, vx_proc_read);
647 }
648 
649 
650 /**
651  * snd_vx_dsp_boot - load the DSP boot
652  */
653 int snd_vx_dsp_boot(struct vx_core *chip, const struct firmware *boot)
654 {
655 	int err;
656 	int cold_reset = !(chip->chip_status & VX_STAT_DEVICE_INIT);
657 
658 	vx_reset_board(chip, cold_reset);
659 	vx_validate_irq(chip, 0);
660 
661 	if ((err = snd_vx_load_boot_image(chip, boot)) < 0)
662 		return err;
663 	msleep(10);
664 
665 	return 0;
666 }
667 
668 EXPORT_SYMBOL(snd_vx_dsp_boot);
669 
670 /**
671  * snd_vx_dsp_load - load the DSP image
672  */
673 int snd_vx_dsp_load(struct vx_core *chip, const struct firmware *dsp)
674 {
675 	unsigned int i;
676 	int err;
677 	unsigned int csum = 0;
678 	const unsigned char *image, *cptr;
679 
680 	if (dsp->size % 3)
681 		return -EINVAL;
682 
683 	vx_toggle_dac_mute(chip, 1);
684 
685 	/* Transfert data buffer from PC to DSP */
686 	for (i = 0; i < dsp->size; i += 3) {
687 		image = dsp->data + i;
688 		/* Wait DSP ready for a new read */
689 		if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
690 			printk(KERN_ERR
691 			       "dsp loading error at position %d\n", i);
692 			return err;
693 		}
694 		cptr = image;
695 		csum ^= *cptr;
696 		csum = (csum >> 24) | (csum << 8);
697 		vx_outb(chip, TXH, *cptr++);
698 		csum ^= *cptr;
699 		csum = (csum >> 24) | (csum << 8);
700 		vx_outb(chip, TXM, *cptr++);
701 		csum ^= *cptr;
702 		csum = (csum >> 24) | (csum << 8);
703 		vx_outb(chip, TXL, *cptr++);
704 	}
705 	snd_printdd(KERN_DEBUG "checksum = 0x%08x\n", csum);
706 
707 	msleep(200);
708 
709 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
710 		return err;
711 
712 	vx_toggle_dac_mute(chip, 0);
713 
714 	vx_test_and_ack(chip);
715 	vx_validate_irq(chip, 1);
716 
717 	return 0;
718 }
719 
720 EXPORT_SYMBOL(snd_vx_dsp_load);
721 
722 #ifdef CONFIG_PM
723 /*
724  * suspend
725  */
726 int snd_vx_suspend(struct vx_core *chip)
727 {
728 	unsigned int i;
729 
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]);
734 
735 	return 0;
736 }
737 
738 EXPORT_SYMBOL(snd_vx_suspend);
739 
740 /*
741  * resume
742  */
743 int snd_vx_resume(struct vx_core *chip)
744 {
745 	int i, err;
746 
747 	chip->chip_status &= ~VX_STAT_CHIP_INIT;
748 
749 	for (i = 0; i < 4; i++) {
750 		if (! chip->firmware[i])
751 			continue;
752 		err = chip->ops->load_dsp(chip, i, chip->firmware[i]);
753 		if (err < 0) {
754 			snd_printk(KERN_ERR "vx: firmware resume error at DSP %d\n", i);
755 			return -EIO;
756 		}
757 	}
758 
759 	chip->chip_status |= VX_STAT_CHIP_INIT;
760 	chip->chip_status &= ~VX_STAT_IN_SUSPEND;
761 
762 	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
763 	return 0;
764 }
765 
766 EXPORT_SYMBOL(snd_vx_resume);
767 #endif
768 
769 /**
770  * snd_vx_create - constructor for struct vx_core
771  * @hw: hardware specific record
772  *
773  * this function allocates the instance and prepare for the hardware
774  * initialization.
775  *
776  * return the instance pointer if successful, NULL in error.
777  */
778 struct vx_core *snd_vx_create(struct snd_card *card, struct snd_vx_hardware *hw,
779 			      struct snd_vx_ops *ops,
780 			      int extra_size)
781 {
782 	struct vx_core *chip;
783 
784 	if (snd_BUG_ON(!card || !hw || !ops))
785 		return NULL;
786 
787 	chip = kzalloc(sizeof(*chip) + extra_size, GFP_KERNEL);
788 	if (! chip) {
789 		snd_printk(KERN_ERR "vx_core: no memory\n");
790 		return NULL;
791 	}
792 	mutex_init(&chip->lock);
793 	chip->irq = -1;
794 	chip->hw = hw;
795 	chip->type = hw->type;
796 	chip->ops = ops;
797 	mutex_init(&chip->mixer_mutex);
798 
799 	chip->card = card;
800 	card->private_data = chip;
801 	strcpy(card->driver, hw->name);
802 	sprintf(card->shortname, "Digigram %s", hw->name);
803 
804 	vx_proc_init(chip);
805 
806 	return chip;
807 }
808 
809 EXPORT_SYMBOL(snd_vx_create);
810 
811 /*
812  * module entries
813  */
814 static int __init alsa_vx_core_init(void)
815 {
816 	return 0;
817 }
818 
819 static void __exit alsa_vx_core_exit(void)
820 {
821 }
822 
823 module_init(alsa_vx_core_init)
824 module_exit(alsa_vx_core_exit)
825