GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / davinci / vpif_capture.c
bloba7f48b53d3fc5aac3ac1cc48c770d6061e3ff8f4
1 /*
2 * Copyright (C) 2009 Texas Instruments Inc
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.
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.
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
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
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>
36 #include <linux/version.h>
37 #include <linux/slab.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
41 #include "vpif_capture.h"
42 #include "vpif.h"
44 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45 MODULE_LICENSE("GPL");
47 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
48 #define vpif_dbg(level, debug, fmt, arg...) \
49 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51 static int debug = 1;
52 static u32 ch0_numbuffers = 3;
53 static u32 ch1_numbuffers = 3;
54 static u32 ch0_bufsize = 1920 * 1080 * 2;
55 static u32 ch1_bufsize = 720 * 576 * 2;
57 module_param(debug, int, 0644);
58 module_param(ch0_numbuffers, uint, S_IRUGO);
59 module_param(ch1_numbuffers, uint, S_IRUGO);
60 module_param(ch0_bufsize, uint, S_IRUGO);
61 module_param(ch1_bufsize, uint, S_IRUGO);
63 MODULE_PARM_DESC(debug, "Debug level 0-1");
64 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
65 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
66 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
67 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
69 static struct vpif_config_params config_params = {
70 .min_numbuffers = 3,
71 .numbuffers[0] = 3,
72 .numbuffers[1] = 3,
73 .min_bufsize[0] = 720 * 480 * 2,
74 .min_bufsize[1] = 720 * 480 * 2,
75 .channel_bufsize[0] = 1920 * 1080 * 2,
76 .channel_bufsize[1] = 720 * 576 * 2,
79 /* global variables */
80 static struct vpif_device vpif_obj = { {NULL} };
81 static struct device *vpif_dev;
83 /**
84 * ch_params: video standard configuration parameters for vpif
86 static const struct vpif_channel_config_params ch_params[] = {
88 "NTSC_M", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
89 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
92 "PAL_BDGHIK", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
93 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
97 /**
98 * vpif_uservirt_to_phys : translate user/virtual address to phy address
99 * @virtp: user/virtual address
101 * This inline function is used to convert user space virtual address to
102 * physical address.
104 static inline u32 vpif_uservirt_to_phys(u32 virtp)
106 unsigned long physp = 0;
107 struct mm_struct *mm = current->mm;
108 struct vm_area_struct *vma;
110 vma = find_vma(mm, virtp);
112 /* For kernel direct-mapped memory, take the easy way */
113 if (virtp >= PAGE_OFFSET)
114 physp = virt_to_phys((void *)virtp);
115 else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
117 * this will catch, kernel-allocated, mmaped-to-usermode
118 * addresses
120 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
121 else {
122 /* otherwise, use get_user_pages() for general userland pages */
123 int res, nr_pages = 1;
124 struct page *pages;
126 down_read(&current->mm->mmap_sem);
128 res = get_user_pages(current, current->mm,
129 virtp, nr_pages, 1, 0, &pages, NULL);
130 up_read(&current->mm->mmap_sem);
132 if (res == nr_pages)
133 physp = __pa(page_address(&pages[0]) +
134 (virtp & ~PAGE_MASK));
135 else {
136 vpif_err("get_user_pages failed\n");
137 return 0;
140 return physp;
144 * buffer_prepare : callback function for buffer prepare
145 * @q : buffer queue ptr
146 * @vb: ptr to video buffer
147 * @field: field info
149 * This is the callback function for buffer prepare when videobuf_qbuf()
150 * function is called. The buffer is prepared and user space virtual address
151 * or user address is converted into physical address
153 static int vpif_buffer_prepare(struct videobuf_queue *q,
154 struct videobuf_buffer *vb,
155 enum v4l2_field field)
157 /* Get the file handle object and channel object */
158 struct vpif_fh *fh = q->priv_data;
159 struct channel_obj *ch = fh->channel;
160 struct common_obj *common;
161 unsigned long addr;
164 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
166 common = &ch->common[VPIF_VIDEO_INDEX];
168 /* If buffer is not initialized, initialize it */
169 if (VIDEOBUF_NEEDS_INIT == vb->state) {
170 vb->width = common->width;
171 vb->height = common->height;
172 vb->size = vb->width * vb->height;
173 vb->field = field;
175 vb->state = VIDEOBUF_PREPARED;
177 * if user pointer memory mechanism is used, get the physical
178 * address of the buffer
180 if (V4L2_MEMORY_USERPTR == common->memory) {
181 if (0 == vb->baddr) {
182 vpif_dbg(1, debug, "buffer address is 0\n");
183 return -EINVAL;
186 vb->boff = vpif_uservirt_to_phys(vb->baddr);
187 if (!IS_ALIGNED(vb->boff, 8))
188 goto exit;
191 addr = vb->boff;
192 if (q->streaming) {
193 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
194 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
195 !IS_ALIGNED((addr + common->ctop_off), 8) ||
196 !IS_ALIGNED((addr + common->cbtm_off), 8))
197 goto exit;
199 return 0;
200 exit:
201 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
202 return -EINVAL;
206 * vpif_buffer_setup : Callback function for buffer setup.
207 * @q: buffer queue ptr
208 * @count: number of buffers
209 * @size: size of the buffer
211 * This callback function is called when reqbuf() is called to adjust
212 * the buffer count and buffer size
214 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
215 unsigned int *size)
217 /* Get the file handle object and channel object */
218 struct vpif_fh *fh = q->priv_data;
219 struct channel_obj *ch = fh->channel;
220 struct common_obj *common;
222 common = &ch->common[VPIF_VIDEO_INDEX];
224 vpif_dbg(2, debug, "vpif_buffer_setup\n");
226 /* If memory type is not mmap, return */
227 if (V4L2_MEMORY_MMAP != common->memory)
228 return 0;
230 /* Calculate the size of the buffer */
231 *size = config_params.channel_bufsize[ch->channel_id];
233 if (*count < config_params.min_numbuffers)
234 *count = config_params.min_numbuffers;
235 return 0;
239 * vpif_buffer_queue : Callback function to add buffer to DMA queue
240 * @q: ptr to videobuf_queue
241 * @vb: ptr to videobuf_buffer
243 static void vpif_buffer_queue(struct videobuf_queue *q,
244 struct videobuf_buffer *vb)
246 /* Get the file handle object and channel object */
247 struct vpif_fh *fh = q->priv_data;
248 struct channel_obj *ch = fh->channel;
249 struct common_obj *common;
251 common = &ch->common[VPIF_VIDEO_INDEX];
253 vpif_dbg(2, debug, "vpif_buffer_queue\n");
255 /* add the buffer to the DMA queue */
256 list_add_tail(&vb->queue, &common->dma_queue);
257 /* Change state of the buffer */
258 vb->state = VIDEOBUF_QUEUED;
262 * vpif_buffer_release : Callback function to free buffer
263 * @q: buffer queue ptr
264 * @vb: ptr to video buffer
266 * This function is called from the videobuf layer to free memory
267 * allocated to the buffers
269 static void vpif_buffer_release(struct videobuf_queue *q,
270 struct videobuf_buffer *vb)
272 /* Get the file handle object and channel object */
273 struct vpif_fh *fh = q->priv_data;
274 struct channel_obj *ch = fh->channel;
275 struct common_obj *common;
277 common = &ch->common[VPIF_VIDEO_INDEX];
279 videobuf_dma_contig_free(q, vb);
280 vb->state = VIDEOBUF_NEEDS_INIT;
283 static struct videobuf_queue_ops video_qops = {
284 .buf_setup = vpif_buffer_setup,
285 .buf_prepare = vpif_buffer_prepare,
286 .buf_queue = vpif_buffer_queue,
287 .buf_release = vpif_buffer_release,
290 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
291 { {1, 1} };
294 * vpif_process_buffer_complete: process a completed buffer
295 * @common: ptr to common channel object
297 * This function time stamp the buffer and mark it as DONE. It also
298 * wake up any process waiting on the QUEUE and set the next buffer
299 * as current
301 static void vpif_process_buffer_complete(struct common_obj *common)
303 do_gettimeofday(&common->cur_frm->ts);
304 common->cur_frm->state = VIDEOBUF_DONE;
305 wake_up_interruptible(&common->cur_frm->done);
306 /* Make curFrm pointing to nextFrm */
307 common->cur_frm = common->next_frm;
311 * vpif_schedule_next_buffer: set next buffer address for capture
312 * @common : ptr to common channel object
314 * This function will get next buffer from the dma queue and
315 * set the buffer address in the vpif register for capture.
316 * the buffer is marked active
318 static void vpif_schedule_next_buffer(struct common_obj *common)
320 unsigned long addr = 0;
322 common->next_frm = list_entry(common->dma_queue.next,
323 struct videobuf_buffer, queue);
324 /* Remove that buffer from the buffer queue */
325 list_del(&common->next_frm->queue);
326 common->next_frm->state = VIDEOBUF_ACTIVE;
327 if (V4L2_MEMORY_USERPTR == common->memory)
328 addr = common->next_frm->boff;
329 else
330 addr = videobuf_to_dma_contig(common->next_frm);
332 /* Set top and bottom field addresses in VPIF registers */
333 common->set_addr(addr + common->ytop_off,
334 addr + common->ybtm_off,
335 addr + common->ctop_off,
336 addr + common->cbtm_off);
340 * vpif_channel_isr : ISR handler for vpif capture
341 * @irq: irq number
342 * @dev_id: dev_id ptr
344 * It changes status of the captured buffer, takes next buffer from the queue
345 * and sets its address in VPIF registers
347 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
349 struct vpif_device *dev = &vpif_obj;
350 struct common_obj *common;
351 struct channel_obj *ch;
352 enum v4l2_field field;
353 int channel_id = 0;
354 int fid = -1, i;
356 channel_id = *(int *)(dev_id);
357 ch = dev->dev[channel_id];
359 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
361 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
362 common = &ch->common[i];
363 /* skip If streaming is not started in this channel */
364 if (0 == common->started)
365 continue;
367 /* Check the field format */
368 if (1 == ch->vpifparams.std_info.frm_fmt) {
369 /* Progressive mode */
370 if (list_empty(&common->dma_queue))
371 continue;
373 if (!channel_first_int[i][channel_id])
374 vpif_process_buffer_complete(common);
376 channel_first_int[i][channel_id] = 0;
378 vpif_schedule_next_buffer(common);
381 channel_first_int[i][channel_id] = 0;
382 } else {
384 * Interlaced mode. If it is first interrupt, ignore
385 * it
387 if (channel_first_int[i][channel_id]) {
388 channel_first_int[i][channel_id] = 0;
389 continue;
391 if (0 == i) {
392 ch->field_id ^= 1;
393 /* Get field id from VPIF registers */
394 fid = vpif_channel_getfid(ch->channel_id);
395 if (fid != ch->field_id) {
397 * If field id does not match stored
398 * field id, make them in sync
400 if (0 == fid)
401 ch->field_id = fid;
402 return IRQ_HANDLED;
405 /* device field id and local field id are in sync */
406 if (0 == fid) {
407 /* this is even field */
408 if (common->cur_frm == common->next_frm)
409 continue;
411 /* mark the current buffer as done */
412 vpif_process_buffer_complete(common);
413 } else if (1 == fid) {
414 /* odd field */
415 if (list_empty(&common->dma_queue) ||
416 (common->cur_frm != common->next_frm))
417 continue;
419 vpif_schedule_next_buffer(common);
423 return IRQ_HANDLED;
427 * vpif_update_std_info() - update standard related info
428 * @ch: ptr to channel object
430 * For a given standard selected by application, update values
431 * in the device data structures
433 static int vpif_update_std_info(struct channel_obj *ch)
435 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
436 struct vpif_params *vpifparams = &ch->vpifparams;
437 const struct vpif_channel_config_params *config;
438 struct vpif_channel_config_params *std_info;
439 struct video_obj *vid_ch = &ch->video;
440 int index;
442 vpif_dbg(2, debug, "vpif_update_std_info\n");
444 std_info = &vpifparams->std_info;
446 for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
447 config = &ch_params[index];
448 if (config->stdid & vid_ch->stdid) {
449 memcpy(std_info, config, sizeof(*config));
450 break;
454 /* standard not found */
455 if (index == ARRAY_SIZE(ch_params))
456 return -EINVAL;
458 common->fmt.fmt.pix.width = std_info->width;
459 common->width = std_info->width;
460 common->fmt.fmt.pix.height = std_info->height;
461 common->height = std_info->height;
462 common->fmt.fmt.pix.bytesperline = std_info->width;
463 vpifparams->video_params.hpitch = std_info->width;
464 vpifparams->video_params.storage_mode = std_info->frm_fmt;
465 return 0;
469 * vpif_calculate_offsets : This function calculates buffers offsets
470 * @ch : ptr to channel object
472 * This function calculates buffer offsets for Y and C in the top and
473 * bottom field
475 static void vpif_calculate_offsets(struct channel_obj *ch)
477 unsigned int hpitch, vpitch, sizeimage;
478 struct video_obj *vid_ch = &(ch->video);
479 struct vpif_params *vpifparams = &ch->vpifparams;
480 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
481 enum v4l2_field field = common->fmt.fmt.pix.field;
483 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
485 if (V4L2_FIELD_ANY == field) {
486 if (vpifparams->std_info.frm_fmt)
487 vid_ch->buf_field = V4L2_FIELD_NONE;
488 else
489 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
490 } else
491 vid_ch->buf_field = common->fmt.fmt.pix.field;
493 if (V4L2_MEMORY_USERPTR == common->memory)
494 sizeimage = common->fmt.fmt.pix.sizeimage;
495 else
496 sizeimage = config_params.channel_bufsize[ch->channel_id];
498 hpitch = common->fmt.fmt.pix.bytesperline;
499 vpitch = sizeimage / (hpitch * 2);
501 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
502 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
503 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
504 common->ytop_off = 0;
505 common->ybtm_off = hpitch;
506 common->ctop_off = sizeimage / 2;
507 common->cbtm_off = sizeimage / 2 + hpitch;
508 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
509 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
510 common->ytop_off = 0;
511 common->ybtm_off = sizeimage / 4;
512 common->ctop_off = sizeimage / 2;
513 common->cbtm_off = common->ctop_off + sizeimage / 4;
514 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
515 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
516 common->ybtm_off = 0;
517 common->ytop_off = sizeimage / 4;
518 common->cbtm_off = sizeimage / 2;
519 common->ctop_off = common->cbtm_off + sizeimage / 4;
521 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
522 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
523 vpifparams->video_params.storage_mode = 1;
524 else
525 vpifparams->video_params.storage_mode = 0;
527 if (1 == vpifparams->std_info.frm_fmt)
528 vpifparams->video_params.hpitch =
529 common->fmt.fmt.pix.bytesperline;
530 else {
531 if ((field == V4L2_FIELD_ANY)
532 || (field == V4L2_FIELD_INTERLACED))
533 vpifparams->video_params.hpitch =
534 common->fmt.fmt.pix.bytesperline * 2;
535 else
536 vpifparams->video_params.hpitch =
537 common->fmt.fmt.pix.bytesperline;
540 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
544 * vpif_config_format: configure default frame format in the device
545 * ch : ptr to channel object
547 static void vpif_config_format(struct channel_obj *ch)
549 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
551 vpif_dbg(2, debug, "vpif_config_format\n");
553 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
554 if (config_params.numbuffers[ch->channel_id] == 0)
555 common->memory = V4L2_MEMORY_USERPTR;
556 else
557 common->memory = V4L2_MEMORY_MMAP;
559 common->fmt.fmt.pix.sizeimage
560 = config_params.channel_bufsize[ch->channel_id];
562 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
563 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
564 else
565 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
566 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
570 * vpif_get_default_field() - Get default field type based on interface
571 * @vpif_params - ptr to vpif params
573 static inline enum v4l2_field vpif_get_default_field(
574 struct vpif_interface *iface)
576 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
577 V4L2_FIELD_INTERLACED;
581 * vpif_check_format() - check given pixel format for compatibility
582 * @ch - channel ptr
583 * @pixfmt - Given pixel format
584 * @update - update the values as per hardware requirement
586 * Check the application pixel format for S_FMT and update the input
587 * values as per hardware limits for TRY_FMT. The default pixel and
588 * field format is selected based on interface type.
590 static int vpif_check_format(struct channel_obj *ch,
591 struct v4l2_pix_format *pixfmt,
592 int update)
594 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
595 struct vpif_params *vpif_params = &ch->vpifparams;
596 enum v4l2_field field = pixfmt->field;
597 u32 sizeimage, hpitch, vpitch;
598 int ret = -EINVAL;
600 vpif_dbg(2, debug, "vpif_check_format\n");
602 * first check for the pixel format. If if_type is Raw bayer,
603 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
604 * V4L2_PIX_FMT_YUV422P is supported
606 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
607 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
608 if (!update) {
609 vpif_dbg(2, debug, "invalid pix format\n");
610 goto exit;
612 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
614 } else {
615 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
616 if (!update) {
617 vpif_dbg(2, debug, "invalid pixel format\n");
618 goto exit;
620 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
624 if (!(VPIF_VALID_FIELD(field))) {
625 if (!update) {
626 vpif_dbg(2, debug, "invalid field format\n");
627 goto exit;
630 * By default use FIELD_NONE for RAW Bayer capture
631 * and FIELD_INTERLACED for other interfaces
633 field = vpif_get_default_field(&vpif_params->iface);
634 } else if (field == V4L2_FIELD_ANY)
635 /* unsupported field. Use default */
636 field = vpif_get_default_field(&vpif_params->iface);
638 /* validate the hpitch */
639 hpitch = pixfmt->bytesperline;
640 if (hpitch < vpif_params->std_info.width) {
641 if (!update) {
642 vpif_dbg(2, debug, "invalid hpitch\n");
643 goto exit;
645 hpitch = vpif_params->std_info.width;
648 if (V4L2_MEMORY_USERPTR == common->memory)
649 sizeimage = pixfmt->sizeimage;
650 else
651 sizeimage = config_params.channel_bufsize[ch->channel_id];
653 vpitch = sizeimage / (hpitch * 2);
655 /* validate the vpitch */
656 if (vpitch < vpif_params->std_info.height) {
657 if (!update) {
658 vpif_dbg(2, debug, "Invalid vpitch\n");
659 goto exit;
661 vpitch = vpif_params->std_info.height;
664 /* Check for 8 byte alignment */
665 if (!ALIGN(hpitch, 8)) {
666 if (!update) {
667 vpif_dbg(2, debug, "invalid pitch alignment\n");
668 goto exit;
670 /* adjust to next 8 byte boundary */
671 hpitch = (((hpitch + 7) / 8) * 8);
673 /* if update is set, modify the bytesperline and sizeimage */
674 if (update) {
675 pixfmt->bytesperline = hpitch;
676 pixfmt->sizeimage = hpitch * vpitch * 2;
679 * Image width and height is always based on current standard width and
680 * height
682 pixfmt->width = common->fmt.fmt.pix.width;
683 pixfmt->height = common->fmt.fmt.pix.height;
684 return 0;
685 exit:
686 return ret;
690 * vpif_config_addr() - function to configure buffer address in vpif
691 * @ch - channel ptr
692 * @muxmode - channel mux mode
694 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
696 struct common_obj *common;
698 vpif_dbg(2, debug, "vpif_config_addr\n");
700 common = &(ch->common[VPIF_VIDEO_INDEX]);
702 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
703 common->set_addr = ch1_set_videobuf_addr;
704 else if (2 == muxmode)
705 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
706 else
707 common->set_addr = ch0_set_videobuf_addr;
711 * vpfe_mmap : It is used to map kernel space buffers into user spaces
712 * @filep: file pointer
713 * @vma: ptr to vm_area_struct
715 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
717 /* Get the channel object and file handle object */
718 struct vpif_fh *fh = filep->private_data;
719 struct channel_obj *ch = fh->channel;
720 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
722 vpif_dbg(2, debug, "vpif_mmap\n");
724 return videobuf_mmap_mapper(&common->buffer_queue, vma);
728 * vpif_poll: It is used for select/poll system call
729 * @filep: file pointer
730 * @wait: poll table to wait
732 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
734 int err = 0;
735 struct vpif_fh *fh = filep->private_data;
736 struct channel_obj *channel = fh->channel;
737 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
739 vpif_dbg(2, debug, "vpif_poll\n");
741 if (common->started)
742 err = videobuf_poll_stream(filep, &common->buffer_queue, wait);
744 return 0;
748 * vpif_open : vpif open handler
749 * @filep: file ptr
751 * It creates object of file handle structure and stores it in private_data
752 * member of filepointer
754 static int vpif_open(struct file *filep)
756 struct vpif_capture_config *config = vpif_dev->platform_data;
757 struct video_device *vdev = video_devdata(filep);
758 struct common_obj *common;
759 struct video_obj *vid_ch;
760 struct channel_obj *ch;
761 struct vpif_fh *fh;
762 int i, ret = 0;
764 vpif_dbg(2, debug, "vpif_open\n");
766 ch = video_get_drvdata(vdev);
768 vid_ch = &ch->video;
769 common = &ch->common[VPIF_VIDEO_INDEX];
771 if (mutex_lock_interruptible(&common->lock))
772 return -ERESTARTSYS;
774 if (NULL == ch->curr_subdev_info) {
776 * search through the sub device to see a registered
777 * sub device and make it as current sub device
779 for (i = 0; i < config->subdev_count; i++) {
780 if (vpif_obj.sd[i]) {
781 /* the sub device is registered */
782 ch->curr_subdev_info = &config->subdev_info[i];
783 /* make first input as the current input */
784 vid_ch->input_idx = 0;
785 break;
788 if (i == config->subdev_count) {
789 vpif_err("No sub device registered\n");
790 ret = -ENOENT;
791 goto exit;
795 /* Allocate memory for the file handle object */
796 fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
797 if (NULL == fh) {
798 vpif_err("unable to allocate memory for file handle object\n");
799 ret = -ENOMEM;
800 goto exit;
803 /* store pointer to fh in private_data member of filep */
804 filep->private_data = fh;
805 fh->channel = ch;
806 fh->initialized = 0;
807 /* If decoder is not initialized. initialize it */
808 if (!ch->initialized) {
809 fh->initialized = 1;
810 ch->initialized = 1;
811 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
813 /* Increment channel usrs counter */
814 ch->usrs++;
815 /* Set io_allowed member to false */
816 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
817 /* Initialize priority of this instance to default priority */
818 fh->prio = V4L2_PRIORITY_UNSET;
819 v4l2_prio_open(&ch->prio, &fh->prio);
820 exit:
821 mutex_unlock(&common->lock);
822 return ret;
826 * vpif_release : function to clean up file close
827 * @filep: file pointer
829 * This function deletes buffer queue, frees the buffers and the vpfe file
830 * handle
832 static int vpif_release(struct file *filep)
834 struct vpif_fh *fh = filep->private_data;
835 struct channel_obj *ch = fh->channel;
836 struct common_obj *common;
838 vpif_dbg(2, debug, "vpif_release\n");
840 common = &ch->common[VPIF_VIDEO_INDEX];
842 if (mutex_lock_interruptible(&common->lock))
843 return -ERESTARTSYS;
845 /* if this instance is doing IO */
846 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
847 /* Reset io_usrs member of channel object */
848 common->io_usrs = 0;
849 /* Disable channel as per its device type and channel id */
850 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
851 enable_channel0(0);
852 channel0_intr_enable(0);
854 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
855 (2 == common->started)) {
856 enable_channel1(0);
857 channel1_intr_enable(0);
859 common->started = 0;
860 /* Free buffers allocated */
861 videobuf_queue_cancel(&common->buffer_queue);
862 videobuf_mmap_free(&common->buffer_queue);
865 /* Decrement channel usrs counter */
866 ch->usrs--;
868 /* unlock mutex on channel object */
869 mutex_unlock(&common->lock);
871 /* Close the priority */
872 v4l2_prio_close(&ch->prio, fh->prio);
874 if (fh->initialized)
875 ch->initialized = 0;
877 filep->private_data = NULL;
878 kfree(fh);
879 return 0;
883 * vpif_reqbufs() - request buffer handler
884 * @file: file ptr
885 * @priv: file handle
886 * @reqbuf: request buffer structure ptr
888 static int vpif_reqbufs(struct file *file, void *priv,
889 struct v4l2_requestbuffers *reqbuf)
891 struct vpif_fh *fh = priv;
892 struct channel_obj *ch = fh->channel;
893 struct common_obj *common;
894 u8 index = 0;
895 int ret = 0;
897 vpif_dbg(2, debug, "vpif_reqbufs\n");
900 * This file handle has not initialized the channel,
901 * It is not allowed to do settings
903 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
904 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
905 if (!fh->initialized) {
906 vpif_dbg(1, debug, "Channel Busy\n");
907 return -EBUSY;
911 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
912 return -EINVAL;
914 index = VPIF_VIDEO_INDEX;
916 common = &ch->common[index];
918 if (mutex_lock_interruptible(&common->lock))
919 return -ERESTARTSYS;
921 if (0 != common->io_usrs) {
922 ret = -EBUSY;
923 goto reqbuf_exit;
926 /* Initialize videobuf queue as per the buffer type */
927 videobuf_queue_dma_contig_init(&common->buffer_queue,
928 &video_qops, NULL,
929 &common->irqlock,
930 reqbuf->type,
931 common->fmt.fmt.pix.field,
932 sizeof(struct videobuf_buffer), fh);
934 /* Set io allowed member of file handle to TRUE */
935 fh->io_allowed[index] = 1;
936 /* Increment io usrs member of channel object to 1 */
937 common->io_usrs = 1;
938 /* Store type of memory requested in channel object */
939 common->memory = reqbuf->memory;
940 INIT_LIST_HEAD(&common->dma_queue);
942 /* Allocate buffers */
943 ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
945 reqbuf_exit:
946 mutex_unlock(&common->lock);
947 return ret;
951 * vpif_querybuf() - query buffer handler
952 * @file: file ptr
953 * @priv: file handle
954 * @buf: v4l2 buffer structure ptr
956 static int vpif_querybuf(struct file *file, void *priv,
957 struct v4l2_buffer *buf)
959 struct vpif_fh *fh = priv;
960 struct channel_obj *ch = fh->channel;
961 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
963 vpif_dbg(2, debug, "vpif_querybuf\n");
965 if (common->fmt.type != buf->type)
966 return -EINVAL;
968 if (common->memory != V4L2_MEMORY_MMAP) {
969 vpif_dbg(1, debug, "Invalid memory\n");
970 return -EINVAL;
973 return videobuf_querybuf(&common->buffer_queue, buf);
977 * vpif_qbuf() - query buffer handler
978 * @file: file ptr
979 * @priv: file handle
980 * @buf: v4l2 buffer structure ptr
982 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
985 struct vpif_fh *fh = priv;
986 struct channel_obj *ch = fh->channel;
987 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
988 struct v4l2_buffer tbuf = *buf;
989 struct videobuf_buffer *buf1;
990 unsigned long addr = 0;
991 unsigned long flags;
992 int ret = 0;
994 vpif_dbg(2, debug, "vpif_qbuf\n");
996 if (common->fmt.type != tbuf.type) {
997 vpif_err("invalid buffer type\n");
998 return -EINVAL;
1001 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1002 vpif_err("fh io not allowed \n");
1003 return -EACCES;
1006 if (!(list_empty(&common->dma_queue)) ||
1007 (common->cur_frm != common->next_frm) ||
1008 !common->started ||
1009 (common->started && (0 == ch->field_id)))
1010 return videobuf_qbuf(&common->buffer_queue, buf);
1012 /* bufferqueue is empty store buffer address in VPIF registers */
1013 mutex_lock(&common->buffer_queue.vb_lock);
1014 buf1 = common->buffer_queue.bufs[tbuf.index];
1016 if ((buf1->state == VIDEOBUF_QUEUED) ||
1017 (buf1->state == VIDEOBUF_ACTIVE)) {
1018 vpif_err("invalid state\n");
1019 goto qbuf_exit;
1022 switch (buf1->memory) {
1023 case V4L2_MEMORY_MMAP:
1024 if (buf1->baddr == 0)
1025 goto qbuf_exit;
1026 break;
1028 case V4L2_MEMORY_USERPTR:
1029 if (tbuf.length < buf1->bsize)
1030 goto qbuf_exit;
1032 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
1033 && (buf1->baddr != tbuf.m.userptr))
1034 vpif_buffer_release(&common->buffer_queue, buf1);
1035 buf1->baddr = tbuf.m.userptr;
1036 break;
1038 default:
1039 goto qbuf_exit;
1042 local_irq_save(flags);
1043 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1044 common->buffer_queue.field);
1045 if (ret < 0) {
1046 local_irq_restore(flags);
1047 goto qbuf_exit;
1050 buf1->state = VIDEOBUF_ACTIVE;
1052 if (V4L2_MEMORY_USERPTR == common->memory)
1053 addr = buf1->boff;
1054 else
1055 addr = videobuf_to_dma_contig(buf1);
1057 common->next_frm = buf1;
1058 common->set_addr(addr + common->ytop_off,
1059 addr + common->ybtm_off,
1060 addr + common->ctop_off,
1061 addr + common->cbtm_off);
1063 local_irq_restore(flags);
1064 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1065 mutex_unlock(&common->buffer_queue.vb_lock);
1066 return 0;
1068 qbuf_exit:
1069 mutex_unlock(&common->buffer_queue.vb_lock);
1070 return -EINVAL;
1074 * vpif_dqbuf() - query buffer handler
1075 * @file: file ptr
1076 * @priv: file handle
1077 * @buf: v4l2 buffer structure ptr
1079 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1081 struct vpif_fh *fh = priv;
1082 struct channel_obj *ch = fh->channel;
1083 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1085 vpif_dbg(2, debug, "vpif_dqbuf\n");
1087 return videobuf_dqbuf(&common->buffer_queue, buf,
1088 file->f_flags & O_NONBLOCK);
1092 * vpif_streamon() - streamon handler
1093 * @file: file ptr
1094 * @priv: file handle
1095 * @buftype: v4l2 buffer type
1097 static int vpif_streamon(struct file *file, void *priv,
1098 enum v4l2_buf_type buftype)
1101 struct vpif_capture_config *config = vpif_dev->platform_data;
1102 struct vpif_fh *fh = priv;
1103 struct channel_obj *ch = fh->channel;
1104 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1105 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1106 struct vpif_params *vpif;
1107 unsigned long addr = 0;
1108 int ret = 0;
1110 vpif_dbg(2, debug, "vpif_streamon\n");
1112 vpif = &ch->vpifparams;
1114 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1115 vpif_dbg(1, debug, "buffer type not supported\n");
1116 return -EINVAL;
1119 /* If file handle is not allowed IO, return error */
1120 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1121 vpif_dbg(1, debug, "io not allowed\n");
1122 return -EACCES;
1125 /* If Streaming is already started, return error */
1126 if (common->started) {
1127 vpif_dbg(1, debug, "channel->started\n");
1128 return -EBUSY;
1131 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1132 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1133 vpif->std_info.ycmux_mode == 0) ||
1134 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1135 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1136 vpif_dbg(1, debug, "other channel is being used\n");
1137 return -EBUSY;
1140 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1141 if (ret)
1142 return ret;
1144 /* Enable streamon on the sub device */
1145 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1146 s_stream, 1);
1148 if (ret && (ret != -ENOIOCTLCMD)) {
1149 vpif_dbg(1, debug, "stream on failed in subdev\n");
1150 return ret;
1153 /* Call videobuf_streamon to start streaming in videobuf */
1154 ret = videobuf_streamon(&common->buffer_queue);
1155 if (ret) {
1156 vpif_dbg(1, debug, "videobuf_streamon\n");
1157 return ret;
1160 if (mutex_lock_interruptible(&common->lock)) {
1161 ret = -ERESTARTSYS;
1162 goto streamoff_exit;
1165 /* If buffer queue is empty, return error */
1166 if (list_empty(&common->dma_queue)) {
1167 vpif_dbg(1, debug, "buffer queue is empty\n");
1168 ret = -EIO;
1169 goto exit;
1172 /* Get the next frame from the buffer queue */
1173 common->cur_frm = list_entry(common->dma_queue.next,
1174 struct videobuf_buffer, queue);
1175 common->next_frm = common->cur_frm;
1177 /* Remove buffer from the buffer queue */
1178 list_del(&common->cur_frm->queue);
1179 /* Mark state of the current frame to active */
1180 common->cur_frm->state = VIDEOBUF_ACTIVE;
1181 /* Initialize field_id and started member */
1182 ch->field_id = 0;
1183 common->started = 1;
1185 if (V4L2_MEMORY_USERPTR == common->memory)
1186 addr = common->cur_frm->boff;
1187 else
1188 addr = videobuf_to_dma_contig(common->cur_frm);
1190 /* Calculate the offset for Y and C data in the buffer */
1191 vpif_calculate_offsets(ch);
1193 if ((vpif->std_info.frm_fmt &&
1194 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1195 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1196 (!vpif->std_info.frm_fmt &&
1197 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1198 vpif_dbg(1, debug, "conflict in field format and std format\n");
1199 ret = -EINVAL;
1200 goto exit;
1203 /* configure 1 or 2 channel mode */
1204 ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1206 if (ret < 0) {
1207 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1208 goto exit;
1211 /* Call vpif_set_params function to set the parameters and addresses */
1212 ret = vpif_set_video_params(vpif, ch->channel_id);
1214 if (ret < 0) {
1215 vpif_dbg(1, debug, "can't set video params\n");
1216 goto exit;
1219 common->started = ret;
1220 vpif_config_addr(ch, ret);
1222 common->set_addr(addr + common->ytop_off,
1223 addr + common->ybtm_off,
1224 addr + common->ctop_off,
1225 addr + common->cbtm_off);
1228 * Set interrupt for both the fields in VPIF Register enable channel in
1229 * VPIF register
1231 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1232 channel0_intr_assert();
1233 channel0_intr_enable(1);
1234 enable_channel0(1);
1236 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1237 (common->started == 2)) {
1238 channel1_intr_assert();
1239 channel1_intr_enable(1);
1240 enable_channel1(1);
1242 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1243 mutex_unlock(&common->lock);
1244 return ret;
1246 exit:
1247 mutex_unlock(&common->lock);
1248 streamoff_exit:
1249 ret = videobuf_streamoff(&common->buffer_queue);
1250 return ret;
1254 * vpif_streamoff() - streamoff handler
1255 * @file: file ptr
1256 * @priv: file handle
1257 * @buftype: v4l2 buffer type
1259 static int vpif_streamoff(struct file *file, void *priv,
1260 enum v4l2_buf_type buftype)
1263 struct vpif_fh *fh = priv;
1264 struct channel_obj *ch = fh->channel;
1265 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1266 int ret;
1268 vpif_dbg(2, debug, "vpif_streamoff\n");
1270 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1271 vpif_dbg(1, debug, "buffer type not supported\n");
1272 return -EINVAL;
1275 /* If io is allowed for this file handle, return error */
1276 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1277 vpif_dbg(1, debug, "io not allowed\n");
1278 return -EACCES;
1281 /* If streaming is not started, return error */
1282 if (!common->started) {
1283 vpif_dbg(1, debug, "channel->started\n");
1284 return -EINVAL;
1287 if (mutex_lock_interruptible(&common->lock))
1288 return -ERESTARTSYS;
1290 /* disable channel */
1291 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1292 enable_channel0(0);
1293 channel0_intr_enable(0);
1294 } else {
1295 enable_channel1(0);
1296 channel1_intr_enable(0);
1299 common->started = 0;
1301 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1302 s_stream, 0);
1304 if (ret && (ret != -ENOIOCTLCMD))
1305 vpif_dbg(1, debug, "stream off failed in subdev\n");
1307 mutex_unlock(&common->lock);
1309 return videobuf_streamoff(&common->buffer_queue);
1313 * vpif_map_sub_device_to_input() - Maps sub device to input
1314 * @ch - ptr to channel
1315 * @config - ptr to capture configuration
1316 * @input_index - Given input index from application
1317 * @sub_device_index - index into sd table
1319 * lookup the sub device information for a given input index.
1320 * we report all the inputs to application. inputs table also
1321 * has sub device name for the each input
1323 static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1324 struct channel_obj *ch,
1325 struct vpif_capture_config *vpif_cfg,
1326 int input_index,
1327 int *sub_device_index)
1329 struct vpif_capture_chan_config *chan_cfg;
1330 struct vpif_subdev_info *subdev_info = NULL;
1331 const char *subdev_name = NULL;
1332 int i;
1334 vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1336 chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1339 * search through the inputs to find the sub device supporting
1340 * the input
1342 for (i = 0; i < chan_cfg->input_count; i++) {
1343 /* For each sub device, loop through input */
1344 if (i == input_index) {
1345 subdev_name = chan_cfg->inputs[i].subdev_name;
1346 break;
1350 /* if reached maximum. return null */
1351 if (i == chan_cfg->input_count || (NULL == subdev_name))
1352 return subdev_info;
1354 /* loop through the sub device list to get the sub device info */
1355 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1356 subdev_info = &vpif_cfg->subdev_info[i];
1357 if (!strcmp(subdev_info->name, subdev_name))
1358 break;
1361 if (i == vpif_cfg->subdev_count)
1362 return subdev_info;
1364 /* check if the sub device is registered */
1365 if (NULL == vpif_obj.sd[i])
1366 return NULL;
1368 *sub_device_index = i;
1369 return subdev_info;
1373 * vpif_querystd() - querystd handler
1374 * @file: file ptr
1375 * @priv: file handle
1376 * @std_id: ptr to std id
1378 * This function is called to detect standard at the selected input
1380 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1382 struct vpif_fh *fh = priv;
1383 struct channel_obj *ch = fh->channel;
1384 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1385 int ret = 0;
1387 vpif_dbg(2, debug, "vpif_querystd\n");
1389 if (mutex_lock_interruptible(&common->lock))
1390 return -ERESTARTSYS;
1392 /* Call querystd function of decoder device */
1393 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1394 querystd, std_id);
1395 if (ret < 0)
1396 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1398 mutex_unlock(&common->lock);
1399 return ret;
1403 * vpif_g_std() - get STD handler
1404 * @file: file ptr
1405 * @priv: file handle
1406 * @std_id: ptr to std id
1408 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1410 struct vpif_fh *fh = priv;
1411 struct channel_obj *ch = fh->channel;
1413 vpif_dbg(2, debug, "vpif_g_std\n");
1415 *std = ch->video.stdid;
1416 return 0;
1420 * vpif_s_std() - set STD handler
1421 * @file: file ptr
1422 * @priv: file handle
1423 * @std_id: ptr to std id
1425 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1427 struct vpif_fh *fh = priv;
1428 struct channel_obj *ch = fh->channel;
1429 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1430 int ret = 0;
1432 vpif_dbg(2, debug, "vpif_s_std\n");
1434 if (common->started) {
1435 vpif_err("streaming in progress\n");
1436 return -EBUSY;
1439 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1440 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1441 if (!fh->initialized) {
1442 vpif_dbg(1, debug, "Channel Busy\n");
1443 return -EBUSY;
1447 ret = v4l2_prio_check(&ch->prio, fh->prio);
1448 if (0 != ret)
1449 return ret;
1451 fh->initialized = 1;
1453 /* Call encoder subdevice function to set the standard */
1454 if (mutex_lock_interruptible(&common->lock))
1455 return -ERESTARTSYS;
1457 ch->video.stdid = *std_id;
1459 /* Get the information about the standard */
1460 if (vpif_update_std_info(ch)) {
1461 ret = -EINVAL;
1462 vpif_err("Error getting the standard info\n");
1463 goto s_std_exit;
1466 /* Configure the default format information */
1467 vpif_config_format(ch);
1469 /* set standard in the sub device */
1470 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1471 s_std, *std_id);
1472 if (ret < 0)
1473 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1475 s_std_exit:
1476 mutex_unlock(&common->lock);
1477 return ret;
1481 * vpif_enum_input() - ENUMINPUT handler
1482 * @file: file ptr
1483 * @priv: file handle
1484 * @input: ptr to input structure
1486 static int vpif_enum_input(struct file *file, void *priv,
1487 struct v4l2_input *input)
1490 struct vpif_capture_config *config = vpif_dev->platform_data;
1491 struct vpif_capture_chan_config *chan_cfg;
1492 struct vpif_fh *fh = priv;
1493 struct channel_obj *ch = fh->channel;
1495 chan_cfg = &config->chan_config[ch->channel_id];
1497 if (input->index >= chan_cfg->input_count) {
1498 vpif_dbg(1, debug, "Invalid input index\n");
1499 return -EINVAL;
1502 memcpy(input, &chan_cfg->inputs[input->index].input,
1503 sizeof(*input));
1504 return 0;
1508 * vpif_g_input() - Get INPUT handler
1509 * @file: file ptr
1510 * @priv: file handle
1511 * @index: ptr to input index
1513 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1515 struct vpif_fh *fh = priv;
1516 struct channel_obj *ch = fh->channel;
1517 struct video_obj *vid_ch = &ch->video;
1519 *index = vid_ch->input_idx;
1521 return 0;
1525 * vpif_s_input() - Set INPUT handler
1526 * @file: file ptr
1527 * @priv: file handle
1528 * @index: input index
1530 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1532 struct vpif_capture_config *config = vpif_dev->platform_data;
1533 struct vpif_capture_chan_config *chan_cfg;
1534 struct vpif_fh *fh = priv;
1535 struct channel_obj *ch = fh->channel;
1536 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1537 struct video_obj *vid_ch = &ch->video;
1538 struct vpif_subdev_info *subdev_info;
1539 int ret = 0, sd_index = 0;
1540 u32 input = 0, output = 0;
1542 chan_cfg = &config->chan_config[ch->channel_id];
1544 if (common->started) {
1545 vpif_err("Streaming in progress\n");
1546 return -EBUSY;
1549 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1550 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1551 if (!fh->initialized) {
1552 vpif_dbg(1, debug, "Channel Busy\n");
1553 return -EBUSY;
1557 ret = v4l2_prio_check(&ch->prio, fh->prio);
1558 if (0 != ret)
1559 return ret;
1561 fh->initialized = 1;
1562 subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1563 &sd_index);
1564 if (NULL == subdev_info) {
1565 vpif_dbg(1, debug,
1566 "couldn't lookup sub device for the input index\n");
1567 return -EINVAL;
1570 if (mutex_lock_interruptible(&common->lock))
1571 return -ERESTARTSYS;
1573 /* first setup input path from sub device to vpif */
1574 if (config->setup_input_path) {
1575 ret = config->setup_input_path(ch->channel_id,
1576 subdev_info->name);
1577 if (ret < 0) {
1578 vpif_dbg(1, debug, "couldn't setup input path for the"
1579 " sub device %s, for input index %d\n",
1580 subdev_info->name, index);
1581 goto exit;
1585 if (subdev_info->can_route) {
1586 input = subdev_info->input;
1587 output = subdev_info->output;
1588 ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1589 input, output, 0);
1590 if (ret < 0) {
1591 vpif_dbg(1, debug, "Failed to set input\n");
1592 goto exit;
1595 vid_ch->input_idx = index;
1596 ch->curr_subdev_info = subdev_info;
1597 ch->curr_sd_index = sd_index;
1598 /* copy interface parameters to vpif */
1599 ch->vpifparams.iface = subdev_info->vpif_if;
1601 /* update tvnorms from the sub device input info */
1602 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1604 exit:
1605 mutex_unlock(&common->lock);
1606 return ret;
1610 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1611 * @file: file ptr
1612 * @priv: file handle
1613 * @index: input index
1615 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1616 struct v4l2_fmtdesc *fmt)
1618 struct vpif_fh *fh = priv;
1619 struct channel_obj *ch = fh->channel;
1621 if (fmt->index != 0) {
1622 vpif_dbg(1, debug, "Invalid format index\n");
1623 return -EINVAL;
1626 /* Fill in the information about format */
1627 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1628 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1629 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1630 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1631 } else {
1632 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1633 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1634 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1636 return 0;
1640 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1641 * @file: file ptr
1642 * @priv: file handle
1643 * @fmt: ptr to v4l2 format structure
1645 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1646 struct v4l2_format *fmt)
1648 struct vpif_fh *fh = priv;
1649 struct channel_obj *ch = fh->channel;
1650 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1652 return vpif_check_format(ch, pixfmt, 1);
1657 * vpif_g_fmt_vid_cap() - Set INPUT handler
1658 * @file: file ptr
1659 * @priv: file handle
1660 * @fmt: ptr to v4l2 format structure
1662 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1663 struct v4l2_format *fmt)
1665 struct vpif_fh *fh = priv;
1666 struct channel_obj *ch = fh->channel;
1667 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1669 /* Check the validity of the buffer type */
1670 if (common->fmt.type != fmt->type)
1671 return -EINVAL;
1673 /* Fill in the information about format */
1674 if (mutex_lock_interruptible(&common->lock))
1675 return -ERESTARTSYS;
1677 *fmt = common->fmt;
1678 mutex_unlock(&common->lock);
1679 return 0;
1683 * vpif_s_fmt_vid_cap() - Set FMT handler
1684 * @file: file ptr
1685 * @priv: file handle
1686 * @fmt: ptr to v4l2 format structure
1688 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1689 struct v4l2_format *fmt)
1691 struct vpif_fh *fh = priv;
1692 struct channel_obj *ch = fh->channel;
1693 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1694 struct v4l2_pix_format *pixfmt;
1695 int ret = 0;
1697 vpif_dbg(2, debug, "VIDIOC_S_FMT\n");
1699 /* If streaming is started, return error */
1700 if (common->started) {
1701 vpif_dbg(1, debug, "Streaming is started\n");
1702 return -EBUSY;
1705 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1706 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1707 if (!fh->initialized) {
1708 vpif_dbg(1, debug, "Channel Busy\n");
1709 return -EBUSY;
1713 ret = v4l2_prio_check(&ch->prio, fh->prio);
1714 if (0 != ret)
1715 return ret;
1717 fh->initialized = 1;
1719 pixfmt = &fmt->fmt.pix;
1720 /* Check for valid field format */
1721 ret = vpif_check_format(ch, pixfmt, 0);
1723 if (ret)
1724 return ret;
1725 /* store the format in the channel object */
1726 if (mutex_lock_interruptible(&common->lock))
1727 return -ERESTARTSYS;
1729 common->fmt = *fmt;
1730 mutex_unlock(&common->lock);
1732 return 0;
1736 * vpif_querycap() - QUERYCAP handler
1737 * @file: file ptr
1738 * @priv: file handle
1739 * @cap: ptr to v4l2_capability structure
1741 static int vpif_querycap(struct file *file, void *priv,
1742 struct v4l2_capability *cap)
1744 struct vpif_capture_config *config = vpif_dev->platform_data;
1746 cap->version = VPIF_CAPTURE_VERSION_CODE;
1747 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1748 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1749 strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info));
1750 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1752 return 0;
1756 * vpif_g_priority() - get priority handler
1757 * @file: file ptr
1758 * @priv: file handle
1759 * @prio: ptr to v4l2_priority structure
1761 static int vpif_g_priority(struct file *file, void *priv,
1762 enum v4l2_priority *prio)
1764 struct vpif_fh *fh = priv;
1765 struct channel_obj *ch = fh->channel;
1767 *prio = v4l2_prio_max(&ch->prio);
1769 return 0;
1773 * vpif_s_priority() - set priority handler
1774 * @file: file ptr
1775 * @priv: file handle
1776 * @prio: ptr to v4l2_priority structure
1778 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1780 struct vpif_fh *fh = priv;
1781 struct channel_obj *ch = fh->channel;
1783 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1787 * vpif_cropcap() - cropcap handler
1788 * @file: file ptr
1789 * @priv: file handle
1790 * @crop: ptr to v4l2_cropcap structure
1792 static int vpif_cropcap(struct file *file, void *priv,
1793 struct v4l2_cropcap *crop)
1795 struct vpif_fh *fh = priv;
1796 struct channel_obj *ch = fh->channel;
1797 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1799 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1800 return -EINVAL;
1802 crop->bounds.left = 0;
1803 crop->bounds.top = 0;
1804 crop->bounds.height = common->height;
1805 crop->bounds.width = common->width;
1806 crop->defrect = crop->bounds;
1807 return 0;
1810 /* vpif capture ioctl operations */
1811 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1812 .vidioc_querycap = vpif_querycap,
1813 .vidioc_g_priority = vpif_g_priority,
1814 .vidioc_s_priority = vpif_s_priority,
1815 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1816 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1817 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1818 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1819 .vidioc_enum_input = vpif_enum_input,
1820 .vidioc_s_input = vpif_s_input,
1821 .vidioc_g_input = vpif_g_input,
1822 .vidioc_reqbufs = vpif_reqbufs,
1823 .vidioc_querybuf = vpif_querybuf,
1824 .vidioc_querystd = vpif_querystd,
1825 .vidioc_s_std = vpif_s_std,
1826 .vidioc_g_std = vpif_g_std,
1827 .vidioc_qbuf = vpif_qbuf,
1828 .vidioc_dqbuf = vpif_dqbuf,
1829 .vidioc_streamon = vpif_streamon,
1830 .vidioc_streamoff = vpif_streamoff,
1831 .vidioc_cropcap = vpif_cropcap,
1834 /* vpif file operations */
1835 static struct v4l2_file_operations vpif_fops = {
1836 .owner = THIS_MODULE,
1837 .open = vpif_open,
1838 .release = vpif_release,
1839 .ioctl = video_ioctl2,
1840 .mmap = vpif_mmap,
1841 .poll = vpif_poll
1844 /* vpif video template */
1845 static struct video_device vpif_video_template = {
1846 .name = "vpif",
1847 .fops = &vpif_fops,
1848 .minor = -1,
1849 .ioctl_ops = &vpif_ioctl_ops,
1853 * initialize_vpif() - Initialize vpif data structures
1855 * Allocate memory for data structures and initialize them
1857 static int initialize_vpif(void)
1859 int err = 0, i, j;
1860 int free_channel_objects_index;
1862 /* Default number of buffers should be 3 */
1863 if ((ch0_numbuffers > 0) &&
1864 (ch0_numbuffers < config_params.min_numbuffers))
1865 ch0_numbuffers = config_params.min_numbuffers;
1866 if ((ch1_numbuffers > 0) &&
1867 (ch1_numbuffers < config_params.min_numbuffers))
1868 ch1_numbuffers = config_params.min_numbuffers;
1870 /* Set buffer size to min buffers size if it is invalid */
1871 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1872 ch0_bufsize =
1873 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1874 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1875 ch1_bufsize =
1876 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1878 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1879 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1880 if (ch0_numbuffers) {
1881 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1882 = ch0_bufsize;
1884 if (ch1_numbuffers) {
1885 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1886 = ch1_bufsize;
1889 /* Allocate memory for six channel objects */
1890 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1891 vpif_obj.dev[i] =
1892 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1893 /* If memory allocation fails, return error */
1894 if (!vpif_obj.dev[i]) {
1895 free_channel_objects_index = i;
1896 err = -ENOMEM;
1897 goto vpif_init_free_channel_objects;
1900 return 0;
1902 vpif_init_free_channel_objects:
1903 for (j = 0; j < free_channel_objects_index; j++)
1904 kfree(vpif_obj.dev[j]);
1905 return err;
1909 * vpif_probe : This function probes the vpif capture driver
1910 * @pdev: platform device pointer
1912 * This creates device entries by register itself to the V4L2 driver and
1913 * initializes fields of each channel objects
1915 static __init int vpif_probe(struct platform_device *pdev)
1917 struct vpif_subdev_info *subdevdata;
1918 struct vpif_capture_config *config;
1919 int i, j, k, m, q, err;
1920 struct i2c_adapter *i2c_adap;
1921 struct channel_obj *ch;
1922 struct common_obj *common;
1923 struct video_device *vfd;
1924 struct resource *res;
1925 int subdev_count;
1927 vpif_dev = &pdev->dev;
1929 err = initialize_vpif();
1930 if (err) {
1931 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1932 return err;
1935 k = 0;
1936 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1937 for (i = res->start; i <= res->end; i++) {
1938 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1939 "DM646x_Capture",
1940 (void *)(&vpif_obj.dev[k]->channel_id))) {
1941 err = -EBUSY;
1942 i--;
1943 goto vpif_int_err;
1946 k++;
1949 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1950 /* Get the pointer to the channel object */
1951 ch = vpif_obj.dev[i];
1952 /* Allocate memory for video device */
1953 vfd = video_device_alloc();
1954 if (NULL == vfd) {
1955 for (j = 0; j < i; j++) {
1956 ch = vpif_obj.dev[j];
1957 video_device_release(ch->video_dev);
1959 err = -ENOMEM;
1960 goto vpif_dev_alloc_err;
1963 /* Initialize field of video device */
1964 *vfd = vpif_video_template;
1965 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1966 vfd->release = video_device_release;
1967 snprintf(vfd->name, sizeof(vfd->name),
1968 "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
1969 (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
1970 (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
1971 (VPIF_CAPTURE_VERSION_CODE) & 0xff);
1972 /* Set video_dev to the video device */
1973 ch->video_dev = vfd;
1976 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1977 ch = vpif_obj.dev[j];
1978 ch->channel_id = j;
1979 common = &(ch->common[VPIF_VIDEO_INDEX]);
1980 spin_lock_init(&common->irqlock);
1981 mutex_init(&common->lock);
1982 /* Initialize prio member of channel object */
1983 v4l2_prio_init(&ch->prio);
1984 err = video_register_device(ch->video_dev,
1985 VFL_TYPE_GRABBER, (j ? 1 : 0));
1986 if (err)
1987 goto probe_out;
1989 video_set_drvdata(ch->video_dev, ch);
1993 i2c_adap = i2c_get_adapter(1);
1994 config = pdev->dev.platform_data;
1996 subdev_count = config->subdev_count;
1997 vpif_obj.sd = kmalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1998 GFP_KERNEL);
1999 if (vpif_obj.sd == NULL) {
2000 vpif_err("unable to allocate memory for subdevice pointers\n");
2001 err = -ENOMEM;
2002 goto probe_out;
2005 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2006 if (err) {
2007 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2008 goto probe_subdev_out;
2011 for (i = 0; i < subdev_count; i++) {
2012 subdevdata = &config->subdev_info[i];
2013 vpif_obj.sd[i] =
2014 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2015 i2c_adap,
2016 subdevdata->name,
2017 &subdevdata->board_info,
2018 NULL);
2020 if (!vpif_obj.sd[i]) {
2021 vpif_err("Error registering v4l2 subdevice\n");
2022 goto probe_subdev_out;
2024 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2025 subdevdata->name);
2027 if (vpif_obj.sd[i])
2028 vpif_obj.sd[i]->grp_id = 1 << i;
2030 v4l2_info(&vpif_obj.v4l2_dev, "DM646x VPIF Capture driver"
2031 " initialized\n");
2033 return 0;
2035 probe_subdev_out:
2036 /* free sub devices memory */
2037 kfree(vpif_obj.sd);
2039 j = VPIF_CAPTURE_MAX_DEVICES;
2040 probe_out:
2041 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2042 for (k = 0; k < j; k++) {
2043 /* Get the pointer to the channel object */
2044 ch = vpif_obj.dev[k];
2045 /* Unregister video device */
2046 video_unregister_device(ch->video_dev);
2049 vpif_dev_alloc_err:
2050 k = VPIF_CAPTURE_MAX_DEVICES-1;
2051 res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2052 i = res->end;
2054 vpif_int_err:
2055 for (q = k; q >= 0; q--) {
2056 for (m = i; m >= (int)res->start; m--)
2057 free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2059 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2060 if (res)
2061 i = res->end;
2063 return err;
2067 * vpif_remove() - driver remove handler
2068 * @device: ptr to platform device structure
2070 * The vidoe device is unregistered
2072 static int vpif_remove(struct platform_device *device)
2074 int i;
2075 struct channel_obj *ch;
2077 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2079 /* un-register device */
2080 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2081 /* Get the pointer to the channel object */
2082 ch = vpif_obj.dev[i];
2083 /* Unregister video device */
2084 video_unregister_device(ch->video_dev);
2086 return 0;
2090 * vpif_suspend: vpif device suspend
2092 * TODO: Add suspend code here
2094 static int
2095 vpif_suspend(struct device *dev)
2097 return -1;
2101 * vpif_resume: vpif device suspend
2103 * TODO: Add resume code here
2105 static int
2106 vpif_resume(struct device *dev)
2108 return -1;
2111 static const struct dev_pm_ops vpif_dev_pm_ops = {
2112 .suspend = vpif_suspend,
2113 .resume = vpif_resume,
2116 static struct platform_driver vpif_driver = {
2117 .driver = {
2118 .name = "vpif_capture",
2119 .owner = THIS_MODULE,
2120 .pm = &vpif_dev_pm_ops,
2122 .probe = vpif_probe,
2123 .remove = vpif_remove,
2127 * vpif_init: initialize the vpif driver
2129 * This function registers device and driver to the kernel, requests irq
2130 * handler and allocates memory
2131 * for channel objects
2133 static __init int vpif_init(void)
2135 return platform_driver_register(&vpif_driver);
2139 * vpif_cleanup : This function clean up the vpif capture resources
2141 * This will un-registers device and driver to the kernel, frees
2142 * requested irq handler and de-allocates memory allocated for channel
2143 * objects.
2145 static void vpif_cleanup(void)
2147 struct platform_device *pdev;
2148 struct resource *res;
2149 int irq_num;
2150 int i = 0;
2152 pdev = container_of(vpif_dev, struct platform_device, dev);
2153 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2154 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2155 free_irq(irq_num,
2156 (void *)(&vpif_obj.dev[i]->channel_id));
2157 i++;
2160 platform_driver_unregister(&vpif_driver);
2162 kfree(vpif_obj.sd);
2163 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2164 kfree(vpif_obj.dev[i]);
2167 /* Function for module initialization and cleanup */
2168 module_init(vpif_init);
2169 module_exit(vpif_cleanup);