[media] v4l2: add const to argument of write-only s_register ioctl
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / platform / davinci / vpif_capture.c
CommitLineData
6ffefff5
MK
1/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
28#include <linux/workqueue.h>
29#include <linux/string.h>
30#include <linux/videodev2.h>
31#include <linux/wait.h>
32#include <linux/time.h>
33#include <linux/i2c.h>
34#include <linux/platform_device.h>
35#include <linux/io.h>
5a0e3ad6 36#include <linux/slab.h>
6ffefff5
MK
37#include <media/v4l2-device.h>
38#include <media/v4l2-ioctl.h>
7036d6a7 39#include <media/v4l2-chip-ident.h>
6ffefff5
MK
40
41#include "vpif_capture.h"
42#include "vpif.h"
43
44MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45MODULE_LICENSE("GPL");
64dc3c1a 46MODULE_VERSION(VPIF_CAPTURE_VERSION);
6ffefff5
MK
47
48#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49#define vpif_dbg(level, debug, fmt, arg...) \
50 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51
52static int debug = 1;
53static u32 ch0_numbuffers = 3;
54static u32 ch1_numbuffers = 3;
55static u32 ch0_bufsize = 1920 * 1080 * 2;
56static u32 ch1_bufsize = 720 * 576 * 2;
57
58module_param(debug, int, 0644);
59module_param(ch0_numbuffers, uint, S_IRUGO);
60module_param(ch1_numbuffers, uint, S_IRUGO);
61module_param(ch0_bufsize, uint, S_IRUGO);
62module_param(ch1_bufsize, uint, S_IRUGO);
63
64MODULE_PARM_DESC(debug, "Debug level 0-1");
65MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
66MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
67MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
68MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
69
70static struct vpif_config_params config_params = {
71 .min_numbuffers = 3,
72 .numbuffers[0] = 3,
73 .numbuffers[1] = 3,
74 .min_bufsize[0] = 720 * 480 * 2,
75 .min_bufsize[1] = 720 * 480 * 2,
76 .channel_bufsize[0] = 1920 * 1080 * 2,
77 .channel_bufsize[1] = 720 * 576 * 2,
78};
79
80/* global variables */
81static struct vpif_device vpif_obj = { {NULL} };
82static struct device *vpif_dev;
60aa38d8
LP
83static void vpif_calculate_offsets(struct channel_obj *ch);
84static void vpif_config_addr(struct channel_obj *ch, int muxmode);
6ffefff5
MK
85
86/**
87 * buffer_prepare : callback function for buffer prepare
60aa38d8 88 * @vb: ptr to vb2_buffer
6ffefff5 89 *
60aa38d8 90 * This is the callback function for buffer prepare when vb2_qbuf()
6ffefff5
MK
91 * function is called. The buffer is prepared and user space virtual address
92 * or user address is converted into physical address
93 */
60aa38d8 94static int vpif_buffer_prepare(struct vb2_buffer *vb)
6ffefff5
MK
95{
96 /* Get the file handle object and channel object */
60aa38d8
LP
97 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
98 struct vb2_queue *q = vb->vb2_queue;
6ffefff5
MK
99 struct channel_obj *ch = fh->channel;
100 struct common_obj *common;
101 unsigned long addr;
102
6ffefff5
MK
103 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
104
105 common = &ch->common[VPIF_VIDEO_INDEX];
106
60aa38d8
LP
107 if (vb->state != VB2_BUF_STATE_ACTIVE &&
108 vb->state != VB2_BUF_STATE_PREPARED) {
109 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
110 if (vb2_plane_vaddr(vb, 0) &&
111 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
6ffefff5 112 goto exit;
60aa38d8 113 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
6ffefff5 114
60aa38d8
LP
115 if (q->streaming) {
116 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
117 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
118 !IS_ALIGNED((addr + common->ctop_off), 8) ||
119 !IS_ALIGNED((addr + common->cbtm_off), 8))
120 goto exit;
121 }
6ffefff5
MK
122 }
123 return 0;
124exit:
125 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
126 return -EINVAL;
127}
128
129/**
60aa38d8
LP
130 * vpif_buffer_queue_setup : Callback function for buffer setup.
131 * @vq: vb2_queue ptr
132 * @fmt: v4l2 format
133 * @nbuffers: ptr to number of buffers requested by application
134 * @nplanes:: contains number of distinct video planes needed to hold a frame
135 * @sizes[]: contains the size (in bytes) of each plane.
136 * @alloc_ctxs: ptr to allocation context
6ffefff5
MK
137 *
138 * This callback function is called when reqbuf() is called to adjust
139 * the buffer count and buffer size
140 */
60aa38d8
LP
141static int vpif_buffer_queue_setup(struct vb2_queue *vq,
142 const struct v4l2_format *fmt,
143 unsigned int *nbuffers, unsigned int *nplanes,
144 unsigned int sizes[], void *alloc_ctxs[])
6ffefff5
MK
145{
146 /* Get the file handle object and channel object */
60aa38d8 147 struct vpif_fh *fh = vb2_get_drv_priv(vq);
6ffefff5
MK
148 struct channel_obj *ch = fh->channel;
149 struct common_obj *common;
60aa38d8 150 unsigned long size;
6ffefff5
MK
151
152 common = &ch->common[VPIF_VIDEO_INDEX];
153
154 vpif_dbg(2, debug, "vpif_buffer_setup\n");
155
156 /* If memory type is not mmap, return */
60aa38d8
LP
157 if (V4L2_MEMORY_MMAP == common->memory) {
158 /* Calculate the size of the buffer */
159 size = config_params.channel_bufsize[ch->channel_id];
160 /*
161 * Checking if the buffer size exceeds the available buffer
162 * ycmux_mode = 0 means 1 channel mode HD and
163 * ycmux_mode = 1 means 2 channels mode SD
164 */
165 if (ch->vpifparams.std_info.ycmux_mode == 0) {
166 if (config_params.video_limit[ch->channel_id])
167 while (size * *nbuffers >
168 (config_params.video_limit[0]
764af39a 169 + config_params.video_limit[1]))
60aa38d8
LP
170 (*nbuffers)--;
171 } else {
172 if (config_params.video_limit[ch->channel_id])
173 while (size * *nbuffers >
764af39a 174 config_params.video_limit[ch->channel_id])
60aa38d8
LP
175 (*nbuffers)--;
176 }
177
178 } else {
179 size = common->fmt.fmt.pix.sizeimage;
764af39a
MH
180 }
181
60aa38d8
LP
182 if (*nbuffers < config_params.min_numbuffers)
183 *nbuffers = config_params.min_numbuffers;
184
185 *nplanes = 1;
186 sizes[0] = size;
187 alloc_ctxs[0] = common->alloc_ctx;
188
6ffefff5
MK
189 return 0;
190}
191
192/**
193 * vpif_buffer_queue : Callback function to add buffer to DMA queue
60aa38d8 194 * @vb: ptr to vb2_buffer
6ffefff5 195 */
60aa38d8 196static void vpif_buffer_queue(struct vb2_buffer *vb)
6ffefff5
MK
197{
198 /* Get the file handle object and channel object */
60aa38d8 199 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
6ffefff5 200 struct channel_obj *ch = fh->channel;
60aa38d8
LP
201 struct vpif_cap_buffer *buf = container_of(vb,
202 struct vpif_cap_buffer, vb);
6ffefff5 203 struct common_obj *common;
aec96832 204 unsigned long flags;
6ffefff5
MK
205
206 common = &ch->common[VPIF_VIDEO_INDEX];
207
208 vpif_dbg(2, debug, "vpif_buffer_queue\n");
209
aec96832 210 spin_lock_irqsave(&common->irqlock, flags);
6ffefff5 211 /* add the buffer to the DMA queue */
60aa38d8 212 list_add_tail(&buf->list, &common->dma_queue);
aec96832 213 spin_unlock_irqrestore(&common->irqlock, flags);
6ffefff5
MK
214}
215
216/**
60aa38d8
LP
217 * vpif_buf_cleanup : Callback function to free buffer
218 * @vb: ptr to vb2_buffer
6ffefff5 219 *
60aa38d8 220 * This function is called from the videobuf2 layer to free memory
6ffefff5
MK
221 * allocated to the buffers
222 */
60aa38d8 223static void vpif_buf_cleanup(struct vb2_buffer *vb)
6ffefff5
MK
224{
225 /* Get the file handle object and channel object */
60aa38d8
LP
226 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
227 struct vpif_cap_buffer *buf = container_of(vb,
228 struct vpif_cap_buffer, vb);
6ffefff5
MK
229 struct channel_obj *ch = fh->channel;
230 struct common_obj *common;
60aa38d8 231 unsigned long flags;
6ffefff5
MK
232
233 common = &ch->common[VPIF_VIDEO_INDEX];
234
60aa38d8
LP
235 spin_lock_irqsave(&common->irqlock, flags);
236 if (vb->state == VB2_BUF_STATE_ACTIVE)
237 list_del_init(&buf->list);
238 spin_unlock_irqrestore(&common->irqlock, flags);
239
6ffefff5
MK
240}
241
60aa38d8
LP
242static void vpif_wait_prepare(struct vb2_queue *vq)
243{
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
245 struct channel_obj *ch = fh->channel;
246 struct common_obj *common;
247
248 common = &ch->common[VPIF_VIDEO_INDEX];
249 mutex_unlock(&common->lock);
250}
251
252static void vpif_wait_finish(struct vb2_queue *vq)
253{
254 struct vpif_fh *fh = vb2_get_drv_priv(vq);
255 struct channel_obj *ch = fh->channel;
256 struct common_obj *common;
257
258 common = &ch->common[VPIF_VIDEO_INDEX];
259 mutex_lock(&common->lock);
260}
261
262static int vpif_buffer_init(struct vb2_buffer *vb)
263{
264 struct vpif_cap_buffer *buf = container_of(vb,
265 struct vpif_cap_buffer, vb);
266
267 INIT_LIST_HEAD(&buf->list);
268
269 return 0;
270}
6ffefff5
MK
271
272static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
273 { {1, 1} };
274
60aa38d8
LP
275static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
276{
277 struct vpif_capture_config *vpif_config_data =
278 vpif_dev->platform_data;
279 struct vpif_fh *fh = vb2_get_drv_priv(vq);
280 struct channel_obj *ch = fh->channel;
281 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
282 struct vpif_params *vpif = &ch->vpifparams;
283 unsigned long addr = 0;
aec96832 284 unsigned long flags;
60aa38d8
LP
285 int ret;
286
aec96832
HV
287 /* If buffer queue is empty, return error */
288 spin_lock_irqsave(&common->irqlock, flags);
60aa38d8 289 if (list_empty(&common->dma_queue)) {
aec96832 290 spin_unlock_irqrestore(&common->irqlock, flags);
60aa38d8
LP
291 vpif_dbg(1, debug, "buffer queue is empty\n");
292 return -EIO;
293 }
294
295 /* Get the next frame from the buffer queue */
296 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
297 struct vpif_cap_buffer, list);
298 /* Remove buffer from the buffer queue */
299 list_del(&common->cur_frm->list);
aec96832 300 spin_unlock_irqrestore(&common->irqlock, flags);
60aa38d8
LP
301 /* Mark state of the current frame to active */
302 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
303 /* Initialize field_id and started member */
304 ch->field_id = 0;
305 common->started = 1;
306 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
307
308 /* Calculate the offset for Y and C data in the buffer */
309 vpif_calculate_offsets(ch);
310
311 if ((vpif->std_info.frm_fmt &&
312 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
313 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
314 (!vpif->std_info.frm_fmt &&
315 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
316 vpif_dbg(1, debug, "conflict in field format and std format\n");
317 return -EINVAL;
318 }
319
320 /* configure 1 or 2 channel mode */
f4ad8d74
LP
321 if (vpif_config_data->setup_input_channel_mode) {
322 ret = vpif_config_data->
323 setup_input_channel_mode(vpif->std_info.ycmux_mode);
324 if (ret < 0) {
325 vpif_dbg(1, debug, "can't set vpif channel mode\n");
326 return ret;
327 }
60aa38d8
LP
328 }
329
330 /* Call vpif_set_params function to set the parameters and addresses */
331 ret = vpif_set_video_params(vpif, ch->channel_id);
332
333 if (ret < 0) {
334 vpif_dbg(1, debug, "can't set video params\n");
335 return ret;
336 }
337
338 common->started = ret;
339 vpif_config_addr(ch, ret);
340
341 common->set_addr(addr + common->ytop_off,
342 addr + common->ybtm_off,
343 addr + common->ctop_off,
344 addr + common->cbtm_off);
345
346 /**
347 * Set interrupt for both the fields in VPIF Register enable channel in
348 * VPIF register
349 */
9e18404a 350 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
60aa38d8
LP
351 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
352 channel0_intr_assert();
353 channel0_intr_enable(1);
354 enable_channel0(1);
355 }
356 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
357 (common->started == 2)) {
358 channel1_intr_assert();
359 channel1_intr_enable(1);
360 enable_channel1(1);
361 }
60aa38d8
LP
362
363 return 0;
364}
365
366/* abort streaming and wait for last buffer */
367static int vpif_stop_streaming(struct vb2_queue *vq)
368{
369 struct vpif_fh *fh = vb2_get_drv_priv(vq);
370 struct channel_obj *ch = fh->channel;
371 struct common_obj *common;
aec96832 372 unsigned long flags;
60aa38d8
LP
373
374 if (!vb2_is_streaming(vq))
375 return 0;
376
377 common = &ch->common[VPIF_VIDEO_INDEX];
378
379 /* release all active buffers */
aec96832 380 spin_lock_irqsave(&common->irqlock, flags);
60aa38d8
LP
381 while (!list_empty(&common->dma_queue)) {
382 common->next_frm = list_entry(common->dma_queue.next,
383 struct vpif_cap_buffer, list);
384 list_del(&common->next_frm->list);
385 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
386 }
aec96832 387 spin_unlock_irqrestore(&common->irqlock, flags);
60aa38d8
LP
388
389 return 0;
390}
391
392static struct vb2_ops video_qops = {
393 .queue_setup = vpif_buffer_queue_setup,
394 .wait_prepare = vpif_wait_prepare,
395 .wait_finish = vpif_wait_finish,
396 .buf_init = vpif_buffer_init,
397 .buf_prepare = vpif_buffer_prepare,
398 .start_streaming = vpif_start_streaming,
399 .stop_streaming = vpif_stop_streaming,
400 .buf_cleanup = vpif_buf_cleanup,
401 .buf_queue = vpif_buffer_queue,
402};
403
6ffefff5
MK
404/**
405 * vpif_process_buffer_complete: process a completed buffer
406 * @common: ptr to common channel object
407 *
408 * This function time stamp the buffer and mark it as DONE. It also
409 * wake up any process waiting on the QUEUE and set the next buffer
410 * as current
411 */
412static void vpif_process_buffer_complete(struct common_obj *common)
413{
8e6057b5 414 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
60aa38d8
LP
415 vb2_buffer_done(&common->cur_frm->vb,
416 VB2_BUF_STATE_DONE);
6ffefff5
MK
417 /* Make curFrm pointing to nextFrm */
418 common->cur_frm = common->next_frm;
419}
420
421/**
422 * vpif_schedule_next_buffer: set next buffer address for capture
423 * @common : ptr to common channel object
424 *
425 * This function will get next buffer from the dma queue and
426 * set the buffer address in the vpif register for capture.
427 * the buffer is marked active
428 */
429static void vpif_schedule_next_buffer(struct common_obj *common)
430{
431 unsigned long addr = 0;
432
aec96832 433 spin_lock(&common->irqlock);
6ffefff5 434 common->next_frm = list_entry(common->dma_queue.next,
60aa38d8 435 struct vpif_cap_buffer, list);
6ffefff5 436 /* Remove that buffer from the buffer queue */
60aa38d8 437 list_del(&common->next_frm->list);
aec96832 438 spin_unlock(&common->irqlock);
60aa38d8
LP
439 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
440 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
6ffefff5
MK
441
442 /* Set top and bottom field addresses in VPIF registers */
443 common->set_addr(addr + common->ytop_off,
444 addr + common->ybtm_off,
445 addr + common->ctop_off,
446 addr + common->cbtm_off);
447}
448
449/**
450 * vpif_channel_isr : ISR handler for vpif capture
451 * @irq: irq number
452 * @dev_id: dev_id ptr
453 *
454 * It changes status of the captured buffer, takes next buffer from the queue
2c0ddd17 455 * and sets its address in VPIF registers
6ffefff5
MK
456 */
457static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
458{
459 struct vpif_device *dev = &vpif_obj;
460 struct common_obj *common;
461 struct channel_obj *ch;
462 enum v4l2_field field;
463 int channel_id = 0;
464 int fid = -1, i;
465
466 channel_id = *(int *)(dev_id);
b1fc4230
MH
467 if (!vpif_intr_status(channel_id))
468 return IRQ_NONE;
469
6ffefff5
MK
470 ch = dev->dev[channel_id];
471
472 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
473
474 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
475 common = &ch->common[i];
476 /* skip If streaming is not started in this channel */
477 if (0 == common->started)
478 continue;
479
480 /* Check the field format */
481 if (1 == ch->vpifparams.std_info.frm_fmt) {
482 /* Progressive mode */
aec96832
HV
483 spin_lock(&common->irqlock);
484 if (list_empty(&common->dma_queue)) {
485 spin_unlock(&common->irqlock);
6ffefff5 486 continue;
aec96832
HV
487 }
488 spin_unlock(&common->irqlock);
6ffefff5
MK
489
490 if (!channel_first_int[i][channel_id])
491 vpif_process_buffer_complete(common);
492
493 channel_first_int[i][channel_id] = 0;
494
495 vpif_schedule_next_buffer(common);
496
497
498 channel_first_int[i][channel_id] = 0;
499 } else {
500 /**
501 * Interlaced mode. If it is first interrupt, ignore
502 * it
503 */
504 if (channel_first_int[i][channel_id]) {
505 channel_first_int[i][channel_id] = 0;
506 continue;
507 }
508 if (0 == i) {
509 ch->field_id ^= 1;
510 /* Get field id from VPIF registers */
511 fid = vpif_channel_getfid(ch->channel_id);
512 if (fid != ch->field_id) {
513 /**
514 * If field id does not match stored
515 * field id, make them in sync
516 */
517 if (0 == fid)
518 ch->field_id = fid;
519 return IRQ_HANDLED;
520 }
521 }
522 /* device field id and local field id are in sync */
523 if (0 == fid) {
524 /* this is even field */
525 if (common->cur_frm == common->next_frm)
526 continue;
527
528 /* mark the current buffer as done */
529 vpif_process_buffer_complete(common);
530 } else if (1 == fid) {
531 /* odd field */
aec96832 532 spin_lock(&common->irqlock);
6ffefff5 533 if (list_empty(&common->dma_queue) ||
aec96832
HV
534 (common->cur_frm != common->next_frm)) {
535 spin_unlock(&common->irqlock);
6ffefff5 536 continue;
aec96832
HV
537 }
538 spin_unlock(&common->irqlock);
6ffefff5
MK
539
540 vpif_schedule_next_buffer(common);
541 }
542 }
543 }
544 return IRQ_HANDLED;
545}
546
547/**
548 * vpif_update_std_info() - update standard related info
549 * @ch: ptr to channel object
550 *
551 * For a given standard selected by application, update values
552 * in the device data structures
553 */
554static int vpif_update_std_info(struct channel_obj *ch)
555{
556 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
557 struct vpif_params *vpifparams = &ch->vpifparams;
558 const struct vpif_channel_config_params *config;
2c0ddd17 559 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
6ffefff5
MK
560 struct video_obj *vid_ch = &ch->video;
561 int index;
562
563 vpif_dbg(2, debug, "vpif_update_std_info\n");
564
aa444406 565 for (index = 0; index < vpif_ch_params_count; index++) {
ced9b21f 566 config = &vpif_ch_params[index];
40c8bcea
MR
567 if (config->hd_sd == 0) {
568 vpif_dbg(2, debug, "SD format\n");
569 if (config->stdid & vid_ch->stdid) {
570 memcpy(std_info, config, sizeof(*config));
571 break;
572 }
573 } else {
574 vpif_dbg(2, debug, "HD format\n");
0598c17b
HV
575 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
576 sizeof(vid_ch->dv_timings))) {
40c8bcea
MR
577 memcpy(std_info, config, sizeof(*config));
578 break;
579 }
6ffefff5
MK
580 }
581 }
582
583 /* standard not found */
aa444406 584 if (index == vpif_ch_params_count)
6ffefff5
MK
585 return -EINVAL;
586
587 common->fmt.fmt.pix.width = std_info->width;
588 common->width = std_info->width;
589 common->fmt.fmt.pix.height = std_info->height;
590 common->height = std_info->height;
591 common->fmt.fmt.pix.bytesperline = std_info->width;
592 vpifparams->video_params.hpitch = std_info->width;
593 vpifparams->video_params.storage_mode = std_info->frm_fmt;
2c0ddd17 594
6ffefff5
MK
595 return 0;
596}
597
598/**
599 * vpif_calculate_offsets : This function calculates buffers offsets
600 * @ch : ptr to channel object
601 *
602 * This function calculates buffer offsets for Y and C in the top and
603 * bottom field
604 */
605static void vpif_calculate_offsets(struct channel_obj *ch)
606{
607 unsigned int hpitch, vpitch, sizeimage;
608 struct video_obj *vid_ch = &(ch->video);
609 struct vpif_params *vpifparams = &ch->vpifparams;
610 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
611 enum v4l2_field field = common->fmt.fmt.pix.field;
612
613 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
614
615 if (V4L2_FIELD_ANY == field) {
616 if (vpifparams->std_info.frm_fmt)
617 vid_ch->buf_field = V4L2_FIELD_NONE;
618 else
619 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
620 } else
621 vid_ch->buf_field = common->fmt.fmt.pix.field;
622
60aa38d8 623 sizeimage = common->fmt.fmt.pix.sizeimage;
6ffefff5
MK
624
625 hpitch = common->fmt.fmt.pix.bytesperline;
626 vpitch = sizeimage / (hpitch * 2);
627
628 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
629 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
630 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
631 common->ytop_off = 0;
632 common->ybtm_off = hpitch;
633 common->ctop_off = sizeimage / 2;
634 common->cbtm_off = sizeimage / 2 + hpitch;
635 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
636 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
637 common->ytop_off = 0;
638 common->ybtm_off = sizeimage / 4;
639 common->ctop_off = sizeimage / 2;
640 common->cbtm_off = common->ctop_off + sizeimage / 4;
641 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
642 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
643 common->ybtm_off = 0;
644 common->ytop_off = sizeimage / 4;
645 common->cbtm_off = sizeimage / 2;
646 common->ctop_off = common->cbtm_off + sizeimage / 4;
647 }
648 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
649 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
650 vpifparams->video_params.storage_mode = 1;
651 else
652 vpifparams->video_params.storage_mode = 0;
653
654 if (1 == vpifparams->std_info.frm_fmt)
655 vpifparams->video_params.hpitch =
656 common->fmt.fmt.pix.bytesperline;
657 else {
658 if ((field == V4L2_FIELD_ANY)
659 || (field == V4L2_FIELD_INTERLACED))
660 vpifparams->video_params.hpitch =
661 common->fmt.fmt.pix.bytesperline * 2;
662 else
663 vpifparams->video_params.hpitch =
664 common->fmt.fmt.pix.bytesperline;
665 }
666
667 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
668}
669
670/**
671 * vpif_config_format: configure default frame format in the device
672 * ch : ptr to channel object
673 */
674static void vpif_config_format(struct channel_obj *ch)
675{
676 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
677
678 vpif_dbg(2, debug, "vpif_config_format\n");
679
680 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
681 if (config_params.numbuffers[ch->channel_id] == 0)
682 common->memory = V4L2_MEMORY_USERPTR;
683 else
684 common->memory = V4L2_MEMORY_MMAP;
685
686 common->fmt.fmt.pix.sizeimage
687 = config_params.channel_bufsize[ch->channel_id];
688
689 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
690 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
691 else
692 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
693 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
694}
695
696/**
697 * vpif_get_default_field() - Get default field type based on interface
698 * @vpif_params - ptr to vpif params
699 */
700static inline enum v4l2_field vpif_get_default_field(
701 struct vpif_interface *iface)
702{
703 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
704 V4L2_FIELD_INTERLACED;
705}
706
707/**
708 * vpif_check_format() - check given pixel format for compatibility
709 * @ch - channel ptr
710 * @pixfmt - Given pixel format
711 * @update - update the values as per hardware requirement
712 *
713 * Check the application pixel format for S_FMT and update the input
714 * values as per hardware limits for TRY_FMT. The default pixel and
715 * field format is selected based on interface type.
716 */
717static int vpif_check_format(struct channel_obj *ch,
718 struct v4l2_pix_format *pixfmt,
719 int update)
720{
721 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
722 struct vpif_params *vpif_params = &ch->vpifparams;
723 enum v4l2_field field = pixfmt->field;
724 u32 sizeimage, hpitch, vpitch;
725 int ret = -EINVAL;
726
727 vpif_dbg(2, debug, "vpif_check_format\n");
728 /**
729 * first check for the pixel format. If if_type is Raw bayer,
730 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
731 * V4L2_PIX_FMT_YUV422P is supported
732 */
733 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
734 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
735 if (!update) {
736 vpif_dbg(2, debug, "invalid pix format\n");
737 goto exit;
738 }
739 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
740 }
741 } else {
742 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
743 if (!update) {
744 vpif_dbg(2, debug, "invalid pixel format\n");
745 goto exit;
746 }
747 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
748 }
749 }
750
751 if (!(VPIF_VALID_FIELD(field))) {
752 if (!update) {
753 vpif_dbg(2, debug, "invalid field format\n");
754 goto exit;
755 }
756 /**
757 * By default use FIELD_NONE for RAW Bayer capture
758 * and FIELD_INTERLACED for other interfaces
759 */
760 field = vpif_get_default_field(&vpif_params->iface);
761 } else if (field == V4L2_FIELD_ANY)
762 /* unsupported field. Use default */
763 field = vpif_get_default_field(&vpif_params->iface);
764
765 /* validate the hpitch */
766 hpitch = pixfmt->bytesperline;
767 if (hpitch < vpif_params->std_info.width) {
768 if (!update) {
769 vpif_dbg(2, debug, "invalid hpitch\n");
770 goto exit;
771 }
772 hpitch = vpif_params->std_info.width;
773 }
774
60aa38d8 775 sizeimage = pixfmt->sizeimage;
6ffefff5
MK
776
777 vpitch = sizeimage / (hpitch * 2);
778
779 /* validate the vpitch */
780 if (vpitch < vpif_params->std_info.height) {
781 if (!update) {
782 vpif_dbg(2, debug, "Invalid vpitch\n");
783 goto exit;
784 }
785 vpitch = vpif_params->std_info.height;
786 }
787
788 /* Check for 8 byte alignment */
789 if (!ALIGN(hpitch, 8)) {
790 if (!update) {
791 vpif_dbg(2, debug, "invalid pitch alignment\n");
792 goto exit;
793 }
794 /* adjust to next 8 byte boundary */
795 hpitch = (((hpitch + 7) / 8) * 8);
796 }
797 /* if update is set, modify the bytesperline and sizeimage */
798 if (update) {
799 pixfmt->bytesperline = hpitch;
800 pixfmt->sizeimage = hpitch * vpitch * 2;
801 }
802 /**
803 * Image width and height is always based on current standard width and
804 * height
805 */
806 pixfmt->width = common->fmt.fmt.pix.width;
807 pixfmt->height = common->fmt.fmt.pix.height;
808 return 0;
809exit:
810 return ret;
811}
812
813/**
814 * vpif_config_addr() - function to configure buffer address in vpif
815 * @ch - channel ptr
816 * @muxmode - channel mux mode
817 */
818static void vpif_config_addr(struct channel_obj *ch, int muxmode)
819{
820 struct common_obj *common;
821
822 vpif_dbg(2, debug, "vpif_config_addr\n");
823
824 common = &(ch->common[VPIF_VIDEO_INDEX]);
825
826 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
827 common->set_addr = ch1_set_videobuf_addr;
828 else if (2 == muxmode)
829 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
830 else
831 common->set_addr = ch0_set_videobuf_addr;
832}
833
834/**
6f45b1b9 835 * vpif_mmap : It is used to map kernel space buffers into user spaces
6ffefff5
MK
836 * @filep: file pointer
837 * @vma: ptr to vm_area_struct
838 */
839static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
840{
841 /* Get the channel object and file handle object */
842 struct vpif_fh *fh = filep->private_data;
843 struct channel_obj *ch = fh->channel;
844 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
7224679a 845 int ret;
6ffefff5
MK
846
847 vpif_dbg(2, debug, "vpif_mmap\n");
848
7224679a
HV
849 if (mutex_lock_interruptible(&common->lock))
850 return -ERESTARTSYS;
851 ret = vb2_mmap(&common->buffer_queue, vma);
852 mutex_unlock(&common->lock);
853 return ret;
6ffefff5
MK
854}
855
856/**
857 * vpif_poll: It is used for select/poll system call
858 * @filep: file pointer
859 * @wait: poll table to wait
860 */
861static unsigned int vpif_poll(struct file *filep, poll_table * wait)
862{
6ffefff5
MK
863 struct vpif_fh *fh = filep->private_data;
864 struct channel_obj *channel = fh->channel;
865 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
7224679a 866 unsigned int res = 0;
6ffefff5
MK
867
868 vpif_dbg(2, debug, "vpif_poll\n");
869
7224679a
HV
870 if (common->started) {
871 mutex_lock(&common->lock);
872 res = vb2_poll(&common->buffer_queue, filep, wait);
873 mutex_unlock(&common->lock);
874 }
875 return res;
6ffefff5
MK
876}
877
878/**
879 * vpif_open : vpif open handler
880 * @filep: file ptr
881 *
882 * It creates object of file handle structure and stores it in private_data
883 * member of filepointer
884 */
885static int vpif_open(struct file *filep)
886{
6ffefff5
MK
887 struct video_device *vdev = video_devdata(filep);
888 struct common_obj *common;
889 struct video_obj *vid_ch;
890 struct channel_obj *ch;
891 struct vpif_fh *fh;
6ffefff5
MK
892
893 vpif_dbg(2, debug, "vpif_open\n");
894
895 ch = video_get_drvdata(vdev);
896
897 vid_ch = &ch->video;
898 common = &ch->common[VPIF_VIDEO_INDEX];
899
6ffefff5 900 /* Allocate memory for the file handle object */
1f8766b4 901 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
6ffefff5
MK
902 if (NULL == fh) {
903 vpif_err("unable to allocate memory for file handle object\n");
46656afa 904 return -ENOMEM;
6ffefff5
MK
905 }
906
7224679a
HV
907 if (mutex_lock_interruptible(&common->lock)) {
908 kfree(fh);
909 return -ERESTARTSYS;
910 }
6ffefff5
MK
911 /* store pointer to fh in private_data member of filep */
912 filep->private_data = fh;
913 fh->channel = ch;
914 fh->initialized = 0;
915 /* If decoder is not initialized. initialize it */
916 if (!ch->initialized) {
917 fh->initialized = 1;
918 ch->initialized = 1;
919 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
920 }
921 /* Increment channel usrs counter */
922 ch->usrs++;
923 /* Set io_allowed member to false */
924 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
925 /* Initialize priority of this instance to default priority */
926 fh->prio = V4L2_PRIORITY_UNSET;
927 v4l2_prio_open(&ch->prio, &fh->prio);
7224679a 928 mutex_unlock(&common->lock);
46656afa 929 return 0;
6ffefff5
MK
930}
931
932/**
933 * vpif_release : function to clean up file close
934 * @filep: file pointer
935 *
6f45b1b9 936 * This function deletes buffer queue, frees the buffers and the vpif file
6ffefff5
MK
937 * handle
938 */
939static int vpif_release(struct file *filep)
940{
941 struct vpif_fh *fh = filep->private_data;
942 struct channel_obj *ch = fh->channel;
943 struct common_obj *common;
944
945 vpif_dbg(2, debug, "vpif_release\n");
946
947 common = &ch->common[VPIF_VIDEO_INDEX];
948
7224679a 949 mutex_lock(&common->lock);
6ffefff5
MK
950 /* if this instance is doing IO */
951 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
952 /* Reset io_usrs member of channel object */
953 common->io_usrs = 0;
954 /* Disable channel as per its device type and channel id */
955 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
956 enable_channel0(0);
957 channel0_intr_enable(0);
958 }
959 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
960 (2 == common->started)) {
961 enable_channel1(0);
962 channel1_intr_enable(0);
963 }
964 common->started = 0;
965 /* Free buffers allocated */
60aa38d8
LP
966 vb2_queue_release(&common->buffer_queue);
967 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
6ffefff5
MK
968 }
969
970 /* Decrement channel usrs counter */
971 ch->usrs--;
972
6ffefff5 973 /* Close the priority */
ffb4877b 974 v4l2_prio_close(&ch->prio, fh->prio);
6ffefff5
MK
975
976 if (fh->initialized)
977 ch->initialized = 0;
978
7224679a 979 mutex_unlock(&common->lock);
6ffefff5
MK
980 filep->private_data = NULL;
981 kfree(fh);
982 return 0;
983}
984
985/**
986 * vpif_reqbufs() - request buffer handler
987 * @file: file ptr
988 * @priv: file handle
989 * @reqbuf: request buffer structure ptr
990 */
991static int vpif_reqbufs(struct file *file, void *priv,
992 struct v4l2_requestbuffers *reqbuf)
993{
994 struct vpif_fh *fh = priv;
995 struct channel_obj *ch = fh->channel;
996 struct common_obj *common;
997 u8 index = 0;
60aa38d8 998 struct vb2_queue *q;
b4a711e7 999 int ret;
6ffefff5
MK
1000
1001 vpif_dbg(2, debug, "vpif_reqbufs\n");
1002
1003 /**
1004 * This file handle has not initialized the channel,
1005 * It is not allowed to do settings
1006 */
1007 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
1008 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1009 if (!fh->initialized) {
1010 vpif_dbg(1, debug, "Channel Busy\n");
1011 return -EBUSY;
1012 }
1013 }
1014
764af39a 1015 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
6ffefff5
MK
1016 return -EINVAL;
1017
1018 index = VPIF_VIDEO_INDEX;
1019
1020 common = &ch->common[index];
1021
46656afa
HV
1022 if (0 != common->io_usrs)
1023 return -EBUSY;
6ffefff5 1024
60aa38d8
LP
1025 /* Initialize videobuf2 queue as per the buffer type */
1026 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
94b76a88 1027 if (IS_ERR(common->alloc_ctx)) {
60aa38d8 1028 vpif_err("Failed to get the context\n");
94b76a88 1029 return PTR_ERR(common->alloc_ctx);
60aa38d8
LP
1030 }
1031 q = &common->buffer_queue;
1032 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1033 q->io_modes = VB2_MMAP | VB2_USERPTR;
1034 q->drv_priv = fh;
1035 q->ops = &video_qops;
1036 q->mem_ops = &vb2_dma_contig_memops;
1037 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
6aa69f99 1038 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
60aa38d8 1039
b4a711e7
LP
1040 ret = vb2_queue_init(q);
1041 if (ret) {
1042 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1043 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1044 return ret;
1045 }
6ffefff5
MK
1046 /* Set io allowed member of file handle to TRUE */
1047 fh->io_allowed[index] = 1;
1048 /* Increment io usrs member of channel object to 1 */
1049 common->io_usrs = 1;
1050 /* Store type of memory requested in channel object */
1051 common->memory = reqbuf->memory;
1052 INIT_LIST_HEAD(&common->dma_queue);
1053
1054 /* Allocate buffers */
60aa38d8 1055 return vb2_reqbufs(&common->buffer_queue, reqbuf);
6ffefff5
MK
1056}
1057
1058/**
1059 * vpif_querybuf() - query buffer handler
1060 * @file: file ptr
1061 * @priv: file handle
1062 * @buf: v4l2 buffer structure ptr
1063 */
1064static int vpif_querybuf(struct file *file, void *priv,
1065 struct v4l2_buffer *buf)
1066{
1067 struct vpif_fh *fh = priv;
1068 struct channel_obj *ch = fh->channel;
1069 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1070
1071 vpif_dbg(2, debug, "vpif_querybuf\n");
1072
1073 if (common->fmt.type != buf->type)
1074 return -EINVAL;
1075
1076 if (common->memory != V4L2_MEMORY_MMAP) {
1077 vpif_dbg(1, debug, "Invalid memory\n");
1078 return -EINVAL;
1079 }
1080
60aa38d8 1081 return vb2_querybuf(&common->buffer_queue, buf);
6ffefff5
MK
1082}
1083
1084/**
1085 * vpif_qbuf() - query buffer handler
1086 * @file: file ptr
1087 * @priv: file handle
1088 * @buf: v4l2 buffer structure ptr
1089 */
1090static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1091{
1092
1093 struct vpif_fh *fh = priv;
1094 struct channel_obj *ch = fh->channel;
1095 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1096 struct v4l2_buffer tbuf = *buf;
6ffefff5
MK
1097
1098 vpif_dbg(2, debug, "vpif_qbuf\n");
1099
1100 if (common->fmt.type != tbuf.type) {
1101 vpif_err("invalid buffer type\n");
1102 return -EINVAL;
1103 }
1104
1105 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
60aa38d8 1106 vpif_err("fh io not allowed\n");
6ffefff5
MK
1107 return -EACCES;
1108 }
1109
60aa38d8 1110 return vb2_qbuf(&common->buffer_queue, buf);
6ffefff5
MK
1111}
1112
1113/**
1114 * vpif_dqbuf() - query buffer handler
1115 * @file: file ptr
1116 * @priv: file handle
1117 * @buf: v4l2 buffer structure ptr
1118 */
1119static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1120{
1121 struct vpif_fh *fh = priv;
1122 struct channel_obj *ch = fh->channel;
1123 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1124
1125 vpif_dbg(2, debug, "vpif_dqbuf\n");
1126
60aa38d8
LP
1127 return vb2_dqbuf(&common->buffer_queue, buf,
1128 (file->f_flags & O_NONBLOCK));
6ffefff5
MK
1129}
1130
1131/**
1132 * vpif_streamon() - streamon handler
1133 * @file: file ptr
1134 * @priv: file handle
1135 * @buftype: v4l2 buffer type
1136 */
1137static int vpif_streamon(struct file *file, void *priv,
1138 enum v4l2_buf_type buftype)
1139{
1140
6ffefff5
MK
1141 struct vpif_fh *fh = priv;
1142 struct channel_obj *ch = fh->channel;
1143 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1144 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1145 struct vpif_params *vpif;
6ffefff5
MK
1146 int ret = 0;
1147
1148 vpif_dbg(2, debug, "vpif_streamon\n");
1149
1150 vpif = &ch->vpifparams;
1151
1152 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1153 vpif_dbg(1, debug, "buffer type not supported\n");
1154 return -EINVAL;
1155 }
1156
1157 /* If file handle is not allowed IO, return error */
1158 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1159 vpif_dbg(1, debug, "io not allowed\n");
1160 return -EACCES;
1161 }
1162
1163 /* If Streaming is already started, return error */
1164 if (common->started) {
1165 vpif_dbg(1, debug, "channel->started\n");
1166 return -EBUSY;
1167 }
1168
1169 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1170 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1171 vpif->std_info.ycmux_mode == 0) ||
1172 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1173 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1174 vpif_dbg(1, debug, "other channel is being used\n");
1175 return -EBUSY;
1176 }
1177
1178 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1179 if (ret)
1180 return ret;
1181
1182 /* Enable streamon on the sub device */
178cce12 1183 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
6ffefff5 1184
178cce12 1185 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
6ffefff5
MK
1186 vpif_dbg(1, debug, "stream on failed in subdev\n");
1187 return ret;
1188 }
1189
60aa38d8
LP
1190 /* Call vb2_streamon to start streaming in videobuf2 */
1191 ret = vb2_streamon(&common->buffer_queue, buftype);
6ffefff5 1192 if (ret) {
60aa38d8 1193 vpif_dbg(1, debug, "vb2_streamon\n");
6ffefff5
MK
1194 return ret;
1195 }
1196
6ffefff5
MK
1197 return ret;
1198}
1199
1200/**
1201 * vpif_streamoff() - streamoff handler
1202 * @file: file ptr
1203 * @priv: file handle
1204 * @buftype: v4l2 buffer type
1205 */
1206static int vpif_streamoff(struct file *file, void *priv,
1207 enum v4l2_buf_type buftype)
1208{
1209
1210 struct vpif_fh *fh = priv;
1211 struct channel_obj *ch = fh->channel;
1212 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1213 int ret;
1214
1215 vpif_dbg(2, debug, "vpif_streamoff\n");
1216
1217 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1218 vpif_dbg(1, debug, "buffer type not supported\n");
1219 return -EINVAL;
1220 }
1221
1222 /* If io is allowed for this file handle, return error */
1223 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1224 vpif_dbg(1, debug, "io not allowed\n");
1225 return -EACCES;
1226 }
1227
1228 /* If streaming is not started, return error */
1229 if (!common->started) {
1230 vpif_dbg(1, debug, "channel->started\n");
1231 return -EINVAL;
1232 }
1233
6ffefff5
MK
1234 /* disable channel */
1235 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1236 enable_channel0(0);
1237 channel0_intr_enable(0);
1238 } else {
1239 enable_channel1(0);
1240 channel1_intr_enable(0);
1241 }
1242
1243 common->started = 0;
1244
178cce12 1245 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
6ffefff5 1246
178cce12 1247 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
6ffefff5
MK
1248 vpif_dbg(1, debug, "stream off failed in subdev\n");
1249
60aa38d8 1250 return vb2_streamoff(&common->buffer_queue, buftype);
6ffefff5
MK
1251}
1252
1253/**
178cce12
HV
1254 * vpif_input_to_subdev() - Maps input to sub device
1255 * @vpif_cfg - global config ptr
1256 * @chan_cfg - channel config ptr
6ffefff5 1257 * @input_index - Given input index from application
6ffefff5
MK
1258 *
1259 * lookup the sub device information for a given input index.
1260 * we report all the inputs to application. inputs table also
1261 * has sub device name for the each input
1262 */
178cce12
HV
1263static int vpif_input_to_subdev(
1264 struct vpif_capture_config *vpif_cfg,
1265 struct vpif_capture_chan_config *chan_cfg,
1266 int input_index)
6ffefff5 1267{
178cce12
HV
1268 struct vpif_subdev_info *subdev_info;
1269 const char *subdev_name;
6ffefff5
MK
1270 int i;
1271
178cce12 1272 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
6ffefff5 1273
178cce12
HV
1274 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1275 if (subdev_name == NULL)
1276 return -1;
6ffefff5
MK
1277
1278 /* loop through the sub device list to get the sub device info */
1279 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1280 subdev_info = &vpif_cfg->subdev_info[i];
1281 if (!strcmp(subdev_info->name, subdev_name))
178cce12 1282 return i;
6ffefff5 1283 }
178cce12
HV
1284 return -1;
1285}
6ffefff5 1286
178cce12
HV
1287/**
1288 * vpif_set_input() - Select an input
1289 * @vpif_cfg - global config ptr
1290 * @ch - channel
1291 * @_index - Given input index from application
1292 *
1293 * Select the given input.
1294 */
1295static int vpif_set_input(
1296 struct vpif_capture_config *vpif_cfg,
1297 struct channel_obj *ch,
1298 int index)
1299{
1300 struct vpif_capture_chan_config *chan_cfg =
1301 &vpif_cfg->chan_config[ch->channel_id];
1302 struct vpif_subdev_info *subdev_info = NULL;
1303 struct v4l2_subdev *sd = NULL;
1304 u32 input = 0, output = 0;
1305 int sd_index;
1306 int ret;
6ffefff5 1307
178cce12
HV
1308 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1309 if (sd_index >= 0) {
1310 sd = vpif_obj.sd[sd_index];
1311 subdev_info = &vpif_cfg->subdev_info[sd_index];
1312 }
6ffefff5 1313
178cce12
HV
1314 /* first setup input path from sub device to vpif */
1315 if (sd && vpif_cfg->setup_input_path) {
1316 ret = vpif_cfg->setup_input_path(ch->channel_id,
1317 subdev_info->name);
1318 if (ret < 0) {
1319 vpif_dbg(1, debug, "couldn't setup input path for the" \
1320 " sub device %s, for input index %d\n",
1321 subdev_info->name, index);
1322 return ret;
1323 }
1324 }
1325
1326 if (sd) {
1327 input = chan_cfg->inputs[index].input_route;
1328 output = chan_cfg->inputs[index].output_route;
1329 ret = v4l2_subdev_call(sd, video, s_routing,
1330 input, output, 0);
1331 if (ret < 0 && ret != -ENOIOCTLCMD) {
1332 vpif_dbg(1, debug, "Failed to set input\n");
1333 return ret;
1334 }
1335 }
1336 ch->input_idx = index;
1337 ch->sd = sd;
1338 /* copy interface parameters to vpif */
0d4f35f3 1339 ch->vpifparams.iface = chan_cfg->vpif_if;
178cce12
HV
1340
1341 /* update tvnorms from the sub device input info */
1342 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1343 return 0;
6ffefff5
MK
1344}
1345
1346/**
1347 * vpif_querystd() - querystd handler
1348 * @file: file ptr
1349 * @priv: file handle
1350 * @std_id: ptr to std id
1351 *
1352 * This function is called to detect standard at the selected input
1353 */
1354static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1355{
1356 struct vpif_fh *fh = priv;
1357 struct channel_obj *ch = fh->channel;
6ffefff5
MK
1358 int ret = 0;
1359
1360 vpif_dbg(2, debug, "vpif_querystd\n");
1361
6ffefff5 1362 /* Call querystd function of decoder device */
178cce12 1363 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
6ffefff5 1364
178cce12
HV
1365 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1366 return -ENODATA;
1367 if (ret) {
1368 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1369 return ret;
1370 }
1371
1372 return 0;
6ffefff5
MK
1373}
1374
1375/**
1376 * vpif_g_std() - get STD handler
1377 * @file: file ptr
1378 * @priv: file handle
1379 * @std_id: ptr to std id
1380 */
1381static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1382{
1383 struct vpif_fh *fh = priv;
1384 struct channel_obj *ch = fh->channel;
1385
1386 vpif_dbg(2, debug, "vpif_g_std\n");
1387
1388 *std = ch->video.stdid;
1389 return 0;
1390}
1391
1392/**
1393 * vpif_s_std() - set STD handler
1394 * @file: file ptr
1395 * @priv: file handle
1396 * @std_id: ptr to std id
1397 */
314527ac 1398static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
6ffefff5
MK
1399{
1400 struct vpif_fh *fh = priv;
1401 struct channel_obj *ch = fh->channel;
1402 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1403 int ret = 0;
1404
1405 vpif_dbg(2, debug, "vpif_s_std\n");
1406
1407 if (common->started) {
1408 vpif_err("streaming in progress\n");
1409 return -EBUSY;
1410 }
1411
1412 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1413 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1414 if (!fh->initialized) {
1415 vpif_dbg(1, debug, "Channel Busy\n");
1416 return -EBUSY;
1417 }
1418 }
1419
ffb4877b 1420 ret = v4l2_prio_check(&ch->prio, fh->prio);
6ffefff5
MK
1421 if (0 != ret)
1422 return ret;
1423
1424 fh->initialized = 1;
1425
1426 /* Call encoder subdevice function to set the standard */
314527ac 1427 ch->video.stdid = std_id;
0598c17b 1428 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
6ffefff5
MK
1429
1430 /* Get the information about the standard */
1431 if (vpif_update_std_info(ch)) {
6ffefff5 1432 vpif_err("Error getting the standard info\n");
46656afa 1433 return -EINVAL;
6ffefff5
MK
1434 }
1435
1436 /* Configure the default format information */
1437 vpif_config_format(ch);
1438
1439 /* set standard in the sub device */
314527ac 1440 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
178cce12 1441 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
6ffefff5 1442 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
178cce12
HV
1443 return ret;
1444 }
1445 return 0;
6ffefff5
MK
1446}
1447
1448/**
1449 * vpif_enum_input() - ENUMINPUT handler
1450 * @file: file ptr
1451 * @priv: file handle
1452 * @input: ptr to input structure
1453 */
1454static int vpif_enum_input(struct file *file, void *priv,
1455 struct v4l2_input *input)
1456{
1457
1458 struct vpif_capture_config *config = vpif_dev->platform_data;
1459 struct vpif_capture_chan_config *chan_cfg;
1460 struct vpif_fh *fh = priv;
1461 struct channel_obj *ch = fh->channel;
1462
1463 chan_cfg = &config->chan_config[ch->channel_id];
1464
1465 if (input->index >= chan_cfg->input_count) {
1466 vpif_dbg(1, debug, "Invalid input index\n");
1467 return -EINVAL;
1468 }
1469
1470 memcpy(input, &chan_cfg->inputs[input->index].input,
1471 sizeof(*input));
1472 return 0;
1473}
1474
1475/**
1476 * vpif_g_input() - Get INPUT handler
1477 * @file: file ptr
1478 * @priv: file handle
1479 * @index: ptr to input index
1480 */
1481static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1482{
1483 struct vpif_fh *fh = priv;
1484 struct channel_obj *ch = fh->channel;
6ffefff5 1485
6f47c6c6 1486 *index = ch->input_idx;
6ffefff5
MK
1487 return 0;
1488}
1489
1490/**
1491 * vpif_s_input() - Set INPUT handler
1492 * @file: file ptr
1493 * @priv: file handle
1494 * @index: input index
1495 */
1496static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1497{
1498 struct vpif_capture_config *config = vpif_dev->platform_data;
1499 struct vpif_capture_chan_config *chan_cfg;
1500 struct vpif_fh *fh = priv;
1501 struct channel_obj *ch = fh->channel;
1502 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
178cce12 1503 int ret;
6ffefff5
MK
1504
1505 chan_cfg = &config->chan_config[ch->channel_id];
1506
7aaad131
HV
1507 if (index >= chan_cfg->input_count)
1508 return -EINVAL;
1509
6ffefff5
MK
1510 if (common->started) {
1511 vpif_err("Streaming in progress\n");
1512 return -EBUSY;
1513 }
1514
1515 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1516 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1517 if (!fh->initialized) {
1518 vpif_dbg(1, debug, "Channel Busy\n");
1519 return -EBUSY;
1520 }
1521 }
1522
ffb4877b 1523 ret = v4l2_prio_check(&ch->prio, fh->prio);
6ffefff5
MK
1524 if (0 != ret)
1525 return ret;
1526
1527 fh->initialized = 1;
178cce12 1528 return vpif_set_input(config, ch, index);
6ffefff5
MK
1529}
1530
1531/**
1532 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1533 * @file: file ptr
1534 * @priv: file handle
1535 * @index: input index
1536 */
1537static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1538 struct v4l2_fmtdesc *fmt)
1539{
1540 struct vpif_fh *fh = priv;
1541 struct channel_obj *ch = fh->channel;
1542
1543 if (fmt->index != 0) {
1544 vpif_dbg(1, debug, "Invalid format index\n");
1545 return -EINVAL;
1546 }
1547
1548 /* Fill in the information about format */
1549 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1550 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1551 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1552 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1553 } else {
1554 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1555 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1556 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1557 }
1558 return 0;
1559}
1560
1561/**
1562 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1563 * @file: file ptr
1564 * @priv: file handle
1565 * @fmt: ptr to v4l2 format structure
1566 */
1567static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1568 struct v4l2_format *fmt)
1569{
1570 struct vpif_fh *fh = priv;
1571 struct channel_obj *ch = fh->channel;
1572 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1573
1574 return vpif_check_format(ch, pixfmt, 1);
1575}
1576
1577
1578/**
1579 * vpif_g_fmt_vid_cap() - Set INPUT handler
1580 * @file: file ptr
1581 * @priv: file handle
1582 * @fmt: ptr to v4l2 format structure
1583 */
1584static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1585 struct v4l2_format *fmt)
1586{
1587 struct vpif_fh *fh = priv;
1588 struct channel_obj *ch = fh->channel;
1589 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1590
1591 /* Check the validity of the buffer type */
1592 if (common->fmt.type != fmt->type)
1593 return -EINVAL;
1594
1595 /* Fill in the information about format */
6ffefff5 1596 *fmt = common->fmt;
6ffefff5
MK
1597 return 0;
1598}
1599
1600/**
1601 * vpif_s_fmt_vid_cap() - Set FMT handler
1602 * @file: file ptr
1603 * @priv: file handle
1604 * @fmt: ptr to v4l2 format structure
1605 */
1606static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1607 struct v4l2_format *fmt)
1608{
1609 struct vpif_fh *fh = priv;
1610 struct channel_obj *ch = fh->channel;
1611 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1612 struct v4l2_pix_format *pixfmt;
1613 int ret = 0;
1614
2c0ddd17 1615 vpif_dbg(2, debug, "%s\n", __func__);
6ffefff5
MK
1616
1617 /* If streaming is started, return error */
1618 if (common->started) {
1619 vpif_dbg(1, debug, "Streaming is started\n");
1620 return -EBUSY;
1621 }
1622
1623 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1624 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1625 if (!fh->initialized) {
1626 vpif_dbg(1, debug, "Channel Busy\n");
1627 return -EBUSY;
1628 }
1629 }
1630
ffb4877b 1631 ret = v4l2_prio_check(&ch->prio, fh->prio);
6ffefff5
MK
1632 if (0 != ret)
1633 return ret;
1634
1635 fh->initialized = 1;
1636
1637 pixfmt = &fmt->fmt.pix;
1638 /* Check for valid field format */
1639 ret = vpif_check_format(ch, pixfmt, 0);
1640
1641 if (ret)
1642 return ret;
1643 /* store the format in the channel object */
6ffefff5 1644 common->fmt = *fmt;
6ffefff5
MK
1645 return 0;
1646}
1647
1648/**
1649 * vpif_querycap() - QUERYCAP handler
1650 * @file: file ptr
1651 * @priv: file handle
1652 * @cap: ptr to v4l2_capability structure
1653 */
1654static int vpif_querycap(struct file *file, void *priv,
1655 struct v4l2_capability *cap)
1656{
1657 struct vpif_capture_config *config = vpif_dev->platform_data;
1658
626d533f
LP
1659 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1660 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1661 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1662 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1663 dev_name(vpif_dev));
6ffefff5
MK
1664 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1665
1666 return 0;
1667}
1668
1669/**
1670 * vpif_g_priority() - get priority handler
1671 * @file: file ptr
1672 * @priv: file handle
1673 * @prio: ptr to v4l2_priority structure
1674 */
1675static int vpif_g_priority(struct file *file, void *priv,
1676 enum v4l2_priority *prio)
1677{
1678 struct vpif_fh *fh = priv;
1679 struct channel_obj *ch = fh->channel;
1680
1681 *prio = v4l2_prio_max(&ch->prio);
1682
1683 return 0;
1684}
1685
1686/**
1687 * vpif_s_priority() - set priority handler
1688 * @file: file ptr
1689 * @priv: file handle
1690 * @prio: ptr to v4l2_priority structure
1691 */
1692static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1693{
1694 struct vpif_fh *fh = priv;
1695 struct channel_obj *ch = fh->channel;
1696
1697 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1698}
1699
1700/**
1701 * vpif_cropcap() - cropcap handler
1702 * @file: file ptr
1703 * @priv: file handle
1704 * @crop: ptr to v4l2_cropcap structure
1705 */
1706static int vpif_cropcap(struct file *file, void *priv,
1707 struct v4l2_cropcap *crop)
1708{
1709 struct vpif_fh *fh = priv;
1710 struct channel_obj *ch = fh->channel;
1711 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1712
1713 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1714 return -EINVAL;
1715
1716 crop->bounds.left = 0;
1717 crop->bounds.top = 0;
1718 crop->bounds.height = common->height;
1719 crop->bounds.width = common->width;
1720 crop->defrect = crop->bounds;
1721 return 0;
1722}
1723
40c8bcea 1724/**
0598c17b 1725 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
40c8bcea
MR
1726 * @file: file ptr
1727 * @priv: file handle
0598c17b 1728 * @timings: input timings
40c8bcea 1729 */
0598c17b
HV
1730static int
1731vpif_enum_dv_timings(struct file *file, void *priv,
1732 struct v4l2_enum_dv_timings *timings)
40c8bcea
MR
1733{
1734 struct vpif_fh *fh = priv;
1735 struct channel_obj *ch = fh->channel;
178cce12 1736 int ret;
40c8bcea 1737
178cce12 1738 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
e070f1b4 1739 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
178cce12
HV
1740 return -EINVAL;
1741 return ret;
40c8bcea
MR
1742}
1743
1744/**
0598c17b 1745 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
40c8bcea
MR
1746 * @file: file ptr
1747 * @priv: file handle
0598c17b 1748 * @timings: input timings
40c8bcea 1749 */
0598c17b
HV
1750static int
1751vpif_query_dv_timings(struct file *file, void *priv,
1752 struct v4l2_dv_timings *timings)
40c8bcea
MR
1753{
1754 struct vpif_fh *fh = priv;
1755 struct channel_obj *ch = fh->channel;
178cce12 1756 int ret;
40c8bcea 1757
178cce12 1758 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
e070f1b4 1759 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
178cce12
HV
1760 return -ENODATA;
1761 return ret;
40c8bcea
MR
1762}
1763
c027e165
MR
1764/**
1765 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1766 * @file: file ptr
1767 * @priv: file handle
1768 * @timings: digital video timings
1769 */
1770static int vpif_s_dv_timings(struct file *file, void *priv,
1771 struct v4l2_dv_timings *timings)
1772{
1773 struct vpif_fh *fh = priv;
1774 struct channel_obj *ch = fh->channel;
1775 struct vpif_params *vpifparams = &ch->vpifparams;
1776 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1777 struct video_obj *vid_ch = &ch->video;
0598c17b 1778 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
c027e165
MR
1779 int ret;
1780
1781 if (timings->type != V4L2_DV_BT_656_1120) {
1782 vpif_dbg(2, debug, "Timing type not defined\n");
1783 return -EINVAL;
1784 }
1785
1786 /* Configure subdevice timings, if any */
178cce12
HV
1787 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1788 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1789 ret = 0;
c027e165
MR
1790 if (ret < 0) {
1791 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1792 return ret;
1793 }
1794
1795 if (!(timings->bt.width && timings->bt.height &&
1796 (timings->bt.hbackporch ||
1797 timings->bt.hfrontporch ||
1798 timings->bt.hsync) &&
1799 timings->bt.vfrontporch &&
1800 (timings->bt.vbackporch ||
1801 timings->bt.vsync))) {
1802 vpif_dbg(2, debug, "Timings for width, height, "
1803 "horizontal back porch, horizontal sync, "
1804 "horizontal front porch, vertical back porch, "
1805 "vertical sync and vertical back porch "
1806 "must be defined\n");
1807 return -EINVAL;
1808 }
1809
0598c17b 1810 vid_ch->dv_timings = *timings;
c027e165
MR
1811
1812 /* Configure video port timings */
1813
1814 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1815 bt->hsync - 8;
1816 std_info->sav2eav = bt->width;
1817
1818 std_info->l1 = 1;
1819 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1820
1821 if (bt->interlaced) {
1822 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1823 std_info->vsize = bt->height * 2 +
1824 bt->vfrontporch + bt->vsync + bt->vbackporch +
1825 bt->il_vfrontporch + bt->il_vsync +
1826 bt->il_vbackporch;
1827 std_info->l5 = std_info->vsize/2 -
1828 (bt->vfrontporch - 1);
1829 std_info->l7 = std_info->vsize/2 + 1;
1830 std_info->l9 = std_info->l7 + bt->il_vsync +
1831 bt->il_vbackporch + 1;
1832 std_info->l11 = std_info->vsize -
1833 (bt->il_vfrontporch - 1);
1834 } else {
1835 vpif_dbg(2, debug, "Required timing values for "
1836 "interlaced BT format missing\n");
1837 return -EINVAL;
1838 }
1839 } else {
1840 std_info->vsize = bt->height + bt->vfrontporch +
1841 bt->vsync + bt->vbackporch;
1842 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1843 }
1844 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1845 std_info->width = bt->width;
1846 std_info->height = bt->height;
1847 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1848 std_info->ycmux_mode = 0;
1849 std_info->capture_format = 0;
1850 std_info->vbi_supported = 0;
1851 std_info->hd_sd = 1;
1852 std_info->stdid = 0;
c027e165
MR
1853
1854 vid_ch->stdid = 0;
c027e165
MR
1855 return 0;
1856}
1857
1858/**
1859 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1860 * @file: file ptr
1861 * @priv: file handle
1862 * @timings: digital video timings
1863 */
1864static int vpif_g_dv_timings(struct file *file, void *priv,
1865 struct v4l2_dv_timings *timings)
1866{
1867 struct vpif_fh *fh = priv;
1868 struct channel_obj *ch = fh->channel;
1869 struct video_obj *vid_ch = &ch->video;
c027e165 1870
0598c17b 1871 *timings = vid_ch->dv_timings;
c027e165
MR
1872
1873 return 0;
1874}
1875
7036d6a7
MR
1876/*
1877 * vpif_g_chip_ident() - Identify the chip
1878 * @file: file ptr
1879 * @priv: file handle
1880 * @chip: chip identity
1881 *
1882 * Returns zero or -EINVAL if read operations fails.
1883 */
1884static int vpif_g_chip_ident(struct file *file, void *priv,
1885 struct v4l2_dbg_chip_ident *chip)
1886{
1887 chip->ident = V4L2_IDENT_NONE;
1888 chip->revision = 0;
1889 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1890 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1891 vpif_dbg(2, debug, "match_type is invalid.\n");
1892 return -EINVAL;
1893 }
1894
1895 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1896 g_chip_ident, chip);
1897}
1898
1899#ifdef CONFIG_VIDEO_ADV_DEBUG
1900/*
1901 * vpif_dbg_g_register() - Read register
1902 * @file: file ptr
1903 * @priv: file handle
1904 * @reg: register to be read
1905 *
1906 * Debugging only
1907 * Returns zero or -EINVAL if read operations fails.
1908 */
1909static int vpif_dbg_g_register(struct file *file, void *priv,
1910 struct v4l2_dbg_register *reg){
1911 struct vpif_fh *fh = priv;
1912 struct channel_obj *ch = fh->channel;
1913
178cce12 1914 return v4l2_subdev_call(ch->sd, core, g_register, reg);
7036d6a7
MR
1915}
1916
1917/*
1918 * vpif_dbg_s_register() - Write to register
1919 * @file: file ptr
1920 * @priv: file handle
1921 * @reg: register to be modified
1922 *
1923 * Debugging only
1924 * Returns zero or -EINVAL if write operations fails.
1925 */
1926static int vpif_dbg_s_register(struct file *file, void *priv,
977ba3b1
HV
1927 const struct v4l2_dbg_register *reg)
1928{
7036d6a7
MR
1929 struct vpif_fh *fh = priv;
1930 struct channel_obj *ch = fh->channel;
1931
178cce12 1932 return v4l2_subdev_call(ch->sd, core, s_register, reg);
7036d6a7
MR
1933}
1934#endif
1935
1936/*
1937 * vpif_log_status() - Status information
1938 * @file: file ptr
1939 * @priv: file handle
1940 *
1941 * Returns zero.
1942 */
1943static int vpif_log_status(struct file *filep, void *priv)
1944{
1945 /* status for sub devices */
1946 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1947
1948 return 0;
1949}
1950
6ffefff5
MK
1951/* vpif capture ioctl operations */
1952static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1953 .vidioc_querycap = vpif_querycap,
1954 .vidioc_g_priority = vpif_g_priority,
1955 .vidioc_s_priority = vpif_s_priority,
1956 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1957 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1958 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1959 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1960 .vidioc_enum_input = vpif_enum_input,
1961 .vidioc_s_input = vpif_s_input,
1962 .vidioc_g_input = vpif_g_input,
1963 .vidioc_reqbufs = vpif_reqbufs,
1964 .vidioc_querybuf = vpif_querybuf,
1965 .vidioc_querystd = vpif_querystd,
1966 .vidioc_s_std = vpif_s_std,
1967 .vidioc_g_std = vpif_g_std,
1968 .vidioc_qbuf = vpif_qbuf,
1969 .vidioc_dqbuf = vpif_dqbuf,
1970 .vidioc_streamon = vpif_streamon,
1971 .vidioc_streamoff = vpif_streamoff,
1972 .vidioc_cropcap = vpif_cropcap,
0598c17b
HV
1973 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1974 .vidioc_query_dv_timings = vpif_query_dv_timings,
c027e165
MR
1975 .vidioc_s_dv_timings = vpif_s_dv_timings,
1976 .vidioc_g_dv_timings = vpif_g_dv_timings,
7036d6a7
MR
1977 .vidioc_g_chip_ident = vpif_g_chip_ident,
1978#ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_register = vpif_dbg_g_register,
1980 .vidioc_s_register = vpif_dbg_s_register,
1981#endif
1982 .vidioc_log_status = vpif_log_status,
6ffefff5
MK
1983};
1984
1985/* vpif file operations */
1986static struct v4l2_file_operations vpif_fops = {
1987 .owner = THIS_MODULE,
1988 .open = vpif_open,
1989 .release = vpif_release,
46656afa 1990 .unlocked_ioctl = video_ioctl2,
6ffefff5
MK
1991 .mmap = vpif_mmap,
1992 .poll = vpif_poll
1993};
1994
1995/* vpif video template */
1996static struct video_device vpif_video_template = {
1997 .name = "vpif",
1998 .fops = &vpif_fops,
1999 .minor = -1,
2000 .ioctl_ops = &vpif_ioctl_ops,
2001};
2002
2003/**
2004 * initialize_vpif() - Initialize vpif data structures
2005 *
2006 * Allocate memory for data structures and initialize them
2007 */
2008static int initialize_vpif(void)
2009{
2010 int err = 0, i, j;
2011 int free_channel_objects_index;
2012
2013 /* Default number of buffers should be 3 */
2014 if ((ch0_numbuffers > 0) &&
2015 (ch0_numbuffers < config_params.min_numbuffers))
2016 ch0_numbuffers = config_params.min_numbuffers;
2017 if ((ch1_numbuffers > 0) &&
2018 (ch1_numbuffers < config_params.min_numbuffers))
2019 ch1_numbuffers = config_params.min_numbuffers;
2020
2021 /* Set buffer size to min buffers size if it is invalid */
2022 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2023 ch0_bufsize =
2024 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2025 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2026 ch1_bufsize =
2027 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2028
2029 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2030 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2031 if (ch0_numbuffers) {
2032 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2033 = ch0_bufsize;
2034 }
2035 if (ch1_numbuffers) {
2036 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2037 = ch1_bufsize;
2038 }
2039
2040 /* Allocate memory for six channel objects */
2041 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2042 vpif_obj.dev[i] =
2043 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2044 /* If memory allocation fails, return error */
2045 if (!vpif_obj.dev[i]) {
2046 free_channel_objects_index = i;
2047 err = -ENOMEM;
2048 goto vpif_init_free_channel_objects;
2049 }
2050 }
2051 return 0;
2052
2053vpif_init_free_channel_objects:
2054 for (j = 0; j < free_channel_objects_index; j++)
2055 kfree(vpif_obj.dev[j]);
2056 return err;
2057}
2058
2059/**
2060 * vpif_probe : This function probes the vpif capture driver
2061 * @pdev: platform device pointer
2062 *
2063 * This creates device entries by register itself to the V4L2 driver and
2064 * initializes fields of each channel objects
2065 */
2066static __init int vpif_probe(struct platform_device *pdev)
2067{
2068 struct vpif_subdev_info *subdevdata;
2069 struct vpif_capture_config *config;
5be452c3
HV
2070 int i, j, k, err;
2071 int res_idx = 0;
6ffefff5
MK
2072 struct i2c_adapter *i2c_adap;
2073 struct channel_obj *ch;
2074 struct common_obj *common;
2075 struct video_device *vfd;
2076 struct resource *res;
2077 int subdev_count;
764af39a 2078 size_t size;
6ffefff5
MK
2079
2080 vpif_dev = &pdev->dev;
2081
2082 err = initialize_vpif();
2083 if (err) {
2084 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2085 return err;
2086 }
2087
d2f7a1a2
HV
2088 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2089 if (err) {
2090 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2091 return err;
2092 }
2093
5be452c3 2094 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
6ffefff5 2095 for (i = res->start; i <= res->end; i++) {
0316b89a 2096 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
5be452c3
HV
2097 "VPIF_Capture", (void *)
2098 (&vpif_obj.dev[res_idx]->channel_id))) {
6ffefff5 2099 err = -EBUSY;
5be452c3
HV
2100 for (j = 0; j < i; j++)
2101 free_irq(j, (void *)
2102 (&vpif_obj.dev[res_idx]->channel_id));
6ffefff5
MK
2103 goto vpif_int_err;
2104 }
2105 }
5be452c3 2106 res_idx++;
6ffefff5
MK
2107 }
2108
2109 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2110 /* Get the pointer to the channel object */
2111 ch = vpif_obj.dev[i];
2112 /* Allocate memory for video device */
2113 vfd = video_device_alloc();
2114 if (NULL == vfd) {
2115 for (j = 0; j < i; j++) {
2116 ch = vpif_obj.dev[j];
2117 video_device_release(ch->video_dev);
2118 }
2119 err = -ENOMEM;
5be452c3 2120 goto vpif_int_err;
6ffefff5
MK
2121 }
2122
2123 /* Initialize field of video device */
2124 *vfd = vpif_video_template;
2125 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2126 vfd->release = video_device_release;
2127 snprintf(vfd->name, sizeof(vfd->name),
0a63172a 2128 "VPIF_Capture_DRIVER_V%s",
64dc3c1a 2129 VPIF_CAPTURE_VERSION);
6ffefff5
MK
2130 /* Set video_dev to the video device */
2131 ch->video_dev = vfd;
2132 }
2133
764af39a
MH
2134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2135 if (res) {
2136 size = resource_size(res);
2137 /* The resources are divided into two equal memory and when we
2138 * have HD output we can add them together
2139 */
2140 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2141 ch = vpif_obj.dev[j];
2142 ch->channel_id = j;
2143 /* only enabled if second resource exists */
2144 config_params.video_limit[ch->channel_id] = 0;
2145 if (size)
2146 config_params.video_limit[ch->channel_id] =
2147 size/2;
2148 }
2149 }
2150
6ffefff5
MK
2151 i2c_adap = i2c_get_adapter(1);
2152 config = pdev->dev.platform_data;
2153
2154 subdev_count = config->subdev_count;
1f8766b4 2155 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
6ffefff5
MK
2156 GFP_KERNEL);
2157 if (vpif_obj.sd == NULL) {
2158 vpif_err("unable to allocate memory for subdevice pointers\n");
2159 err = -ENOMEM;
5be452c3 2160 goto vpif_sd_error;
6ffefff5
MK
2161 }
2162
6ffefff5
MK
2163 for (i = 0; i < subdev_count; i++) {
2164 subdevdata = &config->subdev_info[i];
2165 vpif_obj.sd[i] =
2166 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2167 i2c_adap,
6ffefff5
MK
2168 &subdevdata->board_info,
2169 NULL);
2170
2171 if (!vpif_obj.sd[i]) {
2172 vpif_err("Error registering v4l2 subdevice\n");
2173 goto probe_subdev_out;
2174 }
2175 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2176 subdevdata->name);
6ffefff5 2177 }
6ffefff5 2178
b65814e5
HV
2179 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2180 ch = vpif_obj.dev[j];
2181 ch->channel_id = j;
2182 common = &(ch->common[VPIF_VIDEO_INDEX]);
2183 spin_lock_init(&common->irqlock);
2184 mutex_init(&common->lock);
2185 ch->video_dev->lock = &common->lock;
2186 /* Initialize prio member of channel object */
2187 v4l2_prio_init(&ch->prio);
2188 video_set_drvdata(ch->video_dev, ch);
2189
178cce12
HV
2190 /* select input 0 */
2191 err = vpif_set_input(config, ch, 0);
2192 if (err)
2193 goto probe_out;
2194
b65814e5
HV
2195 err = video_register_device(ch->video_dev,
2196 VFL_TYPE_GRABBER, (j ? 1 : 0));
2197 if (err)
2198 goto probe_out;
2199 }
0a63172a 2200 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
6ffefff5
MK
2201 return 0;
2202
6ffefff5 2203probe_out:
6ffefff5
MK
2204 for (k = 0; k < j; k++) {
2205 /* Get the pointer to the channel object */
2206 ch = vpif_obj.dev[k];
2207 /* Unregister video device */
2208 video_unregister_device(ch->video_dev);
2209 }
b65814e5
HV
2210probe_subdev_out:
2211 /* free sub devices memory */
2212 kfree(vpif_obj.sd);
6ffefff5 2213
5be452c3
HV
2214vpif_sd_error:
2215 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2216 ch = vpif_obj.dev[i];
2217 /* Note: does nothing if ch->video_dev == NULL */
2218 video_device_release(ch->video_dev);
6ffefff5 2219 }
5be452c3 2220vpif_int_err:
d2f7a1a2 2221 v4l2_device_unregister(&vpif_obj.v4l2_dev);
5be452c3
HV
2222 for (i = 0; i < res_idx; i++) {
2223 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
2224 for (j = res->start; j <= res->end; j++)
2225 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
2226 }
6ffefff5
MK
2227 return err;
2228}
2229
2230/**
2231 * vpif_remove() - driver remove handler
2232 * @device: ptr to platform device structure
2233 *
2234 * The vidoe device is unregistered
2235 */
2236static int vpif_remove(struct platform_device *device)
2237{
2238 int i;
2239 struct channel_obj *ch;
2240
2241 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2242
2243 /* un-register device */
2244 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2245 /* Get the pointer to the channel object */
2246 ch = vpif_obj.dev[i];
2247 /* Unregister video device */
2248 video_unregister_device(ch->video_dev);
2249 }
2250 return 0;
2251}
2252
3d5946dc 2253#ifdef CONFIG_PM
6ffefff5
MK
2254/**
2255 * vpif_suspend: vpif device suspend
6ffefff5 2256 */
3d5946dc 2257static int vpif_suspend(struct device *dev)
6ffefff5 2258{
3d5946dc
MH
2259
2260 struct common_obj *common;
2261 struct channel_obj *ch;
2262 int i;
2263
2264 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2265 /* Get the pointer to the channel object */
2266 ch = vpif_obj.dev[i];
2267 common = &ch->common[VPIF_VIDEO_INDEX];
2268 mutex_lock(&common->lock);
2269 if (ch->usrs && common->io_usrs) {
2270 /* Disable channel */
2271 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2272 enable_channel0(0);
2273 channel0_intr_enable(0);
2274 }
2275 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2276 common->started == 2) {
2277 enable_channel1(0);
2278 channel1_intr_enable(0);
2279 }
2280 }
2281 mutex_unlock(&common->lock);
2282 }
2283
2284 return 0;
6ffefff5
MK
2285}
2286
3d5946dc 2287/*
6ffefff5 2288 * vpif_resume: vpif device suspend
6ffefff5 2289 */
3d5946dc 2290static int vpif_resume(struct device *dev)
6ffefff5 2291{
3d5946dc
MH
2292 struct common_obj *common;
2293 struct channel_obj *ch;
2294 int i;
2295
2296 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2297 /* Get the pointer to the channel object */
2298 ch = vpif_obj.dev[i];
2299 common = &ch->common[VPIF_VIDEO_INDEX];
2300 mutex_lock(&common->lock);
2301 if (ch->usrs && common->io_usrs) {
2302 /* Disable channel */
2303 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2304 enable_channel0(1);
2305 channel0_intr_enable(1);
2306 }
2307 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2308 common->started == 2) {
2309 enable_channel1(1);
2310 channel1_intr_enable(1);
2311 }
2312 }
2313 mutex_unlock(&common->lock);
2314 }
2315
2316 return 0;
6ffefff5
MK
2317}
2318
47145210 2319static const struct dev_pm_ops vpif_dev_pm_ops = {
6ffefff5
MK
2320 .suspend = vpif_suspend,
2321 .resume = vpif_resume,
2322};
2323
3d5946dc
MH
2324#define vpif_pm_ops (&vpif_dev_pm_ops)
2325#else
2326#define vpif_pm_ops NULL
2327#endif
2328
ffa1b391 2329static __refdata struct platform_driver vpif_driver = {
6ffefff5
MK
2330 .driver = {
2331 .name = "vpif_capture",
2332 .owner = THIS_MODULE,
3d5946dc 2333 .pm = vpif_pm_ops,
6ffefff5
MK
2334 },
2335 .probe = vpif_probe,
2336 .remove = vpif_remove,
2337};
2338
2339/**
2340 * vpif_init: initialize the vpif driver
2341 *
2342 * This function registers device and driver to the kernel, requests irq
2343 * handler and allocates memory
2344 * for channel objects
2345 */
2346static __init int vpif_init(void)
2347{
2348 return platform_driver_register(&vpif_driver);
2349}
2350
2351/**
2352 * vpif_cleanup : This function clean up the vpif capture resources
2353 *
2354 * This will un-registers device and driver to the kernel, frees
2355 * requested irq handler and de-allocates memory allocated for channel
2356 * objects.
2357 */
2358static void vpif_cleanup(void)
2359{
2360 struct platform_device *pdev;
2361 struct resource *res;
2362 int irq_num;
2363 int i = 0;
2364
2365 pdev = container_of(vpif_dev, struct platform_device, dev);
2366 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2367 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2368 free_irq(irq_num,
2369 (void *)(&vpif_obj.dev[i]->channel_id));
2370 i++;
2371 }
2372
2373 platform_driver_unregister(&vpif_driver);
2374
2375 kfree(vpif_obj.sd);
2376 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2377 kfree(vpif_obj.dev[i]);
2378}
2379
2380/* Function for module initialization and cleanup */
2381module_init(vpif_init);
2382module_exit(vpif_cleanup);