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