cx25821-audio-upstream.c 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  Driver for the Conexant CX25821 PCIe bridge
 *
 *  Copyright (C) 2009 Conexant Systems Inc.
 *  Authors  <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

23 24
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

25 26 27 28 29 30 31 32 33 34 35 36
#include "cx25821-video.h"
#include "cx25821-audio-upstream.h"

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
37
#include <linux/slab.h>
38
#include <linux/uaccess.h>
39 40 41 42 43

MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
MODULE_LICENSE("GPL");

44 45
static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
			FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
46

47
static int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
					      struct sram_channel *ch,
					      unsigned int bpl, u32 risc)
{
	unsigned int i, lines;
	u32 cdt;

	if (ch->cmds_start == 0) {
		cx_write(ch->ptr1_reg, 0);
		cx_write(ch->ptr2_reg, 0);
		cx_write(ch->cnt2_reg, 0);
		cx_write(ch->cnt1_reg, 0);
		return 0;
	}

	bpl = (bpl + 7) & ~7;	/* alignment */
	cdt = ch->cdt;
	lines = ch->fifo_size / bpl;

66
	if (lines > 3)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		lines = 3;

	BUG_ON(lines < 2);

	/* write CDT */
	for (i = 0; i < lines; i++) {
		cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
		cx_write(cdt + 16 * i + 4, 0);
		cx_write(cdt + 16 * i + 8, 0);
		cx_write(cdt + 16 * i + 12, 0);
	}

	/* write CMDS */
	cx_write(ch->cmds_start + 0, risc);

	cx_write(ch->cmds_start + 4, 0);
	cx_write(ch->cmds_start + 8, cdt);
	cx_write(ch->cmds_start + 12, AUDIO_CDT_SIZE_QW);
	cx_write(ch->cmds_start + 16, ch->ctrl_start);

87
	/* IQ size */
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);

	for (i = 24; i < 80; i += 4)
		cx_write(ch->cmds_start + i, 0);

	/* fill registers */
	cx_write(ch->ptr1_reg, ch->fifo_start);
	cx_write(ch->ptr2_reg, cdt);
	cx_write(ch->cnt2_reg, AUDIO_CDT_SIZE_QW);
	cx_write(ch->cnt1_reg, AUDIO_CLUSTER_SIZE_QW - 1);

	return 0;
}

static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
103
						 __le32 *rp,
