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