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