104 105 106 107 108 109
						 dma_addr_t databuf_phys_addr,
						 unsigned int bpl,
						 int fifo_enable)
{
	unsigned int line;
	struct sram_channel *sram_ch =
110
		dev->channels[dev->_audio_upstream_channel].sram_channels;
111 112 113 114 115 116 117 118
	int offset = 0;

	/* scan lines */
	for (line = 0; line < LINES_PER_AUDIO_BUFFER; line++) {
		*(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
		*(rp++) = cpu_to_le32(databuf_phys_addr + offset);
		*(rp++) = cpu_to_le32(0);	/* bits 63-32 */

119 120 121 122
		/* Check if we need to enable the FIFO
		 * after the first 3 lines.
		 * For the upstream audio channel,
		 * the risc engine will enable the FIFO */
123 124 125 126 127 128 129 130 131 132 133 134 135
		if (fifo_enable && line == 2) {
			*(rp++) = RISC_WRITECR;
			*(rp++) = sram_ch->dma_ctl;
			*(rp++) = sram_ch->fld_aud_fifo_en;
			*(rp++) = 0x00000020;
		}

		offset += AUDIO_LINE_SIZE;
	}

	return rp;
}

136
static int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
				       struct pci_dev *pci,
				       unsigned int bpl, unsigned int lines)
{
	__le32 *rp;
	int fifo_enable = 0;
	int frame = 0, i = 0;
	int frame_size = AUDIO_DATA_BUF_SZ;
	int databuf_offset = 0;
	int risc_flag = RISC_CNT_INC;
	dma_addr_t risc_phys_jump_addr;

	/* Virtual address of Risc buffer program */
	rp = dev->_risc_virt_addr;

	/* sync instruction */
	*(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);

	for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
		databuf_offset = frame_size * frame;

		if (frame == 0) {
			fifo_enable = 1;
			risc_flag = RISC_CNT_RESET;
		} else {
			fifo_enable = 0;
			risc_flag = RISC_CNT_INC;
		}

165
		/* Calculate physical jump address */
166 167 168 169 170 171 172 173 174 175 176 177
		if ((frame + 1) == NUM_AUDIO_FRAMES) {
			risc_phys_jump_addr =
			    dev->_risc_phys_start_addr +
			    RISC_SYNC_INSTRUCTION_SIZE;
		} else {
			risc_phys_jump_addr =
			    dev->_risc_phys_start_addr +
			    RISC_SYNC_INSTRUCTION_SIZE +
			    AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
		}

		rp = cx25821_risc_field_upstream_audio(dev, rp,
178 179
				dev->_audiodata_buf_phys_addr + databuf_offset,
				bpl, fifo_enable);
180 181

		if (USE_RISC_NOOP_AUDIO) {
182
			for (i = 0; i < NUM_NO_OPS; i++)
183 184 185
				*(rp++) = cpu_to_le32(RISC_NOOP);
		}

186 187
		/* Loop to (Nth)FrameRISC or to Start of Risc program &
		 * generate IRQ */
188 189 190 191
		*(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
		*(rp++) = cpu_to_le32(risc_phys_jump_addr);
		*(rp++) = cpu_to_le32(0);

192
		/* Recalculate virtual address based on frame index */
193
		rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
194
			(AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
195 196 197 198 199
	}

	return 0;
}

200
static void cx25821_free_memory_audio(struct cx25821_dev *dev)
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
{
	if (dev->_risc_virt_addr) {
		pci_free_consistent(dev->pci, dev->_audiorisc_size,
				    dev->_risc_virt_addr, dev->_risc_phys_addr);
		dev->_risc_virt_addr = NULL;
	}

	if (dev->_audiodata_buf_virt_addr) {
		pci_free_consistent(dev->pci, dev->_audiodata_buf_size,
				    dev->_audiodata_buf_virt_addr,
				    dev->_audiodata_buf_phys_addr);
		dev->_audiodata_buf_virt_addr = NULL;
	}
}

void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
{
	struct sram_channel *sram_ch =
219
		dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
220 221 222
	u32 tmp = 0;

	if (!dev->_audio_is_running) {
223
		printk(KERN_DEBUG
224
		       pr_fmt("No audio file is currently running so return!\n"));
225 226
		return;
	}
227
	/* Disable RISC interrupts */
228 229
	cx_write(sram_ch->int_msk, 0);

230
	/* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
231 232 233 234
	tmp = cx_read(sram_ch->dma_ctl);
	cx_write(sram_ch->dma_ctl,
		 tmp & ~(sram_ch->fld_aud_fifo_en | sram_ch->fld_aud_risc_en));

235
	/* Clear data buffer memory */
236 237 238 239 240 241 242 243 244
	if (dev->_audiodata_buf_virt_addr)
		memset(dev->_audiodata_buf_virt_addr, 0,
		       dev->_audiodata_buf_size);

	dev->_audio_is_running = 0;
	dev->_is_first_audio_frame = 0;
	dev->_audioframe_count = 0;
	dev->_audiofile_status = END_OF_FILE;

245 246
	kfree(dev->_irq_audio_queues);
	dev->_irq_audio_queues = NULL;
247

248
	kfree(dev->_audiofilename);
249 250 251 252
}

void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
{
253
	if (dev->_audio_is_running)
254 255 256 257 258
		cx25821_stop_upstream_audio(dev);

	cx25821_free_memory_audio(dev);
}

259
static int cx25821_get_audio_data(struct cx25821_dev *dev,
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
			   struct sram_channel *sram_ch)
{
	struct file *myfile;
	int frame_index_temp = dev->_audioframe_index;
	int i = 0;
	int line_size = AUDIO_LINE_SIZE;
	int frame_size = AUDIO_DATA_BUF_SZ;
	int frame_offset = frame_size * frame_index_temp;
	ssize_t vfs_read_retval = 0;
	char mybuf[line_size];
	loff_t file_offset = dev->_audioframe_count * frame_size;
	loff_t pos;
	mm_segment_t old_fs;

	if (dev->_audiofile_status == END_OF_FILE)
		return 0;

	myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);

	if (IS_ERR(myfile)) {
		const int open_errno = -PTR_ERR(myfile);
281
		pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
282 283 284 285
		       __func__, dev->_audiofilename, open_errno);
		return PTR_ERR(myfile);
	} else {
		if (!(myfile->f_op)) {
286
			pr_err("%s(): File has no file operations registered!\n",
287
				__func__);
288 289 290 291 292
			filp_close(myfile, NULL);
			return -EIO;
		}

		if (!myfile->f_op->read) {
293
			pr_err("%s(): File has no READ operations registered!\n",
294
				__func__);
295 296 297 298 299 300 301 302 303 304 305
			filp_close(myfile, NULL);
			return -EIO;
		}

		pos = myfile->f_pos;
		old_fs = get_fs();
		set_fs(KERNEL_DS);

		for (i = 0; i < dev->_audio_lines_count; i++) {
			pos = file_offset;

306 307
			vfs_read_retval = vfs_read(myfile, mybuf, line_size,
									&pos);
308 309 310 311 312

			if (vfs_read_retval > 0 && vfs_read_retval == line_size
			    && dev->_audiodata_buf_virt_addr != NULL) {
				memcpy((void *)(dev->_audiodata_buf_virt_addr +
						frame_offset / 4), mybuf,
313
					vfs_read_retval);
314 315 316 317 318 319
			}

			file_offset += vfs_read_retval;
			frame_offset += vfs_read_retval;

			if (vfs_read_retval < line_size) {
320 321
				pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
					__func__);
322 323 324 325 326 327 328
				break;
			}
		}

		if (i > 0)
			dev->_audioframe_count++;

