blob: 0259a408756cf84ccb3b0c6b4ef99b6fdf05e103 [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 =
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -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,
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -0300217 dev->_data_buf_phys_addr +
218 databuf_offset, top_offset, 0, bpl,
219 odd_num_lines, fifo_enable, ODD_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300220 }
221
222 fifo_enable = FIFO_DISABLE;
223
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300224 /* Even Field */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300225 rp = cx25821_risc_field_upstream(dev, rp,
226 dev->_data_buf_phys_addr +
227 databuf_offset, bottom_offset,
228 0x200, bpl, singlefield_lines,
229 fifo_enable, EVEN_FIELD);
230
231 if (frame == 0) {
232 risc_flag = RISC_CNT_RESET;
233 risc_phys_jump_addr =
234 dev->_dma_phys_start_addr + risc_program_size;
235 } else {
236 risc_phys_jump_addr = dev->_dma_phys_start_addr;
237 risc_flag = RISC_CNT_INC;
238 }
239
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300240 /* Loop to 2ndFrameRISC or to Start of Risc
241 * program & generate IRQ
242 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300243 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
244 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
245 *(rp++) = cpu_to_le32(0);
246 }
247
248 return 0;
249}
250
251void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev)
252{
253 struct sram_channel *sram_ch =
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -0300254 dev->channels[VID_UPSTREAM_SRAM_CHANNEL_I].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300255 u32 tmp = 0;
256
257 if (!dev->_is_running) {
Joe Perches36d89f72010-11-07 17:48:21 -0300258 pr_info("No video file is currently running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300259 return;
260 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300261 /* Disable RISC interrupts */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300262 tmp = cx_read(sram_ch->int_msk);
263 cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
264
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300265 /* Turn OFF risc and fifo enable */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300266 tmp = cx_read(sram_ch->dma_ctl);
267 cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
268
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300269 /* Clear data buffer memory */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300270 if (dev->_data_buf_virt_addr)
271 memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
272
273 dev->_is_running = 0;
274 dev->_is_first_frame = 0;
275 dev->_frame_count = 0;
276 dev->_file_status = END_OF_FILE;
277
Ilia Mirkinb0091782011-03-13 00:28:58 -0500278 kfree(dev->_irq_queues);
279 dev->_irq_queues = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300280
Ilia Mirkinb0091782011-03-13 00:28:58 -0500281 kfree(dev->_filename);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300282
283 tmp = cx_read(VID_CH_MODE_SEL);
284 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
285}
286
287void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev)
288{
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300289 if (dev->_is_running)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300290 cx25821_stop_upstream_video_ch1(dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300291
292 if (dev->_dma_virt_addr) {
293 pci_free_consistent(dev->pci, dev->_risc_size,
294 dev->_dma_virt_addr, dev->_dma_phys_addr);
295 dev->_dma_virt_addr = NULL;
296 }
297
298 if (dev->_data_buf_virt_addr) {
299 pci_free_consistent(dev->pci, dev->_data_buf_size,
300 dev->_data_buf_virt_addr,
301 dev->_data_buf_phys_addr);
302 dev->_data_buf_virt_addr = NULL;
303 }
304}
305
306int cx25821_get_frame(struct cx25821_dev *dev, struct sram_channel *sram_ch)
307{
308 struct file *myfile;
309 int frame_index_temp = dev->_frame_index;
310 int i = 0;
311 int line_size =
312 (dev->_pixel_format ==
313 PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
314 int frame_size = 0;
315 int frame_offset = 0;
316 ssize_t vfs_read_retval = 0;
317 char mybuf[line_size];
318 loff_t file_offset;
319 loff_t pos;
320 mm_segment_t old_fs;
321
322 if (dev->_file_status == END_OF_FILE)
323 return 0;
324
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300325 if (dev->_isNTSC)
326 frame_size = (line_size == Y411_LINE_SZ) ?
327 FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
328 else
329 frame_size = (line_size == Y411_LINE_SZ) ?
330 FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300331
332 frame_offset = (frame_index_temp > 0) ? frame_size : 0;
333 file_offset = dev->_frame_count * frame_size;
334
335 myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
336
337 if (IS_ERR(myfile)) {
338 const int open_errno = -PTR_ERR(myfile);
Joe Perches36d89f72010-11-07 17:48:21 -0300339 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
340 __func__, dev->_filename, open_errno);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300341 return PTR_ERR(myfile);
342 } else {
343 if (!(myfile->f_op)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300344 pr_err("%s(): File has no file operations registered!\n",
345 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300346 filp_close(myfile, NULL);
347 return -EIO;
348 }
349
350 if (!myfile->f_op->read) {
Joe Perches36d89f72010-11-07 17:48:21 -0300351 pr_err("%s(): File has no READ operations registered!\n",
352 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300353 filp_close(myfile, NULL);
354 return -EIO;
355 }
356
357 pos = myfile->f_pos;
358 old_fs = get_fs();
359 set_fs(KERNEL_DS);
360
361 for (i = 0; i < dev->_lines_count; i++) {
362 pos = file_offset;
363
364 vfs_read_retval =
365 vfs_read(myfile, mybuf, line_size, &pos);
366
367 if (vfs_read_retval > 0 && vfs_read_retval == line_size
368 && dev->_data_buf_virt_addr != NULL) {
369 memcpy((void *)(dev->_data_buf_virt_addr +
370 frame_offset / 4), mybuf,
371 vfs_read_retval);
372 }
373
374 file_offset += vfs_read_retval;
375 frame_offset += vfs_read_retval;
376
377 if (vfs_read_retval < line_size) {
Joe Perches36d89f72010-11-07 17:48:21 -0300378 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
379 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300380 break;
381 }
382 }
383
384 if (i > 0)
385 dev->_frame_count++;
386
387 dev->_file_status =
388 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
389
390 set_fs(old_fs);
391 filp_close(myfile, NULL);
392 }
393
394 return 0;
395}
396
397static void cx25821_vidups_handler(struct work_struct *work)
398{
399 struct cx25821_dev *dev =
400 container_of(work, struct cx25821_dev, _irq_work_entry);
401
402 if (!dev) {
Joe Perches36d89f72010-11-07 17:48:21 -0300403 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
404 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300405 return;
406 }
407
408 cx25821_get_frame(dev,
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300409 dev->channels[dev->_channel_upstream_select].
410 sram_channels);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300411}
412
413int cx25821_openfile(struct cx25821_dev *dev, struct sram_channel *sram_ch)
414{
415 struct file *myfile;
416 int i = 0, j = 0;
417 int line_size =
418 (dev->_pixel_format ==
419 PIXEL_FRMT_411) ? Y411_LINE_SZ : Y422_LINE_SZ;
420 ssize_t vfs_read_retval = 0;
421 char mybuf[line_size];
422 loff_t pos;
423 loff_t offset = (unsigned long)0;
424 mm_segment_t old_fs;
425
426 myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
427
428 if (IS_ERR(myfile)) {
429 const int open_errno = -PTR_ERR(myfile);
Joe Perches36d89f72010-11-07 17:48:21 -0300430 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300431 __func__, dev->_filename, open_errno);
432 return PTR_ERR(myfile);
433 } else {
434 if (!(myfile->f_op)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300435 pr_err("%s(): File has no file operations registered!\n",
436 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300437 filp_close(myfile, NULL);
438 return -EIO;
439 }
440
441 if (!myfile->f_op->read) {
Joe Perches36d89f72010-11-07 17:48:21 -0300442 pr_err("%s(): File has no READ operations registered! Returning\n",
443 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300444 filp_close(myfile, NULL);
445 return -EIO;
446 }
447
448 pos = myfile->f_pos;
449 old_fs = get_fs();
450 set_fs(KERNEL_DS);
451
452 for (j = 0; j < NUM_FRAMES; j++) {
453 for (i = 0; i < dev->_lines_count; i++) {
454 pos = offset;
455
456 vfs_read_retval =
457 vfs_read(myfile, mybuf, line_size, &pos);
458
459 if (vfs_read_retval > 0
460 && vfs_read_retval == line_size
461 && dev->_data_buf_virt_addr != NULL) {
462 memcpy((void *)(dev->
463 _data_buf_virt_addr +
464 offset / 4), mybuf,
465 vfs_read_retval);
466 }
467
468 offset += vfs_read_retval;
469
470 if (vfs_read_retval < line_size) {
Joe Perches36d89f72010-11-07 17:48:21 -0300471 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
472 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300473 break;
474 }
475 }
476
477 if (i > 0)
478 dev->_frame_count++;
479
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300480 if (vfs_read_retval < line_size)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300481 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300482 }
483
484 dev->_file_status =
485 (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
486
487 set_fs(old_fs);
488 myfile->f_pos = 0;
489 filp_close(myfile, NULL);
490 }
491
492 return 0;
493}
494
495int cx25821_upstream_buffer_prepare(struct cx25821_dev *dev,
496 struct sram_channel *sram_ch, int bpl)
497{
498 int ret = 0;
499 dma_addr_t dma_addr;
500 dma_addr_t data_dma_addr;
501
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300502 if (dev->_dma_virt_addr != NULL)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300503 pci_free_consistent(dev->pci, dev->upstream_riscbuf_size,
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300504 dev->_dma_virt_addr, dev->_dma_phys_addr);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300505
506 dev->_dma_virt_addr =
507 pci_alloc_consistent(dev->pci, dev->upstream_riscbuf_size,
508 &dma_addr);
509 dev->_dma_virt_start_addr = dev->_dma_virt_addr;
510 dev->_dma_phys_start_addr = dma_addr;
511 dev->_dma_phys_addr = dma_addr;
512 dev->_risc_size = dev->upstream_riscbuf_size;
513
514 if (!dev->_dma_virt_addr) {
Joe Perches36d89f72010-11-07 17:48:21 -0300515 pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300516 return -ENOMEM;
517 }
518
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300519 /* Clear memory at address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300520 memset(dev->_dma_virt_addr, 0, dev->_risc_size);
521
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300522 if (dev->_data_buf_virt_addr != NULL)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300523 pci_free_consistent(dev->pci, dev->upstream_databuf_size,
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300524 dev->_data_buf_virt_addr,
525 dev->_data_buf_phys_addr);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300526 /* For Video Data buffer allocation */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300527 dev->_data_buf_virt_addr =
528 pci_alloc_consistent(dev->pci, dev->upstream_databuf_size,
529 &data_dma_addr);
530 dev->_data_buf_phys_addr = data_dma_addr;
531 dev->_data_buf_size = dev->upstream_databuf_size;
532
533 if (!dev->_data_buf_virt_addr) {
Joe Perches36d89f72010-11-07 17:48:21 -0300534 pr_err("FAILED to allocate memory for data buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300535 return -ENOMEM;
536 }
537
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300538 /* Clear memory at address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300539 memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
540
541 ret = cx25821_openfile(dev, sram_ch);
542 if (ret < 0)
543 return ret;
544
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300545 /* Create RISC programs */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300546 ret =
547 cx25821_risc_buffer_upstream(dev, dev->pci, 0, bpl,
548 dev->_lines_count);
549 if (ret < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300550 pr_info("Failed creating Video Upstream Risc programs!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300551 goto error;
552 }
553
554 return 0;
555
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300556error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300557 return ret;
558}
559
560int cx25821_video_upstream_irq(struct cx25821_dev *dev, int chan_num,
561 u32 status)
562{
563 u32 int_msk_tmp;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300564 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300565 int singlefield_lines = NTSC_FIELD_HEIGHT;
566 int line_size_in_bytes = Y422_LINE_SZ;
567 int odd_risc_prog_size = 0;
568 dma_addr_t risc_phys_jump_addr;
569 __le32 *rp;
570
571 if (status & FLD_VID_SRC_RISC1) {
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300572 /* We should only process one program per call */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300573 u32 prog_cnt = cx_read(channel->gpcnt);
574
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300575 /* Since we've identified our IRQ, clear our bits from the
576 * interrupt mask and interrupt status registers */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300577 int_msk_tmp = cx_read(channel->int_msk);
578 cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
579 cx_write(channel->int_stat, _intr_msk);
580
581 spin_lock(&dev->slock);
582
583 dev->_frame_index = prog_cnt;
584
585 queue_work(dev->_irq_queues, &dev->_irq_work_entry);
586
587 if (dev->_is_first_frame) {
588 dev->_is_first_frame = 0;
589
590 if (dev->_isNTSC) {
591 singlefield_lines += 1;
592 odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
593 } else {
594 singlefield_lines = PAL_FIELD_HEIGHT;
595 odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
596 }
597
598 if (dev->_dma_virt_start_addr != NULL) {
599 line_size_in_bytes =
600 (dev->_pixel_format ==
601 PIXEL_FRMT_411) ? Y411_LINE_SZ :
602 Y422_LINE_SZ;
603 risc_phys_jump_addr =
604 dev->_dma_phys_start_addr +
605 odd_risc_prog_size;
606
607 rp = cx25821_update_riscprogram(dev,
Leonid V. Fedorenchik2b2d0392011-09-02 11:55:46 +0800608 dev->_dma_virt_start_addr, TOP_OFFSET,
609 line_size_in_bytes, 0x0,
610 singlefield_lines, FIFO_DISABLE,
611 ODD_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300612
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300613 /* Jump to Even Risc program of 1st Frame */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300614 *(rp++) = cpu_to_le32(RISC_JUMP);
615 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
616 *(rp++) = cpu_to_le32(0);
617 }
618 }
619
620 spin_unlock(&dev->slock);
621 } else {
622 if (status & FLD_VID_SRC_UF)
Joe Perches36d89f72010-11-07 17:48:21 -0300623 pr_err("%s(): Video Received Underflow Error Interrupt!\n",
624 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300625
626 if (status & FLD_VID_SRC_SYNC)
Joe Perches36d89f72010-11-07 17:48:21 -0300627 pr_err("%s(): Video Received Sync Error Interrupt!\n",
628 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300629
630 if (status & FLD_VID_SRC_OPC_ERR)
Joe Perches36d89f72010-11-07 17:48:21 -0300631 pr_err("%s(): Video Received OpCode Error Interrupt!\n",
632 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300633 }
634
635 if (dev->_file_status == END_OF_FILE) {
Joe Perches36d89f72010-11-07 17:48:21 -0300636 pr_err("EOF Channel 1 Framecount = %d\n", dev->_frame_count);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300637 return -1;
638 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300639 /* ElSE, set the interrupt mask register, re-enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300640 int_msk_tmp = cx_read(channel->int_msk);
641 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
642
643 return 0;
644}
645
646static irqreturn_t cx25821_upstream_irq(int irq, void *dev_id)
647{
648 struct cx25821_dev *dev = dev_id;
649 u32 msk_stat, vid_status;
650 int handled = 0;
651 int channel_num = 0;
652 struct sram_channel *sram_ch;
653
654 if (!dev)
655 return -1;
656
657 channel_num = VID_UPSTREAM_SRAM_CHANNEL_I;
658
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300659 sram_ch = dev->channels[channel_num].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300660
661 msk_stat = cx_read(sram_ch->int_mstat);
662 vid_status = cx_read(sram_ch->int_stat);
663
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300664 /* Only deal with our interrupt */
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300665 if (vid_status)
666 handled = cx25821_video_upstream_irq(dev, channel_num,
667 vid_status);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300668
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300669 if (handled < 0)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300670 cx25821_stop_upstream_video_ch1(dev);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300671 else
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300672 handled += handled;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300673
674 return IRQ_RETVAL(handled);
675}
676
677void cx25821_set_pixelengine(struct cx25821_dev *dev, struct sram_channel *ch,
678 int pix_format)
679{
680 int width = WIDTH_D1;
681 int height = dev->_lines_count;
682 int num_lines, odd_num_lines;
683 u32 value;
684 int vip_mode = OUTPUT_FRMT_656;
685
686 value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
687 value &= 0xFFFFFFEF;
688 value |= dev->_isNTSC ? 0 : 0x10;
689 cx_write(ch->vid_fmt_ctl, value);
690
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300691 /* set number of active pixels in each line.
692 * Default is 720 pixels in both NTSC and PAL format */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300693 cx_write(ch->vid_active_ctl1, width);
694
695 num_lines = (height / 2) & 0x3FF;
696 odd_num_lines = num_lines;
697
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300698 if (dev->_isNTSC)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300699 odd_num_lines += 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300700
701 value = (num_lines << 16) | odd_num_lines;
702
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300703 /* set number of active lines in field 0 (top) and field 1 (bottom) */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300704 cx_write(ch->vid_active_ctl2, value);
705
706 cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
707}
708
709int cx25821_start_video_dma_upstream(struct cx25821_dev *dev,
710 struct sram_channel *sram_ch)
711{
712 u32 tmp = 0;
713 int err = 0;
714
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300715 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
716 * channel A-C
717 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300718 tmp = cx_read(VID_CH_MODE_SEL);
719 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
720
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300721 /* Set the physical start address of the RISC program in the initial
722 * program counter(IPC) member of the cmds.
723 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300724 cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300725 /* Risc IPC High 64 bits 63-32 */
726 cx_write(sram_ch->cmds_start + 4, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300727
728 /* reset counter */
729 cx_write(sram_ch->gpcnt_ctl, 3);
730
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300731 /* Clear our bits from the interrupt status register. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300732 cx_write(sram_ch->int_stat, _intr_msk);
733
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300734 /* Set the interrupt mask register, enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300735 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
736 tmp = cx_read(sram_ch->int_msk);
737 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
738
739 err =
740 request_irq(dev->pci->irq, cx25821_upstream_irq,
Yong Zhang18e93512011-09-07 16:10:22 +0800741 IRQF_SHARED, dev->name, dev);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300742 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300743 pr_err("%s: can't get upstream IRQ %d\n",
744 dev->name, dev->pci->irq);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300745 goto fail_irq;
746 }
747
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300748 /* Start the DMA engine */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300749 tmp = cx_read(sram_ch->dma_ctl);
750 cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
751
752 dev->_is_running = 1;
753 dev->_is_first_frame = 1;
754
755 return 0;
756
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300757fail_irq:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300758 cx25821_dev_unregister(dev);
759 return err;
760}
761
762int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev, int channel_select,
763 int pixel_format)
764{
765 struct sram_channel *sram_ch;
766 u32 tmp;
767 int retval = 0;
768 int err = 0;
769 int data_frame_size = 0;
770 int risc_buffer_size = 0;
771 int str_length = 0;
772
773 if (dev->_is_running) {
Joe Perches36d89f72010-11-07 17:48:21 -0300774 pr_info("Video Channel is still running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300775 return 0;
776 }
777
778 dev->_channel_upstream_select = channel_select;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300779 sram_ch = dev->channels[channel_select].sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300780
781 INIT_WORK(&dev->_irq_work_entry, cx25821_vidups_handler);
782 dev->_irq_queues = create_singlethread_workqueue("cx25821_workqueue");
783
784 if (!dev->_irq_queues) {
Joe Perches36d89f72010-11-07 17:48:21 -0300785 pr_err("create_singlethread_workqueue() for Video FAILED!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300786 return -ENOMEM;
787 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300788 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
789 * channel A-C
790 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300791 tmp = cx_read(VID_CH_MODE_SEL);
792 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
793
794 dev->_is_running = 0;
795 dev->_frame_count = 0;
796 dev->_file_status = RESET_STATUS;
797 dev->_lines_count = dev->_isNTSC ? 480 : 576;
798 dev->_pixel_format = pixel_format;
799 dev->_line_size =
800 (dev->_pixel_format ==
801 PIXEL_FRMT_422) ? (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
802 data_frame_size = dev->_isNTSC ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
803 risc_buffer_size =
804 dev->_isNTSC ? NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
805
806 if (dev->input_filename) {
807 str_length = strlen(dev->input_filename);
Julia Lawall32414872010-05-11 20:26:57 +0200808 dev->_filename = kmalloc(str_length + 1, GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300809
810 if (!dev->_filename)
811 goto error;
812
813 memcpy(dev->_filename, dev->input_filename, str_length + 1);
814 } else {
815 str_length = strlen(dev->_defaultname);
Julia Lawall32414872010-05-11 20:26:57 +0200816 dev->_filename = kmalloc(str_length + 1, GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300817
818 if (!dev->_filename)
819 goto error;
820
821 memcpy(dev->_filename, dev->_defaultname, str_length + 1);
822 }
823
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300824 /* Default if filename is empty string */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300825 if (strcmp(dev->input_filename, "") == 0) {
826 if (dev->_isNTSC) {
827 dev->_filename =
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -0300828 (dev->_pixel_format == PIXEL_FRMT_411) ?
829 "/root/vid411.yuv" : "/root/vidtest.yuv";
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300830 } else {
831 dev->_filename =
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -0300832 (dev->_pixel_format == PIXEL_FRMT_411) ?
833 "/root/pal411.yuv" : "/root/pal422.yuv";
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300834 }
835 }
836
837 dev->_is_running = 0;
838 dev->_frame_count = 0;
839 dev->_file_status = RESET_STATUS;
840 dev->_lines_count = dev->_isNTSC ? 480 : 576;
841 dev->_pixel_format = pixel_format;
842 dev->_line_size =
843 (dev->_pixel_format ==
844 PIXEL_FRMT_422) ? (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
845
846 retval =
847 cx25821_sram_channel_setup_upstream(dev, sram_ch, dev->_line_size,
848 0);
849
850 /* setup fifo + format */
851 cx25821_set_pixelengine(dev, sram_ch, dev->_pixel_format);
852
853 dev->upstream_riscbuf_size = risc_buffer_size * 2;
854 dev->upstream_databuf_size = data_frame_size * 2;
855
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300856 /* Allocating buffers and prepare RISC program */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300857 retval = cx25821_upstream_buffer_prepare(dev, sram_ch, dev->_line_size);
858 if (retval < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300859 pr_err("%s: Failed to set up Video upstream buffers!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300860 dev->name);
861 goto error;
862 }
863
864 cx25821_start_video_dma_upstream(dev, sram_ch);
865
866 return 0;
867
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300868error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300869 cx25821_dev_unregister(dev);
870
871 return err;
872}