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