329 330
		dev->_audiofile_status = (vfs_read_retval == line_size) ?
						IN_PROGRESS : END_OF_FILE;
331 332 333 334 335 336 337 338 339 340

		set_fs(old_fs);
		filp_close(myfile, NULL);
	}

	return 0;
}

static void cx25821_audioups_handler(struct work_struct *work)
{
341 342
	struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
			_audio_work_entry);
343 344

	if (!dev) {
345
		pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
346
			__func__);
347 348 349
		return;
	}

350 351
	cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
			sram_channels);
352 353
}

354
static int cx25821_openfile_audio(struct cx25821_dev *dev,
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
			   struct sram_channel *sram_ch)
{
	struct file *myfile;
	int i = 0, j = 0;
	int line_size = AUDIO_LINE_SIZE;
	ssize_t vfs_read_retval = 0;
	char mybuf[line_size];
	loff_t pos;
	loff_t offset = (unsigned long)0;
	mm_segment_t old_fs;

	myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);

	if (IS_ERR(myfile)) {
		const int open_errno = -PTR_ERR(myfile);
370
		pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
371
			__func__, dev->_audiofilename, open_errno);
372 373 374
		return PTR_ERR(myfile);
	} else {
		if (!(myfile->f_op)) {
375
			pr_err("%s(): File has no file operations registered!\n",
376
				__func__);
377 378 379 380 381
			filp_close(myfile, NULL);
			return -EIO;
		}

		if (!myfile->f_op->read) {
382
			pr_err("%s(): File has no READ operations registered!\n",
383
				__func__);
384 385 386 387 388 389 390 391 392 393 394 395
			filp_close(myfile, NULL);
			return -EIO;
		}

		pos = myfile->f_pos;
		old_fs = get_fs();
		set_fs(KERNEL_DS);

		for (j = 0; j < NUM_AUDIO_FRAMES; j++) {
			for (i = 0; i < dev->_audio_lines_count; i++) {
				pos = offset;

396 397
				vfs_read_retval = vfs_read(myfile, mybuf,
						line_size, &pos);
398

399 400 401
				if (vfs_read_retval > 0 &&
				    vfs_read_retval == line_size &&
				    dev->_audiodata_buf_virt_addr != NULL) {
402 403 404 405 406 407 408 409 410
					memcpy((void *)(dev->
							_audiodata_buf_virt_addr
							+ offset / 4), mybuf,
					       vfs_read_retval);
				}

				offset += vfs_read_retval;

				if (vfs_read_retval < line_size) {
411 412
					pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
						__func__);
413 414 415 416
					break;
				}
			}

417
			if (i > 0)
418 419
				dev->_audioframe_count++;

420
			if (vfs_read_retval < line_size)
421 422 423
				break;
		}

