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