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