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