Merge remote-tracking branches 'spi/fix/qup' and 'spi/fix/topcliff-pch' into spi...
[cascardo/linux.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         /* Disable channel as per its device type and channel id */
362         if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
363                 enable_channel0(0);
364                 channel0_intr_enable(0);
365         }
366         if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
367                 (2 == common->started)) {
368                 enable_channel1(0);
369                 channel1_intr_enable(0);
370         }
371         common->started = 0;
372
373         /* release all active buffers */
374         spin_lock_irqsave(&common->irqlock, flags);
375         if (common->cur_frm == common->next_frm) {
376                 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
377         } else {
378                 if (common->cur_frm != NULL)
379                         vb2_buffer_done(&common->cur_frm->vb,
380                                         VB2_BUF_STATE_ERROR);
381                 if (common->next_frm != NULL)
382                         vb2_buffer_done(&common->next_frm->vb,
383                                         VB2_BUF_STATE_ERROR);
384         }
385
386         while (!list_empty(&common->dma_queue)) {
387                 common->next_frm = list_entry(common->dma_queue.next,
388                                                 struct vpif_cap_buffer, list);
389                 list_del(&common->next_frm->list);
390                 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
391         }
392         spin_unlock_irqrestore(&common->irqlock, flags);
393
394         return 0;
395 }
396
397 static struct vb2_ops video_qops = {
398         .queue_setup            = vpif_buffer_queue_setup,
399         .wait_prepare           = vpif_wait_prepare,
400         .wait_finish            = vpif_wait_finish,
401         .buf_init               = vpif_buffer_init,
402         .buf_prepare            = vpif_buffer_prepare,
403         .start_streaming        = vpif_start_streaming,
404         .stop_streaming         = vpif_stop_streaming,
405         .buf_cleanup            = vpif_buf_cleanup,
406         .buf_queue              = vpif_buffer_queue,
407 };
408
409 /**
410  * vpif_process_buffer_complete: process a completed buffer
411  * @common: ptr to common channel object
412  *
413  * This function time stamp the buffer and mark it as DONE. It also
414  * wake up any process waiting on the QUEUE and set the next buffer
415  * as current
416  */
417 static void vpif_process_buffer_complete(struct common_obj *common)
418 {
419         v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
420         vb2_buffer_done(&common->cur_frm->vb,
421                                             VB2_BUF_STATE_DONE);
422         /* Make curFrm pointing to nextFrm */
423         common->cur_frm = common->next_frm;
424 }
425
426 /**
427  * vpif_schedule_next_buffer: set next buffer address for capture
428  * @common : ptr to common channel object
429  *
430  * This function will get next buffer from the dma queue and
431  * set the buffer address in the vpif register for capture.
432  * the buffer is marked active
433  */
434 static void vpif_schedule_next_buffer(struct common_obj *common)
435 {
436         unsigned long addr = 0;
437
438         spin_lock(&common->irqlock);
439         common->next_frm = list_entry(common->dma_queue.next,
440                                      struct vpif_cap_buffer, list);
441         /* Remove that buffer from the buffer queue */
442         list_del(&common->next_frm->list);
443         spin_unlock(&common->irqlock);
444         common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
445         addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
446
447         /* Set top and bottom field addresses in VPIF registers */
448         common->set_addr(addr + common->ytop_off,
449                          addr + common->ybtm_off,
450                          addr + common->ctop_off,
451                          addr + common->cbtm_off);
452 }
453
454 /**
455  * vpif_channel_isr : ISR handler for vpif capture
456  * @irq: irq number
457  * @dev_id: dev_id ptr
458  *
459  * It changes status of the captured buffer, takes next buffer from the queue
460  * and sets its address in VPIF registers
461  */
462 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
463 {
464         struct vpif_device *dev = &vpif_obj;
465         struct common_obj *common;
466         struct channel_obj *ch;
467         enum v4l2_field field;
468         int channel_id = 0;
469         int fid = -1, i;
470
471         channel_id = *(int *)(dev_id);
472         if (!vpif_intr_status(channel_id))
473                 return IRQ_NONE;
474
475         ch = dev->dev[channel_id];
476
477         field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
478
479         for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
480                 common = &ch->common[i];
481                 /* skip If streaming is not started in this channel */
482                 if (0 == common->started)
483                         continue;
484
485                 /* Check the field format */
486                 if (1 == ch->vpifparams.std_info.frm_fmt) {
487                         /* Progressive mode */
488                         spin_lock(&common->irqlock);
489                         if (list_empty(&common->dma_queue)) {
490                                 spin_unlock(&common->irqlock);
491                                 continue;
492                         }
493                         spin_unlock(&common->irqlock);
494
495                         if (!channel_first_int[i][channel_id])
496                                 vpif_process_buffer_complete(common);
497
498                         channel_first_int[i][channel_id] = 0;
499
500                         vpif_schedule_next_buffer(common);
501
502
503                         channel_first_int[i][channel_id] = 0;
504                 } else {
505                         /**
506                          * Interlaced mode. If it is first interrupt, ignore
507                          * it
508                          */
509                         if (channel_first_int[i][channel_id]) {
510                                 channel_first_int[i][channel_id] = 0;
511                                 continue;
512                         }
513                         if (0 == i) {
514                                 ch->field_id ^= 1;
515                                 /* Get field id from VPIF registers */
516                                 fid = vpif_channel_getfid(ch->channel_id);
517                                 if (fid != ch->field_id) {
518                                         /**
519                                          * If field id does not match stored
520                                          * field id, make them in sync
521                                          */
522                                         if (0 == fid)
523                                                 ch->field_id = fid;
524                                         return IRQ_HANDLED;
525                                 }
526                         }
527                         /* device field id and local field id are in sync */
528                         if (0 == fid) {
529                                 /* this is even field */
530                                 if (common->cur_frm == common->next_frm)
531                                         continue;
532
533                                 /* mark the current buffer as done */
534                                 vpif_process_buffer_complete(common);
535                         } else if (1 == fid) {
536                                 /* odd field */
537                                 spin_lock(&common->irqlock);
538                                 if (list_empty(&common->dma_queue) ||
539                                     (common->cur_frm != common->next_frm)) {
540                                         spin_unlock(&common->irqlock);
541                                         continue;
542                                 }
543                                 spin_unlock(&common->irqlock);
544
545                                 vpif_schedule_next_buffer(common);
546                         }
547                 }
548         }
549         return IRQ_HANDLED;
550 }
551
552 /**
553  * vpif_update_std_info() - update standard related info
554  * @ch: ptr to channel object
555  *
556  * For a given standard selected by application, update values
557  * in the device data structures
558  */
559 static int vpif_update_std_info(struct channel_obj *ch)
560 {
561         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
562         struct vpif_params *vpifparams = &ch->vpifparams;
563         const struct vpif_channel_config_params *config;
564         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
565         struct video_obj *vid_ch = &ch->video;
566         int index;
567
568         vpif_dbg(2, debug, "vpif_update_std_info\n");
569
570         for (index = 0; index < vpif_ch_params_count; index++) {
571                 config = &vpif_ch_params[index];
572                 if (config->hd_sd == 0) {
573                         vpif_dbg(2, debug, "SD format\n");
574                         if (config->stdid & vid_ch->stdid) {
575                                 memcpy(std_info, config, sizeof(*config));
576                                 break;
577                         }
578                 } else {
579                         vpif_dbg(2, debug, "HD format\n");
580                         if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
581                                 sizeof(vid_ch->dv_timings))) {
582                                 memcpy(std_info, config, sizeof(*config));
583                                 break;
584                         }
585                 }
586         }
587
588         /* standard not found */
589         if (index == vpif_ch_params_count)
590                 return -EINVAL;
591
592         common->fmt.fmt.pix.width = std_info->width;
593         common->width = std_info->width;
594         common->fmt.fmt.pix.height = std_info->height;
595         common->height = std_info->height;
596         common->fmt.fmt.pix.bytesperline = std_info->width;
597         vpifparams->video_params.hpitch = std_info->width;
598         vpifparams->video_params.storage_mode = std_info->frm_fmt;
599
600         return 0;
601 }
602
603 /**
604  * vpif_calculate_offsets : This function calculates buffers offsets
605  * @ch : ptr to channel object
606  *
607  * This function calculates buffer offsets for Y and C in the top and
608  * bottom field
609  */
610 static void vpif_calculate_offsets(struct channel_obj *ch)
611 {
612         unsigned int hpitch, vpitch, sizeimage;
613         struct video_obj *vid_ch = &(ch->video);
614         struct vpif_params *vpifparams = &ch->vpifparams;
615         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
616         enum v4l2_field field = common->fmt.fmt.pix.field;
617
618         vpif_dbg(2, debug, "vpif_calculate_offsets\n");
619
620         if (V4L2_FIELD_ANY == field) {
621                 if (vpifparams->std_info.frm_fmt)
622                         vid_ch->buf_field = V4L2_FIELD_NONE;
623                 else
624                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
625         } else
626                 vid_ch->buf_field = common->fmt.fmt.pix.field;
627
628         sizeimage = common->fmt.fmt.pix.sizeimage;
629
630         hpitch = common->fmt.fmt.pix.bytesperline;
631         vpitch = sizeimage / (hpitch * 2);
632
633         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
634             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
635                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
636                 common->ytop_off = 0;
637                 common->ybtm_off = hpitch;
638                 common->ctop_off = sizeimage / 2;
639                 common->cbtm_off = sizeimage / 2 + hpitch;
640         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
641                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
642                 common->ytop_off = 0;
643                 common->ybtm_off = sizeimage / 4;
644                 common->ctop_off = sizeimage / 2;
645                 common->cbtm_off = common->ctop_off + sizeimage / 4;
646         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
647                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
648                 common->ybtm_off = 0;
649                 common->ytop_off = sizeimage / 4;
650                 common->cbtm_off = sizeimage / 2;
651                 common->ctop_off = common->cbtm_off + sizeimage / 4;
652         }
653         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
654             (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
655                 vpifparams->video_params.storage_mode = 1;
656         else
657                 vpifparams->video_params.storage_mode = 0;
658
659         if (1 == vpifparams->std_info.frm_fmt)
660                 vpifparams->video_params.hpitch =
661                     common->fmt.fmt.pix.bytesperline;
662         else {
663                 if ((field == V4L2_FIELD_ANY)
664                     || (field == V4L2_FIELD_INTERLACED))
665                         vpifparams->video_params.hpitch =
666                             common->fmt.fmt.pix.bytesperline * 2;
667                 else
668                         vpifparams->video_params.hpitch =
669                             common->fmt.fmt.pix.bytesperline;
670         }
671
672         ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
673 }
674
675 /**
676  * vpif_config_format: configure default frame format in the device
677  * ch : ptr to channel object
678  */
679 static void vpif_config_format(struct channel_obj *ch)
680 {
681         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
682
683         vpif_dbg(2, debug, "vpif_config_format\n");
684
685         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
686         if (config_params.numbuffers[ch->channel_id] == 0)
687                 common->memory = V4L2_MEMORY_USERPTR;
688         else
689                 common->memory = V4L2_MEMORY_MMAP;
690
691         common->fmt.fmt.pix.sizeimage
692             = config_params.channel_bufsize[ch->channel_id];
693
694         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
695                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
696         else
697                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
698         common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
699 }
700
701 /**
702  * vpif_get_default_field() - Get default field type based on interface
703  * @vpif_params - ptr to vpif params
704  */
705 static inline enum v4l2_field vpif_get_default_field(
706                                 struct vpif_interface *iface)
707 {
708         return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
709                                                 V4L2_FIELD_INTERLACED;
710 }
711
712 /**
713  * vpif_check_format()  - check given pixel format for compatibility
714  * @ch - channel  ptr
715  * @pixfmt - Given pixel format
716  * @update - update the values as per hardware requirement
717  *
718  * Check the application pixel format for S_FMT and update the input
719  * values as per hardware limits for TRY_FMT. The default pixel and
720  * field format is selected based on interface type.
721  */
722 static int vpif_check_format(struct channel_obj *ch,
723                              struct v4l2_pix_format *pixfmt,
724                              int update)
725 {
726         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
727         struct vpif_params *vpif_params = &ch->vpifparams;
728         enum v4l2_field field = pixfmt->field;
729         u32 sizeimage, hpitch, vpitch;
730         int ret = -EINVAL;
731
732         vpif_dbg(2, debug, "vpif_check_format\n");
733         /**
734          * first check for the pixel format. If if_type is Raw bayer,
735          * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
736          * V4L2_PIX_FMT_YUV422P is supported
737          */
738         if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
739                 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
740                         if (!update) {
741                                 vpif_dbg(2, debug, "invalid pix format\n");
742                                 goto exit;
743                         }
744                         pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
745                 }
746         } else {
747                 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
748                         if (!update) {
749                                 vpif_dbg(2, debug, "invalid pixel format\n");
750                                 goto exit;
751                         }
752                         pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
753                 }
754         }
755
756         if (!(VPIF_VALID_FIELD(field))) {
757                 if (!update) {
758                         vpif_dbg(2, debug, "invalid field format\n");
759                         goto exit;
760                 }
761                 /**
762                  * By default use FIELD_NONE for RAW Bayer capture
763                  * and FIELD_INTERLACED for other interfaces
764                  */
765                 field = vpif_get_default_field(&vpif_params->iface);
766         } else if (field == V4L2_FIELD_ANY)
767                 /* unsupported field. Use default */
768                 field = vpif_get_default_field(&vpif_params->iface);
769
770         /* validate the hpitch */
771         hpitch = pixfmt->bytesperline;
772         if (hpitch < vpif_params->std_info.width) {
773                 if (!update) {
774                         vpif_dbg(2, debug, "invalid hpitch\n");
775                         goto exit;
776                 }
777                 hpitch = vpif_params->std_info.width;
778         }
779
780         sizeimage = pixfmt->sizeimage;
781
782         vpitch = sizeimage / (hpitch * 2);
783
784         /* validate the vpitch */
785         if (vpitch < vpif_params->std_info.height) {
786                 if (!update) {
787                         vpif_dbg(2, debug, "Invalid vpitch\n");
788                         goto exit;
789                 }
790                 vpitch = vpif_params->std_info.height;
791         }
792
793         /* Check for 8 byte alignment */
794         if (!ALIGN(hpitch, 8)) {
795                 if (!update) {
796                         vpif_dbg(2, debug, "invalid pitch alignment\n");
797                         goto exit;
798                 }
799                 /* adjust to next 8 byte boundary */
800                 hpitch = (((hpitch + 7) / 8) * 8);
801         }
802         /* if update is set, modify the bytesperline and sizeimage */
803         if (update) {
804                 pixfmt->bytesperline = hpitch;
805                 pixfmt->sizeimage = hpitch * vpitch * 2;
806         }
807         /**
808          * Image width and height is always based on current standard width and
809          * height
810          */
811         pixfmt->width = common->fmt.fmt.pix.width;
812         pixfmt->height = common->fmt.fmt.pix.height;
813         return 0;
814 exit:
815         return ret;
816 }
817
818 /**
819  * vpif_config_addr() - function to configure buffer address in vpif
820  * @ch - channel ptr
821  * @muxmode - channel mux mode
822  */
823 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
824 {
825         struct common_obj *common;
826
827         vpif_dbg(2, debug, "vpif_config_addr\n");
828
829         common = &(ch->common[VPIF_VIDEO_INDEX]);
830
831         if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
832                 common->set_addr = ch1_set_videobuf_addr;
833         else if (2 == muxmode)
834                 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
835         else
836                 common->set_addr = ch0_set_videobuf_addr;
837 }
838
839 /**
840  * vpif_mmap : It is used to map kernel space buffers into user spaces
841  * @filep: file pointer
842  * @vma: ptr to vm_area_struct
843  */
844 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
845 {
846         /* Get the channel object and file handle object */
847         struct vpif_fh *fh = filep->private_data;
848         struct channel_obj *ch = fh->channel;
849         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
850         int ret;
851
852         vpif_dbg(2, debug, "vpif_mmap\n");
853
854         if (mutex_lock_interruptible(&common->lock))
855                 return -ERESTARTSYS;
856         ret = vb2_mmap(&common->buffer_queue, vma);
857         mutex_unlock(&common->lock);
858         return ret;
859 }
860
861 /**
862  * vpif_poll: It is used for select/poll system call
863  * @filep: file pointer
864  * @wait: poll table to wait
865  */
866 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
867 {
868         struct vpif_fh *fh = filep->private_data;
869         struct channel_obj *channel = fh->channel;
870         struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
871         unsigned int res = 0;
872
873         vpif_dbg(2, debug, "vpif_poll\n");
874
875         if (common->started) {
876                 mutex_lock(&common->lock);
877                 res = vb2_poll(&common->buffer_queue, filep, wait);
878                 mutex_unlock(&common->lock);
879         }
880         return res;
881 }
882
883 /**
884  * vpif_open : vpif open handler
885  * @filep: file ptr
886  *
887  * It creates object of file handle structure and stores it in private_data
888  * member of filepointer
889  */
890 static int vpif_open(struct file *filep)
891 {
892         struct video_device *vdev = video_devdata(filep);
893         struct common_obj *common;
894         struct video_obj *vid_ch;
895         struct channel_obj *ch;
896         struct vpif_fh *fh;
897
898         vpif_dbg(2, debug, "vpif_open\n");
899
900         ch = video_get_drvdata(vdev);
901
902         vid_ch = &ch->video;
903         common = &ch->common[VPIF_VIDEO_INDEX];
904
905         /* Allocate memory for the file handle object */
906         fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
907         if (NULL == fh) {
908                 vpif_err("unable to allocate memory for file handle object\n");
909                 return -ENOMEM;
910         }
911
912         if (mutex_lock_interruptible(&common->lock)) {
913                 kfree(fh);
914                 return -ERESTARTSYS;
915         }
916         /* store pointer to fh in private_data member of filep */
917         filep->private_data = fh;
918         fh->channel = ch;
919         fh->initialized = 0;
920         /* If decoder is not initialized. initialize it */
921         if (!ch->initialized) {
922                 fh->initialized = 1;
923                 ch->initialized = 1;
924                 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
925         }
926         /* Increment channel usrs counter */
927         ch->usrs++;
928         /* Set io_allowed member to false */
929         fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
930         /* Initialize priority of this instance to default priority */
931         fh->prio = V4L2_PRIORITY_UNSET;
932         v4l2_prio_open(&ch->prio, &fh->prio);
933         mutex_unlock(&common->lock);
934         return 0;
935 }
936
937 /**
938  * vpif_release : function to clean up file close
939  * @filep: file pointer
940  *
941  * This function deletes buffer queue, frees the buffers and the vpif file
942  * handle
943  */
944 static int vpif_release(struct file *filep)
945 {
946         struct vpif_fh *fh = filep->private_data;
947         struct channel_obj *ch = fh->channel;
948         struct common_obj *common;
949
950         vpif_dbg(2, debug, "vpif_release\n");
951
952         common = &ch->common[VPIF_VIDEO_INDEX];
953
954         mutex_lock(&common->lock);
955         /* if this instance is doing IO */
956         if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
957                 /* Reset io_usrs member of channel object */
958                 common->io_usrs = 0;
959                 /* Free buffers allocated */
960                 vb2_queue_release(&common->buffer_queue);
961                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
962         }
963
964         /* Decrement channel usrs counter */
965         ch->usrs--;
966
967         /* Close the priority */
968         v4l2_prio_close(&ch->prio, fh->prio);
969
970         if (fh->initialized)
971                 ch->initialized = 0;
972
973         mutex_unlock(&common->lock);
974         filep->private_data = NULL;
975         kfree(fh);
976         return 0;
977 }
978
979 /**
980  * vpif_reqbufs() - request buffer handler
981  * @file: file ptr
982  * @priv: file handle
983  * @reqbuf: request buffer structure ptr
984  */
985 static int vpif_reqbufs(struct file *file, void *priv,
986                         struct v4l2_requestbuffers *reqbuf)
987 {
988         struct vpif_fh *fh = priv;
989         struct channel_obj *ch = fh->channel;
990         struct common_obj *common;
991         u8 index = 0;
992         struct vb2_queue *q;
993         int ret;
994
995         vpif_dbg(2, debug, "vpif_reqbufs\n");
996
997         /**
998          * This file handle has not initialized the channel,
999          * It is not allowed to do settings
1000          */
1001         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
1002             || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1003                 if (!fh->initialized) {
1004                         vpif_dbg(1, debug, "Channel Busy\n");
1005                         return -EBUSY;
1006                 }
1007         }
1008
1009         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
1010                 return -EINVAL;
1011
1012         index = VPIF_VIDEO_INDEX;
1013
1014         common = &ch->common[index];
1015
1016         if (0 != common->io_usrs)
1017                 return -EBUSY;
1018
1019         /* Initialize videobuf2 queue as per the buffer type */
1020         common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1021         if (IS_ERR(common->alloc_ctx)) {
1022                 vpif_err("Failed to get the context\n");
1023                 return PTR_ERR(common->alloc_ctx);
1024         }
1025         q = &common->buffer_queue;
1026         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1027         q->io_modes = VB2_MMAP | VB2_USERPTR;
1028         q->drv_priv = fh;
1029         q->ops = &video_qops;
1030         q->mem_ops = &vb2_dma_contig_memops;
1031         q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1032         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1033         q->min_buffers_needed = 1;
1034
1035         ret = vb2_queue_init(q);
1036         if (ret) {
1037                 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1038                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1039                 return ret;
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(ch->sd, video, s_stream, 1);
1179
1180         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1181                 vpif_dbg(1, debug, "stream on failed in subdev\n");
1182                 return ret;
1183         }
1184
1185         /* Call vb2_streamon to start streaming in videobuf2 */
1186         ret = vb2_streamon(&common->buffer_queue, buftype);
1187         if (ret) {
1188                 vpif_dbg(1, debug, "vb2_streamon\n");
1189                 return ret;
1190         }
1191
1192         return ret;
1193 }
1194
1195 /**
1196  * vpif_streamoff() - streamoff handler
1197  * @file: file ptr
1198  * @priv: file handle
1199  * @buftype: v4l2 buffer type
1200  */
1201 static int vpif_streamoff(struct file *file, void *priv,
1202                                 enum v4l2_buf_type buftype)
1203 {
1204
1205         struct vpif_fh *fh = priv;
1206         struct channel_obj *ch = fh->channel;
1207         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1208         int ret;
1209
1210         vpif_dbg(2, debug, "vpif_streamoff\n");
1211
1212         if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1213                 vpif_dbg(1, debug, "buffer type not supported\n");
1214                 return -EINVAL;
1215         }
1216
1217         /* If io is allowed for this file handle, return error */
1218         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1219                 vpif_dbg(1, debug, "io not allowed\n");
1220                 return -EACCES;
1221         }
1222
1223         /* If streaming is not started, return error */
1224         if (!common->started) {
1225                 vpif_dbg(1, debug, "channel->started\n");
1226                 return -EINVAL;
1227         }
1228
1229         /* disable channel */
1230         if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1231                 enable_channel0(0);
1232                 channel0_intr_enable(0);
1233         } else {
1234                 enable_channel1(0);
1235                 channel1_intr_enable(0);
1236         }
1237
1238         common->started = 0;
1239
1240         ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
1241
1242         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1243                 vpif_dbg(1, debug, "stream off failed in subdev\n");
1244
1245         return vb2_streamoff(&common->buffer_queue, buftype);
1246 }
1247
1248 /**
1249  * vpif_input_to_subdev() - Maps input to sub device
1250  * @vpif_cfg - global config ptr
1251  * @chan_cfg - channel config ptr
1252  * @input_index - Given input index from application
1253  *
1254  * lookup the sub device information for a given input index.
1255  * we report all the inputs to application. inputs table also
1256  * has sub device name for the each input
1257  */
1258 static int vpif_input_to_subdev(
1259                 struct vpif_capture_config *vpif_cfg,
1260                 struct vpif_capture_chan_config *chan_cfg,
1261                 int input_index)
1262 {
1263         struct vpif_subdev_info *subdev_info;
1264         const char *subdev_name;
1265         int i;
1266
1267         vpif_dbg(2, debug, "vpif_input_to_subdev\n");
1268
1269         subdev_name = chan_cfg->inputs[input_index].subdev_name;
1270         if (subdev_name == NULL)
1271                 return -1;
1272
1273         /* loop through the sub device list to get the sub device info */
1274         for (i = 0; i < vpif_cfg->subdev_count; i++) {
1275                 subdev_info = &vpif_cfg->subdev_info[i];
1276                 if (!strcmp(subdev_info->name, subdev_name))
1277                         return i;
1278         }
1279         return -1;
1280 }
1281
1282 /**
1283  * vpif_set_input() - Select an input
1284  * @vpif_cfg - global config ptr
1285  * @ch - channel
1286  * @_index - Given input index from application
1287  *
1288  * Select the given input.
1289  */
1290 static int vpif_set_input(
1291                 struct vpif_capture_config *vpif_cfg,
1292                 struct channel_obj *ch,
1293                 int index)
1294 {
1295         struct vpif_capture_chan_config *chan_cfg =
1296                         &vpif_cfg->chan_config[ch->channel_id];
1297         struct vpif_subdev_info *subdev_info = NULL;
1298         struct v4l2_subdev *sd = NULL;
1299         u32 input = 0, output = 0;
1300         int sd_index;
1301         int ret;
1302
1303         sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1304         if (sd_index >= 0) {
1305                 sd = vpif_obj.sd[sd_index];
1306                 subdev_info = &vpif_cfg->subdev_info[sd_index];
1307         }
1308
1309         /* first setup input path from sub device to vpif */
1310         if (sd && vpif_cfg->setup_input_path) {
1311                 ret = vpif_cfg->setup_input_path(ch->channel_id,
1312                                        subdev_info->name);
1313                 if (ret < 0) {
1314                         vpif_dbg(1, debug, "couldn't setup input path for the" \
1315                         " sub device %s, for input index %d\n",
1316                         subdev_info->name, index);
1317                         return ret;
1318                 }
1319         }
1320
1321         if (sd) {
1322                 input = chan_cfg->inputs[index].input_route;
1323                 output = chan_cfg->inputs[index].output_route;
1324                 ret = v4l2_subdev_call(sd, video, s_routing,
1325                                 input, output, 0);
1326                 if (ret < 0 && ret != -ENOIOCTLCMD) {
1327                         vpif_dbg(1, debug, "Failed to set input\n");
1328                         return ret;
1329                 }
1330         }
1331         ch->input_idx = index;
1332         ch->sd = sd;
1333         /* copy interface parameters to vpif */
1334         ch->vpifparams.iface = chan_cfg->vpif_if;
1335
1336         /* update tvnorms from the sub device input info */
1337         ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1338         return 0;
1339 }
1340
1341 /**
1342  * vpif_querystd() - querystd handler
1343  * @file: file ptr
1344  * @priv: file handle
1345  * @std_id: ptr to std id
1346  *
1347  * This function is called to detect standard at the selected input
1348  */
1349 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1350 {
1351         struct vpif_fh *fh = priv;
1352         struct channel_obj *ch = fh->channel;
1353         int ret = 0;
1354
1355         vpif_dbg(2, debug, "vpif_querystd\n");
1356
1357         /* Call querystd function of decoder device */
1358         ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
1359
1360         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1361                 return -ENODATA;
1362         if (ret) {
1363                 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1364                 return ret;
1365         }
1366
1367         return 0;
1368 }
1369
1370 /**
1371  * vpif_g_std() - get STD handler
1372  * @file: file ptr
1373  * @priv: file handle
1374  * @std_id: ptr to std id
1375  */
1376 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1377 {
1378         struct vpif_fh *fh = priv;
1379         struct channel_obj *ch = fh->channel;
1380
1381         vpif_dbg(2, debug, "vpif_g_std\n");
1382
1383         *std = ch->video.stdid;
1384         return 0;
1385 }
1386
1387 /**
1388  * vpif_s_std() - set STD handler
1389  * @file: file ptr
1390  * @priv: file handle
1391  * @std_id: ptr to std id
1392  */
1393 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1394 {
1395         struct vpif_fh *fh = priv;
1396         struct channel_obj *ch = fh->channel;
1397         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1398         int ret = 0;
1399
1400         vpif_dbg(2, debug, "vpif_s_std\n");
1401
1402         if (common->started) {
1403                 vpif_err("streaming in progress\n");
1404                 return -EBUSY;
1405         }
1406
1407         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1408             (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1409                 if (!fh->initialized) {
1410                         vpif_dbg(1, debug, "Channel Busy\n");
1411                         return -EBUSY;
1412                 }
1413         }
1414
1415         ret = v4l2_prio_check(&ch->prio, fh->prio);
1416         if (0 != ret)
1417                 return ret;
1418
1419         fh->initialized = 1;
1420
1421         /* Call encoder subdevice function to set the standard */
1422         ch->video.stdid = std_id;
1423         memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1424
1425         /* Get the information about the standard */
1426         if (vpif_update_std_info(ch)) {
1427                 vpif_err("Error getting the standard info\n");
1428                 return -EINVAL;
1429         }
1430
1431         /* Configure the default format information */
1432         vpif_config_format(ch);
1433
1434         /* set standard in the sub device */
1435         ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
1436         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1437                 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1438                 return ret;
1439         }
1440         return 0;
1441 }
1442
1443 /**
1444  * vpif_enum_input() - ENUMINPUT handler
1445  * @file: file ptr
1446  * @priv: file handle
1447  * @input: ptr to input structure
1448  */
1449 static int vpif_enum_input(struct file *file, void *priv,
1450                                 struct v4l2_input *input)
1451 {
1452
1453         struct vpif_capture_config *config = vpif_dev->platform_data;
1454         struct vpif_capture_chan_config *chan_cfg;
1455         struct vpif_fh *fh = priv;
1456         struct channel_obj *ch = fh->channel;
1457
1458         chan_cfg = &config->chan_config[ch->channel_id];
1459
1460         if (input->index >= chan_cfg->input_count) {
1461                 vpif_dbg(1, debug, "Invalid input index\n");
1462                 return -EINVAL;
1463         }
1464
1465         memcpy(input, &chan_cfg->inputs[input->index].input,
1466                 sizeof(*input));
1467         return 0;
1468 }
1469
1470 /**
1471  * vpif_g_input() - Get INPUT handler
1472  * @file: file ptr
1473  * @priv: file handle
1474  * @index: ptr to input index
1475  */
1476 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1477 {
1478         struct vpif_fh *fh = priv;
1479         struct channel_obj *ch = fh->channel;
1480
1481         *index = ch->input_idx;
1482         return 0;
1483 }
1484
1485 /**
1486  * vpif_s_input() - Set INPUT handler
1487  * @file: file ptr
1488  * @priv: file handle
1489  * @index: input index
1490  */
1491 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1492 {
1493         struct vpif_capture_config *config = vpif_dev->platform_data;
1494         struct vpif_capture_chan_config *chan_cfg;
1495         struct vpif_fh *fh = priv;
1496         struct channel_obj *ch = fh->channel;
1497         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1498         int ret;
1499
1500         chan_cfg = &config->chan_config[ch->channel_id];
1501
1502         if (index >= chan_cfg->input_count)
1503                 return -EINVAL;
1504
1505         if (common->started) {
1506                 vpif_err("Streaming in progress\n");
1507                 return -EBUSY;
1508         }
1509
1510         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1511             (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1512                 if (!fh->initialized) {
1513                         vpif_dbg(1, debug, "Channel Busy\n");
1514                         return -EBUSY;
1515                 }
1516         }
1517
1518         ret = v4l2_prio_check(&ch->prio, fh->prio);
1519         if (0 != ret)
1520                 return ret;
1521
1522         fh->initialized = 1;
1523         return vpif_set_input(config, ch, index);
1524 }
1525
1526 /**
1527  * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1528  * @file: file ptr
1529  * @priv: file handle
1530  * @index: input index
1531  */
1532 static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
1533                                         struct v4l2_fmtdesc *fmt)
1534 {
1535         struct vpif_fh *fh = priv;
1536         struct channel_obj *ch = fh->channel;
1537
1538         if (fmt->index != 0) {
1539                 vpif_dbg(1, debug, "Invalid format index\n");
1540                 return -EINVAL;
1541         }
1542
1543         /* Fill in the information about format */
1544         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1545                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1546                 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1547                 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1548         } else {
1549                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1550                 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1551                 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1552         }
1553         return 0;
1554 }
1555
1556 /**
1557  * vpif_try_fmt_vid_cap() - TRY_FMT handler
1558  * @file: file ptr
1559  * @priv: file handle
1560  * @fmt: ptr to v4l2 format structure
1561  */
1562 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1563                                 struct v4l2_format *fmt)
1564 {
1565         struct vpif_fh *fh = priv;
1566         struct channel_obj *ch = fh->channel;
1567         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1568
1569         return vpif_check_format(ch, pixfmt, 1);
1570 }
1571
1572
1573 /**
1574  * vpif_g_fmt_vid_cap() - Set INPUT handler
1575  * @file: file ptr
1576  * @priv: file handle
1577  * @fmt: ptr to v4l2 format structure
1578  */
1579 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1580                                 struct v4l2_format *fmt)
1581 {
1582         struct vpif_fh *fh = priv;
1583         struct channel_obj *ch = fh->channel;
1584         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1585
1586         /* Check the validity of the buffer type */
1587         if (common->fmt.type != fmt->type)
1588                 return -EINVAL;
1589
1590         /* Fill in the information about format */
1591         *fmt = common->fmt;
1592         return 0;
1593 }
1594
1595 /**
1596  * vpif_s_fmt_vid_cap() - Set FMT handler
1597  * @file: file ptr
1598  * @priv: file handle
1599  * @fmt: ptr to v4l2 format structure
1600  */
1601 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1602                                 struct v4l2_format *fmt)
1603 {
1604         struct vpif_fh *fh = priv;
1605         struct channel_obj *ch = fh->channel;
1606         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1607         struct v4l2_pix_format *pixfmt;
1608         int ret = 0;
1609
1610         vpif_dbg(2, debug, "%s\n", __func__);
1611
1612         /* If streaming is started, return error */
1613         if (common->started) {
1614                 vpif_dbg(1, debug, "Streaming is started\n");
1615                 return -EBUSY;
1616         }
1617
1618         if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1619             (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1620                 if (!fh->initialized) {
1621                         vpif_dbg(1, debug, "Channel Busy\n");
1622                         return -EBUSY;
1623                 }
1624         }
1625
1626         ret = v4l2_prio_check(&ch->prio, fh->prio);
1627         if (0 != ret)
1628                 return ret;
1629
1630         fh->initialized = 1;
1631
1632         pixfmt = &fmt->fmt.pix;
1633         /* Check for valid field format */
1634         ret = vpif_check_format(ch, pixfmt, 0);
1635
1636         if (ret)
1637                 return ret;
1638         /* store the format in the channel object */
1639         common->fmt = *fmt;
1640         return 0;
1641 }
1642
1643 /**
1644  * vpif_querycap() - QUERYCAP handler
1645  * @file: file ptr
1646  * @priv: file handle
1647  * @cap: ptr to v4l2_capability structure
1648  */
1649 static int vpif_querycap(struct file *file, void  *priv,
1650                                 struct v4l2_capability *cap)
1651 {
1652         struct vpif_capture_config *config = vpif_dev->platform_data;
1653
1654         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1655         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1656         snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1657         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1658                  dev_name(vpif_dev));
1659         strlcpy(cap->card, config->card_name, sizeof(cap->card));
1660
1661         return 0;
1662 }
1663
1664 /**
1665  * vpif_g_priority() - get priority handler
1666  * @file: file ptr
1667  * @priv: file handle
1668  * @prio: ptr to v4l2_priority structure
1669  */
1670 static int vpif_g_priority(struct file *file, void *priv,
1671                            enum v4l2_priority *prio)
1672 {
1673         struct vpif_fh *fh = priv;
1674         struct channel_obj *ch = fh->channel;
1675
1676         *prio = v4l2_prio_max(&ch->prio);
1677
1678         return 0;
1679 }
1680
1681 /**
1682  * vpif_s_priority() - set priority handler
1683  * @file: file ptr
1684  * @priv: file handle
1685  * @prio: ptr to v4l2_priority structure
1686  */
1687 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1688 {
1689         struct vpif_fh *fh = priv;
1690         struct channel_obj *ch = fh->channel;
1691
1692         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1693 }
1694
1695 /**
1696  * vpif_cropcap() - cropcap handler
1697  * @file: file ptr
1698  * @priv: file handle
1699  * @crop: ptr to v4l2_cropcap structure
1700  */
1701 static int vpif_cropcap(struct file *file, void *priv,
1702                         struct v4l2_cropcap *crop)
1703 {
1704         struct vpif_fh *fh = priv;
1705         struct channel_obj *ch = fh->channel;
1706         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1707
1708         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1709                 return -EINVAL;
1710
1711         crop->bounds.left = 0;
1712         crop->bounds.top = 0;
1713         crop->bounds.height = common->height;
1714         crop->bounds.width = common->width;
1715         crop->defrect = crop->bounds;
1716         return 0;
1717 }
1718
1719 /**
1720  * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1721  * @file: file ptr
1722  * @priv: file handle
1723  * @timings: input timings
1724  */
1725 static int
1726 vpif_enum_dv_timings(struct file *file, void *priv,
1727                      struct v4l2_enum_dv_timings *timings)
1728 {
1729         struct vpif_fh *fh = priv;
1730         struct channel_obj *ch = fh->channel;
1731         int ret;
1732
1733         ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1734         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1735                 return -EINVAL;
1736         return ret;
1737 }
1738
1739 /**
1740  * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1741  * @file: file ptr
1742  * @priv: file handle
1743  * @timings: input timings
1744  */
1745 static int
1746 vpif_query_dv_timings(struct file *file, void *priv,
1747                       struct v4l2_dv_timings *timings)
1748 {
1749         struct vpif_fh *fh = priv;
1750         struct channel_obj *ch = fh->channel;
1751         int ret;
1752
1753         ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1754         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1755                 return -ENODATA;
1756         return ret;
1757 }
1758
1759 /**
1760  * vpif_s_dv_timings() - S_DV_TIMINGS handler
1761  * @file: file ptr
1762  * @priv: file handle
1763  * @timings: digital video timings
1764  */
1765 static int vpif_s_dv_timings(struct file *file, void *priv,
1766                 struct v4l2_dv_timings *timings)
1767 {
1768         struct vpif_fh *fh = priv;
1769         struct channel_obj *ch = fh->channel;
1770         struct vpif_params *vpifparams = &ch->vpifparams;
1771         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1772         struct video_obj *vid_ch = &ch->video;
1773         struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1774         int ret;
1775
1776         if (timings->type != V4L2_DV_BT_656_1120) {
1777                 vpif_dbg(2, debug, "Timing type not defined\n");
1778                 return -EINVAL;
1779         }
1780
1781         /* Configure subdevice timings, if any */
1782         ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1783         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1784                 ret = 0;
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 = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1810         std_info->sav2eav = bt->width;
1811
1812         std_info->l1 = 1;
1813         std_info->l3 = bt->vsync + bt->vbackporch + 1;
1814
1815         std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1816         if (bt->interlaced) {
1817                 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1818                         std_info->l5 = std_info->vsize/2 -
1819                                 (bt->vfrontporch - 1);
1820                         std_info->l7 = std_info->vsize/2 + 1;
1821                         std_info->l9 = std_info->l7 + bt->il_vsync +
1822                                 bt->il_vbackporch + 1;
1823                         std_info->l11 = std_info->vsize -
1824                                 (bt->il_vfrontporch - 1);
1825                 } else {
1826                         vpif_dbg(2, debug, "Required timing values for "
1827                                         "interlaced BT format missing\n");
1828                         return -EINVAL;
1829                 }
1830         } else {
1831                 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1832         }
1833         strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1834         std_info->width = bt->width;
1835         std_info->height = bt->height;
1836         std_info->frm_fmt = bt->interlaced ? 0 : 1;
1837         std_info->ycmux_mode = 0;
1838         std_info->capture_format = 0;
1839         std_info->vbi_supported = 0;
1840         std_info->hd_sd = 1;
1841         std_info->stdid = 0;
1842
1843         vid_ch->stdid = 0;
1844         return 0;
1845 }
1846
1847 /**
1848  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1849  * @file: file ptr
1850  * @priv: file handle
1851  * @timings: digital video timings
1852  */
1853 static int vpif_g_dv_timings(struct file *file, void *priv,
1854                 struct v4l2_dv_timings *timings)
1855 {
1856         struct vpif_fh *fh = priv;
1857         struct channel_obj *ch = fh->channel;
1858         struct video_obj *vid_ch = &ch->video;
1859
1860         *timings = vid_ch->dv_timings;
1861
1862         return 0;
1863 }
1864
1865 /*
1866  * vpif_log_status() - Status information
1867  * @file: file ptr
1868  * @priv: file handle
1869  *
1870  * Returns zero.
1871  */
1872 static int vpif_log_status(struct file *filep, void *priv)
1873 {
1874         /* status for sub devices */
1875         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1876
1877         return 0;
1878 }
1879
1880 /* vpif capture ioctl operations */
1881 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1882         .vidioc_querycap                = vpif_querycap,
1883         .vidioc_g_priority              = vpif_g_priority,
1884         .vidioc_s_priority              = vpif_s_priority,
1885         .vidioc_enum_fmt_vid_cap        = vpif_enum_fmt_vid_cap,
1886         .vidioc_g_fmt_vid_cap           = vpif_g_fmt_vid_cap,
1887         .vidioc_s_fmt_vid_cap           = vpif_s_fmt_vid_cap,
1888         .vidioc_try_fmt_vid_cap         = vpif_try_fmt_vid_cap,
1889         .vidioc_enum_input              = vpif_enum_input,
1890         .vidioc_s_input                 = vpif_s_input,
1891         .vidioc_g_input                 = vpif_g_input,
1892         .vidioc_reqbufs                 = vpif_reqbufs,
1893         .vidioc_querybuf                = vpif_querybuf,
1894         .vidioc_querystd                = vpif_querystd,
1895         .vidioc_s_std                   = vpif_s_std,
1896         .vidioc_g_std                   = vpif_g_std,
1897         .vidioc_qbuf                    = vpif_qbuf,
1898         .vidioc_dqbuf                   = vpif_dqbuf,
1899         .vidioc_streamon                = vpif_streamon,
1900         .vidioc_streamoff               = vpif_streamoff,
1901         .vidioc_cropcap                 = vpif_cropcap,
1902         .vidioc_enum_dv_timings         = vpif_enum_dv_timings,
1903         .vidioc_query_dv_timings        = vpif_query_dv_timings,
1904         .vidioc_s_dv_timings            = vpif_s_dv_timings,
1905         .vidioc_g_dv_timings            = vpif_g_dv_timings,
1906         .vidioc_log_status              = vpif_log_status,
1907 };
1908
1909 /* vpif file operations */
1910 static struct v4l2_file_operations vpif_fops = {
1911         .owner = THIS_MODULE,
1912         .open = vpif_open,
1913         .release = vpif_release,
1914         .unlocked_ioctl = video_ioctl2,
1915         .mmap = vpif_mmap,
1916         .poll = vpif_poll
1917 };
1918
1919 /* vpif video template */
1920 static struct video_device vpif_video_template = {
1921         .name           = "vpif",
1922         .fops           = &vpif_fops,
1923         .minor          = -1,
1924         .ioctl_ops      = &vpif_ioctl_ops,
1925 };
1926
1927 /**
1928  * initialize_vpif() - Initialize vpif data structures
1929  *
1930  * Allocate memory for data structures and initialize them
1931  */
1932 static int initialize_vpif(void)
1933 {
1934         int err = 0, i, j;
1935         int free_channel_objects_index;
1936
1937         /* Default number of buffers should be 3 */
1938         if ((ch0_numbuffers > 0) &&
1939             (ch0_numbuffers < config_params.min_numbuffers))
1940                 ch0_numbuffers = config_params.min_numbuffers;
1941         if ((ch1_numbuffers > 0) &&
1942             (ch1_numbuffers < config_params.min_numbuffers))
1943                 ch1_numbuffers = config_params.min_numbuffers;
1944
1945         /* Set buffer size to min buffers size if it is invalid */
1946         if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1947                 ch0_bufsize =
1948                     config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1949         if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1950                 ch1_bufsize =
1951                     config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1952
1953         config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1954         config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1955         if (ch0_numbuffers) {
1956                 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1957                     = ch0_bufsize;
1958         }
1959         if (ch1_numbuffers) {
1960                 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1961                     = ch1_bufsize;
1962         }
1963
1964         /* Allocate memory for six channel objects */
1965         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1966                 vpif_obj.dev[i] =
1967                     kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1968                 /* If memory allocation fails, return error */
1969                 if (!vpif_obj.dev[i]) {
1970                         free_channel_objects_index = i;
1971                         err = -ENOMEM;
1972                         goto vpif_init_free_channel_objects;
1973                 }
1974         }
1975         return 0;
1976
1977 vpif_init_free_channel_objects:
1978         for (j = 0; j < free_channel_objects_index; j++)
1979                 kfree(vpif_obj.dev[j]);
1980         return err;
1981 }
1982
1983 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1984                             struct v4l2_subdev *subdev,
1985                             struct v4l2_async_subdev *asd)
1986 {
1987         int i;
1988
1989         for (i = 0; i < vpif_obj.config->subdev_count; i++)
1990                 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1991                             subdev->name)) {
1992                         vpif_obj.sd[i] = subdev;
1993                         return 0;
1994                 }
1995
1996         return -EINVAL;
1997 }
1998
1999 static int vpif_probe_complete(void)
2000 {
2001         struct common_obj *common;
2002         struct channel_obj *ch;
2003         int i, j, err, k;
2004
2005         for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2006                 ch = vpif_obj.dev[j];
2007                 ch->channel_id = j;
2008                 common = &(ch->common[VPIF_VIDEO_INDEX]);
2009                 spin_lock_init(&common->irqlock);
2010                 mutex_init(&common->lock);
2011                 ch->video_dev->lock = &common->lock;
2012                 /* Initialize prio member of channel object */
2013                 v4l2_prio_init(&ch->prio);
2014                 video_set_drvdata(ch->video_dev, ch);
2015
2016                 /* select input 0 */
2017                 err = vpif_set_input(vpif_obj.config, ch, 0);
2018                 if (err)
2019                         goto probe_out;
2020
2021                 err = video_register_device(ch->video_dev,
2022                                             VFL_TYPE_GRABBER, (j ? 1 : 0));
2023                 if (err)
2024                         goto probe_out;
2025         }
2026
2027         v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2028         return 0;
2029
2030 probe_out:
2031         for (k = 0; k < j; k++) {
2032                 /* Get the pointer to the channel object */
2033                 ch = vpif_obj.dev[k];
2034                 /* Unregister video device */
2035                 video_unregister_device(ch->video_dev);
2036         }
2037         kfree(vpif_obj.sd);
2038         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2039                 ch = vpif_obj.dev[i];
2040                 /* Note: does nothing if ch->video_dev == NULL */
2041                 video_device_release(ch->video_dev);
2042         }
2043         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2044
2045         return err;
2046 }
2047
2048 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2049 {
2050         return vpif_probe_complete();
2051 }
2052
2053 /**
2054  * vpif_probe : This function probes the vpif capture driver
2055  * @pdev: platform device pointer
2056  *
2057  * This creates device entries by register itself to the V4L2 driver and
2058  * initializes fields of each channel objects
2059  */
2060 static __init int vpif_probe(struct platform_device *pdev)
2061 {
2062         struct vpif_subdev_info *subdevdata;
2063         int i, j, err;
2064         int res_idx = 0;
2065         struct i2c_adapter *i2c_adap;
2066         struct channel_obj *ch;
2067         struct video_device *vfd;
2068         struct resource *res;
2069         int subdev_count;
2070         size_t size;
2071
2072         vpif_dev = &pdev->dev;
2073
2074         err = initialize_vpif();
2075         if (err) {
2076                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2077                 return err;
2078         }
2079
2080         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2081         if (err) {
2082                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2083                 return err;
2084         }
2085
2086         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
2087                 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2088                                         IRQF_SHARED, "VPIF_Capture",
2089                                         (void *)(&vpif_obj.dev[res_idx]->
2090                                         channel_id));
2091                 if (err) {
2092                         err = -EINVAL;
2093                         goto vpif_unregister;
2094                 }
2095                 res_idx++;
2096         }
2097
2098         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2099                 /* Get the pointer to the channel object */
2100                 ch = vpif_obj.dev[i];
2101                 /* Allocate memory for video device */
2102                 vfd = video_device_alloc();
2103                 if (NULL == vfd) {
2104                         for (j = 0; j < i; j++) {
2105                                 ch = vpif_obj.dev[j];
2106                                 video_device_release(ch->video_dev);
2107                         }
2108                         err = -ENOMEM;
2109                         goto vpif_unregister;
2110                 }
2111
2112                 /* Initialize field of video device */
2113                 *vfd = vpif_video_template;
2114                 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2115                 vfd->release = video_device_release;
2116                 snprintf(vfd->name, sizeof(vfd->name),
2117                          "VPIF_Capture_DRIVER_V%s",
2118                          VPIF_CAPTURE_VERSION);
2119                 /* Set video_dev to the video device */
2120                 ch->video_dev = vfd;
2121         }
2122
2123         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2124         if (res) {
2125                 size = resource_size(res);
2126                 /* The resources are divided into two equal memory and when we
2127                  * have HD output we can add them together
2128                  */
2129                 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2130                         ch = vpif_obj.dev[j];
2131                         ch->channel_id = j;
2132                         /* only enabled if second resource exists */
2133                         config_params.video_limit[ch->channel_id] = 0;
2134                         if (size)
2135                                 config_params.video_limit[ch->channel_id] =
2136                                                                         size/2;
2137                 }
2138         }
2139
2140         vpif_obj.config = pdev->dev.platform_data;
2141
2142         subdev_count = vpif_obj.config->subdev_count;
2143         vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2144                                 GFP_KERNEL);
2145         if (vpif_obj.sd == NULL) {
2146                 vpif_err("unable to allocate memory for subdevice pointers\n");
2147                 err = -ENOMEM;
2148                 goto vpif_sd_error;
2149         }
2150
2151         if (!vpif_obj.config->asd_sizes) {
2152                 i2c_adap = i2c_get_adapter(1);
2153                 for (i = 0; i < subdev_count; i++) {
2154                         subdevdata = &vpif_obj.config->subdev_info[i];
2155                         vpif_obj.sd[i] =
2156                                 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2157                                                           i2c_adap,
2158                                                           &subdevdata->
2159                                                           board_info,
2160                                                           NULL);
2161
2162                         if (!vpif_obj.sd[i]) {
2163                                 vpif_err("Error registering v4l2 subdevice\n");
2164                                 err = -ENODEV;
2165                                 goto probe_subdev_out;
2166                         }
2167                         v4l2_info(&vpif_obj.v4l2_dev,
2168                                   "registered sub device %s\n",
2169                                    subdevdata->name);
2170                 }
2171                 vpif_probe_complete();
2172         } else {
2173                 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
2174                 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2175                 vpif_obj.notifier.bound = vpif_async_bound;
2176                 vpif_obj.notifier.complete = vpif_async_complete;
2177                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2178                                                    &vpif_obj.notifier);
2179                 if (err) {
2180                         vpif_err("Error registering async notifier\n");
2181                         err = -EINVAL;
2182                         goto probe_subdev_out;
2183                 }
2184         }
2185
2186         return 0;
2187
2188 probe_subdev_out:
2189         /* free sub devices memory */
2190         kfree(vpif_obj.sd);
2191
2192 vpif_sd_error:
2193         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2194                 ch = vpif_obj.dev[i];
2195                 /* Note: does nothing if ch->video_dev == NULL */
2196                 video_device_release(ch->video_dev);
2197         }
2198 vpif_unregister:
2199         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2200
2201         return err;
2202 }
2203
2204 /**
2205  * vpif_remove() - driver remove handler
2206  * @device: ptr to platform device structure
2207  *
2208  * The vidoe device is unregistered
2209  */
2210 static int vpif_remove(struct platform_device *device)
2211 {
2212         struct channel_obj *ch;
2213         int i;
2214
2215         v4l2_device_unregister(&vpif_obj.v4l2_dev);
2216
2217         kfree(vpif_obj.sd);
2218         /* un-register device */
2219         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2220                 /* Get the pointer to the channel object */
2221                 ch = vpif_obj.dev[i];
2222                 /* Unregister video device */
2223                 video_unregister_device(ch->video_dev);
2224                 kfree(vpif_obj.dev[i]);
2225         }
2226         return 0;
2227 }
2228
2229 #ifdef CONFIG_PM
2230 /**
2231  * vpif_suspend: vpif device suspend
2232  */
2233 static int vpif_suspend(struct device *dev)
2234 {
2235
2236         struct common_obj *common;
2237         struct channel_obj *ch;
2238         int i;
2239
2240         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2241                 /* Get the pointer to the channel object */
2242                 ch = vpif_obj.dev[i];
2243                 common = &ch->common[VPIF_VIDEO_INDEX];
2244                 mutex_lock(&common->lock);
2245                 if (ch->usrs && common->io_usrs) {
2246                         /* Disable channel */
2247                         if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2248                                 enable_channel0(0);
2249                                 channel0_intr_enable(0);
2250                         }
2251                         if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2252                             common->started == 2) {
2253                                 enable_channel1(0);
2254                                 channel1_intr_enable(0);
2255                         }
2256                 }
2257                 mutex_unlock(&common->lock);
2258         }
2259
2260         return 0;
2261 }
2262
2263 /*
2264  * vpif_resume: vpif device suspend
2265  */
2266 static int vpif_resume(struct device *dev)
2267 {
2268         struct common_obj *common;
2269         struct channel_obj *ch;
2270         int i;
2271
2272         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2273                 /* Get the pointer to the channel object */
2274                 ch = vpif_obj.dev[i];
2275                 common = &ch->common[VPIF_VIDEO_INDEX];
2276                 mutex_lock(&common->lock);
2277                 if (ch->usrs && common->io_usrs) {
2278                         /* Disable channel */
2279                         if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2280                                 enable_channel0(1);
2281                                 channel0_intr_enable(1);
2282                         }
2283                         if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2284                             common->started == 2) {
2285                                 enable_channel1(1);
2286                                 channel1_intr_enable(1);
2287                         }
2288                 }
2289                 mutex_unlock(&common->lock);
2290         }
2291
2292         return 0;
2293 }
2294
2295 static const struct dev_pm_ops vpif_dev_pm_ops = {
2296         .suspend = vpif_suspend,
2297         .resume = vpif_resume,
2298 };
2299
2300 #define vpif_pm_ops (&vpif_dev_pm_ops)
2301 #else
2302 #define vpif_pm_ops NULL
2303 #endif
2304
2305 static __refdata struct platform_driver vpif_driver = {
2306         .driver = {
2307                 .name   = "vpif_capture",
2308                 .owner  = THIS_MODULE,
2309                 .pm     = vpif_pm_ops,
2310         },
2311         .probe = vpif_probe,
2312         .remove = vpif_remove,
2313 };
2314
2315 module_platform_driver(vpif_driver);