424 425
		dev->_audiofile_status = (vfs_read_retval == line_size) ?
						IN_PROGRESS : END_OF_FILE;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

		set_fs(old_fs);
		myfile->f_pos = 0;
		filp_close(myfile, NULL);
	}

	return 0;
}

static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
						 struct sram_channel *sram_ch,
						 int bpl)
{
	int ret = 0;
	dma_addr_t dma_addr;
	dma_addr_t data_dma_addr;

	cx25821_free_memory_audio(dev);

445 446
	dev->_risc_virt_addr = pci_alloc_consistent(dev->pci,
			dev->audio_upstream_riscbuf_size, &dma_addr);
447 448 449 450 451 452
	dev->_risc_virt_start_addr = dev->_risc_virt_addr;
	dev->_risc_phys_start_addr = dma_addr;
	dev->_risc_phys_addr = dma_addr;
	dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;

	if (!dev->_risc_virt_addr) {
453
		printk(KERN_DEBUG
454
			pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
455 456
		return -ENOMEM;
	}
457
	/* Clear out memory at address */
458 459
	memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);

460
	/* For Audio Data buffer allocation */
461 462
	dev->_audiodata_buf_virt_addr = pci_alloc_consistent(dev->pci,
			dev->audio_upstream_databuf_size, &data_dma_addr);
463 464 465 466
	dev->_audiodata_buf_phys_addr = data_dma_addr;
	dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;

	if (!dev->_audiodata_buf_virt_addr) {
467
		printk(KERN_DEBUG
468
			pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
469 470
		return -ENOMEM;
	}
471
	/* Clear out memory at address */
472 473 474 475 476 477
	memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);

	ret = cx25821_openfile_audio(dev, sram_ch);
	if (ret < 0)
		return ret;

478
	/* Creating RISC programs */
479 480
	ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
						dev->_audio_lines_count);
481 482
	if (ret < 0) {
		printk(KERN_DEBUG
483
			pr_fmt("ERROR creating audio upstream RISC programs!\n"));
484 485 486 487 488
		goto error;
	}

	return 0;

489
error:
490 491 492
	return ret;
}

493
static int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
494 495 496 497
			       u32 status)
{
	int i = 0;
	u32 int_msk_tmp;
498
	struct sram_channel *channel = dev->channels[chan_num].sram_channels;
499 500 501 502
	dma_addr_t risc_phys_jump_addr;
	__le32 *rp;

	if (status & FLD_AUD_SRC_RISCI1) {
503
		/* Get interrupt_index of the program that interrupted */
504 505
		u32 prog_cnt = cx_read(channel->gpcnt);

506 507
		/* Since we've identified our IRQ, clear our bits from the
		 * interrupt mask and interrupt status registers */
508 509 510 511 512 513
		cx_write(channel->int_msk, 0);
		cx_write(channel->int_stat, cx_read(channel->int_stat));

		spin_lock(&dev->slock);

		while (prog_cnt != dev->_last_index_irq) {
514 515
			/* Update _last_index_irq */
			if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
516
				dev->_last_index_irq++;
517
			else
518 519 520 521 522 523 524 525 526 527 528 529 530
				dev->_last_index_irq = 0;

			dev->_audioframe_index = dev->_last_index_irq;

			queue_work(dev->_irq_audio_queues,
				   &dev->_audio_work_entry);
		}

		if (dev->_is_first_audio_frame) {
			dev->_is_first_audio_frame = 0;

			if (dev->_risc_virt_start_addr != NULL) {
				risc_phys_jump_addr =
531 532 533
					dev->_risc_phys_start_addr +
					RISC_SYNC_INSTRUCTION_SIZE +
					AUDIO_RISC_DMA_BUF_SIZE;
534 535

				rp = cx25821_risc_field_upstream_audio(dev,
536 537 538
						dev->_risc_virt_start_addr + 1,
						dev->_audiodata_buf_phys_addr,
						AUDIO_LINE_SIZE, FIFO_DISABLE);
539 540 541 542 543 544 545

				if (USE_RISC_NOOP_AUDIO) {
					for (i = 0; i < NUM_NO_OPS; i++) {
						*(rp++) =
						    cpu_to_le32(RISC_NOOP);
					}
				}
546
				/* Jump to 2nd Audio Frame */
547
				*(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
548 549 550 551 552 553 554 555 556
						RISC_CNT_RESET);
				*(rp++) = cpu_to_le32(risc_phys_jump_addr);
				*(rp++) = cpu_to_le32(0);
			}
		}

		spin_unlock(&dev->slock);
	} else {
		if (status & FLD_AUD_SRC_OF)
557 558
			pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
				__func__);
559 560

		if (status & FLD_AUD_SRC_SYNC)
561 562
			pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
				__func__);
