Version:  2.0.40 2.2.26 2.4.37 3.6 3.7 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

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us