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