563 564

		if (status & FLD_AUD_SRC_OPC_ERR)
565 566
			pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
				__func__);
567

568 569
		/* Read and write back the interrupt status register to clear
		 * our bits */
570 571 572 573
		cx_write(channel->int_stat, cx_read(channel->int_stat));
	}

	if (dev->_audiofile_status == END_OF_FILE) {
574 575
		pr_warn("EOF Channel Audio Framecount = %d\n",
			dev->_audioframe_count);
576 577
		return -1;
	}
578
	/* ElSE, set the interrupt mask register, re-enable irq. */
579 580 581 582 583 584 585 586 587
	int_msk_tmp = cx_read(channel->int_msk);
	cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);

	return 0;
}

static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
{
	struct cx25821_dev *dev = dev_id;
588
	u32 audio_status;
589 590 591 592 593 594
	int handled = 0;
	struct sram_channel *sram_ch;

	if (!dev)
		return -1;

595
	sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
596 597 598

	audio_status = cx_read(sram_ch->int_stat);

599
	/* Only deal with our interrupt */
600
	if (audio_status) {
601 602
		handled = cx25821_audio_upstream_irq(dev,
				dev->_audio_upstream_channel, audio_status);
603 604
	}

605
	if (handled < 0)
606
		cx25821_stop_upstream_audio(dev);
607
	else
608 609 610 611 612 613 614 615 616 617 618 619
		handled += handled;

	return IRQ_RETVAL(handled);
}

static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
				     struct sram_channel *sram_ch)
{
	int count = 0;
	u32 tmp;

	do {
620 621
		/* Wait 10 microsecond before checking to see if the FIFO is
		 * turned ON. */
622 623 624 625
		udelay(10);

		tmp = cx_read(sram_ch->dma_ctl);

626 627
		/* 10 millisecond timeout */
		if (count++ > 1000) {
628
			pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
629
				__func__);
630 631 632 633 634 635 636
			return;
		}

	} while (!(tmp & sram_ch->fld_aud_fifo_en));

}

637 638
static int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
					    struct sram_channel *sram_ch)
639 640 641 642
{
	u32 tmp = 0;
	int err = 0;

643 644
	/* Set the physical start address of the RISC program in the initial
	 * program counter(IPC) member of the CMDS. */
645
	cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
646 647
	/* Risc IPC High 64 bits 63-32 */
	cx_write(sram_ch->cmds_start + 4, 0);
648 649 650 651

	/* reset counter */
	cx_write(sram_ch->gpcnt_ctl, 3);

652 653
	/* Set the line length       (It looks like we do not need to set the
	 * line length) */
654 655
	cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);

656
	/* Set the input mode to 16-bit */
657
	tmp = cx_read(sram_ch->aud_cfg);
658 659 660
	tmp |= FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
		FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D |
		FLD_AUD_SONY_MODE;
