1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include "cx25821-video.h"
22#include "cx25821-audio-upstream.h"
23
24#include <linux/fs.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>
35
36MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
37MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
38MODULE_LICENSE("GPL");
39
40static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
41 FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
42
43static int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
44 const struct sram_channel *ch,
45 unsigned int bpl, u32 risc)
46{
47 unsigned int i, lines;
48 u32 cdt;
49
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);
55 return 0;
56 }
57
58 bpl = (bpl + 7) & ~7;
59 cdt = ch->cdt;
60 lines = ch->fifo_size / bpl;
61
62 if (lines > 3)
63 lines = 3;
64
65 BUG_ON(lines < 2);
66
67
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);
73 }
74
75
76 cx_write(ch->cmds_start + 0, risc);
77
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);
82
83
84 cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);
85
86 for (i = 24; i < 80; i += 4)
87 cx_write(ch->cmds_start + i, 0);
88
89
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);
94
95 return 0;
96}
97
98static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
99 __le32 *rp,
100 dma_addr_t databuf_phys_addr,
101 unsigned int bpl,
102 int fifo_enable)
103{
104 unsigned int line;
105 const struct sram_channel *sram_ch =
106 dev->channels[dev->_audio_upstream_channel].sram_channels;
107 int offset = 0;
108
109
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);
114
115
116
117
118
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;
124 }
125
126 offset += AUDIO_LINE_SIZE;
127 }
128
129 return rp;
130}
131
132static int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
133 struct pci_dev *pci,
134 unsigned int bpl, unsigned int lines)
135{
136 __le32 *rp;
137 int fifo_enable = 0;
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;
143
144
145 rp = dev->_risc_virt_addr;
146
147
148 *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
149
150 for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
151 databuf_offset = frame_size * frame;
152
153 if (frame == 0) {
154 fifo_enable = 1;
155 risc_flag = RISC_CNT_RESET;
156 } else {
157 fifo_enable = 0;
158 risc_flag = RISC_CNT_INC;
159 }
160
161
162 if ((frame + 1) == NUM_AUDIO_FRAMES) {
163 risc_phys_jump_addr =
164 dev->_risc_phys_start_addr +
165 RISC_SYNC_INSTRUCTION_SIZE;
166 } else {
167 risc_phys_jump_addr =
168 dev->_risc_phys_start_addr +
169 RISC_SYNC_INSTRUCTION_SIZE +
170 AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
171 }
172
173 rp = cx25821_risc_field_upstream_audio(dev, rp,
174 dev->_audiodata_buf_phys_addr + databuf_offset,
175 bpl, fifo_enable);
176
177 if (USE_RISC_NOOP_AUDIO) {
178 for (i = 0; i < NUM_NO_OPS; i++)
179 *(rp++) = cpu_to_le32(RISC_NOOP);
180 }
181
182
183
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);
187
188
189 rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
190 (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
191 }
192
193 return 0;
194}
195
196static void cx25821_free_memory_audio(struct cx25821_dev *dev)
197{
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;
202 }
203
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;
209 }
210}
211
212void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
213{
214 const struct sram_channel *sram_ch =
215 dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
216 u32 tmp = 0;
217
218 if (!dev->_audio_is_running) {
219 printk(KERN_DEBUG
220 pr_fmt("No audio file is currently running so return!\n"));
221 return;
222 }
223
224 cx_write(sram_ch->int_msk, 0);
225
226
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));
230
231
232 if (dev->_audiodata_buf_virt_addr)
233 memset(dev->_audiodata_buf_virt_addr, 0,
234 dev->_audiodata_buf_size);
235
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;
240
241 flush_work(&dev->_audio_work_entry);
242
243 kfree(dev->_audiofilename);
244}
245
246void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
247{
248 if (dev->_audio_is_running)
249 cx25821_stop_upstream_audio(dev);
250
251 cx25821_free_memory_audio(dev);
252}
253
254static int cx25821_get_audio_data(struct cx25821_dev *dev,
255 const struct sram_channel *sram_ch)
256{
257 struct file *file;
258 int frame_index_temp = dev->_audioframe_index;
259 int i = 0;
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;
264 char *p = NULL;
265
266 if (dev->_audiofile_status == END_OF_FILE)
267 return 0;
268
269 file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
270 if (IS_ERR(file)) {
271 pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
272 __func__, dev->_audiofilename, -PTR_ERR(file));
273 return PTR_ERR(file);
274 }
275
276 if (dev->_audiodata_buf_virt_addr)
277 p = (char *)dev->_audiodata_buf_virt_addr + frame_offset;
278
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",
283 __func__);
284 dev->_audiofile_status = END_OF_FILE;
285 fput(file);
286 return 0;
287 }
288 dev->_audiofile_status = IN_PROGRESS;
289 if (p) {
290 memcpy(p, mybuf, n);
291 p += n;
292 }
293 }
294 dev->_audioframe_count++;
295 fput(file);
296
297 return 0;
298}
299
300static void cx25821_audioups_handler(struct work_struct *work)
301{
302 struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
303 _audio_work_entry);
304
305 if (!dev) {
306 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
307 __func__);
308 return;
309 }
310
311 cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
312 sram_channels);
313}
314
315static int cx25821_openfile_audio(struct cx25821_dev *dev,
316 const struct sram_channel *sram_ch)
317{
318 char *p = (void *)dev->_audiodata_buf_virt_addr;
319 struct file *file;
320 loff_t file_offset = 0;
321 int i, j;
322
323 file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
324 if (IS_ERR(file)) {
325 pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
326 __func__, dev->_audiofilename, PTR_ERR(file));
327 return PTR_ERR(file);
328 }
329
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);
335
336 if (n < AUDIO_LINE_SIZE) {
337 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
338 __func__);
339 dev->_audiofile_status = END_OF_FILE;
340 fput(file);
341 return 0;
342 }
343
344 if (p)
345 memcpy(p + offset, buf, n);
346 }
347 dev->_audioframe_count++;
348 }
349 dev->_audiofile_status = IN_PROGRESS;
350 fput(file);
351 return 0;
352}
353
354static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
355 const struct sram_channel *sram_ch,
356 int bpl)
357{
358 int ret = 0;
359 dma_addr_t dma_addr;
360 dma_addr_t data_dma_addr;
361
362 cx25821_free_memory_audio(dev);
363
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;
370
371 if (!dev->_risc_virt_addr) {
372 printk(KERN_DEBUG
373 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
374 return -ENOMEM;
375 }
376
377 memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
378
379
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;
384
385 if (!dev->_audiodata_buf_virt_addr) {
386 printk(KERN_DEBUG
387 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
388 return -ENOMEM;
389 }
390
391 memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
392
393 ret = cx25821_openfile_audio(dev, sram_ch);
394 if (ret < 0)
395 return ret;
396
397
398 ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
399 dev->_audio_lines_count);
400 if (ret < 0) {
401 printk(KERN_DEBUG
402 pr_fmt("ERROR creating audio upstream RISC programs!\n"));
403 goto error;
404 }
405
406 return 0;
407
408error:
409 return ret;
410}
411
412static int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
413 u32 status)
414{
415 int i = 0;
416 u32 int_msk_tmp;
417 const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
418 dma_addr_t risc_phys_jump_addr;
419 __le32 *rp;
420
421 if (status & FLD_AUD_SRC_RISCI1) {
422
423 u32 prog_cnt = cx_read(channel->gpcnt);
424
425
426
427 cx_write(channel->int_msk, 0);
428 cx_write(channel->int_stat, cx_read(channel->int_stat));
429
430 spin_lock(&dev->slock);
431
432 while (prog_cnt != dev->_last_index_irq) {
433
434 if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
435 dev->_last_index_irq++;
436 else
437 dev->_last_index_irq = 0;
438
439 dev->_audioframe_index = dev->_last_index_irq;
440
441 schedule_work(&dev->_audio_work_entry);
442 }
443
444 if (dev->_is_first_audio_frame) {
445 dev->_is_first_audio_frame = 0;
446
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;
452
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);
457
458 if (USE_RISC_NOOP_AUDIO) {
459 for (i = 0; i < NUM_NO_OPS; i++) {
460 *(rp++) =
461 cpu_to_le32(RISC_NOOP);
462 }
463 }
464
465 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
466 RISC_CNT_RESET);
467 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
468 *(rp++) = cpu_to_le32(0);
469 }
470 }
471
472 spin_unlock(&dev->slock);
473 } else {
474 if (status & FLD_AUD_SRC_OF)
475 pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
476 __func__);
477
478 if (status & FLD_AUD_SRC_SYNC)
479 pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
480 __func__);
481
482 if (status & FLD_AUD_SRC_OPC_ERR)
483 pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
484 __func__);
485
486
487
488 cx_write(channel->int_stat, cx_read(channel->int_stat));
489 }
490
491 if (dev->_audiofile_status == END_OF_FILE) {
492 pr_warn("EOF Channel Audio Framecount = %d\n",
493 dev->_audioframe_count);
494 return -1;
495 }
496
497 int_msk_tmp = cx_read(channel->int_msk);
498 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
499
500 return 0;
501}
502
503static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
504{
505 struct cx25821_dev *dev = dev_id;
506 u32 audio_status;
507 int handled = 0;
508 const struct sram_channel *sram_ch;
509
510 if (!dev)
511 return -1;
512
513 sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
514
515 audio_status = cx_read(sram_ch->int_stat);
516
517
518 if (audio_status) {
519 handled = cx25821_audio_upstream_irq(dev,
520 dev->_audio_upstream_channel, audio_status);
521 }
522
523 if (handled < 0)
524 cx25821_stop_upstream_audio(dev);
525 else
526 handled += handled;
527
528 return IRQ_RETVAL(handled);
529}
530
531static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
532 const struct sram_channel *sram_ch)
533{
534 int count = 0;
535 u32 tmp;
536
537 do {
538
539
540 udelay(10);
541
542 tmp = cx_read(sram_ch->dma_ctl);
543
544
545 if (count++ > 1000) {
546 pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
547 __func__);
548 return;
549 }
550
551 } while (!(tmp & sram_ch->fld_aud_fifo_en));
552
553}
554
555static int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
556 const struct sram_channel *sram_ch)
557{
558 u32 tmp = 0;
559 int err = 0;
560
561
562
563 cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
564
565 cx_write(sram_ch->cmds_start + 4, 0);
566
567
568 cx_write(sram_ch->gpcnt_ctl, 3);
569
570
571
572 cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
573
574
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 |
578 FLD_AUD_SONY_MODE;
579 cx_write(sram_ch->aud_cfg, tmp);
580
581
582 tmp = cx_read(sram_ch->int_stat);
583 cx_write(sram_ch->int_stat, tmp);
584
585
586 cx_write(sram_ch->int_stat, _intr_msk);
587
588
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);
592
593 err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
594 IRQF_SHARED, dev->name, dev);
595 if (err < 0) {
596 pr_err("%s: can't get upstream IRQ %d\n", dev->name,
597 dev->pci->irq);
598 goto fail_irq;
599 }
600
601
602 tmp = cx_read(sram_ch->dma_ctl);
603 cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
604
605 dev->_audio_is_running = 1;
606 dev->_is_first_audio_frame = 1;
607
608
609 cx25821_wait_fifo_enable(dev, sram_ch);
610
611 return 0;
612
613fail_irq:
614 cx25821_dev_unregister(dev);
615 return err;
616}
617
618int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
619{
620 const struct sram_channel *sram_ch;
621 int err = 0;
622
623 if (dev->_audio_is_running) {
624 pr_warn("Audio Channel is still running so return!\n");
625 return 0;
626 }
627
628 dev->_audio_upstream_channel = channel_select;
629 sram_ch = dev->channels[channel_select].sram_channels;
630
631
632 INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
633
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;
640
641 if ((dev->input_audiofilename) &&
642 (strcmp(dev->input_audiofilename, "") != 0))
643 dev->_audiofilename = kstrdup(dev->input_audiofilename,
644 GFP_KERNEL);
645 else
646 dev->_audiofilename = kstrdup(_defaultAudioName,
647 GFP_KERNEL);
648
649 if (!dev->_audiofilename) {
650 err = -ENOMEM;
651 goto error;
652 }
653
654 cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
655 _line_size, 0);
656
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;
661
662
663 err = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
664 _line_size);
665 if (err < 0) {
666 pr_err("%s: Failed to set up Audio upstream buffers!\n",
667 dev->name);
668 goto error;
669 }
670
671 cx25821_start_audio_dma_upstream(dev, sram_ch);
672
673 return 0;
674
675error:
676 cx25821_dev_unregister(dev);
677
678 return err;
679}
680