blob: 37cfc834e5c0297ca1eaeb096da582716ec1896c [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,
Hans Verkuilbfef0d32013-04-13 06:28:54 -030047 const struct sram_channel *ch,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030048 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
Hans Verkuil7087d312013-04-14 12:10:32 -0300100static __le32 *cx25821_update_riscprogram(struct cx25821_channel *chan,
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{
Hans Verkuil7087d312013-04-14 12:10:32 -0300106 struct cx25821_video_out_data *out = chan->out;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300107 unsigned int line, i;
108 int dist_betwn_starts = bpl * 2;
109
110 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
111
112 if (USE_RISC_NOOP_VIDEO) {
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300113 for (i = 0; i < NUM_NO_OPS; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300114 *(rp++) = cpu_to_le32(RISC_NOOP);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300115 }
116
117 /* scan lines */
118 for (line = 0; line < lines; line++) {
119 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
Hans Verkuil7087d312013-04-14 12:10:32 -0300120 *(rp++) = cpu_to_le32(out->_data_buf_phys_addr + offset);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300121 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
122
123 if ((lines <= NTSC_FIELD_HEIGHT)
Hans Verkuil7087d312013-04-14 12:10:32 -0300124 || (line < (NTSC_FIELD_HEIGHT - 1)) || !(out->is_60hz)) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300125 offset += dist_betwn_starts;
126 }
127 }
128
129 return rp;
130}
131
Hans Verkuil7087d312013-04-14 12:10:32 -0300132static __le32 *cx25821_risc_field_upstream(struct cx25821_channel *chan, __le32 *rp,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300133 dma_addr_t databuf_phys_addr,
134 unsigned int offset, u32 sync_line,
135 unsigned int bpl, unsigned int lines,
136 int fifo_enable, int field_type)
137{
Hans Verkuil7087d312013-04-14 12:10:32 -0300138 struct cx25821_video_out_data *out = chan->out;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300139 unsigned int line, i;
Hans Verkuil7087d312013-04-14 12:10:32 -0300140 const struct sram_channel *sram_ch = chan->sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300141 int dist_betwn_starts = bpl * 2;
142
143 /* sync instruction */
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300144 if (sync_line != NO_SYNC_LINE)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300145 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300146
147 if (USE_RISC_NOOP_VIDEO) {
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300148 for (i = 0; i < NUM_NO_OPS; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300149 *(rp++) = cpu_to_le32(RISC_NOOP);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300150 }
151
152 /* scan lines */
153 for (line = 0; line < lines; line++) {
154 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
155 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
156 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
157
158 if ((lines <= NTSC_FIELD_HEIGHT)
Hans Verkuil7087d312013-04-14 12:10:32 -0300159 || (line < (NTSC_FIELD_HEIGHT - 1)) || !(out->is_60hz))
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300160 /* to skip the other field line */
161 offset += dist_betwn_starts;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300162
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300163 /* check if we need to enable the FIFO after the first 4 lines
164 * For the upstream video channel, the risc engine will enable
165 * the FIFO. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300166 if (fifo_enable && line == 3) {
167 *(rp++) = RISC_WRITECR;
168 *(rp++) = sram_ch->dma_ctl;
169 *(rp++) = FLD_VID_FIFO_EN;
170 *(rp++) = 0x00000001;
171 }
172 }
173
174 return rp;
175}
176
Hans Verkuil7087d312013-04-14 12:10:32 -0300177static int cx25821_risc_buffer_upstream(struct cx25821_channel *chan,
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300178 struct pci_dev *pci,
179 unsigned int top_offset,
180 unsigned int bpl, unsigned int lines)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300181{
Hans Verkuil7087d312013-04-14 12:10:32 -0300182 struct cx25821_video_out_data *out = chan->out;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300183 __le32 *rp;
184 int fifo_enable = 0;
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300185 /* get line count for single field */
186 int singlefield_lines = lines >> 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300187 int odd_num_lines = singlefield_lines;
188 int frame = 0;
189 int frame_size = 0;
190 int databuf_offset = 0;
191 int risc_program_size = 0;
192 int risc_flag = RISC_CNT_RESET;
193 unsigned int bottom_offset = bpl;
194 dma_addr_t risc_phys_jump_addr;
195
Hans Verkuil7087d312013-04-14 12:10:32 -0300196 if (out->is_60hz) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300197 odd_num_lines = singlefield_lines + 1;
198 risc_program_size = FRAME1_VID_PROG_SIZE;
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300199 frame_size = (bpl == Y411_LINE_SZ) ?
200 FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300201 } else {
202 risc_program_size = PAL_VID_PROG_SIZE;
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300203 frame_size = (bpl == Y411_LINE_SZ) ?
204 FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300205 }
206
207 /* Virtual address of Risc buffer program */
Hans Verkuil7087d312013-04-14 12:10:32 -0300208 rp = out->_dma_virt_addr;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300209
210 for (frame = 0; frame < NUM_FRAMES; frame++) {
211 databuf_offset = frame_size * frame;
212
213 if (UNSET != top_offset) {
214 fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
Hans Verkuil7087d312013-04-14 12:10:32 -0300215 rp = cx25821_risc_field_upstream(chan, rp,
216 out->_data_buf_phys_addr +
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -0300217 databuf_offset, top_offset, 0, bpl,
218 odd_num_lines, fifo_enable, ODD_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300219 }
220
221 fifo_enable = FIFO_DISABLE;
222
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300223 /* Even Field */
Hans Verkuil7087d312013-04-14 12:10:32 -0300224 rp = cx25821_risc_field_upstream(chan, rp,
225 out->_data_buf_phys_addr +
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300226 databuf_offset, bottom_offset,
227 0x200, bpl, singlefield_lines,
228 fifo_enable, EVEN_FIELD);
229
230 if (frame == 0) {
231 risc_flag = RISC_CNT_RESET;
Hans Verkuil7087d312013-04-14 12:10:32 -0300232 risc_phys_jump_addr = out->_dma_phys_start_addr +
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300233 risc_program_size;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300234 } else {
Hans Verkuil7087d312013-04-14 12:10:32 -0300235 risc_phys_jump_addr = out->_dma_phys_start_addr;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300236 risc_flag = RISC_CNT_INC;
237 }
238
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300239 /* Loop to 2ndFrameRISC or to Start of Risc
240 * program & generate IRQ
241 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300242 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
243 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
244 *(rp++) = cpu_to_le32(0);
245 }
246
247 return 0;
248}
249
Hans Verkuil7087d312013-04-14 12:10:32 -0300250void cx25821_stop_upstream_video(struct cx25821_channel *chan)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300251{
Hans Verkuil7087d312013-04-14 12:10:32 -0300252 struct cx25821_video_out_data *out = chan->out;
253 struct cx25821_dev *dev = chan->dev;
254 const struct sram_channel *sram_ch = chan->sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300255 u32 tmp = 0;
256
Hans Verkuil7087d312013-04-14 12:10:32 -0300257 if (!out->_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 */
Hans Verkuil7087d312013-04-14 12:10:32 -0300270 if (out->_data_buf_virt_addr)
271 memset(out->_data_buf_virt_addr, 0, out->_data_buf_size);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300272
Hans Verkuil7087d312013-04-14 12:10:32 -0300273 out->_is_running = 0;
274 out->_is_first_frame = 0;
275 out->_frame_count = 0;
276 out->_file_status = END_OF_FILE;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300277
Hans Verkuil7087d312013-04-14 12:10:32 -0300278 destroy_workqueue(out->_irq_queues);
279 out->_irq_queues = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300280
Hans Verkuil7087d312013-04-14 12:10:32 -0300281 kfree(out->_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
Hans Verkuil7087d312013-04-14 12:10:32 -0300287void cx25821_free_mem_upstream(struct cx25821_channel *chan)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300288{
Hans Verkuil7087d312013-04-14 12:10:32 -0300289 struct cx25821_video_out_data *out = chan->out;
290 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300291
Hans Verkuil7087d312013-04-14 12:10:32 -0300292 if (out->_is_running)
293 cx25821_stop_upstream_video(chan);
294
295 if (out->_dma_virt_addr) {
296 pci_free_consistent(dev->pci, out->_risc_size,
297 out->_dma_virt_addr, out->_dma_phys_addr);
298 out->_dma_virt_addr = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300299 }
300
Hans Verkuil7087d312013-04-14 12:10:32 -0300301 if (out->_data_buf_virt_addr) {
302 pci_free_consistent(dev->pci, out->_data_buf_size,
303 out->_data_buf_virt_addr,
304 out->_data_buf_phys_addr);
305 out->_data_buf_virt_addr = NULL;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300306 }
307}
308
Hans Verkuil7087d312013-04-14 12:10:32 -0300309static int cx25821_get_frame(struct cx25821_channel *chan,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300310 const struct sram_channel *sram_ch)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300311{
Hans Verkuil7087d312013-04-14 12:10:32 -0300312 struct cx25821_video_out_data *out = chan->out;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300313 struct file *myfile;
Hans Verkuil7087d312013-04-14 12:10:32 -0300314 int frame_index_temp = out->_frame_index;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300315 int i = 0;
Hans Verkuil7087d312013-04-14 12:10:32 -0300316 int line_size = (out->_pixel_format == PIXEL_FRMT_411) ?
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300317 Y411_LINE_SZ : Y422_LINE_SZ;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300318 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
Hans Verkuil7087d312013-04-14 12:10:32 -0300326 if (out->_file_status == END_OF_FILE)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300327 return 0;
328
Hans Verkuil7087d312013-04-14 12:10:32 -0300329 if (out->is_60hz)
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300330 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;
Hans Verkuil7087d312013-04-14 12:10:32 -0300337 file_offset = out->_frame_count * frame_size;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300338
Hans Verkuil7087d312013-04-14 12:10:32 -0300339 myfile = filp_open(out->_filename, O_RDONLY | O_LARGEFILE, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300340
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",
Hans Verkuil7087d312013-04-14 12:10:32 -0300344 __func__, out->_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
Hans Verkuil7087d312013-04-14 12:10:32 -0300365 for (i = 0; i < out->_lines_count; i++) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300366 pos = file_offset;
367
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300368 vfs_read_retval = vfs_read(myfile, mybuf, line_size,
369 &pos);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300370
371 if (vfs_read_retval > 0 && vfs_read_retval == line_size
Hans Verkuil7087d312013-04-14 12:10:32 -0300372 && out->_data_buf_virt_addr != NULL) {
373 memcpy((void *)(out->_data_buf_virt_addr +
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300374 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)
Hans Verkuil7087d312013-04-14 12:10:32 -0300389 out->_frame_count++;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300390
Hans Verkuil7087d312013-04-14 12:10:32 -0300391 out->_file_status = (vfs_read_retval == line_size) ?
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300392 IN_PROGRESS : END_OF_FILE;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300393
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{
Hans Verkuil7087d312013-04-14 12:10:32 -0300403 struct cx25821_video_out_data *out =
404 container_of(work, struct cx25821_video_out_data, _irq_work_entry);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300405
Hans Verkuil7087d312013-04-14 12:10:32 -0300406 cx25821_get_frame(out->chan, out->chan->sram_channels);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300407}
408
Hans Verkuil7087d312013-04-14 12:10:32 -0300409static int cx25821_openfile(struct cx25821_channel *chan,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300410 const struct sram_channel *sram_ch)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300411{
Hans Verkuil7087d312013-04-14 12:10:32 -0300412 struct cx25821_video_out_data *out = chan->out;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300413 struct file *myfile;
414 int i = 0, j = 0;
Hans Verkuil7087d312013-04-14 12:10:32 -0300415 int line_size = (out->_pixel_format == PIXEL_FRMT_411) ?
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300416 Y411_LINE_SZ : Y422_LINE_SZ;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300417 ssize_t vfs_read_retval = 0;
418 char mybuf[line_size];
419 loff_t pos;
420 loff_t offset = (unsigned long)0;
421 mm_segment_t old_fs;
422
Hans Verkuil7087d312013-04-14 12:10:32 -0300423 myfile = filp_open(out->_filename, O_RDONLY | O_LARGEFILE, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300424
425 if (IS_ERR(myfile)) {
426 const int open_errno = -PTR_ERR(myfile);
Joe Perches36d89f72010-11-07 17:48:21 -0300427 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
Hans Verkuil7087d312013-04-14 12:10:32 -0300428 __func__, out->_filename, open_errno);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300429 return PTR_ERR(myfile);
430 } else {
431 if (!(myfile->f_op)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300432 pr_err("%s(): File has no file operations registered!\n",
433 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300434 filp_close(myfile, NULL);
435 return -EIO;
436 }
437
438 if (!myfile->f_op->read) {
Joe Perches36d89f72010-11-07 17:48:21 -0300439 pr_err("%s(): File has no READ operations registered! Returning\n",
440 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300441 filp_close(myfile, NULL);
442 return -EIO;
443 }
444
445 pos = myfile->f_pos;
446 old_fs = get_fs();
447 set_fs(KERNEL_DS);
448
449 for (j = 0; j < NUM_FRAMES; j++) {
Hans Verkuil7087d312013-04-14 12:10:32 -0300450 for (i = 0; i < out->_lines_count; i++) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300451 pos = offset;
452
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300453 vfs_read_retval = vfs_read(myfile, mybuf,
454 line_size, &pos);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300455
456 if (vfs_read_retval > 0
457 && vfs_read_retval == line_size
Hans Verkuil7087d312013-04-14 12:10:32 -0300458 && out->_data_buf_virt_addr != NULL) {
459 memcpy((void *)(out->
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300460 _data_buf_virt_addr +
461 offset / 4), mybuf,
462 vfs_read_retval);
463 }
464
465 offset += vfs_read_retval;
466
467 if (vfs_read_retval < line_size) {
Joe Perches36d89f72010-11-07 17:48:21 -0300468 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
469 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300470 break;
471 }
472 }
473
474 if (i > 0)
Hans Verkuil7087d312013-04-14 12:10:32 -0300475 out->_frame_count++;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300476
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300477 if (vfs_read_retval < line_size)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300478 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300479 }
480
Hans Verkuil7087d312013-04-14 12:10:32 -0300481 out->_file_status = (vfs_read_retval == line_size) ?
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300482 IN_PROGRESS : END_OF_FILE;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300483
484 set_fs(old_fs);
485 myfile->f_pos = 0;
486 filp_close(myfile, NULL);
487 }
488
489 return 0;
490}
491
Hans Verkuil7087d312013-04-14 12:10:32 -0300492static int cx25821_upstream_buffer_prepare(struct cx25821_channel *chan,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300493 const struct sram_channel *sram_ch,
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300494 int bpl)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300495{
Hans Verkuil7087d312013-04-14 12:10:32 -0300496 struct cx25821_video_out_data *out = chan->out;
497 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300498 int ret = 0;
499 dma_addr_t dma_addr;
500 dma_addr_t data_dma_addr;
501
Hans Verkuil7087d312013-04-14 12:10:32 -0300502 if (out->_dma_virt_addr != NULL)
503 pci_free_consistent(dev->pci, out->upstream_riscbuf_size,
504 out->_dma_virt_addr, out->_dma_phys_addr);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300505
Hans Verkuil7087d312013-04-14 12:10:32 -0300506 out->_dma_virt_addr = pci_alloc_consistent(dev->pci,
507 out->upstream_riscbuf_size, &dma_addr);
508 out->_dma_virt_start_addr = out->_dma_virt_addr;
509 out->_dma_phys_start_addr = dma_addr;
510 out->_dma_phys_addr = dma_addr;
511 out->_risc_size = out->upstream_riscbuf_size;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300512
Hans Verkuil7087d312013-04-14 12:10:32 -0300513 if (!out->_dma_virt_addr) {
Joe Perches36d89f72010-11-07 17:48:21 -0300514 pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300515 return -ENOMEM;
516 }
517
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300518 /* Clear memory at address */
Hans Verkuil7087d312013-04-14 12:10:32 -0300519 memset(out->_dma_virt_addr, 0, out->_risc_size);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300520
Hans Verkuil7087d312013-04-14 12:10:32 -0300521 if (out->_data_buf_virt_addr != NULL)
522 pci_free_consistent(dev->pci, out->upstream_databuf_size,
523 out->_data_buf_virt_addr,
524 out->_data_buf_phys_addr);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300525 /* For Video Data buffer allocation */
Hans Verkuil7087d312013-04-14 12:10:32 -0300526 out->_data_buf_virt_addr = pci_alloc_consistent(dev->pci,
527 out->upstream_databuf_size, &data_dma_addr);
528 out->_data_buf_phys_addr = data_dma_addr;
529 out->_data_buf_size = out->upstream_databuf_size;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300530
Hans Verkuil7087d312013-04-14 12:10:32 -0300531 if (!out->_data_buf_virt_addr) {
Joe Perches36d89f72010-11-07 17:48:21 -0300532 pr_err("FAILED to allocate memory for data buffer! Returning\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300533 return -ENOMEM;
534 }
535
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300536 /* Clear memory at address */
Hans Verkuil7087d312013-04-14 12:10:32 -0300537 memset(out->_data_buf_virt_addr, 0, out->_data_buf_size);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300538
Hans Verkuil7087d312013-04-14 12:10:32 -0300539 ret = cx25821_openfile(chan, sram_ch);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300540 if (ret < 0)
541 return ret;
542
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300543 /* Create RISC programs */
Hans Verkuil7087d312013-04-14 12:10:32 -0300544 ret = cx25821_risc_buffer_upstream(chan, dev->pci, 0, bpl,
545 out->_lines_count);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300546 if (ret < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300547 pr_info("Failed creating Video Upstream Risc programs!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300548 goto error;
549 }
550
551 return 0;
552
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300553error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300554 return ret;
555}
556
Hans Verkuil7087d312013-04-14 12:10:32 -0300557static int cx25821_video_upstream_irq(struct cx25821_channel *chan, u32 status)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300558{
Hans Verkuil7087d312013-04-14 12:10:32 -0300559 struct cx25821_video_out_data *out = chan->out;
560 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300561 u32 int_msk_tmp;
Hans Verkuil7087d312013-04-14 12:10:32 -0300562 const struct sram_channel *channel = chan->sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300563 int singlefield_lines = NTSC_FIELD_HEIGHT;
564 int line_size_in_bytes = Y422_LINE_SZ;
565 int odd_risc_prog_size = 0;
566 dma_addr_t risc_phys_jump_addr;
567 __le32 *rp;
568
569 if (status & FLD_VID_SRC_RISC1) {
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300570 /* We should only process one program per call */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300571 u32 prog_cnt = cx_read(channel->gpcnt);
572
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300573 /* Since we've identified our IRQ, clear our bits from the
574 * interrupt mask and interrupt status registers */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300575 int_msk_tmp = cx_read(channel->int_msk);
576 cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
577 cx_write(channel->int_stat, _intr_msk);
578
579 spin_lock(&dev->slock);
580
Hans Verkuil7087d312013-04-14 12:10:32 -0300581 out->_frame_index = prog_cnt;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300582
Hans Verkuil7087d312013-04-14 12:10:32 -0300583 queue_work(out->_irq_queues, &out->_irq_work_entry);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300584
Hans Verkuil7087d312013-04-14 12:10:32 -0300585 if (out->_is_first_frame) {
586 out->_is_first_frame = 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300587
Hans Verkuil7087d312013-04-14 12:10:32 -0300588 if (out->is_60hz) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300589 singlefield_lines += 1;
590 odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
591 } else {
592 singlefield_lines = PAL_FIELD_HEIGHT;
593 odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
594 }
595
Hans Verkuil7087d312013-04-14 12:10:32 -0300596 if (out->_dma_virt_start_addr != NULL) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300597 line_size_in_bytes =
Hans Verkuil7087d312013-04-14 12:10:32 -0300598 (out->_pixel_format ==
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300599 PIXEL_FRMT_411) ? Y411_LINE_SZ :
600 Y422_LINE_SZ;
601 risc_phys_jump_addr =
Hans Verkuil7087d312013-04-14 12:10:32 -0300602 out->_dma_phys_start_addr +
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300603 odd_risc_prog_size;
604
Hans Verkuil7087d312013-04-14 12:10:32 -0300605 rp = cx25821_update_riscprogram(chan,
606 out->_dma_virt_start_addr, TOP_OFFSET,
Leonid V. Fedorenchik2b2d0392011-09-02 11:55:46 +0800607 line_size_in_bytes, 0x0,
608 singlefield_lines, FIFO_DISABLE,
609 ODD_FIELD);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300610
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300611 /* Jump to Even Risc program of 1st Frame */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300612 *(rp++) = cpu_to_le32(RISC_JUMP);
613 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
614 *(rp++) = cpu_to_le32(0);
615 }
616 }
617
618 spin_unlock(&dev->slock);
619 } else {
620 if (status & FLD_VID_SRC_UF)
Joe Perches36d89f72010-11-07 17:48:21 -0300621 pr_err("%s(): Video Received Underflow Error Interrupt!\n",
622 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300623
624 if (status & FLD_VID_SRC_SYNC)
Joe Perches36d89f72010-11-07 17:48:21 -0300625 pr_err("%s(): Video Received Sync Error Interrupt!\n",
626 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300627
628 if (status & FLD_VID_SRC_OPC_ERR)
Joe Perches36d89f72010-11-07 17:48:21 -0300629 pr_err("%s(): Video Received OpCode Error Interrupt!\n",
630 __func__);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300631 }
632
Hans Verkuil7087d312013-04-14 12:10:32 -0300633 if (out->_file_status == END_OF_FILE) {
634 pr_err("EOF Channel 1 Framecount = %d\n", out->_frame_count);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300635 return -1;
636 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300637 /* ElSE, set the interrupt mask register, re-enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300638 int_msk_tmp = cx_read(channel->int_msk);
639 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
640
641 return 0;
642}
643
644static irqreturn_t cx25821_upstream_irq(int irq, void *dev_id)
645{
Hans Verkuil7087d312013-04-14 12:10:32 -0300646 struct cx25821_channel *chan = dev_id;
647 struct cx25821_dev *dev = chan->dev;
Hans Verkuil30fdf032012-04-20 06:26:19 -0300648 u32 vid_status;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300649 int handled = 0;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300650 const struct sram_channel *sram_ch;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300651
652 if (!dev)
653 return -1;
654
Hans Verkuil7087d312013-04-14 12:10:32 -0300655 sram_ch = chan->sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300656
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300657 vid_status = cx_read(sram_ch->int_stat);
658
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300659 /* Only deal with our interrupt */
Leonid V. Fedorenchik16f0fda2011-10-22 01:43:46 -0300660 if (vid_status)
Hans Verkuil7087d312013-04-14 12:10:32 -0300661 handled = cx25821_video_upstream_irq(chan, vid_status);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300662
663 return IRQ_RETVAL(handled);
664}
665
Hans Verkuil7087d312013-04-14 12:10:32 -0300666static void cx25821_set_pixelengine(struct cx25821_channel *chan,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300667 const struct sram_channel *ch,
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300668 int pix_format)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300669{
Hans Verkuil7087d312013-04-14 12:10:32 -0300670 struct cx25821_video_out_data *out = chan->out;
671 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300672 int width = WIDTH_D1;
Hans Verkuil7087d312013-04-14 12:10:32 -0300673 int height = out->_lines_count;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300674 int num_lines, odd_num_lines;
675 u32 value;
676 int vip_mode = OUTPUT_FRMT_656;
677
678 value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
679 value &= 0xFFFFFFEF;
Hans Verkuil7087d312013-04-14 12:10:32 -0300680 value |= out->is_60hz ? 0 : 0x10;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300681 cx_write(ch->vid_fmt_ctl, value);
682
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300683 /* set number of active pixels in each line.
684 * Default is 720 pixels in both NTSC and PAL format */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300685 cx_write(ch->vid_active_ctl1, width);
686
687 num_lines = (height / 2) & 0x3FF;
688 odd_num_lines = num_lines;
689
Hans Verkuil7087d312013-04-14 12:10:32 -0300690 if (out->is_60hz)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300691 odd_num_lines += 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300692
693 value = (num_lines << 16) | odd_num_lines;
694
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300695 /* set number of active lines in field 0 (top) and field 1 (bottom) */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300696 cx_write(ch->vid_active_ctl2, value);
697
698 cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
699}
700
Hans Verkuil7087d312013-04-14 12:10:32 -0300701static int cx25821_start_video_dma_upstream(struct cx25821_channel *chan,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300702 const struct sram_channel *sram_ch)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300703{
Hans Verkuil7087d312013-04-14 12:10:32 -0300704 struct cx25821_video_out_data *out = chan->out;
705 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300706 u32 tmp = 0;
707 int err = 0;
708
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300709 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
710 * channel A-C
711 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300712 tmp = cx_read(VID_CH_MODE_SEL);
713 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
714
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300715 /* Set the physical start address of the RISC program in the initial
716 * program counter(IPC) member of the cmds.
717 */
Hans Verkuil7087d312013-04-14 12:10:32 -0300718 cx_write(sram_ch->cmds_start + 0, out->_dma_phys_addr);
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300719 /* Risc IPC High 64 bits 63-32 */
720 cx_write(sram_ch->cmds_start + 4, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300721
722 /* reset counter */
723 cx_write(sram_ch->gpcnt_ctl, 3);
724
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300725 /* Clear our bits from the interrupt status register. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300726 cx_write(sram_ch->int_stat, _intr_msk);
727
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300728 /* Set the interrupt mask register, enable irq. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300729 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
730 tmp = cx_read(sram_ch->int_msk);
731 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
732
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300733 err = request_irq(dev->pci->irq, cx25821_upstream_irq,
Hans Verkuil7087d312013-04-14 12:10:32 -0300734 IRQF_SHARED, dev->name, chan);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300735 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300736 pr_err("%s: can't get upstream IRQ %d\n",
737 dev->name, dev->pci->irq);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300738 goto fail_irq;
739 }
740
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300741 /* Start the DMA engine */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300742 tmp = cx_read(sram_ch->dma_ctl);
743 cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
744
Hans Verkuil7087d312013-04-14 12:10:32 -0300745 out->_is_running = 1;
746 out->_is_first_frame = 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300747
748 return 0;
749
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300750fail_irq:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300751 cx25821_dev_unregister(dev);
752 return err;
753}
754
Hans Verkuil7087d312013-04-14 12:10:32 -0300755int cx25821_vidupstream_init(struct cx25821_channel *chan,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300756 int pixel_format)
757{
Hans Verkuil7087d312013-04-14 12:10:32 -0300758 struct cx25821_video_out_data *out = chan->out;
759 struct cx25821_dev *dev = chan->dev;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300760 const struct sram_channel *sram_ch;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300761 u32 tmp;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300762 int err = 0;
763 int data_frame_size = 0;
764 int risc_buffer_size = 0;
765 int str_length = 0;
766
Hans Verkuil7087d312013-04-14 12:10:32 -0300767 if (out->_is_running) {
Joe Perches36d89f72010-11-07 17:48:21 -0300768 pr_info("Video Channel is still running so return!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300769 return 0;
770 }
771
Hans Verkuil7087d312013-04-14 12:10:32 -0300772 sram_ch = chan->sram_channels;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300773
Hans Verkuil7087d312013-04-14 12:10:32 -0300774 INIT_WORK(&out->_irq_work_entry, cx25821_vidups_handler);
775 out->_irq_queues = create_singlethread_workqueue("cx25821_workqueue");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300776
Hans Verkuil7087d312013-04-14 12:10:32 -0300777 if (!out->_irq_queues) {
Joe Perches36d89f72010-11-07 17:48:21 -0300778 pr_err("create_singlethread_workqueue() for Video FAILED!\n");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300779 return -ENOMEM;
780 }
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300781 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
782 * channel A-C
783 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300784 tmp = cx_read(VID_CH_MODE_SEL);
785 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
786
Hans Verkuil7087d312013-04-14 12:10:32 -0300787 out->_is_running = 0;
788 out->_frame_count = 0;
789 out->_file_status = RESET_STATUS;
790 out->_lines_count = out->is_60hz ? 480 : 576;
791 out->_pixel_format = pixel_format;
792 out->_line_size = (out->_pixel_format == PIXEL_FRMT_422) ?
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300793 (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
Hans Verkuil7087d312013-04-14 12:10:32 -0300794 data_frame_size = out->is_60hz ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
795 risc_buffer_size = out->is_60hz ?
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300796 NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300797
Hans Verkuil7087d312013-04-14 12:10:32 -0300798 if (out->input_filename) {
799 str_length = strlen(out->input_filename);
800 out->_filename = kmemdup(out->input_filename, str_length + 1,
Thomas Meyer0ceaec12011-11-17 19:12:18 -0300801 GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300802
Hans Verkuil7087d312013-04-14 12:10:32 -0300803 if (!out->_filename) {
Peter Senna Tschudin216f3932012-09-06 11:23:56 -0300804 err = -ENOENT;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300805 goto error;
Peter Senna Tschudin216f3932012-09-06 11:23:56 -0300806 }
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300807 } else {
Hans Verkuil7087d312013-04-14 12:10:32 -0300808 str_length = strlen(out->_defaultname);
809 out->_filename = kmemdup(out->_defaultname, str_length + 1,
Thomas Meyer0ceaec12011-11-17 19:12:18 -0300810 GFP_KERNEL);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300811
Hans Verkuil7087d312013-04-14 12:10:32 -0300812 if (!out->_filename) {
Peter Senna Tschudin216f3932012-09-06 11:23:56 -0300813 err = -ENOENT;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300814 goto error;
Peter Senna Tschudin216f3932012-09-06 11:23:56 -0300815 }
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300816 }
817
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300818 /* Default if filename is empty string */
Hans Verkuil7087d312013-04-14 12:10:32 -0300819 if (strcmp(out->_filename, "") == 0) {
820 if (out->is_60hz) {
821 out->_filename =
822 (out->_pixel_format == PIXEL_FRMT_411) ?
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -0300823 "/root/vid411.yuv" : "/root/vidtest.yuv";
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300824 } else {
Hans Verkuil7087d312013-04-14 12:10:32 -0300825 out->_filename =
826 (out->_pixel_format == PIXEL_FRMT_411) ?
Leonid V. Fedorenchik2c68e932011-10-22 01:43:47 -0300827 "/root/pal411.yuv" : "/root/pal422.yuv";
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300828 }
829 }
830
Hans Verkuil7087d312013-04-14 12:10:32 -0300831 out->_is_running = 0;
832 out->_frame_count = 0;
833 out->_file_status = RESET_STATUS;
834 out->_lines_count = out->is_60hz ? 480 : 576;
835 out->_pixel_format = pixel_format;
836 out->_line_size = (out->_pixel_format == PIXEL_FRMT_422) ?
Leonid V. Fedorenchik8eb1fdf2011-10-22 01:43:48 -0300837 (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300838
Peter Senna Tschudin216f3932012-09-06 11:23:56 -0300839 err = cx25821_sram_channel_setup_upstream(dev, sram_ch,
Hans Verkuil7087d312013-04-14 12:10:32 -0300840 out->_line_size, 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300841
842 /* setup fifo + format */
Hans Verkuil7087d312013-04-14 12:10:32 -0300843 cx25821_set_pixelengine(chan, sram_ch, out->_pixel_format);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300844
Hans Verkuil7087d312013-04-14 12:10:32 -0300845 out->upstream_riscbuf_size = risc_buffer_size * 2;
846 out->upstream_databuf_size = data_frame_size * 2;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300847
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300848 /* Allocating buffers and prepare RISC program */
Hans Verkuil7087d312013-04-14 12:10:32 -0300849 err = cx25821_upstream_buffer_prepare(chan, sram_ch, out->_line_size);
Peter Senna Tschudin216f3932012-09-06 11:23:56 -0300850 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300851 pr_err("%s: Failed to set up Video upstream buffers!\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300852 dev->name);
853 goto error;
854 }
855
Hans Verkuil7087d312013-04-14 12:10:32 -0300856 cx25821_start_video_dma_upstream(chan, sram_ch);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300857
858 return 0;
859
Olimpiu Pascariu10991232010-04-06 02:09:00 -0300860error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300861 cx25821_dev_unregister(dev);
862
863 return err;
864}