]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/davinci/vpif_capture.c
V4L/DVB: drivers/media/video: Adjust confusing if indentation
[net-next-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/version.h>
37 #include <linux/slab.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.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
47 #define vpif_err(fmt, arg...)   v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
48 #define vpif_dbg(level, debug, fmt, arg...)     \
49                 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
50
51 static int debug = 1;
52 static u32 ch0_numbuffers = 3;
53 static u32 ch1_numbuffers = 3;
54 static u32 ch0_bufsize = 1920 * 1080 * 2;
55 static u32 ch1_bufsize = 720 * 576 * 2;
56
57 module_param(debug, int, 0644);
58 module_param(ch0_numbuffers, uint, S_IRUGO);
59 module_param(ch1_numbuffers, uint, S_IRUGO);
60 module_param(ch0_bufsize, uint, S_IRUGO);
61 module_param(ch1_bufsize, uint, S_IRUGO);
62
63 MODULE_PARM_DESC(debug, "Debug level 0-1");
64 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
65 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
66 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
67 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
68
69 static struct vpif_config_params config_params = {
70         .min_numbuffers = 3,
71         .numbuffers[0] = 3,
72         .numbuffers[1] = 3,
73         .min_bufsize[0] = 720 * 480 * 2,
74         .min_bufsize[1] = 720 * 480 * 2,
75         .channel_bufsize[0] = 1920 * 1080 * 2,
76         .channel_bufsize[1] = 720 * 576 * 2,
77 };
78
79 /* global variables */
80 static struct vpif_device vpif_obj = { {NULL} };
81 static struct device *vpif_dev;
82
83 /**
84  * ch_params: video standard configuration parameters for vpif
85  */
86 static const struct vpif_channel_config_params ch_params[] = {
87         {
88                 "NTSC_M", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
89                 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
90         },
91         {
92                 "PAL_BDGHIK", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
93                 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
94         },
95 };
96
97 /**
98  * vpif_uservirt_to_phys : translate user/virtual address to phy address
99  * @virtp: user/virtual address
100  *
101  * This inline function is used to convert user space virtual address to
102  * physical address.
103  */
104 static inline u32 vpif_uservirt_to_phys(u32 virtp)
105 {
106         unsigned long physp = 0;
107         struct mm_struct *mm = current->mm;
108         struct vm_area_struct *vma;
109
110         vma = find_vma(mm, virtp);
111
112         /* For kernel direct-mapped memory, take the easy way */
113         if (virtp >= PAGE_OFFSET)
114                 physp = virt_to_phys((void *)virtp);
115         else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
116                 /**
117                  * this will catch, kernel-allocated, mmaped-to-usermode
118                  * addresses
119                  */
120                 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
121         else {
122                 /* otherwise, use get_user_pages() for general userland pages */
123                 int res, nr_pages = 1;
124                         struct page *pages;
125
126                 down_read(&current->mm->mmap_sem);
127
128                 res = get_user_pages(current, current->mm,
129                                      virtp, nr_pages, 1, 0, &pages, NULL);
130                 up_read(&current->mm->mmap_sem);
131
132                 if (res == nr_pages)
133                         physp = __pa(page_address(&pages[0]) +
134                                      (virtp & ~PAGE_MASK));
135                 else {
136                         vpif_err("get_user_pages failed\n");
137                         return 0;
138                 }
139         }
140         return physp;
141 }
142
143 /**
144  * buffer_prepare :  callback function for buffer prepare
145  * @q : buffer queue ptr
146  * @vb: ptr to video buffer
147  * @field: field info
148  *
149  * This is the callback function for buffer prepare when videobuf_qbuf()
150  * function is called. The buffer is prepared and user space virtual address
151  * or user address is converted into  physical address
152  */
153 static int vpif_buffer_prepare(struct videobuf_queue *q,
154                                struct videobuf_buffer *vb,
155                                enum v4l2_field field)
156 {
157         /* Get the file handle object and channel object */
158         struct vpif_fh *fh = q->priv_data;
159         struct channel_obj *ch = fh->channel;
160         struct common_obj *common;
161         unsigned long addr;
162
163
164         vpif_dbg(2, debug, "vpif_buffer_prepare\n");
165
166         common = &ch->common[VPIF_VIDEO_INDEX];
167
168         /* If buffer is not initialized, initialize it */
169         if (VIDEOBUF_NEEDS_INIT == vb->state) {
170                 vb->width = common->width;
171                 vb->height = common->height;
172                 vb->size = vb->width * vb->height;
173                 vb->field = field;
174         }
175         vb->state = VIDEOBUF_PREPARED;
176         /**
177          * if user pointer memory mechanism is used, get the physical
178          * address of the buffer
179          */
180         if (V4L2_MEMORY_USERPTR == common->memory) {
181                 if (0 == vb->baddr) {
182                         vpif_dbg(1, debug, "buffer address is 0\n");
183                         return -EINVAL;
184
185                 }
186                 vb->boff = vpif_uservirt_to_phys(vb->baddr);
187                 if (!IS_ALIGNED(vb->boff, 8))
188                         goto exit;
189         }
190
191         addr = vb->boff;
192         if (q->streaming) {
193                 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
194                     !IS_ALIGNED((addr + common->ybtm_off), 8) ||
195                     !IS_ALIGNED((addr + common->ctop_off), 8) ||
196                     !IS_ALIGNED((addr + common->cbtm_off), 8))
197                         goto exit;
198         }
199         return 0;
200 exit:
201         vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
202         return -EINVAL;
203 }
204
205 /**
206  * vpif_buffer_setup : Callback function for buffer setup.
207  * @q: buffer queue ptr
208  * @count: number of buffers
209  * @size: size of the buffer
210  *
211  * This callback function is called when reqbuf() is called to adjust
212  * the buffer count and buffer size
213  */
214 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
215                              unsigned int *size)
216 {
217         /* Get the file handle object and channel object */
218         struct vpif_fh *fh = q->priv_data;
219         struct channel_obj *ch = fh->channel;
220         struct common_obj *common;
221
222         common = &ch->common[VPIF_VIDEO_INDEX];
223
224         vpif_dbg(2, debug, "vpif_buffer_setup\n");
225
226         /* If memory type is not mmap, return */
227         if (V4L2_MEMORY_MMAP != common->memory)
228                 return 0;
229
230         /* Calculate the size of the buffer */
231         *size = config_params.channel_bufsize[ch->channel_id];
232
233         if (*count < config_params.min_numbuffers)
234                 *count = config_params.min_numbuffers;
235         return 0;
236 }
237
238 /**
239  * vpif_buffer_queue : Callback function to add buffer to DMA queue
240  * @q: ptr to videobuf_queue
241  * @vb: ptr to videobuf_buffer
242  */
243 static void vpif_buffer_queue(struct videobuf_queue *q,
244                               struct videobuf_buffer *vb)
245 {
246         /* Get the file handle object and channel object */
247         struct vpif_fh *fh = q->priv_data;
248         struct channel_obj *ch = fh->channel;
249         struct common_obj *common;
250
251         common = &ch->common[VPIF_VIDEO_INDEX];
252
253         vpif_dbg(2, debug, "vpif_buffer_queue\n");
254
255         /* add the buffer to the DMA queue */
256         list_add_tail(&vb->queue, &common->dma_queue);
257         /* Change state of the buffer */
258         vb->state = VIDEOBUF_QUEUED;
259 }
260
261 /**
262  * vpif_buffer_release : Callback function to free buffer
263  * @q: buffer queue ptr
264  * @vb: ptr to video buffer
265  *
266  * This function is called from the videobuf layer to free memory
267  * allocated to  the buffers
268  */
269 static void vpif_buffer_release(struct videobuf_queue *q,
270                                 struct videobuf_buffer *vb)
271 {
272         /* Get the file handle object and channel object */
273         struct vpif_fh *fh = q->priv_data;
274         struct channel_obj *ch = fh->channel;
275         struct common_obj *common;
276
277         common = &ch->common[VPIF_VIDEO_INDEX];
278
279         videobuf_dma_contig_free(q, vb);
280         vb->state = VIDEOBUF_NEEDS_INIT;
281 }
282
283 static struct videobuf_queue_ops video_qops = {
284         .buf_setup = vpif_buffer_setup,
285         .buf_prepare = vpif_buffer_prepare,
286         .buf_queue = vpif_buffer_queue,
287         .buf_release = vpif_buffer_release,
288 };
289
290 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
291         { {1, 1} };
292
293 /**
294  * vpif_process_buffer_complete: process a completed buffer
295  * @common: ptr to common channel object
296  *
297  * This function time stamp the buffer and mark it as DONE. It also
298  * wake up any process waiting on the QUEUE and set the next buffer
299  * as current
300  */
301 static void vpif_process_buffer_complete(struct common_obj *common)
302 {
303         do_gettimeofday(&common->cur_frm->ts);
304         common->cur_frm->state = VIDEOBUF_DONE;
305         wake_up_interruptible(&common->cur_frm->done);
306         /* Make curFrm pointing to nextFrm */
307         common->cur_frm = common->next_frm;
308 }
309
310 /**
311  * vpif_schedule_next_buffer: set next buffer address for capture
312  * @common : ptr to common channel object
313  *
314  * This function will get next buffer from the dma queue and
315  * set the buffer address in the vpif register for capture.
316  * the buffer is marked active
317  */
318 static void vpif_schedule_next_buffer(struct common_obj *common)
319 {
320         unsigned long addr = 0;
321
322         common->next_frm = list_entry(common->dma_queue.next,
323                                      struct videobuf_buffer, queue);
324         /* Remove that buffer from the buffer queue */
325         list_del(&common->next_frm->queue);
326         common->next_frm->state = VIDEOBUF_ACTIVE;
327         if (V4L2_MEMORY_USERPTR == common->memory)
328                 addr = common->next_frm->boff;
329         else
330                 addr = videobuf_to_dma_contig(common->next_frm);
331
332         /* Set top and bottom field addresses in VPIF registers */
333         common->set_addr(addr + common->ytop_off,
334                          addr + common->ybtm_off,
335                          addr + common->ctop_off,
336                          addr + common->cbtm_off);
337 }
338
339 /**
340  * vpif_channel_isr : ISR handler for vpif capture
341  * @irq: irq number
342  * @dev_id: dev_id ptr
343  *
344  * It changes status of the captured buffer, takes next buffer from the queue
345  * and sets its address in VPIF  registers
346  */
347 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
348 {
349         struct vpif_device *dev = &vpif_obj;
350         struct common_obj *common;
351         struct channel_obj *ch;
352         enum v4l2_field field;
353         int channel_id = 0;
354         int fid = -1, i;
355
356         channel_id = *(int *)(dev_id);
357         ch = dev->dev[channel_id];
358
359         field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
360
361         for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
362                 common = &ch->common[i];
363                 /* skip If streaming is not started in this channel */
364                 if (0 == common->started)
365                         continue;
366
367                 /* Check the field format */
368                 if (1 == ch->vpifparams.std_info.frm_fmt) {
369                         /* Progressive mode */
370                         if (list_empty(&common->dma_queue))
371                                 continue;
372
373                         if (!channel_first_int[i][channel_id])
374                                 vpif_process_buffer_complete(common);
375
376                         channel_first_int[i][channel_id] = 0;
377
378                         vpif_schedule_next_buffer(common);
379
380
381                         channel_first_int[i][channel_id] = 0;
382                 } else {
383                         /**
384                          * Interlaced mode. If it is first interrupt, ignore
385                          * it
386                          */
387                         if (channel_first_int[i][channel_id]) {
388                                 channel_first_int[i][channel_id] = 0;
389                                 continue;
390                         }
391                         if (0 == i) {
392                                 ch->field_id ^= 1;
393                                 /* Get field id from VPIF registers */
394                                 fid = vpif_channel_getfid(ch->channel_id);
395                                 if (fid != ch->field_id) {
396                                         /**
397                                          * If field id does not match stored
398                                          * field id, make them in sync
399                                          */
400                                         if (0 == fid)
401                                                 ch->field_id = fid;
402                                         return IRQ_HANDLED;
403                                 }
404                         }
405                         /* device field id and local field id are in sync */
406                         if (0 == fid) {
407                                 /* this is even field */
408                                 if (common->cur_frm == common->next_frm)
409                                         continue;
410
411                                 /* mark the current buffer as done */
412                                 vpif_process_buffer_complete(common);
413                         } else if (1 == fid) {
414                                 /* odd field */
415                                 if (list_empty(&common->dma_queue) ||
416                                     (common->cur_frm != common->next_frm))
417                                         continue;
418
419                                 vpif_schedule_next_buffer(common);
420                         }
421                 }
422         }
423         return IRQ_HANDLED;
424 }
425
426 /**
427  * vpif_update_std_info() - update standard related info
428  * @ch: ptr to channel object
429  *
430  * For a given standard selected by application, update values
431  * in the device data structures
432  */
433 static int vpif_update_std_info(struct channel_obj *ch)
434 {
435         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
436         struct vpif_params *vpifparams = &ch->vpifparams;
437         const struct vpif_channel_config_params *config;
438         struct vpif_channel_config_params *std_info;
439         struct video_obj *vid_ch = &ch->video;
440         int index;
441
442         vpif_dbg(2, debug, "vpif_update_std_info\n");
443
444         std_info = &vpifparams->std_info;
445
446         for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
447                 config = &ch_params[index];
448                 if (config->stdid & vid_ch->stdid) {
449                         memcpy(std_info, config, sizeof(*config));
450                         break;
451                 }
452         }
453
454         /* standard not found */
455         if (index == ARRAY_SIZE(ch_params))
456                 return -EINVAL;
457
458         common->fmt.fmt.pix.width = std_info->width;
459         common->width = std_info->width;
460         common->fmt.fmt.pix.height = std_info->height;
461         common->height = std_info->height;
462         common->fmt.fmt.pix.bytesperline = std_info->width;
463         vpifparams->video_params.hpitch = std_info->width;
464         vpifparams->video_params.storage_mode = std_info->frm_fmt;
465         return 0;
466 }
467
468 /**
469  * vpif_calculate_offsets : This function calculates buffers offsets
470  * @ch : ptr to channel object
471  *
472  * This function calculates buffer offsets for Y and C in the top and
473  * bottom field
474  */
475 static void vpif_calculate_offsets(struct channel_obj *ch)
476 {
477         unsigned int hpitch, vpitch, sizeimage;
478         struct video_obj *vid_ch = &(ch->video);
479         struct vpif_params *vpifparams = &ch->vpifparams;
480         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
481         enum v4l2_field field = common->fmt.fmt.pix.field;
482
483         vpif_dbg(2, debug, "vpif_calculate_offsets\n");
484
485         if (V4L2_FIELD_ANY == field) {
486                 if (vpifparams->std_info.frm_fmt)
487                         vid_ch->buf_field = V4L2_FIELD_NONE;
488                 else
489                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
490         } else
491                 vid_ch->buf_field = common->fmt.fmt.pix.field;
492
493         if (V4L2_MEMORY_USERPTR == common->memory)
494                 sizeimage = common->fmt.fmt.pix.sizeimage;
495         else
496                 sizeimage = config_params.channel_bufsize[ch->channel_id];
497
498         hpitch = common->fmt.fmt.pix.bytesperline;
499         vpitch = sizeimage / (hpitch * 2);
500
501         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
502             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
503                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
504                 common->ytop_off = 0;
505                 common->ybtm_off = hpitch;
506                 common->ctop_off = sizeimage / 2;
507                 common->cbtm_off = sizeimage / 2 + hpitch;
508         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
509                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
510                 common->ytop_off = 0;
511                 common->ybtm_off = sizeimage / 4;
512                 common->ctop_off = sizeimage / 2;
513                 common->cbtm_off = common->ctop_off + sizeimage / 4;
514         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
515                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
516                 common->ybtm_off = 0;
517                 common->ytop_off = sizeimage / 4;
518                 common->cbtm_off = sizeimage / 2;
519                 common->ctop_off = common->cbtm_off + sizeimage / 4;
520         }
521         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
522             (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
523                 vpifparams->video_params.storage_mode = 1;
524         else
525                 vpifparams->video_params.storage_mode = 0;
526
527         if (1 == vpifparams->std_info.frm_fmt)
528                 vpifparams->video_params.hpitch =
529                     common->fmt.fmt.pix.bytesperline;
530         else {
531                 if ((field == V4L2_FIELD_ANY)
532                     || (field == V4L2_FIELD_INTERLACED))
533                         vpifparams->video_params.hpitch =
534                             common->fmt.fmt.pix.bytesperline * 2;
535                 else
536                         vpifparams->video_params.hpitch =
537                             common->fmt.fmt.pix.bytesperline;
538         }
539
540         ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
541 }
542
543 /**
544  * vpif_config_format: configure default frame format in the device
545  * ch : ptr to channel object
546  */
547 static void vpif_config_format(struct channel_obj *ch)
548 {
549         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
550
551         vpif_dbg(2, debug, "vpif_config_format\n");
552
553         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
554         if (config_params.numbuffers[ch->channel_id] == 0)
555                 common->memory = V4L2_MEMORY_USERPTR;
556         else
557                 common->memory = V4L2_MEMORY_MMAP;
558
559         common->fmt.fmt.pix.sizeimage
560             = config_params.channel_bufsize[ch->channel_id];
561
562         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
563                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
564         else
565                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
566         common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
567 }
568
569 /**
570  * vpif_get_default_field() - Get default field type based on interface
571  * @vpif_params - ptr to vpif params
572  */
573 static inline enum v4l2_field vpif_get_default_field(
574                                 struct vpif_interface *iface)
575 {
576         return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
577                                                 V4L2_FIELD_INTERLACED;
578 }
579
580 /**
581  * vpif_check_format()  - check given pixel format for compatibility
582  * @ch - channel  ptr
583  * @pixfmt - Given pixel format
584  * @update - update the values as per hardware requirement
585  *
586  * Check the application pixel format for S_FMT and update the input
587  * values as per hardware limits for TRY_FMT. The default pixel and
588  * field format is selected based on interface type.
589  */
590 static int vpif_check_format(struct channel_obj *ch,
591                              struct v4l2_pix_format *pixfmt,
592                              int update)
593 {
594         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
595         struct vpif_params *vpif_params = &ch->vpifparams;
596         enum v4l2_field field = pixfmt->field;
597         u32 sizeimage, hpitch, vpitch;
598         int ret = -EINVAL;
599
600         vpif_dbg(2, debug, "vpif_check_format\n");
601         /**
602          * first check for the pixel format. If if_type is Raw bayer,
603          * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
604          * V4L2_PIX_FMT_YUV422P is supported
605          */
606         if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
607                 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
608                         if (!update) {
609                                 vpif_dbg(2, debug, "invalid pix format\n");
610                                 goto exit;
611                         }
612                         pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
613                 }
614         } else {
615                 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
616                         if (!update) {
617                                 vpif_dbg(2, debug, "invalid pixel format\n");
618                                 goto exit;
619                         }
620                         pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
621                 }
622         }
623
624         if (!(VPIF_VALID_FIELD(field))) {
625                 if (!update) {
626                         vpif_dbg(2, debug, "invalid field format\n");
627                         goto exit;
628                 }
629                 /**
630                  * By default use FIELD_NONE for RAW Bayer capture
631                  * and FIELD_INTERLACED for other interfaces
632                  */
633                 field = vpif_get_default_field(&vpif_params->iface);
634         } else if (field == V4L2_FIELD_ANY)
635                 /* unsupported field. Use default */
636                 field = vpif_get_default_field(&vpif_params->iface);
637
638         /* validate the hpitch */
639         hpitch = pixfmt->bytesperline;
640         if (hpitch < vpif_params->std_info.width) {
641                 if (!update) {
642                         vpif_dbg(2, debug, "invalid hpitch\n");
643                         goto exit;
644                 }
645                 hpitch = vpif_params->std_info.width;
646         }
647
648         if (V4L2_MEMORY_USERPTR == common->memory)
649                 sizeimage = pixfmt->sizeimage;
650         else
651                 sizeimage = config_params.channel_bufsize[ch->channel_id];
652
653         vpitch = sizeimage / (hpitch * 2);
654
655         /* validate the vpitch */
656         if (vpitch < vpif_params->std_info.height) {
657                 if (!update) {
658                         vpif_dbg(2, debug, "Invalid vpitch\n");
659                         goto exit;
660                 }
661                 vpitch = vpif_params->std_info.height;
662         }
663
664         /* Check for 8 byte alignment */
665         if (!ALIGN(hpitch, 8)) {
666                 if (!update) {
667                         vpif_dbg(2, debug, "invalid pitch alignment\n");
668                         goto exit;
669                 }
670                 /* adjust to next 8 byte boundary */
671                 hpitch = (((hpitch + 7) / 8) * 8);
672         }
673         /* if update is set, modify the bytesperline and sizeimage */
674         if (update) {
675                 pixfmt->bytesperline = hpitch;
676                 pixfmt->sizeimage = hpitch * vpitch * 2;
677         }
678         /**
679          * Image width and height is always based on current standard width and
680          * height
681          */
682         pixfmt->width = common->fmt.fmt.pix.width;
683         pixfmt->height = common->fmt.fmt.pix.height;
684         return 0;
685 exit:
686         return ret;
687 }
688
689 /**
690  * vpif_config_addr() - function to configure buffer address in vpif
691  * @ch - channel ptr
692  * @muxmode - channel mux mode
693  */
694 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
695 {
696         struct common_obj *common;
697
698         vpif_dbg(2, debug, "vpif_config_addr\n");
699
700         common = &(ch->common[VPIF_VIDEO_INDEX]);
701
702         if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
703                 common->set_addr = ch1_set_videobuf_addr;
704         else if (2 == muxmode)
705                 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
706         else
707                 common->set_addr = ch0_set_videobuf_addr;
708 }
709
710 /**
711  * vpfe_mmap : It is used to map kernel space buffers into user spaces
712  * @filep: file pointer
713  * @vma: ptr to vm_area_struct
714  */
715 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
716 {
717         /* Get the channel object and file handle object */
718         struct vpif_fh *fh = filep->private_data;
719         struct channel_obj *ch = fh->channel;
720         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
721
722         vpif_dbg(2, debug, "vpif_mmap\n");
723
724         return videobuf_mmap_mapper(&common->buffer_queue, vma);
725 }
726
727 /**
728  * vpif_poll: It is used for select/poll system call
729  * @filep: file pointer
730  * @wait: poll table to wait
731  */
732 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
733 {
734         int err = 0;
735         struct vpif_fh *fh = filep->private_data;
736         struct channel_obj *channel = fh->channel;
737         struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
738
739         vpif_dbg(2, debug, "vpif_poll\n");
740
741         if (common->started)
742                 err = videobuf_poll_stream(filep, &common->buffer_queue, wait);
743
744         return 0;
745 }
746
747 /**
748  * vpif_open : vpif open handler
749  * @filep: file ptr
750  *
751  * It creates object of file handle structure and stores it in private_data
752  * member of filepointer
753  */
754 static int vpif_open(struct file *filep)
755 {
756         struct vpif_capture_config *config = vpif_dev->platform_data;
757         struct video_device *vdev = video_devdata(filep);
758         struct common_obj *common;
759         struct video_obj *vid_ch;
760         struct channel_obj *ch;
761         struct vpif_fh *fh;
762         int i, ret = 0;
763
764         vpif_dbg(2, debug, "vpif_open\n");
765
766         ch = video_get_drvdata(vdev);
767
768         vid_ch = &ch->video;
769         common = &ch->common[VPIF_VIDEO_INDEX];
770
771         if (mutex_lock_interruptible(&common->lock))
772                 return -ERESTARTSYS;
773
774         if (NULL == ch->curr_subdev_info) {
775                 /**
776                  * search through the sub device to see a registered
777                  * sub device and make it as current sub device
778                  */
779                 for (i = 0; i < config->subdev_count; i++) {
780                         if (vpif_obj.sd[i]) {
781                                 /* the sub device is registered */
782                                 ch->curr_subdev_info = &config->subdev_info[i];
783                                 /* make first input as the current input */
784                                 vid_ch->input_idx = 0;
785                                 break;
786                         }
787                 }
788                 if (i == config->subdev_count) {
789                         vpif_err("No sub device registered\n");
790                         ret = -ENOENT;
791                         goto exit;
792                 }
793         }
794
795         /* Allocate memory for the file handle object */
796         fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
797         if (NULL == fh) {
798                 vpif_err("unable to allocate memory for file handle object\n");
799                 ret = -ENOMEM;
800                 goto exit;
801         }
802
803         /* store pointer to fh in private_data member of filep */
804         filep->private_data = fh;
805         fh->channel = ch;
806         fh->initialized = 0;
807         /* If decoder is not initialized. initialize it */
808         if (!ch->initialized) {
809                 fh->initialized = 1;
810                 ch->initialized = 1;
811                 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
812         }
813         /* Increment channel usrs counter */
814         ch->usrs++;
815         /* Set io_allowed member to false */
816         fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
817         /* Initialize priority of this instance to default priority */
818         fh->prio = V4L2_PRIORITY_UNSET;
819         v4l2_prio_open(&ch->prio, &fh->prio);
820 exit:
821         mutex_unlock(&common->lock);
822         return ret;
823 }
824
825 /**
826  * vpif_release : function to clean up file close
827  * @filep: file pointer
828  *
829  * This function deletes buffer queue, frees the buffers and the vpfe file
830  * handle
831  */
832 static int vpif_release(struct file *filep)
833 {
834         struct vpif_fh *fh = filep->private_data;
835         struct channel_obj *ch = fh->channel;
836         struct common_obj *common;
837
838         vpif_dbg(2, debug, "vpif_release\n");
839
840         common = &ch->common[VPIF_VIDEO_INDEX];
841
842         if (mutex_lock_interruptible(&common->lock))
843                 return -ERESTARTSYS;
844
845         /* if this instance is doing IO */
846         if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
847                 /* Reset io_usrs member of channel object */
848                 common->io_usrs = 0;
849                 /* Disable channel as per its device type and channel id */
850                 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
851                         enable_channel0(0);
852                         channel0_intr_enable(0);
853                 }
854                 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
855                     (2 == common->started)) {
856                         enable_channel1(0);
857                         channel1_intr_enable(0);
858                 }
859                 common->started = 0;
860                 /* Free buffers allocated */
861                 videobuf_queue_cancel(&common->buffer_queue);
862                 videobuf_mmap_free(&common->buffer_queue);
863         }
864
865         /* Decrement channel usrs counter */
866         ch->usrs--;
867
868         /* unlock mutex on channel object */
869         mutex_unlock(&common->lock);
870
871         /* Close the priority */
872         v4l2_prio_close(&ch->prio, fh->prio);
873
874         if (fh->initialized)
875                 ch->initialized = 0;
876
877         filep->private_data = NULL;
878         kfree(fh);
879         return 0;
880 }
881
882 /**
883  * vpif_reqbufs() - request buffer handler
884  * @file: file ptr
885  * @priv: file handle
886  * @reqbuf: request buffer structure ptr
887  */
888 static int vpif_reqbufs(struct file *file, void *priv,
889                         struct v4l2_requestbuffers *reqbuf)
890 {
891         struct vpif_fh *fh = priv;
892         struct channel_obj *ch = fh->channel;
893         struct common_obj *common;
894         u8 index = 0;
895         int ret = 0;
896
897         vpif_dbg(2, debug, "vpif_reqbufs\n");
898
899         /**
900          * This file handle has not initialized the channel,
901          * It is not allowed to do settings
902          */
903         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
904             || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
905                 if (!fh->initialized) {
906                         vpif_dbg(1, debug, "Channel Busy\n");
907                         return -EBUSY;
908                 }
909         }
910
911         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
912                 return -EINVAL;
913
914         index = VPIF_VIDEO_INDEX;
915
916         common = &ch->common[index];
917
918         if (mutex_lock_interruptible(&common->lock))
919                 return -ERESTARTSYS;
920
921         if (0 != common->io_usrs) {
922                 ret = -EBUSY;
923                 goto reqbuf_exit;
924         }
925
926         /* Initialize videobuf queue as per the buffer type */
927         videobuf_queue_dma_contig_init(&common->buffer_queue,
928                                             &video_qops, NULL,
929                                             &common->irqlock,
930                                             reqbuf->type,
931                                             common->fmt.fmt.pix.field,
932                                             sizeof(struct videobuf_buffer), fh);
933
934         /* Set io allowed member of file handle to TRUE */
935         fh->io_allowed[index] = 1;
936         /* Increment io usrs member of channel object to 1 */
937         common->io_usrs = 1;
938         /* Store type of memory requested in channel object */
939         common->memory = reqbuf->memory;
940         INIT_LIST_HEAD(&common->dma_queue);
941
942         /* Allocate buffers */
943         ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
944
945 reqbuf_exit:
946         mutex_unlock(&common->lock);
947         return ret;
948 }
949
950 /**
951  * vpif_querybuf() - query buffer handler
952  * @file: file ptr
953  * @priv: file handle
954  * @buf: v4l2 buffer structure ptr
955  */
956 static int vpif_querybuf(struct file *file, void *priv,
957                                 struct v4l2_buffer *buf)
958 {
959         struct vpif_fh *fh = priv;
960         struct channel_obj *ch = fh->channel;
961         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
962
963         vpif_dbg(2, debug, "vpif_querybuf\n");
964
965         if (common->fmt.type != buf->type)
966                 return -EINVAL;
967
968         if (common->memory != V4L2_MEMORY_MMAP) {
969                 vpif_dbg(1, debug, "Invalid memory\n");
970                 return -EINVAL;
971         }
972
973         return videobuf_querybuf(&common->buffer_queue, buf);
974 }
975
976 /**
977  * vpif_qbuf() - query buffer handler
978  * @file: file ptr
979  * @priv: file handle
980  * @buf: v4l2 buffer structure ptr
981  */
982 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
983 {
984
985         struct vpif_fh *fh = priv;
986         struct channel_obj *ch = fh->channel;
987         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
988         struct v4l2_buffer tbuf = *buf;
989         struct videobuf_buffer *buf1;
990         unsigned long addr = 0;
991         unsigned long flags;
992         int ret = 0;
993
994         vpif_dbg(2, debug, "vpif_qbuf\n");
995
996         if (common->fmt.type != tbuf.type) {
997                 vpif_err("invalid buffer type\n");
998                 return -EINVAL;
999         }
1000
1001         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1002                 vpif_err("fh io not allowed \n");
1003                 return -EACCES;
1004         }
1005
1006         if (!(list_empty(&common->dma_queue)) ||
1007             (common->cur_frm != common->next_frm) ||
1008             !common->started ||
1009             (common->started && (0 == ch->field_id)))
1010                 return videobuf_qbuf(&common->buffer_queue, buf);
1011
1012         /* bufferqueue is empty store buffer address in VPIF registers */
1013         mutex_lock(&common->buffer_queue.vb_lock);
1014         buf1 = common->buffer_queue.bufs[tbuf.index];
1015
1016         if ((buf1->state == VIDEOBUF_QUEUED) ||
1017             (buf1->state == VIDEOBUF_ACTIVE)) {
1018                 vpif_err("invalid state\n");
1019                 goto qbuf_exit;
1020         }
1021
1022         switch (buf1->memory) {
1023         case V4L2_MEMORY_MMAP:
1024                 if (buf1->baddr == 0)
1025                         goto qbuf_exit;
1026                 break;
1027
1028         case V4L2_MEMORY_USERPTR:
1029                 if (tbuf.length < buf1->bsize)
1030                         goto qbuf_exit;
1031
1032                 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
1033                             && (buf1->baddr != tbuf.m.userptr)) {
1034                         vpif_buffer_release(&common->buffer_queue, buf1);
1035                         buf1->baddr = tbuf.m.userptr;
1036                 }
1037                 break;
1038
1039         default:
1040                 goto qbuf_exit;
1041         }
1042
1043         local_irq_save(flags);
1044         ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1045                                         common->buffer_queue.field);
1046         if (ret < 0) {
1047                 local_irq_restore(flags);
1048                 goto qbuf_exit;
1049         }
1050
1051         buf1->state = VIDEOBUF_ACTIVE;
1052
1053         if (V4L2_MEMORY_USERPTR == common->memory)
1054                 addr = buf1->boff;
1055         else
1056                 addr = videobuf_to_dma_contig(buf1);
1057
1058         common->next_frm = buf1;
1059         common->set_addr(addr + common->ytop_off,
1060                          addr + common->ybtm_off,
1061                          addr + common->ctop_off,
1062                          addr + common->cbtm_off);
1063
1064         local_irq_restore(flags);
1065         list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1066         mutex_unlock(&common->buffer_queue.vb_lock);
1067         return 0;
1068
1069 qbuf_exit:
1070         mutex_unlock(&common->buffer_queue.vb_lock);
1071         return -EINVAL;
1072 }
1073
1074 /**
1075  * vpif_dqbuf() - query buffer handler
1076  * @file: file ptr
1077  * @priv: file handle
1078  * @buf: v4l2 buffer structure ptr
1079  */
1080 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1081 {
1082         struct vpif_fh *fh = priv;
1083         struct channel_obj *ch = fh->channel;
1084         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1085
1086         vpif_dbg(2, debug, "vpif_dqbuf\n");
1087
1088         return videobuf_dqbuf(&common->buffer_queue, buf,
1089                                         file->f_flags & O_NONBLOCK);
1090 }
1091
1092 /**
1093  * vpif_streamon() - streamon handler
1094  * @file: file ptr
1095  * @priv: file handle
1096  * @buftype: v4l2 buffer type
1097  */
1098 static int vpif_streamon(struct file *file, void *priv,
1099                                 enum v4l2_buf_type buftype)
1100 {
1101
1102         struct vpif_capture_config *config = vpif_dev->platform_data;
1103         struct vpif_fh *fh = priv;
1104         struct channel_obj *ch = fh->channel;
1105         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1106         struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1107         struct vpif_params *vpif;
1108         unsigned long addr = 0;
1109         int ret = 0;
1110
1111         vpif_dbg(2, debug, "vpif_streamon\n");
1112
1113         vpif = &ch->vpifparams;
1114
1115         if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1116                 vpif_dbg(1, debug, "buffer type not supported\n");
1117                 return -EINVAL;
1118         }
1119
1120         /* If file handle is not allowed IO, return error */
1121         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1122                 vpif_dbg(1, debug, "io not allowed\n");
1123                 return -EACCES;
1124         }
1125
1126         /* If Streaming is already started, return error */
1127         if (common->started) {
1128                 vpif_dbg(1, debug, "channel->started\n");
1129                 return -EBUSY;
1130         }
1131
1132         if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1133             oth_ch->common[VPIF_VIDEO_INDEX].started &&
1134             vpif->std_info.ycmux_mode == 0) ||
1135            ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1136             (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1137                 vpif_dbg(1, debug, "other channel is being used\n");
1138                 return -EBUSY;
1139         }
1140
1141         ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1142         if (ret)
1143                 return ret;
1144
1145         /* Enable streamon on the sub device */
1146         ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1147                                 s_stream, 1);
1148
1149         if (ret && (ret != -ENOIOCTLCMD)) {
1150                 vpif_dbg(1, debug, "stream on failed in subdev\n");
1151                 return ret;
1152         }
1153
1154         /* Call videobuf_streamon to start streaming in videobuf */
1155         ret = videobuf_streamon(&common->buffer_queue);
1156         if (ret) {
1157                 vpif_dbg(1, debug, "videobuf_streamon\n");
1158                 return ret;
1159         }
1160
1161         if (mutex_lock_interruptible(&common->lock)) {
1162                 ret = -ERESTARTSYS;
1163                 goto streamoff_exit;
1164         }
1165
1166         /* If buffer queue is empty, return error */
1167         if (list_empty(&common->dma_queue)) {
1168                 vpif_dbg(1, debug, "buffer queue is empty\n");
1169                 ret = -EIO;
1170                 goto exit;
1171         }
1172
1173         /* Get the next frame from the buffer queue */
1174         common->cur_frm = list_entry(common->dma_queue.next,
1175                                     struct videobuf_buffer, queue);
1176         common->next_frm = common->cur_frm;
1177
1178         /* Remove buffer from the buffer queue */
1179         list_del(&common->cur_frm->queue);
1180         /* Mark state of the current frame to active */
1181         common->cur_frm->state = VIDEOBUF_ACTIVE;
1182         /* Initialize field_id and started member */
1183         ch->field_id = 0;
1184         common->started = 1;
1185
1186         if (V4L2_MEMORY_USERPTR == common->memory)
1187                 addr = common->cur_frm->boff;
1188         else
1189                 addr = videobuf_to_dma_contig(common->cur_frm);
1190
1191         /* Calculate the offset for Y and C data in the buffer */
1192         vpif_calculate_offsets(ch);
1193
1194         if ((vpif->std_info.frm_fmt &&
1195             ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1196              (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1197             (!vpif->std_info.frm_fmt &&
1198              (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1199                 vpif_dbg(1, debug, "conflict in field format and std format\n");
1200                 ret = -EINVAL;
1201                 goto exit;
1202         }
1203
1204         /* configure 1 or 2 channel mode */
1205         ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1206
1207         if (ret < 0) {
1208                 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1209                 goto exit;
1210         }
1211
1212         /* Call vpif_set_params function to set the parameters and addresses */
1213         ret = vpif_set_video_params(vpif, ch->channel_id);
1214
1215         if (ret < 0) {
1216                 vpif_dbg(1, debug, "can't set video params\n");
1217                 goto exit;
1218         }
1219
1220         common->started = ret;
1221         vpif_config_addr(ch, ret);
1222
1223         common->set_addr(addr + common->ytop_off,
1224                          addr + common->ybtm_off,
1225                          addr + common->ctop_off,
1226                          addr + common->cbtm_off);
1227
1228         /**
1229          * Set interrupt for both the fields in VPIF Register enable channel in
1230          * VPIF register
1231          */
1232         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1233                 channel0_intr_assert();
1234                 channel0_intr_enable(1);
1235                 enable_channel0(1);
1236         }
1237         if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1238             (common->started == 2)) {
1239                 channel1_intr_assert();
1240                 channel1_intr_enable(1);
1241                 enable_channel1(1);
1242         }
1243         channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1244         mutex_unlock(&common->lock);
1245         return ret;
1246
1247 exit:
1248         mutex_unlock(&common->lock);
1249 streamoff_exit:
1250         ret = videobuf_streamoff(&common->buffer_queue);
1251         return ret;
1252 }
1253
1254 /**
1255  * vpif_streamoff() - streamoff handler
1256  * @file: file ptr
1257  * @priv: file handle
1258  * @buftype: v4l2 buffer type
1259  */
1260 static int vpif_streamoff(struct file *file, void *priv,
1261                                 enum v4l2_buf_type buftype)
1262 {
1263
1264         struct vpif_fh *fh = priv;
1265         struct channel_obj *ch = fh->channel;
1266         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1267         int ret;
1268
1269         vpif_dbg(2, debug, "vpif_streamoff\n");
1270
1271         if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1272                 vpif_dbg(1, debug, "buffer type not supported\n");
1273                 return -EINVAL;
1274         }
1275
1276         /* If io is allowed for this file handle, return error */
1277         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1278                 vpif_dbg(1, debug, "io not allowed\n");
1279                 return -EACCES;
1280         }
1281
1282         /* If streaming is not started, return error */
1283         if (!common->started) {
1284                 vpif_dbg(1, debug, "channel->started\n");
1285                 return -EINVAL;
1286         }
1287
1288         if (mutex_lock_interruptible(&common->lock))
1289                 return -ERESTARTSYS;
1290
1291         /* disable channel */
1292         if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1293                 enable_channel0(0);
1294                 channel0_intr_enable(0);
1295         } else {
1296                 enable_channel1(0);
1297                 channel1_intr_enable(0);
1298         }
1299
1300         common->started = 0;
1301
1302         ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1303                                 s_stream, 0);
1304
1305         if (ret && (ret != -ENOIOCTLCMD))
1306                 vpif_dbg(1, debug, "stream off failed in subdev\n");
1307
1308         mutex_unlock(&common->lock);
1309
1310         return videobuf_streamoff(&common->buffer_queue);
1311 }
1312
1313 /**
1314  * vpif_map_sub_device_to_input() - Maps sub device to input
1315  * @ch - ptr to channel
1316  * @config - ptr to capture configuration
1317  * @input_index - Given input index from application
1318  * @sub_device_index - index into sd table
1319  *
1320  * lookup the sub device information for a given input index.
1321  * we report all the inputs to application. inputs table also
1322  * has sub device name for the each input
1323  */
1324 static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1325                                 struct channel_obj *ch,
1326                                 struct vpif_capture_config *vpif_cfg,
1327                                 int input_index,
1328                                 int *sub_device_index)
1329 {
1330         struct vpif_capture_chan_config *chan_cfg;
1331         struct vpif_subdev_info *subdev_info = NULL;
1332         const char *subdev_name = NULL;
1333         int i;
1334
1335         vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1336
1337         chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1338
1339         /**
1340          * search through the inputs to find the sub device supporting
1341          * the input
1342          */
1343         for (i = 0; i < chan_cfg->input_count; i++) {
1344                 /* For each sub device, loop through input */
1345                 if (i == input_index) {
1346                         subdev_name = chan_cfg->inputs[i].subdev_name;
1347                         break;
1348                 }
1349         }
1350
1351         /* if reached maximum. return null */
1352         if (i == chan_cfg->input_count || (NULL == subdev_name))
1353                 return subdev_info;
1354
1355         /* loop through the sub device list to get the sub device info */
1356         for (i = 0; i < vpif_cfg->subdev_count; i++) {
1357                 subdev_info = &vpif_cfg->subdev_info[i];
1358                 if (!strcmp(subdev_info->name, subdev_name))
1359                         break;
1360         }
1361
1362         if (i == vpif_cfg->subdev_count)
1363                 return subdev_info;
1364
1365         /* check if the sub device is registered */
1366         if (NULL == vpif_obj.sd[i])
1367                 return NULL;
1368
1369         *sub_device_index = i;
1370         return subdev_info;
1371 }
1372
1373 /**
1374  * vpif_querystd() - querystd handler
1375  * @file: file ptr
1376  * @priv: file handle
1377  * @std_id: ptr to std id
1378  *
1379  * This function is called to detect standard at the selected input
1380  */
1381 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1382 {
1383         struct vpif_fh *fh = priv;
1384         struct channel_obj *ch = fh->channel;
1385         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1386         int ret = 0;
1387
1388         vpif_dbg(2, debug, "vpif_querystd\n");
1389
1390         if (mutex_lock_interruptible(&common->lock))
1391                 return -ERESTARTSYS;
1392
1393         /* Call querystd function of decoder device */
1394         ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1395                                 querystd, std_id);
1396         if (ret < 0)
1397                 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1398
1399         mutex_unlock(&common->lock);
1400         return ret;
1401 }
1402
1403 /**
1404  * vpif_g_std() - get STD handler
1405  * @file: file ptr
1406  * @priv: file handle
1407  * @std_id: ptr to std id
1408  */
1409 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1410 {
1411         struct vpif_fh *fh = priv;
1412         struct channel_obj *ch = fh->channel;
1413
1414         vpif_dbg(2, debug, "vpif_g_std\n");
1415
1416         *std = ch->video.stdid;
1417         return 0;
1418 }
1419
1420 /**
1421  * vpif_s_std() - set STD handler
1422  * @file: file ptr
1423  * @priv: file handle
1424  * @std_id: ptr to std id
1425  */
1426 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1427 {
1428         struct vpif_fh *fh = priv;
1429         struct channel_obj *ch = fh->channel;
1430         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1431         int ret = 0;
1432
1433         vpif_dbg(2, debug, "vpif_s_std\n");
1434
1435         if (common->started) {
1436                 vpif_err("streaming in progress\n");
1437                 return -EBUSY;
1438         }
1439
1440         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1441             (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1442                 if (!fh->initialized) {
1443                         vpif_dbg(1, debug, "Channel Busy\n");
1444                         return -EBUSY;
1445                 }
1446         }
1447
1448         ret = v4l2_prio_check(&ch->prio, fh->prio);
1449         if (0 != ret)
1450                 return ret;
1451
1452         fh->initialized = 1;
1453
1454         /* Call encoder subdevice function to set the standard */
1455         if (mutex_lock_interruptible(&common->lock))
1456                 return -ERESTARTSYS;
1457
1458         ch->video.stdid = *std_id;
1459
1460         /* Get the information about the standard */
1461         if (vpif_update_std_info(ch)) {
1462                 ret = -EINVAL;
1463                 vpif_err("Error getting the standard info\n");
1464                 goto s_std_exit;
1465         }
1466
1467         /* Configure the default format information */
1468         vpif_config_format(ch);
1469
1470         /* set standard in the sub device */
1471         ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1472                                 s_std, *std_id);
1473         if (ret < 0)
1474                 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1475
1476 s_std_exit:
1477         mutex_unlock(&common->lock);
1478         return ret;
1479 }
1480
1481 /**
1482  * vpif_enum_input() - ENUMINPUT handler
1483  * @file: file ptr
1484  * @priv: file handle
1485  * @input: ptr to input structure
1486  */
1487 static int vpif_enum_input(struct file *file, void *priv,
1488                                 struct v4l2_input *input)
1489 {
1490
1491         struct vpif_capture_config *config = vpif_dev->platform_data;
1492         struct vpif_capture_chan_config *chan_cfg;
1493         struct vpif_fh *fh = priv;
1494         struct channel_obj *ch = fh->channel;
1495
1496         chan_cfg = &config->chan_config[ch->channel_id];
1497
1498         if (input->index >= chan_cfg->input_count) {
1499                 vpif_dbg(1, debug, "Invalid input index\n");
1500                 return -EINVAL;
1501         }
1502
1503         memcpy(input, &chan_cfg->inputs[input->index].input,
1504                 sizeof(*input));
1505         return 0;
1506 }
1507
1508 /**
1509  * vpif_g_input() - Get INPUT handler
1510  * @file: file ptr
1511  * @priv: file handle
1512  * @index: ptr to input index
1513  */
1514 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1515 {
1516         struct vpif_fh *fh = priv;
1517         struct channel_obj *ch = fh->channel;
1518         struct video_obj *vid_ch = &ch->video;
1519
1520         *index = vid_ch->input_idx;
1521
1522         return 0;
1523 }
1524
1525 /**
1526  * vpif_s_input() - Set INPUT handler
1527  * @file: file ptr
1528  * @priv: file handle
1529  * @index: input index
1530  */
1531 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1532 {
1533         struct vpif_capture_config *config = vpif_dev->platform_data;
1534         struct vpif_capture_chan_config *chan_cfg;
1535         struct vpif_fh *fh = priv;
1536         struct channel_obj *ch = fh->channel;
1537         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1538         struct video_obj *vid_ch = &ch->video;
1539         struct vpif_subdev_info *subdev_info;
1540         int ret = 0, sd_index = 0;
1541         u32 input = 0, output = 0;
1542
1543         chan_cfg = &config->chan_config[ch->channel_id];
1544
1545         if (common->started) {
1546                 vpif_err("Streaming in progress\n");
1547                 return -EBUSY;
1548         }
1549
1550         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1551             (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1552                 if (!fh->initialized) {
1553                         vpif_dbg(1, debug, "Channel Busy\n");
1554                         return -EBUSY;
1555                 }
1556         }
1557
1558         ret = v4l2_prio_check(&ch->prio, fh->prio);
1559         if (0 != ret)
1560                 return ret;
1561
1562         fh->initialized = 1;
1563         subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1564                                                    &sd_index);
1565         if (NULL == subdev_info) {
1566                 vpif_dbg(1, debug,
1567                         "couldn't lookup sub device for the input index\n");
1568                 return -EINVAL;
1569         }
1570
1571         if (mutex_lock_interruptible(&common->lock))
1572                 return -ERESTARTSYS;
1573
1574         /* first setup input path from sub device to vpif */
1575         if (config->setup_input_path) {
1576                 ret = config->setup_input_path(ch->channel_id,
1577                                                subdev_info->name);
1578                 if (ret < 0) {
1579                         vpif_dbg(1, debug, "couldn't setup input path for the"
1580                                 " sub device %s, for input index %d\n",
1581                                 subdev_info->name, index);
1582                         goto exit;
1583                 }
1584         }
1585
1586         if (subdev_info->can_route) {
1587                 input = subdev_info->input;
1588                 output = subdev_info->output;
1589                 ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1590                                         input, output, 0);
1591                 if (ret < 0) {
1592                         vpif_dbg(1, debug, "Failed to set input\n");
1593                         goto exit;
1594                 }
1595         }
1596         vid_ch->input_idx = index;
1597         ch->curr_subdev_info = subdev_info;
1598         ch->curr_sd_index = sd_index;
1599         /* copy interface parameters to vpif */
1600         ch->vpifparams.iface = subdev_info->vpif_if;
1601
1602         /* update tvnorms from the sub device input info */
1603         ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1604
1605 exit:
1606         mutex_unlock(&common->lock);
1607         return ret;
1608 }
1609
1610 /**
1611  * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1612  * @file: file ptr
1613  * @priv: file handle
1614  * @index: input index
1615  */
1616 static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
1617                                         struct v4l2_fmtdesc *fmt)
1618 {
1619         struct vpif_fh *fh = priv;
1620         struct channel_obj *ch = fh->channel;
1621
1622         if (fmt->index != 0) {
1623                 vpif_dbg(1, debug, "Invalid format index\n");
1624                 return -EINVAL;
1625         }
1626
1627         /* Fill in the information about format */
1628         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1629                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1630                 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1631                 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1632         } else {
1633                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1634                 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1635                 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1636         }
1637         return 0;
1638 }
1639
1640 /**
1641  * vpif_try_fmt_vid_cap() - TRY_FMT handler
1642  * @file: file ptr
1643  * @priv: file handle
1644  * @fmt: ptr to v4l2 format structure
1645  */
1646 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1647                                 struct v4l2_format *fmt)
1648 {
1649         struct vpif_fh *fh = priv;
1650         struct channel_obj *ch = fh->channel;
1651         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1652
1653         return vpif_check_format(ch, pixfmt, 1);
1654 }
1655
1656
1657 /**
1658  * vpif_g_fmt_vid_cap() - Set INPUT handler
1659  * @file: file ptr
1660  * @priv: file handle
1661  * @fmt: ptr to v4l2 format structure
1662  */
1663 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1664                                 struct v4l2_format *fmt)
1665 {
1666         struct vpif_fh *fh = priv;
1667         struct channel_obj *ch = fh->channel;
1668         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1669
1670         /* Check the validity of the buffer type */
1671         if (common->fmt.type != fmt->type)
1672                 return -EINVAL;
1673
1674         /* Fill in the information about format */
1675         if (mutex_lock_interruptible(&common->lock))
1676                 return -ERESTARTSYS;
1677
1678         *fmt = common->fmt;
1679         mutex_unlock(&common->lock);
1680         return 0;
1681 }
1682
1683 /**
1684  * vpif_s_fmt_vid_cap() - Set FMT handler
1685  * @file: file ptr
1686  * @priv: file handle
1687  * @fmt: ptr to v4l2 format structure
1688  */
1689 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1690                                 struct v4l2_format *fmt)
1691 {
1692         struct vpif_fh *fh = priv;
1693         struct channel_obj *ch = fh->channel;
1694         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1695         struct v4l2_pix_format *pixfmt;
1696         int ret = 0;
1697
1698         vpif_dbg(2, debug, "VIDIOC_S_FMT\n");
1699
1700         /* If streaming is started, return error */
1701         if (common->started) {
1702                 vpif_dbg(1, debug, "Streaming is started\n");
1703                 return -EBUSY;
1704         }
1705
1706         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1707             (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1708                 if (!fh->initialized) {
1709                         vpif_dbg(1, debug, "Channel Busy\n");
1710                         return -EBUSY;
1711                 }
1712         }
1713
1714         ret = v4l2_prio_check(&ch->prio, fh->prio);
1715         if (0 != ret)
1716                 return ret;
1717
1718         fh->initialized = 1;
1719
1720         pixfmt = &fmt->fmt.pix;
1721         /* Check for valid field format */
1722         ret = vpif_check_format(ch, pixfmt, 0);
1723
1724         if (ret)
1725                 return ret;
1726         /* store the format in the channel object */
1727         if (mutex_lock_interruptible(&common->lock))
1728                 return -ERESTARTSYS;
1729
1730         common->fmt = *fmt;
1731         mutex_unlock(&common->lock);
1732
1733         return 0;
1734 }
1735
1736 /**
1737  * vpif_querycap() - QUERYCAP handler
1738  * @file: file ptr
1739  * @priv: file handle
1740  * @cap: ptr to v4l2_capability structure
1741  */
1742 static int vpif_querycap(struct file *file, void  *priv,
1743                                 struct v4l2_capability *cap)
1744 {
1745         struct vpif_capture_config *config = vpif_dev->platform_data;
1746
1747         cap->version = VPIF_CAPTURE_VERSION_CODE;
1748         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1749         strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1750         strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info));
1751         strlcpy(cap->card, config->card_name, sizeof(cap->card));
1752
1753         return 0;
1754 }
1755
1756 /**
1757  * vpif_g_priority() - get priority handler
1758  * @file: file ptr
1759  * @priv: file handle
1760  * @prio: ptr to v4l2_priority structure
1761  */
1762 static int vpif_g_priority(struct file *file, void *priv,
1763                            enum v4l2_priority *prio)
1764 {
1765         struct vpif_fh *fh = priv;
1766         struct channel_obj *ch = fh->channel;
1767
1768         *prio = v4l2_prio_max(&ch->prio);
1769
1770         return 0;
1771 }
1772
1773 /**
1774  * vpif_s_priority() - set priority handler
1775  * @file: file ptr
1776  * @priv: file handle
1777  * @prio: ptr to v4l2_priority structure
1778  */
1779 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1780 {
1781         struct vpif_fh *fh = priv;
1782         struct channel_obj *ch = fh->channel;
1783
1784         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1785 }
1786
1787 /**
1788  * vpif_cropcap() - cropcap handler
1789  * @file: file ptr
1790  * @priv: file handle
1791  * @crop: ptr to v4l2_cropcap structure
1792  */
1793 static int vpif_cropcap(struct file *file, void *priv,
1794                         struct v4l2_cropcap *crop)
1795 {
1796         struct vpif_fh *fh = priv;
1797         struct channel_obj *ch = fh->channel;
1798         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1799
1800         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1801                 return -EINVAL;
1802
1803         crop->bounds.left = 0;
1804         crop->bounds.top = 0;
1805         crop->bounds.height = common->height;
1806         crop->bounds.width = common->width;
1807         crop->defrect = crop->bounds;
1808         return 0;
1809 }
1810
1811 /* vpif capture ioctl operations */
1812 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1813         .vidioc_querycap                = vpif_querycap,
1814         .vidioc_g_priority              = vpif_g_priority,
1815         .vidioc_s_priority              = vpif_s_priority,
1816         .vidioc_enum_fmt_vid_cap        = vpif_enum_fmt_vid_cap,
1817         .vidioc_g_fmt_vid_cap           = vpif_g_fmt_vid_cap,
1818         .vidioc_s_fmt_vid_cap           = vpif_s_fmt_vid_cap,
1819         .vidioc_try_fmt_vid_cap         = vpif_try_fmt_vid_cap,
1820         .vidioc_enum_input              = vpif_enum_input,
1821         .vidioc_s_input                 = vpif_s_input,
1822         .vidioc_g_input                 = vpif_g_input,
1823         .vidioc_reqbufs                 = vpif_reqbufs,
1824         .vidioc_querybuf                = vpif_querybuf,
1825         .vidioc_querystd                = vpif_querystd,
1826         .vidioc_s_std                   = vpif_s_std,
1827         .vidioc_g_std                   = vpif_g_std,
1828         .vidioc_qbuf                    = vpif_qbuf,
1829         .vidioc_dqbuf                   = vpif_dqbuf,
1830         .vidioc_streamon                = vpif_streamon,
1831         .vidioc_streamoff               = vpif_streamoff,
1832         .vidioc_cropcap                 = vpif_cropcap,
1833 };
1834
1835 /* vpif file operations */
1836 static struct v4l2_file_operations vpif_fops = {
1837         .owner = THIS_MODULE,
1838         .open = vpif_open,
1839         .release = vpif_release,
1840         .ioctl = video_ioctl2,
1841         .mmap = vpif_mmap,
1842         .poll = vpif_poll
1843 };
1844
1845 /* vpif video template */
1846 static struct video_device vpif_video_template = {
1847         .name           = "vpif",
1848         .fops           = &vpif_fops,
1849         .minor          = -1,
1850         .ioctl_ops      = &vpif_ioctl_ops,
1851 };
1852
1853 /**
1854  * initialize_vpif() - Initialize vpif data structures
1855  *
1856  * Allocate memory for data structures and initialize them
1857  */
1858 static int initialize_vpif(void)
1859 {
1860         int err = 0, i, j;
1861         int free_channel_objects_index;
1862
1863         /* Default number of buffers should be 3 */
1864         if ((ch0_numbuffers > 0) &&
1865             (ch0_numbuffers < config_params.min_numbuffers))
1866                 ch0_numbuffers = config_params.min_numbuffers;
1867         if ((ch1_numbuffers > 0) &&
1868             (ch1_numbuffers < config_params.min_numbuffers))
1869                 ch1_numbuffers = config_params.min_numbuffers;
1870
1871         /* Set buffer size to min buffers size if it is invalid */
1872         if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1873                 ch0_bufsize =
1874                     config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1875         if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1876                 ch1_bufsize =
1877                     config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1878
1879         config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1880         config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1881         if (ch0_numbuffers) {
1882                 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1883                     = ch0_bufsize;
1884         }
1885         if (ch1_numbuffers) {
1886                 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1887                     = ch1_bufsize;
1888         }
1889
1890         /* Allocate memory for six channel objects */
1891         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1892                 vpif_obj.dev[i] =
1893                     kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1894                 /* If memory allocation fails, return error */
1895                 if (!vpif_obj.dev[i]) {
1896                         free_channel_objects_index = i;
1897                         err = -ENOMEM;
1898                         goto vpif_init_free_channel_objects;
1899                 }
1900         }
1901         return 0;
1902
1903 vpif_init_free_channel_objects:
1904         for (j = 0; j < free_channel_objects_index; j++)
1905                 kfree(vpif_obj.dev[j]);
1906         return err;
1907 }
1908
1909 /**
1910  * vpif_probe : This function probes the vpif capture driver
1911  * @pdev: platform device pointer
1912  *
1913  * This creates device entries by register itself to the V4L2 driver and
1914  * initializes fields of each channel objects
1915  */
1916 static __init int vpif_probe(struct platform_device *pdev)
1917 {
1918         struct vpif_subdev_info *subdevdata;
1919         struct vpif_capture_config *config;
1920         int i, j, k, m, q, err;
1921         struct i2c_adapter *i2c_adap;
1922         struct channel_obj *ch;
1923         struct common_obj *common;
1924         struct video_device *vfd;
1925         struct resource *res;
1926         int subdev_count;
1927
1928         vpif_dev = &pdev->dev;
1929
1930         err = initialize_vpif();
1931         if (err) {
1932                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1933                 return err;
1934         }
1935
1936         k = 0;
1937         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1938                 for (i = res->start; i <= res->end; i++) {
1939                         if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1940                                         "DM646x_Capture",
1941                                 (void *)(&vpif_obj.dev[k]->channel_id))) {
1942                                 err = -EBUSY;
1943                                 i--;
1944                                 goto vpif_int_err;
1945                         }
1946                 }
1947                 k++;
1948         }
1949
1950         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1951                 /* Get the pointer to the channel object */
1952                 ch = vpif_obj.dev[i];
1953                 /* Allocate memory for video device */
1954                 vfd = video_device_alloc();
1955                 if (NULL == vfd) {
1956                         for (j = 0; j < i; j++) {
1957                                 ch = vpif_obj.dev[j];
1958                                 video_device_release(ch->video_dev);
1959                         }
1960                         err = -ENOMEM;
1961                         goto vpif_dev_alloc_err;
1962                 }
1963
1964                 /* Initialize field of video device */
1965                 *vfd = vpif_video_template;
1966                 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1967                 vfd->release = video_device_release;
1968                 snprintf(vfd->name, sizeof(vfd->name),
1969                          "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
1970                          (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
1971                          (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
1972                          (VPIF_CAPTURE_VERSION_CODE) & 0xff);
1973                 /* Set video_dev to the video device */
1974                 ch->video_dev = vfd;
1975         }
1976
1977         for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1978                 ch = vpif_obj.dev[j];
1979                 ch->channel_id = j;
1980                 common = &(ch->common[VPIF_VIDEO_INDEX]);
1981                 spin_lock_init(&common->irqlock);
1982                 mutex_init(&common->lock);
1983                 /* Initialize prio member of channel object */
1984                 v4l2_prio_init(&ch->prio);
1985                 err = video_register_device(ch->video_dev,
1986                                             VFL_TYPE_GRABBER, (j ? 1 : 0));
1987                 if (err)
1988                         goto probe_out;
1989
1990                 video_set_drvdata(ch->video_dev, ch);
1991
1992         }
1993
1994         i2c_adap = i2c_get_adapter(1);
1995         config = pdev->dev.platform_data;
1996
1997         subdev_count = config->subdev_count;
1998         vpif_obj.sd = kmalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1999                                 GFP_KERNEL);
2000         if (vpif_obj.sd == NULL) {
2001                 vpif_err("unable to allocate memory for subdevice pointers\n");
2002                 err = -ENOMEM;
2003                 goto probe_out;
2004         }
2005
2006         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2007         if (err) {
2008                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2009                 goto probe_subdev_out;
2010         }
2011
2012         for (i = 0; i < subdev_count; i++) {
2013                 subdevdata = &config->subdev_info[i];
2014                 vpif_obj.sd[i] =
2015                         v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2016                                                   i2c_adap,
2017                                                   subdevdata->name,
2018                                                   &subdevdata->board_info,
2019                                                   NULL);
2020
2021                 if (!vpif_obj.sd[i]) {
2022                         vpif_err("Error registering v4l2 subdevice\n");
2023                         goto probe_subdev_out;
2024                 }
2025                 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2026                           subdevdata->name);
2027
2028                 if (vpif_obj.sd[i])
2029                         vpif_obj.sd[i]->grp_id = 1 << i;
2030         }
2031         v4l2_info(&vpif_obj.v4l2_dev, "DM646x VPIF Capture driver"
2032                   " initialized\n");
2033
2034         return 0;
2035
2036 probe_subdev_out:
2037         /* free sub devices memory */
2038         kfree(vpif_obj.sd);
2039
2040         j = VPIF_CAPTURE_MAX_DEVICES;
2041 probe_out:
2042         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2043         for (k = 0; k < j; k++) {
2044                 /* Get the pointer to the channel object */
2045                 ch = vpif_obj.dev[k];
2046                 /* Unregister video device */
2047                 video_unregister_device(ch->video_dev);
2048         }
2049
2050 vpif_dev_alloc_err:
2051         k = VPIF_CAPTURE_MAX_DEVICES-1;
2052         res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2053         i = res->end;
2054
2055 vpif_int_err:
2056         for (q = k; q >= 0; q--) {
2057                 for (m = i; m >= (int)res->start; m--)
2058                         free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2059
2060                 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2061                 if (res)
2062                         i = res->end;
2063         }
2064         return err;
2065 }
2066
2067 /**
2068  * vpif_remove() - driver remove handler
2069  * @device: ptr to platform device structure
2070  *
2071  * The vidoe device is unregistered
2072  */
2073 static int vpif_remove(struct platform_device *device)
2074 {
2075         int i;
2076         struct channel_obj *ch;
2077
2078         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2079
2080         /* un-register device */
2081         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2082                 /* Get the pointer to the channel object */
2083                 ch = vpif_obj.dev[i];
2084                 /* Unregister video device */
2085                 video_unregister_device(ch->video_dev);
2086         }
2087         return 0;
2088 }
2089
2090 /**
2091  * vpif_suspend: vpif device suspend
2092  *
2093  * TODO: Add suspend code here
2094  */
2095 static int
2096 vpif_suspend(struct device *dev)
2097 {
2098         return -1;
2099 }
2100
2101 /**
2102  * vpif_resume: vpif device suspend
2103  *
2104  * TODO: Add resume code here
2105  */
2106 static int
2107 vpif_resume(struct device *dev)
2108 {
2109         return -1;
2110 }
2111
2112 static const struct dev_pm_ops vpif_dev_pm_ops = {
2113         .suspend = vpif_suspend,
2114         .resume = vpif_resume,
2115 };
2116
2117 static struct platform_driver vpif_driver = {
2118         .driver = {
2119                 .name   = "vpif_capture",
2120                 .owner  = THIS_MODULE,
2121                 .pm = &vpif_dev_pm_ops,
2122         },
2123         .probe = vpif_probe,
2124         .remove = vpif_remove,
2125 };
2126
2127 /**
2128  * vpif_init: initialize the vpif driver
2129  *
2130  * This function registers device and driver to the kernel, requests irq
2131  * handler and allocates memory
2132  * for channel objects
2133  */
2134 static __init int vpif_init(void)
2135 {
2136         return platform_driver_register(&vpif_driver);
2137 }
2138
2139 /**
2140  * vpif_cleanup : This function clean up the vpif capture resources
2141  *
2142  * This will un-registers device and driver to the kernel, frees
2143  * requested irq handler and de-allocates memory allocated for channel
2144  * objects.
2145  */
2146 static void vpif_cleanup(void)
2147 {
2148         struct platform_device *pdev;
2149         struct resource *res;
2150         int irq_num;
2151         int i = 0;
2152
2153         pdev = container_of(vpif_dev, struct platform_device, dev);
2154         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2155                 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2156                         free_irq(irq_num,
2157                                  (void *)(&vpif_obj.dev[i]->channel_id));
2158                 i++;
2159         }
2160
2161         platform_driver_unregister(&vpif_driver);
2162
2163         kfree(vpif_obj.sd);
2164         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2165                 kfree(vpif_obj.dev[i]);
2166 }
2167
2168 /* Function for module initialization and cleanup */
2169 module_init(vpif_init);
2170 module_exit(vpif_cleanup);