Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / media / video / davinci / vpif_capture.c
blob6504e40a31dd2fb27277253ae7166ff3da64decb
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/slab.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
41 #include "vpif_capture.h"
42 #include "vpif.h"
44 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(VPIF_CAPTURE_VERSION);
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)
52 static int debug = 1;
53 static u32 ch0_numbuffers = 3;
54 static u32 ch1_numbuffers = 3;
55 static u32 ch0_bufsize = 1920 * 1080 * 2;
56 static u32 ch1_bufsize = 720 * 576 * 2;
58 module_param(debug, int, 0644);
59 module_param(ch0_numbuffers, uint, S_IRUGO);
60 module_param(ch1_numbuffers, uint, S_IRUGO);
61 module_param(ch0_bufsize, uint, S_IRUGO);
62 module_param(ch1_bufsize, uint, S_IRUGO);
64 MODULE_PARM_DESC(debug, "Debug level 0-1");
65 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
66 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
67 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
68 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
70 static 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,
80 /* global variables */
81 static struct vpif_device vpif_obj = { {NULL} };
82 static struct device *vpif_dev;
84 /**
85 * vpif_uservirt_to_phys : translate user/virtual address to phy address
86 * @virtp: user/virtual address
88 * This inline function is used to convert user space virtual address to
89 * physical address.
91 static inline u32 vpif_uservirt_to_phys(u32 virtp)
93 unsigned long physp = 0;
94 struct mm_struct *mm = current->mm;
95 struct vm_area_struct *vma;
97 vma = find_vma(mm, virtp);
99 /* For kernel direct-mapped memory, take the easy way */
100 if (virtp >= PAGE_OFFSET)
101 physp = virt_to_phys((void *)virtp);
102 else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
104 * this will catch, kernel-allocated, mmaped-to-usermode
105 * addresses
107 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
108 else {
109 /* otherwise, use get_user_pages() for general userland pages */
110 int res, nr_pages = 1;
111 struct page *pages;
113 down_read(&current->mm->mmap_sem);
115 res = get_user_pages(current, current->mm,
116 virtp, nr_pages, 1, 0, &pages, NULL);
117 up_read(&current->mm->mmap_sem);
119 if (res == nr_pages)
120 physp = __pa(page_address(&pages[0]) +
121 (virtp & ~PAGE_MASK));
122 else {
123 vpif_err("get_user_pages failed\n");
124 return 0;
127 return physp;
131 * buffer_prepare : callback function for buffer prepare
132 * @q : buffer queue ptr
133 * @vb: ptr to video buffer
134 * @field: field info
136 * This is the callback function for buffer prepare when videobuf_qbuf()
137 * function is called. The buffer is prepared and user space virtual address
138 * or user address is converted into physical address
140 static int vpif_buffer_prepare(struct videobuf_queue *q,
141 struct videobuf_buffer *vb,
142 enum v4l2_field field)
144 /* Get the file handle object and channel object */
145 struct vpif_fh *fh = q->priv_data;
146 struct channel_obj *ch = fh->channel;
147 struct common_obj *common;
148 unsigned long addr;
151 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
153 common = &ch->common[VPIF_VIDEO_INDEX];
155 /* If buffer is not initialized, initialize it */
156 if (VIDEOBUF_NEEDS_INIT == vb->state) {
157 vb->width = common->width;
158 vb->height = common->height;
159 vb->size = vb->width * vb->height;
160 vb->field = field;
162 vb->state = VIDEOBUF_PREPARED;
164 * if user pointer memory mechanism is used, get the physical
165 * address of the buffer
167 if (V4L2_MEMORY_USERPTR == common->memory) {
168 if (0 == vb->baddr) {
169 vpif_dbg(1, debug, "buffer address is 0\n");
170 return -EINVAL;
173 vb->boff = vpif_uservirt_to_phys(vb->baddr);
174 if (!IS_ALIGNED(vb->boff, 8))
175 goto exit;
178 addr = vb->boff;
179 if (q->streaming) {
180 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
181 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
182 !IS_ALIGNED((addr + common->ctop_off), 8) ||
183 !IS_ALIGNED((addr + common->cbtm_off), 8))
184 goto exit;
186 return 0;
187 exit:
188 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
189 return -EINVAL;
193 * vpif_buffer_setup : Callback function for buffer setup.
194 * @q: buffer queue ptr
195 * @count: number of buffers
196 * @size: size of the buffer
198 * This callback function is called when reqbuf() is called to adjust
199 * the buffer count and buffer size
201 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
202 unsigned int *size)
204 /* Get the file handle object and channel object */
205 struct vpif_fh *fh = q->priv_data;
206 struct channel_obj *ch = fh->channel;
207 struct common_obj *common;
209 common = &ch->common[VPIF_VIDEO_INDEX];
211 vpif_dbg(2, debug, "vpif_buffer_setup\n");
213 /* If memory type is not mmap, return */
214 if (V4L2_MEMORY_MMAP != common->memory)
215 return 0;
217 /* Calculate the size of the buffer */
218 *size = config_params.channel_bufsize[ch->channel_id];
220 if (*count < config_params.min_numbuffers)
221 *count = config_params.min_numbuffers;
222 return 0;
226 * vpif_buffer_queue : Callback function to add buffer to DMA queue
227 * @q: ptr to videobuf_queue
228 * @vb: ptr to videobuf_buffer
230 static void vpif_buffer_queue(struct videobuf_queue *q,
231 struct videobuf_buffer *vb)
233 /* Get the file handle object and channel object */
234 struct vpif_fh *fh = q->priv_data;
235 struct channel_obj *ch = fh->channel;
236 struct common_obj *common;
238 common = &ch->common[VPIF_VIDEO_INDEX];
240 vpif_dbg(2, debug, "vpif_buffer_queue\n");
242 /* add the buffer to the DMA queue */
243 list_add_tail(&vb->queue, &common->dma_queue);
244 /* Change state of the buffer */
245 vb->state = VIDEOBUF_QUEUED;
249 * vpif_buffer_release : Callback function to free buffer
250 * @q: buffer queue ptr
251 * @vb: ptr to video buffer
253 * This function is called from the videobuf layer to free memory
254 * allocated to the buffers
256 static void vpif_buffer_release(struct videobuf_queue *q,
257 struct videobuf_buffer *vb)
259 /* Get the file handle object and channel object */
260 struct vpif_fh *fh = q->priv_data;
261 struct channel_obj *ch = fh->channel;
262 struct common_obj *common;
264 common = &ch->common[VPIF_VIDEO_INDEX];
266 videobuf_dma_contig_free(q, vb);
267 vb->state = VIDEOBUF_NEEDS_INIT;
270 static struct videobuf_queue_ops video_qops = {
271 .buf_setup = vpif_buffer_setup,
272 .buf_prepare = vpif_buffer_prepare,
273 .buf_queue = vpif_buffer_queue,
274 .buf_release = vpif_buffer_release,
277 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
278 { {1, 1} };
281 * vpif_process_buffer_complete: process a completed buffer
282 * @common: ptr to common channel object
284 * This function time stamp the buffer and mark it as DONE. It also
285 * wake up any process waiting on the QUEUE and set the next buffer
286 * as current
288 static void vpif_process_buffer_complete(struct common_obj *common)
290 do_gettimeofday(&common->cur_frm->ts);
291 common->cur_frm->state = VIDEOBUF_DONE;
292 wake_up_interruptible(&common->cur_frm->done);
293 /* Make curFrm pointing to nextFrm */
294 common->cur_frm = common->next_frm;
298 * vpif_schedule_next_buffer: set next buffer address for capture
299 * @common : ptr to common channel object
301 * This function will get next buffer from the dma queue and
302 * set the buffer address in the vpif register for capture.
303 * the buffer is marked active
305 static void vpif_schedule_next_buffer(struct common_obj *common)
307 unsigned long addr = 0;
309 common->next_frm = list_entry(common->dma_queue.next,
310 struct videobuf_buffer, queue);
311 /* Remove that buffer from the buffer queue */
312 list_del(&common->next_frm->queue);
313 common->next_frm->state = VIDEOBUF_ACTIVE;
314 if (V4L2_MEMORY_USERPTR == common->memory)
315 addr = common->next_frm->boff;
316 else
317 addr = videobuf_to_dma_contig(common->next_frm);
319 /* Set top and bottom field addresses in VPIF registers */
320 common->set_addr(addr + common->ytop_off,
321 addr + common->ybtm_off,
322 addr + common->ctop_off,
323 addr + common->cbtm_off);
327 * vpif_channel_isr : ISR handler for vpif capture
328 * @irq: irq number
329 * @dev_id: dev_id ptr
331 * It changes status of the captured buffer, takes next buffer from the queue
332 * and sets its address in VPIF registers
334 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
336 struct vpif_device *dev = &vpif_obj;
337 struct common_obj *common;
338 struct channel_obj *ch;
339 enum v4l2_field field;
340 int channel_id = 0;
341 int fid = -1, i;
343 channel_id = *(int *)(dev_id);
344 ch = dev->dev[channel_id];
346 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
348 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
349 common = &ch->common[i];
350 /* skip If streaming is not started in this channel */
351 if (0 == common->started)
352 continue;
354 /* Check the field format */
355 if (1 == ch->vpifparams.std_info.frm_fmt) {
356 /* Progressive mode */
357 if (list_empty(&common->dma_queue))
358 continue;
360 if (!channel_first_int[i][channel_id])
361 vpif_process_buffer_complete(common);
363 channel_first_int[i][channel_id] = 0;
365 vpif_schedule_next_buffer(common);
368 channel_first_int[i][channel_id] = 0;
369 } else {
371 * Interlaced mode. If it is first interrupt, ignore
372 * it
374 if (channel_first_int[i][channel_id]) {
375 channel_first_int[i][channel_id] = 0;
376 continue;
378 if (0 == i) {
379 ch->field_id ^= 1;
380 /* Get field id from VPIF registers */
381 fid = vpif_channel_getfid(ch->channel_id);
382 if (fid != ch->field_id) {
384 * If field id does not match stored
385 * field id, make them in sync
387 if (0 == fid)
388 ch->field_id = fid;
389 return IRQ_HANDLED;
392 /* device field id and local field id are in sync */
393 if (0 == fid) {
394 /* this is even field */
395 if (common->cur_frm == common->next_frm)
396 continue;
398 /* mark the current buffer as done */
399 vpif_process_buffer_complete(common);
400 } else if (1 == fid) {
401 /* odd field */
402 if (list_empty(&common->dma_queue) ||
403 (common->cur_frm != common->next_frm))
404 continue;
406 vpif_schedule_next_buffer(common);
410 return IRQ_HANDLED;
414 * vpif_update_std_info() - update standard related info
415 * @ch: ptr to channel object
417 * For a given standard selected by application, update values
418 * in the device data structures
420 static int vpif_update_std_info(struct channel_obj *ch)
422 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
423 struct vpif_params *vpifparams = &ch->vpifparams;
424 const struct vpif_channel_config_params *config;
425 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
426 struct video_obj *vid_ch = &ch->video;
427 int index;
429 vpif_dbg(2, debug, "vpif_update_std_info\n");
431 for (index = 0; index < vpif_ch_params_count; index++) {
432 config = &ch_params[index];
433 if (config->hd_sd == 0) {
434 vpif_dbg(2, debug, "SD format\n");
435 if (config->stdid & vid_ch->stdid) {
436 memcpy(std_info, config, sizeof(*config));
437 break;
439 } else {
440 vpif_dbg(2, debug, "HD format\n");
441 if (config->dv_preset == vid_ch->dv_preset) {
442 memcpy(std_info, config, sizeof(*config));
443 break;
448 /* standard not found */
449 if (index == vpif_ch_params_count)
450 return -EINVAL;
452 common->fmt.fmt.pix.width = std_info->width;
453 common->width = std_info->width;
454 common->fmt.fmt.pix.height = std_info->height;
455 common->height = std_info->height;
456 common->fmt.fmt.pix.bytesperline = std_info->width;
457 vpifparams->video_params.hpitch = std_info->width;
458 vpifparams->video_params.storage_mode = std_info->frm_fmt;
460 return 0;
464 * vpif_calculate_offsets : This function calculates buffers offsets
465 * @ch : ptr to channel object
467 * This function calculates buffer offsets for Y and C in the top and
468 * bottom field
470 static void vpif_calculate_offsets(struct channel_obj *ch)
472 unsigned int hpitch, vpitch, sizeimage;
473 struct video_obj *vid_ch = &(ch->video);
474 struct vpif_params *vpifparams = &ch->vpifparams;
475 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
476 enum v4l2_field field = common->fmt.fmt.pix.field;
478 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
480 if (V4L2_FIELD_ANY == field) {
481 if (vpifparams->std_info.frm_fmt)
482 vid_ch->buf_field = V4L2_FIELD_NONE;
483 else
484 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
485 } else
486 vid_ch->buf_field = common->fmt.fmt.pix.field;
488 if (V4L2_MEMORY_USERPTR == common->memory)
489 sizeimage = common->fmt.fmt.pix.sizeimage;
490 else
491 sizeimage = config_params.channel_bufsize[ch->channel_id];
493 hpitch = common->fmt.fmt.pix.bytesperline;
494 vpitch = sizeimage / (hpitch * 2);
496 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
497 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
498 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
499 common->ytop_off = 0;
500 common->ybtm_off = hpitch;
501 common->ctop_off = sizeimage / 2;
502 common->cbtm_off = sizeimage / 2 + hpitch;
503 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
504 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
505 common->ytop_off = 0;
506 common->ybtm_off = sizeimage / 4;
507 common->ctop_off = sizeimage / 2;
508 common->cbtm_off = common->ctop_off + sizeimage / 4;
509 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
510 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
511 common->ybtm_off = 0;
512 common->ytop_off = sizeimage / 4;
513 common->cbtm_off = sizeimage / 2;
514 common->ctop_off = common->cbtm_off + sizeimage / 4;
516 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
517 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
518 vpifparams->video_params.storage_mode = 1;
519 else
520 vpifparams->video_params.storage_mode = 0;
522 if (1 == vpifparams->std_info.frm_fmt)
523 vpifparams->video_params.hpitch =
524 common->fmt.fmt.pix.bytesperline;
525 else {
526 if ((field == V4L2_FIELD_ANY)
527 || (field == V4L2_FIELD_INTERLACED))
528 vpifparams->video_params.hpitch =
529 common->fmt.fmt.pix.bytesperline * 2;
530 else
531 vpifparams->video_params.hpitch =
532 common->fmt.fmt.pix.bytesperline;
535 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
539 * vpif_config_format: configure default frame format in the device
540 * ch : ptr to channel object
542 static void vpif_config_format(struct channel_obj *ch)
544 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
546 vpif_dbg(2, debug, "vpif_config_format\n");
548 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
549 if (config_params.numbuffers[ch->channel_id] == 0)
550 common->memory = V4L2_MEMORY_USERPTR;
551 else
552 common->memory = V4L2_MEMORY_MMAP;
554 common->fmt.fmt.pix.sizeimage
555 = config_params.channel_bufsize[ch->channel_id];
557 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
558 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
559 else
560 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
561 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
565 * vpif_get_default_field() - Get default field type based on interface
566 * @vpif_params - ptr to vpif params
568 static inline enum v4l2_field vpif_get_default_field(
569 struct vpif_interface *iface)
571 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
572 V4L2_FIELD_INTERLACED;
576 * vpif_check_format() - check given pixel format for compatibility
577 * @ch - channel ptr
578 * @pixfmt - Given pixel format
579 * @update - update the values as per hardware requirement
581 * Check the application pixel format for S_FMT and update the input
582 * values as per hardware limits for TRY_FMT. The default pixel and
583 * field format is selected based on interface type.
585 static int vpif_check_format(struct channel_obj *ch,
586 struct v4l2_pix_format *pixfmt,
587 int update)
589 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
590 struct vpif_params *vpif_params = &ch->vpifparams;
591 enum v4l2_field field = pixfmt->field;
592 u32 sizeimage, hpitch, vpitch;
593 int ret = -EINVAL;
595 vpif_dbg(2, debug, "vpif_check_format\n");
597 * first check for the pixel format. If if_type is Raw bayer,
598 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
599 * V4L2_PIX_FMT_YUV422P is supported
601 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
602 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
603 if (!update) {
604 vpif_dbg(2, debug, "invalid pix format\n");
605 goto exit;
607 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
609 } else {
610 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
611 if (!update) {
612 vpif_dbg(2, debug, "invalid pixel format\n");
613 goto exit;
615 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
619 if (!(VPIF_VALID_FIELD(field))) {
620 if (!update) {
621 vpif_dbg(2, debug, "invalid field format\n");
622 goto exit;
625 * By default use FIELD_NONE for RAW Bayer capture
626 * and FIELD_INTERLACED for other interfaces
628 field = vpif_get_default_field(&vpif_params->iface);
629 } else if (field == V4L2_FIELD_ANY)
630 /* unsupported field. Use default */
631 field = vpif_get_default_field(&vpif_params->iface);
633 /* validate the hpitch */
634 hpitch = pixfmt->bytesperline;
635 if (hpitch < vpif_params->std_info.width) {
636 if (!update) {
637 vpif_dbg(2, debug, "invalid hpitch\n");
638 goto exit;
640 hpitch = vpif_params->std_info.width;
643 if (V4L2_MEMORY_USERPTR == common->memory)
644 sizeimage = pixfmt->sizeimage;
645 else
646 sizeimage = config_params.channel_bufsize[ch->channel_id];
648 vpitch = sizeimage / (hpitch * 2);
650 /* validate the vpitch */
651 if (vpitch < vpif_params->std_info.height) {
652 if (!update) {
653 vpif_dbg(2, debug, "Invalid vpitch\n");
654 goto exit;
656 vpitch = vpif_params->std_info.height;
659 /* Check for 8 byte alignment */
660 if (!ALIGN(hpitch, 8)) {
661 if (!update) {
662 vpif_dbg(2, debug, "invalid pitch alignment\n");
663 goto exit;
665 /* adjust to next 8 byte boundary */
666 hpitch = (((hpitch + 7) / 8) * 8);
668 /* if update is set, modify the bytesperline and sizeimage */
669 if (update) {
670 pixfmt->bytesperline = hpitch;
671 pixfmt->sizeimage = hpitch * vpitch * 2;
674 * Image width and height is always based on current standard width and
675 * height
677 pixfmt->width = common->fmt.fmt.pix.width;
678 pixfmt->height = common->fmt.fmt.pix.height;
679 return 0;
680 exit:
681 return ret;
685 * vpif_config_addr() - function to configure buffer address in vpif
686 * @ch - channel ptr
687 * @muxmode - channel mux mode
689 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
691 struct common_obj *common;
693 vpif_dbg(2, debug, "vpif_config_addr\n");
695 common = &(ch->common[VPIF_VIDEO_INDEX]);
697 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
698 common->set_addr = ch1_set_videobuf_addr;
699 else if (2 == muxmode)
700 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
701 else
702 common->set_addr = ch0_set_videobuf_addr;
706 * vpfe_mmap : It is used to map kernel space buffers into user spaces
707 * @filep: file pointer
708 * @vma: ptr to vm_area_struct
710 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
712 /* Get the channel object and file handle object */
713 struct vpif_fh *fh = filep->private_data;
714 struct channel_obj *ch = fh->channel;
715 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
717 vpif_dbg(2, debug, "vpif_mmap\n");
719 return videobuf_mmap_mapper(&common->buffer_queue, vma);
723 * vpif_poll: It is used for select/poll system call
724 * @filep: file pointer
725 * @wait: poll table to wait
727 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
729 struct vpif_fh *fh = filep->private_data;
730 struct channel_obj *channel = fh->channel;
731 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
733 vpif_dbg(2, debug, "vpif_poll\n");
735 if (common->started)
736 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
737 return 0;
741 * vpif_open : vpif open handler
742 * @filep: file ptr
744 * It creates object of file handle structure and stores it in private_data
745 * member of filepointer
747 static int vpif_open(struct file *filep)
749 struct vpif_capture_config *config = vpif_dev->platform_data;
750 struct video_device *vdev = video_devdata(filep);
751 struct common_obj *common;
752 struct video_obj *vid_ch;
753 struct channel_obj *ch;
754 struct vpif_fh *fh;
755 int i;
757 vpif_dbg(2, debug, "vpif_open\n");
759 ch = video_get_drvdata(vdev);
761 vid_ch = &ch->video;
762 common = &ch->common[VPIF_VIDEO_INDEX];
764 if (NULL == ch->curr_subdev_info) {
766 * search through the sub device to see a registered
767 * sub device and make it as current sub device
769 for (i = 0; i < config->subdev_count; i++) {
770 if (vpif_obj.sd[i]) {
771 /* the sub device is registered */
772 ch->curr_subdev_info = &config->subdev_info[i];
773 /* make first input as the current input */
774 vid_ch->input_idx = 0;
775 break;
778 if (i == config->subdev_count) {
779 vpif_err("No sub device registered\n");
780 return -ENOENT;
784 /* Allocate memory for the file handle object */
785 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
786 if (NULL == fh) {
787 vpif_err("unable to allocate memory for file handle object\n");
788 return -ENOMEM;
791 /* store pointer to fh in private_data member of filep */
792 filep->private_data = fh;
793 fh->channel = ch;
794 fh->initialized = 0;
795 /* If decoder is not initialized. initialize it */
796 if (!ch->initialized) {
797 fh->initialized = 1;
798 ch->initialized = 1;
799 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
801 /* Increment channel usrs counter */
802 ch->usrs++;
803 /* Set io_allowed member to false */
804 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
805 /* Initialize priority of this instance to default priority */
806 fh->prio = V4L2_PRIORITY_UNSET;
807 v4l2_prio_open(&ch->prio, &fh->prio);
808 return 0;
812 * vpif_release : function to clean up file close
813 * @filep: file pointer
815 * This function deletes buffer queue, frees the buffers and the vpfe file
816 * handle
818 static int vpif_release(struct file *filep)
820 struct vpif_fh *fh = filep->private_data;
821 struct channel_obj *ch = fh->channel;
822 struct common_obj *common;
824 vpif_dbg(2, debug, "vpif_release\n");
826 common = &ch->common[VPIF_VIDEO_INDEX];
828 /* if this instance is doing IO */
829 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
830 /* Reset io_usrs member of channel object */
831 common->io_usrs = 0;
832 /* Disable channel as per its device type and channel id */
833 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
834 enable_channel0(0);
835 channel0_intr_enable(0);
837 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
838 (2 == common->started)) {
839 enable_channel1(0);
840 channel1_intr_enable(0);
842 common->started = 0;
843 /* Free buffers allocated */
844 videobuf_queue_cancel(&common->buffer_queue);
845 videobuf_mmap_free(&common->buffer_queue);
848 /* Decrement channel usrs counter */
849 ch->usrs--;
851 /* Close the priority */
852 v4l2_prio_close(&ch->prio, fh->prio);
854 if (fh->initialized)
855 ch->initialized = 0;
857 filep->private_data = NULL;
858 kfree(fh);
859 return 0;
863 * vpif_reqbufs() - request buffer handler
864 * @file: file ptr
865 * @priv: file handle
866 * @reqbuf: request buffer structure ptr
868 static int vpif_reqbufs(struct file *file, void *priv,
869 struct v4l2_requestbuffers *reqbuf)
871 struct vpif_fh *fh = priv;
872 struct channel_obj *ch = fh->channel;
873 struct common_obj *common;
874 u8 index = 0;
876 vpif_dbg(2, debug, "vpif_reqbufs\n");
879 * This file handle has not initialized the channel,
880 * It is not allowed to do settings
882 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
883 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
884 if (!fh->initialized) {
885 vpif_dbg(1, debug, "Channel Busy\n");
886 return -EBUSY;
890 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
891 return -EINVAL;
893 index = VPIF_VIDEO_INDEX;
895 common = &ch->common[index];
897 if (0 != common->io_usrs)
898 return -EBUSY;
900 /* Initialize videobuf queue as per the buffer type */
901 videobuf_queue_dma_contig_init(&common->buffer_queue,
902 &video_qops, NULL,
903 &common->irqlock,
904 reqbuf->type,
905 common->fmt.fmt.pix.field,
906 sizeof(struct videobuf_buffer), fh,
907 &common->lock);
909 /* Set io allowed member of file handle to TRUE */
910 fh->io_allowed[index] = 1;
911 /* Increment io usrs member of channel object to 1 */
912 common->io_usrs = 1;
913 /* Store type of memory requested in channel object */
914 common->memory = reqbuf->memory;
915 INIT_LIST_HEAD(&common->dma_queue);
917 /* Allocate buffers */
918 return videobuf_reqbufs(&common->buffer_queue, reqbuf);
922 * vpif_querybuf() - query buffer handler
923 * @file: file ptr
924 * @priv: file handle
925 * @buf: v4l2 buffer structure ptr
927 static int vpif_querybuf(struct file *file, void *priv,
928 struct v4l2_buffer *buf)
930 struct vpif_fh *fh = priv;
931 struct channel_obj *ch = fh->channel;
932 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
934 vpif_dbg(2, debug, "vpif_querybuf\n");
936 if (common->fmt.type != buf->type)
937 return -EINVAL;
939 if (common->memory != V4L2_MEMORY_MMAP) {
940 vpif_dbg(1, debug, "Invalid memory\n");
941 return -EINVAL;
944 return videobuf_querybuf(&common->buffer_queue, buf);
948 * vpif_qbuf() - query buffer handler
949 * @file: file ptr
950 * @priv: file handle
951 * @buf: v4l2 buffer structure ptr
953 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
956 struct vpif_fh *fh = priv;
957 struct channel_obj *ch = fh->channel;
958 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
959 struct v4l2_buffer tbuf = *buf;
960 struct videobuf_buffer *buf1;
961 unsigned long addr = 0;
962 unsigned long flags;
963 int ret = 0;
965 vpif_dbg(2, debug, "vpif_qbuf\n");
967 if (common->fmt.type != tbuf.type) {
968 vpif_err("invalid buffer type\n");
969 return -EINVAL;
972 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
973 vpif_err("fh io not allowed \n");
974 return -EACCES;
977 if (!(list_empty(&common->dma_queue)) ||
978 (common->cur_frm != common->next_frm) ||
979 !common->started ||
980 (common->started && (0 == ch->field_id)))
981 return videobuf_qbuf(&common->buffer_queue, buf);
983 /* bufferqueue is empty store buffer address in VPIF registers */
984 mutex_lock(&common->buffer_queue.vb_lock);
985 buf1 = common->buffer_queue.bufs[tbuf.index];
987 if ((buf1->state == VIDEOBUF_QUEUED) ||
988 (buf1->state == VIDEOBUF_ACTIVE)) {
989 vpif_err("invalid state\n");
990 goto qbuf_exit;
993 switch (buf1->memory) {
994 case V4L2_MEMORY_MMAP:
995 if (buf1->baddr == 0)
996 goto qbuf_exit;
997 break;
999 case V4L2_MEMORY_USERPTR:
1000 if (tbuf.length < buf1->bsize)
1001 goto qbuf_exit;
1003 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
1004 && (buf1->baddr != tbuf.m.userptr)) {
1005 vpif_buffer_release(&common->buffer_queue, buf1);
1006 buf1->baddr = tbuf.m.userptr;
1008 break;
1010 default:
1011 goto qbuf_exit;
1014 local_irq_save(flags);
1015 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1016 common->buffer_queue.field);
1017 if (ret < 0) {
1018 local_irq_restore(flags);
1019 goto qbuf_exit;
1022 buf1->state = VIDEOBUF_ACTIVE;
1024 if (V4L2_MEMORY_USERPTR == common->memory)
1025 addr = buf1->boff;
1026 else
1027 addr = videobuf_to_dma_contig(buf1);
1029 common->next_frm = buf1;
1030 common->set_addr(addr + common->ytop_off,
1031 addr + common->ybtm_off,
1032 addr + common->ctop_off,
1033 addr + common->cbtm_off);
1035 local_irq_restore(flags);
1036 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1037 mutex_unlock(&common->buffer_queue.vb_lock);
1038 return 0;
1040 qbuf_exit:
1041 mutex_unlock(&common->buffer_queue.vb_lock);
1042 return -EINVAL;
1046 * vpif_dqbuf() - query buffer handler
1047 * @file: file ptr
1048 * @priv: file handle
1049 * @buf: v4l2 buffer structure ptr
1051 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1053 struct vpif_fh *fh = priv;
1054 struct channel_obj *ch = fh->channel;
1055 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1057 vpif_dbg(2, debug, "vpif_dqbuf\n");
1059 return videobuf_dqbuf(&common->buffer_queue, buf,
1060 file->f_flags & O_NONBLOCK);
1064 * vpif_streamon() - streamon handler
1065 * @file: file ptr
1066 * @priv: file handle
1067 * @buftype: v4l2 buffer type
1069 static int vpif_streamon(struct file *file, void *priv,
1070 enum v4l2_buf_type buftype)
1073 struct vpif_capture_config *config = vpif_dev->platform_data;
1074 struct vpif_fh *fh = priv;
1075 struct channel_obj *ch = fh->channel;
1076 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1077 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1078 struct vpif_params *vpif;
1079 unsigned long addr = 0;
1080 int ret = 0;
1082 vpif_dbg(2, debug, "vpif_streamon\n");
1084 vpif = &ch->vpifparams;
1086 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1087 vpif_dbg(1, debug, "buffer type not supported\n");
1088 return -EINVAL;
1091 /* If file handle is not allowed IO, return error */
1092 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1093 vpif_dbg(1, debug, "io not allowed\n");
1094 return -EACCES;
1097 /* If Streaming is already started, return error */
1098 if (common->started) {
1099 vpif_dbg(1, debug, "channel->started\n");
1100 return -EBUSY;
1103 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1104 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1105 vpif->std_info.ycmux_mode == 0) ||
1106 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1107 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1108 vpif_dbg(1, debug, "other channel is being used\n");
1109 return -EBUSY;
1112 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1113 if (ret)
1114 return ret;
1116 /* Enable streamon on the sub device */
1117 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1118 s_stream, 1);
1120 if (ret && (ret != -ENOIOCTLCMD)) {
1121 vpif_dbg(1, debug, "stream on failed in subdev\n");
1122 return ret;
1125 /* Call videobuf_streamon to start streaming in videobuf */
1126 ret = videobuf_streamon(&common->buffer_queue);
1127 if (ret) {
1128 vpif_dbg(1, debug, "videobuf_streamon\n");
1129 return ret;
1132 /* If buffer queue is empty, return error */
1133 if (list_empty(&common->dma_queue)) {
1134 vpif_dbg(1, debug, "buffer queue is empty\n");
1135 ret = -EIO;
1136 goto exit;
1139 /* Get the next frame from the buffer queue */
1140 common->cur_frm = list_entry(common->dma_queue.next,
1141 struct videobuf_buffer, queue);
1142 common->next_frm = common->cur_frm;
1144 /* Remove buffer from the buffer queue */
1145 list_del(&common->cur_frm->queue);
1146 /* Mark state of the current frame to active */
1147 common->cur_frm->state = VIDEOBUF_ACTIVE;
1148 /* Initialize field_id and started member */
1149 ch->field_id = 0;
1150 common->started = 1;
1152 if (V4L2_MEMORY_USERPTR == common->memory)
1153 addr = common->cur_frm->boff;
1154 else
1155 addr = videobuf_to_dma_contig(common->cur_frm);
1157 /* Calculate the offset for Y and C data in the buffer */
1158 vpif_calculate_offsets(ch);
1160 if ((vpif->std_info.frm_fmt &&
1161 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1162 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1163 (!vpif->std_info.frm_fmt &&
1164 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1165 vpif_dbg(1, debug, "conflict in field format and std format\n");
1166 ret = -EINVAL;
1167 goto exit;
1170 /* configure 1 or 2 channel mode */
1171 ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1173 if (ret < 0) {
1174 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1175 goto exit;
1178 /* Call vpif_set_params function to set the parameters and addresses */
1179 ret = vpif_set_video_params(vpif, ch->channel_id);
1181 if (ret < 0) {
1182 vpif_dbg(1, debug, "can't set video params\n");
1183 goto exit;
1186 common->started = ret;
1187 vpif_config_addr(ch, ret);
1189 common->set_addr(addr + common->ytop_off,
1190 addr + common->ybtm_off,
1191 addr + common->ctop_off,
1192 addr + common->cbtm_off);
1195 * Set interrupt for both the fields in VPIF Register enable channel in
1196 * VPIF register
1198 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1199 channel0_intr_assert();
1200 channel0_intr_enable(1);
1201 enable_channel0(1);
1203 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1204 (common->started == 2)) {
1205 channel1_intr_assert();
1206 channel1_intr_enable(1);
1207 enable_channel1(1);
1209 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1210 return ret;
1212 exit:
1213 videobuf_streamoff(&common->buffer_queue);
1214 return ret;
1218 * vpif_streamoff() - streamoff handler
1219 * @file: file ptr
1220 * @priv: file handle
1221 * @buftype: v4l2 buffer type
1223 static int vpif_streamoff(struct file *file, void *priv,
1224 enum v4l2_buf_type buftype)
1227 struct vpif_fh *fh = priv;
1228 struct channel_obj *ch = fh->channel;
1229 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1230 int ret;
1232 vpif_dbg(2, debug, "vpif_streamoff\n");
1234 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1235 vpif_dbg(1, debug, "buffer type not supported\n");
1236 return -EINVAL;
1239 /* If io is allowed for this file handle, return error */
1240 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1241 vpif_dbg(1, debug, "io not allowed\n");
1242 return -EACCES;
1245 /* If streaming is not started, return error */
1246 if (!common->started) {
1247 vpif_dbg(1, debug, "channel->started\n");
1248 return -EINVAL;
1251 /* disable channel */
1252 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1253 enable_channel0(0);
1254 channel0_intr_enable(0);
1255 } else {
1256 enable_channel1(0);
1257 channel1_intr_enable(0);
1260 common->started = 0;
1262 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1263 s_stream, 0);
1265 if (ret && (ret != -ENOIOCTLCMD))
1266 vpif_dbg(1, debug, "stream off failed in subdev\n");
1268 return videobuf_streamoff(&common->buffer_queue);
1272 * vpif_map_sub_device_to_input() - Maps sub device to input
1273 * @ch - ptr to channel
1274 * @config - ptr to capture configuration
1275 * @input_index - Given input index from application
1276 * @sub_device_index - index into sd table
1278 * lookup the sub device information for a given input index.
1279 * we report all the inputs to application. inputs table also
1280 * has sub device name for the each input
1282 static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1283 struct channel_obj *ch,
1284 struct vpif_capture_config *vpif_cfg,
1285 int input_index,
1286 int *sub_device_index)
1288 struct vpif_capture_chan_config *chan_cfg;
1289 struct vpif_subdev_info *subdev_info = NULL;
1290 const char *subdev_name = NULL;
1291 int i;
1293 vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1295 chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1298 * search through the inputs to find the sub device supporting
1299 * the input
1301 for (i = 0; i < chan_cfg->input_count; i++) {
1302 /* For each sub device, loop through input */
1303 if (i == input_index) {
1304 subdev_name = chan_cfg->inputs[i].subdev_name;
1305 break;
1309 /* if reached maximum. return null */
1310 if (i == chan_cfg->input_count || (NULL == subdev_name))
1311 return subdev_info;
1313 /* loop through the sub device list to get the sub device info */
1314 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1315 subdev_info = &vpif_cfg->subdev_info[i];
1316 if (!strcmp(subdev_info->name, subdev_name))
1317 break;
1320 if (i == vpif_cfg->subdev_count)
1321 return subdev_info;
1323 /* check if the sub device is registered */
1324 if (NULL == vpif_obj.sd[i])
1325 return NULL;
1327 *sub_device_index = i;
1328 return subdev_info;
1332 * vpif_querystd() - querystd handler
1333 * @file: file ptr
1334 * @priv: file handle
1335 * @std_id: ptr to std id
1337 * This function is called to detect standard at the selected input
1339 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1341 struct vpif_fh *fh = priv;
1342 struct channel_obj *ch = fh->channel;
1343 int ret = 0;
1345 vpif_dbg(2, debug, "vpif_querystd\n");
1347 /* Call querystd function of decoder device */
1348 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1349 querystd, std_id);
1350 if (ret < 0)
1351 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1353 return ret;
1357 * vpif_g_std() - get STD handler
1358 * @file: file ptr
1359 * @priv: file handle
1360 * @std_id: ptr to std id
1362 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1364 struct vpif_fh *fh = priv;
1365 struct channel_obj *ch = fh->channel;
1367 vpif_dbg(2, debug, "vpif_g_std\n");
1369 *std = ch->video.stdid;
1370 return 0;
1374 * vpif_s_std() - set STD handler
1375 * @file: file ptr
1376 * @priv: file handle
1377 * @std_id: ptr to std id
1379 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1381 struct vpif_fh *fh = priv;
1382 struct channel_obj *ch = fh->channel;
1383 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1384 int ret = 0;
1386 vpif_dbg(2, debug, "vpif_s_std\n");
1388 if (common->started) {
1389 vpif_err("streaming in progress\n");
1390 return -EBUSY;
1393 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1394 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1395 if (!fh->initialized) {
1396 vpif_dbg(1, debug, "Channel Busy\n");
1397 return -EBUSY;
1401 ret = v4l2_prio_check(&ch->prio, fh->prio);
1402 if (0 != ret)
1403 return ret;
1405 fh->initialized = 1;
1407 /* Call encoder subdevice function to set the standard */
1408 ch->video.stdid = *std_id;
1409 ch->video.dv_preset = V4L2_DV_INVALID;
1410 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1412 /* Get the information about the standard */
1413 if (vpif_update_std_info(ch)) {
1414 vpif_err("Error getting the standard info\n");
1415 return -EINVAL;
1418 /* Configure the default format information */
1419 vpif_config_format(ch);
1421 /* set standard in the sub device */
1422 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1423 s_std, *std_id);
1424 if (ret < 0)
1425 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1426 return ret;
1430 * vpif_enum_input() - ENUMINPUT handler
1431 * @file: file ptr
1432 * @priv: file handle
1433 * @input: ptr to input structure
1435 static int vpif_enum_input(struct file *file, void *priv,
1436 struct v4l2_input *input)
1439 struct vpif_capture_config *config = vpif_dev->platform_data;
1440 struct vpif_capture_chan_config *chan_cfg;
1441 struct vpif_fh *fh = priv;
1442 struct channel_obj *ch = fh->channel;
1444 chan_cfg = &config->chan_config[ch->channel_id];
1446 if (input->index >= chan_cfg->input_count) {
1447 vpif_dbg(1, debug, "Invalid input index\n");
1448 return -EINVAL;
1451 memcpy(input, &chan_cfg->inputs[input->index].input,
1452 sizeof(*input));
1453 return 0;
1457 * vpif_g_input() - Get INPUT handler
1458 * @file: file ptr
1459 * @priv: file handle
1460 * @index: ptr to input index
1462 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1464 struct vpif_fh *fh = priv;
1465 struct channel_obj *ch = fh->channel;
1466 struct video_obj *vid_ch = &ch->video;
1468 *index = vid_ch->input_idx;
1470 return 0;
1474 * vpif_s_input() - Set INPUT handler
1475 * @file: file ptr
1476 * @priv: file handle
1477 * @index: input index
1479 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1481 struct vpif_capture_config *config = vpif_dev->platform_data;
1482 struct vpif_capture_chan_config *chan_cfg;
1483 struct vpif_fh *fh = priv;
1484 struct channel_obj *ch = fh->channel;
1485 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1486 struct video_obj *vid_ch = &ch->video;
1487 struct vpif_subdev_info *subdev_info;
1488 int ret = 0, sd_index = 0;
1489 u32 input = 0, output = 0;
1491 chan_cfg = &config->chan_config[ch->channel_id];
1493 if (common->started) {
1494 vpif_err("Streaming in progress\n");
1495 return -EBUSY;
1498 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1499 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1500 if (!fh->initialized) {
1501 vpif_dbg(1, debug, "Channel Busy\n");
1502 return -EBUSY;
1506 ret = v4l2_prio_check(&ch->prio, fh->prio);
1507 if (0 != ret)
1508 return ret;
1510 fh->initialized = 1;
1511 subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1512 &sd_index);
1513 if (NULL == subdev_info) {
1514 vpif_dbg(1, debug,
1515 "couldn't lookup sub device for the input index\n");
1516 return -EINVAL;
1519 /* first setup input path from sub device to vpif */
1520 if (config->setup_input_path) {
1521 ret = config->setup_input_path(ch->channel_id,
1522 subdev_info->name);
1523 if (ret < 0) {
1524 vpif_dbg(1, debug, "couldn't setup input path for the"
1525 " sub device %s, for input index %d\n",
1526 subdev_info->name, index);
1527 return ret;
1531 if (subdev_info->can_route) {
1532 input = subdev_info->input;
1533 output = subdev_info->output;
1534 ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1535 input, output, 0);
1536 if (ret < 0) {
1537 vpif_dbg(1, debug, "Failed to set input\n");
1538 return ret;
1541 vid_ch->input_idx = index;
1542 ch->curr_subdev_info = subdev_info;
1543 ch->curr_sd_index = sd_index;
1544 /* copy interface parameters to vpif */
1545 ch->vpifparams.iface = subdev_info->vpif_if;
1547 /* update tvnorms from the sub device input info */
1548 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1549 return ret;
1553 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1554 * @file: file ptr
1555 * @priv: file handle
1556 * @index: input index
1558 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1559 struct v4l2_fmtdesc *fmt)
1561 struct vpif_fh *fh = priv;
1562 struct channel_obj *ch = fh->channel;
1564 if (fmt->index != 0) {
1565 vpif_dbg(1, debug, "Invalid format index\n");
1566 return -EINVAL;
1569 /* Fill in the information about format */
1570 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1571 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1572 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1573 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1574 } else {
1575 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1576 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1577 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1579 return 0;
1583 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1584 * @file: file ptr
1585 * @priv: file handle
1586 * @fmt: ptr to v4l2 format structure
1588 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1589 struct v4l2_format *fmt)
1591 struct vpif_fh *fh = priv;
1592 struct channel_obj *ch = fh->channel;
1593 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1595 return vpif_check_format(ch, pixfmt, 1);
1600 * vpif_g_fmt_vid_cap() - Set INPUT handler
1601 * @file: file ptr
1602 * @priv: file handle
1603 * @fmt: ptr to v4l2 format structure
1605 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1606 struct v4l2_format *fmt)
1608 struct vpif_fh *fh = priv;
1609 struct channel_obj *ch = fh->channel;
1610 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1612 /* Check the validity of the buffer type */
1613 if (common->fmt.type != fmt->type)
1614 return -EINVAL;
1616 /* Fill in the information about format */
1617 *fmt = common->fmt;
1618 return 0;
1622 * vpif_s_fmt_vid_cap() - Set FMT handler
1623 * @file: file ptr
1624 * @priv: file handle
1625 * @fmt: ptr to v4l2 format structure
1627 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1628 struct v4l2_format *fmt)
1630 struct vpif_fh *fh = priv;
1631 struct channel_obj *ch = fh->channel;
1632 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1633 struct v4l2_pix_format *pixfmt;
1634 int ret = 0;
1636 vpif_dbg(2, debug, "%s\n", __func__);
1638 /* If streaming is started, return error */
1639 if (common->started) {
1640 vpif_dbg(1, debug, "Streaming is started\n");
1641 return -EBUSY;
1644 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1645 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1646 if (!fh->initialized) {
1647 vpif_dbg(1, debug, "Channel Busy\n");
1648 return -EBUSY;
1652 ret = v4l2_prio_check(&ch->prio, fh->prio);
1653 if (0 != ret)
1654 return ret;
1656 fh->initialized = 1;
1658 pixfmt = &fmt->fmt.pix;
1659 /* Check for valid field format */
1660 ret = vpif_check_format(ch, pixfmt, 0);
1662 if (ret)
1663 return ret;
1664 /* store the format in the channel object */
1665 common->fmt = *fmt;
1666 return 0;
1670 * vpif_querycap() - QUERYCAP handler
1671 * @file: file ptr
1672 * @priv: file handle
1673 * @cap: ptr to v4l2_capability structure
1675 static int vpif_querycap(struct file *file, void *priv,
1676 struct v4l2_capability *cap)
1678 struct vpif_capture_config *config = vpif_dev->platform_data;
1680 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1681 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1682 strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info));
1683 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1685 return 0;
1689 * vpif_g_priority() - get priority handler
1690 * @file: file ptr
1691 * @priv: file handle
1692 * @prio: ptr to v4l2_priority structure
1694 static int vpif_g_priority(struct file *file, void *priv,
1695 enum v4l2_priority *prio)
1697 struct vpif_fh *fh = priv;
1698 struct channel_obj *ch = fh->channel;
1700 *prio = v4l2_prio_max(&ch->prio);
1702 return 0;
1706 * vpif_s_priority() - set priority handler
1707 * @file: file ptr
1708 * @priv: file handle
1709 * @prio: ptr to v4l2_priority structure
1711 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1713 struct vpif_fh *fh = priv;
1714 struct channel_obj *ch = fh->channel;
1716 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1720 * vpif_cropcap() - cropcap handler
1721 * @file: file ptr
1722 * @priv: file handle
1723 * @crop: ptr to v4l2_cropcap structure
1725 static int vpif_cropcap(struct file *file, void *priv,
1726 struct v4l2_cropcap *crop)
1728 struct vpif_fh *fh = priv;
1729 struct channel_obj *ch = fh->channel;
1730 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1732 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1733 return -EINVAL;
1735 crop->bounds.left = 0;
1736 crop->bounds.top = 0;
1737 crop->bounds.height = common->height;
1738 crop->bounds.width = common->width;
1739 crop->defrect = crop->bounds;
1740 return 0;
1744 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1745 * @file: file ptr
1746 * @priv: file handle
1747 * @preset: input preset
1749 static int vpif_enum_dv_presets(struct file *file, void *priv,
1750 struct v4l2_dv_enum_preset *preset)
1752 struct vpif_fh *fh = priv;
1753 struct channel_obj *ch = fh->channel;
1755 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1756 video, enum_dv_presets, preset);
1760 * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1761 * @file: file ptr
1762 * @priv: file handle
1763 * @preset: input preset
1765 static int vpif_query_dv_preset(struct file *file, void *priv,
1766 struct v4l2_dv_preset *preset)
1768 struct vpif_fh *fh = priv;
1769 struct channel_obj *ch = fh->channel;
1771 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1772 video, query_dv_preset, preset);
1775 * vpif_s_dv_presets() - S_DV_PRESETS handler
1776 * @file: file ptr
1777 * @priv: file handle
1778 * @preset: input preset
1780 static int vpif_s_dv_preset(struct file *file, void *priv,
1781 struct v4l2_dv_preset *preset)
1783 struct vpif_fh *fh = priv;
1784 struct channel_obj *ch = fh->channel;
1785 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1786 int ret = 0;
1788 if (common->started) {
1789 vpif_dbg(1, debug, "streaming in progress\n");
1790 return -EBUSY;
1793 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1794 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1795 if (!fh->initialized) {
1796 vpif_dbg(1, debug, "Channel Busy\n");
1797 return -EBUSY;
1801 ret = v4l2_prio_check(&ch->prio, fh->prio);
1802 if (ret)
1803 return ret;
1805 fh->initialized = 1;
1807 /* Call encoder subdevice function to set the standard */
1808 if (mutex_lock_interruptible(&common->lock))
1809 return -ERESTARTSYS;
1811 ch->video.dv_preset = preset->preset;
1812 ch->video.stdid = V4L2_STD_UNKNOWN;
1813 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1815 /* Get the information about the standard */
1816 if (vpif_update_std_info(ch)) {
1817 vpif_dbg(1, debug, "Error getting the standard info\n");
1818 ret = -EINVAL;
1819 } else {
1820 /* Configure the default format information */
1821 vpif_config_format(ch);
1823 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1824 video, s_dv_preset, preset);
1827 mutex_unlock(&common->lock);
1829 return ret;
1832 * vpif_g_dv_presets() - G_DV_PRESETS handler
1833 * @file: file ptr
1834 * @priv: file handle
1835 * @preset: input preset
1837 static int vpif_g_dv_preset(struct file *file, void *priv,
1838 struct v4l2_dv_preset *preset)
1840 struct vpif_fh *fh = priv;
1841 struct channel_obj *ch = fh->channel;
1843 preset->preset = ch->video.dv_preset;
1845 return 0;
1849 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1850 * @file: file ptr
1851 * @priv: file handle
1852 * @timings: digital video timings
1854 static int vpif_s_dv_timings(struct file *file, void *priv,
1855 struct v4l2_dv_timings *timings)
1857 struct vpif_fh *fh = priv;
1858 struct channel_obj *ch = fh->channel;
1859 struct vpif_params *vpifparams = &ch->vpifparams;
1860 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1861 struct video_obj *vid_ch = &ch->video;
1862 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1863 int ret;
1865 if (timings->type != V4L2_DV_BT_656_1120) {
1866 vpif_dbg(2, debug, "Timing type not defined\n");
1867 return -EINVAL;
1870 /* Configure subdevice timings, if any */
1871 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1872 video, s_dv_timings, timings);
1873 if (ret == -ENOIOCTLCMD) {
1874 vpif_dbg(2, debug, "Custom DV timings not supported by "
1875 "subdevice\n");
1876 return -EINVAL;
1878 if (ret < 0) {
1879 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1880 return ret;
1883 if (!(timings->bt.width && timings->bt.height &&
1884 (timings->bt.hbackporch ||
1885 timings->bt.hfrontporch ||
1886 timings->bt.hsync) &&
1887 timings->bt.vfrontporch &&
1888 (timings->bt.vbackporch ||
1889 timings->bt.vsync))) {
1890 vpif_dbg(2, debug, "Timings for width, height, "
1891 "horizontal back porch, horizontal sync, "
1892 "horizontal front porch, vertical back porch, "
1893 "vertical sync and vertical back porch "
1894 "must be defined\n");
1895 return -EINVAL;
1898 *bt = timings->bt;
1900 /* Configure video port timings */
1902 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1903 bt->hsync - 8;
1904 std_info->sav2eav = bt->width;
1906 std_info->l1 = 1;
1907 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1909 if (bt->interlaced) {
1910 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1911 std_info->vsize = bt->height * 2 +
1912 bt->vfrontporch + bt->vsync + bt->vbackporch +
1913 bt->il_vfrontporch + bt->il_vsync +
1914 bt->il_vbackporch;
1915 std_info->l5 = std_info->vsize/2 -
1916 (bt->vfrontporch - 1);
1917 std_info->l7 = std_info->vsize/2 + 1;
1918 std_info->l9 = std_info->l7 + bt->il_vsync +
1919 bt->il_vbackporch + 1;
1920 std_info->l11 = std_info->vsize -
1921 (bt->il_vfrontporch - 1);
1922 } else {
1923 vpif_dbg(2, debug, "Required timing values for "
1924 "interlaced BT format missing\n");
1925 return -EINVAL;
1927 } else {
1928 std_info->vsize = bt->height + bt->vfrontporch +
1929 bt->vsync + bt->vbackporch;
1930 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1932 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1933 std_info->width = bt->width;
1934 std_info->height = bt->height;
1935 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1936 std_info->ycmux_mode = 0;
1937 std_info->capture_format = 0;
1938 std_info->vbi_supported = 0;
1939 std_info->hd_sd = 1;
1940 std_info->stdid = 0;
1941 std_info->dv_preset = V4L2_DV_INVALID;
1943 vid_ch->stdid = 0;
1944 vid_ch->dv_preset = V4L2_DV_INVALID;
1945 return 0;
1949 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1950 * @file: file ptr
1951 * @priv: file handle
1952 * @timings: digital video timings
1954 static int vpif_g_dv_timings(struct file *file, void *priv,
1955 struct v4l2_dv_timings *timings)
1957 struct vpif_fh *fh = priv;
1958 struct channel_obj *ch = fh->channel;
1959 struct video_obj *vid_ch = &ch->video;
1960 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1962 timings->bt = *bt;
1964 return 0;
1968 * vpif_g_chip_ident() - Identify the chip
1969 * @file: file ptr
1970 * @priv: file handle
1971 * @chip: chip identity
1973 * Returns zero or -EINVAL if read operations fails.
1975 static int vpif_g_chip_ident(struct file *file, void *priv,
1976 struct v4l2_dbg_chip_ident *chip)
1978 chip->ident = V4L2_IDENT_NONE;
1979 chip->revision = 0;
1980 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1981 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1982 vpif_dbg(2, debug, "match_type is invalid.\n");
1983 return -EINVAL;
1986 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1987 g_chip_ident, chip);
1990 #ifdef CONFIG_VIDEO_ADV_DEBUG
1992 * vpif_dbg_g_register() - Read register
1993 * @file: file ptr
1994 * @priv: file handle
1995 * @reg: register to be read
1997 * Debugging only
1998 * Returns zero or -EINVAL if read operations fails.
2000 static int vpif_dbg_g_register(struct file *file, void *priv,
2001 struct v4l2_dbg_register *reg){
2002 struct vpif_fh *fh = priv;
2003 struct channel_obj *ch = fh->channel;
2005 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2006 g_register, reg);
2010 * vpif_dbg_s_register() - Write to register
2011 * @file: file ptr
2012 * @priv: file handle
2013 * @reg: register to be modified
2015 * Debugging only
2016 * Returns zero or -EINVAL if write operations fails.
2018 static int vpif_dbg_s_register(struct file *file, void *priv,
2019 struct v4l2_dbg_register *reg){
2020 struct vpif_fh *fh = priv;
2021 struct channel_obj *ch = fh->channel;
2023 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2024 s_register, reg);
2026 #endif
2029 * vpif_log_status() - Status information
2030 * @file: file ptr
2031 * @priv: file handle
2033 * Returns zero.
2035 static int vpif_log_status(struct file *filep, void *priv)
2037 /* status for sub devices */
2038 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
2040 return 0;
2043 /* vpif capture ioctl operations */
2044 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
2045 .vidioc_querycap = vpif_querycap,
2046 .vidioc_g_priority = vpif_g_priority,
2047 .vidioc_s_priority = vpif_s_priority,
2048 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
2049 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
2050 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
2051 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
2052 .vidioc_enum_input = vpif_enum_input,
2053 .vidioc_s_input = vpif_s_input,
2054 .vidioc_g_input = vpif_g_input,
2055 .vidioc_reqbufs = vpif_reqbufs,
2056 .vidioc_querybuf = vpif_querybuf,
2057 .vidioc_querystd = vpif_querystd,
2058 .vidioc_s_std = vpif_s_std,
2059 .vidioc_g_std = vpif_g_std,
2060 .vidioc_qbuf = vpif_qbuf,
2061 .vidioc_dqbuf = vpif_dqbuf,
2062 .vidioc_streamon = vpif_streamon,
2063 .vidioc_streamoff = vpif_streamoff,
2064 .vidioc_cropcap = vpif_cropcap,
2065 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
2066 .vidioc_s_dv_preset = vpif_s_dv_preset,
2067 .vidioc_g_dv_preset = vpif_g_dv_preset,
2068 .vidioc_query_dv_preset = vpif_query_dv_preset,
2069 .vidioc_s_dv_timings = vpif_s_dv_timings,
2070 .vidioc_g_dv_timings = vpif_g_dv_timings,
2071 .vidioc_g_chip_ident = vpif_g_chip_ident,
2072 #ifdef CONFIG_VIDEO_ADV_DEBUG
2073 .vidioc_g_register = vpif_dbg_g_register,
2074 .vidioc_s_register = vpif_dbg_s_register,
2075 #endif
2076 .vidioc_log_status = vpif_log_status,
2079 /* vpif file operations */
2080 static struct v4l2_file_operations vpif_fops = {
2081 .owner = THIS_MODULE,
2082 .open = vpif_open,
2083 .release = vpif_release,
2084 .unlocked_ioctl = video_ioctl2,
2085 .mmap = vpif_mmap,
2086 .poll = vpif_poll
2089 /* vpif video template */
2090 static struct video_device vpif_video_template = {
2091 .name = "vpif",
2092 .fops = &vpif_fops,
2093 .minor = -1,
2094 .ioctl_ops = &vpif_ioctl_ops,
2098 * initialize_vpif() - Initialize vpif data structures
2100 * Allocate memory for data structures and initialize them
2102 static int initialize_vpif(void)
2104 int err = 0, i, j;
2105 int free_channel_objects_index;
2107 /* Default number of buffers should be 3 */
2108 if ((ch0_numbuffers > 0) &&
2109 (ch0_numbuffers < config_params.min_numbuffers))
2110 ch0_numbuffers = config_params.min_numbuffers;
2111 if ((ch1_numbuffers > 0) &&
2112 (ch1_numbuffers < config_params.min_numbuffers))
2113 ch1_numbuffers = config_params.min_numbuffers;
2115 /* Set buffer size to min buffers size if it is invalid */
2116 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2117 ch0_bufsize =
2118 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2119 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2120 ch1_bufsize =
2121 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2123 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2124 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2125 if (ch0_numbuffers) {
2126 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2127 = ch0_bufsize;
2129 if (ch1_numbuffers) {
2130 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2131 = ch1_bufsize;
2134 /* Allocate memory for six channel objects */
2135 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2136 vpif_obj.dev[i] =
2137 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2138 /* If memory allocation fails, return error */
2139 if (!vpif_obj.dev[i]) {
2140 free_channel_objects_index = i;
2141 err = -ENOMEM;
2142 goto vpif_init_free_channel_objects;
2145 return 0;
2147 vpif_init_free_channel_objects:
2148 for (j = 0; j < free_channel_objects_index; j++)
2149 kfree(vpif_obj.dev[j]);
2150 return err;
2154 * vpif_probe : This function probes the vpif capture driver
2155 * @pdev: platform device pointer
2157 * This creates device entries by register itself to the V4L2 driver and
2158 * initializes fields of each channel objects
2160 static __init int vpif_probe(struct platform_device *pdev)
2162 struct vpif_subdev_info *subdevdata;
2163 struct vpif_capture_config *config;
2164 int i, j, k, m, q, err;
2165 struct i2c_adapter *i2c_adap;
2166 struct channel_obj *ch;
2167 struct common_obj *common;
2168 struct video_device *vfd;
2169 struct resource *res;
2170 int subdev_count;
2172 vpif_dev = &pdev->dev;
2174 err = initialize_vpif();
2175 if (err) {
2176 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2177 return err;
2180 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2181 if (err) {
2182 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2183 return err;
2186 k = 0;
2187 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2188 for (i = res->start; i <= res->end; i++) {
2189 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
2190 "DM646x_Capture",
2191 (void *)(&vpif_obj.dev[k]->channel_id))) {
2192 err = -EBUSY;
2193 i--;
2194 goto vpif_int_err;
2197 k++;
2200 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2201 /* Get the pointer to the channel object */
2202 ch = vpif_obj.dev[i];
2203 /* Allocate memory for video device */
2204 vfd = video_device_alloc();
2205 if (NULL == vfd) {
2206 for (j = 0; j < i; j++) {
2207 ch = vpif_obj.dev[j];
2208 video_device_release(ch->video_dev);
2210 err = -ENOMEM;
2211 goto vpif_dev_alloc_err;
2214 /* Initialize field of video device */
2215 *vfd = vpif_video_template;
2216 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2217 vfd->release = video_device_release;
2218 snprintf(vfd->name, sizeof(vfd->name),
2219 "DM646x_VPIFCapture_DRIVER_V%s",
2220 VPIF_CAPTURE_VERSION);
2221 /* Set video_dev to the video device */
2222 ch->video_dev = vfd;
2225 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2226 ch = vpif_obj.dev[j];
2227 ch->channel_id = j;
2228 common = &(ch->common[VPIF_VIDEO_INDEX]);
2229 spin_lock_init(&common->irqlock);
2230 mutex_init(&common->lock);
2231 ch->video_dev->lock = &common->lock;
2232 /* Initialize prio member of channel object */
2233 v4l2_prio_init(&ch->prio);
2234 err = video_register_device(ch->video_dev,
2235 VFL_TYPE_GRABBER, (j ? 1 : 0));
2236 if (err)
2237 goto probe_out;
2239 video_set_drvdata(ch->video_dev, ch);
2243 i2c_adap = i2c_get_adapter(1);
2244 config = pdev->dev.platform_data;
2246 subdev_count = config->subdev_count;
2247 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2248 GFP_KERNEL);
2249 if (vpif_obj.sd == NULL) {
2250 vpif_err("unable to allocate memory for subdevice pointers\n");
2251 err = -ENOMEM;
2252 goto probe_out;
2255 for (i = 0; i < subdev_count; i++) {
2256 subdevdata = &config->subdev_info[i];
2257 vpif_obj.sd[i] =
2258 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2259 i2c_adap,
2260 &subdevdata->board_info,
2261 NULL);
2263 if (!vpif_obj.sd[i]) {
2264 vpif_err("Error registering v4l2 subdevice\n");
2265 goto probe_subdev_out;
2267 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2268 subdevdata->name);
2270 if (vpif_obj.sd[i])
2271 vpif_obj.sd[i]->grp_id = 1 << i;
2274 v4l2_info(&vpif_obj.v4l2_dev,
2275 "DM646x VPIF capture driver initialized\n");
2276 return 0;
2278 probe_subdev_out:
2279 /* free sub devices memory */
2280 kfree(vpif_obj.sd);
2282 j = VPIF_CAPTURE_MAX_DEVICES;
2283 probe_out:
2284 for (k = 0; k < j; k++) {
2285 /* Get the pointer to the channel object */
2286 ch = vpif_obj.dev[k];
2287 /* Unregister video device */
2288 video_unregister_device(ch->video_dev);
2291 vpif_dev_alloc_err:
2292 k = VPIF_CAPTURE_MAX_DEVICES-1;
2293 res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2294 i = res->end;
2296 vpif_int_err:
2297 for (q = k; q >= 0; q--) {
2298 for (m = i; m >= (int)res->start; m--)
2299 free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2301 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2302 if (res)
2303 i = res->end;
2305 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2306 return err;
2310 * vpif_remove() - driver remove handler
2311 * @device: ptr to platform device structure
2313 * The vidoe device is unregistered
2315 static int vpif_remove(struct platform_device *device)
2317 int i;
2318 struct channel_obj *ch;
2320 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2322 /* un-register device */
2323 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2324 /* Get the pointer to the channel object */
2325 ch = vpif_obj.dev[i];
2326 /* Unregister video device */
2327 video_unregister_device(ch->video_dev);
2329 return 0;
2333 * vpif_suspend: vpif device suspend
2335 * TODO: Add suspend code here
2337 static int
2338 vpif_suspend(struct device *dev)
2340 return -1;
2344 * vpif_resume: vpif device suspend
2346 * TODO: Add resume code here
2348 static int
2349 vpif_resume(struct device *dev)
2351 return -1;
2354 static const struct dev_pm_ops vpif_dev_pm_ops = {
2355 .suspend = vpif_suspend,
2356 .resume = vpif_resume,
2359 static __refdata struct platform_driver vpif_driver = {
2360 .driver = {
2361 .name = "vpif_capture",
2362 .owner = THIS_MODULE,
2363 .pm = &vpif_dev_pm_ops,
2365 .probe = vpif_probe,
2366 .remove = vpif_remove,
2370 * vpif_init: initialize the vpif driver
2372 * This function registers device and driver to the kernel, requests irq
2373 * handler and allocates memory
2374 * for channel objects
2376 static __init int vpif_init(void)
2378 return platform_driver_register(&vpif_driver);
2382 * vpif_cleanup : This function clean up the vpif capture resources
2384 * This will un-registers device and driver to the kernel, frees
2385 * requested irq handler and de-allocates memory allocated for channel
2386 * objects.
2388 static void vpif_cleanup(void)
2390 struct platform_device *pdev;
2391 struct resource *res;
2392 int irq_num;
2393 int i = 0;
2395 pdev = container_of(vpif_dev, struct platform_device, dev);
2396 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2397 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2398 free_irq(irq_num,
2399 (void *)(&vpif_obj.dev[i]->channel_id));
2400 i++;
2403 platform_driver_unregister(&vpif_driver);
2405 kfree(vpif_obj.sd);
2406 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2407 kfree(vpif_obj.dev[i]);
2410 /* Function for module initialization and cleanup */
2411 module_init(vpif_init);
2412 module_exit(vpif_cleanup);