]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/pci/cx25821/cx25821-audio-upstream.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / media / pci / cx25821 / cx25821-audio-upstream.c
CommitLineData
1a9fc855
MCC
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6 *
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.
11 *
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
15 *
16 * GNU General Public License for more details.
1a9fc855
MCC
17 */
18
36d89f7d
JP
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
1a9fc855
MCC
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>
5a0e3ad6 33#include <linux/slab.h>
b5f11cc7 34#include <linux/uaccess.h>
1a9fc855
MCC
35
36MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
37MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
38MODULE_LICENSE("GPL");
39
f063a0c0
LT
40static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
41 FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
1a9fc855 42
dafc456c 43static int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
bfef0d35 44 const struct sram_channel *ch,
1a9fc855
MCC
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; /* alignment */
59 cdt = ch->cdt;
60 lines = ch->fifo_size / bpl;
61
b5f11cc7 62 if (lines > 3)
1a9fc855 63 lines = 3;
1a9fc855
MCC
64
65 BUG_ON(lines < 2);
66
67 /* write CDT */
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 /* write CMDS */
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
b5f11cc7 83 /* IQ size */
1a9fc855
MCC
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 /* fill registers */
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,
b5f11cc7 99 __le32 *rp,
1a9fc855
MCC
100 dma_addr_t databuf_phys_addr,
101 unsigned int bpl,
102 int fifo_enable)
103{
104 unsigned int line;
bfef0d35 105 const struct sram_channel *sram_ch =
81f4c8dd 106 dev->channels[dev->_audio_upstream_channel].sram_channels;
1a9fc855
MCC
107 int offset = 0;
108
109 /* scan lines */
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 */
114
b5f11cc7
MCC
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 */
1a9fc855
MCC
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
dafc456c 132static int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
1a9fc855
MCC
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 /* Virtual address of Risc buffer program */
145 rp = dev->_risc_virt_addr;
146
147 /* sync instruction */
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
b5f11cc7 161 /* Calculate physical jump address */
1a9fc855
MCC
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,
72a52fc3
LF
174 dev->_audiodata_buf_phys_addr + databuf_offset,
175 bpl, fifo_enable);
1a9fc855
MCC
176
177 if (USE_RISC_NOOP_AUDIO) {
b5f11cc7 178 for (i = 0; i < NUM_NO_OPS; i++)
1a9fc855 179 *(rp++) = cpu_to_le32(RISC_NOOP);
1a9fc855
MCC
180 }
181
b5f11cc7
MCC
182 /* Loop to (Nth)FrameRISC or to Start of Risc program &
183 * generate IRQ */
1a9fc855
MCC
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
b5f11cc7 188 /* Recalculate virtual address based on frame index */
1a9fc855 189 rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
81f4c8dd 190 (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
1a9fc855
MCC
191 }
192
193 return 0;
194}
195
dafc456c 196static void cx25821_free_memory_audio(struct cx25821_dev *dev)
1a9fc855
MCC
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{
bfef0d35 214 const struct sram_channel *sram_ch =
81f4c8dd 215 dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
1a9fc855
MCC
216 u32 tmp = 0;
217
218 if (!dev->_audio_is_running) {
b5f11cc7 219 printk(KERN_DEBUG
36d89f7d 220 pr_fmt("No audio file is currently running so return!\n"));
1a9fc855
MCC
221 return;
222 }
b5f11cc7 223 /* Disable RISC interrupts */
1a9fc855
MCC
224 cx_write(sram_ch->int_msk, 0);
225
b5f11cc7 226 /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
1a9fc855
MCC
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
b5f11cc7 231 /* Clear data buffer memory */
1a9fc855
MCC
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
072973ba 241 flush_work(&dev->_audio_work_entry);
1a9fc855 242
b0091780 243 kfree(dev->_audiofilename);
1a9fc855
MCC
244}
245
246void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
247{
b5f11cc7 248 if (dev->_audio_is_running)
1a9fc855 249 cx25821_stop_upstream_audio(dev);
1a9fc855
MCC
250
251 cx25821_free_memory_audio(dev);
252}
253
dafc456c 254static int cx25821_get_audio_data(struct cx25821_dev *dev,
bfef0d35 255 const struct sram_channel *sram_ch)
1a9fc855 256{
2ce8fce2 257 struct file *file;
1a9fc855
MCC
258 int frame_index_temp = dev->_audioframe_index;
259 int i = 0;
1a9fc855
MCC
260 int frame_size = AUDIO_DATA_BUF_SZ;
261 int frame_offset = frame_size * frame_index_temp;
2ce8fce2 262 char mybuf[AUDIO_LINE_SIZE];
1a9fc855 263 loff_t file_offset = dev->_audioframe_count * frame_size;
2ce8fce2 264 char *p = NULL;
1a9fc855
MCC
265
266 if (dev->_audiofile_status == END_OF_FILE)
267 return 0;
268
2ce8fce2
AV
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 }
1a9fc855 275
2ce8fce2
AV
276 if (dev->_audiodata_buf_virt_addr)
277 p = (char *)dev->_audiodata_buf_virt_addr + frame_offset;
1a9fc855 278
2ce8fce2
AV
279 for (i = 0; i < dev->_audio_lines_count; i++) {
280 int n = kernel_read(file, file_offset, mybuf, AUDIO_LINE_SIZE);
281 if (n < AUDIO_LINE_SIZE) {
282 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
81f4c8dd 283 __func__);
2ce8fce2
AV
284 dev->_audiofile_status = END_OF_FILE;
285 fput(file);
286 return 0;
1a9fc855 287 }
2ce8fce2
AV
288 dev->_audiofile_status = IN_PROGRESS;
289 if (p) {
290 memcpy(p, mybuf, n);
291 p += n;
1a9fc855 292 }
2ce8fce2 293 file_offset += n;
1a9fc855 294 }
2ce8fce2
AV
295 dev->_audioframe_count++;
296 fput(file);
1a9fc855
MCC
297
298 return 0;
299}
300
301static void cx25821_audioups_handler(struct work_struct *work)
302{
72a52fc3
LF
303 struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
304 _audio_work_entry);
1a9fc855
MCC
305
306 if (!dev) {
36d89f7d 307 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
81f4c8dd 308 __func__);
1a9fc855
MCC
309 return;
310 }
311
1531e889
LF
312 cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
313 sram_channels);
1a9fc855
MCC
314}
315
dafc456c 316static int cx25821_openfile_audio(struct cx25821_dev *dev,
bfef0d35 317 const struct sram_channel *sram_ch)
1a9fc855 318{
3af07613
AV
319 char *p = (void *)dev->_audiodata_buf_virt_addr;
320 struct file *file;
321 loff_t offset;
322 int i, j;
1a9fc855 323
3af07613
AV
324 file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
325 if (IS_ERR(file)) {
326 pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
327 __func__, dev->_audiofilename, PTR_ERR(file));
328 return PTR_ERR(file);
329 }
1a9fc855 330
3af07613
AV
331 for (j = 0, offset = 0; j < NUM_AUDIO_FRAMES; j++) {
332 for (i = 0; i < dev->_audio_lines_count; i++) {
333 char buf[AUDIO_LINE_SIZE];
334 int n = kernel_read(file, offset, buf,
335 AUDIO_LINE_SIZE);
1a9fc855 336
3af07613
AV
337 if (n < AUDIO_LINE_SIZE) {
338 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
339 __func__);
340 dev->_audiofile_status = END_OF_FILE;
341 fput(file);
342 return 0;
1a9fc855
MCC
343 }
344
3af07613
AV
345 if (p)
346 memcpy(p + offset, buf, n);
1a9fc855 347
3af07613 348 offset += n;
1a9fc855 349 }
3af07613 350 dev->_audioframe_count++;
1a9fc855 351 }
3af07613
AV
352 dev->_audiofile_status = IN_PROGRESS;
353 fput(file);
1a9fc855
MCC
354 return 0;
355}
356
357static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
bfef0d35 358 const struct sram_channel *sram_ch,
1a9fc855
MCC
359 int bpl)
360{
361 int ret = 0;
362 dma_addr_t dma_addr;
363 dma_addr_t data_dma_addr;
364
365 cx25821_free_memory_audio(dev);
366
72a52fc3
LF
367 dev->_risc_virt_addr = pci_alloc_consistent(dev->pci,
368 dev->audio_upstream_riscbuf_size, &dma_addr);
1a9fc855
MCC
369 dev->_risc_virt_start_addr = dev->_risc_virt_addr;
370 dev->_risc_phys_start_addr = dma_addr;
371 dev->_risc_phys_addr = dma_addr;
372 dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
373
374 if (!dev->_risc_virt_addr) {
b5f11cc7 375 printk(KERN_DEBUG
81f4c8dd 376 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
1a9fc855
MCC
377 return -ENOMEM;
378 }
b5f11cc7 379 /* Clear out memory at address */
1a9fc855
MCC
380 memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
381
b5f11cc7 382 /* For Audio Data buffer allocation */
72a52fc3
LF
383 dev->_audiodata_buf_virt_addr = pci_alloc_consistent(dev->pci,
384 dev->audio_upstream_databuf_size, &data_dma_addr);
1a9fc855
MCC
385 dev->_audiodata_buf_phys_addr = data_dma_addr;
386 dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
387
388 if (!dev->_audiodata_buf_virt_addr) {
b5f11cc7 389 printk(KERN_DEBUG
81f4c8dd 390 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
1a9fc855
MCC
391 return -ENOMEM;
392 }
b5f11cc7 393 /* Clear out memory at address */
1a9fc855
MCC
394 memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
395
396 ret = cx25821_openfile_audio(dev, sram_ch);
397 if (ret < 0)
398 return ret;
399
b5f11cc7 400 /* Creating RISC programs */
72a52fc3
LF
401 ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
402 dev->_audio_lines_count);
1a9fc855
MCC
403 if (ret < 0) {
404 printk(KERN_DEBUG
81f4c8dd 405 pr_fmt("ERROR creating audio upstream RISC programs!\n"));
1a9fc855
MCC
406 goto error;
407 }
408
409 return 0;
410
b5f11cc7 411error:
1a9fc855
MCC
412 return ret;
413}
414
dafc456c 415static int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
1a9fc855
MCC
416 u32 status)
417{
418 int i = 0;
419 u32 int_msk_tmp;
bfef0d35 420 const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
1a9fc855
MCC
421 dma_addr_t risc_phys_jump_addr;
422 __le32 *rp;
423
424 if (status & FLD_AUD_SRC_RISCI1) {
b5f11cc7 425 /* Get interrupt_index of the program that interrupted */
1a9fc855
MCC
426 u32 prog_cnt = cx_read(channel->gpcnt);
427
b5f11cc7
MCC
428 /* Since we've identified our IRQ, clear our bits from the
429 * interrupt mask and interrupt status registers */
1a9fc855
MCC
430 cx_write(channel->int_msk, 0);
431 cx_write(channel->int_stat, cx_read(channel->int_stat));
432
433 spin_lock(&dev->slock);
434
435 while (prog_cnt != dev->_last_index_irq) {
b5f11cc7
MCC
436 /* Update _last_index_irq */
437 if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
1a9fc855 438 dev->_last_index_irq++;
b5f11cc7 439 else
1a9fc855 440 dev->_last_index_irq = 0;
1a9fc855
MCC
441
442 dev->_audioframe_index = dev->_last_index_irq;
443
072973ba 444 schedule_work(&dev->_audio_work_entry);
1a9fc855
MCC
445 }
446
447 if (dev->_is_first_audio_frame) {
448 dev->_is_first_audio_frame = 0;
449
450 if (dev->_risc_virt_start_addr != NULL) {
451 risc_phys_jump_addr =
81f4c8dd
LF
452 dev->_risc_phys_start_addr +
453 RISC_SYNC_INSTRUCTION_SIZE +
454 AUDIO_RISC_DMA_BUF_SIZE;
1a9fc855
MCC
455
456 rp = cx25821_risc_field_upstream_audio(dev,
54082f1d
LF
457 dev->_risc_virt_start_addr + 1,
458 dev->_audiodata_buf_phys_addr,
459 AUDIO_LINE_SIZE, FIFO_DISABLE);
1a9fc855
MCC
460
461 if (USE_RISC_NOOP_AUDIO) {
462 for (i = 0; i < NUM_NO_OPS; i++) {
463 *(rp++) =
464 cpu_to_le32(RISC_NOOP);
465 }
466 }
b5f11cc7 467 /* Jump to 2nd Audio Frame */
1531e889 468 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
1a9fc855
MCC
469 RISC_CNT_RESET);
470 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
471 *(rp++) = cpu_to_le32(0);
472 }
473 }
474
475 spin_unlock(&dev->slock);
476 } else {
477 if (status & FLD_AUD_SRC_OF)
36d89f7d
JP
478 pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
479 __func__);
1a9fc855
MCC
480
481 if (status & FLD_AUD_SRC_SYNC)
36d89f7d
JP
482 pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
483 __func__);
1a9fc855
MCC
484
485 if (status & FLD_AUD_SRC_OPC_ERR)
36d89f7d
JP
486 pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
487 __func__);
1a9fc855 488
b5f11cc7
MCC
489 /* Read and write back the interrupt status register to clear
490 * our bits */
1a9fc855
MCC
491 cx_write(channel->int_stat, cx_read(channel->int_stat));
492 }
493
494 if (dev->_audiofile_status == END_OF_FILE) {
36d89f7d
JP
495 pr_warn("EOF Channel Audio Framecount = %d\n",
496 dev->_audioframe_count);
1a9fc855
MCC
497 return -1;
498 }
b5f11cc7 499 /* ElSE, set the interrupt mask register, re-enable irq. */
1a9fc855
MCC
500 int_msk_tmp = cx_read(channel->int_msk);
501 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
502
503 return 0;
504}
505
506static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
507{
508 struct cx25821_dev *dev = dev_id;
30fdf035 509 u32 audio_status;
1a9fc855 510 int handled = 0;
bfef0d35 511 const struct sram_channel *sram_ch;
1a9fc855
MCC
512
513 if (!dev)
514 return -1;
515
6100c579 516 sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
1a9fc855 517
1a9fc855
MCC
518 audio_status = cx_read(sram_ch->int_stat);
519
b5f11cc7 520 /* Only deal with our interrupt */
1a9fc855 521 if (audio_status) {
1531e889
LF
522 handled = cx25821_audio_upstream_irq(dev,
523 dev->_audio_upstream_channel, audio_status);
1a9fc855
MCC
524 }
525
b5f11cc7 526 if (handled < 0)
1a9fc855 527 cx25821_stop_upstream_audio(dev);
b5f11cc7 528 else
1a9fc855 529 handled += handled;
1a9fc855
MCC
530
531 return IRQ_RETVAL(handled);
532}
533
534static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
bfef0d35 535 const struct sram_channel *sram_ch)
1a9fc855
MCC
536{
537 int count = 0;
538 u32 tmp;
539
540 do {
b5f11cc7
MCC
541 /* Wait 10 microsecond before checking to see if the FIFO is
542 * turned ON. */
1a9fc855
MCC
543 udelay(10);
544
545 tmp = cx_read(sram_ch->dma_ctl);
546
b5f11cc7
MCC
547 /* 10 millisecond timeout */
548 if (count++ > 1000) {
36d89f7d 549 pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
81f4c8dd 550 __func__);
1a9fc855
MCC
551 return;
552 }
553
554 } while (!(tmp & sram_ch->fld_aud_fifo_en));
555
556}
557
dafc456c 558static int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
bfef0d35 559 const struct sram_channel *sram_ch)
1a9fc855
MCC
560{
561 u32 tmp = 0;
562 int err = 0;
563
b5f11cc7
MCC
564 /* Set the physical start address of the RISC program in the initial
565 * program counter(IPC) member of the CMDS. */
1a9fc855 566 cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
b5f11cc7
MCC
567 /* Risc IPC High 64 bits 63-32 */
568 cx_write(sram_ch->cmds_start + 4, 0);
1a9fc855
MCC
569
570 /* reset counter */
571 cx_write(sram_ch->gpcnt_ctl, 3);
572
b5f11cc7
MCC
573 /* Set the line length (It looks like we do not need to set the
574 * line length) */
1a9fc855
MCC
575 cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
576
b5f11cc7 577 /* Set the input mode to 16-bit */
1a9fc855 578 tmp = cx_read(sram_ch->aud_cfg);
72a52fc3
LF
579 tmp |= FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
580 FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D |
581 FLD_AUD_SONY_MODE;
1a9fc855
MCC
582 cx_write(sram_ch->aud_cfg, tmp);
583
b5f11cc7 584 /* Read and write back the interrupt status register to clear it */
1a9fc855
MCC
585 tmp = cx_read(sram_ch->int_stat);
586 cx_write(sram_ch->int_stat, tmp);
587
b5f11cc7 588 /* Clear our bits from the interrupt status register. */
1a9fc855
MCC
589 cx_write(sram_ch->int_stat, _intr_msk);
590
b5f11cc7 591 /* Set the interrupt mask register, enable irq. */
1a9fc855
MCC
592 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
593 tmp = cx_read(sram_ch->int_msk);
594 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
595
72a52fc3 596 err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
18e9351e 597 IRQF_SHARED, dev->name, dev);
1a9fc855 598 if (err < 0) {
72a52fc3
LF
599 pr_err("%s: can't get upstream IRQ %d\n", dev->name,
600 dev->pci->irq);
1a9fc855
MCC
601 goto fail_irq;
602 }
603
b5f11cc7 604 /* Start the DMA engine */
1a9fc855
MCC
605 tmp = cx_read(sram_ch->dma_ctl);
606 cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
607
608 dev->_audio_is_running = 1;
609 dev->_is_first_audio_frame = 1;
610
b5f11cc7 611 /* The fifo_en bit turns on by the first Risc program */
1a9fc855
MCC
612 cx25821_wait_fifo_enable(dev, sram_ch);
613
614 return 0;
615
b5f11cc7 616fail_irq:
1a9fc855
MCC
617 cx25821_dev_unregister(dev);
618 return err;
619}
620
621int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
622{
bfef0d35 623 const struct sram_channel *sram_ch;
1a9fc855 624 int err = 0;
1a9fc855
MCC
625
626 if (dev->_audio_is_running) {
36d89f7d 627 pr_warn("Audio Channel is still running so return!\n");
1a9fc855
MCC
628 return 0;
629 }
630
6100c579 631 dev->_audio_upstream_channel = channel_select;
e4115bb2 632 sram_ch = dev->channels[channel_select].sram_channels;
1a9fc855 633
b5f11cc7 634 /* Work queue */
1a9fc855 635 INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
1a9fc855
MCC
636
637 dev->_last_index_irq = 0;
638 dev->_audio_is_running = 0;
639 dev->_audioframe_count = 0;
640 dev->_audiofile_status = RESET_STATUS;
641 dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
642 _line_size = AUDIO_LINE_SIZE;
643
cab3e1ff
PST
644 if ((dev->input_audiofilename) &&
645 (strcmp(dev->input_audiofilename, "") != 0))
6c17c24d
PST
646 dev->_audiofilename = kstrdup(dev->input_audiofilename,
647 GFP_KERNEL);
cab3e1ff 648 else
6c17c24d
PST
649 dev->_audiofilename = kstrdup(_defaultAudioName,
650 GFP_KERNEL);
1a9fc855 651
cab3e1ff
PST
652 if (!dev->_audiofilename) {
653 err = -ENOMEM;
654 goto error;
1a9fc855
MCC
655 }
656
c81d4972
PST
657 cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
658 _line_size, 0);
1a9fc855
MCC
659
660 dev->audio_upstream_riscbuf_size =
81f4c8dd
LF
661 AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
662 RISC_SYNC_INSTRUCTION_SIZE;
1a9fc855
MCC
663 dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
664
b5f11cc7 665 /* Allocating buffers and prepare RISC program */
c81d4972 666 err = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
1531e889 667 _line_size);
c81d4972 668 if (err < 0) {
36d89f7d 669 pr_err("%s: Failed to set up Audio upstream buffers!\n",
81f4c8dd 670 dev->name);
1a9fc855
MCC
671 goto error;
672 }
b5f11cc7 673 /* Start RISC engine */
1a9fc855
MCC
674 cx25821_start_audio_dma_upstream(dev, sram_ch);
675
676 return 0;
677
b5f11cc7 678error:
1a9fc855
MCC
679 cx25821_dev_unregister(dev);
680
681 return err;
682}