Linux-libre 4.10.7-gnu
[librecmc/linux-libre.git] / drivers / media / platform / davinci / vpif_capture.c
1 /*
2  * Copyright (C) 2009 Texas Instruments Inc
3  * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  * TODO : add support for VBI & HBI data service
20  *        add static buffer allocation
21  */
22
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27
28 #include <media/v4l2-ioctl.h>
29
30 #include "vpif.h"
31 #include "vpif_capture.h"
32
33 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(VPIF_CAPTURE_VERSION);
36
37 #define vpif_err(fmt, arg...)   v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38 #define vpif_dbg(level, debug, fmt, arg...)     \
39                 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41 static int debug = 1;
42
43 module_param(debug, int, 0644);
44
45 MODULE_PARM_DESC(debug, "Debug level 0-1");
46
47 #define VPIF_DRIVER_NAME        "vpif_capture"
48
49 /* global variables */
50 static struct vpif_device vpif_obj = { {NULL} };
51 static struct device *vpif_dev;
52 static void vpif_calculate_offsets(struct channel_obj *ch);
53 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
54
55 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
56
57 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
58 static int ycmux_mode;
59
60 static inline
61 struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
62 {
63         return container_of(vb, struct vpif_cap_buffer, vb);
64 }
65
66 /**
67  * vpif_buffer_prepare :  callback function for buffer prepare
68  * @vb: ptr to vb2_buffer
69  *
70  * This is the callback function for buffer prepare when vb2_qbuf()
71  * function is called. The buffer is prepared and user space virtual address
72  * or user address is converted into  physical address
73  */
74 static int vpif_buffer_prepare(struct vb2_buffer *vb)
75 {
76         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
77         struct vb2_queue *q = vb->vb2_queue;
78         struct channel_obj *ch = vb2_get_drv_priv(q);
79         struct common_obj *common;
80         unsigned long addr;
81
82         vpif_dbg(2, debug, "vpif_buffer_prepare\n");
83
84         common = &ch->common[VPIF_VIDEO_INDEX];
85
86         vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
87         if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
88                 return -EINVAL;
89
90         vbuf->field = common->fmt.fmt.pix.field;
91
92         addr = vb2_dma_contig_plane_dma_addr(vb, 0);
93         if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
94                 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
95                 !IS_ALIGNED((addr + common->ctop_off), 8) ||
96                 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
97                 vpif_dbg(1, debug, "offset is not aligned\n");
98                 return -EINVAL;
99         }
100
101         return 0;
102 }
103
104 /**
105  * vpif_buffer_queue_setup : Callback function for buffer setup.
106  * @vq: vb2_queue ptr
107  * @nbuffers: ptr to number of buffers requested by application
108  * @nplanes:: contains number of distinct video planes needed to hold a frame
109  * @sizes[]: contains the size (in bytes) of each plane.
110  * @alloc_devs: ptr to allocation context
111  *
112  * This callback function is called when reqbuf() is called to adjust
113  * the buffer count and buffer size
114  */
115 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
116                                 unsigned int *nbuffers, unsigned int *nplanes,
117                                 unsigned int sizes[], struct device *alloc_devs[])
118 {
119         struct channel_obj *ch = vb2_get_drv_priv(vq);
120         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
121         unsigned size = common->fmt.fmt.pix.sizeimage;
122
123         vpif_dbg(2, debug, "vpif_buffer_setup\n");
124
125         if (*nplanes) {
126                 if (sizes[0] < size)
127                         return -EINVAL;
128                 size = sizes[0];
129         }
130
131         if (vq->num_buffers + *nbuffers < 3)
132                 *nbuffers = 3 - vq->num_buffers;
133
134         *nplanes = 1;
135         sizes[0] = size;
136
137         /* Calculate the offset for Y and C data in the buffer */
138         vpif_calculate_offsets(ch);
139
140         return 0;
141 }
142
143 /**
144  * vpif_buffer_queue : Callback function to add buffer to DMA queue
145  * @vb: ptr to vb2_buffer
146  */
147 static void vpif_buffer_queue(struct vb2_buffer *vb)
148 {
149         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
150         struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
151         struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
152         struct common_obj *common;
153         unsigned long flags;
154
155         common = &ch->common[VPIF_VIDEO_INDEX];
156
157         vpif_dbg(2, debug, "vpif_buffer_queue\n");
158
159         spin_lock_irqsave(&common->irqlock, flags);
160         /* add the buffer to the DMA queue */
161         list_add_tail(&buf->list, &common->dma_queue);
162         spin_unlock_irqrestore(&common->irqlock, flags);
163 }
164
165 /**
166  * vpif_start_streaming : Starts the DMA engine for streaming
167  * @vb: ptr to vb2_buffer
168  * @count: number of buffers
169  */
170 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
171 {
172         struct vpif_capture_config *vpif_config_data =
173                                         vpif_dev->platform_data;
174         struct channel_obj *ch = vb2_get_drv_priv(vq);
175         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
176         struct vpif_params *vpif = &ch->vpifparams;
177         struct vpif_cap_buffer *buf, *tmp;
178         unsigned long addr, flags;
179         int ret;
180
181         spin_lock_irqsave(&common->irqlock, flags);
182
183         /* Initialize field_id */
184         ch->field_id = 0;
185
186         /* configure 1 or 2 channel mode */
187         if (vpif_config_data->setup_input_channel_mode) {
188                 ret = vpif_config_data->
189                         setup_input_channel_mode(vpif->std_info.ycmux_mode);
190                 if (ret < 0) {
191                         vpif_dbg(1, debug, "can't set vpif channel mode\n");
192                         goto err;
193                 }
194         }
195
196         ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
197         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
198                 vpif_dbg(1, debug, "stream on failed in subdev\n");
199                 goto err;
200         }
201
202         /* Call vpif_set_params function to set the parameters and addresses */
203         ret = vpif_set_video_params(vpif, ch->channel_id);
204         if (ret < 0) {
205                 vpif_dbg(1, debug, "can't set video params\n");
206                 goto err;
207         }
208
209         ycmux_mode = ret;
210         vpif_config_addr(ch, ret);
211
212         /* Get the next frame from the buffer queue */
213         common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
214                                     struct vpif_cap_buffer, list);
215         /* Remove buffer from the buffer queue */
216         list_del(&common->cur_frm->list);
217         spin_unlock_irqrestore(&common->irqlock, flags);
218
219         addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
220
221         common->set_addr(addr + common->ytop_off,
222                          addr + common->ybtm_off,
223                          addr + common->ctop_off,
224                          addr + common->cbtm_off);
225
226         /**
227          * Set interrupt for both the fields in VPIF Register enable channel in
228          * VPIF register
229          */
230         channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
231         if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
232                 channel0_intr_assert();
233                 channel0_intr_enable(1);
234                 enable_channel0(1);
235         }
236         if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
237                 ycmux_mode == 2) {
238                 channel1_intr_assert();
239                 channel1_intr_enable(1);
240                 enable_channel1(1);
241         }
242
243         return 0;
244
245 err:
246         list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
247                 list_del(&buf->list);
248                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
249         }
250         spin_unlock_irqrestore(&common->irqlock, flags);
251
252         return ret;
253 }
254
255 /**
256  * vpif_stop_streaming : Stop the DMA engine
257  * @vq: ptr to vb2_queue
258  *
259  * This callback stops the DMA engine and any remaining buffers
260  * in the DMA queue are released.
261  */
262 static void vpif_stop_streaming(struct vb2_queue *vq)
263 {
264         struct channel_obj *ch = vb2_get_drv_priv(vq);
265         struct common_obj *common;
266         unsigned long flags;
267         int ret;
268
269         common = &ch->common[VPIF_VIDEO_INDEX];
270
271         /* Disable channel as per its device type and channel id */
272         if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
273                 enable_channel0(0);
274                 channel0_intr_enable(0);
275         }
276         if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
277                 ycmux_mode == 2) {
278                 enable_channel1(0);
279                 channel1_intr_enable(0);
280         }
281
282         ycmux_mode = 0;
283
284         ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
285         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
286                 vpif_dbg(1, debug, "stream off failed in subdev\n");
287
288         /* release all active buffers */
289         spin_lock_irqsave(&common->irqlock, flags);
290         if (common->cur_frm == common->next_frm) {
291                 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
292                                 VB2_BUF_STATE_ERROR);
293         } else {
294                 if (common->cur_frm)
295                         vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
296                                         VB2_BUF_STATE_ERROR);
297                 if (common->next_frm)
298                         vb2_buffer_done(&common->next_frm->vb.vb2_buf,
299                                         VB2_BUF_STATE_ERROR);
300         }
301
302         while (!list_empty(&common->dma_queue)) {
303                 common->next_frm = list_entry(common->dma_queue.next,
304                                                 struct vpif_cap_buffer, list);
305                 list_del(&common->next_frm->list);
306                 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
307                                 VB2_BUF_STATE_ERROR);
308         }
309         spin_unlock_irqrestore(&common->irqlock, flags);
310 }
311
312 static struct vb2_ops video_qops = {
313         .queue_setup            = vpif_buffer_queue_setup,
314         .buf_prepare            = vpif_buffer_prepare,
315         .start_streaming        = vpif_start_streaming,
316         .stop_streaming         = vpif_stop_streaming,
317         .buf_queue              = vpif_buffer_queue,
318         .wait_prepare           = vb2_ops_wait_prepare,
319         .wait_finish            = vb2_ops_wait_finish,
320 };
321
322 /**
323  * vpif_process_buffer_complete: process a completed buffer
324  * @common: ptr to common channel object
325  *
326  * This function time stamp the buffer and mark it as DONE. It also
327  * wake up any process waiting on the QUEUE and set the next buffer
328  * as current
329  */
330 static void vpif_process_buffer_complete(struct common_obj *common)
331 {
332         common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
333         vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
334         /* Make curFrm pointing to nextFrm */
335         common->cur_frm = common->next_frm;
336 }
337
338 /**
339  * vpif_schedule_next_buffer: set next buffer address for capture
340  * @common : ptr to common channel object
341  *
342  * This function will get next buffer from the dma queue and
343  * set the buffer address in the vpif register for capture.
344  * the buffer is marked active
345  */
346 static void vpif_schedule_next_buffer(struct common_obj *common)
347 {
348         unsigned long addr = 0;
349
350         spin_lock(&common->irqlock);
351         common->next_frm = list_entry(common->dma_queue.next,
352                                      struct vpif_cap_buffer, list);
353         /* Remove that buffer from the buffer queue */
354         list_del(&common->next_frm->list);
355         spin_unlock(&common->irqlock);
356         addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
357
358         /* Set top and bottom field addresses in VPIF registers */
359         common->set_addr(addr + common->ytop_off,
360                          addr + common->ybtm_off,
361                          addr + common->ctop_off,
362                          addr + common->cbtm_off);
363 }
364
365 /**
366  * vpif_channel_isr : ISR handler for vpif capture
367  * @irq: irq number
368  * @dev_id: dev_id ptr
369  *
370  * It changes status of the captured buffer, takes next buffer from the queue
371  * and sets its address in VPIF registers
372  */
373 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
374 {
375         struct vpif_device *dev = &vpif_obj;
376         struct common_obj *common;
377         struct channel_obj *ch;
378         int channel_id;
379         int fid = -1, i;
380
381         channel_id = *(int *)(dev_id);
382         if (!vpif_intr_status(channel_id))
383                 return IRQ_NONE;
384
385         ch = dev->dev[channel_id];
386
387         for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
388                 common = &ch->common[i];
389                 /* skip If streaming is not started in this channel */
390                 /* Check the field format */
391                 if (1 == ch->vpifparams.std_info.frm_fmt) {
392                         /* Progressive mode */
393                         spin_lock(&common->irqlock);
394                         if (list_empty(&common->dma_queue)) {
395                                 spin_unlock(&common->irqlock);
396                                 continue;
397                         }
398                         spin_unlock(&common->irqlock);
399
400                         if (!channel_first_int[i][channel_id])
401                                 vpif_process_buffer_complete(common);
402
403                         channel_first_int[i][channel_id] = 0;
404
405                         vpif_schedule_next_buffer(common);
406
407
408                         channel_first_int[i][channel_id] = 0;
409                 } else {
410                         /**
411                          * Interlaced mode. If it is first interrupt, ignore
412                          * it
413                          */
414                         if (channel_first_int[i][channel_id]) {
415                                 channel_first_int[i][channel_id] = 0;
416                                 continue;
417                         }
418                         if (0 == i) {
419                                 ch->field_id ^= 1;
420                                 /* Get field id from VPIF registers */
421                                 fid = vpif_channel_getfid(ch->channel_id);
422                                 if (fid != ch->field_id) {
423                                         /**
424                                          * If field id does not match stored
425                                          * field id, make them in sync
426                                          */
427                                         if (0 == fid)
428                                                 ch->field_id = fid;
429                                         return IRQ_HANDLED;
430                                 }
431                         }
432                         /* device field id and local field id are in sync */
433                         if (0 == fid) {
434                                 /* this is even field */
435                                 if (common->cur_frm == common->next_frm)
436                                         continue;
437
438                                 /* mark the current buffer as done */
439                                 vpif_process_buffer_complete(common);
440                         } else if (1 == fid) {
441                                 /* odd field */
442                                 spin_lock(&common->irqlock);
443                                 if (list_empty(&common->dma_queue) ||
444                                     (common->cur_frm != common->next_frm)) {
445                                         spin_unlock(&common->irqlock);
446                                         continue;
447                                 }
448                                 spin_unlock(&common->irqlock);
449
450                                 vpif_schedule_next_buffer(common);
451                         }
452                 }
453         }
454         return IRQ_HANDLED;
455 }
456
457 /**
458  * vpif_update_std_info() - update standard related info
459  * @ch: ptr to channel object
460  *
461  * For a given standard selected by application, update values
462  * in the device data structures
463  */
464 static int vpif_update_std_info(struct channel_obj *ch)
465 {
466         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
467         struct vpif_params *vpifparams = &ch->vpifparams;
468         const struct vpif_channel_config_params *config;
469         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
470         struct video_obj *vid_ch = &ch->video;
471         int index;
472
473         vpif_dbg(2, debug, "vpif_update_std_info\n");
474
475         for (index = 0; index < vpif_ch_params_count; index++) {
476                 config = &vpif_ch_params[index];
477                 if (config->hd_sd == 0) {
478                         vpif_dbg(2, debug, "SD format\n");
479                         if (config->stdid & vid_ch->stdid) {
480                                 memcpy(std_info, config, sizeof(*config));
481                                 break;
482                         }
483                 } else {
484                         vpif_dbg(2, debug, "HD format\n");
485                         if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
486                                 sizeof(vid_ch->dv_timings))) {
487                                 memcpy(std_info, config, sizeof(*config));
488                                 break;
489                         }
490                 }
491         }
492
493         /* standard not found */
494         if (index == vpif_ch_params_count)
495                 return -EINVAL;
496
497         common->fmt.fmt.pix.width = std_info->width;
498         common->width = std_info->width;
499         common->fmt.fmt.pix.height = std_info->height;
500         common->height = std_info->height;
501         common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
502         common->fmt.fmt.pix.bytesperline = std_info->width;
503         vpifparams->video_params.hpitch = std_info->width;
504         vpifparams->video_params.storage_mode = std_info->frm_fmt;
505
506         if (vid_ch->stdid)
507                 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
508         else
509                 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
510
511         if (ch->vpifparams.std_info.frm_fmt)
512                 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
513         else
514                 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
515
516         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
517                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
518         else
519                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
520
521         common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
522
523         return 0;
524 }
525
526 /**
527  * vpif_calculate_offsets : This function calculates buffers offsets
528  * @ch : ptr to channel object
529  *
530  * This function calculates buffer offsets for Y and C in the top and
531  * bottom field
532  */
533 static void vpif_calculate_offsets(struct channel_obj *ch)
534 {
535         unsigned int hpitch, sizeimage;
536         struct video_obj *vid_ch = &(ch->video);
537         struct vpif_params *vpifparams = &ch->vpifparams;
538         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
539         enum v4l2_field field = common->fmt.fmt.pix.field;
540
541         vpif_dbg(2, debug, "vpif_calculate_offsets\n");
542
543         if (V4L2_FIELD_ANY == field) {
544                 if (vpifparams->std_info.frm_fmt)
545                         vid_ch->buf_field = V4L2_FIELD_NONE;
546                 else
547                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
548         } else
549                 vid_ch->buf_field = common->fmt.fmt.pix.field;
550
551         sizeimage = common->fmt.fmt.pix.sizeimage;
552
553         hpitch = common->fmt.fmt.pix.bytesperline;
554
555         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
556             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
557                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
558                 common->ytop_off = 0;
559                 common->ybtm_off = hpitch;
560                 common->ctop_off = sizeimage / 2;
561                 common->cbtm_off = sizeimage / 2 + hpitch;
562         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
563                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
564                 common->ytop_off = 0;
565                 common->ybtm_off = sizeimage / 4;
566                 common->ctop_off = sizeimage / 2;
567                 common->cbtm_off = common->ctop_off + sizeimage / 4;
568         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
569                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
570                 common->ybtm_off = 0;
571                 common->ytop_off = sizeimage / 4;
572                 common->cbtm_off = sizeimage / 2;
573                 common->ctop_off = common->cbtm_off + sizeimage / 4;
574         }
575         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
576             (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
577                 vpifparams->video_params.storage_mode = 1;
578         else
579                 vpifparams->video_params.storage_mode = 0;
580
581         if (1 == vpifparams->std_info.frm_fmt)
582                 vpifparams->video_params.hpitch =
583                     common->fmt.fmt.pix.bytesperline;
584         else {
585                 if ((field == V4L2_FIELD_ANY)
586                     || (field == V4L2_FIELD_INTERLACED))
587                         vpifparams->video_params.hpitch =
588                             common->fmt.fmt.pix.bytesperline * 2;
589                 else
590                         vpifparams->video_params.hpitch =
591                             common->fmt.fmt.pix.bytesperline;
592         }
593
594         ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
595 }
596
597 /**
598  * vpif_get_default_field() - Get default field type based on interface
599  * @vpif_params - ptr to vpif params
600  */
601 static inline enum v4l2_field vpif_get_default_field(
602                                 struct vpif_interface *iface)
603 {
604         return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
605                                                 V4L2_FIELD_INTERLACED;
606 }
607
608 /**
609  * vpif_config_addr() - function to configure buffer address in vpif
610  * @ch - channel ptr
611  * @muxmode - channel mux mode
612  */
613 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
614 {
615         struct common_obj *common;
616
617         vpif_dbg(2, debug, "vpif_config_addr\n");
618
619         common = &(ch->common[VPIF_VIDEO_INDEX]);
620
621         if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
622                 common->set_addr = ch1_set_videobuf_addr;
623         else if (2 == muxmode)
624                 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
625         else
626                 common->set_addr = ch0_set_videobuf_addr;
627 }
628
629 /**
630  * vpif_input_to_subdev() - Maps input to sub device
631  * @vpif_cfg - global config ptr
632  * @chan_cfg - channel config ptr
633  * @input_index - Given input index from application
634  *
635  * lookup the sub device information for a given input index.
636  * we report all the inputs to application. inputs table also
637  * has sub device name for the each input
638  */
639 static int vpif_input_to_subdev(
640                 struct vpif_capture_config *vpif_cfg,
641                 struct vpif_capture_chan_config *chan_cfg,
642                 int input_index)
643 {
644         struct vpif_subdev_info *subdev_info;
645         const char *subdev_name;
646         int i;
647
648         vpif_dbg(2, debug, "vpif_input_to_subdev\n");
649
650         subdev_name = chan_cfg->inputs[input_index].subdev_name;
651         if (!subdev_name)
652                 return -1;
653
654         /* loop through the sub device list to get the sub device info */
655         for (i = 0; i < vpif_cfg->subdev_count; i++) {
656                 subdev_info = &vpif_cfg->subdev_info[i];
657                 if (!strcmp(subdev_info->name, subdev_name))
658                         return i;
659         }
660         return -1;
661 }
662
663 /**
664  * vpif_set_input() - Select an input
665  * @vpif_cfg - global config ptr
666  * @ch - channel
667  * @_index - Given input index from application
668  *
669  * Select the given input.
670  */
671 static int vpif_set_input(
672                 struct vpif_capture_config *vpif_cfg,
673                 struct channel_obj *ch,
674                 int index)
675 {
676         struct vpif_capture_chan_config *chan_cfg =
677                         &vpif_cfg->chan_config[ch->channel_id];
678         struct vpif_subdev_info *subdev_info = NULL;
679         struct v4l2_subdev *sd = NULL;
680         u32 input = 0, output = 0;
681         int sd_index;
682         int ret;
683
684         sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
685         if (sd_index >= 0) {
686                 sd = vpif_obj.sd[sd_index];
687                 subdev_info = &vpif_cfg->subdev_info[sd_index];
688         }
689
690         /* first setup input path from sub device to vpif */
691         if (sd && vpif_cfg->setup_input_path) {
692                 ret = vpif_cfg->setup_input_path(ch->channel_id,
693                                        subdev_info->name);
694                 if (ret < 0) {
695                         vpif_dbg(1, debug, "couldn't setup input path for the" \
696                         " sub device %s, for input index %d\n",
697                         subdev_info->name, index);
698                         return ret;
699                 }
700         }
701
702         if (sd) {
703                 input = chan_cfg->inputs[index].input_route;
704                 output = chan_cfg->inputs[index].output_route;
705                 ret = v4l2_subdev_call(sd, video, s_routing,
706                                 input, output, 0);
707                 if (ret < 0 && ret != -ENOIOCTLCMD) {
708                         vpif_dbg(1, debug, "Failed to set input\n");
709                         return ret;
710                 }
711         }
712         ch->input_idx = index;
713         ch->sd = sd;
714         /* copy interface parameters to vpif */
715         ch->vpifparams.iface = chan_cfg->vpif_if;
716
717         /* update tvnorms from the sub device input info */
718         ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
719         return 0;
720 }
721
722 /**
723  * vpif_querystd() - querystd handler
724  * @file: file ptr
725  * @priv: file handle
726  * @std_id: ptr to std id
727  *
728  * This function is called to detect standard at the selected input
729  */
730 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
731 {
732         struct video_device *vdev = video_devdata(file);
733         struct channel_obj *ch = video_get_drvdata(vdev);
734         int ret;
735
736         vpif_dbg(2, debug, "vpif_querystd\n");
737
738         /* Call querystd function of decoder device */
739         ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
740
741         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
742                 return -ENODATA;
743         if (ret) {
744                 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
745                 return ret;
746         }
747
748         return 0;
749 }
750
751 /**
752  * vpif_g_std() - get STD handler
753  * @file: file ptr
754  * @priv: file handle
755  * @std_id: ptr to std id
756  */
757 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
758 {
759         struct vpif_capture_config *config = vpif_dev->platform_data;
760         struct video_device *vdev = video_devdata(file);
761         struct channel_obj *ch = video_get_drvdata(vdev);
762         struct vpif_capture_chan_config *chan_cfg;
763         struct v4l2_input input;
764
765         vpif_dbg(2, debug, "vpif_g_std\n");
766
767         if (!config->chan_config[ch->channel_id].inputs)
768                 return -ENODATA;
769
770         chan_cfg = &config->chan_config[ch->channel_id];
771         input = chan_cfg->inputs[ch->input_idx].input;
772         if (input.capabilities != V4L2_IN_CAP_STD)
773                 return -ENODATA;
774
775         *std = ch->video.stdid;
776         return 0;
777 }
778
779 /**
780  * vpif_s_std() - set STD handler
781  * @file: file ptr
782  * @priv: file handle
783  * @std_id: ptr to std id
784  */
785 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
786 {
787         struct vpif_capture_config *config = vpif_dev->platform_data;
788         struct video_device *vdev = video_devdata(file);
789         struct channel_obj *ch = video_get_drvdata(vdev);
790         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
791         struct vpif_capture_chan_config *chan_cfg;
792         struct v4l2_input input;
793         int ret;
794
795         vpif_dbg(2, debug, "vpif_s_std\n");
796
797         if (!config->chan_config[ch->channel_id].inputs)
798                 return -ENODATA;
799
800         chan_cfg = &config->chan_config[ch->channel_id];
801         input = chan_cfg->inputs[ch->input_idx].input;
802         if (input.capabilities != V4L2_IN_CAP_STD)
803                 return -ENODATA;
804
805         if (vb2_is_busy(&common->buffer_queue))
806                 return -EBUSY;
807
808         /* Call encoder subdevice function to set the standard */
809         ch->video.stdid = std_id;
810         memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
811
812         /* Get the information about the standard */
813         if (vpif_update_std_info(ch)) {
814                 vpif_err("Error getting the standard info\n");
815                 return -EINVAL;
816         }
817
818         /* set standard in the sub device */
819         ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
820         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
821                 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
822                 return ret;
823         }
824         return 0;
825 }
826
827 /**
828  * vpif_enum_input() - ENUMINPUT handler
829  * @file: file ptr
830  * @priv: file handle
831  * @input: ptr to input structure
832  */
833 static int vpif_enum_input(struct file *file, void *priv,
834                                 struct v4l2_input *input)
835 {
836
837         struct vpif_capture_config *config = vpif_dev->platform_data;
838         struct video_device *vdev = video_devdata(file);
839         struct channel_obj *ch = video_get_drvdata(vdev);
840         struct vpif_capture_chan_config *chan_cfg;
841
842         chan_cfg = &config->chan_config[ch->channel_id];
843
844         if (input->index >= chan_cfg->input_count)
845                 return -EINVAL;
846
847         memcpy(input, &chan_cfg->inputs[input->index].input,
848                 sizeof(*input));
849         return 0;
850 }
851
852 /**
853  * vpif_g_input() - Get INPUT handler
854  * @file: file ptr
855  * @priv: file handle
856  * @index: ptr to input index
857  */
858 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
859 {
860         struct video_device *vdev = video_devdata(file);
861         struct channel_obj *ch = video_get_drvdata(vdev);
862
863         *index = ch->input_idx;
864         return 0;
865 }
866
867 /**
868  * vpif_s_input() - Set INPUT handler
869  * @file: file ptr
870  * @priv: file handle
871  * @index: input index
872  */
873 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
874 {
875         struct vpif_capture_config *config = vpif_dev->platform_data;
876         struct video_device *vdev = video_devdata(file);
877         struct channel_obj *ch = video_get_drvdata(vdev);
878         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
879         struct vpif_capture_chan_config *chan_cfg;
880
881         chan_cfg = &config->chan_config[ch->channel_id];
882
883         if (index >= chan_cfg->input_count)
884                 return -EINVAL;
885
886         if (vb2_is_busy(&common->buffer_queue))
887                 return -EBUSY;
888
889         return vpif_set_input(config, ch, index);
890 }
891
892 /**
893  * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
894  * @file: file ptr
895  * @priv: file handle
896  * @index: input index
897  */
898 static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
899                                         struct v4l2_fmtdesc *fmt)
900 {
901         struct video_device *vdev = video_devdata(file);
902         struct channel_obj *ch = video_get_drvdata(vdev);
903
904         if (fmt->index != 0) {
905                 vpif_dbg(1, debug, "Invalid format index\n");
906                 return -EINVAL;
907         }
908
909         /* Fill in the information about format */
910         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
911                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
912                 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
913                 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
914         } else {
915                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
916                 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
917                 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
918         }
919         return 0;
920 }
921
922 /**
923  * vpif_try_fmt_vid_cap() - TRY_FMT handler
924  * @file: file ptr
925  * @priv: file handle
926  * @fmt: ptr to v4l2 format structure
927  */
928 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
929                                 struct v4l2_format *fmt)
930 {
931         struct video_device *vdev = video_devdata(file);
932         struct channel_obj *ch = video_get_drvdata(vdev);
933         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
934         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
935         struct vpif_params *vpif_params = &ch->vpifparams;
936
937         /*
938          * to supress v4l-compliance warnings silently correct
939          * the pixelformat
940          */
941         if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
942                 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
943                         pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
944         } else {
945                 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
946                         pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
947         }
948
949         common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
950
951         vpif_update_std_info(ch);
952
953         pixfmt->field = common->fmt.fmt.pix.field;
954         pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
955         pixfmt->bytesperline = common->fmt.fmt.pix.width;
956         pixfmt->width = common->fmt.fmt.pix.width;
957         pixfmt->height = common->fmt.fmt.pix.height;
958         pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
959         pixfmt->priv = 0;
960
961         return 0;
962 }
963
964
965 /**
966  * vpif_g_fmt_vid_cap() - Set INPUT handler
967  * @file: file ptr
968  * @priv: file handle
969  * @fmt: ptr to v4l2 format structure
970  */
971 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
972                                 struct v4l2_format *fmt)
973 {
974         struct video_device *vdev = video_devdata(file);
975         struct channel_obj *ch = video_get_drvdata(vdev);
976         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
977
978         /* Check the validity of the buffer type */
979         if (common->fmt.type != fmt->type)
980                 return -EINVAL;
981
982         /* Fill in the information about format */
983         *fmt = common->fmt;
984         return 0;
985 }
986
987 /**
988  * vpif_s_fmt_vid_cap() - Set FMT handler
989  * @file: file ptr
990  * @priv: file handle
991  * @fmt: ptr to v4l2 format structure
992  */
993 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
994                                 struct v4l2_format *fmt)
995 {
996         struct video_device *vdev = video_devdata(file);
997         struct channel_obj *ch = video_get_drvdata(vdev);
998         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
999         int ret;
1000
1001         vpif_dbg(2, debug, "%s\n", __func__);
1002
1003         if (vb2_is_busy(&common->buffer_queue))
1004                 return -EBUSY;
1005
1006         ret = vpif_try_fmt_vid_cap(file, priv, fmt);
1007         if (ret)
1008                 return ret;
1009
1010         /* store the format in the channel object */
1011         common->fmt = *fmt;
1012         return 0;
1013 }
1014
1015 /**
1016  * vpif_querycap() - QUERYCAP handler
1017  * @file: file ptr
1018  * @priv: file handle
1019  * @cap: ptr to v4l2_capability structure
1020  */
1021 static int vpif_querycap(struct file *file, void  *priv,
1022                                 struct v4l2_capability *cap)
1023 {
1024         struct vpif_capture_config *config = vpif_dev->platform_data;
1025
1026         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1027         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1028         strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1029         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1030                  dev_name(vpif_dev));
1031         strlcpy(cap->card, config->card_name, sizeof(cap->card));
1032
1033         return 0;
1034 }
1035
1036 /**
1037  * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1038  * @file: file ptr
1039  * @priv: file handle
1040  * @timings: input timings
1041  */
1042 static int
1043 vpif_enum_dv_timings(struct file *file, void *priv,
1044                      struct v4l2_enum_dv_timings *timings)
1045 {
1046         struct vpif_capture_config *config = vpif_dev->platform_data;
1047         struct video_device *vdev = video_devdata(file);
1048         struct channel_obj *ch = video_get_drvdata(vdev);
1049         struct vpif_capture_chan_config *chan_cfg;
1050         struct v4l2_input input;
1051         int ret;
1052
1053         if (!config->chan_config[ch->channel_id].inputs)
1054                 return -ENODATA;
1055
1056         chan_cfg = &config->chan_config[ch->channel_id];
1057         input = chan_cfg->inputs[ch->input_idx].input;
1058         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1059                 return -ENODATA;
1060
1061         timings->pad = 0;
1062
1063         ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1064         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1065                 return -EINVAL;
1066
1067         return ret;
1068 }
1069
1070 /**
1071  * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1072  * @file: file ptr
1073  * @priv: file handle
1074  * @timings: input timings
1075  */
1076 static int
1077 vpif_query_dv_timings(struct file *file, void *priv,
1078                       struct v4l2_dv_timings *timings)
1079 {
1080         struct vpif_capture_config *config = vpif_dev->platform_data;
1081         struct video_device *vdev = video_devdata(file);
1082         struct channel_obj *ch = video_get_drvdata(vdev);
1083         struct vpif_capture_chan_config *chan_cfg;
1084         struct v4l2_input input;
1085         int ret;
1086
1087         if (!config->chan_config[ch->channel_id].inputs)
1088                 return -ENODATA;
1089
1090         chan_cfg = &config->chan_config[ch->channel_id];
1091         input = chan_cfg->inputs[ch->input_idx].input;
1092         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1093                 return -ENODATA;
1094
1095         ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1096         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1097                 return -ENODATA;
1098
1099         return ret;
1100 }
1101
1102 /**
1103  * vpif_s_dv_timings() - S_DV_TIMINGS handler
1104  * @file: file ptr
1105  * @priv: file handle
1106  * @timings: digital video timings
1107  */
1108 static int vpif_s_dv_timings(struct file *file, void *priv,
1109                 struct v4l2_dv_timings *timings)
1110 {
1111         struct vpif_capture_config *config = vpif_dev->platform_data;
1112         struct video_device *vdev = video_devdata(file);
1113         struct channel_obj *ch = video_get_drvdata(vdev);
1114         struct vpif_params *vpifparams = &ch->vpifparams;
1115         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1116         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1117         struct video_obj *vid_ch = &ch->video;
1118         struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1119         struct vpif_capture_chan_config *chan_cfg;
1120         struct v4l2_input input;
1121         int ret;
1122
1123         if (!config->chan_config[ch->channel_id].inputs)
1124                 return -ENODATA;
1125
1126         chan_cfg = &config->chan_config[ch->channel_id];
1127         input = chan_cfg->inputs[ch->input_idx].input;
1128         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1129                 return -ENODATA;
1130
1131         if (timings->type != V4L2_DV_BT_656_1120) {
1132                 vpif_dbg(2, debug, "Timing type not defined\n");
1133                 return -EINVAL;
1134         }
1135
1136         if (vb2_is_busy(&common->buffer_queue))
1137                 return -EBUSY;
1138
1139         /* Configure subdevice timings, if any */
1140         ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1141         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1142                 ret = 0;
1143         if (ret < 0) {
1144                 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1145                 return ret;
1146         }
1147
1148         if (!(timings->bt.width && timings->bt.height &&
1149                                 (timings->bt.hbackporch ||
1150                                  timings->bt.hfrontporch ||
1151                                  timings->bt.hsync) &&
1152                                 timings->bt.vfrontporch &&
1153                                 (timings->bt.vbackporch ||
1154                                  timings->bt.vsync))) {
1155                 vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n");
1156                 return -EINVAL;
1157         }
1158
1159         vid_ch->dv_timings = *timings;
1160
1161         /* Configure video port timings */
1162
1163         std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1164         std_info->sav2eav = bt->width;
1165
1166         std_info->l1 = 1;
1167         std_info->l3 = bt->vsync + bt->vbackporch + 1;
1168
1169         std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1170         if (bt->interlaced) {
1171                 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1172                         std_info->l5 = std_info->vsize/2 -
1173                                 (bt->vfrontporch - 1);
1174                         std_info->l7 = std_info->vsize/2 + 1;
1175                         std_info->l9 = std_info->l7 + bt->il_vsync +
1176                                 bt->il_vbackporch + 1;
1177                         std_info->l11 = std_info->vsize -
1178                                 (bt->il_vfrontporch - 1);
1179                 } else {
1180                         vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
1181                         return -EINVAL;
1182                 }
1183         } else {
1184                 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1185         }
1186         strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1187         std_info->width = bt->width;
1188         std_info->height = bt->height;
1189         std_info->frm_fmt = bt->interlaced ? 0 : 1;
1190         std_info->ycmux_mode = 0;
1191         std_info->capture_format = 0;
1192         std_info->vbi_supported = 0;
1193         std_info->hd_sd = 1;
1194         std_info->stdid = 0;
1195
1196         vid_ch->stdid = 0;
1197         return 0;
1198 }
1199
1200 /**
1201  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1202  * @file: file ptr
1203  * @priv: file handle
1204  * @timings: digital video timings
1205  */
1206 static int vpif_g_dv_timings(struct file *file, void *priv,
1207                 struct v4l2_dv_timings *timings)
1208 {
1209         struct vpif_capture_config *config = vpif_dev->platform_data;
1210         struct video_device *vdev = video_devdata(file);
1211         struct channel_obj *ch = video_get_drvdata(vdev);
1212         struct video_obj *vid_ch = &ch->video;
1213         struct vpif_capture_chan_config *chan_cfg;
1214         struct v4l2_input input;
1215
1216         if (!config->chan_config[ch->channel_id].inputs)
1217                 return -ENODATA;
1218
1219         chan_cfg = &config->chan_config[ch->channel_id];
1220         input = chan_cfg->inputs[ch->input_idx].input;
1221         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1222                 return -ENODATA;
1223
1224         *timings = vid_ch->dv_timings;
1225
1226         return 0;
1227 }
1228
1229 /*
1230  * vpif_log_status() - Status information
1231  * @file: file ptr
1232  * @priv: file handle
1233  *
1234  * Returns zero.
1235  */
1236 static int vpif_log_status(struct file *filep, void *priv)
1237 {
1238         /* status for sub devices */
1239         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1240
1241         return 0;
1242 }
1243
1244 /* vpif capture ioctl operations */
1245 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1246         .vidioc_querycap                = vpif_querycap,
1247         .vidioc_enum_fmt_vid_cap        = vpif_enum_fmt_vid_cap,
1248         .vidioc_g_fmt_vid_cap           = vpif_g_fmt_vid_cap,
1249         .vidioc_s_fmt_vid_cap           = vpif_s_fmt_vid_cap,
1250         .vidioc_try_fmt_vid_cap         = vpif_try_fmt_vid_cap,
1251
1252         .vidioc_enum_input              = vpif_enum_input,
1253         .vidioc_s_input                 = vpif_s_input,
1254         .vidioc_g_input                 = vpif_g_input,
1255
1256         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
1257         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
1258         .vidioc_querybuf                = vb2_ioctl_querybuf,
1259         .vidioc_qbuf                    = vb2_ioctl_qbuf,
1260         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1261         .vidioc_expbuf                  = vb2_ioctl_expbuf,
1262         .vidioc_streamon                = vb2_ioctl_streamon,
1263         .vidioc_streamoff               = vb2_ioctl_streamoff,
1264
1265         .vidioc_querystd                = vpif_querystd,
1266         .vidioc_s_std                   = vpif_s_std,
1267         .vidioc_g_std                   = vpif_g_std,
1268
1269         .vidioc_enum_dv_timings         = vpif_enum_dv_timings,
1270         .vidioc_query_dv_timings        = vpif_query_dv_timings,
1271         .vidioc_s_dv_timings            = vpif_s_dv_timings,
1272         .vidioc_g_dv_timings            = vpif_g_dv_timings,
1273
1274         .vidioc_log_status              = vpif_log_status,
1275 };
1276
1277 /* vpif file operations */
1278 static struct v4l2_file_operations vpif_fops = {
1279         .owner = THIS_MODULE,
1280         .open = v4l2_fh_open,
1281         .release = vb2_fop_release,
1282         .unlocked_ioctl = video_ioctl2,
1283         .mmap = vb2_fop_mmap,
1284         .poll = vb2_fop_poll
1285 };
1286
1287 /**
1288  * initialize_vpif() - Initialize vpif data structures
1289  *
1290  * Allocate memory for data structures and initialize them
1291  */
1292 static int initialize_vpif(void)
1293 {
1294         int err, i, j;
1295         int free_channel_objects_index;
1296
1297         /* Allocate memory for six channel objects */
1298         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1299                 vpif_obj.dev[i] =
1300                     kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1301                 /* If memory allocation fails, return error */
1302                 if (!vpif_obj.dev[i]) {
1303                         free_channel_objects_index = i;
1304                         err = -ENOMEM;
1305                         goto vpif_init_free_channel_objects;
1306                 }
1307         }
1308         return 0;
1309
1310 vpif_init_free_channel_objects:
1311         for (j = 0; j < free_channel_objects_index; j++)
1312                 kfree(vpif_obj.dev[j]);
1313         return err;
1314 }
1315
1316 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1317                             struct v4l2_subdev *subdev,
1318                             struct v4l2_async_subdev *asd)
1319 {
1320         int i;
1321
1322         for (i = 0; i < vpif_obj.config->subdev_count; i++)
1323                 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1324                             subdev->name)) {
1325                         vpif_obj.sd[i] = subdev;
1326                         return 0;
1327                 }
1328
1329         return -EINVAL;
1330 }
1331
1332 static int vpif_probe_complete(void)
1333 {
1334         struct common_obj *common;
1335         struct video_device *vdev;
1336         struct channel_obj *ch;
1337         struct vb2_queue *q;
1338         int j, err, k;
1339
1340         for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1341                 ch = vpif_obj.dev[j];
1342                 ch->channel_id = j;
1343                 common = &(ch->common[VPIF_VIDEO_INDEX]);
1344                 spin_lock_init(&common->irqlock);
1345                 mutex_init(&common->lock);
1346
1347                 /* select input 0 */
1348                 err = vpif_set_input(vpif_obj.config, ch, 0);
1349                 if (err)
1350                         goto probe_out;
1351
1352                 /* set initial format */
1353                 ch->video.stdid = V4L2_STD_525_60;
1354                 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1355                 vpif_update_std_info(ch);
1356
1357                 /* Initialize vb2 queue */
1358                 q = &common->buffer_queue;
1359                 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1360                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1361                 q->drv_priv = ch;
1362                 q->ops = &video_qops;
1363                 q->mem_ops = &vb2_dma_contig_memops;
1364                 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1365                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1366                 q->min_buffers_needed = 1;
1367                 q->lock = &common->lock;
1368                 q->dev = vpif_dev;
1369
1370                 err = vb2_queue_init(q);
1371                 if (err) {
1372                         vpif_err("vpif_capture: vb2_queue_init() failed\n");
1373                         goto probe_out;
1374                 }
1375
1376                 INIT_LIST_HEAD(&common->dma_queue);
1377
1378                 /* Initialize the video_device structure */
1379                 vdev = &ch->video_dev;
1380                 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1381                 vdev->release = video_device_release_empty;
1382                 vdev->fops = &vpif_fops;
1383                 vdev->ioctl_ops = &vpif_ioctl_ops;
1384                 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1385                 vdev->vfl_dir = VFL_DIR_RX;
1386                 vdev->queue = q;
1387                 vdev->lock = &common->lock;
1388                 video_set_drvdata(&ch->video_dev, ch);
1389                 err = video_register_device(vdev,
1390                                             VFL_TYPE_GRABBER, (j ? 1 : 0));
1391                 if (err)
1392                         goto probe_out;
1393         }
1394
1395         v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1396         return 0;
1397
1398 probe_out:
1399         for (k = 0; k < j; k++) {
1400                 /* Get the pointer to the channel object */
1401                 ch = vpif_obj.dev[k];
1402                 common = &ch->common[k];
1403                 /* Unregister video device */
1404                 video_unregister_device(&ch->video_dev);
1405         }
1406         kfree(vpif_obj.sd);
1407         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1408
1409         return err;
1410 }
1411
1412 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1413 {
1414         return vpif_probe_complete();
1415 }
1416
1417 /**
1418  * vpif_probe : This function probes the vpif capture driver
1419  * @pdev: platform device pointer
1420  *
1421  * This creates device entries by register itself to the V4L2 driver and
1422  * initializes fields of each channel objects
1423  */
1424 static __init int vpif_probe(struct platform_device *pdev)
1425 {
1426         struct vpif_subdev_info *subdevdata;
1427         struct i2c_adapter *i2c_adap;
1428         struct resource *res;
1429         int subdev_count;
1430         int res_idx = 0;
1431         int i, err;
1432
1433         vpif_dev = &pdev->dev;
1434
1435         err = initialize_vpif();
1436         if (err) {
1437                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1438                 return err;
1439         }
1440
1441         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1442         if (err) {
1443                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1444                 return err;
1445         }
1446
1447         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1448                 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1449                                         IRQF_SHARED, VPIF_DRIVER_NAME,
1450                                         (void *)(&vpif_obj.dev[res_idx]->
1451                                         channel_id));
1452                 if (err) {
1453                         err = -EINVAL;
1454                         goto vpif_unregister;
1455                 }
1456                 res_idx++;
1457         }
1458
1459         vpif_obj.config = pdev->dev.platform_data;
1460
1461         subdev_count = vpif_obj.config->subdev_count;
1462         vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
1463         if (!vpif_obj.sd) {
1464                 err = -ENOMEM;
1465                 goto vpif_unregister;
1466         }
1467
1468         if (!vpif_obj.config->asd_sizes) {
1469                 i2c_adap = i2c_get_adapter(1);
1470                 for (i = 0; i < subdev_count; i++) {
1471                         subdevdata = &vpif_obj.config->subdev_info[i];
1472                         vpif_obj.sd[i] =
1473                                 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1474                                                           i2c_adap,
1475                                                           &subdevdata->
1476                                                           board_info,
1477                                                           NULL);
1478
1479                         if (!vpif_obj.sd[i]) {
1480                                 vpif_err("Error registering v4l2 subdevice\n");
1481                                 err = -ENODEV;
1482                                 goto probe_subdev_out;
1483                         }
1484                         v4l2_info(&vpif_obj.v4l2_dev,
1485                                   "registered sub device %s\n",
1486                                    subdevdata->name);
1487                 }
1488                 vpif_probe_complete();
1489         } else {
1490                 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1491                 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1492                 vpif_obj.notifier.bound = vpif_async_bound;
1493                 vpif_obj.notifier.complete = vpif_async_complete;
1494                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1495                                                    &vpif_obj.notifier);
1496                 if (err) {
1497                         vpif_err("Error registering async notifier\n");
1498                         err = -EINVAL;
1499                         goto probe_subdev_out;
1500                 }
1501         }
1502
1503         return 0;
1504
1505 probe_subdev_out:
1506         /* free sub devices memory */
1507         kfree(vpif_obj.sd);
1508 vpif_unregister:
1509         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1510
1511         return err;
1512 }
1513
1514 /**
1515  * vpif_remove() - driver remove handler
1516  * @device: ptr to platform device structure
1517  *
1518  * The vidoe device is unregistered
1519  */
1520 static int vpif_remove(struct platform_device *device)
1521 {
1522         struct common_obj *common;
1523         struct channel_obj *ch;
1524         int i;
1525
1526         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1527
1528         kfree(vpif_obj.sd);
1529         /* un-register device */
1530         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1531                 /* Get the pointer to the channel object */
1532                 ch = vpif_obj.dev[i];
1533                 common = &ch->common[VPIF_VIDEO_INDEX];
1534                 /* Unregister video device */
1535                 video_unregister_device(&ch->video_dev);
1536                 kfree(vpif_obj.dev[i]);
1537         }
1538         return 0;
1539 }
1540
1541 #ifdef CONFIG_PM_SLEEP
1542 /**
1543  * vpif_suspend: vpif device suspend
1544  */
1545 static int vpif_suspend(struct device *dev)
1546 {
1547
1548         struct common_obj *common;
1549         struct channel_obj *ch;
1550         int i;
1551
1552         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1553                 /* Get the pointer to the channel object */
1554                 ch = vpif_obj.dev[i];
1555                 common = &ch->common[VPIF_VIDEO_INDEX];
1556
1557                 if (!vb2_start_streaming_called(&common->buffer_queue))
1558                         continue;
1559
1560                 mutex_lock(&common->lock);
1561                 /* Disable channel */
1562                 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1563                         enable_channel0(0);
1564                         channel0_intr_enable(0);
1565                 }
1566                 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1567                         ycmux_mode == 2) {
1568                         enable_channel1(0);
1569                         channel1_intr_enable(0);
1570                 }
1571                 mutex_unlock(&common->lock);
1572         }
1573
1574         return 0;
1575 }
1576
1577 /*
1578  * vpif_resume: vpif device suspend
1579  */
1580 static int vpif_resume(struct device *dev)
1581 {
1582         struct common_obj *common;
1583         struct channel_obj *ch;
1584         int i;
1585
1586         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1587                 /* Get the pointer to the channel object */
1588                 ch = vpif_obj.dev[i];
1589                 common = &ch->common[VPIF_VIDEO_INDEX];
1590
1591                 if (!vb2_start_streaming_called(&common->buffer_queue))
1592                         continue;
1593
1594                 mutex_lock(&common->lock);
1595                 /* Enable channel */
1596                 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1597                         enable_channel0(1);
1598                         channel0_intr_enable(1);
1599                 }
1600                 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1601                         ycmux_mode == 2) {
1602                         enable_channel1(1);
1603                         channel1_intr_enable(1);
1604                 }
1605                 mutex_unlock(&common->lock);
1606         }
1607
1608         return 0;
1609 }
1610 #endif
1611
1612 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1613
1614 static __refdata struct platform_driver vpif_driver = {
1615         .driver = {
1616                 .name   = VPIF_DRIVER_NAME,
1617                 .pm     = &vpif_pm_ops,
1618         },
1619         .probe = vpif_probe,
1620         .remove = vpif_remove,
1621 };
1622
1623 module_platform_driver(vpif_driver);