int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev, int channel_select,
				 int pixel_format)
{
	struct sram_channel *sram_ch;
	u32 tmp;
	int retval = 0;
	int err = 0;
	int data_frame_size = 0;
	int risc_buffer_size = 0;
	int str_length = 0;

	if (dev->_is_running_ch2) {
		pr_info("Video Channel is still running so return!\n");
		return 0;
	}

	dev->_channel2_upstream_select = channel_select;
	sram_ch = dev->channels[channel_select].sram_channels;

	INIT_WORK(&dev->_irq_work_entry_ch2, cx25821_vidups_handler_ch2);
	dev->_irq_queues_ch2 =
	    create_singlethread_workqueue("cx25821_workqueue2");

	if (!dev->_irq_queues_ch2) {
		pr_err("create_singlethread_workqueue() for Video FAILED!\n");
		return -ENOMEM;
	}
	/*
	 * 656/VIP SRC Upstream Channel I & J and 7 -
	 * Host Bus Interface for channel A-C
	 */
	tmp = cx_read(VID_CH_MODE_SEL);
	cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);

	dev->_is_running_ch2 = 0;
	dev->_frame_count_ch2 = 0;
	dev->_file_status_ch2 = RESET_STATUS;
	dev->_lines_count_ch2 = dev->_isNTSC_ch2 ? 480 : 576;
	dev->_pixel_format_ch2 = pixel_format;
	dev->_line_size_ch2 = (dev->_pixel_format_ch2 == PIXEL_FRMT_422) ?
		(WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
	data_frame_size = dev->_isNTSC_ch2 ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
	risc_buffer_size = dev->_isNTSC_ch2 ?
		NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;

	if (dev->input_filename_ch2) {
		str_length = strlen(dev->input_filename_ch2);
		dev->_filename_ch2 = kmemdup(dev->input_filename_ch2,
					     str_length + 1, GFP_KERNEL);

		if (!dev->_filename_ch2)
			goto error;
	} else {
		str_length = strlen(dev->_defaultname_ch2);
		dev->_filename_ch2 = kmemdup(dev->_defaultname_ch2,
					     str_length + 1, GFP_KERNEL);

		if (!dev->_filename_ch2)
			goto error;
	}

	/* Default if filename is empty string */
	if (strcmp(dev->input_filename_ch2, "") == 0) {
		if (dev->_isNTSC_ch2) {
			dev->_filename_ch2 = (dev->_pixel_format_ch2 ==
				PIXEL_FRMT_411) ? "/root/vid411.yuv" :
				"/root/vidtest.yuv";
		} else {
			dev->_filename_ch2 = (dev->_pixel_format_ch2 ==
				PIXEL_FRMT_411) ? "/root/pal411.yuv" :
				"/root/pal422.yuv";
		}
	}

	retval = cx25821_sram_channel_setup_upstream(dev, sram_ch,
						dev->_line_size_ch2, 0);

	/* setup fifo + format */
	cx25821_set_pixelengine_ch2(dev, sram_ch, dev->_pixel_format_ch2);

	dev->upstream_riscbuf_size_ch2 = risc_buffer_size * 2;
	dev->upstream_databuf_size_ch2 = data_frame_size * 2;

	/* Allocating buffers and prepare RISC program */
	retval = cx25821_upstream_buffer_prepare_ch2(dev, sram_ch,
						dev->_line_size_ch2);
	if (retval < 0) {
		pr_err("%s: Failed to set up Video upstream buffers!\n",
		       dev->name);
		goto error;
	}

	cx25821_start_video_dma_upstream_ch2(dev, sram_ch);

	return 0;

error:
	cx25821_dev_unregister(dev);

	return err;
}
int cx25821_vidupstream_init(struct cx25821_channel *chan,
				 int pixel_format)
{
	struct cx25821_video_out_data *out = chan->out;
	struct cx25821_dev *dev = chan->dev;
	const struct sram_channel *sram_ch;
	u32 tmp;
	int err = 0;
	int data_frame_size = 0;
	int risc_buffer_size = 0;

	if (out->_is_running) {
		pr_info("Video Channel is still running so return!\n");
		return 0;
	}

	sram_ch = chan->sram_channels;

	out->is_60hz = dev->tvnorm & V4L2_STD_525_60;

	/* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
	 * channel A-C
	 */
	tmp = cx_read(VID_CH_MODE_SEL);
	cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);

	out->_is_running = 0;
	out->_frame_count = 0;
	out->_file_status = RESET_STATUS;
	out->_lines_count = out->is_60hz ? 480 : 576;
	out->_pixel_format = pixel_format;
	out->_line_size = (out->_pixel_format == PIXEL_FRMT_422) ?
		(WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
	data_frame_size = out->is_60hz ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
	risc_buffer_size = out->is_60hz ?
		NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;

	out->_is_running = 0;
	out->_frame_count = 0;
	out->_file_status = RESET_STATUS;
	out->_lines_count = out->is_60hz ? 480 : 576;
	out->_pixel_format = pixel_format;
	out->_line_size = (out->_pixel_format == PIXEL_FRMT_422) ?
		(WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
	out->curpos = 0;
	init_waitqueue_head(&out->waitq);

	err = cx25821_sram_channel_setup_upstream(dev, sram_ch,
			out->_line_size, 0);

	/* setup fifo + format */
	cx25821_set_pixelengine(chan, sram_ch, out->_pixel_format);

	out->upstream_riscbuf_size = risc_buffer_size * 2;
	out->upstream_databuf_size = data_frame_size * 2;

	/* Allocating buffers and prepare RISC program */
	err = cx25821_upstream_buffer_prepare(chan, sram_ch, out->_line_size);
	if (err < 0) {
		pr_err("%s: Failed to set up Video upstream buffers!\n",
		       dev->name);
		goto error;
	}

	cx25821_start_video_dma_upstream(chan, sram_ch);

	return 0;

error:
	cx25821_dev_unregister(dev);

	return err;
}