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