blob: 289f041706cd2526be2743281ef6f1b94be49a5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040029#include <linux/module.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010030#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/asoundef.h>
34#include <sound/info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/vx_core.h>
36#include "vx_cmd.h"
37
38MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
39MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
40MODULE_LICENSE("GPL");
41
42
43/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +010052int snd_vx_check_reg_bit(struct vx_core *chip, int reg, int mask, int bit, int time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 unsigned long end_time = jiffies + (time * HZ + 999) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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 };
Takashi Iwai86b27232013-01-25 10:54:07 +010062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 do {
64 if ((snd_vx_inb(chip, reg) & mask) == bit)
65 return 0;
Takashi Iwaibdbae7e2005-11-17 10:21:19 +010066 //msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 } 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
Takashi Iwaifa325eb2006-04-28 15:13:40 +020072EXPORT_SYMBOL(snd_vx_check_reg_bit);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +010082static int vx_send_irq_dsp(struct vx_core *chip, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100103static int vx_reset_chk(struct vx_core *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
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().
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200120 * NB: call with mutex held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100122static int vx_transfer_end(struct vx_core *chip, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
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().
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200158 * NB: call with mutex held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100160static int vx_read_status(struct vx_core *chip, struct vx_rmh *rmh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
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;
Dan Carpenterfefe2282013-06-21 15:25:33 +0300208 if (snd_BUG_ON(size >= SIZE_MAX_STATUS))
Takashi Iwai5e246b82008-08-08 17:12:47 +0200209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
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 *
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200239 * this function doesn't call mutex lock at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100241int vx_send_msg_nolock(struct vx_core *chip, struct vx_rmh *rmh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
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/*
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200340 * vx_send_msg - send a DSP message with mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100346int vx_send_msg(struct vx_core *chip, struct vx_rmh *rmh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int err;
349
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200350 mutex_lock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 err = vx_send_msg_nolock(chip, rmh);
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200352 mutex_unlock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 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 *
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200364 * this function doesn't call mutex at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 * unlike RMH, no command is sent to DSP.
367 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100368int vx_send_rih_nolock(struct vx_core *chip, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
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/*
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200400 * vx_send_rih - send an RIH with mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * @cmd: the command to send
402 *
403 * see vx_send_rih_nolock().
404 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100405int vx_send_rih(struct vx_core *chip, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int err;
408
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200409 mutex_lock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 err = vx_send_rih_nolock(chip, cmd);
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200411 mutex_unlock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 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
Takashi Iwai2a9e8df2014-11-10 16:46:35 +0100419 * @chip: VX core instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * @boot: the boot record to load
421 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100422int snd_vx_load_boot_image(struct vx_core *chip, const struct firmware *boot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 unsigned int i;
425 int no_fillup = vx_has_new_dsp(chip);
426
427 /* check the length of boot image */
Takashi Iwai5e246b82008-08-08 17:12:47 +0200428 if (boot->size <= 0)
429 return -EINVAL;
430 if (boot->size % 3)
431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432#if 0
433 {
434 /* more strict check */
435 unsigned int c = ((u32)boot->data[0] << 16) | ((u32)boot->data[1] << 8) | boot->data[2];
Takashi Iwai5e246b82008-08-08 17:12:47 +0200436 if (boot->size != (c + 2) * 3)
437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439#endif
440
441 /* reset dsp */
442 vx_reset_dsp(chip);
443
444 udelay(END_OF_RESET_WAIT_TIME); /* another wait? */
445
446 /* download boot strap */
447 for (i = 0; i < 0x600; i += 3) {
448 if (i >= boot->size) {
449 if (no_fillup)
450 break;
451 if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
452 snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
453 return -EIO;
454 }
455 vx_outb(chip, TXH, 0);
456 vx_outb(chip, TXM, 0);
457 vx_outb(chip, TXL, 0);
458 } else {
David Woodhouse2f0600b2008-05-24 00:02:49 +0100459 const unsigned char *image = boot->data + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
461 snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
462 return -EIO;
463 }
464 vx_outb(chip, TXH, image[0]);
465 vx_outb(chip, TXM, image[1]);
466 vx_outb(chip, TXL, image[2]);
467 }
468 }
469 return 0;
470}
471
Takashi Iwaifa325eb2006-04-28 15:13:40 +0200472EXPORT_SYMBOL(snd_vx_load_boot_image);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
475 * vx_test_irq_src - query the source of interrupts
476 *
477 * called from irq handler only
478 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100479static int vx_test_irq_src(struct vx_core *chip, unsigned int *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 int err;
482
483 vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200484 mutex_lock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 err = vx_send_msg_nolock(chip, &chip->irq_rmh);
486 if (err < 0)
487 *ret = 0;
488 else
489 *ret = chip->irq_rmh.Stat[0];
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200490 mutex_unlock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return err;
492}
493
494
495/*
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200496 * snd_vx_threaded_irq_handler - threaded irq handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200498irqreturn_t snd_vx_threaded_irq_handler(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200500 struct vx_core *chip = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned int events;
502
503 if (chip->chip_status & VX_STAT_IS_STALE)
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200504 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (vx_test_irq_src(chip, &events) < 0)
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200507 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509#if 0
510 if (events & 0x000800)
511 printk(KERN_ERR "DSP Stream underrun ! IRQ events = 0x%x\n", events);
512#endif
513 // printk(KERN_DEBUG "IRQ events = 0x%x\n", events);
514
515 /* We must prevent any application using this DSP
516 * and block any further request until the application
517 * either unregisters or reloads the DSP
518 */
519 if (events & FATAL_DSP_ERROR) {
520 snd_printk(KERN_ERR "vx_core: fatal DSP error!!\n");
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200521 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 /* The start on time code conditions are filled (ie the time code
525 * received by the board is equal to one of those given to it).
526 */
527 if (events & TIME_CODE_EVENT_PENDING)
528 ; /* so far, nothing to do yet */
529
530 /* The frequency has changed on the board (UER mode). */
531 if (events & FREQUENCY_CHANGE_EVENT_PENDING)
532 vx_change_frequency(chip);
533
534 /* update the pcm streams */
535 vx_pcm_update_intr(chip, events);
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200536 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200538EXPORT_SYMBOL(snd_vx_threaded_irq_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540/**
541 * snd_vx_irq_handler - interrupt handler
Takashi Iwai2a9e8df2014-11-10 16:46:35 +0100542 * @irq: irq number
543 * @dev: VX core instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
David Howells7d12e782006-10-05 14:55:46 +0100545irqreturn_t snd_vx_irq_handler(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100547 struct vx_core *chip = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (! (chip->chip_status & VX_STAT_CHIP_INIT) ||
550 (chip->chip_status & VX_STAT_IS_STALE))
551 return IRQ_NONE;
552 if (! vx_test_and_ack(chip))
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200553 return IRQ_WAKE_THREAD;
554 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Takashi Iwaifa325eb2006-04-28 15:13:40 +0200557EXPORT_SYMBOL(snd_vx_irq_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559/*
560 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100561static void vx_reset_board(struct vx_core *chip, int cold_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Takashi Iwai5e246b82008-08-08 17:12:47 +0200563 if (snd_BUG_ON(!chip->ops->reset_board))
564 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* current source, later sync'ed with target */
567 chip->audio_source = VX_AUDIO_SRC_LINE;
568 if (cold_reset) {
569 chip->audio_source_target = chip->audio_source;
570 chip->clock_source = INTERNAL_QUARTZ;
571 chip->clock_mode = VX_CLOCK_MODE_AUTO;
572 chip->freq = 48000;
573 chip->uer_detected = VX_UER_MODE_NOT_PRESENT;
574 chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
575 }
576
577 chip->ops->reset_board(chip, cold_reset);
578
579 vx_reset_codec(chip, cold_reset);
580
581 vx_set_internal_clock(chip, chip->freq);
582
583 /* Reset the DSP */
584 vx_reset_dsp(chip);
585
586 if (vx_is_pcmcia(chip)) {
587 /* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
588 vx_test_and_ack(chip);
589 vx_validate_irq(chip, 1);
590 }
591
592 /* init CBits */
593 vx_set_iec958_status(chip, chip->uer_bits);
594}
595
596
597/*
598 * proc interface
599 */
600
Takashi Iwaiaf263672005-11-17 14:46:59 +0100601static void vx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100603 struct vx_core *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 static char *audio_src_vxp[] = { "Line", "Mic", "Digital" };
605 static char *audio_src_vx2[] = { "Analog", "Analog", "Digital" };
606 static char *clock_mode[] = { "Auto", "Internal", "External" };
607 static char *clock_src[] = { "Internal", "External" };
608 static char *uer_type[] = { "Consumer", "Professional", "Not Present" };
609
610 snd_iprintf(buffer, "%s\n", chip->card->longname);
611 snd_iprintf(buffer, "Xilinx Firmware: %s\n",
612 chip->chip_status & VX_STAT_XILINX_LOADED ? "Loaded" : "No");
613 snd_iprintf(buffer, "Device Initialized: %s\n",
614 chip->chip_status & VX_STAT_DEVICE_INIT ? "Yes" : "No");
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);
642}
643
Takashi Iwaiaf263672005-11-17 14:46:59 +0100644static void vx_proc_init(struct vx_core *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100646 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (! snd_card_proc_new(chip->card, "vx-status", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +0200649 snd_info_set_text_ops(entry, chip, vx_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652
653/**
654 * snd_vx_dsp_boot - load the DSP boot
Takashi Iwai2a9e8df2014-11-10 16:46:35 +0100655 * @chip: VX core instance
656 * @boot: firmware data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100658int snd_vx_dsp_boot(struct vx_core *chip, const struct firmware *boot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int err;
661 int cold_reset = !(chip->chip_status & VX_STAT_DEVICE_INIT);
662
663 vx_reset_board(chip, cold_reset);
664 vx_validate_irq(chip, 0);
665
666 if ((err = snd_vx_load_boot_image(chip, boot)) < 0)
667 return err;
Takashi Iwaibdbae7e2005-11-17 10:21:19 +0100668 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 return 0;
671}
672
Takashi Iwaifa325eb2006-04-28 15:13:40 +0200673EXPORT_SYMBOL(snd_vx_dsp_boot);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/**
676 * snd_vx_dsp_load - load the DSP image
Takashi Iwai2a9e8df2014-11-10 16:46:35 +0100677 * @chip: VX core instance
678 * @dsp: firmware data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100680int snd_vx_dsp_load(struct vx_core *chip, const struct firmware *dsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 unsigned int i;
683 int err;
684 unsigned int csum = 0;
David Woodhouse2f0600b2008-05-24 00:02:49 +0100685 const unsigned char *image, *cptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Takashi Iwai5e246b82008-08-08 17:12:47 +0200687 if (dsp->size % 3)
688 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 vx_toggle_dac_mute(chip, 1);
691
692 /* Transfert data buffer from PC to DSP */
693 for (i = 0; i < dsp->size; i += 3) {
694 image = dsp->data + i;
695 /* Wait DSP ready for a new read */
696 if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
Takashi Iwai45203832009-02-05 15:51:50 +0100697 printk(KERN_ERR
698 "dsp loading error at position %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return err;
700 }
701 cptr = image;
702 csum ^= *cptr;
703 csum = (csum >> 24) | (csum << 8);
704 vx_outb(chip, TXH, *cptr++);
705 csum ^= *cptr;
706 csum = (csum >> 24) | (csum << 8);
707 vx_outb(chip, TXM, *cptr++);
708 csum ^= *cptr;
709 csum = (csum >> 24) | (csum << 8);
710 vx_outb(chip, TXL, *cptr++);
711 }
712 snd_printdd(KERN_DEBUG "checksum = 0x%08x\n", csum);
713
Takashi Iwaibdbae7e2005-11-17 10:21:19 +0100714 msleep(200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
717 return err;
718
719 vx_toggle_dac_mute(chip, 0);
720
721 vx_test_and_ack(chip);
722 vx_validate_irq(chip, 1);
723
724 return 0;
725}
726
Takashi Iwaifa325eb2006-04-28 15:13:40 +0200727EXPORT_SYMBOL(snd_vx_dsp_load);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729#ifdef CONFIG_PM
730/*
731 * suspend
732 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200733int snd_vx_suspend(struct vx_core *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 unsigned int i;
736
Takashi Iwai0ed1cad2005-11-17 16:06:05 +0100737 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 chip->chip_status |= VX_STAT_IN_SUSPEND;
739 for (i = 0; i < chip->hw->num_codecs; i++)
740 snd_pcm_suspend_all(chip->pcm[i]);
741
742 return 0;
743}
744
Takashi Iwaifa325eb2006-04-28 15:13:40 +0200745EXPORT_SYMBOL(snd_vx_suspend);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
748 * resume
749 */
Takashi Iwai0ed1cad2005-11-17 16:06:05 +0100750int snd_vx_resume(struct vx_core *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int i, err;
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 chip->chip_status &= ~VX_STAT_CHIP_INIT;
755
756 for (i = 0; i < 4; i++) {
757 if (! chip->firmware[i])
758 continue;
759 err = chip->ops->load_dsp(chip, i, chip->firmware[i]);
760 if (err < 0) {
761 snd_printk(KERN_ERR "vx: firmware resume error at DSP %d\n", i);
762 return -EIO;
763 }
764 }
765
766 chip->chip_status |= VX_STAT_CHIP_INIT;
767 chip->chip_status &= ~VX_STAT_IN_SUSPEND;
768
Takashi Iwai0ed1cad2005-11-17 16:06:05 +0100769 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771}
772
Takashi Iwaifa325eb2006-04-28 15:13:40 +0200773EXPORT_SYMBOL(snd_vx_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774#endif
775
776/**
Takashi Iwaiaf263672005-11-17 14:46:59 +0100777 * snd_vx_create - constructor for struct vx_core
Takashi Iwai2a9e8df2014-11-10 16:46:35 +0100778 * @card: card instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * @hw: hardware specific record
Takashi Iwai2a9e8df2014-11-10 16:46:35 +0100780 * @ops: VX ops pointer
781 * @extra_size: extra byte size to allocate appending to chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
783 * this function allocates the instance and prepare for the hardware
784 * initialization.
785 *
786 * return the instance pointer if successful, NULL in error.
787 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100788struct vx_core *snd_vx_create(struct snd_card *card, struct snd_vx_hardware *hw,
789 struct snd_vx_ops *ops,
790 int extra_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100792 struct vx_core *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Takashi Iwai5e246b82008-08-08 17:12:47 +0200794 if (snd_BUG_ON(!card || !hw || !ops))
795 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Takashi Iwai561b2202005-09-09 14:22:34 +0200797 chip = kzalloc(sizeof(*chip) + extra_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (! chip) {
799 snd_printk(KERN_ERR "vx_core: no memory\n");
800 return NULL;
801 }
Takashi Iwaidb0a5212014-09-09 17:17:20 +0200802 mutex_init(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 chip->irq = -1;
804 chip->hw = hw;
805 chip->type = hw->type;
806 chip->ops = ops;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100807 mutex_init(&chip->mixer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 chip->card = card;
810 card->private_data = chip;
811 strcpy(card->driver, hw->name);
812 sprintf(card->shortname, "Digigram %s", hw->name);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 vx_proc_init(chip);
815
816 return chip;
817}
818
Takashi Iwaifa325eb2006-04-28 15:13:40 +0200819EXPORT_SYMBOL(snd_vx_create);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/*
822 * module entries
823 */
824static int __init alsa_vx_core_init(void)
825{
826 return 0;
827}
828
829static void __exit alsa_vx_core_exit(void)
830{
831}
832
833module_init(alsa_vx_core_init)
834module_exit(alsa_vx_core_exit)