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