ocfs2: Make the left masklogs compat.
[taoma-kernel.git] / drivers / staging / cx25821 / cx25821-audio-upstream.c
blob7992a3ba526f12196c9fe2940f78c50f47b6ffc9
1 /*
2 * Driver for the Conexant CX25821 PCIe bridge
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include "cx25821-video.h"
26 #include "cx25821-audio-upstream.h"
28 #include <linux/fs.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/syscalls.h>
34 #include <linux/file.h>
35 #include <linux/fcntl.h>
36 #include <linux/delay.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
40 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
41 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
42 MODULE_LICENSE("GPL");
44 static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
45 FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
47 int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
48 struct sram_channel *ch,
49 unsigned int bpl, u32 risc)
51 unsigned int i, lines;
52 u32 cdt;
54 if (ch->cmds_start == 0) {
55 cx_write(ch->ptr1_reg, 0);
56 cx_write(ch->ptr2_reg, 0);
57 cx_write(ch->cnt2_reg, 0);
58 cx_write(ch->cnt1_reg, 0);
59 return 0;
62 bpl = (bpl + 7) & ~7; /* alignment */
63 cdt = ch->cdt;
64 lines = ch->fifo_size / bpl;
66 if (lines > 3)
67 lines = 3;
69 BUG_ON(lines < 2);
71 /* write CDT */
72 for (i = 0; i < lines; i++) {
73 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
74 cx_write(cdt + 16 * i + 4, 0);
75 cx_write(cdt + 16 * i + 8, 0);
76 cx_write(cdt + 16 * i + 12, 0);
79 /* write CMDS */
80 cx_write(ch->cmds_start + 0, risc);
82 cx_write(ch->cmds_start + 4, 0);
83 cx_write(ch->cmds_start + 8, cdt);
84 cx_write(ch->cmds_start + 12, AUDIO_CDT_SIZE_QW);
85 cx_write(ch->cmds_start + 16, ch->ctrl_start);
87 /* IQ size */
88 cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);
90 for (i = 24; i < 80; i += 4)
91 cx_write(ch->cmds_start + i, 0);
93 /* fill registers */
94 cx_write(ch->ptr1_reg, ch->fifo_start);
95 cx_write(ch->ptr2_reg, cdt);
96 cx_write(ch->cnt2_reg, AUDIO_CDT_SIZE_QW);
97 cx_write(ch->cnt1_reg, AUDIO_CLUSTER_SIZE_QW - 1);
99 return 0;
102 static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
103 __le32 *rp,
104 dma_addr_t databuf_phys_addr,
105 unsigned int bpl,
106 int fifo_enable)
108 unsigned int line;
109 struct sram_channel *sram_ch =
110 dev->channels[dev->_audio_upstream_channel_select].sram_channels;
111 int offset = 0;
113 /* scan lines */
114 for (line = 0; line < LINES_PER_AUDIO_BUFFER; line++) {
115 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
116 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
117 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
119 /* Check if we need to enable the FIFO
120 * after the first 3 lines.
121 * For the upstream audio channel,
122 * the risc engine will enable the FIFO */
123 if (fifo_enable && line == 2) {
124 *(rp++) = RISC_WRITECR;
125 *(rp++) = sram_ch->dma_ctl;
126 *(rp++) = sram_ch->fld_aud_fifo_en;
127 *(rp++) = 0x00000020;
130 offset += AUDIO_LINE_SIZE;
133 return rp;
136 int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
137 struct pci_dev *pci,
138 unsigned int bpl, unsigned int lines)
140 __le32 *rp;
141 int fifo_enable = 0;
142 int frame = 0, i = 0;
143 int frame_size = AUDIO_DATA_BUF_SZ;
144 int databuf_offset = 0;
145 int risc_flag = RISC_CNT_INC;
146 dma_addr_t risc_phys_jump_addr;
148 /* Virtual address of Risc buffer program */
149 rp = dev->_risc_virt_addr;
151 /* sync instruction */
152 *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
154 for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
155 databuf_offset = frame_size * frame;
157 if (frame == 0) {
158 fifo_enable = 1;
159 risc_flag = RISC_CNT_RESET;
160 } else {
161 fifo_enable = 0;
162 risc_flag = RISC_CNT_INC;
165 /* Calculate physical jump address */
166 if ((frame + 1) == NUM_AUDIO_FRAMES) {
167 risc_phys_jump_addr =
168 dev->_risc_phys_start_addr +
169 RISC_SYNC_INSTRUCTION_SIZE;
170 } else {
171 risc_phys_jump_addr =
172 dev->_risc_phys_start_addr +
173 RISC_SYNC_INSTRUCTION_SIZE +
174 AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
177 rp = cx25821_risc_field_upstream_audio(dev, rp,
178 dev->
179 _audiodata_buf_phys_addr
180 + databuf_offset, bpl,
181 fifo_enable);
183 if (USE_RISC_NOOP_AUDIO) {
184 for (i = 0; i < NUM_NO_OPS; i++)
185 *(rp++) = cpu_to_le32(RISC_NOOP);
188 /* Loop to (Nth)FrameRISC or to Start of Risc program &
189 * generate IRQ */
190 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
191 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
192 *(rp++) = cpu_to_le32(0);
194 /* Recalculate virtual address based on frame index */
195 rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
196 (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
199 return 0;
202 void cx25821_free_memory_audio(struct cx25821_dev *dev)
204 if (dev->_risc_virt_addr) {
205 pci_free_consistent(dev->pci, dev->_audiorisc_size,
206 dev->_risc_virt_addr, dev->_risc_phys_addr);
207 dev->_risc_virt_addr = NULL;
210 if (dev->_audiodata_buf_virt_addr) {
211 pci_free_consistent(dev->pci, dev->_audiodata_buf_size,
212 dev->_audiodata_buf_virt_addr,
213 dev->_audiodata_buf_phys_addr);
214 dev->_audiodata_buf_virt_addr = NULL;
218 void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
220 struct sram_channel *sram_ch =
221 dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
222 u32 tmp = 0;
224 if (!dev->_audio_is_running) {
225 printk(KERN_DEBUG
226 pr_fmt("No audio file is currently running so return!\n"));
227 return;
229 /* Disable RISC interrupts */
230 cx_write(sram_ch->int_msk, 0);
232 /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
233 tmp = cx_read(sram_ch->dma_ctl);
234 cx_write(sram_ch->dma_ctl,
235 tmp & ~(sram_ch->fld_aud_fifo_en | sram_ch->fld_aud_risc_en));
237 /* Clear data buffer memory */
238 if (dev->_audiodata_buf_virt_addr)
239 memset(dev->_audiodata_buf_virt_addr, 0,
240 dev->_audiodata_buf_size);
242 dev->_audio_is_running = 0;
243 dev->_is_first_audio_frame = 0;
244 dev->_audioframe_count = 0;
245 dev->_audiofile_status = END_OF_FILE;
247 if (dev->_irq_audio_queues) {
248 kfree(dev->_irq_audio_queues);
249 dev->_irq_audio_queues = NULL;
252 if (dev->_audiofilename != NULL)
253 kfree(dev->_audiofilename);
256 void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
258 if (dev->_audio_is_running)
259 cx25821_stop_upstream_audio(dev);
261 cx25821_free_memory_audio(dev);
264 int cx25821_get_audio_data(struct cx25821_dev *dev,
265 struct sram_channel *sram_ch)
267 struct file *myfile;
268 int frame_index_temp = dev->_audioframe_index;
269 int i = 0;
270 int line_size = AUDIO_LINE_SIZE;
271 int frame_size = AUDIO_DATA_BUF_SZ;
272 int frame_offset = frame_size * frame_index_temp;
273 ssize_t vfs_read_retval = 0;
274 char mybuf[line_size];
275 loff_t file_offset = dev->_audioframe_count * frame_size;
276 loff_t pos;
277 mm_segment_t old_fs;
279 if (dev->_audiofile_status == END_OF_FILE)
280 return 0;
282 myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
284 if (IS_ERR(myfile)) {
285 const int open_errno = -PTR_ERR(myfile);
286 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
287 __func__, dev->_audiofilename, open_errno);
288 return PTR_ERR(myfile);
289 } else {
290 if (!(myfile->f_op)) {
291 pr_err("%s(): File has no file operations registered!\n",
292 __func__);
293 filp_close(myfile, NULL);
294 return -EIO;
297 if (!myfile->f_op->read) {
298 pr_err("%s(): File has no READ operations registered!\n",
299 __func__);
300 filp_close(myfile, NULL);
301 return -EIO;
304 pos = myfile->f_pos;
305 old_fs = get_fs();
306 set_fs(KERNEL_DS);
308 for (i = 0; i < dev->_audio_lines_count; i++) {
309 pos = file_offset;
311 vfs_read_retval =
312 vfs_read(myfile, mybuf, line_size, &pos);
314 if (vfs_read_retval > 0 && vfs_read_retval == line_size
315 && dev->_audiodata_buf_virt_addr != NULL) {
316 memcpy((void *)(dev->_audiodata_buf_virt_addr +
317 frame_offset / 4), mybuf,
318 vfs_read_retval);
321 file_offset += vfs_read_retval;
322 frame_offset += vfs_read_retval;
324 if (vfs_read_retval < line_size) {
325 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
326 __func__);
327 break;
331 if (i > 0)
332 dev->_audioframe_count++;
334 dev->_audiofile_status =
335 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
337 set_fs(old_fs);
338 filp_close(myfile, NULL);
341 return 0;
344 static void cx25821_audioups_handler(struct work_struct *work)
346 struct cx25821_dev *dev =
347 container_of(work, struct cx25821_dev, _audio_work_entry);
349 if (!dev) {
350 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
351 __func__);
352 return;
355 cx25821_get_audio_data(dev,
356 dev->channels[dev->
357 _audio_upstream_channel_select].
358 sram_channels);
361 int cx25821_openfile_audio(struct cx25821_dev *dev,
362 struct sram_channel *sram_ch)
364 struct file *myfile;
365 int i = 0, j = 0;
366 int line_size = AUDIO_LINE_SIZE;
367 ssize_t vfs_read_retval = 0;
368 char mybuf[line_size];
369 loff_t pos;
370 loff_t offset = (unsigned long)0;
371 mm_segment_t old_fs;
373 myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
375 if (IS_ERR(myfile)) {
376 const int open_errno = -PTR_ERR(myfile);
377 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
378 __func__, dev->_audiofilename, open_errno);
379 return PTR_ERR(myfile);
380 } else {
381 if (!(myfile->f_op)) {
382 pr_err("%s(): File has no file operations registered!\n",
383 __func__);
384 filp_close(myfile, NULL);
385 return -EIO;
388 if (!myfile->f_op->read) {
389 pr_err("%s(): File has no READ operations registered!\n",
390 __func__);
391 filp_close(myfile, NULL);
392 return -EIO;
395 pos = myfile->f_pos;
396 old_fs = get_fs();
397 set_fs(KERNEL_DS);
399 for (j = 0; j < NUM_AUDIO_FRAMES; j++) {
400 for (i = 0; i < dev->_audio_lines_count; i++) {
401 pos = offset;
403 vfs_read_retval =
404 vfs_read(myfile, mybuf, line_size, &pos);
406 if (vfs_read_retval > 0
407 && vfs_read_retval == line_size
408 && dev->_audiodata_buf_virt_addr != NULL) {
409 memcpy((void *)(dev->
410 _audiodata_buf_virt_addr
411 + offset / 4), mybuf,
412 vfs_read_retval);
415 offset += vfs_read_retval;
417 if (vfs_read_retval < line_size) {
418 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
419 __func__);
420 break;
424 if (i > 0)
425 dev->_audioframe_count++;
427 if (vfs_read_retval < line_size)
428 break;
431 dev->_audiofile_status =
432 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
434 set_fs(old_fs);
435 myfile->f_pos = 0;
436 filp_close(myfile, NULL);
439 return 0;
442 static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
443 struct sram_channel *sram_ch,
444 int bpl)
446 int ret = 0;
447 dma_addr_t dma_addr;
448 dma_addr_t data_dma_addr;
450 cx25821_free_memory_audio(dev);
452 dev->_risc_virt_addr =
453 pci_alloc_consistent(dev->pci, dev->audio_upstream_riscbuf_size,
454 &dma_addr);
455 dev->_risc_virt_start_addr = dev->_risc_virt_addr;
456 dev->_risc_phys_start_addr = dma_addr;
457 dev->_risc_phys_addr = dma_addr;
458 dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
460 if (!dev->_risc_virt_addr) {
461 printk(KERN_DEBUG
462 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
463 return -ENOMEM;
465 /* Clear out memory at address */
466 memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
468 /* For Audio Data buffer allocation */
469 dev->_audiodata_buf_virt_addr =
470 pci_alloc_consistent(dev->pci, dev->audio_upstream_databuf_size,
471 &data_dma_addr);
472 dev->_audiodata_buf_phys_addr = data_dma_addr;
473 dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
475 if (!dev->_audiodata_buf_virt_addr) {
476 printk(KERN_DEBUG
477 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
478 return -ENOMEM;
480 /* Clear out memory at address */
481 memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
483 ret = cx25821_openfile_audio(dev, sram_ch);
484 if (ret < 0)
485 return ret;
487 /* Creating RISC programs */
488 ret =
489 cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
490 dev->_audio_lines_count);
491 if (ret < 0) {
492 printk(KERN_DEBUG
493 pr_fmt("ERROR creating audio upstream RISC programs!\n"));
494 goto error;
497 return 0;
499 error:
500 return ret;
503 int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
504 u32 status)
506 int i = 0;
507 u32 int_msk_tmp;
508 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
509 dma_addr_t risc_phys_jump_addr;
510 __le32 *rp;
512 if (status & FLD_AUD_SRC_RISCI1) {
513 /* Get interrupt_index of the program that interrupted */
514 u32 prog_cnt = cx_read(channel->gpcnt);
516 /* Since we've identified our IRQ, clear our bits from the
517 * interrupt mask and interrupt status registers */
518 cx_write(channel->int_msk, 0);
519 cx_write(channel->int_stat, cx_read(channel->int_stat));
521 spin_lock(&dev->slock);
523 while (prog_cnt != dev->_last_index_irq) {
524 /* Update _last_index_irq */
525 if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
526 dev->_last_index_irq++;
527 else
528 dev->_last_index_irq = 0;
530 dev->_audioframe_index = dev->_last_index_irq;
532 queue_work(dev->_irq_audio_queues,
533 &dev->_audio_work_entry);
536 if (dev->_is_first_audio_frame) {
537 dev->_is_first_audio_frame = 0;
539 if (dev->_risc_virt_start_addr != NULL) {
540 risc_phys_jump_addr =
541 dev->_risc_phys_start_addr +
542 RISC_SYNC_INSTRUCTION_SIZE +
543 AUDIO_RISC_DMA_BUF_SIZE;
545 rp = cx25821_risc_field_upstream_audio(dev,
546 dev->
547 _risc_virt_start_addr
548 + 1,
549 dev->
550 _audiodata_buf_phys_addr,
551 AUDIO_LINE_SIZE,
552 FIFO_DISABLE);
554 if (USE_RISC_NOOP_AUDIO) {
555 for (i = 0; i < NUM_NO_OPS; i++) {
556 *(rp++) =
557 cpu_to_le32(RISC_NOOP);
560 /* Jump to 2nd Audio Frame */
561 *(rp++) =
562 cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
563 RISC_CNT_RESET);
564 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
565 *(rp++) = cpu_to_le32(0);
569 spin_unlock(&dev->slock);
570 } else {
571 if (status & FLD_AUD_SRC_OF)
572 pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
573 __func__);
575 if (status & FLD_AUD_SRC_SYNC)
576 pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
577 __func__);
579 if (status & FLD_AUD_SRC_OPC_ERR)
580 pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
581 __func__);
583 /* Read and write back the interrupt status register to clear
584 * our bits */
585 cx_write(channel->int_stat, cx_read(channel->int_stat));
588 if (dev->_audiofile_status == END_OF_FILE) {
589 pr_warn("EOF Channel Audio Framecount = %d\n",
590 dev->_audioframe_count);
591 return -1;
593 /* ElSE, set the interrupt mask register, re-enable irq. */
594 int_msk_tmp = cx_read(channel->int_msk);
595 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
597 return 0;
600 static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
602 struct cx25821_dev *dev = dev_id;
603 u32 msk_stat, audio_status;
604 int handled = 0;
605 struct sram_channel *sram_ch;
607 if (!dev)
608 return -1;
610 sram_ch = dev->channels[dev->_audio_upstream_channel_select].sram_channels;
612 msk_stat = cx_read(sram_ch->int_mstat);
613 audio_status = cx_read(sram_ch->int_stat);
615 /* Only deal with our interrupt */
616 if (audio_status) {
617 handled =
618 cx25821_audio_upstream_irq(dev,
619 dev->
620 _audio_upstream_channel_select,
621 audio_status);
624 if (handled < 0)
625 cx25821_stop_upstream_audio(dev);
626 else
627 handled += handled;
629 return IRQ_RETVAL(handled);
632 static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
633 struct sram_channel *sram_ch)
635 int count = 0;
636 u32 tmp;
638 do {
639 /* Wait 10 microsecond before checking to see if the FIFO is
640 * turned ON. */
641 udelay(10);
643 tmp = cx_read(sram_ch->dma_ctl);
645 /* 10 millisecond timeout */
646 if (count++ > 1000) {
647 pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
648 __func__);
649 return;
652 } while (!(tmp & sram_ch->fld_aud_fifo_en));
656 int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
657 struct sram_channel *sram_ch)
659 u32 tmp = 0;
660 int err = 0;
662 /* Set the physical start address of the RISC program in the initial
663 * program counter(IPC) member of the CMDS. */
664 cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
665 /* Risc IPC High 64 bits 63-32 */
666 cx_write(sram_ch->cmds_start + 4, 0);
668 /* reset counter */
669 cx_write(sram_ch->gpcnt_ctl, 3);
671 /* Set the line length (It looks like we do not need to set the
672 * line length) */
673 cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
675 /* Set the input mode to 16-bit */
676 tmp = cx_read(sram_ch->aud_cfg);
677 tmp |=
678 FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
679 FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D | FLD_AUD_SONY_MODE;
680 cx_write(sram_ch->aud_cfg, tmp);
682 /* Read and write back the interrupt status register to clear it */
683 tmp = cx_read(sram_ch->int_stat);
684 cx_write(sram_ch->int_stat, tmp);
686 /* Clear our bits from the interrupt status register. */
687 cx_write(sram_ch->int_stat, _intr_msk);
689 /* Set the interrupt mask register, enable irq. */
690 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
691 tmp = cx_read(sram_ch->int_msk);
692 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
694 err =
695 request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
696 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
697 if (err < 0) {
698 pr_err("%s: can't get upstream IRQ %d\n",
699 dev->name, dev->pci->irq);
700 goto fail_irq;
703 /* Start the DMA engine */
704 tmp = cx_read(sram_ch->dma_ctl);
705 cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
707 dev->_audio_is_running = 1;
708 dev->_is_first_audio_frame = 1;
710 /* The fifo_en bit turns on by the first Risc program */
711 cx25821_wait_fifo_enable(dev, sram_ch);
713 return 0;
715 fail_irq:
716 cx25821_dev_unregister(dev);
717 return err;
720 int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
722 struct sram_channel *sram_ch;
723 int retval = 0;
724 int err = 0;
725 int str_length = 0;
727 if (dev->_audio_is_running) {
728 pr_warn("Audio Channel is still running so return!\n");
729 return 0;
732 dev->_audio_upstream_channel_select = channel_select;
733 sram_ch = dev->channels[channel_select].sram_channels;
735 /* Work queue */
736 INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
737 dev->_irq_audio_queues =
738 create_singlethread_workqueue("cx25821_audioworkqueue");
740 if (!dev->_irq_audio_queues) {
741 printk(KERN_DEBUG
742 pr_fmt("ERROR: create_singlethread_workqueue() for Audio FAILED!\n"));
743 return -ENOMEM;
746 dev->_last_index_irq = 0;
747 dev->_audio_is_running = 0;
748 dev->_audioframe_count = 0;
749 dev->_audiofile_status = RESET_STATUS;
750 dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
751 _line_size = AUDIO_LINE_SIZE;
753 if (dev->input_audiofilename) {
754 str_length = strlen(dev->input_audiofilename);
755 dev->_audiofilename = kmalloc(str_length + 1, GFP_KERNEL);
757 if (!dev->_audiofilename)
758 goto error;
760 memcpy(dev->_audiofilename, dev->input_audiofilename,
761 str_length + 1);
763 /* Default if filename is empty string */
764 if (strcmp(dev->input_audiofilename, "") == 0)
765 dev->_audiofilename = "/root/audioGOOD.wav";
766 } else {
767 str_length = strlen(_defaultAudioName);
768 dev->_audiofilename = kmalloc(str_length + 1, GFP_KERNEL);
770 if (!dev->_audiofilename)
771 goto error;
773 memcpy(dev->_audiofilename, _defaultAudioName, str_length + 1);
776 retval =
777 cx25821_sram_channel_setup_upstream_audio(dev, sram_ch, _line_size,
780 dev->audio_upstream_riscbuf_size =
781 AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
782 RISC_SYNC_INSTRUCTION_SIZE;
783 dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
785 /* Allocating buffers and prepare RISC program */
786 retval =
787 cx25821_audio_upstream_buffer_prepare(dev, sram_ch, _line_size);
788 if (retval < 0) {
789 pr_err("%s: Failed to set up Audio upstream buffers!\n",
790 dev->name);
791 goto error;
793 /* Start RISC engine */
794 cx25821_start_audio_dma_upstream(dev, sram_ch);
796 return 0;
798 error:
799 cx25821_dev_unregister(dev);
801 return err;