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