Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / platform / davinci / vpbe_display.c
1 /*
2  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation version 2.
7  *
8  * This program is distributed WITHOUT ANY WARRANTY of any
9  * kind, whether express or implied; without even the implied warranty
10  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/interrupt.h>
18 #include <linux/string.h>
19 #include <linux/wait.h>
20 #include <linux/time.h>
21 #include <linux/platform_device.h>
22 #include <linux/irq.h>
23 #include <linux/mm.h>
24 #include <linux/mutex.h>
25 #include <linux/videodev2.h>
26 #include <linux/slab.h>
27
28 #include <asm/pgtable.h>
29 #include <mach/cputype.h>
30
31 #include <media/v4l2-dev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-device.h>
35 #include <media/davinci/vpbe_display.h>
36 #include <media/davinci/vpbe_types.h>
37 #include <media/davinci/vpbe.h>
38 #include <media/davinci/vpbe_venc.h>
39 #include <media/davinci/vpbe_osd.h>
40 #include "vpbe_venc_regs.h"
41
42 #define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
43
44 static int debug;
45
46 #define VPBE_DEFAULT_NUM_BUFS 3
47
48 module_param(debug, int, 0644);
49
50 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
51                         struct vpbe_layer *layer);
52
53 static int venc_is_second_field(struct vpbe_display *disp_dev)
54 {
55         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
56         int ret;
57         int val;
58
59         ret = v4l2_subdev_call(vpbe_dev->venc,
60                                core,
61                                ioctl,
62                                VENC_GET_FLD,
63                                &val);
64         if (ret < 0) {
65                 v4l2_err(&vpbe_dev->v4l2_dev,
66                          "Error in getting Field ID 0\n");
67         }
68         return val;
69 }
70
71 static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
72                                 struct vpbe_layer *layer)
73 {
74         struct timespec timevalue;
75
76         if (layer->cur_frm == layer->next_frm)
77                 return;
78         ktime_get_ts(&timevalue);
79         layer->cur_frm->vb.v4l2_buf.timestamp.tv_sec =
80                 timevalue.tv_sec;
81         layer->cur_frm->vb.v4l2_buf.timestamp.tv_usec =
82                 timevalue.tv_nsec / NSEC_PER_USEC;
83         vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_DONE);
84         /* Make cur_frm pointing to next_frm */
85         layer->cur_frm = layer->next_frm;
86 }
87
88 static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
89                                 struct vpbe_layer *layer)
90 {
91         struct osd_state *osd_device = disp_obj->osd_device;
92         unsigned long addr;
93
94         spin_lock(&disp_obj->dma_queue_lock);
95         if (list_empty(&layer->dma_queue) ||
96                 (layer->cur_frm != layer->next_frm)) {
97                 spin_unlock(&disp_obj->dma_queue_lock);
98                 return;
99         }
100         /*
101          * one field is displayed configure
102          * the next frame if it is available
103          * otherwise hold on current frame
104          * Get next from the buffer queue
105          */
106         layer->next_frm = list_entry(layer->dma_queue.next,
107                           struct  vpbe_disp_buffer, list);
108         /* Remove that from the buffer queue */
109         list_del(&layer->next_frm->list);
110         spin_unlock(&disp_obj->dma_queue_lock);
111         /* Mark state of the frame to active */
112         layer->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
113         addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb, 0);
114         osd_device->ops.start_layer(osd_device,
115                         layer->layer_info.id,
116                         addr,
117                         disp_obj->cbcr_ofst);
118 }
119
120 /* interrupt service routine */
121 static irqreturn_t venc_isr(int irq, void *arg)
122 {
123         struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
124         struct vpbe_layer *layer;
125         static unsigned last_event;
126         unsigned event = 0;
127         int fid;
128         int i;
129
130         if ((NULL == arg) || (NULL == disp_dev->dev[0]))
131                 return IRQ_HANDLED;
132
133         if (venc_is_second_field(disp_dev))
134                 event |= VENC_SECOND_FIELD;
135         else
136                 event |= VENC_FIRST_FIELD;
137
138         if (event == (last_event & ~VENC_END_OF_FRAME)) {
139                 /*
140                 * If the display is non-interlaced, then we need to flag the
141                 * end-of-frame event at every interrupt regardless of the
142                 * value of the FIDST bit.  We can conclude that the display is
143                 * non-interlaced if the value of the FIDST bit is unchanged
144                 * from the previous interrupt.
145                 */
146                 event |= VENC_END_OF_FRAME;
147         } else if (event == VENC_SECOND_FIELD) {
148                 /* end-of-frame for interlaced display */
149                 event |= VENC_END_OF_FRAME;
150         }
151         last_event = event;
152
153         for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
154                 layer = disp_dev->dev[i];
155                 /* If streaming is started in this layer */
156                 if (!layer->started)
157                         continue;
158
159                 if (layer->layer_first_int) {
160                         layer->layer_first_int = 0;
161                         continue;
162                 }
163                 /* Check the field format */
164                 if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
165                         (event & VENC_END_OF_FRAME)) {
166                         /* Progressive mode */
167
168                         vpbe_isr_even_field(disp_dev, layer);
169                         vpbe_isr_odd_field(disp_dev, layer);
170                 } else {
171                 /* Interlaced mode */
172
173                         layer->field_id ^= 1;
174                         if (event & VENC_FIRST_FIELD)
175                                 fid = 0;
176                         else
177                                 fid = 1;
178
179                         /*
180                         * If field id does not match with store
181                         * field id
182                         */
183                         if (fid != layer->field_id) {
184                                 /* Make them in sync */
185                                 layer->field_id = fid;
186                                 continue;
187                         }
188                         /*
189                         * device field id and local field id are
190                         * in sync. If this is even field
191                         */
192                         if (0 == fid)
193                                 vpbe_isr_even_field(disp_dev, layer);
194                         else  /* odd field */
195                                 vpbe_isr_odd_field(disp_dev, layer);
196                 }
197         }
198
199         return IRQ_HANDLED;
200 }
201
202 /*
203  * vpbe_buffer_prepare()
204  * This is the callback function called from vb2_qbuf() function
205  * the buffer is prepared and user space virtual address is converted into
206  * physical address
207  */
208 static int vpbe_buffer_prepare(struct vb2_buffer *vb)
209 {
210         struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
211         struct vb2_queue *q = vb->vb2_queue;
212         struct vpbe_layer *layer = fh->layer;
213         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
214         unsigned long addr;
215
216         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
217                                 "vpbe_buffer_prepare\n");
218
219         if (vb->state != VB2_BUF_STATE_ACTIVE &&
220                 vb->state != VB2_BUF_STATE_PREPARED) {
221                 vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
222                 if (vb2_plane_vaddr(vb, 0) &&
223                 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
224                         return -EINVAL;
225
226                 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
227                 if (q->streaming) {
228                         if (!IS_ALIGNED(addr, 8)) {
229                                 v4l2_err(&vpbe_dev->v4l2_dev,
230                                         "buffer_prepare:offset is \
231                                         not aligned to 32 bytes\n");
232                                 return -EINVAL;
233                         }
234                 }
235         }
236         return 0;
237 }
238
239 /*
240  * vpbe_buffer_setup()
241  * This function allocates memory for the buffers
242  */
243 static int
244 vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
245                         unsigned int *nbuffers, unsigned int *nplanes,
246                         unsigned int sizes[], void *alloc_ctxs[])
247
248 {
249         /* Get the file handle object and layer object */
250         struct vpbe_fh *fh = vb2_get_drv_priv(vq);
251         struct vpbe_layer *layer = fh->layer;
252         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
253
254         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
255
256         /* Store number of buffers allocated in numbuffer member */
257         if (*nbuffers < VPBE_DEFAULT_NUM_BUFS)
258                 *nbuffers = layer->numbuffers = VPBE_DEFAULT_NUM_BUFS;
259
260         *nplanes = 1;
261         sizes[0] = layer->pix_fmt.sizeimage;
262         alloc_ctxs[0] = layer->alloc_ctx;
263
264         return 0;
265 }
266
267 /*
268  * vpbe_buffer_queue()
269  * This function adds the buffer to DMA queue
270  */
271 static void vpbe_buffer_queue(struct vb2_buffer *vb)
272 {
273         /* Get the file handle object and layer object */
274         struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
275         struct vpbe_disp_buffer *buf = container_of(vb,
276                                 struct vpbe_disp_buffer, vb);
277         struct vpbe_layer *layer = fh->layer;
278         struct vpbe_display *disp = fh->disp_dev;
279         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
280         unsigned long flags;
281
282         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
283                         "vpbe_buffer_queue\n");
284
285         /* add the buffer to the DMA queue */
286         spin_lock_irqsave(&disp->dma_queue_lock, flags);
287         list_add_tail(&buf->list, &layer->dma_queue);
288         spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
289 }
290
291 /*
292  * vpbe_buf_cleanup()
293  * This function is called from the vb2 layer to free memory allocated to
294  * the buffers
295  */
296 static void vpbe_buf_cleanup(struct vb2_buffer *vb)
297 {
298         /* Get the file handle object and layer object */
299         struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
300         struct vpbe_layer *layer = fh->layer;
301         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
302         struct vpbe_disp_buffer *buf = container_of(vb,
303                                         struct vpbe_disp_buffer, vb);
304         unsigned long flags;
305
306         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
307                         "vpbe_buf_cleanup\n");
308
309         spin_lock_irqsave(&layer->irqlock, flags);
310         if (vb->state == VB2_BUF_STATE_ACTIVE)
311                 list_del_init(&buf->list);
312         spin_unlock_irqrestore(&layer->irqlock, flags);
313 }
314
315 static void vpbe_wait_prepare(struct vb2_queue *vq)
316 {
317         struct vpbe_fh *fh = vb2_get_drv_priv(vq);
318         struct vpbe_layer *layer = fh->layer;
319
320         mutex_unlock(&layer->opslock);
321 }
322
323 static void vpbe_wait_finish(struct vb2_queue *vq)
324 {
325         struct vpbe_fh *fh = vb2_get_drv_priv(vq);
326         struct vpbe_layer *layer = fh->layer;
327
328         mutex_lock(&layer->opslock);
329 }
330
331 static int vpbe_buffer_init(struct vb2_buffer *vb)
332 {
333         struct vpbe_disp_buffer *buf = container_of(vb,
334                                         struct vpbe_disp_buffer, vb);
335
336         INIT_LIST_HEAD(&buf->list);
337         return 0;
338 }
339
340 static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
341 {
342         struct vpbe_fh *fh = vb2_get_drv_priv(vq);
343         struct vpbe_layer *layer = fh->layer;
344         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
345         int ret;
346
347         /* Get the next frame from the buffer queue */
348         layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
349                                 struct vpbe_disp_buffer, list);
350         /* Remove buffer from the buffer queue */
351         list_del(&layer->cur_frm->list);
352         /* Mark state of the current frame to active */
353         layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
354         /* Initialize field_id and started member */
355         layer->field_id = 0;
356
357         /* Set parameters in OSD and VENC */
358         ret = vpbe_set_osd_display_params(fh->disp_dev, layer);
359         if (ret < 0)
360                 return ret;
361
362         /*
363          * if request format is yuv420 semiplanar, need to
364          * enable both video windows
365          */
366         layer->started = 1;
367         layer->layer_first_int = 1;
368
369         return ret;
370 }
371
372 static int vpbe_stop_streaming(struct vb2_queue *vq)
373 {
374         struct vpbe_fh *fh = vb2_get_drv_priv(vq);
375         struct vpbe_layer *layer = fh->layer;
376
377         if (!vb2_is_streaming(vq))
378                 return 0;
379
380         /* release all active buffers */
381         while (!list_empty(&layer->dma_queue)) {
382                 layer->next_frm = list_entry(layer->dma_queue.next,
383                                                 struct vpbe_disp_buffer, list);
384                 list_del(&layer->next_frm->list);
385                 vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
386         }
387
388         return 0;
389 }
390
391 static struct vb2_ops video_qops = {
392         .queue_setup = vpbe_buffer_queue_setup,
393         .wait_prepare = vpbe_wait_prepare,
394         .wait_finish = vpbe_wait_finish,
395         .buf_init = vpbe_buffer_init,
396         .buf_prepare = vpbe_buffer_prepare,
397         .start_streaming = vpbe_start_streaming,
398         .stop_streaming = vpbe_stop_streaming,
399         .buf_cleanup = vpbe_buf_cleanup,
400         .buf_queue = vpbe_buffer_queue,
401 };
402
403 static
404 struct vpbe_layer*
405 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
406                         struct vpbe_layer *layer)
407 {
408         enum vpbe_display_device_id thiswin, otherwin;
409         thiswin = layer->device_id;
410
411         otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
412         VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
413         return disp_dev->dev[otherwin];
414 }
415
416 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
417                         struct vpbe_layer *layer)
418 {
419         struct osd_layer_config *cfg  = &layer->layer_info.config;
420         struct osd_state *osd_device = disp_dev->osd_device;
421         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
422         unsigned long addr;
423         int ret;
424
425         addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
426         /* Set address in the display registers */
427         osd_device->ops.start_layer(osd_device,
428                                     layer->layer_info.id,
429                                     addr,
430                                     disp_dev->cbcr_ofst);
431
432         ret = osd_device->ops.enable_layer(osd_device,
433                                 layer->layer_info.id, 0);
434         if (ret < 0) {
435                 v4l2_err(&vpbe_dev->v4l2_dev,
436                         "Error in enabling osd window layer 0\n");
437                 return -1;
438         }
439
440         /* Enable the window */
441         layer->layer_info.enable = 1;
442         if (cfg->pixfmt == PIXFMT_NV12) {
443                 struct vpbe_layer *otherlayer =
444                         _vpbe_display_get_other_win_layer(disp_dev, layer);
445
446                 ret = osd_device->ops.enable_layer(osd_device,
447                                 otherlayer->layer_info.id, 1);
448                 if (ret < 0) {
449                         v4l2_err(&vpbe_dev->v4l2_dev,
450                                 "Error in enabling osd window layer 1\n");
451                         return -1;
452                 }
453                 otherlayer->layer_info.enable = 1;
454         }
455         return 0;
456 }
457
458 static void
459 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
460                         struct vpbe_layer *layer,
461                         int expected_xsize, int expected_ysize)
462 {
463         struct display_layer_info *layer_info = &layer->layer_info;
464         struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
465         struct osd_layer_config *cfg  = &layer->layer_info.config;
466         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
467         int calculated_xsize;
468         int h_exp = 0;
469         int v_exp = 0;
470         int h_scale;
471         int v_scale;
472
473         v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
474
475         /*
476          * Application initially set the image format. Current display
477          * size is obtained from the vpbe display controller. expected_xsize
478          * and expected_ysize are set through S_CROP ioctl. Based on this,
479          * driver will calculate the scale factors for vertical and
480          * horizontal direction so that the image is displayed scaled
481          * and expanded. Application uses expansion to display the image
482          * in a square pixel. Otherwise it is displayed using displays
483          * pixel aspect ratio.It is expected that application chooses
484          * the crop coordinates for cropped or scaled display. if crop
485          * size is less than the image size, it is displayed cropped or
486          * it is displayed scaled and/or expanded.
487          *
488          * to begin with, set the crop window same as expected. Later we
489          * will override with scaled window size
490          */
491
492         cfg->xsize = pixfmt->width;
493         cfg->ysize = pixfmt->height;
494         layer_info->h_zoom = ZOOM_X1;   /* no horizontal zoom */
495         layer_info->v_zoom = ZOOM_X1;   /* no horizontal zoom */
496         layer_info->h_exp = H_EXP_OFF;  /* no horizontal zoom */
497         layer_info->v_exp = V_EXP_OFF;  /* no horizontal zoom */
498
499         if (pixfmt->width < expected_xsize) {
500                 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
501                 if (h_scale < 2)
502                         h_scale = 1;
503                 else if (h_scale >= 4)
504                         h_scale = 4;
505                 else
506                         h_scale = 2;
507                 cfg->xsize *= h_scale;
508                 if (cfg->xsize < expected_xsize) {
509                         if ((standard_id & V4L2_STD_525_60) ||
510                         (standard_id & V4L2_STD_625_50)) {
511                                 calculated_xsize = (cfg->xsize *
512                                         VPBE_DISPLAY_H_EXP_RATIO_N) /
513                                         VPBE_DISPLAY_H_EXP_RATIO_D;
514                                 if (calculated_xsize <= expected_xsize) {
515                                         h_exp = 1;
516                                         cfg->xsize = calculated_xsize;
517                                 }
518                         }
519                 }
520                 if (h_scale == 2)
521                         layer_info->h_zoom = ZOOM_X2;
522                 else if (h_scale == 4)
523                         layer_info->h_zoom = ZOOM_X4;
524                 if (h_exp)
525                         layer_info->h_exp = H_EXP_9_OVER_8;
526         } else {
527                 /* no scaling, only cropping. Set display area to crop area */
528                 cfg->xsize = expected_xsize;
529         }
530
531         if (pixfmt->height < expected_ysize) {
532                 v_scale = expected_ysize / pixfmt->height;
533                 if (v_scale < 2)
534                         v_scale = 1;
535                 else if (v_scale >= 4)
536                         v_scale = 4;
537                 else
538                         v_scale = 2;
539                 cfg->ysize *= v_scale;
540                 if (cfg->ysize < expected_ysize) {
541                         if ((standard_id & V4L2_STD_625_50)) {
542                                 calculated_xsize = (cfg->ysize *
543                                         VPBE_DISPLAY_V_EXP_RATIO_N) /
544                                         VPBE_DISPLAY_V_EXP_RATIO_D;
545                                 if (calculated_xsize <= expected_ysize) {
546                                         v_exp = 1;
547                                         cfg->ysize = calculated_xsize;
548                                 }
549                         }
550                 }
551                 if (v_scale == 2)
552                         layer_info->v_zoom = ZOOM_X2;
553                 else if (v_scale == 4)
554                         layer_info->v_zoom = ZOOM_X4;
555                 if (v_exp)
556                         layer_info->h_exp = V_EXP_6_OVER_5;
557         } else {
558                 /* no scaling, only cropping. Set display area to crop area */
559                 cfg->ysize = expected_ysize;
560         }
561         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
562                 "crop display xsize = %d, ysize = %d\n",
563                 cfg->xsize, cfg->ysize);
564 }
565
566 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
567                         struct vpbe_layer *layer,
568                         int top, int left)
569 {
570         struct osd_layer_config *cfg = &layer->layer_info.config;
571         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
572
573         cfg->xpos = min((unsigned int)left,
574                         vpbe_dev->current_timings.xres - cfg->xsize);
575         cfg->ypos = min((unsigned int)top,
576                         vpbe_dev->current_timings.yres - cfg->ysize);
577
578         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
579                 "new xpos = %d, ypos = %d\n",
580                 cfg->xpos, cfg->ypos);
581 }
582
583 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
584                         struct v4l2_rect *c)
585 {
586         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
587
588         if ((c->width == 0) ||
589           ((c->width + c->left) > vpbe_dev->current_timings.xres))
590                 c->width = vpbe_dev->current_timings.xres - c->left;
591
592         if ((c->height == 0) || ((c->height + c->top) >
593           vpbe_dev->current_timings.yres))
594                 c->height = vpbe_dev->current_timings.yres - c->top;
595
596         /* window height must be even for interlaced display */
597         if (vpbe_dev->current_timings.interlaced)
598                 c->height &= (~0x01);
599
600 }
601
602 /**
603  * vpbe_try_format()
604  * If user application provides width and height, and have bytesperline set
605  * to zero, driver calculates bytesperline and sizeimage based on hardware
606  * limits.
607  */
608 static int vpbe_try_format(struct vpbe_display *disp_dev,
609                         struct v4l2_pix_format *pixfmt, int check)
610 {
611         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
612         int min_height = 1;
613         int min_width = 32;
614         int max_height;
615         int max_width;
616         int bpp;
617
618         if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
619             (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
620                 /* choose default as V4L2_PIX_FMT_UYVY */
621                 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
622
623         /* Check the field format */
624         if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
625                 (pixfmt->field != V4L2_FIELD_NONE)) {
626                 if (vpbe_dev->current_timings.interlaced)
627                         pixfmt->field = V4L2_FIELD_INTERLACED;
628                 else
629                         pixfmt->field = V4L2_FIELD_NONE;
630         }
631
632         if (pixfmt->field == V4L2_FIELD_INTERLACED)
633                 min_height = 2;
634
635         if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
636                 bpp = 1;
637         else
638                 bpp = 2;
639
640         max_width = vpbe_dev->current_timings.xres;
641         max_height = vpbe_dev->current_timings.yres;
642
643         min_width /= bpp;
644
645         if (!pixfmt->width || (pixfmt->width < min_width) ||
646                 (pixfmt->width > max_width)) {
647                 pixfmt->width = vpbe_dev->current_timings.xres;
648         }
649
650         if (!pixfmt->height || (pixfmt->height  < min_height) ||
651                 (pixfmt->height  > max_height)) {
652                 pixfmt->height = vpbe_dev->current_timings.yres;
653         }
654
655         if (pixfmt->bytesperline < (pixfmt->width * bpp))
656                 pixfmt->bytesperline = pixfmt->width * bpp;
657
658         /* Make the bytesperline 32 byte aligned */
659         pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
660
661         if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
662                 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
663                                 (pixfmt->bytesperline * pixfmt->height >> 1);
664         else
665                 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
666
667         return 0;
668 }
669
670 static int vpbe_display_g_priority(struct file *file, void *priv,
671                                 enum v4l2_priority *p)
672 {
673         struct vpbe_fh *fh = file->private_data;
674         struct vpbe_layer *layer = fh->layer;
675
676         *p = v4l2_prio_max(&layer->prio);
677
678         return 0;
679 }
680
681 static int vpbe_display_s_priority(struct file *file, void *priv,
682                                 enum v4l2_priority p)
683 {
684         struct vpbe_fh *fh = file->private_data;
685         struct vpbe_layer *layer = fh->layer;
686         int ret;
687
688         ret = v4l2_prio_change(&layer->prio, &fh->prio, p);
689
690         return ret;
691 }
692
693 static int vpbe_display_querycap(struct file *file, void  *priv,
694                                struct v4l2_capability *cap)
695 {
696         struct vpbe_fh *fh = file->private_data;
697         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
698
699         cap->version = VPBE_DISPLAY_VERSION_CODE;
700         cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
701         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
702         snprintf(cap->driver, sizeof(cap->driver), "%s",
703                 dev_name(vpbe_dev->pdev));
704         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
705                  dev_name(vpbe_dev->pdev));
706         strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
707
708         return 0;
709 }
710
711 static int vpbe_display_s_crop(struct file *file, void *priv,
712                              const struct v4l2_crop *crop)
713 {
714         struct vpbe_fh *fh = file->private_data;
715         struct vpbe_layer *layer = fh->layer;
716         struct vpbe_display *disp_dev = fh->disp_dev;
717         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
718         struct osd_layer_config *cfg = &layer->layer_info.config;
719         struct osd_state *osd_device = disp_dev->osd_device;
720         struct v4l2_rect rect = crop->c;
721         int ret;
722
723         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
724                 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
725
726         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
727                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
728                 return -EINVAL;
729         }
730
731         if (rect.top < 0)
732                 rect.top = 0;
733         if (rect.left < 0)
734                 rect.left = 0;
735
736         vpbe_disp_check_window_params(disp_dev, &rect);
737
738         osd_device->ops.get_layer_config(osd_device,
739                         layer->layer_info.id, cfg);
740
741         vpbe_disp_calculate_scale_factor(disp_dev, layer,
742                                         rect.width,
743                                         rect.height);
744         vpbe_disp_adj_position(disp_dev, layer, rect.top,
745                                         rect.left);
746         ret = osd_device->ops.set_layer_config(osd_device,
747                                 layer->layer_info.id, cfg);
748         if (ret < 0) {
749                 v4l2_err(&vpbe_dev->v4l2_dev,
750                         "Error in set layer config:\n");
751                 return -EINVAL;
752         }
753
754         /* apply zooming and h or v expansion */
755         osd_device->ops.set_zoom(osd_device,
756                         layer->layer_info.id,
757                         layer->layer_info.h_zoom,
758                         layer->layer_info.v_zoom);
759         ret = osd_device->ops.set_vid_expansion(osd_device,
760                         layer->layer_info.h_exp,
761                         layer->layer_info.v_exp);
762         if (ret < 0) {
763                 v4l2_err(&vpbe_dev->v4l2_dev,
764                 "Error in set vid expansion:\n");
765                 return -EINVAL;
766         }
767
768         if ((layer->layer_info.h_zoom != ZOOM_X1) ||
769                 (layer->layer_info.v_zoom != ZOOM_X1) ||
770                 (layer->layer_info.h_exp != H_EXP_OFF) ||
771                 (layer->layer_info.v_exp != V_EXP_OFF))
772                 /* Enable expansion filter */
773                 osd_device->ops.set_interpolation_filter(osd_device, 1);
774         else
775                 osd_device->ops.set_interpolation_filter(osd_device, 0);
776
777         return 0;
778 }
779
780 static int vpbe_display_g_crop(struct file *file, void *priv,
781                              struct v4l2_crop *crop)
782 {
783         struct vpbe_fh *fh = file->private_data;
784         struct vpbe_layer *layer = fh->layer;
785         struct osd_layer_config *cfg = &layer->layer_info.config;
786         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
787         struct osd_state *osd_device = fh->disp_dev->osd_device;
788         struct v4l2_rect *rect = &crop->c;
789
790         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
791                         "VIDIOC_G_CROP, layer id = %d\n",
792                         layer->device_id);
793
794         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
795                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
796                 return -EINVAL;
797         }
798         osd_device->ops.get_layer_config(osd_device,
799                                 layer->layer_info.id, cfg);
800         rect->top = cfg->ypos;
801         rect->left = cfg->xpos;
802         rect->width = cfg->xsize;
803         rect->height = cfg->ysize;
804
805         return 0;
806 }
807
808 static int vpbe_display_cropcap(struct file *file, void *priv,
809                               struct v4l2_cropcap *cropcap)
810 {
811         struct vpbe_fh *fh = file->private_data;
812         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
813
814         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
815
816         cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
817         cropcap->bounds.left = 0;
818         cropcap->bounds.top = 0;
819         cropcap->bounds.width = vpbe_dev->current_timings.xres;
820         cropcap->bounds.height = vpbe_dev->current_timings.yres;
821         cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
822         cropcap->defrect = cropcap->bounds;
823         return 0;
824 }
825
826 static int vpbe_display_g_fmt(struct file *file, void *priv,
827                                 struct v4l2_format *fmt)
828 {
829         struct vpbe_fh *fh = file->private_data;
830         struct vpbe_layer *layer = fh->layer;
831         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
832
833         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
834                         "VIDIOC_G_FMT, layer id = %d\n",
835                         layer->device_id);
836
837         /* If buffer type is video output */
838         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
839                 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
840                 return -EINVAL;
841         }
842         /* Fill in the information about format */
843         fmt->fmt.pix = layer->pix_fmt;
844
845         return 0;
846 }
847
848 static int vpbe_display_enum_fmt(struct file *file, void  *priv,
849                                    struct v4l2_fmtdesc *fmt)
850 {
851         struct vpbe_fh *fh = file->private_data;
852         struct vpbe_layer *layer = fh->layer;
853         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
854         unsigned int index = 0;
855
856         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
857                                 "VIDIOC_ENUM_FMT, layer id = %d\n",
858                                 layer->device_id);
859         if (fmt->index > 1) {
860                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
861                 return -EINVAL;
862         }
863
864         /* Fill in the information about format */
865         index = fmt->index;
866         memset(fmt, 0, sizeof(*fmt));
867         fmt->index = index;
868         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
869         if (index == 0) {
870                 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
871                 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
872         } else {
873                 strcpy(fmt->description, "Y/CbCr 4:2:0");
874                 fmt->pixelformat = V4L2_PIX_FMT_NV12;
875         }
876
877         return 0;
878 }
879
880 static int vpbe_display_s_fmt(struct file *file, void *priv,
881                                 struct v4l2_format *fmt)
882 {
883         struct vpbe_fh *fh = file->private_data;
884         struct vpbe_layer *layer = fh->layer;
885         struct vpbe_display *disp_dev = fh->disp_dev;
886         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
887         struct osd_layer_config *cfg  = &layer->layer_info.config;
888         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
889         struct osd_state *osd_device = disp_dev->osd_device;
890         int ret;
891
892         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
893                         "VIDIOC_S_FMT, layer id = %d\n",
894                         layer->device_id);
895
896         /* If streaming is started, return error */
897         if (layer->started) {
898                 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
899                 return -EBUSY;
900         }
901         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
902                 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
903                 return -EINVAL;
904         }
905         /* Check for valid pixel format */
906         ret = vpbe_try_format(disp_dev, pixfmt, 1);
907         if (ret)
908                 return ret;
909
910         /* YUV420 is requested, check availability of the
911         other video window */
912
913         layer->pix_fmt = *pixfmt;
914         if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
915                 struct vpbe_layer *otherlayer;
916
917                 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
918                 /* if other layer is available, only
919                  * claim it, do not configure it
920                  */
921                 ret = osd_device->ops.request_layer(osd_device,
922                                                     otherlayer->layer_info.id);
923                 if (ret < 0) {
924                         v4l2_err(&vpbe_dev->v4l2_dev,
925                                  "Display Manager failed to allocate layer\n");
926                         return -EBUSY;
927                 }
928         }
929
930         /* Get osd layer config */
931         osd_device->ops.get_layer_config(osd_device,
932                         layer->layer_info.id, cfg);
933         /* Store the pixel format in the layer object */
934         cfg->xsize = pixfmt->width;
935         cfg->ysize = pixfmt->height;
936         cfg->line_length = pixfmt->bytesperline;
937         cfg->ypos = 0;
938         cfg->xpos = 0;
939         cfg->interlaced = vpbe_dev->current_timings.interlaced;
940
941         if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
942                 cfg->pixfmt = PIXFMT_YCBCRI;
943
944         /* Change of the default pixel format for both video windows */
945         if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
946                 struct vpbe_layer *otherlayer;
947                 cfg->pixfmt = PIXFMT_NV12;
948                 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
949                                                                 layer);
950                 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
951         }
952
953         /* Set the layer config in the osd window */
954         ret = osd_device->ops.set_layer_config(osd_device,
955                                 layer->layer_info.id, cfg);
956         if (ret < 0) {
957                 v4l2_err(&vpbe_dev->v4l2_dev,
958                                 "Error in S_FMT params:\n");
959                 return -EINVAL;
960         }
961
962         /* Readback and fill the local copy of current pix format */
963         osd_device->ops.get_layer_config(osd_device,
964                         layer->layer_info.id, cfg);
965
966         return 0;
967 }
968
969 static int vpbe_display_try_fmt(struct file *file, void *priv,
970                                   struct v4l2_format *fmt)
971 {
972         struct vpbe_fh *fh = file->private_data;
973         struct vpbe_display *disp_dev = fh->disp_dev;
974         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
975         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
976
977         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
978
979         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
980                 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
981                 return -EINVAL;
982         }
983
984         /* Check for valid field format */
985         return  vpbe_try_format(disp_dev, pixfmt, 0);
986
987 }
988
989 /**
990  * vpbe_display_s_std - Set the given standard in the encoder
991  *
992  * Sets the standard if supported by the current encoder. Return the status.
993  * 0 - success & -EINVAL on error
994  */
995 static int vpbe_display_s_std(struct file *file, void *priv,
996                                 v4l2_std_id std_id)
997 {
998         struct vpbe_fh *fh = priv;
999         struct vpbe_layer *layer = fh->layer;
1000         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1001         int ret;
1002
1003         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
1004
1005         /* If streaming is started, return error */
1006         if (layer->started) {
1007                 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1008                 return -EBUSY;
1009         }
1010         if (NULL != vpbe_dev->ops.s_std) {
1011                 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
1012                 if (ret) {
1013                         v4l2_err(&vpbe_dev->v4l2_dev,
1014                         "Failed to set standard for sub devices\n");
1015                         return -EINVAL;
1016                 }
1017         } else {
1018                 return -EINVAL;
1019         }
1020
1021         return 0;
1022 }
1023
1024 /**
1025  * vpbe_display_g_std - Get the standard in the current encoder
1026  *
1027  * Get the standard in the current encoder. Return the status. 0 - success
1028  * -EINVAL on error
1029  */
1030 static int vpbe_display_g_std(struct file *file, void *priv,
1031                                 v4l2_std_id *std_id)
1032 {
1033         struct vpbe_fh *fh = priv;
1034         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1035
1036         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
1037
1038         /* Get the standard from the current encoder */
1039         if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
1040                 *std_id = vpbe_dev->current_timings.std_id;
1041                 return 0;
1042         }
1043
1044         return -EINVAL;
1045 }
1046
1047 /**
1048  * vpbe_display_enum_output - enumerate outputs
1049  *
1050  * Enumerates the outputs available at the vpbe display
1051  * returns the status, -EINVAL if end of output list
1052  */
1053 static int vpbe_display_enum_output(struct file *file, void *priv,
1054                                     struct v4l2_output *output)
1055 {
1056         struct vpbe_fh *fh = priv;
1057         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1058         int ret;
1059
1060         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1061
1062         /* Enumerate outputs */
1063
1064         if (NULL == vpbe_dev->ops.enum_outputs)
1065                 return -EINVAL;
1066
1067         ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1068         if (ret) {
1069                 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1070                         "Failed to enumerate outputs\n");
1071                 return -EINVAL;
1072         }
1073
1074         return 0;
1075 }
1076
1077 /**
1078  * vpbe_display_s_output - Set output to
1079  * the output specified by the index
1080  */
1081 static int vpbe_display_s_output(struct file *file, void *priv,
1082                                 unsigned int i)
1083 {
1084         struct vpbe_fh *fh = priv;
1085         struct vpbe_layer *layer = fh->layer;
1086         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1087         int ret;
1088
1089         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1090         /* If streaming is started, return error */
1091         if (layer->started) {
1092                 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1093                 return -EBUSY;
1094         }
1095         if (NULL == vpbe_dev->ops.set_output)
1096                 return -EINVAL;
1097
1098         ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1099         if (ret) {
1100                 v4l2_err(&vpbe_dev->v4l2_dev,
1101                         "Failed to set output for sub devices\n");
1102                 return -EINVAL;
1103         }
1104
1105         return 0;
1106 }
1107
1108 /**
1109  * vpbe_display_g_output - Get output from subdevice
1110  * for a given by the index
1111  */
1112 static int vpbe_display_g_output(struct file *file, void *priv,
1113                                 unsigned int *i)
1114 {
1115         struct vpbe_fh *fh = priv;
1116         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1117
1118         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1119         /* Get the standard from the current encoder */
1120         *i = vpbe_dev->current_out_index;
1121
1122         return 0;
1123 }
1124
1125 /**
1126  * vpbe_display_enum_dv_timings - Enumerate the dv timings
1127  *
1128  * enum the timings in the current encoder. Return the status. 0 - success
1129  * -EINVAL on error
1130  */
1131 static int
1132 vpbe_display_enum_dv_timings(struct file *file, void *priv,
1133                         struct v4l2_enum_dv_timings *timings)
1134 {
1135         struct vpbe_fh *fh = priv;
1136         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1137         int ret;
1138
1139         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1140
1141         /* Enumerate outputs */
1142         if (NULL == vpbe_dev->ops.enum_dv_timings)
1143                 return -EINVAL;
1144
1145         ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1146         if (ret) {
1147                 v4l2_err(&vpbe_dev->v4l2_dev,
1148                         "Failed to enumerate dv timings info\n");
1149                 return -EINVAL;
1150         }
1151
1152         return 0;
1153 }
1154
1155 /**
1156  * vpbe_display_s_dv_timings - Set the dv timings
1157  *
1158  * Set the timings in the current encoder. Return the status. 0 - success
1159  * -EINVAL on error
1160  */
1161 static int
1162 vpbe_display_s_dv_timings(struct file *file, void *priv,
1163                                 struct v4l2_dv_timings *timings)
1164 {
1165         struct vpbe_fh *fh = priv;
1166         struct vpbe_layer *layer = fh->layer;
1167         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1168         int ret;
1169
1170         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1171
1172
1173         /* If streaming is started, return error */
1174         if (layer->started) {
1175                 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1176                 return -EBUSY;
1177         }
1178
1179         /* Set the given standard in the encoder */
1180         if (!vpbe_dev->ops.s_dv_timings)
1181                 return -EINVAL;
1182
1183         ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1184         if (ret) {
1185                 v4l2_err(&vpbe_dev->v4l2_dev,
1186                         "Failed to set the dv timings info\n");
1187                 return -EINVAL;
1188         }
1189
1190         return 0;
1191 }
1192
1193 /**
1194  * vpbe_display_g_dv_timings - Set the dv timings
1195  *
1196  * Get the timings in the current encoder. Return the status. 0 - success
1197  * -EINVAL on error
1198  */
1199 static int
1200 vpbe_display_g_dv_timings(struct file *file, void *priv,
1201                                 struct v4l2_dv_timings *dv_timings)
1202 {
1203         struct vpbe_fh *fh = priv;
1204         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1205
1206         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1207
1208         /* Get the given standard in the encoder */
1209
1210         if (vpbe_dev->current_timings.timings_type &
1211                                 VPBE_ENC_DV_TIMINGS) {
1212                 *dv_timings = vpbe_dev->current_timings.dv_timings;
1213         } else {
1214                 return -EINVAL;
1215         }
1216
1217         return 0;
1218 }
1219
1220 static int vpbe_display_streamoff(struct file *file, void *priv,
1221                                 enum v4l2_buf_type buf_type)
1222 {
1223         struct vpbe_fh *fh = file->private_data;
1224         struct vpbe_layer *layer = fh->layer;
1225         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1226         struct osd_state *osd_device = fh->disp_dev->osd_device;
1227         int ret;
1228
1229         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1230                         "VIDIOC_STREAMOFF,layer id = %d\n",
1231                         layer->device_id);
1232
1233         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1234                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1235                 return -EINVAL;
1236         }
1237
1238         /* If io is allowed for this file handle, return error */
1239         if (!fh->io_allowed) {
1240                 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1241                 return -EACCES;
1242         }
1243
1244         /* If streaming is not started, return error */
1245         if (!layer->started) {
1246                 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1247                         " id = %d\n", layer->device_id);
1248                 return -EINVAL;
1249         }
1250
1251         osd_device->ops.disable_layer(osd_device,
1252                         layer->layer_info.id);
1253         layer->started = 0;
1254         ret = vb2_streamoff(&layer->buffer_queue, buf_type);
1255
1256         return ret;
1257 }
1258
1259 static int vpbe_display_streamon(struct file *file, void *priv,
1260                          enum v4l2_buf_type buf_type)
1261 {
1262         struct vpbe_fh *fh = file->private_data;
1263         struct vpbe_layer *layer = fh->layer;
1264         struct vpbe_display *disp_dev = fh->disp_dev;
1265         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1266         struct osd_state *osd_device = disp_dev->osd_device;
1267         int ret;
1268
1269         osd_device->ops.disable_layer(osd_device,
1270                         layer->layer_info.id);
1271
1272         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1273                                                 layer->device_id);
1274
1275         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1276                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1277                 return -EINVAL;
1278         }
1279
1280         /* If file handle is not allowed IO, return error */
1281         if (!fh->io_allowed) {
1282                 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1283                 return -EACCES;
1284         }
1285         /* If Streaming is already started, return error */
1286         if (layer->started) {
1287                 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1288                 return -EBUSY;
1289         }
1290
1291         /*
1292          * Call vb2_streamon to start streaming
1293          * in videobuf
1294          */
1295         ret = vb2_streamon(&layer->buffer_queue, buf_type);
1296         if (ret) {
1297                 v4l2_err(&vpbe_dev->v4l2_dev,
1298                 "error in vb2_streamon\n");
1299                 return ret;
1300         }
1301         return ret;
1302 }
1303
1304 static int vpbe_display_dqbuf(struct file *file, void *priv,
1305                       struct v4l2_buffer *buf)
1306 {
1307         struct vpbe_fh *fh = file->private_data;
1308         struct vpbe_layer *layer = fh->layer;
1309         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1310         int ret;
1311
1312         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1313                 "VIDIOC_DQBUF, layer id = %d\n",
1314                 layer->device_id);
1315
1316         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1317                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1318                 return -EINVAL;
1319         }
1320         /* If this file handle is not allowed to do IO, return error */
1321         if (!fh->io_allowed) {
1322                 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1323                 return -EACCES;
1324         }
1325         if (file->f_flags & O_NONBLOCK)
1326                 /* Call videobuf_dqbuf for non blocking mode */
1327                 ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
1328         else
1329                 /* Call videobuf_dqbuf for blocking mode */
1330                 ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
1331
1332         return ret;
1333 }
1334
1335 static int vpbe_display_qbuf(struct file *file, void *priv,
1336                      struct v4l2_buffer *p)
1337 {
1338         struct vpbe_fh *fh = file->private_data;
1339         struct vpbe_layer *layer = fh->layer;
1340         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1341
1342         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1343                 "VIDIOC_QBUF, layer id = %d\n",
1344                 layer->device_id);
1345
1346         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1347                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1348                 return -EINVAL;
1349         }
1350
1351         /* If this file handle is not allowed to do IO, return error */
1352         if (!fh->io_allowed) {
1353                 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1354                 return -EACCES;
1355         }
1356
1357         return vb2_qbuf(&layer->buffer_queue, p);
1358 }
1359
1360 static int vpbe_display_querybuf(struct file *file, void *priv,
1361                          struct v4l2_buffer *buf)
1362 {
1363         struct vpbe_fh *fh = file->private_data;
1364         struct vpbe_layer *layer = fh->layer;
1365         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1366
1367         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1368                 "VIDIOC_QUERYBUF, layer id = %d\n",
1369                 layer->device_id);
1370
1371         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1372                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1373                 return -EINVAL;
1374         }
1375         /* Call vb2_querybuf to get information */
1376         return vb2_querybuf(&layer->buffer_queue, buf);
1377 }
1378
1379 static int vpbe_display_reqbufs(struct file *file, void *priv,
1380                         struct v4l2_requestbuffers *req_buf)
1381 {
1382         struct vpbe_fh *fh = file->private_data;
1383         struct vpbe_layer *layer = fh->layer;
1384         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1385         struct vb2_queue *q;
1386         int ret;
1387         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1388
1389         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1390                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1391                 return -EINVAL;
1392         }
1393
1394         /* If io users of the layer is not zero, return error */
1395         if (0 != layer->io_usrs) {
1396                 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1397                 return -EBUSY;
1398         }
1399         /* Initialize videobuf queue as per the buffer type */
1400         layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
1401         if (IS_ERR(layer->alloc_ctx)) {
1402                 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
1403                 return PTR_ERR(layer->alloc_ctx);
1404         }
1405         q = &layer->buffer_queue;
1406         memset(q, 0, sizeof(*q));
1407         q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1408         q->io_modes = VB2_MMAP | VB2_USERPTR;
1409         q->drv_priv = fh;
1410         q->ops = &video_qops;
1411         q->mem_ops = &vb2_dma_contig_memops;
1412         q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1413         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1414
1415         ret = vb2_queue_init(q);
1416         if (ret) {
1417                 v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1418                 vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
1419                 return ret;
1420         }
1421         /* Set io allowed member of file handle to TRUE */
1422         fh->io_allowed = 1;
1423         /* Increment io usrs member of layer object to 1 */
1424         layer->io_usrs = 1;
1425         /* Store type of memory requested in layer object */
1426         layer->memory = req_buf->memory;
1427         /* Initialize buffer queue */
1428         INIT_LIST_HEAD(&layer->dma_queue);
1429         /* Allocate buffers */
1430         return vb2_reqbufs(q, req_buf);
1431 }
1432
1433 /*
1434  * vpbe_display_mmap()
1435  * It is used to map kernel space buffers into user spaces
1436  */
1437 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1438 {
1439         /* Get the layer object and file handle object */
1440         struct vpbe_fh *fh = filep->private_data;
1441         struct vpbe_layer *layer = fh->layer;
1442         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1443         int ret;
1444
1445         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1446
1447         if (mutex_lock_interruptible(&layer->opslock))
1448                 return -ERESTARTSYS;
1449         ret = vb2_mmap(&layer->buffer_queue, vma);
1450         mutex_unlock(&layer->opslock);
1451         return ret;
1452 }
1453
1454 /* vpbe_display_poll(): It is used for select/poll system call
1455  */
1456 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1457 {
1458         struct vpbe_fh *fh = filep->private_data;
1459         struct vpbe_layer *layer = fh->layer;
1460         struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1461         unsigned int err = 0;
1462
1463         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1464         if (layer->started) {
1465                 mutex_lock(&layer->opslock);
1466                 err = vb2_poll(&layer->buffer_queue, filep, wait);
1467                 mutex_unlock(&layer->opslock);
1468         }
1469         return err;
1470 }
1471
1472 /*
1473  * vpbe_display_open()
1474  * It creates object of file handle structure and stores it in private_data
1475  * member of filepointer
1476  */
1477 static int vpbe_display_open(struct file *file)
1478 {
1479         struct vpbe_fh *fh = NULL;
1480         struct vpbe_layer *layer = video_drvdata(file);
1481         struct vpbe_display *disp_dev = layer->disp_dev;
1482         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1483         struct osd_state *osd_device = disp_dev->osd_device;
1484         int err;
1485
1486         /* Allocate memory for the file handle object */
1487         fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1488         if (fh == NULL) {
1489                 v4l2_err(&vpbe_dev->v4l2_dev,
1490                         "unable to allocate memory for file handle object\n");
1491                 return -ENOMEM;
1492         }
1493         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1494                         "vpbe display open plane = %d\n",
1495                         layer->device_id);
1496
1497         /* store pointer to fh in private_data member of filep */
1498         file->private_data = fh;
1499         fh->layer = layer;
1500         fh->disp_dev = disp_dev;
1501
1502         if (!layer->usrs) {
1503                 if (mutex_lock_interruptible(&layer->opslock))
1504                         return -ERESTARTSYS;
1505                 /* First claim the layer for this device */
1506                 err = osd_device->ops.request_layer(osd_device,
1507                                                 layer->layer_info.id);
1508                 mutex_unlock(&layer->opslock);
1509                 if (err < 0) {
1510                         /* Couldn't get layer */
1511                         v4l2_err(&vpbe_dev->v4l2_dev,
1512                                 "Display Manager failed to allocate layer\n");
1513                         kfree(fh);
1514                         return -EINVAL;
1515                 }
1516         }
1517         /* Increment layer usrs counter */
1518         layer->usrs++;
1519         /* Set io_allowed member to false */
1520         fh->io_allowed = 0;
1521         /* Initialize priority of this instance to default priority */
1522         fh->prio = V4L2_PRIORITY_UNSET;
1523         v4l2_prio_open(&layer->prio, &fh->prio);
1524         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1525                         "vpbe display device opened successfully\n");
1526         return 0;
1527 }
1528
1529 /*
1530  * vpbe_display_release()
1531  * This function deletes buffer queue, frees the buffers and the davinci
1532  * display file * handle
1533  */
1534 static int vpbe_display_release(struct file *file)
1535 {
1536         /* Get the layer object and file handle object */
1537         struct vpbe_fh *fh = file->private_data;
1538         struct vpbe_layer *layer = fh->layer;
1539         struct osd_layer_config *cfg  = &layer->layer_info.config;
1540         struct vpbe_display *disp_dev = fh->disp_dev;
1541         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1542         struct osd_state *osd_device = disp_dev->osd_device;
1543
1544         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1545
1546         mutex_lock(&layer->opslock);
1547         /* if this instance is doing IO */
1548         if (fh->io_allowed) {
1549                 /* Reset io_usrs member of layer object */
1550                 layer->io_usrs = 0;
1551
1552                 osd_device->ops.disable_layer(osd_device,
1553                                 layer->layer_info.id);
1554                 layer->started = 0;
1555                 /* Free buffers allocated */
1556                 vb2_queue_release(&layer->buffer_queue);
1557                 vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
1558         }
1559
1560         /* Decrement layer usrs counter */
1561         layer->usrs--;
1562         /* If this file handle has initialize encoder device, reset it */
1563         if (!layer->usrs) {
1564                 if (cfg->pixfmt == PIXFMT_NV12) {
1565                         struct vpbe_layer *otherlayer;
1566                         otherlayer =
1567                         _vpbe_display_get_other_win_layer(disp_dev, layer);
1568                         osd_device->ops.disable_layer(osd_device,
1569                                         otherlayer->layer_info.id);
1570                         osd_device->ops.release_layer(osd_device,
1571                                         otherlayer->layer_info.id);
1572                 }
1573                 osd_device->ops.disable_layer(osd_device,
1574                                 layer->layer_info.id);
1575                 osd_device->ops.release_layer(osd_device,
1576                                 layer->layer_info.id);
1577         }
1578         /* Close the priority */
1579         v4l2_prio_close(&layer->prio, fh->prio);
1580         file->private_data = NULL;
1581         mutex_unlock(&layer->opslock);
1582
1583         /* Free memory allocated to file handle object */
1584         kfree(fh);
1585
1586         disp_dev->cbcr_ofst = 0;
1587
1588         return 0;
1589 }
1590
1591 /* vpbe capture ioctl operations */
1592 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1593         .vidioc_querycap         = vpbe_display_querycap,
1594         .vidioc_g_fmt_vid_out    = vpbe_display_g_fmt,
1595         .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1596         .vidioc_s_fmt_vid_out    = vpbe_display_s_fmt,
1597         .vidioc_try_fmt_vid_out  = vpbe_display_try_fmt,
1598         .vidioc_reqbufs          = vpbe_display_reqbufs,
1599         .vidioc_querybuf         = vpbe_display_querybuf,
1600         .vidioc_qbuf             = vpbe_display_qbuf,
1601         .vidioc_dqbuf            = vpbe_display_dqbuf,
1602         .vidioc_streamon         = vpbe_display_streamon,
1603         .vidioc_streamoff        = vpbe_display_streamoff,
1604         .vidioc_cropcap          = vpbe_display_cropcap,
1605         .vidioc_g_crop           = vpbe_display_g_crop,
1606         .vidioc_s_crop           = vpbe_display_s_crop,
1607         .vidioc_g_priority       = vpbe_display_g_priority,
1608         .vidioc_s_priority       = vpbe_display_s_priority,
1609         .vidioc_s_std            = vpbe_display_s_std,
1610         .vidioc_g_std            = vpbe_display_g_std,
1611         .vidioc_enum_output      = vpbe_display_enum_output,
1612         .vidioc_s_output         = vpbe_display_s_output,
1613         .vidioc_g_output         = vpbe_display_g_output,
1614         .vidioc_s_dv_timings     = vpbe_display_s_dv_timings,
1615         .vidioc_g_dv_timings     = vpbe_display_g_dv_timings,
1616         .vidioc_enum_dv_timings  = vpbe_display_enum_dv_timings,
1617 };
1618
1619 static struct v4l2_file_operations vpbe_fops = {
1620         .owner = THIS_MODULE,
1621         .open = vpbe_display_open,
1622         .release = vpbe_display_release,
1623         .unlocked_ioctl = video_ioctl2,
1624         .mmap = vpbe_display_mmap,
1625         .poll = vpbe_display_poll
1626 };
1627
1628 static int vpbe_device_get(struct device *dev, void *data)
1629 {
1630         struct platform_device *pdev = to_platform_device(dev);
1631         struct vpbe_display *vpbe_disp  = data;
1632
1633         if (strcmp("vpbe_controller", pdev->name) == 0)
1634                 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1635
1636         if (strstr(pdev->name, "vpbe-osd") != NULL)
1637                 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1638
1639         return 0;
1640 }
1641
1642 static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1643                            struct platform_device *pdev)
1644 {
1645         struct vpbe_layer *vpbe_display_layer = NULL;
1646         struct video_device *vbd = NULL;
1647
1648         /* Allocate memory for four plane display objects */
1649
1650         disp_dev->dev[i] =
1651                 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1652
1653         /* If memory allocation fails, return error */
1654         if (!disp_dev->dev[i]) {
1655                 printk(KERN_ERR "ran out of memory\n");
1656                 return  -ENOMEM;
1657         }
1658         spin_lock_init(&disp_dev->dev[i]->irqlock);
1659         mutex_init(&disp_dev->dev[i]->opslock);
1660
1661         /* Get the pointer to the layer object */
1662         vpbe_display_layer = disp_dev->dev[i];
1663         vbd = &vpbe_display_layer->video_dev;
1664         /* Initialize field of video device */
1665         vbd->release    = video_device_release_empty;
1666         vbd->fops       = &vpbe_fops;
1667         vbd->ioctl_ops  = &vpbe_ioctl_ops;
1668         vbd->minor      = -1;
1669         vbd->v4l2_dev   = &disp_dev->vpbe_dev->v4l2_dev;
1670         vbd->lock       = &vpbe_display_layer->opslock;
1671         vbd->vfl_dir    = VFL_DIR_TX;
1672
1673         if (disp_dev->vpbe_dev->current_timings.timings_type &
1674                         VPBE_ENC_STD)
1675                 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1676
1677         snprintf(vbd->name, sizeof(vbd->name),
1678                         "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1679                         (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1680                         (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1681                         (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1682
1683         vpbe_display_layer->device_id = i;
1684
1685         vpbe_display_layer->layer_info.id =
1686                 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1687
1688         /* Initialize prio member of layer object */
1689         v4l2_prio_init(&vpbe_display_layer->prio);
1690
1691         return 0;
1692 }
1693
1694 static int register_device(struct vpbe_layer *vpbe_display_layer,
1695                            struct vpbe_display *disp_dev,
1696                            struct platform_device *pdev)
1697 {
1698         int err;
1699
1700         v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1701                   "Trying to register VPBE display device.\n");
1702         v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1703                   "layer=%x,layer->video_dev=%x\n",
1704                   (int)vpbe_display_layer,
1705                   (int)&vpbe_display_layer->video_dev);
1706
1707         err = video_register_device(&vpbe_display_layer->video_dev,
1708                                     VFL_TYPE_GRABBER,
1709                                     -1);
1710         if (err)
1711                 return -ENODEV;
1712
1713         vpbe_display_layer->disp_dev = disp_dev;
1714         /* set the driver data in platform device */
1715         platform_set_drvdata(pdev, disp_dev);
1716         video_set_drvdata(&vpbe_display_layer->video_dev,
1717                           vpbe_display_layer);
1718
1719         return 0;
1720 }
1721
1722
1723
1724 /*
1725  * vpbe_display_probe()
1726  * This function creates device entries by register itself to the V4L2 driver
1727  * and initializes fields of each layer objects
1728  */
1729 static int vpbe_display_probe(struct platform_device *pdev)
1730 {
1731         struct vpbe_layer *vpbe_display_layer;
1732         struct vpbe_display *disp_dev;
1733         struct resource *res = NULL;
1734         int k;
1735         int i;
1736         int err;
1737         int irq;
1738
1739         printk(KERN_DEBUG "vpbe_display_probe\n");
1740         /* Allocate memory for vpbe_display */
1741         disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1742                                 GFP_KERNEL);
1743         if (!disp_dev)
1744                 return -ENOMEM;
1745
1746         spin_lock_init(&disp_dev->dma_queue_lock);
1747         /*
1748          * Scan all the platform devices to find the vpbe
1749          * controller device and get the vpbe_dev object
1750          */
1751         err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1752                         vpbe_device_get);
1753         if (err < 0)
1754                 return err;
1755         /* Initialize the vpbe display controller */
1756         if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1757                 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1758                                                          disp_dev->vpbe_dev);
1759                 if (err) {
1760                         v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1761                                         "Error initing vpbe\n");
1762                         err = -ENOMEM;
1763                         goto probe_out;
1764                 }
1765         }
1766
1767         for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1768                 if (init_vpbe_layer(i, disp_dev, pdev)) {
1769                         err = -ENODEV;
1770                         goto probe_out;
1771                 }
1772         }
1773
1774         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1775         if (!res) {
1776                 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1777                          "Unable to get VENC interrupt resource\n");
1778                 err = -ENODEV;
1779                 goto probe_out;
1780         }
1781
1782         irq = res->start;
1783         err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1784                                VPBE_DISPLAY_DRIVER, disp_dev);
1785         if (err) {
1786                 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1787                                 "Unable to request interrupt\n");
1788                 goto probe_out;
1789         }
1790
1791         for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1792                 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1793                         err = -ENODEV;
1794                         goto probe_out;
1795                 }
1796         }
1797
1798         printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
1799         return 0;
1800
1801 probe_out:
1802         for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1803                 /* Get the pointer to the layer object */
1804                 vpbe_display_layer = disp_dev->dev[k];
1805                 /* Unregister video device */
1806                 if (vpbe_display_layer) {
1807                         video_unregister_device(
1808                                 &vpbe_display_layer->video_dev);
1809                                 kfree(disp_dev->dev[k]);
1810                 }
1811         }
1812         return err;
1813 }
1814
1815 /*
1816  * vpbe_display_remove()
1817  * It un-register hardware layer from V4L2 driver
1818  */
1819 static int vpbe_display_remove(struct platform_device *pdev)
1820 {
1821         struct vpbe_layer *vpbe_display_layer;
1822         struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1823         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1824         int i;
1825
1826         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1827
1828         /* deinitialize the vpbe display controller */
1829         if (NULL != vpbe_dev->ops.deinitialize)
1830                 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1831         /* un-register device */
1832         for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1833                 /* Get the pointer to the layer object */
1834                 vpbe_display_layer = disp_dev->dev[i];
1835                 /* Unregister video device */
1836                 video_unregister_device(&vpbe_display_layer->video_dev);
1837
1838         }
1839         for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1840                 kfree(disp_dev->dev[i]);
1841                 disp_dev->dev[i] = NULL;
1842         }
1843
1844         return 0;
1845 }
1846
1847 static struct platform_driver vpbe_display_driver = {
1848         .driver = {
1849                 .name = VPBE_DISPLAY_DRIVER,
1850                 .owner = THIS_MODULE,
1851                 .bus = &platform_bus_type,
1852         },
1853         .probe = vpbe_display_probe,
1854         .remove = vpbe_display_remove,
1855 };
1856
1857 module_platform_driver(vpbe_display_driver);
1858
1859 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1860 MODULE_LICENSE("GPL");
1861 MODULE_AUTHOR("Texas Instruments");