[media] vpif_cap/disp: Add debug functionality
[pandora-kernel.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 #include <media/v4l2-chip-ident.h>
41
42 #include "vpif_capture.h"
43 #include "vpif.h"
44
45 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
46 MODULE_LICENSE("GPL");
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  * ch_params: video standard configuration parameters for vpif
86  */
87 static const struct vpif_channel_config_params ch_params[] = {
88         {
89                 "NTSC_M", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
90                 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
91         },
92         {
93                 "PAL_BDGHIK", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
94                 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
95         },
96 };
97
98 /**
99  * vpif_uservirt_to_phys : translate user/virtual address to phy address
100  * @virtp: user/virtual address
101  *
102  * This inline function is used to convert user space virtual address to
103  * physical address.
104  */
105 static inline u32 vpif_uservirt_to_phys(u32 virtp)
106 {
107         unsigned long physp = 0;
108         struct mm_struct *mm = current->mm;
109         struct vm_area_struct *vma;
110
111         vma = find_vma(mm, virtp);
112
113         /* For kernel direct-mapped memory, take the easy way */
114         if (virtp >= PAGE_OFFSET)
115                 physp = virt_to_phys((void *)virtp);
116         else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
117                 /**
118                  * this will catch, kernel-allocated, mmaped-to-usermode
119                  * addresses
120                  */
121                 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
122         else {
123                 /* otherwise, use get_user_pages() for general userland pages */
124                 int res, nr_pages = 1;
125                         struct page *pages;
126
127                 down_read(&current->mm->mmap_sem);
128
129                 res = get_user_pages(current, current->mm,
130                                      virtp, nr_pages, 1, 0, &pages, NULL);
131                 up_read(&current->mm->mmap_sem);
132
133                 if (res == nr_pages)
134                         physp = __pa(page_address(&pages[0]) +
135                                      (virtp & ~PAGE_MASK));
136                 else {
137                         vpif_err("get_user_pages failed\n");
138                         return 0;
139                 }
140         }
141         return physp;
142 }
143
144 /**
145  * buffer_prepare :  callback function for buffer prepare
146  * @q : buffer queue ptr
147  * @vb: ptr to video buffer
148  * @field: field info
149  *
150  * This is the callback function for buffer prepare when videobuf_qbuf()
151  * function is called. The buffer is prepared and user space virtual address
152  * or user address is converted into  physical address
153  */
154 static int vpif_buffer_prepare(struct videobuf_queue *q,
155                                struct videobuf_buffer *vb,
156                                enum v4l2_field field)
157 {
158         /* Get the file handle object and channel object */
159         struct vpif_fh *fh = q->priv_data;
160         struct channel_obj *ch = fh->channel;
161         struct common_obj *common;
162         unsigned long addr;
163
164
165         vpif_dbg(2, debug, "vpif_buffer_prepare\n");
166
167         common = &ch->common[VPIF_VIDEO_INDEX];
168
169         /* If buffer is not initialized, initialize it */
170         if (VIDEOBUF_NEEDS_INIT == vb->state) {
171                 vb->width = common->width;
172                 vb->height = common->height;
173                 vb->size = vb->width * vb->height;
174                 vb->field = field;
175         }
176         vb->state = VIDEOBUF_PREPARED;
177         /**
178          * if user pointer memory mechanism is used, get the physical
179          * address of the buffer
180          */
181         if (V4L2_MEMORY_USERPTR == common->memory) {
182                 if (0 == vb->baddr) {
183                         vpif_dbg(1, debug, "buffer address is 0\n");
184                         return -EINVAL;
185
186                 }
187                 vb->boff = vpif_uservirt_to_phys(vb->baddr);
188                 if (!IS_ALIGNED(vb->boff, 8))
189                         goto exit;
190         }
191
192         addr = vb->boff;
193         if (q->streaming) {
194                 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
195                     !IS_ALIGNED((addr + common->ybtm_off), 8) ||
196                     !IS_ALIGNED((addr + common->ctop_off), 8) ||
197                     !IS_ALIGNED((addr + common->cbtm_off), 8))
198                         goto exit;
199         }
200         return 0;
201 exit:
202         vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
203         return -EINVAL;
204 }
205
206 /**
207  * vpif_buffer_setup : Callback function for buffer setup.
208  * @q: buffer queue ptr
209  * @count: number of buffers
210  * @size: size of the buffer
211  *
212  * This callback function is called when reqbuf() is called to adjust
213  * the buffer count and buffer size
214  */
215 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
216                              unsigned int *size)
217 {
218         /* Get the file handle object and channel object */
219         struct vpif_fh *fh = q->priv_data;
220         struct channel_obj *ch = fh->channel;
221         struct common_obj *common;
222
223         common = &ch->common[VPIF_VIDEO_INDEX];
224
225         vpif_dbg(2, debug, "vpif_buffer_setup\n");
226
227         /* If memory type is not mmap, return */
228         if (V4L2_MEMORY_MMAP != common->memory)
229                 return 0;
230
231         /* Calculate the size of the buffer */
232         *size = config_params.channel_bufsize[ch->channel_id];
233
234         if (*count < config_params.min_numbuffers)
235                 *count = config_params.min_numbuffers;
236         return 0;
237 }
238
239 /**
240  * vpif_buffer_queue : Callback function to add buffer to DMA queue
241  * @q: ptr to videobuf_queue
242  * @vb: ptr to videobuf_buffer
243  */
244 static void vpif_buffer_queue(struct videobuf_queue *q,
245                               struct videobuf_buffer *vb)
246 {
247         /* Get the file handle object and channel object */
248         struct vpif_fh *fh = q->priv_data;
249         struct channel_obj *ch = fh->channel;
250         struct common_obj *common;
251
252         common = &ch->common[VPIF_VIDEO_INDEX];
253
254         vpif_dbg(2, debug, "vpif_buffer_queue\n");
255
256         /* add the buffer to the DMA queue */
257         list_add_tail(&vb->queue, &common->dma_queue);
258         /* Change state of the buffer */
259         vb->state = VIDEOBUF_QUEUED;
260 }
261
262 /**
263  * vpif_buffer_release : Callback function to free buffer
264  * @q: buffer queue ptr
265  * @vb: ptr to video buffer
266  *
267  * This function is called from the videobuf layer to free memory
268  * allocated to  the buffers
269  */
270 static void vpif_buffer_release(struct videobuf_queue *q,
271                                 struct videobuf_buffer *vb)
272 {
273         /* Get the file handle object and channel object */
274         struct vpif_fh *fh = q->priv_data;
275         struct channel_obj *ch = fh->channel;
276         struct common_obj *common;
277
278         common = &ch->common[VPIF_VIDEO_INDEX];
279
280         videobuf_dma_contig_free(q, vb);
281         vb->state = VIDEOBUF_NEEDS_INIT;
282 }
283
284 static struct videobuf_queue_ops video_qops = {
285         .buf_setup = vpif_buffer_setup,
286         .buf_prepare = vpif_buffer_prepare,
287         .buf_queue = vpif_buffer_queue,
288         .buf_release = vpif_buffer_release,
289 };
290
291 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
292         { {1, 1} };
293
294 /**
295  * vpif_process_buffer_complete: process a completed buffer
296  * @common: ptr to common channel object
297  *
298  * This function time stamp the buffer and mark it as DONE. It also
299  * wake up any process waiting on the QUEUE and set the next buffer
300  * as current
301  */
302 static void vpif_process_buffer_complete(struct common_obj *common)
303 {
304         do_gettimeofday(&common->cur_frm->ts);
305         common->cur_frm->state = VIDEOBUF_DONE;
306         wake_up_interruptible(&common->cur_frm->done);
307         /* Make curFrm pointing to nextFrm */
308         common->cur_frm = common->next_frm;
309 }
310
311 /**
312  * vpif_schedule_next_buffer: set next buffer address for capture
313  * @common : ptr to common channel object
314  *
315  * This function will get next buffer from the dma queue and
316  * set the buffer address in the vpif register for capture.
317  * the buffer is marked active
318  */
319 static void vpif_schedule_next_buffer(struct common_obj *common)
320 {
321         unsigned long addr = 0;
322
323         common->next_frm = list_entry(common->dma_queue.next,
324                                      struct videobuf_buffer, queue);
325         /* Remove that buffer from the buffer queue */
326         list_del(&common->next_frm->queue);
327         common->next_frm->state = VIDEOBUF_ACTIVE;
328         if (V4L2_MEMORY_USERPTR == common->memory)
329                 addr = common->next_frm->boff;
330         else
331                 addr = videobuf_to_dma_contig(common->next_frm);
332
333         /* Set top and bottom field addresses in VPIF registers */
334         common->set_addr(addr + common->ytop_off,
335                          addr + common->ybtm_off,
336                          addr + common->ctop_off,
337                          addr + common->cbtm_off);
338 }
339
340 /**
341  * vpif_channel_isr : ISR handler for vpif capture
342  * @irq: irq number
343  * @dev_id: dev_id ptr
344  *
345  * It changes status of the captured buffer, takes next buffer from the queue
346  * and sets its address in VPIF  registers
347  */
348 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
349 {
350         struct vpif_device *dev = &vpif_obj;
351         struct common_obj *common;
352         struct channel_obj *ch;
353         enum v4l2_field field;
354         int channel_id = 0;
355         int fid = -1, i;
356
357         channel_id = *(int *)(dev_id);
358         ch = dev->dev[channel_id];
359
360         field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
361
362         for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
363                 common = &ch->common[i];
364                 /* skip If streaming is not started in this channel */
365                 if (0 == common->started)
366                         continue;
367
368                 /* Check the field format */
369                 if (1 == ch->vpifparams.std_info.frm_fmt) {
370                         /* Progressive mode */
371                         if (list_empty(&common->dma_queue))
372                                 continue;
373
374                         if (!channel_first_int[i][channel_id])
375                                 vpif_process_buffer_complete(common);
376
377                         channel_first_int[i][channel_id] = 0;
378
379                         vpif_schedule_next_buffer(common);
380
381
382                         channel_first_int[i][channel_id] = 0;
383                 } else {
384                         /**
385                          * Interlaced mode. If it is first interrupt, ignore
386                          * it
387                          */
388                         if (channel_first_int[i][channel_id]) {
389                                 channel_first_int[i][channel_id] = 0;
390                                 continue;
391                         }
392                         if (0 == i) {
393                                 ch->field_id ^= 1;
394                                 /* Get field id from VPIF registers */
395                                 fid = vpif_channel_getfid(ch->channel_id);
396                                 if (fid != ch->field_id) {
397                                         /**
398                                          * If field id does not match stored
399                                          * field id, make them in sync
400                                          */
401                                         if (0 == fid)
402                                                 ch->field_id = fid;
403                                         return IRQ_HANDLED;
404                                 }
405                         }
406                         /* device field id and local field id are in sync */
407                         if (0 == fid) {
408                                 /* this is even field */
409                                 if (common->cur_frm == common->next_frm)
410                                         continue;
411
412                                 /* mark the current buffer as done */
413                                 vpif_process_buffer_complete(common);
414                         } else if (1 == fid) {
415                                 /* odd field */
416                                 if (list_empty(&common->dma_queue) ||
417                                     (common->cur_frm != common->next_frm))
418                                         continue;
419
420                                 vpif_schedule_next_buffer(common);
421                         }
422                 }
423         }
424         return IRQ_HANDLED;
425 }
426
427 /**
428  * vpif_update_std_info() - update standard related info
429  * @ch: ptr to channel object
430  *
431  * For a given standard selected by application, update values
432  * in the device data structures
433  */
434 static int vpif_update_std_info(struct channel_obj *ch)
435 {
436         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
437         struct vpif_params *vpifparams = &ch->vpifparams;
438         const struct vpif_channel_config_params *config;
439         struct vpif_channel_config_params *std_info;
440         struct video_obj *vid_ch = &ch->video;
441         int index;
442
443         vpif_dbg(2, debug, "vpif_update_std_info\n");
444
445         std_info = &vpifparams->std_info;
446
447         for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
448                 config = &ch_params[index];
449                 if (config->stdid & vid_ch->stdid) {
450                         memcpy(std_info, config, sizeof(*config));
451                         break;
452                 }
453         }
454
455         /* standard not found */
456         if (index == ARRAY_SIZE(ch_params))
457                 return -EINVAL;
458
459         common->fmt.fmt.pix.width = std_info->width;
460         common->width = std_info->width;
461         common->fmt.fmt.pix.height = std_info->height;
462         common->height = std_info->height;
463         common->fmt.fmt.pix.bytesperline = std_info->width;
464         vpifparams->video_params.hpitch = std_info->width;
465         vpifparams->video_params.storage_mode = std_info->frm_fmt;
466         return 0;
467 }
468
469 /**
470  * vpif_calculate_offsets : This function calculates buffers offsets
471  * @ch : ptr to channel object
472  *
473  * This function calculates buffer offsets for Y and C in the top and
474  * bottom field
475  */
476 static void vpif_calculate_offsets(struct channel_obj *ch)
477 {
478         unsigned int hpitch, vpitch, sizeimage;
479         struct video_obj *vid_ch = &(ch->video);
480         struct vpif_params *vpifparams = &ch->vpifparams;
481         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
482         enum v4l2_field field = common->fmt.fmt.pix.field;
483
484         vpif_dbg(2, debug, "vpif_calculate_offsets\n");
485
486         if (V4L2_FIELD_ANY == field) {
487                 if (vpifparams->std_info.frm_fmt)
488                         vid_ch->buf_field = V4L2_FIELD_NONE;
489                 else
490                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
491         } else
492                 vid_ch->buf_field = common->fmt.fmt.pix.field;
493
494         if (V4L2_MEMORY_USERPTR == common->memory)
495                 sizeimage = common->fmt.fmt.pix.sizeimage;
496         else
497                 sizeimage = config_params.channel_bufsize[ch->channel_id];
498
499         hpitch = common->fmt.fmt.pix.bytesperline;
500         vpitch = sizeimage / (hpitch * 2);
501
502         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
503             (V4L2_FIELD_INTERLACED == 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 = hpitch;
507                 common->ctop_off = sizeimage / 2;
508                 common->cbtm_off = sizeimage / 2 + hpitch;
509         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
510                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
511                 common->ytop_off = 0;
512                 common->ybtm_off = sizeimage / 4;
513                 common->ctop_off = sizeimage / 2;
514                 common->cbtm_off = common->ctop_off + sizeimage / 4;
515         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
516                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
517                 common->ybtm_off = 0;
518                 common->ytop_off = sizeimage / 4;
519                 common->cbtm_off = sizeimage / 2;
520                 common->ctop_off = common->cbtm_off + sizeimage / 4;
521         }
522         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
523             (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
524                 vpifparams->video_params.storage_mode = 1;
525         else
526                 vpifparams->video_params.storage_mode = 0;
527
528         if (1 == vpifparams->std_info.frm_fmt)
529                 vpifparams->video_params.hpitch =
530                     common->fmt.fmt.pix.bytesperline;
531         else {
532                 if ((field == V4L2_FIELD_ANY)
533                     || (field == V4L2_FIELD_INTERLACED))
534                         vpifparams->video_params.hpitch =
535                             common->fmt.fmt.pix.bytesperline * 2;
536                 else
537                         vpifparams->video_params.hpitch =
538                             common->fmt.fmt.pix.bytesperline;
539         }
540
541         ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
542 }
543
544 /**
545  * vpif_config_format: configure default frame format in the device
546  * ch : ptr to channel object
547  */
548 static void vpif_config_format(struct channel_obj *ch)
549 {
550         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
551
552         vpif_dbg(2, debug, "vpif_config_format\n");
553
554         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
555         if (config_params.numbuffers[ch->channel_id] == 0)
556                 common->memory = V4L2_MEMORY_USERPTR;
557         else
558                 common->memory = V4L2_MEMORY_MMAP;
559
560         common->fmt.fmt.pix.sizeimage
561             = config_params.channel_bufsize[ch->channel_id];
562
563         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
564                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
565         else
566                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
567         common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
568 }
569
570 /**
571  * vpif_get_default_field() - Get default field type based on interface
572  * @vpif_params - ptr to vpif params
573  */
574 static inline enum v4l2_field vpif_get_default_field(
575                                 struct vpif_interface *iface)
576 {
577         return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
578                                                 V4L2_FIELD_INTERLACED;
579 }
580
581 /**
582  * vpif_check_format()  - check given pixel format for compatibility
583  * @ch - channel  ptr
584  * @pixfmt - Given pixel format
585  * @update - update the values as per hardware requirement
586  *
587  * Check the application pixel format for S_FMT and update the input
588  * values as per hardware limits for TRY_FMT. The default pixel and
589  * field format is selected based on interface type.
590  */
591 static int vpif_check_format(struct channel_obj *ch,
592                              struct v4l2_pix_format *pixfmt,
593                              int update)
594 {
595         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
596         struct vpif_params *vpif_params = &ch->vpifparams;
597         enum v4l2_field field = pixfmt->field;
598         u32 sizeimage, hpitch, vpitch;
599         int ret = -EINVAL;
600
601         vpif_dbg(2, debug, "vpif_check_format\n");
602         /**
603          * first check for the pixel format. If if_type is Raw bayer,
604          * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
605          * V4L2_PIX_FMT_YUV422P is supported
606          */
607         if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
608                 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
609                         if (!update) {
610                                 vpif_dbg(2, debug, "invalid pix format\n");
611                                 goto exit;
612                         }
613                         pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
614                 }
615         } else {
616                 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
617                         if (!update) {
618                                 vpif_dbg(2, debug, "invalid pixel format\n");
619                                 goto exit;
620                         }
621                         pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
622                 }
623         }
624
625         if (!(VPIF_VALID_FIELD(field))) {
626                 if (!update) {
627                         vpif_dbg(2, debug, "invalid field format\n");
628                         goto exit;
629                 }
630                 /**
631                  * By default use FIELD_NONE for RAW Bayer capture
632                  * and FIELD_INTERLACED for other interfaces
633                  */
634                 field = vpif_get_default_field(&vpif_params->iface);
635         } else if (field == V4L2_FIELD_ANY)
636                 /* unsupported field. Use default */
637                 field = vpif_get_default_field(&vpif_params->iface);
638
639         /* validate the hpitch */
640         hpitch = pixfmt->bytesperline;
641         if (hpitch < vpif_params->std_info.width) {
642                 if (!update) {
643                         vpif_dbg(2, debug, "invalid hpitch\n");
644                         goto exit;
645                 }
646                 hpitch = vpif_params->std_info.width;
647         }
648
649         if (V4L2_MEMORY_USERPTR == common->memory)
650                 sizeimage = pixfmt->sizeimage;
651         else
652                 sizeimage = config_params.channel_bufsize[ch->channel_id];
653
654         vpitch = sizeimage / (hpitch * 2);
655
656         /* validate the vpitch */
657         if (vpitch < vpif_params->std_info.height) {
658                 if (!update) {
659                         vpif_dbg(2, debug, "Invalid vpitch\n");
660                         goto exit;
661                 }
662                 vpitch = vpif_params->std_info.height;
663         }
664
665         /* Check for 8 byte alignment */
666         if (!ALIGN(hpitch, 8)) {
667                 if (!update) {
668                         vpif_dbg(2, debug, "invalid pitch alignment\n");
669                         goto exit;
670                 }
671                 /* adjust to next 8 byte boundary */
672                 hpitch = (((hpitch + 7) / 8) * 8);
673         }
674         /* if update is set, modify the bytesperline and sizeimage */
675         if (update) {
676                 pixfmt->bytesperline = hpitch;
677                 pixfmt->sizeimage = hpitch * vpitch * 2;
678         }
679         /**
680          * Image width and height is always based on current standard width and
681          * height
682          */
683         pixfmt->width = common->fmt.fmt.pix.width;
684         pixfmt->height = common->fmt.fmt.pix.height;
685         return 0;
686 exit:
687         return ret;
688 }
689
690 /**
691  * vpif_config_addr() - function to configure buffer address in vpif
692  * @ch - channel ptr
693  * @muxmode - channel mux mode
694  */
695 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
696 {
697         struct common_obj *common;
698
699         vpif_dbg(2, debug, "vpif_config_addr\n");
700
701         common = &(ch->common[VPIF_VIDEO_INDEX]);
702
703         if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
704                 common->set_addr = ch1_set_videobuf_addr;
705         else if (2 == muxmode)
706                 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
707         else
708                 common->set_addr = ch0_set_videobuf_addr;
709 }
710
711 /**
712  * vpfe_mmap : It is used to map kernel space buffers into user spaces
713  * @filep: file pointer
714  * @vma: ptr to vm_area_struct
715  */
716 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
717 {
718         /* Get the channel object and file handle object */
719         struct vpif_fh *fh = filep->private_data;
720         struct channel_obj *ch = fh->channel;
721         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
722
723         vpif_dbg(2, debug, "vpif_mmap\n");
724
725         return videobuf_mmap_mapper(&common->buffer_queue, vma);
726 }
727
728 /**
729  * vpif_poll: It is used for select/poll system call
730  * @filep: file pointer
731  * @wait: poll table to wait
732  */
733 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
734 {
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                 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
743         return 0;
744 }
745
746 /**
747  * vpif_open : vpif open handler
748  * @filep: file ptr
749  *
750  * It creates object of file handle structure and stores it in private_data
751  * member of filepointer
752  */
753 static int vpif_open(struct file *filep)
754 {
755         struct vpif_capture_config *config = vpif_dev->platform_data;
756         struct video_device *vdev = video_devdata(filep);
757         struct common_obj *common;
758         struct video_obj *vid_ch;
759         struct channel_obj *ch;
760         struct vpif_fh *fh;
761         int i, ret = 0;
762
763         vpif_dbg(2, debug, "vpif_open\n");
764
765         ch = video_get_drvdata(vdev);
766
767         vid_ch = &ch->video;
768         common = &ch->common[VPIF_VIDEO_INDEX];
769
770         if (mutex_lock_interruptible(&common->lock))
771                 return -ERESTARTSYS;
772
773         if (NULL == ch->curr_subdev_info) {
774                 /**
775                  * search through the sub device to see a registered
776                  * sub device and make it as current sub device
777                  */
778                 for (i = 0; i < config->subdev_count; i++) {
779                         if (vpif_obj.sd[i]) {
780                                 /* the sub device is registered */
781                                 ch->curr_subdev_info = &config->subdev_info[i];
782                                 /* make first input as the current input */
783                                 vid_ch->input_idx = 0;
784                                 break;
785                         }
786                 }
787                 if (i == config->subdev_count) {
788                         vpif_err("No sub device registered\n");
789                         ret = -ENOENT;
790                         goto exit;
791                 }
792         }
793
794         /* Allocate memory for the file handle object */
795         fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
796         if (NULL == fh) {
797                 vpif_err("unable to allocate memory for file handle object\n");
798                 ret = -ENOMEM;
799                 goto exit;
800         }
801
802         /* store pointer to fh in private_data member of filep */
803         filep->private_data = fh;
804         fh->channel = ch;
805         fh->initialized = 0;
806         /* If decoder is not initialized. initialize it */
807         if (!ch->initialized) {
808                 fh->initialized = 1;
809                 ch->initialized = 1;
810                 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
811         }
812         /* Increment channel usrs counter */
813         ch->usrs++;
814         /* Set io_allowed member to false */
815         fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
816         /* Initialize priority of this instance to default priority */
817         fh->prio = V4L2_PRIORITY_UNSET;
818         v4l2_prio_open(&ch->prio, &fh->prio);
819 exit:
820         mutex_unlock(&common->lock);
821         return ret;
822 }
823
824 /**
825  * vpif_release : function to clean up file close
826  * @filep: file pointer
827  *
828  * This function deletes buffer queue, frees the buffers and the vpfe file
829  * handle
830  */
831 static int vpif_release(struct file *filep)
832 {
833         struct vpif_fh *fh = filep->private_data;
834         struct channel_obj *ch = fh->channel;
835         struct common_obj *common;
836
837         vpif_dbg(2, debug, "vpif_release\n");
838
839         common = &ch->common[VPIF_VIDEO_INDEX];
840
841         if (mutex_lock_interruptible(&common->lock))
842                 return -ERESTARTSYS;
843
844         /* if this instance is doing IO */
845         if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
846                 /* Reset io_usrs member of channel object */
847                 common->io_usrs = 0;
848                 /* Disable channel as per its device type and channel id */
849                 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
850                         enable_channel0(0);
851                         channel0_intr_enable(0);
852                 }
853                 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
854                     (2 == common->started)) {
855                         enable_channel1(0);
856                         channel1_intr_enable(0);
857                 }
858                 common->started = 0;
859                 /* Free buffers allocated */
860                 videobuf_queue_cancel(&common->buffer_queue);
861                 videobuf_mmap_free(&common->buffer_queue);
862         }
863
864         /* Decrement channel usrs counter */
865         ch->usrs--;
866
867         /* unlock mutex on channel object */
868         mutex_unlock(&common->lock);
869
870         /* Close the priority */
871         v4l2_prio_close(&ch->prio, fh->prio);
872
873         if (fh->initialized)
874                 ch->initialized = 0;
875
876         filep->private_data = NULL;
877         kfree(fh);
878         return 0;
879 }
880
881 /**
882  * vpif_reqbufs() - request buffer handler
883  * @file: file ptr
884  * @priv: file handle
885  * @reqbuf: request buffer structure ptr
886  */
887 static int vpif_reqbufs(struct file *file, void *priv,
888                         struct v4l2_requestbuffers *reqbuf)
889 {
890         struct vpif_fh *fh = priv;
891         struct channel_obj *ch = fh->channel;
892         struct common_obj *common;
893         u8 index = 0;
894         int ret = 0;
895
896         vpif_dbg(2, debug, "vpif_reqbufs\n");
897
898         /**
899          * This file handle has not initialized the channel,
900          * It is not allowed to do settings
901          */
902         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
903             || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
904                 if (!fh->initialized) {
905                         vpif_dbg(1, debug, "Channel Busy\n");
906                         return -EBUSY;
907                 }
908         }
909
910         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
911                 return -EINVAL;
912
913         index = VPIF_VIDEO_INDEX;
914
915         common = &ch->common[index];
916
917         if (mutex_lock_interruptible(&common->lock))
918                 return -ERESTARTSYS;
919
920         if (0 != common->io_usrs) {
921                 ret = -EBUSY;
922                 goto reqbuf_exit;
923         }
924
925         /* Initialize videobuf queue as per the buffer type */
926         videobuf_queue_dma_contig_init(&common->buffer_queue,
927                                             &video_qops, NULL,
928                                             &common->irqlock,
929                                             reqbuf->type,
930                                             common->fmt.fmt.pix.field,
931                                             sizeof(struct videobuf_buffer), fh,
932                                             NULL);
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 /*
1812  * vpif_g_chip_ident() - Identify the chip
1813  * @file: file ptr
1814  * @priv: file handle
1815  * @chip: chip identity
1816  *
1817  * Returns zero or -EINVAL if read operations fails.
1818  */
1819 static int vpif_g_chip_ident(struct file *file, void *priv,
1820                 struct v4l2_dbg_chip_ident *chip)
1821 {
1822         chip->ident = V4L2_IDENT_NONE;
1823         chip->revision = 0;
1824         if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1825                         chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1826                 vpif_dbg(2, debug, "match_type is invalid.\n");
1827                 return -EINVAL;
1828         }
1829
1830         return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1831                         g_chip_ident, chip);
1832 }
1833
1834 #ifdef CONFIG_VIDEO_ADV_DEBUG
1835 /*
1836  * vpif_dbg_g_register() - Read register
1837  * @file: file ptr
1838  * @priv: file handle
1839  * @reg: register to be read
1840  *
1841  * Debugging only
1842  * Returns zero or -EINVAL if read operations fails.
1843  */
1844 static int vpif_dbg_g_register(struct file *file, void *priv,
1845                 struct v4l2_dbg_register *reg){
1846         struct vpif_fh *fh = priv;
1847         struct channel_obj *ch = fh->channel;
1848
1849         return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1850                         g_register, reg);
1851 }
1852
1853 /*
1854  * vpif_dbg_s_register() - Write to register
1855  * @file: file ptr
1856  * @priv: file handle
1857  * @reg: register to be modified
1858  *
1859  * Debugging only
1860  * Returns zero or -EINVAL if write operations fails.
1861  */
1862 static int vpif_dbg_s_register(struct file *file, void *priv,
1863                 struct v4l2_dbg_register *reg){
1864         struct vpif_fh *fh = priv;
1865         struct channel_obj *ch = fh->channel;
1866
1867         return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1868                         s_register, reg);
1869 }
1870 #endif
1871
1872 /*
1873  * vpif_log_status() - Status information
1874  * @file: file ptr
1875  * @priv: file handle
1876  *
1877  * Returns zero.
1878  */
1879 static int vpif_log_status(struct file *filep, void *priv)
1880 {
1881         /* status for sub devices */
1882         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1883
1884         return 0;
1885 }
1886
1887 /* vpif capture ioctl operations */
1888 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1889         .vidioc_querycap                = vpif_querycap,
1890         .vidioc_g_priority              = vpif_g_priority,
1891         .vidioc_s_priority              = vpif_s_priority,
1892         .vidioc_enum_fmt_vid_cap        = vpif_enum_fmt_vid_cap,
1893         .vidioc_g_fmt_vid_cap           = vpif_g_fmt_vid_cap,
1894         .vidioc_s_fmt_vid_cap           = vpif_s_fmt_vid_cap,
1895         .vidioc_try_fmt_vid_cap         = vpif_try_fmt_vid_cap,
1896         .vidioc_enum_input              = vpif_enum_input,
1897         .vidioc_s_input                 = vpif_s_input,
1898         .vidioc_g_input                 = vpif_g_input,
1899         .vidioc_reqbufs                 = vpif_reqbufs,
1900         .vidioc_querybuf                = vpif_querybuf,
1901         .vidioc_querystd                = vpif_querystd,
1902         .vidioc_s_std                   = vpif_s_std,
1903         .vidioc_g_std                   = vpif_g_std,
1904         .vidioc_qbuf                    = vpif_qbuf,
1905         .vidioc_dqbuf                   = vpif_dqbuf,
1906         .vidioc_streamon                = vpif_streamon,
1907         .vidioc_streamoff               = vpif_streamoff,
1908         .vidioc_cropcap                 = vpif_cropcap,
1909         .vidioc_g_chip_ident            = vpif_g_chip_ident,
1910 #ifdef CONFIG_VIDEO_ADV_DEBUG
1911         .vidioc_g_register              = vpif_dbg_g_register,
1912         .vidioc_s_register              = vpif_dbg_s_register,
1913 #endif
1914         .vidioc_log_status              = vpif_log_status,
1915 };
1916
1917 /* vpif file operations */
1918 static struct v4l2_file_operations vpif_fops = {
1919         .owner = THIS_MODULE,
1920         .open = vpif_open,
1921         .release = vpif_release,
1922         .ioctl = video_ioctl2,
1923         .mmap = vpif_mmap,
1924         .poll = vpif_poll
1925 };
1926
1927 /* vpif video template */
1928 static struct video_device vpif_video_template = {
1929         .name           = "vpif",
1930         .fops           = &vpif_fops,
1931         .minor          = -1,
1932         .ioctl_ops      = &vpif_ioctl_ops,
1933 };
1934
1935 /**
1936  * initialize_vpif() - Initialize vpif data structures
1937  *
1938  * Allocate memory for data structures and initialize them
1939  */
1940 static int initialize_vpif(void)
1941 {
1942         int err = 0, i, j;
1943         int free_channel_objects_index;
1944
1945         /* Default number of buffers should be 3 */
1946         if ((ch0_numbuffers > 0) &&
1947             (ch0_numbuffers < config_params.min_numbuffers))
1948                 ch0_numbuffers = config_params.min_numbuffers;
1949         if ((ch1_numbuffers > 0) &&
1950             (ch1_numbuffers < config_params.min_numbuffers))
1951                 ch1_numbuffers = config_params.min_numbuffers;
1952
1953         /* Set buffer size to min buffers size if it is invalid */
1954         if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1955                 ch0_bufsize =
1956                     config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1957         if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1958                 ch1_bufsize =
1959                     config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1960
1961         config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1962         config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1963         if (ch0_numbuffers) {
1964                 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1965                     = ch0_bufsize;
1966         }
1967         if (ch1_numbuffers) {
1968                 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1969                     = ch1_bufsize;
1970         }
1971
1972         /* Allocate memory for six channel objects */
1973         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1974                 vpif_obj.dev[i] =
1975                     kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1976                 /* If memory allocation fails, return error */
1977                 if (!vpif_obj.dev[i]) {
1978                         free_channel_objects_index = i;
1979                         err = -ENOMEM;
1980                         goto vpif_init_free_channel_objects;
1981                 }
1982         }
1983         return 0;
1984
1985 vpif_init_free_channel_objects:
1986         for (j = 0; j < free_channel_objects_index; j++)
1987                 kfree(vpif_obj.dev[j]);
1988         return err;
1989 }
1990
1991 /**
1992  * vpif_probe : This function probes the vpif capture driver
1993  * @pdev: platform device pointer
1994  *
1995  * This creates device entries by register itself to the V4L2 driver and
1996  * initializes fields of each channel objects
1997  */
1998 static __init int vpif_probe(struct platform_device *pdev)
1999 {
2000         struct vpif_subdev_info *subdevdata;
2001         struct vpif_capture_config *config;
2002         int i, j, k, m, q, err;
2003         struct i2c_adapter *i2c_adap;
2004         struct channel_obj *ch;
2005         struct common_obj *common;
2006         struct video_device *vfd;
2007         struct resource *res;
2008         int subdev_count;
2009
2010         vpif_dev = &pdev->dev;
2011
2012         err = initialize_vpif();
2013         if (err) {
2014                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2015                 return err;
2016         }
2017
2018         k = 0;
2019         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2020                 for (i = res->start; i <= res->end; i++) {
2021                         if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
2022                                         "DM646x_Capture",
2023                                 (void *)(&vpif_obj.dev[k]->channel_id))) {
2024                                 err = -EBUSY;
2025                                 i--;
2026                                 goto vpif_int_err;
2027                         }
2028                 }
2029                 k++;
2030         }
2031
2032         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2033                 /* Get the pointer to the channel object */
2034                 ch = vpif_obj.dev[i];
2035                 /* Allocate memory for video device */
2036                 vfd = video_device_alloc();
2037                 if (NULL == vfd) {
2038                         for (j = 0; j < i; j++) {
2039                                 ch = vpif_obj.dev[j];
2040                                 video_device_release(ch->video_dev);
2041                         }
2042                         err = -ENOMEM;
2043                         goto vpif_dev_alloc_err;
2044                 }
2045
2046                 /* Initialize field of video device */
2047                 *vfd = vpif_video_template;
2048                 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2049                 vfd->release = video_device_release;
2050                 snprintf(vfd->name, sizeof(vfd->name),
2051                          "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
2052                          (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
2053                          (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
2054                          (VPIF_CAPTURE_VERSION_CODE) & 0xff);
2055                 /* Set video_dev to the video device */
2056                 ch->video_dev = vfd;
2057         }
2058
2059         for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2060                 ch = vpif_obj.dev[j];
2061                 ch->channel_id = j;
2062                 common = &(ch->common[VPIF_VIDEO_INDEX]);
2063                 spin_lock_init(&common->irqlock);
2064                 mutex_init(&common->lock);
2065                 /* Initialize prio member of channel object */
2066                 v4l2_prio_init(&ch->prio);
2067                 err = video_register_device(ch->video_dev,
2068                                             VFL_TYPE_GRABBER, (j ? 1 : 0));
2069                 if (err)
2070                         goto probe_out;
2071
2072                 video_set_drvdata(ch->video_dev, ch);
2073
2074         }
2075
2076         i2c_adap = i2c_get_adapter(1);
2077         config = pdev->dev.platform_data;
2078
2079         subdev_count = config->subdev_count;
2080         vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2081                                 GFP_KERNEL);
2082         if (vpif_obj.sd == NULL) {
2083                 vpif_err("unable to allocate memory for subdevice pointers\n");
2084                 err = -ENOMEM;
2085                 goto probe_out;
2086         }
2087
2088         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2089         if (err) {
2090                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2091                 goto probe_subdev_out;
2092         }
2093
2094         for (i = 0; i < subdev_count; i++) {
2095                 subdevdata = &config->subdev_info[i];
2096                 vpif_obj.sd[i] =
2097                         v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2098                                                   i2c_adap,
2099                                                   &subdevdata->board_info,
2100                                                   NULL);
2101
2102                 if (!vpif_obj.sd[i]) {
2103                         vpif_err("Error registering v4l2 subdevice\n");
2104                         goto probe_subdev_out;
2105                 }
2106                 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2107                           subdevdata->name);
2108
2109                 if (vpif_obj.sd[i])
2110                         vpif_obj.sd[i]->grp_id = 1 << i;
2111         }
2112         v4l2_info(&vpif_obj.v4l2_dev, "DM646x VPIF Capture driver"
2113                   " initialized\n");
2114
2115         return 0;
2116
2117 probe_subdev_out:
2118         /* free sub devices memory */
2119         kfree(vpif_obj.sd);
2120
2121         j = VPIF_CAPTURE_MAX_DEVICES;
2122 probe_out:
2123         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2124         for (k = 0; k < j; k++) {
2125                 /* Get the pointer to the channel object */
2126                 ch = vpif_obj.dev[k];
2127                 /* Unregister video device */
2128                 video_unregister_device(ch->video_dev);
2129         }
2130
2131 vpif_dev_alloc_err:
2132         k = VPIF_CAPTURE_MAX_DEVICES-1;
2133         res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2134         i = res->end;
2135
2136 vpif_int_err:
2137         for (q = k; q >= 0; q--) {
2138                 for (m = i; m >= (int)res->start; m--)
2139                         free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2140
2141                 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2142                 if (res)
2143                         i = res->end;
2144         }
2145         return err;
2146 }
2147
2148 /**
2149  * vpif_remove() - driver remove handler
2150  * @device: ptr to platform device structure
2151  *
2152  * The vidoe device is unregistered
2153  */
2154 static int vpif_remove(struct platform_device *device)
2155 {
2156         int i;
2157         struct channel_obj *ch;
2158
2159         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2160
2161         /* un-register device */
2162         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2163                 /* Get the pointer to the channel object */
2164                 ch = vpif_obj.dev[i];
2165                 /* Unregister video device */
2166                 video_unregister_device(ch->video_dev);
2167         }
2168         return 0;
2169 }
2170
2171 /**
2172  * vpif_suspend: vpif device suspend
2173  *
2174  * TODO: Add suspend code here
2175  */
2176 static int
2177 vpif_suspend(struct device *dev)
2178 {
2179         return -1;
2180 }
2181
2182 /**
2183  * vpif_resume: vpif device suspend
2184  *
2185  * TODO: Add resume code here
2186  */
2187 static int
2188 vpif_resume(struct device *dev)
2189 {
2190         return -1;
2191 }
2192
2193 static const struct dev_pm_ops vpif_dev_pm_ops = {
2194         .suspend = vpif_suspend,
2195         .resume = vpif_resume,
2196 };
2197
2198 static __refdata struct platform_driver vpif_driver = {
2199         .driver = {
2200                 .name   = "vpif_capture",
2201                 .owner  = THIS_MODULE,
2202                 .pm = &vpif_dev_pm_ops,
2203         },
2204         .probe = vpif_probe,
2205         .remove = vpif_remove,
2206 };
2207
2208 /**
2209  * vpif_init: initialize the vpif driver
2210  *
2211  * This function registers device and driver to the kernel, requests irq
2212  * handler and allocates memory
2213  * for channel objects
2214  */
2215 static __init int vpif_init(void)
2216 {
2217         return platform_driver_register(&vpif_driver);
2218 }
2219
2220 /**
2221  * vpif_cleanup : This function clean up the vpif capture resources
2222  *
2223  * This will un-registers device and driver to the kernel, frees
2224  * requested irq handler and de-allocates memory allocated for channel
2225  * objects.
2226  */
2227 static void vpif_cleanup(void)
2228 {
2229         struct platform_device *pdev;
2230         struct resource *res;
2231         int irq_num;
2232         int i = 0;
2233
2234         pdev = container_of(vpif_dev, struct platform_device, dev);
2235         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2236                 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2237                         free_irq(irq_num,
2238                                  (void *)(&vpif_obj.dev[i]->channel_id));
2239                 i++;
2240         }
2241
2242         platform_driver_unregister(&vpif_driver);
2243
2244         kfree(vpif_obj.sd);
2245         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2246                 kfree(vpif_obj.dev[i]);
2247 }
2248
2249 /* Function for module initialization and cleanup */
2250 module_init(vpif_init);
2251 module_exit(vpif_cleanup);