blob: 1e452b85f54454e4d3f8ed3167132ac3519eb373 [file] [log] [blame]
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -03001/*
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
Joe Perches36d89f72010-11-07 17:48:21 -030023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030025#include "cx25821-video.h"
26#include "cx25821-video-upstream-ch2.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Ruslan Pisareve4115bb2010-09-27 10:01:36 -030037#include <linux/uaccess.h>
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030038
39MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
40MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
41MODULE_LICENSE("GPL");
42
Leonid V. Fedorenchik0b225972011-09-02 11:55:44 +080043static int _intr_msk = FLD_VID_SRC_RISC1 | FLD_VID_SRC_UF | FLD_VID_SRC_SYNC |
44 FLD_VID_SRC_OPC_ERR;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030045
46static __le32 *cx25821_update_riscprogram_ch2(struct cx25821_dev *dev,
Ruslan Pisareve4115bb2010-09-27 10:01:36 -030047 __le32 *rp, unsigned int offset,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030048 unsigned int bpl, u32 sync_line,
49 unsigned int lines,
50 int fifo_enable, int field_type)
51{
52 unsigned int line, i;
53 int dist_betwn_starts = bpl * 2;
54
55 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
56
57 if (USE_RISC_NOOP_VIDEO) {
Ruslan Pisareve4115bb2010-09-27 10:01:36 -030058 for (i = 0; i < NUM_NO_OPS; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030059 *(rp++) = cpu_to_le32(RISC_NOOP);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030060 }
61
62 /* scan lines */
63 for (line = 0; line < lines; line++) {
64 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
65 *(rp++) = cpu_to_le32(dev->_data_buf_phys_addr_ch2 + offset);
66 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
67
Leonid V. Fedorenchik3a59fab2011-10-22 01:43:43 -030068 if ((lines <= NTSC_FIELD_HEIGHT) ||
69 (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC_ch2)) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030070 offset += dist_betwn_starts;
71 }
72 }
73
74 return rp;
75}
76
77static __le32 *cx25821_risc_field_upstream_ch2(struct cx25821_dev *dev,
Ruslan Pisareve4115bb2010-09-27 10:01:36 -030078 __le32 *rp,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030079 dma_addr_t databuf_phys_addr,
80 unsigned int offset,
81 u32 sync_line, unsigned int bpl,
82 unsigned int lines,
83 int fifo_enable, int field_type)
84{
85 unsigned int line, i;
86 struct sram_channel *sram_ch =
Leonid V. Fedorenchik2a4492d2011-10-22 01:43:42 -030087 dev->channels[dev->_channel2_upstream_select].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030088 int dist_betwn_starts = bpl * 2;
89
90 /* sync instruction */
Ruslan Pisareve4115bb2010-09-27 10:01:36 -030091 if (sync_line != NO_SYNC_LINE)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030092 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030093
94 if (USE_RISC_NOOP_VIDEO) {
Ruslan Pisareve4115bb2010-09-27 10:01:36 -030095 for (i = 0; i < NUM_NO_OPS; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030096 *(rp++) = cpu_to_le32(RISC_NOOP);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030097 }
98
99 /* scan lines */
100 for (line = 0; line < lines; line++) {
101 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
102 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
103 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
104
Leonid V. Fedorenchik3a59fab2011-10-22 01:43:43 -0300105 if ((lines <= NTSC_FIELD_HEIGHT) ||
106 (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC_ch2)) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300107 offset += dist_betwn_starts;
108 }
109
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300110 /*
111 check if we need to enable the FIFO after the first 4 lines
112 For the upstream video channel, the risc engine will enable
113 the FIFO.
114 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300115 if (fifo_enable && line == 3) {
116 *(rp++) = RISC_WRITECR;
117 *(rp++) = sram_ch->dma_ctl;
118 *(rp++) = FLD_VID_FIFO_EN;
119 *(rp++) = 0x00000001;
120 }
121 }
122
123 return rp;
124}
125
126int cx25821_risc_buffer_upstream_ch2(struct cx25821_dev *dev,
127 struct pci_dev *pci,
128 unsigned int top_offset, unsigned int bpl,
129 unsigned int lines)
130{
131 __le32 *rp;
132 int fifo_enable = 0;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300133 int singlefield_lines = lines >> 1; /*get line count for single field */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300134 int odd_num_lines = singlefield_lines;
135 int frame = 0;
136 int frame_size = 0;
137 int databuf_offset = 0;
138 int risc_program_size = 0;
139 int risc_flag = RISC_CNT_RESET;
140 unsigned int bottom_offset = bpl;
141 dma_addr_t risc_phys_jump_addr;
142
143 if (dev->_isNTSC_ch2) {
144 odd_num_lines = singlefield_lines + 1;
145 risc_program_size = FRAME1_VID_PROG_SIZE;
Leonid V. Fedorenchik9737153b2011-09-02 11:55:41 +0800146 if (bpl == Y411_LINE_SZ)
147 frame_size = FRAME_SIZE_NTSC_Y411;
148 else
149 frame_size = FRAME_SIZE_NTSC_Y422;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300150 } else {
151 risc_program_size = PAL_VID_PROG_SIZE;
Leonid V. Fedorenchik9737153b2011-09-02 11:55:41 +0800152 if (bpl == Y411_LINE_SZ)
153 frame_size = FRAME_SIZE_PAL_Y411;
154 else
155 frame_size = FRAME_SIZE_PAL_Y422;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300156 }
157
158 /* Virtual address of Risc buffer program */
159 rp = dev->_dma_virt_addr_ch2;
160
161 for (frame = 0; frame < NUM_FRAMES; frame++) {
162 databuf_offset = frame_size * frame;
163
164 if (UNSET != top_offset) {
165 fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
166 rp = cx25821_risc_field_upstream_ch2(dev, rp,
Leonid V. Fedorenchik0b225972011-09-02 11:55:44 +0800167 dev->_data_buf_phys_addr_ch2 + databuf_offset,
168 top_offset, 0, bpl, odd_num_lines, fifo_enable,
169 ODD_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300170 }
171
172 fifo_enable = FIFO_DISABLE;
173
Leonid V. Fedorenchik3940de72011-10-22 01:43:41 -0300174 /* Even field */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300175 rp = cx25821_risc_field_upstream_ch2(dev, rp,
Leonid V. Fedorenchik6d71b8f2011-09-02 11:55:43 +0800176 dev->_data_buf_phys_addr_ch2 + databuf_offset,
177 bottom_offset, 0x200, bpl, singlefield_lines,
178 fifo_enable, EVEN_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300179
180 if (frame == 0) {
181 risc_flag = RISC_CNT_RESET;
Leonid V. Fedorenchik6d71b8f2011-09-02 11:55:43 +0800182 risc_phys_jump_addr = dev->_dma_phys_start_addr_ch2 +
183 risc_program_size;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300184 } else {
185 risc_flag = RISC_CNT_INC;
186 risc_phys_jump_addr = dev->_dma_phys_start_addr_ch2;
187 }
188
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300189 /*
Leonid V. Fedorenchik3940de72011-10-22 01:43:41 -0300190 * Loop to 2ndFrameRISC or to Start of
191 * Risc program & generate IRQ
192 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300193 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
194 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
195 *(rp++) = cpu_to_le32(0);
196 }
197
198 return 0;
199}
200
201void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev)
202{
203 struct sram_channel *sram_ch =
Leonid V. Fedorenchik2a4492d2011-10-22 01:43:42 -0300204 dev->channels[VID_UPSTREAM_SRAM_CHANNEL_J].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300205 u32 tmp = 0;
206
207 if (!dev->_is_running_ch2) {
Joe Perches36d89f72010-11-07 17:48:21 -0300208 pr_info("No video file is currently running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300209 return;
210 }
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300211 /* Disable RISC interrupts */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300212 tmp = cx_read(sram_ch->int_msk);
213 cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
214
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300215 /* Turn OFF risc and fifo */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300216 tmp = cx_read(sram_ch->dma_ctl);
217 cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
218
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300219 /* Clear data buffer memory */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300220 if (dev->_data_buf_virt_addr_ch2)
221 memset(dev->_data_buf_virt_addr_ch2, 0,
222 dev->_data_buf_size_ch2);
223
224 dev->_is_running_ch2 = 0;
225 dev->_is_first_frame_ch2 = 0;
226 dev->_frame_count_ch2 = 0;
227 dev->_file_status_ch2 = END_OF_FILE;
228
Ilia Mirkinb0091782011-03-13 00:28:58 -0500229 kfree(dev->_irq_queues_ch2);
230 dev->_irq_queues_ch2 = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300231
Ilia Mirkinb0091782011-03-13 00:28:58 -0500232 kfree(dev->_filename_ch2);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300233
234 tmp = cx_read(VID_CH_MODE_SEL);
235 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
236}
237
238void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev)
239{
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300240 if (dev->_is_running_ch2)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300241 cx25821_stop_upstream_video_ch2(dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300242
243 if (dev->_dma_virt_addr_ch2) {
244 pci_free_consistent(dev->pci, dev->_risc_size_ch2,
245 dev->_dma_virt_addr_ch2,
246 dev->_dma_phys_addr_ch2);
247 dev->_dma_virt_addr_ch2 = NULL;
248 }
249
250 if (dev->_data_buf_virt_addr_ch2) {
251 pci_free_consistent(dev->pci, dev->_data_buf_size_ch2,
252 dev->_data_buf_virt_addr_ch2,
253 dev->_data_buf_phys_addr_ch2);
254 dev->_data_buf_virt_addr_ch2 = NULL;
255 }
256}
257
258int cx25821_get_frame_ch2(struct cx25821_dev *dev, struct sram_channel *sram_ch)
259{
260 struct file *myfile;
261 int frame_index_temp = dev->_frame_index_ch2;
262 int i = 0;
263 int line_size =
264 (dev->_pixel_format_ch2 ==
265 PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
266 int frame_size = 0;
267 int frame_offset = 0;
268 ssize_t vfs_read_retval = 0;
269 char mybuf[line_size];
270 loff_t file_offset;
271 loff_t pos;
272 mm_segment_t old_fs;
273
274 if (dev->_file_status_ch2 == END_OF_FILE)
275 return 0;
276
277 if (dev->_isNTSC_ch2) {
278 frame_size =
279 (line_size ==
280 Y411_LINE_SZ) ? FRAME_SIZE_NTSC_Y411 :
281 FRAME_SIZE_NTSC_Y422;
282 } else {
283 frame_size =
284 (line_size ==
285 Y411_LINE_SZ) ? FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
286 }
287
288 frame_offset = (frame_index_temp > 0) ? frame_size : 0;
289 file_offset = dev->_frame_count_ch2 * frame_size;
290
291 myfile = filp_open(dev->_filename_ch2, O_RDONLY | O_LARGEFILE, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300292 if (IS_ERR(myfile)) {
293 const int open_errno = -PTR_ERR(myfile);
Joe Perches36d89f72010-11-07 17:48:21 -0300294 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
295 __func__, dev->_filename_ch2, open_errno);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300296 return PTR_ERR(myfile);
297 } else {
298 if (!(myfile->f_op)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300299 pr_err("%s(): File has no file operations registered!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300300 __func__);
301 filp_close(myfile, NULL);
302 return -EIO;
303 }
304
305 if (!myfile->f_op->read) {
Joe Perches36d89f72010-11-07 17:48:21 -0300306 pr_err("%s(): File has no READ operations registered!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300307 __func__);
308 filp_close(myfile, NULL);
309 return -EIO;
310 }
311
312 pos = myfile->f_pos;
313 old_fs = get_fs();
314 set_fs(KERNEL_DS);
315
316 for (i = 0; i < dev->_lines_count_ch2; i++) {
317 pos = file_offset;
318
319 vfs_read_retval =
320 vfs_read(myfile, mybuf, line_size, &pos);
321
322 if (vfs_read_retval > 0 && vfs_read_retval == line_size
323 && dev->_data_buf_virt_addr_ch2 != NULL) {
324 memcpy((void *)(dev->_data_buf_virt_addr_ch2 +
325 frame_offset / 4), mybuf,
Leonid V. Fedorenchik2a4492d2011-10-22 01:43:42 -0300326 vfs_read_retval);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300327 }
328
329 file_offset += vfs_read_retval;
330 frame_offset += vfs_read_retval;
331
332 if (vfs_read_retval < line_size) {
Joe Perches36d89f72010-11-07 17:48:21 -0300333 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
334 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300335 break;
336 }
337 }
338
339 if (i > 0)
340 dev->_frame_count_ch2++;
341
342 dev->_file_status_ch2 =
343 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
344
345 set_fs(old_fs);
346 filp_close(myfile, NULL);
347 }
348
349 return 0;
350}
351
352static void cx25821_vidups_handler_ch2(struct work_struct *work)
353{
354 struct cx25821_dev *dev =
355 container_of(work, struct cx25821_dev, _irq_work_entry_ch2);
356
357 if (!dev) {
Joe Perches36d89f72010-11-07 17:48:21 -0300358 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
359 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300360 return;
361 }
362
363 cx25821_get_frame_ch2(dev,
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300364 dev->channels[dev->
365 _channel2_upstream_select].sram_channels);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300366}
367
368int cx25821_openfile_ch2(struct cx25821_dev *dev, struct sram_channel *sram_ch)
369{
370 struct file *myfile;
371 int i = 0, j = 0;
372 int line_size =
373 (dev->_pixel_format_ch2 ==
374 PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
375 ssize_t vfs_read_retval = 0;
376 char mybuf[line_size];
377 loff_t pos;
378 loff_t offset = (unsigned long)0;
379 mm_segment_t old_fs;
380
381 myfile = filp_open(dev->_filename_ch2, O_RDONLY | O_LARGEFILE, 0);
382
383 if (IS_ERR(myfile)) {
384 const int open_errno = -PTR_ERR(myfile);
Joe Perches36d89f72010-11-07 17:48:21 -0300385 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
386 __func__, dev->_filename_ch2, open_errno);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300387 return PTR_ERR(myfile);
388 } else {
389 if (!(myfile->f_op)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300390 pr_err("%s(): File has no file operations registered!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300391 __func__);
392 filp_close(myfile, NULL);
393 return -EIO;
394 }
395
396 if (!myfile->f_op->read) {
Joe Perches36d89f72010-11-07 17:48:21 -0300397 pr_err("%s(): File has no READ operations registered! Returning\n",
398 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300399 filp_close(myfile, NULL);
400 return -EIO;
401 }
402
403 pos = myfile->f_pos;
404 old_fs = get_fs();
405 set_fs(KERNEL_DS);
406
407 for (j = 0; j < NUM_FRAMES; j++) {
408 for (i = 0; i < dev->_lines_count_ch2; i++) {
409 pos = offset;
410
411 vfs_read_retval =
412 vfs_read(myfile, mybuf, line_size, &pos);
413
Leonid V. Fedorenchik3a59fab2011-10-22 01:43:43 -0300414 if (vfs_read_retval > 0 &&
415 vfs_read_retval == line_size &&
416 dev->_data_buf_virt_addr_ch2 != NULL) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300417 memcpy((void *)(dev->
418 _data_buf_virt_addr_ch2
419 + offset / 4), mybuf,
Leonid V. Fedorenchik2a4492d2011-10-22 01:43:42 -0300420 vfs_read_retval);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300421 }
422
423 offset += vfs_read_retval;
424
425 if (vfs_read_retval < line_size) {
Joe Perches36d89f72010-11-07 17:48:21 -0300426 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
427 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300428 break;
429 }
430 }
431
432 if (i > 0)
433 dev->_frame_count_ch2++;
434
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300435 if (vfs_read_retval < line_size)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300436 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300437 }
438
439 dev->_file_status_ch2 =
440 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
441
442 set_fs(old_fs);
443 myfile->f_pos = 0;
444 filp_close(myfile, NULL);
445 }
446
447 return 0;
448}
449
450static int cx25821_upstream_buffer_prepare_ch2(struct cx25821_dev *dev,
451 struct sram_channel *sram_ch,
452 int bpl)
453{
454 int ret = 0;
455 dma_addr_t dma_addr;
456 dma_addr_t data_dma_addr;
457
458 if (dev->_dma_virt_addr_ch2 != NULL) {
459 pci_free_consistent(dev->pci, dev->upstream_riscbuf_size_ch2,
460 dev->_dma_virt_addr_ch2,
461 dev->_dma_phys_addr_ch2);
462 }
463
464 dev->_dma_virt_addr_ch2 =
465 pci_alloc_consistent(dev->pci, dev->upstream_riscbuf_size_ch2,
466 &dma_addr);
467 dev->_dma_virt_start_addr_ch2 = dev->_dma_virt_addr_ch2;
468 dev->_dma_phys_start_addr_ch2 = dma_addr;
469 dev->_dma_phys_addr_ch2 = dma_addr;
470 dev->_risc_size_ch2 = dev->upstream_riscbuf_size_ch2;
471
472 if (!dev->_dma_virt_addr_ch2) {
Joe Perches36d89f72010-11-07 17:48:21 -0300473 pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300474 return -ENOMEM;
475 }
476
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300477 /* Iniitize at this address until n bytes to 0 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300478 memset(dev->_dma_virt_addr_ch2, 0, dev->_risc_size_ch2);
479
480 if (dev->_data_buf_virt_addr_ch2 != NULL) {
481 pci_free_consistent(dev->pci, dev->upstream_databuf_size_ch2,
482 dev->_data_buf_virt_addr_ch2,
483 dev->_data_buf_phys_addr_ch2);
484 }
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300485 /* For Video Data buffer allocation */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300486 dev->_data_buf_virt_addr_ch2 =
487 pci_alloc_consistent(dev->pci, dev->upstream_databuf_size_ch2,
488 &data_dma_addr);
489 dev->_data_buf_phys_addr_ch2 = data_dma_addr;
490 dev->_data_buf_size_ch2 = dev->upstream_databuf_size_ch2;
491
492 if (!dev->_data_buf_virt_addr_ch2) {
Joe Perches36d89f72010-11-07 17:48:21 -0300493 pr_err("FAILED to allocate memory for data buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300494 return -ENOMEM;
495 }
496
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300497 /* Initialize at this address until n bytes to 0 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300498 memset(dev->_data_buf_virt_addr_ch2, 0, dev->_data_buf_size_ch2);
499
500 ret = cx25821_openfile_ch2(dev, sram_ch);
501 if (ret < 0)
502 return ret;
503
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300504 /* Creating RISC programs */
Leonid V. Fedorenchik6d71b8f2011-09-02 11:55:43 +0800505 ret = cx25821_risc_buffer_upstream_ch2(dev, dev->pci, 0, bpl,
506 dev->_lines_count_ch2);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300507 if (ret < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300508 pr_info("Failed creating Video Upstream Risc programs!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300509 goto error;
510 }
511
512 return 0;
513
Leonid V. Fedorenchikcc9518f2011-09-02 11:55:42 +0800514error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300515 return ret;
516}
517
518int cx25821_video_upstream_irq_ch2(struct cx25821_dev *dev, int chan_num,
519 u32 status)
520{
521 u32 int_msk_tmp;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300522 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300523 int singlefield_lines = NTSC_FIELD_HEIGHT;
524 int line_size_in_bytes = Y422_LINE_SZ;
525 int odd_risc_prog_size = 0;
526 dma_addr_t risc_phys_jump_addr;
527 __le32 *rp;
528
529 if (status & FLD_VID_SRC_RISC1) {
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300530 /* We should only process one program per call */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300531 u32 prog_cnt = cx_read(channel->gpcnt);
532
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300533 /*
534 * Since we've identified our IRQ, clear our bits from the
535 * interrupt mask and interrupt status registers
536 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300537 int_msk_tmp = cx_read(channel->int_msk);
538 cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
539 cx_write(channel->int_stat, _intr_msk);
540
541 spin_lock(&dev->slock);
542
543 dev->_frame_index_ch2 = prog_cnt;
544
545 queue_work(dev->_irq_queues_ch2, &dev->_irq_work_entry_ch2);
546
547 if (dev->_is_first_frame_ch2) {
548 dev->_is_first_frame_ch2 = 0;
549
550 if (dev->_isNTSC_ch2) {
551 singlefield_lines += 1;
552 odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
553 } else {
554 singlefield_lines = PAL_FIELD_HEIGHT;
555 odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
556 }
557
558 if (dev->_dma_virt_start_addr_ch2 != NULL) {
Leonid V. Fedorenchik9737153b2011-09-02 11:55:41 +0800559 if (dev->_pixel_format_ch2 == PIXEL_FRMT_411)
560 line_size_in_bytes = Y411_LINE_SZ;
561 else
562 line_size_in_bytes = Y422_LINE_SZ;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300563 risc_phys_jump_addr =
Leonid V. Fedorenchik2a4492d2011-10-22 01:43:42 -0300564 dev->_dma_phys_start_addr_ch2 +
565 odd_risc_prog_size;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300566
567 rp = cx25821_update_riscprogram_ch2(dev,
Leonid V. Fedorenchik0b225972011-09-02 11:55:44 +0800568 dev->_dma_virt_start_addr_ch2,
569 TOP_OFFSET, line_size_in_bytes,
570 0x0, singlefield_lines,
571 FIFO_DISABLE, ODD_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300572
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300573 /* Jump to Even Risc program of 1st Frame */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300574 *(rp++) = cpu_to_le32(RISC_JUMP);
575 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
576 *(rp++) = cpu_to_le32(0);
577 }
578 }
579
580 spin_unlock(&dev->slock);
581 }
582
583 if (dev->_file_status_ch2 == END_OF_FILE) {
Joe Perches36d89f72010-11-07 17:48:21 -0300584 pr_info("EOF Channel 2 Framecount = %d\n",
585 dev->_frame_count_ch2);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300586 return -1;
587 }
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300588 /* ElSE, set the interrupt mask register, re-enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300589 int_msk_tmp = cx_read(channel->int_msk);
590 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
591
592 return 0;
593}
594
595static irqreturn_t cx25821_upstream_irq_ch2(int irq, void *dev_id)
596{
597 struct cx25821_dev *dev = dev_id;
598 u32 msk_stat, vid_status;
599 int handled = 0;
600 int channel_num = 0;
601 struct sram_channel *sram_ch;
602
603 if (!dev)
604 return -1;
605
606 channel_num = VID_UPSTREAM_SRAM_CHANNEL_J;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300607 sram_ch = dev->channels[channel_num].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300608
609 msk_stat = cx_read(sram_ch->int_mstat);
610 vid_status = cx_read(sram_ch->int_stat);
611
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300612 /* Only deal with our interrupt */
Leonid V. Fedorenchikdf253422011-10-22 01:43:44 -0300613 if (vid_status)
614 handled = cx25821_video_upstream_irq_ch2(dev, channel_num,
615 vid_status);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300616
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300617 if (handled < 0)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300618 cx25821_stop_upstream_video_ch2(dev);
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300619 else
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300620 handled += handled;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300621
622 return IRQ_RETVAL(handled);
623}
624
625static void cx25821_set_pixelengine_ch2(struct cx25821_dev *dev,
626 struct sram_channel *ch, int pix_format)
627{
628 int width = WIDTH_D1;
629 int height = dev->_lines_count_ch2;
630 int num_lines, odd_num_lines;
631 u32 value;
632 int vip_mode = PIXEL_ENGINE_VIP1;
633
634 value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
635 value &= 0xFFFFFFEF;
636 value |= dev->_isNTSC_ch2 ? 0 : 0x10;
637 cx_write(ch->vid_fmt_ctl, value);
638
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300639 /*
640 * set number of active pixels in each line. Default is 720
641 * pixels in both NTSC and PAL format
642 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300643 cx_write(ch->vid_active_ctl1, width);
644
645 num_lines = (height / 2) & 0x3FF;
646 odd_num_lines = num_lines;
647
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300648 if (dev->_isNTSC_ch2)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300649 odd_num_lines += 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300650
651 value = (num_lines << 16) | odd_num_lines;
652
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300653 /* set number of active lines in field 0 (top) and field 1 (bottom) */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300654 cx_write(ch->vid_active_ctl2, value);
655
656 cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
657}
658
659int cx25821_start_video_dma_upstream_ch2(struct cx25821_dev *dev,
660 struct sram_channel *sram_ch)
661{
662 u32 tmp = 0;
663 int err = 0;
664
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300665 /*
666 * 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface
667 * for channel A-C
668 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300669 tmp = cx_read(VID_CH_MODE_SEL);
670 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
671
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300672 /*
673 * Set the physical start address of the RISC program in the initial
674 * program counter(IPC) member of the cmds.
675 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300676 cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr_ch2);
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300677 cx_write(sram_ch->cmds_start + 4, 0); /* Risc IPC High 64 bits 63-32 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300678
679 /* reset counter */
680 cx_write(sram_ch->gpcnt_ctl, 3);
681
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300682 /* Clear our bits from the interrupt status register. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300683 cx_write(sram_ch->int_stat, _intr_msk);
684
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300685 /* Set the interrupt mask register, enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300686 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
687 tmp = cx_read(sram_ch->int_msk);
688 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
689
690 err =
691 request_irq(dev->pci->irq, cx25821_upstream_irq_ch2,
Yong Zhang18e93512011-09-07 16:10:22 +0800692 IRQF_SHARED, dev->name, dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300693 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300694 pr_err("%s: can't get upstream IRQ %d\n",
695 dev->name, dev->pci->irq);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300696 goto fail_irq;
697 }
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300698 /* Start the DMA engine */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300699 tmp = cx_read(sram_ch->dma_ctl);
700 cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
701
702 dev->_is_running_ch2 = 1;
703 dev->_is_first_frame_ch2 = 1;
704
705 return 0;
706
Leonid V. Fedorenchikcc9518f2011-09-02 11:55:42 +0800707fail_irq:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300708 cx25821_dev_unregister(dev);
709 return err;
710}
711
712int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev, int channel_select,
713 int pixel_format)
714{
715 struct sram_channel *sram_ch;
716 u32 tmp;
717 int retval = 0;
718 int err = 0;
719 int data_frame_size = 0;
720 int risc_buffer_size = 0;
721 int str_length = 0;
722
723 if (dev->_is_running_ch2) {
Joe Perches36d89f72010-11-07 17:48:21 -0300724 pr_info("Video Channel is still running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300725 return 0;
726 }
727
728 dev->_channel2_upstream_select = channel_select;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300729 sram_ch = dev->channels[channel_select].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300730
731 INIT_WORK(&dev->_irq_work_entry_ch2, cx25821_vidups_handler_ch2);
732 dev->_irq_queues_ch2 =
733 create_singlethread_workqueue("cx25821_workqueue2");
734
735 if (!dev->_irq_queues_ch2) {
Joe Perches36d89f72010-11-07 17:48:21 -0300736 pr_err("create_singlethread_workqueue() for Video FAILED!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300737 return -ENOMEM;
738 }
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300739 /*
740 * 656/VIP SRC Upstream Channel I & J and 7 -
741 * Host Bus Interface for channel A-C
742 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300743 tmp = cx_read(VID_CH_MODE_SEL);
744 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
745
746 dev->_is_running_ch2 = 0;
747 dev->_frame_count_ch2 = 0;
748 dev->_file_status_ch2 = RESET_STATUS;
749 dev->_lines_count_ch2 = dev->_isNTSC_ch2 ? 480 : 576;
750 dev->_pixel_format_ch2 = pixel_format;
751 dev->_line_size_ch2 =
752 (dev->_pixel_format_ch2 ==
753 PIXEL_FRMT_422) ? (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
754 data_frame_size = dev->_isNTSC_ch2 ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
755 risc_buffer_size =
756 dev->_isNTSC_ch2 ? NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
757
758 if (dev->input_filename_ch2) {
759 str_length = strlen(dev->input_filename_ch2);
Julia Lawall32414872010-05-11 20:26:57 +0200760 dev->_filename_ch2 = kmalloc(str_length + 1, GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300761
762 if (!dev->_filename_ch2)
763 goto error;
764
765 memcpy(dev->_filename_ch2, dev->input_filename_ch2,
766 str_length + 1);
767 } else {
768 str_length = strlen(dev->_defaultname_ch2);
Julia Lawall32414872010-05-11 20:26:57 +0200769 dev->_filename_ch2 = kmalloc(str_length + 1, GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300770
771 if (!dev->_filename_ch2)
772 goto error;
773
774 memcpy(dev->_filename_ch2, dev->_defaultname_ch2,
775 str_length + 1);
776 }
777
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300778 /* Default if filename is empty string */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300779 if (strcmp(dev->input_filename_ch2, "") == 0) {
780 if (dev->_isNTSC_ch2) {
781 dev->_filename_ch2 =
782 (dev->_pixel_format_ch2 ==
783 PIXEL_FRMT_411) ? "/root/vid411.yuv" :
784 "/root/vidtest.yuv";
785 } else {
786 dev->_filename_ch2 =
787 (dev->_pixel_format_ch2 ==
788 PIXEL_FRMT_411) ? "/root/pal411.yuv" :
789 "/root/pal422.yuv";
790 }
791 }
792
Leonid V. Fedorenchik6d71b8f2011-09-02 11:55:43 +0800793 retval = cx25821_sram_channel_setup_upstream(dev, sram_ch,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300794 dev->_line_size_ch2, 0);
795
796 /* setup fifo + format */
797 cx25821_set_pixelengine_ch2(dev, sram_ch, dev->_pixel_format_ch2);
798
799 dev->upstream_riscbuf_size_ch2 = risc_buffer_size * 2;
800 dev->upstream_databuf_size_ch2 = data_frame_size * 2;
801
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300802 /* Allocating buffers and prepare RISC program */
Leonid V. Fedorenchik6d71b8f2011-09-02 11:55:43 +0800803 retval = cx25821_upstream_buffer_prepare_ch2(dev, sram_ch,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300804 dev->_line_size_ch2);
805 if (retval < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300806 pr_err("%s: Failed to set up Video upstream buffers!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300807 dev->name);
808 goto error;
809 }
810
811 cx25821_start_video_dma_upstream_ch2(dev, sram_ch);
812
813 return 0;
814
Leonid V. Fedorenchikcc9518f2011-09-02 11:55:42 +0800815error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300816 cx25821_dev_unregister(dev);
817
818 return err;
819}