blob: 61b5397375601137a46c9800ec66ed2d0c35f5c3 [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.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>
Olimpiu Pascariu10991232010-04-06 02:09:00 -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. Fedorenchik2b2d0392011-09-02 11:55:46 +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
46int cx25821_sram_channel_setup_upstream(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
Olimpiu Pascariu10991232010-04-06 02:09:00 -030065 if (lines > 4)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030066 lines = 4;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030067
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, (lines * 16) >> 3);
84 cx_write(ch->cmds_start + 16, ch->ctrl_start);
85
86 cx_write(ch->cmds_start + 20, VID_IQ_SIZE_DW);
87
88 for (i = 24; i < 80; i += 4)
89 cx_write(ch->cmds_start + i, 0);
90
91 /* fill registers */
92 cx_write(ch->ptr1_reg, ch->fifo_start);
93 cx_write(ch->ptr2_reg, cdt);
94 cx_write(ch->cnt2_reg, (lines * 16) >> 3);
95 cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
96
97 return 0;
98}
99
100static __le32 *cx25821_update_riscprogram(struct cx25821_dev *dev,
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300101 __le32 *rp, unsigned int offset,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300102 unsigned int bpl, u32 sync_line,
103 unsigned int lines, int fifo_enable,
104 int field_type)
105{
106 unsigned int line, i;
107 int dist_betwn_starts = bpl * 2;
108
109 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
110
111 if (USE_RISC_NOOP_VIDEO) {
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300112 for (i = 0; i < NUM_NO_OPS; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300113 *(rp++) = cpu_to_le32(RISC_NOOP);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300114 }
115
116 /* scan lines */
117 for (line = 0; line < lines; line++) {
118 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
119 *(rp++) = cpu_to_le32(dev->_data_buf_phys_addr + offset);
120 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
121
122 if ((lines <= NTSC_FIELD_HEIGHT)
123 || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC)) {
124 offset += dist_betwn_starts;
125 }
126 }
127
128 return rp;
129}
130
131static __le32 *cx25821_risc_field_upstream(struct cx25821_dev *dev, __le32 * rp,
132 dma_addr_t databuf_phys_addr,
133 unsigned int offset, u32 sync_line,
134 unsigned int bpl, unsigned int lines,
135 int fifo_enable, int field_type)
136{
137 unsigned int line, i;
138 struct sram_channel *sram_ch =
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300139 dev->channels[dev->_channel_upstream_select].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300140 int dist_betwn_starts = bpl * 2;
141
142 /* sync instruction */
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300143 if (sync_line != NO_SYNC_LINE)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300144 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300145
146 if (USE_RISC_NOOP_VIDEO) {
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300147 for (i = 0; i < NUM_NO_OPS; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300148 *(rp++) = cpu_to_le32(RISC_NOOP);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300149 }
150
151 /* scan lines */
152 for (line = 0; line < lines; line++) {
153 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
154 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
155 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
156
157 if ((lines <= NTSC_FIELD_HEIGHT)
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300158 || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC))
159 /* to skip the other field line */
160 offset += dist_betwn_starts;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300161
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300162 /* check if we need to enable the FIFO after the first 4 lines
163 * For the upstream video channel, the risc engine will enable
164 * the FIFO. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300165 if (fifo_enable && line == 3) {
166 *(rp++) = RISC_WRITECR;
167 *(rp++) = sram_ch->dma_ctl;
168 *(rp++) = FLD_VID_FIFO_EN;
169 *(rp++) = 0x00000001;
170 }
171 }
172
173 return rp;
174}
175
176int cx25821_risc_buffer_upstream(struct cx25821_dev *dev,
177 struct pci_dev *pci,
178 unsigned int top_offset,
179 unsigned int bpl, unsigned int lines)
180{
181 __le32 *rp;
182 int fifo_enable = 0;
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300183 /* get line count for single field */
184 int singlefield_lines = lines >> 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300185 int odd_num_lines = singlefield_lines;
186 int frame = 0;
187 int frame_size = 0;
188 int databuf_offset = 0;
189 int risc_program_size = 0;
190 int risc_flag = RISC_CNT_RESET;
191 unsigned int bottom_offset = bpl;
192 dma_addr_t risc_phys_jump_addr;
193
194 if (dev->_isNTSC) {
195 odd_num_lines = singlefield_lines + 1;
196 risc_program_size = FRAME1_VID_PROG_SIZE;
197 frame_size =
198 (bpl ==
199 Y411_LINE_SZ) ? FRAME_SIZE_NTSC_Y411 :
200 FRAME_SIZE_NTSC_Y422;
201 } else {
202 risc_program_size = PAL_VID_PROG_SIZE;
203 frame_size =
204 (bpl ==
205 Y411_LINE_SZ) ? FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
206 }
207
208 /* Virtual address of Risc buffer program */
209 rp = dev->_dma_virt_addr;
210
211 for (frame = 0; frame < NUM_FRAMES; frame++) {
212 databuf_offset = frame_size * frame;
213
214 if (UNSET != top_offset) {
215 fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
216 rp = cx25821_risc_field_upstream(dev, rp,
217 dev->
218 _data_buf_phys_addr +
219 databuf_offset,
220 top_offset, 0, bpl,
221 odd_num_lines,
222 fifo_enable,
223 ODD_FIELD);
224 }
225
226 fifo_enable = FIFO_DISABLE;
227
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300228 /* Even Field */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300229 rp = cx25821_risc_field_upstream(dev, rp,
230 dev->_data_buf_phys_addr +
231 databuf_offset, bottom_offset,
232 0x200, bpl, singlefield_lines,
233 fifo_enable, EVEN_FIELD);
234
235 if (frame == 0) {
236 risc_flag = RISC_CNT_RESET;
237 risc_phys_jump_addr =
238 dev->_dma_phys_start_addr + risc_program_size;
239 } else {
240 risc_phys_jump_addr = dev->_dma_phys_start_addr;
241 risc_flag = RISC_CNT_INC;
242 }
243
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300244 /* Loop to 2ndFrameRISC or to Start of Risc
245 * program & generate IRQ
246 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300247 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
248 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
249 *(rp++) = cpu_to_le32(0);
250 }
251
252 return 0;
253}
254
255void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev)
256{
257 struct sram_channel *sram_ch =
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300258 dev->channels[VID_UPSTREAM_SRAM_CHANNEL_I].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300259 u32 tmp = 0;
260
261 if (!dev->_is_running) {
Joe Perches36d89f72010-11-07 17:48:21 -0300262 pr_info("No video file is currently running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300263 return;
264 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300265 /* Disable RISC interrupts */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300266 tmp = cx_read(sram_ch->int_msk);
267 cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
268
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300269 /* Turn OFF risc and fifo enable */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300270 tmp = cx_read(sram_ch->dma_ctl);
271 cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
272
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300273 /* Clear data buffer memory */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300274 if (dev->_data_buf_virt_addr)
275 memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
276
277 dev->_is_running = 0;
278 dev->_is_first_frame = 0;
279 dev->_frame_count = 0;
280 dev->_file_status = END_OF_FILE;
281
Ilia Mirkinb0091782011-03-13 00:28:58 -0500282 kfree(dev->_irq_queues);
283 dev->_irq_queues = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300284
Ilia Mirkinb0091782011-03-13 00:28:58 -0500285 kfree(dev->_filename);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300286
287 tmp = cx_read(VID_CH_MODE_SEL);
288 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
289}
290
291void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev)
292{
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300293 if (dev->_is_running)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300294 cx25821_stop_upstream_video_ch1(dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300295
296 if (dev->_dma_virt_addr) {
297 pci_free_consistent(dev->pci, dev->_risc_size,
298 dev->_dma_virt_addr, dev->_dma_phys_addr);
299 dev->_dma_virt_addr = NULL;
300 }
301
302 if (dev->_data_buf_virt_addr) {
303 pci_free_consistent(dev->pci, dev->_data_buf_size,
304 dev->_data_buf_virt_addr,
305 dev->_data_buf_phys_addr);
306 dev->_data_buf_virt_addr = NULL;
307 }
308}
309
310int cx25821_get_frame(struct cx25821_dev *dev, struct sram_channel *sram_ch)
311{
312 struct file *myfile;
313 int frame_index_temp = dev->_frame_index;
314 int i = 0;
315 int line_size =
316 (dev->_pixel_format ==
317 PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
318 int frame_size = 0;
319 int frame_offset = 0;
320 ssize_t vfs_read_retval = 0;
321 char mybuf[line_size];
322 loff_t file_offset;
323 loff_t pos;
324 mm_segment_t old_fs;
325
326 if (dev->_file_status == END_OF_FILE)
327 return 0;
328
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300329 if (dev->_isNTSC)
330 frame_size = (line_size == Y411_LINE_SZ) ?
331 FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
332 else
333 frame_size = (line_size == Y411_LINE_SZ) ?
334 FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300335
336 frame_offset = (frame_index_temp > 0) ? frame_size : 0;
337 file_offset = dev->_frame_count * frame_size;
338
339 myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
340
341 if (IS_ERR(myfile)) {
342 const int open_errno = -PTR_ERR(myfile);
Joe Perches36d89f72010-11-07 17:48:21 -0300343 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
344 __func__, dev->_filename, open_errno);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300345 return PTR_ERR(myfile);
346 } else {
347 if (!(myfile->f_op)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300348 pr_err("%s(): File has no file operations registered!\n",
349 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300350 filp_close(myfile, NULL);
351 return -EIO;
352 }
353
354 if (!myfile->f_op->read) {
Joe Perches36d89f72010-11-07 17:48:21 -0300355 pr_err("%s(): File has no READ operations registered!\n",
356 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300357 filp_close(myfile, NULL);
358 return -EIO;
359 }
360
361 pos = myfile->f_pos;
362 old_fs = get_fs();
363 set_fs(KERNEL_DS);
364
365 for (i = 0; i < dev->_lines_count; i++) {
366 pos = file_offset;
367
368 vfs_read_retval =
369 vfs_read(myfile, mybuf, line_size, &pos);
370
371 if (vfs_read_retval > 0 && vfs_read_retval == line_size
372 && dev->_data_buf_virt_addr != NULL) {
373 memcpy((void *)(dev->_data_buf_virt_addr +
374 frame_offset / 4), mybuf,
375 vfs_read_retval);
376 }
377
378 file_offset += vfs_read_retval;
379 frame_offset += vfs_read_retval;
380
381 if (vfs_read_retval < line_size) {
Joe Perches36d89f72010-11-07 17:48:21 -0300382 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
383 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300384 break;
385 }
386 }
387
388 if (i > 0)
389 dev->_frame_count++;
390
391 dev->_file_status =
392 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
393
394 set_fs(old_fs);
395 filp_close(myfile, NULL);
396 }
397
398 return 0;
399}
400
401static void cx25821_vidups_handler(struct work_struct *work)
402{
403 struct cx25821_dev *dev =
404 container_of(work, struct cx25821_dev, _irq_work_entry);
405
406 if (!dev) {
Joe Perches36d89f72010-11-07 17:48:21 -0300407 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
408 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300409 return;
410 }
411
412 cx25821_get_frame(dev,
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300413 dev->channels[dev->_channel_upstream_select].
414 sram_channels);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300415}
416
417int cx25821_openfile(struct cx25821_dev *dev, struct sram_channel *sram_ch)
418{
419 struct file *myfile;
420 int i = 0, j = 0;
421 int line_size =
422 (dev->_pixel_format ==
423 PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
424 ssize_t vfs_read_retval = 0;
425 char mybuf[line_size];
426 loff_t pos;
427 loff_t offset = (unsigned long)0;
428 mm_segment_t old_fs;
429
430 myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
431
432 if (IS_ERR(myfile)) {
433 const int open_errno = -PTR_ERR(myfile);
Joe Perches36d89f72010-11-07 17:48:21 -0300434 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300435 __func__, dev->_filename, open_errno);
436 return PTR_ERR(myfile);
437 } else {
438 if (!(myfile->f_op)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300439 pr_err("%s(): File has no file operations registered!\n",
440 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300441 filp_close(myfile, NULL);
442 return -EIO;
443 }
444
445 if (!myfile->f_op->read) {
Joe Perches36d89f72010-11-07 17:48:21 -0300446 pr_err("%s(): File has no READ operations registered! Returning\n",
447 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300448 filp_close(myfile, NULL);
449 return -EIO;
450 }
451
452 pos = myfile->f_pos;
453 old_fs = get_fs();
454 set_fs(KERNEL_DS);
455
456 for (j = 0; j < NUM_FRAMES; j++) {
457 for (i = 0; i < dev->_lines_count; i++) {
458 pos = offset;
459
460 vfs_read_retval =
461 vfs_read(myfile, mybuf, line_size, &pos);
462
463 if (vfs_read_retval > 0
464 && vfs_read_retval == line_size
465 && dev->_data_buf_virt_addr != NULL) {
466 memcpy((void *)(dev->
467 _data_buf_virt_addr +
468 offset / 4), mybuf,
469 vfs_read_retval);
470 }
471
472 offset += vfs_read_retval;
473
474 if (vfs_read_retval < line_size) {
Joe Perches36d89f72010-11-07 17:48:21 -0300475 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
476 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300477 break;
478 }
479 }
480
481 if (i > 0)
482 dev->_frame_count++;
483
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300484 if (vfs_read_retval < line_size)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300485 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300486 }
487
488 dev->_file_status =
489 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
490
491 set_fs(old_fs);
492 myfile->f_pos = 0;
493 filp_close(myfile, NULL);
494 }
495
496 return 0;
497}
498
499int cx25821_upstream_buffer_prepare(struct cx25821_dev *dev,
500 struct sram_channel *sram_ch, int bpl)
501{
502 int ret = 0;
503 dma_addr_t dma_addr;
504 dma_addr_t data_dma_addr;
505
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300506 if (dev->_dma_virt_addr != NULL)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300507 pci_free_consistent(dev->pci, dev->upstream_riscbuf_size,
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300508 dev->_dma_virt_addr, dev->_dma_phys_addr);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300509
510 dev->_dma_virt_addr =
511 pci_alloc_consistent(dev->pci, dev->upstream_riscbuf_size,
512 &dma_addr);
513 dev->_dma_virt_start_addr = dev->_dma_virt_addr;
514 dev->_dma_phys_start_addr = dma_addr;
515 dev->_dma_phys_addr = dma_addr;
516 dev->_risc_size = dev->upstream_riscbuf_size;
517
518 if (!dev->_dma_virt_addr) {
Joe Perches36d89f72010-11-07 17:48:21 -0300519 pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300520 return -ENOMEM;
521 }
522
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300523 /* Clear memory at address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300524 memset(dev->_dma_virt_addr, 0, dev->_risc_size);
525
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300526 if (dev->_data_buf_virt_addr != NULL)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300527 pci_free_consistent(dev->pci, dev->upstream_databuf_size,
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300528 dev->_data_buf_virt_addr,
529 dev->_data_buf_phys_addr);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300530 /* For Video Data buffer allocation */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300531 dev->_data_buf_virt_addr =
532 pci_alloc_consistent(dev->pci, dev->upstream_databuf_size,
533 &data_dma_addr);
534 dev->_data_buf_phys_addr = data_dma_addr;
535 dev->_data_buf_size = dev->upstream_databuf_size;
536
537 if (!dev->_data_buf_virt_addr) {
Joe Perches36d89f72010-11-07 17:48:21 -0300538 pr_err("FAILED to allocate memory for data buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300539 return -ENOMEM;
540 }
541
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300542 /* Clear memory at address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300543 memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
544
545 ret = cx25821_openfile(dev, sram_ch);
546 if (ret < 0)
547 return ret;
548
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300549 /* Create RISC programs */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300550 ret =
551 cx25821_risc_buffer_upstream(dev, dev->pci, 0, bpl,
552 dev->_lines_count);
553 if (ret < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300554 pr_info("Failed creating Video Upstream Risc programs!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300555 goto error;
556 }
557
558 return 0;
559
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300560error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300561 return ret;
562}
563
564int cx25821_video_upstream_irq(struct cx25821_dev *dev, int chan_num,
565 u32 status)
566{
567 u32 int_msk_tmp;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300568 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300569 int singlefield_lines = NTSC_FIELD_HEIGHT;
570 int line_size_in_bytes = Y422_LINE_SZ;
571 int odd_risc_prog_size = 0;
572 dma_addr_t risc_phys_jump_addr;
573 __le32 *rp;
574
575 if (status & FLD_VID_SRC_RISC1) {
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300576 /* We should only process one program per call */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300577 u32 prog_cnt = cx_read(channel->gpcnt);
578
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300579 /* Since we've identified our IRQ, clear our bits from the
580 * interrupt mask and interrupt status registers */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300581 int_msk_tmp = cx_read(channel->int_msk);
582 cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
583 cx_write(channel->int_stat, _intr_msk);
584
585 spin_lock(&dev->slock);
586
587 dev->_frame_index = prog_cnt;
588
589 queue_work(dev->_irq_queues, &dev->_irq_work_entry);
590
591 if (dev->_is_first_frame) {
592 dev->_is_first_frame = 0;
593
594 if (dev->_isNTSC) {
595 singlefield_lines += 1;
596 odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
597 } else {
598 singlefield_lines = PAL_FIELD_HEIGHT;
599 odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
600 }
601
602 if (dev->_dma_virt_start_addr != NULL) {
603 line_size_in_bytes =
604 (dev->_pixel_format ==
605 PIXEL_FRMT_411) ? Y411_LINE_SZ :
606 Y422_LINE_SZ;
607 risc_phys_jump_addr =
608 dev->_dma_phys_start_addr +
609 odd_risc_prog_size;
610
611 rp = cx25821_update_riscprogram(dev,
Leonid V. Fedorenchik2b2d0392011-09-02 11:55:46 +0800612 dev->_dma_virt_start_addr, TOP_OFFSET,
613 line_size_in_bytes, 0x0,
614 singlefield_lines, FIFO_DISABLE,
615 ODD_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300616
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300617 /* Jump to Even Risc program of 1st Frame */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300618 *(rp++) = cpu_to_le32(RISC_JUMP);
619 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
620 *(rp++) = cpu_to_le32(0);
621 }
622 }
623
624 spin_unlock(&dev->slock);
625 } else {
626 if (status & FLD_VID_SRC_UF)
Joe Perches36d89f72010-11-07 17:48:21 -0300627 pr_err("%s(): Video Received Underflow Error Interrupt!\n",
628 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300629
630 if (status & FLD_VID_SRC_SYNC)
Joe Perches36d89f72010-11-07 17:48:21 -0300631 pr_err("%s(): Video Received Sync Error Interrupt!\n",
632 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300633
634 if (status & FLD_VID_SRC_OPC_ERR)
Joe Perches36d89f72010-11-07 17:48:21 -0300635 pr_err("%s(): Video Received OpCode Error Interrupt!\n",
636 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300637 }
638
639 if (dev->_file_status == END_OF_FILE) {
Joe Perches36d89f72010-11-07 17:48:21 -0300640 pr_err("EOF Channel 1 Framecount = %d\n", dev->_frame_count);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300641 return -1;
642 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300643 /* ElSE, set the interrupt mask register, re-enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300644 int_msk_tmp = cx_read(channel->int_msk);
645 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
646
647 return 0;
648}
649
650static irqreturn_t cx25821_upstream_irq(int irq, void *dev_id)
651{
652 struct cx25821_dev *dev = dev_id;
653 u32 msk_stat, vid_status;
654 int handled = 0;
655 int channel_num = 0;
656 struct sram_channel *sram_ch;
657
658 if (!dev)
659 return -1;
660
661 channel_num = VID_UPSTREAM_SRAM_CHANNEL_I;
662
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300663 sram_ch = dev->channels[channel_num].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300664
665 msk_stat = cx_read(sram_ch->int_mstat);
666 vid_status = cx_read(sram_ch->int_stat);
667
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300668 /* Only deal with our interrupt */
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300669 if (vid_status)
670 handled = cx25821_video_upstream_irq(dev, channel_num,
671 vid_status);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300672
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300673 if (handled < 0)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300674 cx25821_stop_upstream_video_ch1(dev);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300675 else
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300676 handled += handled;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300677
678 return IRQ_RETVAL(handled);
679}
680
681void cx25821_set_pixelengine(struct cx25821_dev *dev, struct sram_channel *ch,
682 int pix_format)
683{
684 int width = WIDTH_D1;
685 int height = dev->_lines_count;
686 int num_lines, odd_num_lines;
687 u32 value;
688 int vip_mode = OUTPUT_FRMT_656;
689
690 value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
691 value &= 0xFFFFFFEF;
692 value |= dev->_isNTSC ? 0 : 0x10;
693 cx_write(ch->vid_fmt_ctl, value);
694
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300695 /* set number of active pixels in each line.
696 * Default is 720 pixels in both NTSC and PAL format */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300697 cx_write(ch->vid_active_ctl1, width);
698
699 num_lines = (height / 2) & 0x3FF;
700 odd_num_lines = num_lines;
701
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300702 if (dev->_isNTSC)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300703 odd_num_lines += 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300704
705 value = (num_lines << 16) | odd_num_lines;
706
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300707 /* set number of active lines in field 0 (top) and field 1 (bottom) */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300708 cx_write(ch->vid_active_ctl2, value);
709
710 cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
711}
712
713int cx25821_start_video_dma_upstream(struct cx25821_dev *dev,
714 struct sram_channel *sram_ch)
715{
716 u32 tmp = 0;
717 int err = 0;
718
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300719 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
720 * channel A-C
721 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300722 tmp = cx_read(VID_CH_MODE_SEL);
723 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
724
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300725 /* Set the physical start address of the RISC program in the initial
726 * program counter(IPC) member of the cmds.
727 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300728 cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300729 /* Risc IPC High 64 bits 63-32 */
730 cx_write(sram_ch->cmds_start + 4, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300731
732 /* reset counter */
733 cx_write(sram_ch->gpcnt_ctl, 3);
734
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300735 /* Clear our bits from the interrupt status register. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300736 cx_write(sram_ch->int_stat, _intr_msk);
737
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300738 /* Set the interrupt mask register, enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300739 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
740 tmp = cx_read(sram_ch->int_msk);
741 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
742
743 err =
744 request_irq(dev->pci->irq, cx25821_upstream_irq,
Yong Zhang18e93512011-09-07 16:10:22 +0800745 IRQF_SHARED, dev->name, dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300746 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300747 pr_err("%s: can't get upstream IRQ %d\n",
748 dev->name, dev->pci->irq);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300749 goto fail_irq;
750 }
751
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300752 /* Start the DMA engine */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300753 tmp = cx_read(sram_ch->dma_ctl);
754 cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
755
756 dev->_is_running = 1;
757 dev->_is_first_frame = 1;
758
759 return 0;
760
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300761fail_irq:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300762 cx25821_dev_unregister(dev);
763 return err;
764}
765
766int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev, int channel_select,
767 int pixel_format)
768{
769 struct sram_channel *sram_ch;
770 u32 tmp;
771 int retval = 0;
772 int err = 0;
773 int data_frame_size = 0;
774 int risc_buffer_size = 0;
775 int str_length = 0;
776
777 if (dev->_is_running) {
Joe Perches36d89f72010-11-07 17:48:21 -0300778 pr_info("Video Channel is still running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300779 return 0;
780 }
781
782 dev->_channel_upstream_select = channel_select;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300783 sram_ch = dev->channels[channel_select].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300784
785 INIT_WORK(&dev->_irq_work_entry, cx25821_vidups_handler);
786 dev->_irq_queues = create_singlethread_workqueue("cx25821_workqueue");
787
788 if (!dev->_irq_queues) {
Joe Perches36d89f72010-11-07 17:48:21 -0300789 pr_err("create_singlethread_workqueue() for Video FAILED!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300790 return -ENOMEM;
791 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300792 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
793 * channel A-C
794 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300795 tmp = cx_read(VID_CH_MODE_SEL);
796 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
797
798 dev->_is_running = 0;
799 dev->_frame_count = 0;
800 dev->_file_status = RESET_STATUS;
801 dev->_lines_count = dev->_isNTSC ? 480 : 576;
802 dev->_pixel_format = pixel_format;
803 dev->_line_size =
804 (dev->_pixel_format ==
805 PIXEL_FRMT_422) ? (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
806 data_frame_size = dev->_isNTSC ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
807 risc_buffer_size =
808 dev->_isNTSC ? NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
809
810 if (dev->input_filename) {
811 str_length = strlen(dev->input_filename);
Julia Lawall32414872010-05-11 20:26:57 +0200812 dev->_filename = kmalloc(str_length + 1, GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300813
814 if (!dev->_filename)
815 goto error;
816
817 memcpy(dev->_filename, dev->input_filename, str_length + 1);
818 } else {
819 str_length = strlen(dev->_defaultname);
Julia Lawall32414872010-05-11 20:26:57 +0200820 dev->_filename = kmalloc(str_length + 1, GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300821
822 if (!dev->_filename)
823 goto error;
824
825 memcpy(dev->_filename, dev->_defaultname, str_length + 1);
826 }
827
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300828 /* Default if filename is empty string */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300829 if (strcmp(dev->input_filename, "") == 0) {
830 if (dev->_isNTSC) {
831 dev->_filename =
832 (dev->_pixel_format ==
833 PIXEL_FRMT_411) ? "/root/vid411.yuv" :
834 "/root/vidtest.yuv";
835 } else {
836 dev->_filename =
837 (dev->_pixel_format ==
838 PIXEL_FRMT_411) ? "/root/pal411.yuv" :
839 "/root/pal422.yuv";
840 }
841 }
842
843 dev->_is_running = 0;
844 dev->_frame_count = 0;
845 dev->_file_status = RESET_STATUS;
846 dev->_lines_count = dev->_isNTSC ? 480 : 576;
847 dev->_pixel_format = pixel_format;
848 dev->_line_size =
849 (dev->_pixel_format ==
850 PIXEL_FRMT_422) ? (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
851
852 retval =
853 cx25821_sram_channel_setup_upstream(dev, sram_ch, dev->_line_size,
854 0);
855
856 /* setup fifo + format */
857 cx25821_set_pixelengine(dev, sram_ch, dev->_pixel_format);
858
859 dev->upstream_riscbuf_size = risc_buffer_size * 2;
860 dev->upstream_databuf_size = data_frame_size * 2;
861
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300862 /* Allocating buffers and prepare RISC program */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300863 retval = cx25821_upstream_buffer_prepare(dev, sram_ch, dev->_line_size);
864 if (retval < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300865 pr_err("%s: Failed to set up Video upstream buffers!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300866 dev->name);
867 goto error;
868 }
869
870 cx25821_start_video_dma_upstream(dev, sram_ch);
871
872 return 0;
873
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300874error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300875 cx25821_dev_unregister(dev);
876
877 return err;
878}