Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6.git] / drivers / media / video / davinci / vpif_capture.c
1 /*
2  * Copyright (C) 2009 Texas Instruments Inc
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * TODO : add support for VBI & HBI data service
19  *        add static buffer allocation
20  */
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/workqueue.h>
29 #include <linux/string.h>
30 #include <linux/videodev2.h>
31 #include <linux/wait.h>
32 #include <linux/time.h>
33 #include <linux/i2c.h>
34 #include <linux/platform_device.h>
35 #include <linux/io.h>
36 #include <linux/slab.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
40
41 #include "vpif_capture.h"
42 #include "vpif.h"
43
44 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(VPIF_CAPTURE_VERSION);
47
48 #define vpif_err(fmt, arg...)   v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49 #define vpif_dbg(level, debug, fmt, arg...)     \
50                 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51
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;
57
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);
63
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)");
69
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,
78 };
79
80 /* global variables */
81 static struct vpif_device vpif_obj = { {NULL} };
82 static struct device *vpif_dev;
83
84 /**
85  * vpif_uservirt_to_phys : translate user/virtual address to phy address
86  * @virtp: user/virtual address
87  *
88  * This inline function is used to convert user space virtual address to
89  * physical address.
90  */
91 static inline u32 vpif_uservirt_to_phys(u32 virtp)
92 {
93         unsigned long physp = 0;
94         struct mm_struct *mm = current->mm;
95         struct vm_area_struct *vma;
96
97         vma = find_vma(mm, virtp);
98
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))
103                 /**
104                  * this will catch, kernel-allocated, mmaped-to-usermode
105                  * addresses
106                  */
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;
112
113                 down_read(&current->mm->mmap_sem);
114
115                 res = get_user_pages(current, current->mm,
116                                      virtp, nr_pages, 1, 0, &pages, NULL);
117                 up_read(&current->mm->mmap_sem);
118
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;
125                 }
126         }
127         return physp;
128 }
129
130 /**
131  * buffer_prepare :  callback function for buffer prepare
132  * @q : buffer queue ptr
133  * @vb: ptr to video buffer
134  * @field: field info
135  *
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
139  */
140 static int vpif_buffer_prepare(struct videobuf_queue *q,
141                                struct videobuf_buffer *vb,
142                                enum v4l2_field field)
143 {
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;
149
150
151         vpif_dbg(2, debug, "vpif_buffer_prepare\n");
152
153         common = &ch->common[VPIF_VIDEO_INDEX];
154
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;
161         }
162         vb->state = VIDEOBUF_PREPARED;
163         /**
164          * if user pointer memory mechanism is used, get the physical
165          * address of the buffer
166          */
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;
171
172                 }
173                 vb->boff = vpif_uservirt_to_phys(vb->baddr);
174                 if (!IS_ALIGNED(vb->boff, 8))
175                         goto exit;
176         }
177
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;
185         }
186         return 0;
187 exit:
188         vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
189         return -EINVAL;
190 }
191
192 /**
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
197  *
198  * This callback function is called when reqbuf() is called to adjust
199  * the buffer count and buffer size
200  */
201 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
202                              unsigned int *size)
203 {
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;
208
209         common = &ch->common[VPIF_VIDEO_INDEX];
210
211         vpif_dbg(2, debug, "vpif_buffer_setup\n");
212
213         /* If memory type is not mmap, return */
214         if (V4L2_MEMORY_MMAP != common->memory)
215                 return 0;
216
217         /* Calculate the size of the buffer */
218         *size = config_params.channel_bufsize[ch->channel_id];
219
220         if (*count < config_params.min_numbuffers)
221                 *count = config_params.min_numbuffers;
222         return 0;
223 }
224
225 /**
226  * vpif_buffer_queue : Callback function to add buffer to DMA queue
227  * @q: ptr to videobuf_queue
228  * @vb: ptr to videobuf_buffer
229  */
230 static void vpif_buffer_queue(struct videobuf_queue *q,
231                               struct videobuf_buffer *vb)
232 {
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;
237
238         common = &ch->common[VPIF_VIDEO_INDEX];
239
240         vpif_dbg(2, debug, "vpif_buffer_queue\n");
241
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;
246 }
247
248 /**
249  * vpif_buffer_release : Callback function to free buffer
250  * @q: buffer queue ptr
251  * @vb: ptr to video buffer
252  *
253  * This function is called from the videobuf layer to free memory
254  * allocated to  the buffers
255  */
256 static void vpif_buffer_release(struct videobuf_queue *q,
257                                 struct videobuf_buffer *vb)
258 {
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;
263
264         common = &ch->common[VPIF_VIDEO_INDEX];
265
266         videobuf_dma_contig_free(q, vb);
267         vb->state = VIDEOBUF_NEEDS_INIT;
268 }
269
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,
275 };
276
277 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
278         { {1, 1} };
279
280 /**
281  * vpif_process_buffer_complete: process a completed buffer
282  * @common: ptr to common channel object
283  *
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
287  */
288 static void vpif_process_buffer_complete(struct common_obj *common)
289 {
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;
295 }
296
297 /**
298  * vpif_schedule_next_buffer: set next buffer address for capture
299  * @common : ptr to common channel object
300  *
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
304  */
305 static void vpif_schedule_next_buffer(struct common_obj *common)
306 {
307         unsigned long addr = 0;
308
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);
318
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);
324 }
325
326 /**
327  * vpif_channel_isr : ISR handler for vpif capture
328  * @irq: irq number
329  * @dev_id: dev_id ptr
330  *
331  * It changes status of the captured buffer, takes next buffer from the queue
332  * and sets its address in VPIF registers
333  */
334 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
335 {
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;
342
343         channel_id = *(int *)(dev_id);
344         ch = dev->dev[channel_id];
345
346         field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
347
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;
353
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;
359
360                         if (!channel_first_int[i][channel_id])
361                                 vpif_process_buffer_complete(common);
362
363                         channel_first_int[i][channel_id] = 0;
364
365                         vpif_schedule_next_buffer(common);
366
367
368                         channel_first_int[i][channel_id] = 0;
369                 } else {
370                         /**
371                          * Interlaced mode. If it is first interrupt, ignore
372                          * it
373                          */
374                         if (channel_first_int[i][channel_id]) {
375                                 channel_first_int[i][channel_id] = 0;
376                                 continue;
377                         }
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) {
383                                         /**
384                                          * If field id does not match stored
385                                          * field id, make them in sync
386                                          */
387                                         if (0 == fid)
388                                                 ch->field_id = fid;
389                                         return IRQ_HANDLED;
390                                 }
391                         }
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;
397
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;
405
406                                 vpif_schedule_next_buffer(common);
407                         }
408                 }
409         }
410         return IRQ_HANDLED;
411 }
412
413 /**
414  * vpif_update_std_info() - update standard related info
415  * @ch: ptr to channel object
416  *
417  * For a given standard selected by application, update values
418  * in the device data structures
419  */
420 static int vpif_update_std_info(struct channel_obj *ch)
421 {
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;
428
429         vpif_dbg(2, debug, "vpif_update_std_info\n");
430
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;
438                         }
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;
444                         }
445                 }
446         }
447
448         /* standard not found */
449         if (index == vpif_ch_params_count)
450                 return -EINVAL;
451
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;
459
460         return 0;
461 }
462
463 /**
464  * vpif_calculate_offsets : This function calculates buffers offsets
465  * @ch : ptr to channel object
466  *
467  * This function calculates buffer offsets for Y and C in the top and
468  * bottom field
469  */
470 static void vpif_calculate_offsets(struct channel_obj *ch)
471 {
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;
477
478         vpif_dbg(2, debug, "vpif_calculate_offsets\n");
479
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;
487
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];
492
493         hpitch = common->fmt.fmt.pix.bytesperline;
494         vpitch = sizeimage / (hpitch * 2);
495
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;
515         }
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;
521
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;
533         }
534
535         ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
536 }
537
538 /**
539  * vpif_config_format: configure default frame format in the device
540  * ch : ptr to channel object
541  */
542 static void vpif_config_format(struct channel_obj *ch)
543 {
544         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
545
546         vpif_dbg(2, debug, "vpif_config_format\n");
547
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;
553
554         common->fmt.fmt.pix.sizeimage
555             = config_params.channel_bufsize[ch->channel_id];
556
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;
562 }
563
564 /**
565  * vpif_get_default_field() - Get default field type based on interface
566  * @vpif_params - ptr to vpif params
567  */
568 static inline enum v4l2_field vpif_get_default_field(
569                                 struct vpif_interface *iface)
570 {
571         return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
572                                                 V4L2_FIELD_INTERLACED;
573 }
574
575 /**
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
580  *
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.
584  */
585 static int vpif_check_format(struct channel_obj *ch,
586                              struct v4l2_pix_format *pixfmt,
587                              int update)
588 {
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;
594
595         vpif_dbg(2, debug, "vpif_check_format\n");
596         /**
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
600          */
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;
606                         }
607                         pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
608                 }
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;
614                         }
615                         pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
616                 }
617         }
618
619         if (!(VPIF_VALID_FIELD(field))) {
620                 if (!update) {
621                         vpif_dbg(2, debug, "invalid field format\n");
622                         goto exit;
623                 }
624                 /**
625                  * By default use FIELD_NONE for RAW Bayer capture
626                  * and FIELD_INTERLACED for other interfaces
627                  */
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);
632
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;
639                 }
640                 hpitch = vpif_params->std_info.width;
641         }
642
643         if (V4L2_MEMORY_USERPTR == common->memory)
644                 sizeimage = pixfmt->sizeimage;
645         else
646                 sizeimage = config_params.channel_bufsize[ch->channel_id];
647
648         vpitch = sizeimage / (hpitch * 2);
649
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;
655                 }
656                 vpitch = vpif_params->std_info.height;
657         }
658
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;
664                 }
665                 /* adjust to next 8 byte boundary */
666                 hpitch = (((hpitch + 7) / 8) * 8);
667         }
668         /* if update is set, modify the bytesperline and sizeimage */
669         if (update) {
670                 pixfmt->bytesperline = hpitch;
671                 pixfmt->sizeimage = hpitch * vpitch * 2;
672         }
673         /**
674          * Image width and height is always based on current standard width and
675          * height
676          */
677         pixfmt->width = common->fmt.fmt.pix.width;
678         pixfmt->height = common->fmt.fmt.pix.height;
679         return 0;
680 exit:
681         return ret;
682 }
683
684 /**
685  * vpif_config_addr() - function to configure buffer address in vpif
686  * @ch - channel ptr
687  * @muxmode - channel mux mode
688  */
689 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
690 {
691         struct common_obj *common;
692
693         vpif_dbg(2, debug, "vpif_config_addr\n");
694
695         common = &(ch->common[VPIF_VIDEO_INDEX]);
696
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;
703 }
704
705 /**
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
709  */
710 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
711 {
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]);
716
717         vpif_dbg(2, debug, "vpif_mmap\n");
718
719         return videobuf_mmap_mapper(&common->buffer_queue, vma);
720 }
721
722 /**
723  * vpif_poll: It is used for select/poll system call
724  * @filep: file pointer
725  * @wait: poll table to wait
726  */
727 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
728 {
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]);
732
733         vpif_dbg(2, debug, "vpif_poll\n");
734
735         if (common->started)
736                 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
737         return 0;
738 }
739
740 /**
741  * vpif_open : vpif open handler
742  * @filep: file ptr
743  *
744  * It creates object of file handle structure and stores it in private_data
745  * member of filepointer
746  */
747 static int vpif_open(struct file *filep)
748 {
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;
756
757         vpif_dbg(2, debug, "vpif_open\n");
758
759         ch = video_get_drvdata(vdev);
760
761         vid_ch = &ch->video;
762         common = &ch->common[VPIF_VIDEO_INDEX];
763
764         if (NULL == ch->curr_subdev_info) {
765                 /**
766                  * search through the sub device to see a registered
767                  * sub device and make it as current sub device
768                  */
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;
776                         }
777                 }
778                 if (i == config->subdev_count) {
779                         vpif_err("No sub device registered\n");
780                         return -ENOENT;
781                 }
782         }
783
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;
789         }
790
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));
800         }
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;
809 }
810
811 /**
812  * vpif_release : function to clean up file close
813  * @filep: file pointer
814  *
815  * This function deletes buffer queue, frees the buffers and the vpfe file
816  * handle
817  */
818 static int vpif_release(struct file *filep)
819 {
820         struct vpif_fh *fh = filep->private_data;
821         struct channel_obj *ch = fh->channel;
822         struct common_obj *common;
823
824         vpif_dbg(2, debug, "vpif_release\n");
825
826         common = &ch->common[VPIF_VIDEO_INDEX];
827
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);
836                 }
837                 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
838                     (2 == common->started)) {
839                         enable_channel1(0);
840                         channel1_intr_enable(0);
841                 }
842                 common->started = 0;
843                 /* Free buffers allocated */
844                 videobuf_queue_cancel(&common->buffer_queue);
845                 videobuf_mmap_free(&common->buffer_queue);
846         }
847
848         /* Decrement channel usrs counter */
849         ch->usrs--;
850
851         /* Close the priority */
852         v4l2_prio_close(&ch->prio, fh->prio);
853
854         if (fh->initialized)
855                 ch->initialized = 0;
856
857         filep->private_data = NULL;
858         kfree(fh);
859         return 0;
860 }
861
862 /**
863  * vpif_reqbufs() - request buffer handler
864  * @file: file ptr
865  * @priv: file handle
866  * @reqbuf: request buffer structure ptr
867  */
868 static int vpif_reqbufs(struct file *file, void *priv,
869                         struct v4l2_requestbuffers *reqbuf)
870 {
871         struct vpif_fh *fh = priv;
872         struct channel_obj *ch = fh->channel;
873         struct common_obj *common;
874         u8 index = 0;
875
876         vpif_dbg(2, debug, "vpif_reqbufs\n");
877
878         /**
879          * This file handle has not initialized the channel,
880          * It is not allowed to do settings
881          */
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;
887                 }
888         }
889
890         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
891                 return -EINVAL;
892
893         index = VPIF_VIDEO_INDEX;
894
895         common = &ch->common[index];
896
897         if (0 != common->io_usrs)
898                 return -EBUSY;
899
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);
908
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);
916
917         /* Allocate buffers */
918         return videobuf_reqbufs(&common->buffer_queue, reqbuf);
919 }
920
921 /**
922  * vpif_querybuf() - query buffer handler
923  * @file: file ptr
924  * @priv: file handle
925  * @buf: v4l2 buffer structure ptr
926  */
927 static int vpif_querybuf(struct file *file, void *priv,
928                                 struct v4l2_buffer *buf)
929 {
930         struct vpif_fh *fh = priv;
931         struct channel_obj *ch = fh->channel;
932         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
933
934         vpif_dbg(2, debug, "vpif_querybuf\n");
935
936         if (common->fmt.type != buf->type)
937                 return -EINVAL;
938
939         if (common->memory != V4L2_MEMORY_MMAP) {
940                 vpif_dbg(1, debug, "Invalid memory\n");
941                 return -EINVAL;
942         }
943
944         return videobuf_querybuf(&common->buffer_queue, buf);
945 }
946
947 /**
948  * vpif_qbuf() - query buffer handler
949  * @file: file ptr
950  * @priv: file handle
951  * @buf: v4l2 buffer structure ptr
952  */
953 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
954 {
955
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;
964
965         vpif_dbg(2, debug, "vpif_qbuf\n");
966
967         if (common->fmt.type != tbuf.type) {
968                 vpif_err("invalid buffer type\n");
969                 return -EINVAL;
970         }
971
972         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
973                 vpif_err("fh io not allowed \n");
974                 return -EACCES;
975         }
976
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);
982
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];
986
987         if ((buf1->state == VIDEOBUF_QUEUED) ||
988             (buf1->state == VIDEOBUF_ACTIVE)) {
989                 vpif_err("invalid state\n");
990                 goto qbuf_exit;
991         }
992
993         switch (buf1->memory) {
994         case V4L2_MEMORY_MMAP:
995                 if (buf1->baddr == 0)
996                         goto qbuf_exit;
997                 break;
998
999         case V4L2_MEMORY_USERPTR:
1000                 if (tbuf.length < buf1->bsize)
1001                         goto qbuf_exit;
1002
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;
1007                 }
1008                 break;
1009
1010         default:
1011                 goto qbuf_exit;
1012         }
1013
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;
1020         }
1021
1022         buf1->state = VIDEOBUF_ACTIVE;
1023
1024         if (V4L2_MEMORY_USERPTR == common->memory)
1025                 addr = buf1->boff;
1026         else
1027                 addr = videobuf_to_dma_contig(buf1);
1028
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);
1034
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;
1039
1040 qbuf_exit:
1041         mutex_unlock(&common->buffer_queue.vb_lock);
1042         return -EINVAL;
1043 }
1044
1045 /**
1046  * vpif_dqbuf() - query buffer handler
1047  * @file: file ptr
1048  * @priv: file handle
1049  * @buf: v4l2 buffer structure ptr
1050  */
1051 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1052 {
1053         struct vpif_fh *fh = priv;
1054         struct channel_obj *ch = fh->channel;
1055         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1056
1057         vpif_dbg(2, debug, "vpif_dqbuf\n");
1058
1059         return videobuf_dqbuf(&common->buffer_queue, buf,
1060                                         file->f_flags & O_NONBLOCK);
1061 }
1062
1063 /**
1064  * vpif_streamon() - streamon handler
1065  * @file: file ptr
1066  * @priv: file handle
1067  * @buftype: v4l2 buffer type
1068  */
1069 static int vpif_streamon(struct file *file, void *priv,
1070                                 enum v4l2_buf_type buftype)
1071 {
1072
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;
1081
1082         vpif_dbg(2, debug, "vpif_streamon\n");
1083
1084         vpif = &ch->vpifparams;
1085
1086         if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1087                 vpif_dbg(1, debug, "buffer type not supported\n");
1088                 return -EINVAL;
1089         }
1090
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;
1095         }
1096
1097         /* If Streaming is already started, return error */
1098         if (common->started) {
1099                 vpif_dbg(1, debug, "channel->started\n");
1100                 return -EBUSY;
1101         }
1102
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;
1110         }
1111
1112         ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1113         if (ret)
1114                 return ret;
1115
1116         /* Enable streamon on the sub device */
1117         ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1118                                 s_stream, 1);
1119
1120         if (ret && (ret != -ENOIOCTLCMD)) {
1121                 vpif_dbg(1, debug, "stream on failed in subdev\n");
1122                 return ret;
1123         }
1124
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;
1130         }
1131
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;
1137         }
1138
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;
1143
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;
1151
1152         if (V4L2_MEMORY_USERPTR == common->memory)
1153                 addr = common->cur_frm->boff;
1154         else
1155                 addr = videobuf_to_dma_contig(common->cur_frm);
1156
1157         /* Calculate the offset for Y and C data in the buffer */
1158         vpif_calculate_offsets(ch);
1159
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;
1168         }
1169
1170         /* configure 1 or 2 channel mode */
1171         ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1172
1173         if (ret < 0) {
1174                 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1175                 goto exit;
1176         }
1177
1178         /* Call vpif_set_params function to set the parameters and addresses */
1179         ret = vpif_set_video_params(vpif, ch->channel_id);
1180
1181         if (ret < 0) {
1182                 vpif_dbg(1, debug, "can't set video params\n");
1183                 goto exit;
1184         }
1185
1186         common->started = ret;
1187         vpif_config_addr(ch, ret);
1188
1189         common->set_addr(addr + common->ytop_off,
1190                          addr + common->ybtm_off,
1191                          addr + common->ctop_off,
1192                          addr + common->cbtm_off);
1193
1194         /**
1195          * Set interrupt for both the fields in VPIF Register enable channel in
1196          * VPIF register
1197          */
1198         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1199                 channel0_intr_assert();
1200                 channel0_intr_enable(1);
1201                 enable_channel0(1);
1202         }
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);
1208         }
1209         channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1210         return ret;
1211
1212 exit:
1213         videobuf_streamoff(&common->buffer_queue);
1214         return ret;
1215 }
1216
1217 /**
1218  * vpif_streamoff() - streamoff handler
1219  * @file: file ptr
1220  * @priv: file handle
1221  * @buftype: v4l2 buffer type
1222  */
1223 static int vpif_streamoff(struct file *file, void *priv,
1224                                 enum v4l2_buf_type buftype)
1225 {
1226
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;
1231
1232         vpif_dbg(2, debug, "vpif_streamoff\n");
1233
1234         if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1235                 vpif_dbg(1, debug, "buffer type not supported\n");
1236                 return -EINVAL;
1237         }
1238
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;
1243         }
1244
1245         /* If streaming is not started, return error */
1246         if (!common->started) {
1247                 vpif_dbg(1, debug, "channel->started\n");
1248                 return -EINVAL;
1249         }
1250
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);
1258         }
1259
1260         common->started = 0;
1261
1262         ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1263                                 s_stream, 0);
1264
1265         if (ret && (ret != -ENOIOCTLCMD))
1266                 vpif_dbg(1, debug, "stream off failed in subdev\n");
1267
1268         return videobuf_streamoff(&common->buffer_queue);
1269 }
1270
1271 /**
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
1277  *
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
1281  */
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)
1287 {
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;
1292
1293         vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1294
1295         chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1296
1297         /**
1298          * search through the inputs to find the sub device supporting
1299          * the input
1300          */
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;
1306                 }
1307         }
1308
1309         /* if reached maximum. return null */
1310         if (i == chan_cfg->input_count || (NULL == subdev_name))
1311                 return subdev_info;
1312
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;
1318         }
1319
1320         if (i == vpif_cfg->subdev_count)
1321                 return subdev_info;
1322
1323         /* check if the sub device is registered */
1324         if (NULL == vpif_obj.sd[i])
1325                 return NULL;
1326
1327         *sub_device_index = i;
1328         return subdev_info;
1329 }
1330
1331 /**
1332  * vpif_querystd() - querystd handler
1333  * @file: file ptr
1334  * @priv: file handle
1335  * @std_id: ptr to std id
1336  *
1337  * This function is called to detect standard at the selected input
1338  */
1339 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1340 {
1341         struct vpif_fh *fh = priv;
1342         struct channel_obj *ch = fh->channel;
1343         int ret = 0;
1344
1345         vpif_dbg(2, debug, "vpif_querystd\n");
1346
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");
1352
1353         return ret;
1354 }
1355
1356 /**
1357  * vpif_g_std() - get STD handler
1358  * @file: file ptr
1359  * @priv: file handle
1360  * @std_id: ptr to std id
1361  */
1362 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1363 {
1364         struct vpif_fh *fh = priv;
1365         struct channel_obj *ch = fh->channel;
1366
1367         vpif_dbg(2, debug, "vpif_g_std\n");
1368
1369         *std = ch->video.stdid;
1370         return 0;
1371 }
1372
1373 /**
1374  * vpif_s_std() - set STD handler
1375  * @file: file ptr
1376  * @priv: file handle
1377  * @std_id: ptr to std id
1378  */
1379 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1380 {
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;
1385
1386         vpif_dbg(2, debug, "vpif_s_std\n");
1387
1388         if (common->started) {
1389                 vpif_err("streaming in progress\n");
1390                 return -EBUSY;
1391         }
1392
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;
1398                 }
1399         }
1400
1401         ret = v4l2_prio_check(&ch->prio, fh->prio);
1402         if (0 != ret)
1403                 return ret;
1404
1405         fh->initialized = 1;
1406
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));
1411
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;
1416         }
1417
1418         /* Configure the default format information */
1419         vpif_config_format(ch);
1420
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;
1427 }
1428
1429 /**
1430  * vpif_enum_input() - ENUMINPUT handler
1431  * @file: file ptr
1432  * @priv: file handle
1433  * @input: ptr to input structure
1434  */
1435 static int vpif_enum_input(struct file *file, void *priv,
1436                                 struct v4l2_input *input)
1437 {
1438
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;
1443
1444         chan_cfg = &config->chan_config[ch->channel_id];
1445
1446         if (input->index >= chan_cfg->input_count) {
1447                 vpif_dbg(1, debug, "Invalid input index\n");
1448                 return -EINVAL;
1449         }
1450
1451         memcpy(input, &chan_cfg->inputs[input->index].input,
1452                 sizeof(*input));
1453         return 0;
1454 }
1455
1456 /**
1457  * vpif_g_input() - Get INPUT handler
1458  * @file: file ptr
1459  * @priv: file handle
1460  * @index: ptr to input index
1461  */
1462 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1463 {
1464         struct vpif_fh *fh = priv;
1465         struct channel_obj *ch = fh->channel;
1466         struct video_obj *vid_ch = &ch->video;
1467
1468         *index = vid_ch->input_idx;
1469
1470         return 0;
1471 }
1472
1473 /**
1474  * vpif_s_input() - Set INPUT handler
1475  * @file: file ptr
1476  * @priv: file handle
1477  * @index: input index
1478  */
1479 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1480 {
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;
1490
1491         chan_cfg = &config->chan_config[ch->channel_id];
1492
1493         if (common->started) {
1494                 vpif_err("Streaming in progress\n");
1495                 return -EBUSY;
1496         }
1497
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;
1503                 }
1504         }
1505
1506         ret = v4l2_prio_check(&ch->prio, fh->prio);
1507         if (0 != ret)
1508                 return ret;
1509
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;
1517         }
1518
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;
1528                 }
1529         }
1530
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;
1539                 }
1540         }
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;
1546
1547         /* update tvnorms from the sub device input info */
1548         ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1549         return ret;
1550 }
1551
1552 /**
1553  * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1554  * @file: file ptr
1555  * @priv: file handle
1556  * @index: input index
1557  */
1558 static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
1559                                         struct v4l2_fmtdesc *fmt)
1560 {
1561         struct vpif_fh *fh = priv;
1562         struct channel_obj *ch = fh->channel;
1563
1564         if (fmt->index != 0) {
1565                 vpif_dbg(1, debug, "Invalid format index\n");
1566                 return -EINVAL;
1567         }
1568
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;
1578         }
1579         return 0;
1580 }
1581
1582 /**
1583  * vpif_try_fmt_vid_cap() - TRY_FMT handler
1584  * @file: file ptr
1585  * @priv: file handle
1586  * @fmt: ptr to v4l2 format structure
1587  */
1588 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1589                                 struct v4l2_format *fmt)
1590 {
1591         struct vpif_fh *fh = priv;
1592         struct channel_obj *ch = fh->channel;
1593         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1594
1595         return vpif_check_format(ch, pixfmt, 1);
1596 }
1597
1598
1599 /**
1600  * vpif_g_fmt_vid_cap() - Set INPUT handler
1601  * @file: file ptr
1602  * @priv: file handle
1603  * @fmt: ptr to v4l2 format structure
1604  */
1605 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1606                                 struct v4l2_format *fmt)
1607 {
1608         struct vpif_fh *fh = priv;
1609         struct channel_obj *ch = fh->channel;
1610         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1611
1612         /* Check the validity of the buffer type */
1613         if (common->fmt.type != fmt->type)
1614                 return -EINVAL;
1615
1616         /* Fill in the information about format */
1617         *fmt = common->fmt;
1618         return 0;
1619 }
1620
1621 /**
1622  * vpif_s_fmt_vid_cap() - Set FMT handler
1623  * @file: file ptr
1624  * @priv: file handle
1625  * @fmt: ptr to v4l2 format structure
1626  */
1627 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1628                                 struct v4l2_format *fmt)
1629 {
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;
1635
1636         vpif_dbg(2, debug, "%s\n", __func__);
1637
1638         /* If streaming is started, return error */
1639         if (common->started) {
1640                 vpif_dbg(1, debug, "Streaming is started\n");
1641                 return -EBUSY;
1642         }
1643
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;
1649                 }
1650         }
1651
1652         ret = v4l2_prio_check(&ch->prio, fh->prio);
1653         if (0 != ret)
1654                 return ret;
1655
1656         fh->initialized = 1;
1657
1658         pixfmt = &fmt->fmt.pix;
1659         /* Check for valid field format */
1660         ret = vpif_check_format(ch, pixfmt, 0);
1661
1662         if (ret)
1663                 return ret;
1664         /* store the format in the channel object */
1665         common->fmt = *fmt;
1666         return 0;
1667 }
1668
1669 /**
1670  * vpif_querycap() - QUERYCAP handler
1671  * @file: file ptr
1672  * @priv: file handle
1673  * @cap: ptr to v4l2_capability structure
1674  */
1675 static int vpif_querycap(struct file *file, void  *priv,
1676                                 struct v4l2_capability *cap)
1677 {
1678         struct vpif_capture_config *config = vpif_dev->platform_data;
1679
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));
1684
1685         return 0;
1686 }
1687
1688 /**
1689  * vpif_g_priority() - get priority handler
1690  * @file: file ptr
1691  * @priv: file handle
1692  * @prio: ptr to v4l2_priority structure
1693  */
1694 static int vpif_g_priority(struct file *file, void *priv,
1695                            enum v4l2_priority *prio)
1696 {
1697         struct vpif_fh *fh = priv;
1698         struct channel_obj *ch = fh->channel;
1699
1700         *prio = v4l2_prio_max(&ch->prio);
1701
1702         return 0;
1703 }
1704
1705 /**
1706  * vpif_s_priority() - set priority handler
1707  * @file: file ptr
1708  * @priv: file handle
1709  * @prio: ptr to v4l2_priority structure
1710  */
1711 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1712 {
1713         struct vpif_fh *fh = priv;
1714         struct channel_obj *ch = fh->channel;
1715
1716         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1717 }
1718
1719 /**
1720  * vpif_cropcap() - cropcap handler
1721  * @file: file ptr
1722  * @priv: file handle
1723  * @crop: ptr to v4l2_cropcap structure
1724  */
1725 static int vpif_cropcap(struct file *file, void *priv,
1726                         struct v4l2_cropcap *crop)
1727 {
1728         struct vpif_fh *fh = priv;
1729         struct channel_obj *ch = fh->channel;
1730         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1731
1732         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1733                 return -EINVAL;
1734
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;
1741 }
1742
1743 /**
1744  * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1745  * @file: file ptr
1746  * @priv: file handle
1747  * @preset: input preset
1748  */
1749 static int vpif_enum_dv_presets(struct file *file, void *priv,
1750                 struct v4l2_dv_enum_preset *preset)
1751 {
1752         struct vpif_fh *fh = priv;
1753         struct channel_obj *ch = fh->channel;
1754
1755         return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1756                         video, enum_dv_presets, preset);
1757 }
1758
1759 /**
1760  * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1761  * @file: file ptr
1762  * @priv: file handle
1763  * @preset: input preset
1764  */
1765 static int vpif_query_dv_preset(struct file *file, void *priv,
1766                 struct v4l2_dv_preset *preset)
1767 {
1768         struct vpif_fh *fh = priv;
1769         struct channel_obj *ch = fh->channel;
1770
1771         return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1772                        video, query_dv_preset, preset);
1773 }
1774 /**
1775  * vpif_s_dv_presets() - S_DV_PRESETS handler
1776  * @file: file ptr
1777  * @priv: file handle
1778  * @preset: input preset
1779  */
1780 static int vpif_s_dv_preset(struct file *file, void *priv,
1781                 struct v4l2_dv_preset *preset)
1782 {
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;
1787
1788         if (common->started) {
1789                 vpif_dbg(1, debug, "streaming in progress\n");
1790                 return -EBUSY;
1791         }
1792
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;
1798                 }
1799         }
1800
1801         ret = v4l2_prio_check(&ch->prio, fh->prio);
1802         if (ret)
1803                 return ret;
1804
1805         fh->initialized = 1;
1806
1807         /* Call encoder subdevice function to set the standard */
1808         if (mutex_lock_interruptible(&common->lock))
1809                 return -ERESTARTSYS;
1810
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));
1814
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);
1822
1823                 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1824                                 video, s_dv_preset, preset);
1825         }
1826
1827         mutex_unlock(&common->lock);
1828
1829         return ret;
1830 }
1831 /**
1832  * vpif_g_dv_presets() - G_DV_PRESETS handler
1833  * @file: file ptr
1834  * @priv: file handle
1835  * @preset: input preset
1836  */
1837 static int vpif_g_dv_preset(struct file *file, void *priv,
1838                 struct v4l2_dv_preset *preset)
1839 {
1840         struct vpif_fh *fh = priv;
1841         struct channel_obj *ch = fh->channel;
1842
1843         preset->preset = ch->video.dv_preset;
1844
1845         return 0;
1846 }
1847
1848 /**
1849  * vpif_s_dv_timings() - S_DV_TIMINGS handler
1850  * @file: file ptr
1851  * @priv: file handle
1852  * @timings: digital video timings
1853  */
1854 static int vpif_s_dv_timings(struct file *file, void *priv,
1855                 struct v4l2_dv_timings *timings)
1856 {
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;
1864
1865         if (timings->type != V4L2_DV_BT_656_1120) {
1866                 vpif_dbg(2, debug, "Timing type not defined\n");
1867                 return -EINVAL;
1868         }
1869
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;
1877         }
1878         if (ret < 0) {
1879                 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1880                 return ret;
1881         }
1882
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;
1896         }
1897
1898         *bt = timings->bt;
1899
1900         /* Configure video port timings */
1901
1902         std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1903                 bt->hsync - 8;
1904         std_info->sav2eav = bt->width;
1905
1906         std_info->l1 = 1;
1907         std_info->l3 = bt->vsync + bt->vbackporch + 1;
1908
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;
1926                 }
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);
1931         }
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;
1942
1943         vid_ch->stdid = 0;
1944         vid_ch->dv_preset = V4L2_DV_INVALID;
1945         return 0;
1946 }
1947
1948 /**
1949  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1950  * @file: file ptr
1951  * @priv: file handle
1952  * @timings: digital video timings
1953  */
1954 static int vpif_g_dv_timings(struct file *file, void *priv,
1955                 struct v4l2_dv_timings *timings)
1956 {
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;
1961
1962         timings->bt = *bt;
1963
1964         return 0;
1965 }
1966
1967 /*
1968  * vpif_g_chip_ident() - Identify the chip
1969  * @file: file ptr
1970  * @priv: file handle
1971  * @chip: chip identity
1972  *
1973  * Returns zero or -EINVAL if read operations fails.
1974  */
1975 static int vpif_g_chip_ident(struct file *file, void *priv,
1976                 struct v4l2_dbg_chip_ident *chip)
1977 {
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;
1984         }
1985
1986         return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1987                         g_chip_ident, chip);
1988 }
1989
1990 #ifdef CONFIG_VIDEO_ADV_DEBUG
1991 /*
1992  * vpif_dbg_g_register() - Read register
1993  * @file: file ptr
1994  * @priv: file handle
1995  * @reg: register to be read
1996  *
1997  * Debugging only
1998  * Returns zero or -EINVAL if read operations fails.
1999  */
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;
2004
2005         return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2006                         g_register, reg);
2007 }
2008
2009 /*
2010  * vpif_dbg_s_register() - Write to register
2011  * @file: file ptr
2012  * @priv: file handle
2013  * @reg: register to be modified
2014  *
2015  * Debugging only
2016  * Returns zero or -EINVAL if write operations fails.
2017  */
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;
2022
2023         return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2024                         s_register, reg);
2025 }
2026 #endif
2027
2028 /*
2029  * vpif_log_status() - Status information
2030  * @file: file ptr
2031  * @priv: file handle
2032  *
2033  * Returns zero.
2034  */
2035 static int vpif_log_status(struct file *filep, void *priv)
2036 {
2037         /* status for sub devices */
2038         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
2039
2040         return 0;
2041 }
2042
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,
2077 };
2078
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
2087 };
2088
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,
2095 };
2096
2097 /**
2098  * initialize_vpif() - Initialize vpif data structures
2099  *
2100  * Allocate memory for data structures and initialize them
2101  */
2102 static int initialize_vpif(void)
2103 {
2104         int err = 0, i, j;
2105         int free_channel_objects_index;
2106
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;
2114
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];
2122
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;
2128         }
2129         if (ch1_numbuffers) {
2130                 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2131                     = ch1_bufsize;
2132         }
2133
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;
2143                 }
2144         }
2145         return 0;
2146
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;
2151 }
2152
2153 /**
2154  * vpif_probe : This function probes the vpif capture driver
2155  * @pdev: platform device pointer
2156  *
2157  * This creates device entries by register itself to the V4L2 driver and
2158  * initializes fields of each channel objects
2159  */
2160 static __init int vpif_probe(struct platform_device *pdev)
2161 {
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;
2171
2172         vpif_dev = &pdev->dev;
2173
2174         err = initialize_vpif();
2175         if (err) {
2176                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2177                 return err;
2178         }
2179
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;
2184         }
2185
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;
2195                         }
2196                 }
2197                 k++;
2198         }
2199
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);
2209                         }
2210                         err = -ENOMEM;
2211                         goto vpif_dev_alloc_err;
2212                 }
2213
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;
2223         }
2224
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;
2238
2239                 video_set_drvdata(ch->video_dev, ch);
2240
2241         }
2242
2243         i2c_adap = i2c_get_adapter(1);
2244         config = pdev->dev.platform_data;
2245
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;
2253         }
2254
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);
2262
2263                 if (!vpif_obj.sd[i]) {
2264                         vpif_err("Error registering v4l2 subdevice\n");
2265                         goto probe_subdev_out;
2266                 }
2267                 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2268                           subdevdata->name);
2269
2270                 if (vpif_obj.sd[i])
2271                         vpif_obj.sd[i]->grp_id = 1 << i;
2272         }
2273
2274         v4l2_info(&vpif_obj.v4l2_dev,
2275                         "DM646x VPIF capture driver initialized\n");
2276         return 0;
2277
2278 probe_subdev_out:
2279         /* free sub devices memory */
2280         kfree(vpif_obj.sd);
2281
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);
2289         }
2290
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;
2295
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));
2300
2301                 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2302                 if (res)
2303                         i = res->end;
2304         }
2305         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2306         return err;
2307 }
2308
2309 /**
2310  * vpif_remove() - driver remove handler
2311  * @device: ptr to platform device structure
2312  *
2313  * The vidoe device is unregistered
2314  */
2315 static int vpif_remove(struct platform_device *device)
2316 {
2317         int i;
2318         struct channel_obj *ch;
2319
2320         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2321
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);
2328         }
2329         return 0;
2330 }
2331
2332 /**
2333  * vpif_suspend: vpif device suspend
2334  *
2335  * TODO: Add suspend code here
2336  */
2337 static int
2338 vpif_suspend(struct device *dev)
2339 {
2340         return -1;
2341 }
2342
2343 /**
2344  * vpif_resume: vpif device suspend
2345  *
2346  * TODO: Add resume code here
2347  */
2348 static int
2349 vpif_resume(struct device *dev)
2350 {
2351         return -1;
2352 }
2353
2354 static const struct dev_pm_ops vpif_dev_pm_ops = {
2355         .suspend = vpif_suspend,
2356         .resume = vpif_resume,
2357 };
2358
2359 static __refdata struct platform_driver vpif_driver = {
2360         .driver = {
2361                 .name   = "vpif_capture",
2362                 .owner  = THIS_MODULE,
2363                 .pm = &vpif_dev_pm_ops,
2364         },
2365         .probe = vpif_probe,
2366         .remove = vpif_remove,
2367 };
2368
2369 /**
2370  * vpif_init: initialize the vpif driver
2371  *
2372  * This function registers device and driver to the kernel, requests irq
2373  * handler and allocates memory
2374  * for channel objects
2375  */
2376 static __init int vpif_init(void)
2377 {
2378         return platform_driver_register(&vpif_driver);
2379 }
2380
2381 /**
2382  * vpif_cleanup : This function clean up the vpif capture resources
2383  *
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.
2387  */
2388 static void vpif_cleanup(void)
2389 {
2390         struct platform_device *pdev;
2391         struct resource *res;
2392         int irq_num;
2393         int i = 0;
2394
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++;
2401         }
2402
2403         platform_driver_unregister(&vpif_driver);
2404
2405         kfree(vpif_obj.sd);
2406         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2407                 kfree(vpif_obj.dev[i]);
2408 }
2409
2410 /* Function for module initialization and cleanup */
2411 module_init(vpif_init);
2412 module_exit(vpif_cleanup);