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