661 662
	cx_write(sram_ch->aud_cfg, tmp);

663
	/* Read and write back the interrupt status register to clear it */
664 665 666
	tmp = cx_read(sram_ch->int_stat);
	cx_write(sram_ch->int_stat, tmp);

667
	/* Clear our bits from the interrupt status register. */
668 669
	cx_write(sram_ch->int_stat, _intr_msk);

670
	/* Set the interrupt mask register, enable irq. */
671 672 673 674
	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
	tmp = cx_read(sram_ch->int_msk);
	cx_write(sram_ch->int_msk, tmp |= _intr_msk);

675
	err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
Y
Yong Zhang 已提交
676
			IRQF_SHARED, dev->name, dev);
677
	if (err < 0) {
678 679
		pr_err("%s: can't get upstream IRQ %d\n", dev->name,
				dev->pci->irq);
680 681 682
		goto fail_irq;
	}

683
	/* Start the DMA  engine */
684 685 686 687 688 689
	tmp = cx_read(sram_ch->dma_ctl);
	cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);

	dev->_audio_is_running = 1;
	dev->_is_first_audio_frame = 1;

690
	/* The fifo_en bit turns on by the first Risc program */
691 692 693 694
	cx25821_wait_fifo_enable(dev, sram_ch);

	return 0;

695
fail_irq:
696 697 698 699 700 701 702 703 704 705 706
	cx25821_dev_unregister(dev);
	return err;
}

int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
{
	struct sram_channel *sram_ch;
	int err = 0;
	int str_length = 0;

	if (dev->_audio_is_running) {
707
		pr_warn("Audio Channel is still running so return!\n");
708 709 710
		return 0;
	}

711
	dev->_audio_upstream_channel = channel_select;
712
	sram_ch = dev->channels[channel_select].sram_channels;
713

714
	/* Work queue */
715 716 717 718 719
	INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
	dev->_irq_audio_queues =
	    create_singlethread_workqueue("cx25821_audioworkqueue");

	if (!dev->_irq_audio_queues) {
720
		printk(KERN_DEBUG
721
			pr_fmt("ERROR: create_singlethread_workqueue() for Audio FAILED!\n"));
722 723 724 725 726 727 728 729 730 731 732 733
		return -ENOMEM;
	}

	dev->_last_index_irq = 0;
	dev->_audio_is_running = 0;
	dev->_audioframe_count = 0;
	dev->_audiofile_status = RESET_STATUS;
	dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
	_line_size = AUDIO_LINE_SIZE;

	if (dev->input_audiofilename) {
		str_length = strlen(dev->input_audiofilename);
734 735
		dev->_audiofilename = kmemdup(dev->input_audiofilename,
					      str_length + 1, GFP_KERNEL);
736

737 738
		if (!dev->_audiofilename) {
			err = -ENOMEM;
739
			goto error;
740
		}
741

742
		/* Default if filename is empty string */
743
		if (strcmp(dev->input_audiofilename, "") == 0)
744 745 746
			dev->_audiofilename = "/root/audioGOOD.wav";
	} else {
		str_length = strlen(_defaultAudioName);
747 748
		dev->_audiofilename = kmemdup(_defaultAudioName,
					      str_length + 1, GFP_KERNEL);
749

750 751
		if (!dev->_audiofilename) {
			err = -ENOMEM;
752
			goto error;
753
		}
754 755
	}

756 757
	cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
						  _line_size, 0);
758 759

	dev->audio_upstream_riscbuf_size =
760 761
		AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
		RISC_SYNC_INSTRUCTION_SIZE;
762 763
	dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;

764
	/* Allocating buffers and prepare RISC program */
765
	err = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
766
							_line_size);
767
	if (err < 0) {
768
		pr_err("%s: Failed to set up Audio upstream buffers!\n",
769
			dev->name);
770 771
		goto error;
	}
772
	/* Start RISC engine */
773 774 775 776
	cx25821_start_audio_dma_upstream(dev, sram_ch);

	return 0;

777
error:
778 779 780 781
	cx25821_dev_unregister(dev);

	return err;
}