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.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include "cx25821-video.h"
22 #include "cx25821-audio-upstream.h"
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/syscalls.h>
30 #include <linux/file.h>
31 #include <linux/fcntl.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
36 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
37 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
38 MODULE_LICENSE("GPL");
40 static int _intr_msk
= FLD_AUD_SRC_RISCI1
| FLD_AUD_SRC_OF
|
41 FLD_AUD_SRC_SYNC
| FLD_AUD_SRC_OPC_ERR
;
43 static int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev
*dev
,
44 const struct sram_channel
*ch
,
45 unsigned int bpl
, u32 risc
)
47 unsigned int i
, lines
;
50 if (ch
->cmds_start
== 0) {
51 cx_write(ch
->ptr1_reg
, 0);
52 cx_write(ch
->ptr2_reg
, 0);
53 cx_write(ch
->cnt2_reg
, 0);
54 cx_write(ch
->cnt1_reg
, 0);
58 bpl
= (bpl
+ 7) & ~7; /* alignment */
60 lines
= ch
->fifo_size
/ bpl
;
68 for (i
= 0; i
< lines
; i
++) {
69 cx_write(cdt
+ 16 * i
, ch
->fifo_start
+ bpl
* i
);
70 cx_write(cdt
+ 16 * i
+ 4, 0);
71 cx_write(cdt
+ 16 * i
+ 8, 0);
72 cx_write(cdt
+ 16 * i
+ 12, 0);
76 cx_write(ch
->cmds_start
+ 0, risc
);
78 cx_write(ch
->cmds_start
+ 4, 0);
79 cx_write(ch
->cmds_start
+ 8, cdt
);
80 cx_write(ch
->cmds_start
+ 12, AUDIO_CDT_SIZE_QW
);
81 cx_write(ch
->cmds_start
+ 16, ch
->ctrl_start
);
84 cx_write(ch
->cmds_start
+ 20, AUDIO_IQ_SIZE_DW
);
86 for (i
= 24; i
< 80; i
+= 4)
87 cx_write(ch
->cmds_start
+ i
, 0);
90 cx_write(ch
->ptr1_reg
, ch
->fifo_start
);
91 cx_write(ch
->ptr2_reg
, cdt
);
92 cx_write(ch
->cnt2_reg
, AUDIO_CDT_SIZE_QW
);
93 cx_write(ch
->cnt1_reg
, AUDIO_CLUSTER_SIZE_QW
- 1);
98 static __le32
*cx25821_risc_field_upstream_audio(struct cx25821_dev
*dev
,
100 dma_addr_t databuf_phys_addr
,
105 const struct sram_channel
*sram_ch
=
106 dev
->channels
[dev
->_audio_upstream_channel
].sram_channels
;
110 for (line
= 0; line
< LINES_PER_AUDIO_BUFFER
; line
++) {
111 *(rp
++) = cpu_to_le32(RISC_READ
| RISC_SOL
| RISC_EOL
| bpl
);
112 *(rp
++) = cpu_to_le32(databuf_phys_addr
+ offset
);
113 *(rp
++) = cpu_to_le32(0); /* bits 63-32 */
115 /* Check if we need to enable the FIFO
116 * after the first 3 lines.
117 * For the upstream audio channel,
118 * the risc engine will enable the FIFO */
119 if (fifo_enable
&& line
== 2) {
120 *(rp
++) = RISC_WRITECR
;
121 *(rp
++) = sram_ch
->dma_ctl
;
122 *(rp
++) = sram_ch
->fld_aud_fifo_en
;
123 *(rp
++) = 0x00000020;
126 offset
+= AUDIO_LINE_SIZE
;
132 static int cx25821_risc_buffer_upstream_audio(struct cx25821_dev
*dev
,
134 unsigned int bpl
, unsigned int lines
)
138 int frame
= 0, i
= 0;
139 int frame_size
= AUDIO_DATA_BUF_SZ
;
140 int databuf_offset
= 0;
141 int risc_flag
= RISC_CNT_INC
;
142 dma_addr_t risc_phys_jump_addr
;
144 /* Virtual address of Risc buffer program */
145 rp
= dev
->_risc_virt_addr
;
147 /* sync instruction */
148 *(rp
++) = cpu_to_le32(RISC_RESYNC
| AUDIO_SYNC_LINE
);
150 for (frame
= 0; frame
< NUM_AUDIO_FRAMES
; frame
++) {
151 databuf_offset
= frame_size
* frame
;
155 risc_flag
= RISC_CNT_RESET
;
158 risc_flag
= RISC_CNT_INC
;
161 /* Calculate physical jump address */
162 if ((frame
+ 1) == NUM_AUDIO_FRAMES
) {
163 risc_phys_jump_addr
=
164 dev
->_risc_phys_start_addr
+
165 RISC_SYNC_INSTRUCTION_SIZE
;
167 risc_phys_jump_addr
=
168 dev
->_risc_phys_start_addr
+
169 RISC_SYNC_INSTRUCTION_SIZE
+
170 AUDIO_RISC_DMA_BUF_SIZE
* (frame
+ 1);
173 rp
= cx25821_risc_field_upstream_audio(dev
, rp
,
174 dev
->_audiodata_buf_phys_addr
+ databuf_offset
,
177 if (USE_RISC_NOOP_AUDIO
) {
178 for (i
= 0; i
< NUM_NO_OPS
; i
++)
179 *(rp
++) = cpu_to_le32(RISC_NOOP
);
182 /* Loop to (Nth)FrameRISC or to Start of Risc program &
184 *(rp
++) = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| risc_flag
);
185 *(rp
++) = cpu_to_le32(risc_phys_jump_addr
);
186 *(rp
++) = cpu_to_le32(0);
188 /* Recalculate virtual address based on frame index */
189 rp
= dev
->_risc_virt_addr
+ RISC_SYNC_INSTRUCTION_SIZE
/ 4 +
190 (AUDIO_RISC_DMA_BUF_SIZE
* (frame
+ 1) / 4);
196 static void cx25821_free_memory_audio(struct cx25821_dev
*dev
)
198 if (dev
->_risc_virt_addr
) {
199 pci_free_consistent(dev
->pci
, dev
->_audiorisc_size
,
200 dev
->_risc_virt_addr
, dev
->_risc_phys_addr
);
201 dev
->_risc_virt_addr
= NULL
;
204 if (dev
->_audiodata_buf_virt_addr
) {
205 pci_free_consistent(dev
->pci
, dev
->_audiodata_buf_size
,
206 dev
->_audiodata_buf_virt_addr
,
207 dev
->_audiodata_buf_phys_addr
);
208 dev
->_audiodata_buf_virt_addr
= NULL
;
212 void cx25821_stop_upstream_audio(struct cx25821_dev
*dev
)
214 const struct sram_channel
*sram_ch
=
215 dev
->channels
[AUDIO_UPSTREAM_SRAM_CHANNEL_B
].sram_channels
;
218 if (!dev
->_audio_is_running
) {
220 pr_fmt("No audio file is currently running so return!\n"));
223 /* Disable RISC interrupts */
224 cx_write(sram_ch
->int_msk
, 0);
226 /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
227 tmp
= cx_read(sram_ch
->dma_ctl
);
228 cx_write(sram_ch
->dma_ctl
,
229 tmp
& ~(sram_ch
->fld_aud_fifo_en
| sram_ch
->fld_aud_risc_en
));
231 /* Clear data buffer memory */
232 if (dev
->_audiodata_buf_virt_addr
)
233 memset(dev
->_audiodata_buf_virt_addr
, 0,
234 dev
->_audiodata_buf_size
);
236 dev
->_audio_is_running
= 0;
237 dev
->_is_first_audio_frame
= 0;
238 dev
->_audioframe_count
= 0;
239 dev
->_audiofile_status
= END_OF_FILE
;
241 flush_work(&dev
->_audio_work_entry
);
243 kfree(dev
->_audiofilename
);
246 void cx25821_free_mem_upstream_audio(struct cx25821_dev
*dev
)
248 if (dev
->_audio_is_running
)
249 cx25821_stop_upstream_audio(dev
);
251 cx25821_free_memory_audio(dev
);
254 static int cx25821_get_audio_data(struct cx25821_dev
*dev
,
255 const struct sram_channel
*sram_ch
)
258 int frame_index_temp
= dev
->_audioframe_index
;
260 int frame_size
= AUDIO_DATA_BUF_SZ
;
261 int frame_offset
= frame_size
* frame_index_temp
;
262 char mybuf
[AUDIO_LINE_SIZE
];
263 loff_t file_offset
= dev
->_audioframe_count
* frame_size
;
266 if (dev
->_audiofile_status
== END_OF_FILE
)
269 file
= filp_open(dev
->_audiofilename
, O_RDONLY
| O_LARGEFILE
, 0);
271 pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
272 __func__
, dev
->_audiofilename
, -PTR_ERR(file
));
273 return PTR_ERR(file
);
276 if (dev
->_audiodata_buf_virt_addr
)
277 p
= (char *)dev
->_audiodata_buf_virt_addr
+ frame_offset
;
279 for (i
= 0; i
< dev
->_audio_lines_count
; i
++) {
280 int n
= kernel_read(file
, mybuf
, AUDIO_LINE_SIZE
, &file_offset
);
281 if (n
< AUDIO_LINE_SIZE
) {
282 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
284 dev
->_audiofile_status
= END_OF_FILE
;
288 dev
->_audiofile_status
= IN_PROGRESS
;
294 dev
->_audioframe_count
++;
300 static void cx25821_audioups_handler(struct work_struct
*work
)
302 struct cx25821_dev
*dev
= container_of(work
, struct cx25821_dev
,
306 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
311 cx25821_get_audio_data(dev
, dev
->channels
[dev
->_audio_upstream_channel
].
315 static int cx25821_openfile_audio(struct cx25821_dev
*dev
,
316 const struct sram_channel
*sram_ch
)
318 char *p
= (void *)dev
->_audiodata_buf_virt_addr
;
320 loff_t file_offset
= 0;
323 file
= filp_open(dev
->_audiofilename
, O_RDONLY
| O_LARGEFILE
, 0);
325 pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
326 __func__
, dev
->_audiofilename
, PTR_ERR(file
));
327 return PTR_ERR(file
);
330 for (j
= 0; j
< NUM_AUDIO_FRAMES
; j
++) {
331 for (i
= 0; i
< dev
->_audio_lines_count
; i
++) {
332 char buf
[AUDIO_LINE_SIZE
];
333 loff_t offset
= file_offset
;
334 int n
= kernel_read(file
, buf
, AUDIO_LINE_SIZE
, &file_offset
);
336 if (n
< AUDIO_LINE_SIZE
) {
337 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
339 dev
->_audiofile_status
= END_OF_FILE
;
345 memcpy(p
+ offset
, buf
, n
);
347 dev
->_audioframe_count
++;
349 dev
->_audiofile_status
= IN_PROGRESS
;
354 static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev
*dev
,
355 const struct sram_channel
*sram_ch
,
360 dma_addr_t data_dma_addr
;
362 cx25821_free_memory_audio(dev
);
364 dev
->_risc_virt_addr
= pci_alloc_consistent(dev
->pci
,
365 dev
->audio_upstream_riscbuf_size
, &dma_addr
);
366 dev
->_risc_virt_start_addr
= dev
->_risc_virt_addr
;
367 dev
->_risc_phys_start_addr
= dma_addr
;
368 dev
->_risc_phys_addr
= dma_addr
;
369 dev
->_audiorisc_size
= dev
->audio_upstream_riscbuf_size
;
371 if (!dev
->_risc_virt_addr
) {
373 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
376 /* Clear out memory at address */
377 memset(dev
->_risc_virt_addr
, 0, dev
->_audiorisc_size
);
379 /* For Audio Data buffer allocation */
380 dev
->_audiodata_buf_virt_addr
= pci_alloc_consistent(dev
->pci
,
381 dev
->audio_upstream_databuf_size
, &data_dma_addr
);
382 dev
->_audiodata_buf_phys_addr
= data_dma_addr
;
383 dev
->_audiodata_buf_size
= dev
->audio_upstream_databuf_size
;
385 if (!dev
->_audiodata_buf_virt_addr
) {
387 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
390 /* Clear out memory at address */
391 memset(dev
->_audiodata_buf_virt_addr
, 0, dev
->_audiodata_buf_size
);
393 ret
= cx25821_openfile_audio(dev
, sram_ch
);
397 /* Creating RISC programs */
398 ret
= cx25821_risc_buffer_upstream_audio(dev
, dev
->pci
, bpl
,
399 dev
->_audio_lines_count
);
402 pr_fmt("ERROR creating audio upstream RISC programs!\n"));
412 static int cx25821_audio_upstream_irq(struct cx25821_dev
*dev
, int chan_num
,
417 const struct sram_channel
*channel
= dev
->channels
[chan_num
].sram_channels
;
418 dma_addr_t risc_phys_jump_addr
;
421 if (status
& FLD_AUD_SRC_RISCI1
) {
422 /* Get interrupt_index of the program that interrupted */
423 u32 prog_cnt
= cx_read(channel
->gpcnt
);
425 /* Since we've identified our IRQ, clear our bits from the
426 * interrupt mask and interrupt status registers */
427 cx_write(channel
->int_msk
, 0);
428 cx_write(channel
->int_stat
, cx_read(channel
->int_stat
));
430 spin_lock(&dev
->slock
);
432 while (prog_cnt
!= dev
->_last_index_irq
) {
433 /* Update _last_index_irq */
434 if (dev
->_last_index_irq
< (NUMBER_OF_PROGRAMS
- 1))
435 dev
->_last_index_irq
++;
437 dev
->_last_index_irq
= 0;
439 dev
->_audioframe_index
= dev
->_last_index_irq
;
441 schedule_work(&dev
->_audio_work_entry
);
444 if (dev
->_is_first_audio_frame
) {
445 dev
->_is_first_audio_frame
= 0;
447 if (dev
->_risc_virt_start_addr
!= NULL
) {
448 risc_phys_jump_addr
=
449 dev
->_risc_phys_start_addr
+
450 RISC_SYNC_INSTRUCTION_SIZE
+
451 AUDIO_RISC_DMA_BUF_SIZE
;
453 rp
= cx25821_risc_field_upstream_audio(dev
,
454 dev
->_risc_virt_start_addr
+ 1,
455 dev
->_audiodata_buf_phys_addr
,
456 AUDIO_LINE_SIZE
, FIFO_DISABLE
);
458 if (USE_RISC_NOOP_AUDIO
) {
459 for (i
= 0; i
< NUM_NO_OPS
; i
++) {
461 cpu_to_le32(RISC_NOOP
);
464 /* Jump to 2nd Audio Frame */
465 *(rp
++) = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
|
467 *(rp
++) = cpu_to_le32(risc_phys_jump_addr
);
468 *(rp
++) = cpu_to_le32(0);
472 spin_unlock(&dev
->slock
);
474 if (status
& FLD_AUD_SRC_OF
)
475 pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
478 if (status
& FLD_AUD_SRC_SYNC
)
479 pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
482 if (status
& FLD_AUD_SRC_OPC_ERR
)
483 pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
486 /* Read and write back the interrupt status register to clear
488 cx_write(channel
->int_stat
, cx_read(channel
->int_stat
));
491 if (dev
->_audiofile_status
== END_OF_FILE
) {
492 pr_warn("EOF Channel Audio Framecount = %d\n",
493 dev
->_audioframe_count
);
496 /* ElSE, set the interrupt mask register, re-enable irq. */
497 int_msk_tmp
= cx_read(channel
->int_msk
);
498 cx_write(channel
->int_msk
, int_msk_tmp
|= _intr_msk
);
503 static irqreturn_t
cx25821_upstream_irq_audio(int irq
, void *dev_id
)
505 struct cx25821_dev
*dev
= dev_id
;
508 const struct sram_channel
*sram_ch
;
513 sram_ch
= dev
->channels
[dev
->_audio_upstream_channel
].sram_channels
;
515 audio_status
= cx_read(sram_ch
->int_stat
);
517 /* Only deal with our interrupt */
519 handled
= cx25821_audio_upstream_irq(dev
,
520 dev
->_audio_upstream_channel
, audio_status
);
524 cx25821_stop_upstream_audio(dev
);
528 return IRQ_RETVAL(handled
);
531 static void cx25821_wait_fifo_enable(struct cx25821_dev
*dev
,
532 const struct sram_channel
*sram_ch
)
538 /* Wait 10 microsecond before checking to see if the FIFO is
542 tmp
= cx_read(sram_ch
->dma_ctl
);
544 /* 10 millisecond timeout */
545 if (count
++ > 1000) {
546 pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
551 } while (!(tmp
& sram_ch
->fld_aud_fifo_en
));
555 static int cx25821_start_audio_dma_upstream(struct cx25821_dev
*dev
,
556 const struct sram_channel
*sram_ch
)
561 /* Set the physical start address of the RISC program in the initial
562 * program counter(IPC) member of the CMDS. */
563 cx_write(sram_ch
->cmds_start
+ 0, dev
->_risc_phys_addr
);
564 /* Risc IPC High 64 bits 63-32 */
565 cx_write(sram_ch
->cmds_start
+ 4, 0);
568 cx_write(sram_ch
->gpcnt_ctl
, 3);
570 /* Set the line length (It looks like we do not need to set the
572 cx_write(sram_ch
->aud_length
, AUDIO_LINE_SIZE
& FLD_AUD_DST_LN_LNGTH
);
574 /* Set the input mode to 16-bit */
575 tmp
= cx_read(sram_ch
->aud_cfg
);
576 tmp
|= FLD_AUD_SRC_ENABLE
| FLD_AUD_DST_PK_MODE
| FLD_AUD_CLK_ENABLE
|
577 FLD_AUD_MASTER_MODE
| FLD_AUD_CLK_SELECT_PLL_D
|
579 cx_write(sram_ch
->aud_cfg
, tmp
);
581 /* Read and write back the interrupt status register to clear it */
582 tmp
= cx_read(sram_ch
->int_stat
);
583 cx_write(sram_ch
->int_stat
, tmp
);
585 /* Clear our bits from the interrupt status register. */
586 cx_write(sram_ch
->int_stat
, _intr_msk
);
588 /* Set the interrupt mask register, enable irq. */
589 cx_set(PCI_INT_MSK
, cx_read(PCI_INT_MSK
) | (1 << sram_ch
->irq_bit
));
590 tmp
= cx_read(sram_ch
->int_msk
);
591 cx_write(sram_ch
->int_msk
, tmp
|= _intr_msk
);
593 err
= request_irq(dev
->pci
->irq
, cx25821_upstream_irq_audio
,
594 IRQF_SHARED
, dev
->name
, dev
);
596 pr_err("%s: can't get upstream IRQ %d\n", dev
->name
,
601 /* Start the DMA engine */
602 tmp
= cx_read(sram_ch
->dma_ctl
);
603 cx_set(sram_ch
->dma_ctl
, tmp
| sram_ch
->fld_aud_risc_en
);
605 dev
->_audio_is_running
= 1;
606 dev
->_is_first_audio_frame
= 1;
608 /* The fifo_en bit turns on by the first Risc program */
609 cx25821_wait_fifo_enable(dev
, sram_ch
);
614 cx25821_dev_unregister(dev
);
618 int cx25821_audio_upstream_init(struct cx25821_dev
*dev
, int channel_select
)
620 const struct sram_channel
*sram_ch
;
623 if (dev
->_audio_is_running
) {
624 pr_warn("Audio Channel is still running so return!\n");
628 dev
->_audio_upstream_channel
= channel_select
;
629 sram_ch
= dev
->channels
[channel_select
].sram_channels
;
632 INIT_WORK(&dev
->_audio_work_entry
, cx25821_audioups_handler
);
634 dev
->_last_index_irq
= 0;
635 dev
->_audio_is_running
= 0;
636 dev
->_audioframe_count
= 0;
637 dev
->_audiofile_status
= RESET_STATUS
;
638 dev
->_audio_lines_count
= LINES_PER_AUDIO_BUFFER
;
639 _line_size
= AUDIO_LINE_SIZE
;
641 if ((dev
->input_audiofilename
) &&
642 (strcmp(dev
->input_audiofilename
, "") != 0))
643 dev
->_audiofilename
= kstrdup(dev
->input_audiofilename
,
646 dev
->_audiofilename
= kstrdup(_defaultAudioName
,
649 if (!dev
->_audiofilename
) {
654 cx25821_sram_channel_setup_upstream_audio(dev
, sram_ch
,
657 dev
->audio_upstream_riscbuf_size
=
658 AUDIO_RISC_DMA_BUF_SIZE
* NUM_AUDIO_PROGS
+
659 RISC_SYNC_INSTRUCTION_SIZE
;
660 dev
->audio_upstream_databuf_size
= AUDIO_DATA_BUF_SZ
* NUM_AUDIO_PROGS
;
662 /* Allocating buffers and prepare RISC program */
663 err
= cx25821_audio_upstream_buffer_prepare(dev
, sram_ch
,
666 pr_err("%s: Failed to set up Audio upstream buffers!\n",
670 /* Start RISC engine */
671 cx25821_start_audio_dma_upstream(dev
, sram_ch
);
676 cx25821_dev_unregister(dev
);