Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / drivers / staging / cx25821 / cx25821-audio-upstream.c
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include "cx25821-video.h"
24 #include "cx25821-audio-upstream.h"
25
26 #include <linux/fs.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/syscalls.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/delay.h>
35 #include <linux/slab.h>
36 #include <linux/uaccess.h>
37
38 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
39 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
40 MODULE_LICENSE("GPL");
41
42 static int _intr_msk =
43     FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF | FLD_AUD_SRC_SYNC |
44     FLD_AUD_SRC_OPC_ERR;
45
46 int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
47                                               struct sram_channel *ch,
48                                               unsigned int bpl, u32 risc)
49 {
50         unsigned int i, lines;
51         u32 cdt;
52
53         if (ch->cmds_start == 0) {
54                 cx_write(ch->ptr1_reg, 0);
55                 cx_write(ch->ptr2_reg, 0);
56                 cx_write(ch->cnt2_reg, 0);
57                 cx_write(ch->cnt1_reg, 0);
58                 return 0;
59         }
60
61         bpl = (bpl + 7) & ~7;   /* alignment */
62         cdt = ch->cdt;
63         lines = ch->fifo_size / bpl;
64
65         if (lines > 3)
66                 lines = 3;
67
68         BUG_ON(lines < 2);
69
70         /* write CDT */
71         for (i = 0; i < lines; i++) {
72                 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
73                 cx_write(cdt + 16 * i + 4, 0);
74                 cx_write(cdt + 16 * i + 8, 0);
75                 cx_write(cdt + 16 * i + 12, 0);
76         }
77
78         /* write CMDS */
79         cx_write(ch->cmds_start + 0, risc);
80
81         cx_write(ch->cmds_start + 4, 0);
82         cx_write(ch->cmds_start + 8, cdt);
83         cx_write(ch->cmds_start + 12, AUDIO_CDT_SIZE_QW);
84         cx_write(ch->cmds_start + 16, ch->ctrl_start);
85
86         /* IQ size */
87         cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);
88
89         for (i = 24; i < 80; i += 4)
90                 cx_write(ch->cmds_start + i, 0);
91
92         /* fill registers */
93         cx_write(ch->ptr1_reg, ch->fifo_start);
94         cx_write(ch->ptr2_reg, cdt);
95         cx_write(ch->cnt2_reg, AUDIO_CDT_SIZE_QW);
96         cx_write(ch->cnt1_reg, AUDIO_CLUSTER_SIZE_QW - 1);
97
98         return 0;
99 }
100
101 static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
102                                                  __le32 *rp,
103                                                  dma_addr_t databuf_phys_addr,
104                                                  unsigned int bpl,
105                                                  int fifo_enable)
106 {
107         unsigned int line;
108         struct sram_channel *sram_ch =
109            dev->channels[dev->_audio_upstream_channel_select].sram_channels;
110         int offset = 0;
111
112         /* scan lines */
113         for (line = 0; line < LINES_PER_AUDIO_BUFFER; line++) {
114                 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
115                 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
116                 *(rp++) = cpu_to_le32(0);       /* bits 63-32 */
117
118                 /* Check if we need to enable the FIFO
119                  * after the first 3 lines.
120                  * For the upstream audio channel,
121                  * the risc engine will enable the FIFO */
122                 if (fifo_enable && line == 2) {
123                         *(rp++) = RISC_WRITECR;
124                         *(rp++) = sram_ch->dma_ctl;
125                         *(rp++) = sram_ch->fld_aud_fifo_en;
126                         *(rp++) = 0x00000020;
127                 }
128
129                 offset += AUDIO_LINE_SIZE;
130         }
131
132         return rp;
133 }
134
135 int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
136                                        struct pci_dev *pci,
137                                        unsigned int bpl, unsigned int lines)
138 {
139         __le32 *rp;
140         int fifo_enable = 0;
141         int frame = 0, i = 0;
142         int frame_size = AUDIO_DATA_BUF_SZ;
143         int databuf_offset = 0;
144         int risc_flag = RISC_CNT_INC;
145         dma_addr_t risc_phys_jump_addr;
146
147         /* Virtual address of Risc buffer program */
148         rp = dev->_risc_virt_addr;
149
150         /* sync instruction */
151         *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
152
153         for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
154                 databuf_offset = frame_size * frame;
155
156                 if (frame == 0) {
157                         fifo_enable = 1;
158                         risc_flag = RISC_CNT_RESET;
159                 } else {
160                         fifo_enable = 0;
161                         risc_flag = RISC_CNT_INC;
162                 }
163
164                 /* Calculate physical jump address */
165                 if ((frame + 1) == NUM_AUDIO_FRAMES) {
166                         risc_phys_jump_addr =
167                             dev->_risc_phys_start_addr +
168                             RISC_SYNC_INSTRUCTION_SIZE;
169                 } else {
170                         risc_phys_jump_addr =
171                             dev->_risc_phys_start_addr +
172                             RISC_SYNC_INSTRUCTION_SIZE +
173                             AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
174                 }
175
176                 rp = cx25821_risc_field_upstream_audio(dev, rp,
177                                                        dev->
178                                                        _audiodata_buf_phys_addr
179                                                        + databuf_offset, bpl,
180                                                        fifo_enable);
181
182                 if (USE_RISC_NOOP_AUDIO) {
183                         for (i = 0; i < NUM_NO_OPS; i++)
184                                 *(rp++) = cpu_to_le32(RISC_NOOP);
185                 }
186
187                 /* Loop to (Nth)FrameRISC or to Start of Risc program &
188                  * generate IRQ */
189                 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
190                 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
191                 *(rp++) = cpu_to_le32(0);
192
193                 /* Recalculate virtual address based on frame index */
194                 rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
195                     (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
196         }
197
198         return 0;
199 }
200
201 void cx25821_free_memory_audio(struct cx25821_dev *dev)
202 {
203         if (dev->_risc_virt_addr) {
204                 pci_free_consistent(dev->pci, dev->_audiorisc_size,
205                                     dev->_risc_virt_addr, dev->_risc_phys_addr);
206                 dev->_risc_virt_addr = NULL;
207         }
208
209         if (dev->_audiodata_buf_virt_addr) {
210                 pci_free_consistent(dev->pci, dev->_audiodata_buf_size,
211                                     dev->_audiodata_buf_virt_addr,
212                                     dev->_audiodata_buf_phys_addr);
213                 dev->_audiodata_buf_virt_addr = NULL;
214         }
215 }
216
217 void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
218 {
219         struct sram_channel *sram_ch =
220            dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
221         u32 tmp = 0;
222
223         if (!dev->_audio_is_running) {
224                 printk(KERN_DEBUG
225                     "cx25821: No audio file is currently running so return!\n");
226                 return;
227         }
228         /* Disable RISC interrupts */
229         cx_write(sram_ch->int_msk, 0);
230
231         /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
232         tmp = cx_read(sram_ch->dma_ctl);
233         cx_write(sram_ch->dma_ctl,
234                  tmp & ~(sram_ch->fld_aud_fifo_en | sram_ch->fld_aud_risc_en));
235
236         /* Clear data buffer memory */
237         if (dev->_audiodata_buf_virt_addr)
238                 memset(dev->_audiodata_buf_virt_addr, 0,
239                        dev->_audiodata_buf_size);
240
241         dev->_audio_is_running = 0;
242         dev->_is_first_audio_frame = 0;
243         dev->_audioframe_count = 0;
244         dev->_audiofile_status = END_OF_FILE;
245
246         if (dev->_irq_audio_queues) {
247                 kfree(dev->_irq_audio_queues);
248                 dev->_irq_audio_queues = NULL;
249         }
250
251         if (dev->_audiofilename != NULL)
252                 kfree(dev->_audiofilename);
253 }
254
255 void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
256 {
257         if (dev->_audio_is_running)
258                 cx25821_stop_upstream_audio(dev);
259
260         cx25821_free_memory_audio(dev);
261 }
262
263 int cx25821_get_audio_data(struct cx25821_dev *dev,
264                            struct sram_channel *sram_ch)
265 {
266         struct file *myfile;
267         int frame_index_temp = dev->_audioframe_index;
268         int i = 0;
269         int line_size = AUDIO_LINE_SIZE;
270         int frame_size = AUDIO_DATA_BUF_SZ;
271         int frame_offset = frame_size * frame_index_temp;
272         ssize_t vfs_read_retval = 0;
273         char mybuf[line_size];
274         loff_t file_offset = dev->_audioframe_count * frame_size;
275         loff_t pos;
276         mm_segment_t old_fs;
277
278         if (dev->_audiofile_status == END_OF_FILE)
279                 return 0;
280
281         myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
282
283         if (IS_ERR(myfile)) {
284                 const int open_errno = -PTR_ERR(myfile);
285                 printk(KERN_ERR "%s(): ERROR opening file(%s) with errno = %d!\n",
286                        __func__, dev->_audiofilename, open_errno);
287                 return PTR_ERR(myfile);
288         } else {
289                 if (!(myfile->f_op)) {
290                         printk(KERN_ERR "%s: File has no file operations registered!\n",
291                                __func__);
292                         filp_close(myfile, NULL);
293                         return -EIO;
294                 }
295
296                 if (!myfile->f_op->read) {
297                         printk(KERN_ERR "%s: File has no READ operations registered!\n",
298                                __func__);
299                         filp_close(myfile, NULL);
300                         return -EIO;
301                 }
302
303                 pos = myfile->f_pos;
304                 old_fs = get_fs();
305                 set_fs(KERNEL_DS);
306
307                 for (i = 0; i < dev->_audio_lines_count; i++) {
308                         pos = file_offset;
309
310                         vfs_read_retval =
311                             vfs_read(myfile, mybuf, line_size, &pos);
312
313                         if (vfs_read_retval > 0 && vfs_read_retval == line_size
314                             && dev->_audiodata_buf_virt_addr != NULL) {
315                                 memcpy((void *)(dev->_audiodata_buf_virt_addr +
316                                                 frame_offset / 4), mybuf,
317                                        vfs_read_retval);
318                         }
319
320                         file_offset += vfs_read_retval;
321                         frame_offset += vfs_read_retval;
322
323                         if (vfs_read_retval < line_size) {
324                                 printk(KERN_INFO
325                                        "Done: exit %s() since no more bytes to read from Audio file.\n",
326                                        __func__);
327                                 break;
328                         }
329                 }
330
331                 if (i > 0)
332                         dev->_audioframe_count++;
333
334                 dev->_audiofile_status =
335                     (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
336
337                 set_fs(old_fs);
338                 filp_close(myfile, NULL);
339         }
340
341         return 0;
342 }
343
344 static void cx25821_audioups_handler(struct work_struct *work)
345 {
346         struct cx25821_dev *dev =
347             container_of(work, struct cx25821_dev, _audio_work_entry);
348
349         if (!dev) {
350                 printk(KERN_ERR "ERROR %s(): since container_of(work_struct) FAILED!\n",
351                        __func__);
352                 return;
353         }
354
355         cx25821_get_audio_data(dev,
356                               dev->channels[dev->
357                                        _audio_upstream_channel_select].
358                                        sram_channels);
359 }
360
361 int cx25821_openfile_audio(struct cx25821_dev *dev,
362                            struct sram_channel *sram_ch)
363 {
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;
372
373         myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
374
375         if (IS_ERR(myfile)) {
376                 const int open_errno = -PTR_ERR(myfile);
377                 printk(KERN_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                         printk(KERN_ERR "%s: File has no file operations registered!\n",
383                                __func__);
384                         filp_close(myfile, NULL);
385                         return -EIO;
386                 }
387
388                 if (!myfile->f_op->read) {
389                         printk(KERN_ERR "%s: File has no READ operations registered!\n",
390                                __func__);
391                         filp_close(myfile, NULL);
392                         return -EIO;
393                 }
394
395                 pos = myfile->f_pos;
396                 old_fs = get_fs();
397                 set_fs(KERNEL_DS);
398
399                 for (j = 0; j < NUM_AUDIO_FRAMES; j++) {
400                         for (i = 0; i < dev->_audio_lines_count; i++) {
401                                 pos = offset;
402
403                                 vfs_read_retval =
404                                     vfs_read(myfile, mybuf, line_size, &pos);
405
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);
413                                 }
414
415                                 offset += vfs_read_retval;
416
417                                 if (vfs_read_retval < line_size) {
418                                         printk(KERN_INFO
419                                                "Done: exit %s() since no more bytes to read from Audio file.\n",
420                                                __func__);
421                                         break;
422                                 }
423                         }
424
425                         if (i > 0)
426                                 dev->_audioframe_count++;
427
428                         if (vfs_read_retval < line_size)
429                                 break;
430                 }
431
432                 dev->_audiofile_status =
433                     (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
434
435                 set_fs(old_fs);
436                 myfile->f_pos = 0;
437                 filp_close(myfile, NULL);
438         }
439
440         return 0;
441 }
442
443 static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
444                                                  struct sram_channel *sram_ch,
445                                                  int bpl)
446 {
447         int ret = 0;
448         dma_addr_t dma_addr;
449         dma_addr_t data_dma_addr;
450
451         cx25821_free_memory_audio(dev);
452
453         dev->_risc_virt_addr =
454             pci_alloc_consistent(dev->pci, dev->audio_upstream_riscbuf_size,
455                                  &dma_addr);
456         dev->_risc_virt_start_addr = dev->_risc_virt_addr;
457         dev->_risc_phys_start_addr = dma_addr;
458         dev->_risc_phys_addr = dma_addr;
459         dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
460
461         if (!dev->_risc_virt_addr) {
462                 printk(KERN_DEBUG
463                         "cx25821 ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning.\n");
464                 return -ENOMEM;
465         }
466         /* Clear out memory at address */
467         memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
468
469         /* For Audio Data buffer allocation */
470         dev->_audiodata_buf_virt_addr =
471             pci_alloc_consistent(dev->pci, dev->audio_upstream_databuf_size,
472                                  &data_dma_addr);
473         dev->_audiodata_buf_phys_addr = data_dma_addr;
474         dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
475
476         if (!dev->_audiodata_buf_virt_addr) {
477                 printk(KERN_DEBUG
478                         "cx25821 ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning.\n");
479                 return -ENOMEM;
480         }
481         /* Clear out memory at address */
482         memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
483
484         ret = cx25821_openfile_audio(dev, sram_ch);
485         if (ret < 0)
486                 return ret;
487
488         /* Creating RISC programs */
489         ret =
490             cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
491                                                dev->_audio_lines_count);
492         if (ret < 0) {
493                 printk(KERN_DEBUG
494                       "cx25821 ERROR creating audio upstream RISC programs!\n");
495                 goto error;
496         }
497
498         return 0;
499
500 error:
501         return ret;
502 }
503
504 int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
505                                u32 status)
506 {
507         int i = 0;
508         u32 int_msk_tmp;
509        struct sram_channel *channel = dev->channels[chan_num].sram_channels;
510         dma_addr_t risc_phys_jump_addr;
511         __le32 *rp;
512
513         if (status & FLD_AUD_SRC_RISCI1) {
514                 /* Get interrupt_index of the program that interrupted */
515                 u32 prog_cnt = cx_read(channel->gpcnt);
516
517                 /* Since we've identified our IRQ, clear our bits from the
518                  * interrupt mask and interrupt status registers */
519                 cx_write(channel->int_msk, 0);
520                 cx_write(channel->int_stat, cx_read(channel->int_stat));
521
522                 spin_lock(&dev->slock);
523
524                 while (prog_cnt != dev->_last_index_irq) {
525                         /* Update _last_index_irq */
526                         if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
527                                 dev->_last_index_irq++;
528                         else
529                                 dev->_last_index_irq = 0;
530
531                         dev->_audioframe_index = dev->_last_index_irq;
532
533                         queue_work(dev->_irq_audio_queues,
534                                    &dev->_audio_work_entry);
535                 }
536
537                 if (dev->_is_first_audio_frame) {
538                         dev->_is_first_audio_frame = 0;
539
540                         if (dev->_risc_virt_start_addr != NULL) {
541                                 risc_phys_jump_addr =
542                                     dev->_risc_phys_start_addr +
543                                     RISC_SYNC_INSTRUCTION_SIZE +
544                                     AUDIO_RISC_DMA_BUF_SIZE;
545
546                                 rp = cx25821_risc_field_upstream_audio(dev,
547                                                                        dev->
548                                                                        _risc_virt_start_addr
549                                                                        + 1,
550                                                                        dev->
551                                                                        _audiodata_buf_phys_addr,
552                                                                        AUDIO_LINE_SIZE,
553                                                                        FIFO_DISABLE);
554
555                                 if (USE_RISC_NOOP_AUDIO) {
556                                         for (i = 0; i < NUM_NO_OPS; i++) {
557                                                 *(rp++) =
558                                                     cpu_to_le32(RISC_NOOP);
559                                         }
560                                 }
561                                 /* Jump to 2nd Audio Frame */
562                                 *(rp++) =
563                                     cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
564                                                 RISC_CNT_RESET);
565                                 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
566                                 *(rp++) = cpu_to_le32(0);
567                         }
568                 }
569
570                 spin_unlock(&dev->slock);
571         } else {
572                 if (status & FLD_AUD_SRC_OF)
573                         printk(KERN_WARNING "%s: Audio Received Overflow Error Interrupt!\n",
574                                __func__);
575
576                 if (status & FLD_AUD_SRC_SYNC)
577                         printk(KERN_WARNING "%s: Audio Received Sync Error Interrupt!\n",
578                                __func__);
579
580                 if (status & FLD_AUD_SRC_OPC_ERR)
581                         printk(KERN_WARNING "%s: Audio Received OpCode Error Interrupt!\n",
582                                __func__);
583
584                 /* Read and write back the interrupt status register to clear
585                  * our bits */
586                 cx_write(channel->int_stat, cx_read(channel->int_stat));
587         }
588
589         if (dev->_audiofile_status == END_OF_FILE) {
590                 printk(KERN_WARNING "cx25821: EOF Channel Audio Framecount = %d\n",
591                        dev->_audioframe_count);
592                 return -1;
593         }
594         /* ElSE, set the interrupt mask register, re-enable irq. */
595         int_msk_tmp = cx_read(channel->int_msk);
596         cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
597
598         return 0;
599 }
600
601 static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
602 {
603         struct cx25821_dev *dev = dev_id;
604         u32 msk_stat, audio_status;
605         int handled = 0;
606         struct sram_channel *sram_ch;
607
608         if (!dev)
609                 return -1;
610
611        sram_ch = dev->channels[dev->_audio_upstream_channel_select].
612                                        sram_channels;
613
614         msk_stat = cx_read(sram_ch->int_mstat);
615         audio_status = cx_read(sram_ch->int_stat);
616
617         /* Only deal with our interrupt */
618         if (audio_status) {
619                 handled =
620                     cx25821_audio_upstream_irq(dev,
621                                                dev->
622                                                _audio_upstream_channel_select,
623                                                audio_status);
624         }
625
626         if (handled < 0)
627                 cx25821_stop_upstream_audio(dev);
628         else
629                 handled += handled;
630
631         return IRQ_RETVAL(handled);
632 }
633
634 static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
635                                      struct sram_channel *sram_ch)
636 {
637         int count = 0;
638         u32 tmp;
639
640         do {
641                 /* Wait 10 microsecond before checking to see if the FIFO is
642                  * turned ON. */
643                 udelay(10);
644
645                 tmp = cx_read(sram_ch->dma_ctl);
646
647                 /* 10 millisecond timeout */
648                 if (count++ > 1000) {
649                         printk(KERN_ERR
650                                "cx25821 ERROR: %s() fifo is NOT turned on. Timeout!\n",
651                              __func__);
652                         return;
653                 }
654
655         } while (!(tmp & sram_ch->fld_aud_fifo_en));
656
657 }
658
659 int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
660                                      struct sram_channel *sram_ch)
661 {
662         u32 tmp = 0;
663         int err = 0;
664
665         /* Set the physical start address of the RISC program in the initial
666          * program counter(IPC) member of the CMDS. */
667         cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
668         /* Risc IPC High 64 bits 63-32 */
669         cx_write(sram_ch->cmds_start + 4, 0);
670
671         /* reset counter */
672         cx_write(sram_ch->gpcnt_ctl, 3);
673
674         /* Set the line length       (It looks like we do not need to set the
675          * line length) */
676         cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
677
678         /* Set the input mode to 16-bit */
679         tmp = cx_read(sram_ch->aud_cfg);
680         tmp |=
681             FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
682             FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D | FLD_AUD_SONY_MODE;
683         cx_write(sram_ch->aud_cfg, tmp);
684
685         /* Read and write back the interrupt status register to clear it */
686         tmp = cx_read(sram_ch->int_stat);
687         cx_write(sram_ch->int_stat, tmp);
688
689         /* Clear our bits from the interrupt status register. */
690         cx_write(sram_ch->int_stat, _intr_msk);
691
692         /* Set the interrupt mask register, enable irq. */
693         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
694         tmp = cx_read(sram_ch->int_msk);
695         cx_write(sram_ch->int_msk, tmp |= _intr_msk);
696
697         err =
698             request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
699                         IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
700         if (err < 0) {
701                 printk(KERN_ERR "%s: can't get upstream IRQ %d\n", dev->name,
702                        dev->pci->irq);
703                 goto fail_irq;
704         }
705
706         /* Start the DMA  engine */
707         tmp = cx_read(sram_ch->dma_ctl);
708         cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
709
710         dev->_audio_is_running = 1;
711         dev->_is_first_audio_frame = 1;
712
713         /* The fifo_en bit turns on by the first Risc program */
714         cx25821_wait_fifo_enable(dev, sram_ch);
715
716         return 0;
717
718 fail_irq:
719         cx25821_dev_unregister(dev);
720         return err;
721 }
722
723 int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
724 {
725         struct sram_channel *sram_ch;
726         int retval = 0;
727         int err = 0;
728         int str_length = 0;
729
730         if (dev->_audio_is_running) {
731                 printk(KERN_WARNING "Audio Channel is still running so return!\n");
732                 return 0;
733         }
734
735         dev->_audio_upstream_channel_select = channel_select;
736        sram_ch = dev->channels[channel_select].sram_channels;
737
738         /* Work queue */
739         INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
740         dev->_irq_audio_queues =
741             create_singlethread_workqueue("cx25821_audioworkqueue");
742
743         if (!dev->_irq_audio_queues) {
744                 printk(KERN_DEBUG
745                         "cx25821 ERROR: create_singlethread_workqueue() for Audio FAILED!\n");
746                 return -ENOMEM;
747         }
748
749         dev->_last_index_irq = 0;
750         dev->_audio_is_running = 0;
751         dev->_audioframe_count = 0;
752         dev->_audiofile_status = RESET_STATUS;
753         dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
754         _line_size = AUDIO_LINE_SIZE;
755
756         if (dev->input_audiofilename) {
757                 str_length = strlen(dev->input_audiofilename);
758                 dev->_audiofilename = kmalloc(str_length + 1, GFP_KERNEL);
759
760                 if (!dev->_audiofilename)
761                         goto error;
762
763                 memcpy(dev->_audiofilename, dev->input_audiofilename,
764                        str_length + 1);
765
766                 /* Default if filename is empty string */
767                 if (strcmp(dev->input_audiofilename, "") == 0) {
768                         dev->_audiofilename = "/root/audioGOOD.wav";
769                 }
770         } else {
771                 str_length = strlen(_defaultAudioName);
772                 dev->_audiofilename = kmalloc(str_length + 1, GFP_KERNEL);
773
774                 if (!dev->_audiofilename)
775                         goto error;
776
777                 memcpy(dev->_audiofilename, _defaultAudioName, str_length + 1);
778         }
779
780         retval =
781             cx25821_sram_channel_setup_upstream_audio(dev, sram_ch, _line_size,
782                                                       0);
783
784         dev->audio_upstream_riscbuf_size =
785             AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
786             RISC_SYNC_INSTRUCTION_SIZE;
787         dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
788
789         /* Allocating buffers and prepare RISC program */
790         retval =
791             cx25821_audio_upstream_buffer_prepare(dev, sram_ch, _line_size);
792         if (retval < 0) {
793                 printk(KERN_ERR
794                        "%s: Failed to set up Audio upstream buffers!\n",
795                        dev->name);
796                 goto error;
797         }
798         /* Start RISC engine */
799         cx25821_start_audio_dma_upstream(dev, sram_ch);
800
801         return 0;
802
803 error:
804         cx25821_dev_unregister(dev);
805
806         return err;
807 }