Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5

Linux/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         if (layer->cur_frm == layer->next_frm)
 75                 return;
 76 
 77         layer->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
 78         vb2_buffer_done(&layer->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
 79         /* Make cur_frm pointing to next_frm */
 80         layer->cur_frm = layer->next_frm;
 81 }
 82 
 83 static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
 84                                 struct vpbe_layer *layer)
 85 {
 86         struct osd_state *osd_device = disp_obj->osd_device;
 87         unsigned long addr;
 88 
 89         spin_lock(&disp_obj->dma_queue_lock);
 90         if (list_empty(&layer->dma_queue) ||
 91                 (layer->cur_frm != layer->next_frm)) {
 92                 spin_unlock(&disp_obj->dma_queue_lock);
 93                 return;
 94         }
 95         /*
 96          * one field is displayed configure
 97          * the next frame if it is available
 98          * otherwise hold on current frame
 99          * Get next from the buffer queue
100          */
101         layer->next_frm = list_entry(layer->dma_queue.next,
102                           struct  vpbe_disp_buffer, list);
103         /* Remove that from the buffer queue */
104         list_del(&layer->next_frm->list);
105         spin_unlock(&disp_obj->dma_queue_lock);
106         /* Mark state of the frame to active */
107         layer->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
108         addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb.vb2_buf, 0);
109         osd_device->ops.start_layer(osd_device,
110                         layer->layer_info.id,
111                         addr,
112                         disp_obj->cbcr_ofst);
113 }
114 
115 /* interrupt service routine */
116 static irqreturn_t venc_isr(int irq, void *arg)
117 {
118         struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
119         struct vpbe_layer *layer;
120         static unsigned last_event;
121         unsigned event = 0;
122         int fid;
123         int i;
124 
125         if ((NULL == arg) || (NULL == disp_dev->dev[0]))
126                 return IRQ_HANDLED;
127 
128         if (venc_is_second_field(disp_dev))
129                 event |= VENC_SECOND_FIELD;
130         else
131                 event |= VENC_FIRST_FIELD;
132 
133         if (event == (last_event & ~VENC_END_OF_FRAME)) {
134                 /*
135                 * If the display is non-interlaced, then we need to flag the
136                 * end-of-frame event at every interrupt regardless of the
137                 * value of the FIDST bit.  We can conclude that the display is
138                 * non-interlaced if the value of the FIDST bit is unchanged
139                 * from the previous interrupt.
140                 */
141                 event |= VENC_END_OF_FRAME;
142         } else if (event == VENC_SECOND_FIELD) {
143                 /* end-of-frame for interlaced display */
144                 event |= VENC_END_OF_FRAME;
145         }
146         last_event = event;
147 
148         for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
149                 layer = disp_dev->dev[i];
150 
151                 if (!vb2_start_streaming_called(&layer->buffer_queue))
152                         continue;
153 
154                 if (layer->layer_first_int) {
155                         layer->layer_first_int = 0;
156                         continue;
157                 }
158                 /* Check the field format */
159                 if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
160                         (event & VENC_END_OF_FRAME)) {
161                         /* Progressive mode */
162 
163                         vpbe_isr_even_field(disp_dev, layer);
164                         vpbe_isr_odd_field(disp_dev, layer);
165                 } else {
166                 /* Interlaced mode */
167 
168                         layer->field_id ^= 1;
169                         if (event & VENC_FIRST_FIELD)
170                                 fid = 0;
171                         else
172                                 fid = 1;
173 
174                         /*
175                         * If field id does not match with store
176                         * field id
177                         */
178                         if (fid != layer->field_id) {
179                                 /* Make them in sync */
180                                 layer->field_id = fid;
181                                 continue;
182                         }
183                         /*
184                         * device field id and local field id are
185                         * in sync. If this is even field
186                         */
187                         if (0 == fid)
188                                 vpbe_isr_even_field(disp_dev, layer);
189                         else  /* odd field */
190                                 vpbe_isr_odd_field(disp_dev, layer);
191                 }
192         }
193 
194         return IRQ_HANDLED;
195 }
196 
197 /*
198  * vpbe_buffer_prepare()
199  * This is the callback function called from vb2_qbuf() function
200  * the buffer is prepared and user space virtual address is converted into
201  * physical address
202  */
203 static int vpbe_buffer_prepare(struct vb2_buffer *vb)
204 {
205         struct vb2_queue *q = vb->vb2_queue;
206         struct vpbe_layer *layer = vb2_get_drv_priv(q);
207         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
208         unsigned long addr;
209 
210         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
211                                 "vpbe_buffer_prepare\n");
212 
213         vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
214         if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
215                 return -EINVAL;
216 
217         addr = vb2_dma_contig_plane_dma_addr(vb, 0);
218         if (!IS_ALIGNED(addr, 8)) {
219                 v4l2_err(&vpbe_dev->v4l2_dev,
220                          "buffer_prepare:offset is not aligned to 32 bytes\n");
221                 return -EINVAL;
222         }
223         return 0;
224 }
225 
226 /*
227  * vpbe_buffer_setup()
228  * This function allocates memory for the buffers
229  */
230 static int
231 vpbe_buffer_queue_setup(struct vb2_queue *vq,
232                         unsigned int *nbuffers, unsigned int *nplanes,
233                         unsigned int sizes[], void *alloc_ctxs[])
234 
235 {
236         /* Get the file handle object and layer object */
237         struct vpbe_layer *layer = vb2_get_drv_priv(vq);
238         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
239 
240         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
241 
242         /* Store number of buffers allocated in numbuffer member */
243         if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
244                 *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
245         alloc_ctxs[0] = layer->alloc_ctx;
246 
247         if (*nplanes)
248                 return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0;
249 
250         *nplanes = 1;
251         sizes[0] = layer->pix_fmt.sizeimage;
252 
253         return 0;
254 }
255 
256 /*
257  * vpbe_buffer_queue()
258  * This function adds the buffer to DMA queue
259  */
260 static void vpbe_buffer_queue(struct vb2_buffer *vb)
261 {
262         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
263         /* Get the file handle object and layer object */
264         struct vpbe_disp_buffer *buf = container_of(vbuf,
265                                 struct vpbe_disp_buffer, vb);
266         struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
267         struct vpbe_display *disp = layer->disp_dev;
268         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
269         unsigned long flags;
270 
271         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
272                         "vpbe_buffer_queue\n");
273 
274         /* add the buffer to the DMA queue */
275         spin_lock_irqsave(&disp->dma_queue_lock, flags);
276         list_add_tail(&buf->list, &layer->dma_queue);
277         spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
278 }
279 
280 static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
281 {
282         struct vpbe_layer *layer = vb2_get_drv_priv(vq);
283         struct osd_state *osd_device = layer->disp_dev->osd_device;
284         int ret;
285 
286          osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
287 
288         /* Get the next frame from the buffer queue */
289         layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
290                                 struct vpbe_disp_buffer, list);
291         /* Remove buffer from the buffer queue */
292         list_del(&layer->cur_frm->list);
293         /* Mark state of the current frame to active */
294         layer->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
295         /* Initialize field_id and started member */
296         layer->field_id = 0;
297 
298         /* Set parameters in OSD and VENC */
299         ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
300         if (ret < 0) {
301                 struct vpbe_disp_buffer *buf, *tmp;
302 
303                 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
304                                 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,
308                                         VB2_BUF_STATE_QUEUED);
309                 }
310 
311                 return ret;
312         }
313 
314         /*
315          * if request format is yuv420 semiplanar, need to
316          * enable both video windows
317          */
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,
339                                 VB2_BUF_STATE_ERROR);
340         } else {
341                 if (layer->cur_frm != NULL)
342                         vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
343                                         VB2_BUF_STATE_ERROR);
344                 if (layer->next_frm != NULL)
345                         vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
346                                         VB2_BUF_STATE_ERROR);
347         }
348 
349         while (!list_empty(&layer->dma_queue)) {
350                 layer->next_frm = list_entry(layer->dma_queue.next,
351                                                 struct vpbe_disp_buffer, list);
352                 list_del(&layer->next_frm->list);
353                 vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
354                                 VB2_BUF_STATE_ERROR);
355         }
356         spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
357 }
358 
359 static struct vb2_ops video_qops = {
360         .queue_setup = vpbe_buffer_queue_setup,
361         .wait_prepare = vb2_ops_wait_prepare,
362         .wait_finish = vb2_ops_wait_finish,
363         .buf_prepare = vpbe_buffer_prepare,
364         .start_streaming = vpbe_start_streaming,
365         .stop_streaming = vpbe_stop_streaming,
366         .buf_queue = vpbe_buffer_queue,
367 };
368 
369 static
370 struct vpbe_layer*
371 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
372                         struct vpbe_layer *layer)
373 {
374         enum vpbe_display_device_id thiswin, otherwin;
375         thiswin = layer->device_id;
376 
377         otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
378         VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
379         return disp_dev->dev[otherwin];
380 }
381 
382 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
383                         struct vpbe_layer *layer)
384 {
385         struct osd_layer_config *cfg  = &layer->layer_info.config;
386         struct osd_state *osd_device = disp_dev->osd_device;
387         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
388         unsigned long addr;
389         int ret;
390 
391         addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb.vb2_buf, 0);
392         /* Set address in the display registers */
393         osd_device->ops.start_layer(osd_device,
394                                     layer->layer_info.id,
395                                     addr,
396                                     disp_dev->cbcr_ofst);
397 
398         ret = osd_device->ops.enable_layer(osd_device,
399                                 layer->layer_info.id, 0);
400         if (ret < 0) {
401                 v4l2_err(&vpbe_dev->v4l2_dev,
402                         "Error in enabling osd window layer 0\n");
403                 return -1;
404         }
405 
406         /* Enable the window */
407         layer->layer_info.enable = 1;
408         if (cfg->pixfmt == PIXFMT_NV12) {
409                 struct vpbe_layer *otherlayer =
410                         _vpbe_display_get_other_win_layer(disp_dev, layer);
411 
412                 ret = osd_device->ops.enable_layer(osd_device,
413                                 otherlayer->layer_info.id, 1);
414                 if (ret < 0) {
415                         v4l2_err(&vpbe_dev->v4l2_dev,
416                                 "Error in enabling osd window layer 1\n");
417                         return -1;
418                 }
419                 otherlayer->layer_info.enable = 1;
420         }
421         return 0;
422 }
423 
424 static void
425 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
426                         struct vpbe_layer *layer,
427                         int expected_xsize, int expected_ysize)
428 {
429         struct display_layer_info *layer_info = &layer->layer_info;
430         struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
431         struct osd_layer_config *cfg  = &layer->layer_info.config;
432         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
433         int calculated_xsize;
434         int h_exp = 0;
435         int v_exp = 0;
436         int h_scale;
437         int v_scale;
438 
439         v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
440 
441         /*
442          * Application initially set the image format. Current display
443          * size is obtained from the vpbe display controller. expected_xsize
444          * and expected_ysize are set through S_CROP ioctl. Based on this,
445          * driver will calculate the scale factors for vertical and
446          * horizontal direction so that the image is displayed scaled
447          * and expanded. Application uses expansion to display the image
448          * in a square pixel. Otherwise it is displayed using displays
449          * pixel aspect ratio.It is expected that application chooses
450          * the crop coordinates for cropped or scaled display. if crop
451          * size is less than the image size, it is displayed cropped or
452          * it is displayed scaled and/or expanded.
453          *
454          * to begin with, set the crop window same as expected. Later we
455          * will override with scaled window size
456          */
457 
458         cfg->xsize = pixfmt->width;
459         cfg->ysize = pixfmt->height;
460         layer_info->h_zoom = ZOOM_X1;   /* no horizontal zoom */
461         layer_info->v_zoom = ZOOM_X1;   /* no horizontal zoom */
462         layer_info->h_exp = H_EXP_OFF;  /* no horizontal zoom */
463         layer_info->v_exp = V_EXP_OFF;  /* no horizontal zoom */
464 
465         if (pixfmt->width < expected_xsize) {
466                 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
467                 if (h_scale < 2)
468                         h_scale = 1;
469                 else if (h_scale >= 4)
470                         h_scale = 4;
471                 else
472                         h_scale = 2;
473                 cfg->xsize *= h_scale;
474                 if (cfg->xsize < expected_xsize) {
475                         if ((standard_id & V4L2_STD_525_60) ||
476                         (standard_id & V4L2_STD_625_50)) {
477                                 calculated_xsize = (cfg->xsize *
478                                         VPBE_DISPLAY_H_EXP_RATIO_N) /
479                                         VPBE_DISPLAY_H_EXP_RATIO_D;
480                                 if (calculated_xsize <= expected_xsize) {
481                                         h_exp = 1;
482                                         cfg->xsize = calculated_xsize;
483                                 }
484                         }
485                 }
486                 if (h_scale == 2)
487                         layer_info->h_zoom = ZOOM_X2;
488                 else if (h_scale == 4)
489                         layer_info->h_zoom = ZOOM_X4;
490                 if (h_exp)
491                         layer_info->h_exp = H_EXP_9_OVER_8;
492         } else {
493                 /* no scaling, only cropping. Set display area to crop area */
494                 cfg->xsize = expected_xsize;
495         }
496 
497         if (pixfmt->height < expected_ysize) {
498                 v_scale = expected_ysize / pixfmt->height;
499                 if (v_scale < 2)
500                         v_scale = 1;
501                 else if (v_scale >= 4)
502                         v_scale = 4;
503                 else
504                         v_scale = 2;
505                 cfg->ysize *= v_scale;
506                 if (cfg->ysize < expected_ysize) {
507                         if ((standard_id & V4L2_STD_625_50)) {
508                                 calculated_xsize = (cfg->ysize *
509                                         VPBE_DISPLAY_V_EXP_RATIO_N) /
510                                         VPBE_DISPLAY_V_EXP_RATIO_D;
511                                 if (calculated_xsize <= expected_ysize) {
512                                         v_exp = 1;
513                                         cfg->ysize = calculated_xsize;
514                                 }
515                         }
516                 }
517                 if (v_scale == 2)
518                         layer_info->v_zoom = ZOOM_X2;
519                 else if (v_scale == 4)
520                         layer_info->v_zoom = ZOOM_X4;
521                 if (v_exp)
522                         layer_info->h_exp = V_EXP_6_OVER_5;
523         } else {
524                 /* no scaling, only cropping. Set display area to crop area */
525                 cfg->ysize = expected_ysize;
526         }
527         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
528                 "crop display xsize = %d, ysize = %d\n",
529                 cfg->xsize, cfg->ysize);
530 }
531 
532 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
533                         struct vpbe_layer *layer,
534                         int top, int left)
535 {
536         struct osd_layer_config *cfg = &layer->layer_info.config;
537         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
538 
539         cfg->xpos = min((unsigned int)left,
540                         vpbe_dev->current_timings.xres - cfg->xsize);
541         cfg->ypos = min((unsigned int)top,
542                         vpbe_dev->current_timings.yres - cfg->ysize);
543 
544         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
545                 "new xpos = %d, ypos = %d\n",
546                 cfg->xpos, cfg->ypos);
547 }
548 
549 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
550                         struct v4l2_rect *c)
551 {
552         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
553 
554         if ((c->width == 0) ||
555           ((c->width + c->left) > vpbe_dev->current_timings.xres))
556                 c->width = vpbe_dev->current_timings.xres - c->left;
557 
558         if ((c->height == 0) || ((c->height + c->top) >
559           vpbe_dev->current_timings.yres))
560                 c->height = vpbe_dev->current_timings.yres - c->top;
561 
562         /* window height must be even for interlaced display */
563         if (vpbe_dev->current_timings.interlaced)
564                 c->height &= (~0x01);
565 
566 }
567 
568 /**
569  * vpbe_try_format()
570  * If user application provides width and height, and have bytesperline set
571  * to zero, driver calculates bytesperline and sizeimage based on hardware
572  * limits.
573  */
574 static int vpbe_try_format(struct vpbe_display *disp_dev,
575                         struct v4l2_pix_format *pixfmt, int check)
576 {
577         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
578         int min_height = 1;
579         int min_width = 32;
580         int max_height;
581         int max_width;
582         int bpp;
583 
584         if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
585             (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
586                 /* choose default as V4L2_PIX_FMT_UYVY */
587                 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
588 
589         /* Check the field format */
590         if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
591                 (pixfmt->field != V4L2_FIELD_NONE)) {
592                 if (vpbe_dev->current_timings.interlaced)
593                         pixfmt->field = V4L2_FIELD_INTERLACED;
594                 else
595                         pixfmt->field = V4L2_FIELD_NONE;
596         }
597 
598         if (pixfmt->field == V4L2_FIELD_INTERLACED)
599                 min_height = 2;
600 
601         if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
602                 bpp = 1;
603         else
604                 bpp = 2;
605 
606         max_width = vpbe_dev->current_timings.xres;
607         max_height = vpbe_dev->current_timings.yres;
608 
609         min_width /= bpp;
610 
611         if (!pixfmt->width || (pixfmt->width < min_width) ||
612                 (pixfmt->width > max_width)) {
613                 pixfmt->width = vpbe_dev->current_timings.xres;
614         }
615 
616         if (!pixfmt->height || (pixfmt->height  < min_height) ||
617                 (pixfmt->height  > max_height)) {
618                 pixfmt->height = vpbe_dev->current_timings.yres;
619         }
620 
621         if (pixfmt->bytesperline < (pixfmt->width * bpp))
622                 pixfmt->bytesperline = pixfmt->width * bpp;
623 
624         /* Make the bytesperline 32 byte aligned */
625         pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
626 
627         if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
628                 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
629                                 (pixfmt->bytesperline * pixfmt->height >> 1);
630         else
631                 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
632 
633         return 0;
634 }
635 
636 static int vpbe_display_querycap(struct file *file, void  *priv,
637                                struct v4l2_capability *cap)
638 {
639         struct vpbe_layer *layer = video_drvdata(file);
640         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
641 
642         cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
643         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
644         snprintf(cap->driver, sizeof(cap->driver), "%s",
645                 dev_name(vpbe_dev->pdev));
646         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
647                  dev_name(vpbe_dev->pdev));
648         strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
649 
650         return 0;
651 }
652 
653 static int vpbe_display_s_crop(struct file *file, void *priv,
654                              const struct v4l2_crop *crop)
655 {
656         struct vpbe_layer *layer = video_drvdata(file);
657         struct vpbe_display *disp_dev = layer->disp_dev;
658         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
659         struct osd_layer_config *cfg = &layer->layer_info.config;
660         struct osd_state *osd_device = disp_dev->osd_device;
661         struct v4l2_rect rect = crop->c;
662         int ret;
663 
664         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
665                 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
666 
667         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
668                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
669                 return -EINVAL;
670         }
671 
672         if (rect.top < 0)
673                 rect.top = 0;
674         if (rect.left < 0)
675                 rect.left = 0;
676 
677         vpbe_disp_check_window_params(disp_dev, &rect);
678 
679         osd_device->ops.get_layer_config(osd_device,
680                         layer->layer_info.id, cfg);
681 
682         vpbe_disp_calculate_scale_factor(disp_dev, layer,
683                                         rect.width,
684                                         rect.height);
685         vpbe_disp_adj_position(disp_dev, layer, rect.top,
686                                         rect.left);
687         ret = osd_device->ops.set_layer_config(osd_device,
688                                 layer->layer_info.id, cfg);
689         if (ret < 0) {
690                 v4l2_err(&vpbe_dev->v4l2_dev,
691                         "Error in set layer config:\n");
692                 return -EINVAL;
693         }
694 
695         /* apply zooming and h or v expansion */
696         osd_device->ops.set_zoom(osd_device,
697                         layer->layer_info.id,
698                         layer->layer_info.h_zoom,
699                         layer->layer_info.v_zoom);
700         ret = osd_device->ops.set_vid_expansion(osd_device,
701                         layer->layer_info.h_exp,
702                         layer->layer_info.v_exp);
703         if (ret < 0) {
704                 v4l2_err(&vpbe_dev->v4l2_dev,
705                 "Error in set vid expansion:\n");
706                 return -EINVAL;
707         }
708 
709         if ((layer->layer_info.h_zoom != ZOOM_X1) ||
710                 (layer->layer_info.v_zoom != ZOOM_X1) ||
711                 (layer->layer_info.h_exp != H_EXP_OFF) ||
712                 (layer->layer_info.v_exp != V_EXP_OFF))
713                 /* Enable expansion filter */
714                 osd_device->ops.set_interpolation_filter(osd_device, 1);
715         else
716                 osd_device->ops.set_interpolation_filter(osd_device, 0);
717 
718         return 0;
719 }
720 
721 static int vpbe_display_g_crop(struct file *file, void *priv,
722                              struct v4l2_crop *crop)
723 {
724         struct vpbe_layer *layer = video_drvdata(file);
725         struct osd_layer_config *cfg = &layer->layer_info.config;
726         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
727         struct osd_state *osd_device = layer->disp_dev->osd_device;
728         struct v4l2_rect *rect = &crop->c;
729 
730         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
731                         "VIDIOC_G_CROP, layer id = %d\n",
732                         layer->device_id);
733 
734         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
735                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
736                 return -EINVAL;
737         }
738         osd_device->ops.get_layer_config(osd_device,
739                                 layer->layer_info.id, cfg);
740         rect->top = cfg->ypos;
741         rect->left = cfg->xpos;
742         rect->width = cfg->xsize;
743         rect->height = cfg->ysize;
744 
745         return 0;
746 }
747 
748 static int vpbe_display_cropcap(struct file *file, void *priv,
749                               struct v4l2_cropcap *cropcap)
750 {
751         struct vpbe_layer *layer = video_drvdata(file);
752         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
753 
754         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
755 
756         cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
757         cropcap->bounds.left = 0;
758         cropcap->bounds.top = 0;
759         cropcap->bounds.width = vpbe_dev->current_timings.xres;
760         cropcap->bounds.height = vpbe_dev->current_timings.yres;
761         cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
762         cropcap->defrect = cropcap->bounds;
763         return 0;
764 }
765 
766 static int vpbe_display_g_fmt(struct file *file, void *priv,
767                                 struct v4l2_format *fmt)
768 {
769         struct vpbe_layer *layer = video_drvdata(file);
770         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
771 
772         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
773                         "VIDIOC_G_FMT, layer id = %d\n",
774                         layer->device_id);
775 
776         /* If buffer type is video output */
777         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
778                 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
779                 return -EINVAL;
780         }
781         /* Fill in the information about format */
782         fmt->fmt.pix = layer->pix_fmt;
783 
784         return 0;
785 }
786 
787 static int vpbe_display_enum_fmt(struct file *file, void  *priv,
788                                    struct v4l2_fmtdesc *fmt)
789 {
790         struct vpbe_layer *layer = video_drvdata(file);
791         struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
792         unsigned int index = 0;
793 
794         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
795                                 "VIDIOC_ENUM_FMT, layer id = %d\n",
796                                 layer->device_id);
797         if (fmt->index > 1) {
798                 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
799                 return -EINVAL;
800         }
801 
802         /* Fill in the information about format */
803         index = fmt->index;
804         memset(fmt, 0, sizeof(*fmt));
805         fmt->index = index;
806         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
807         if (index == 0) {
808                 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
809                 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
810         } else {
811                 strcpy(fmt->description, "Y/CbCr 4:2:0");
812                 fmt->pixelformat = V4L2_PIX_FMT_NV12;
813         }
814 
815         return 0;
816 }
817 
818 static int vpbe_display_s_fmt(struct file *file, void *priv,
819                                 struct v4l2_format *fmt)
820 {
821         struct vpbe_layer *layer = video_drvdata(file);
822         struct vpbe_display *disp_dev = layer->disp_dev;
823         struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
824         struct osd_layer_config *cfg  = &layer->layer_info.config;
825         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
826         struct osd_state *osd_device = disp_dev->osd_device;
827         int ret;
828 
829         v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
830                         "VIDIOC_S_FMT, layer id = %d\n",
831                         layer->device_id);
832 
833         if (vb2_is_busy(&layer->buffer_queue))
834                 return -EBUSY;
835 
836         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
837                 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
838                 return -EINVAL;
839         }
840         /* Check for valid pixel format */
841         ret = vpbe_try_format(disp_dev, pixfmt, 1);
842         if (ret)
843                 return ret;
844 
845         /* YUV420 is requested, check availability of the
846         other video window */
847 
848         layer->pix_fmt = *pixfmt;
849         if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
850                 struct vpbe_layer *otherlayer;
851 
852                 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
853                 /* if other layer is available, only
854                  * claim it, do not configure it
855                  */
856                 ret = osd_device->ops.request_layer(osd_device,
857                                                     otherlayer->layer_info.id);
858                 if (ret < 0) {
859                         v4l2_err(&vpbe_dev->v4l2_dev,
860                                  "Display Manager failed to allocate layer\n");
861                         return -EBUSY;
862                 }
863         }
864 
865         /* Get osd layer config */
866         osd_device->ops.get_layer_config(osd_device,
867                         layer->layer_info.id, cfg);
868         /* Store the pixel format in the layer object */
869         cfg->xsize = pixfmt->width;
870         cfg->ysize = pixfmt->height;
871         cfg->line_length = pixfmt->bytesperline;
872         cfg->ypos = 0;
873         cfg->xpos = 0;
874         cfg->interlaced = vpbe_dev->current_timings.interlaced;
875 
876         if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
877                 cfg->pixfmt = PIXFMT_YCBCRI;
878 
879         /* Change of the default pixel format for both video windows */
880         if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
881                 struct vpbe_layer *otherlayer;
882                 cfg->pixfmt = PIXFMT_NV12;
883                 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
884                                                                 layer);
885                 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
886         }
887 
888         /* Set the layer config in the osd window */
889         ret = osd_device->ops.set_layer_config(osd_device,
890                                 layer->layer_info.id, cfg);
891         if (ret < 0) {
892                 v4l2_err(&vpbe_dev->v4l2_dev,
893                                 "Error in S_FMT params:\n");
894                 return -EINVAL;
895         }
896 
897         /* Readback and fill the local copy of current pix format */
898         osd_device->ops.get_layer_config(osd_device,
899                         layer->layer_info.id, cfg);
900 
901         return 0;
902 }
903 
904 static int vpbe_display_try_fmt(struct file *file, void *priv,
905                                   struct v4l2_format *fmt)
906 {
907         struct vpbe_layer *layer = video_drvdata(file);
908         struct vpbe_display *disp_dev = layer->disp_dev;
909         struct vpbe_device *vpbe_dev = layer->disp_dev->