2 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
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>
24 #include <linux/mutex.h>
25 #include <linux/videodev2.h>
26 #include <linux/slab.h>
28 #include <asm/pgtable.h>
29 #include <mach/cputype.h>
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"
42 #define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
46 #define VPBE_DEFAULT_NUM_BUFS 3
48 module_param(debug, int, 0644);
50 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
51 struct vpbe_layer *layer);
53 static int venc_is_second_field(struct vpbe_display *disp_dev)
55 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
59 ret = v4l2_subdev_call(vpbe_dev->venc,
65 v4l2_err(&vpbe_dev->v4l2_dev,
66 "Error in getting Field ID 0\n");
71 static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
72 struct vpbe_layer *layer)
74 struct timespec timevalue;
76 if (layer->cur_frm == layer->next_frm)
78 ktime_get_ts(&timevalue);
79 layer->cur_frm->vb.v4l2_buf.timestamp.tv_sec =
81 layer->cur_frm->vb.v4l2_buf.timestamp.tv_usec =
82 timevalue.tv_nsec / NSEC_PER_USEC;
83 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_DONE);
84 /* Make cur_frm pointing to next_frm */
85 layer->cur_frm = layer->next_frm;
88 static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
89 struct vpbe_layer *layer)
91 struct osd_state *osd_device = disp_obj->osd_device;
94 spin_lock(&disp_obj->dma_queue_lock);
95 if (list_empty(&layer->dma_queue) ||
96 (layer->cur_frm != layer->next_frm)) {
97 spin_unlock(&disp_obj->dma_queue_lock);
101 * one field is displayed configure
102 * the next frame if it is available
103 * otherwise hold on current frame
104 * Get next from the buffer queue
106 layer->next_frm = list_entry(layer->dma_queue.next,
107 struct vpbe_disp_buffer, list);
108 /* Remove that from the buffer queue */
109 list_del(&layer->next_frm->list);
110 spin_unlock(&disp_obj->dma_queue_lock);
111 /* Mark state of the frame to active */
112 layer->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
113 addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb, 0);
114 osd_device->ops.start_layer(osd_device,
115 layer->layer_info.id,
117 disp_obj->cbcr_ofst);
120 /* interrupt service routine */
121 static irqreturn_t venc_isr(int irq, void *arg)
123 struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
124 struct vpbe_layer *layer;
125 static unsigned last_event;
130 if ((NULL == arg) || (NULL == disp_dev->dev[0]))
133 if (venc_is_second_field(disp_dev))
134 event |= VENC_SECOND_FIELD;
136 event |= VENC_FIRST_FIELD;
138 if (event == (last_event & ~VENC_END_OF_FRAME)) {
140 * If the display is non-interlaced, then we need to flag the
141 * end-of-frame event at every interrupt regardless of the
142 * value of the FIDST bit. We can conclude that the display is
143 * non-interlaced if the value of the FIDST bit is unchanged
144 * from the previous interrupt.
146 event |= VENC_END_OF_FRAME;
147 } else if (event == VENC_SECOND_FIELD) {
148 /* end-of-frame for interlaced display */
149 event |= VENC_END_OF_FRAME;
153 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
154 layer = disp_dev->dev[i];
155 /* If streaming is started in this layer */
159 if (layer->layer_first_int) {
160 layer->layer_first_int = 0;
163 /* Check the field format */
164 if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
165 (event & VENC_END_OF_FRAME)) {
166 /* Progressive mode */
168 vpbe_isr_even_field(disp_dev, layer);
169 vpbe_isr_odd_field(disp_dev, layer);
171 /* Interlaced mode */
173 layer->field_id ^= 1;
174 if (event & VENC_FIRST_FIELD)
180 * If field id does not match with store
183 if (fid != layer->field_id) {
184 /* Make them in sync */
185 layer->field_id = fid;
189 * device field id and local field id are
190 * in sync. If this is even field
193 vpbe_isr_even_field(disp_dev, layer);
195 vpbe_isr_odd_field(disp_dev, layer);
203 * vpbe_buffer_prepare()
204 * This is the callback function called from vb2_qbuf() function
205 * the buffer is prepared and user space virtual address is converted into
208 static int vpbe_buffer_prepare(struct vb2_buffer *vb)
210 struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
211 struct vb2_queue *q = vb->vb2_queue;
212 struct vpbe_layer *layer = fh->layer;
213 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
216 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
217 "vpbe_buffer_prepare\n");
219 if (vb->state != VB2_BUF_STATE_ACTIVE &&
220 vb->state != VB2_BUF_STATE_PREPARED) {
221 vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
222 if (vb2_plane_vaddr(vb, 0) &&
223 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
226 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
228 if (!IS_ALIGNED(addr, 8)) {
229 v4l2_err(&vpbe_dev->v4l2_dev,
230 "buffer_prepare:offset is \
231 not aligned to 32 bytes\n");
240 * vpbe_buffer_setup()
241 * This function allocates memory for the buffers
244 vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
245 unsigned int *nbuffers, unsigned int *nplanes,
246 unsigned int sizes[], void *alloc_ctxs[])
249 /* Get the file handle object and layer object */
250 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
251 struct vpbe_layer *layer = fh->layer;
252 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
254 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
256 /* Store number of buffers allocated in numbuffer member */
257 if (*nbuffers < VPBE_DEFAULT_NUM_BUFS)
258 *nbuffers = layer->numbuffers = VPBE_DEFAULT_NUM_BUFS;
261 sizes[0] = layer->pix_fmt.sizeimage;
262 alloc_ctxs[0] = layer->alloc_ctx;
268 * vpbe_buffer_queue()
269 * This function adds the buffer to DMA queue
271 static void vpbe_buffer_queue(struct vb2_buffer *vb)
273 /* Get the file handle object and layer object */
274 struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
275 struct vpbe_disp_buffer *buf = container_of(vb,
276 struct vpbe_disp_buffer, vb);
277 struct vpbe_layer *layer = fh->layer;
278 struct vpbe_display *disp = fh->disp_dev;
279 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
282 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
283 "vpbe_buffer_queue\n");
285 /* add the buffer to the DMA queue */
286 spin_lock_irqsave(&disp->dma_queue_lock, flags);
287 list_add_tail(&buf->list, &layer->dma_queue);
288 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
293 * This function is called from the vb2 layer to free memory allocated to
296 static void vpbe_buf_cleanup(struct vb2_buffer *vb)
298 /* Get the file handle object and layer object */
299 struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
300 struct vpbe_layer *layer = fh->layer;
301 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
302 struct vpbe_disp_buffer *buf = container_of(vb,
303 struct vpbe_disp_buffer, vb);
306 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
307 "vpbe_buf_cleanup\n");
309 spin_lock_irqsave(&layer->irqlock, flags);
310 if (vb->state == VB2_BUF_STATE_ACTIVE)
311 list_del_init(&buf->list);
312 spin_unlock_irqrestore(&layer->irqlock, flags);
315 static void vpbe_wait_prepare(struct vb2_queue *vq)
317 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
318 struct vpbe_layer *layer = fh->layer;
320 mutex_unlock(&layer->opslock);
323 static void vpbe_wait_finish(struct vb2_queue *vq)
325 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
326 struct vpbe_layer *layer = fh->layer;
328 mutex_lock(&layer->opslock);
331 static int vpbe_buffer_init(struct vb2_buffer *vb)
333 struct vpbe_disp_buffer *buf = container_of(vb,
334 struct vpbe_disp_buffer, vb);
336 INIT_LIST_HEAD(&buf->list);
340 static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
342 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
343 struct vpbe_layer *layer = fh->layer;
346 /* Get the next frame from the buffer queue */
347 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
348 struct vpbe_disp_buffer, list);
349 /* Remove buffer from the buffer queue */
350 list_del(&layer->cur_frm->list);
351 /* Mark state of the current frame to active */
352 layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
353 /* Initialize field_id and started member */
356 /* Set parameters in OSD and VENC */
357 ret = vpbe_set_osd_display_params(fh->disp_dev, layer);
359 struct vpbe_disp_buffer *buf, *tmp;
361 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_QUEUED);
362 list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
363 list_del(&buf->list);
364 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
371 * if request format is yuv420 semiplanar, need to
372 * enable both video windows
375 layer->layer_first_int = 1;
380 static void vpbe_stop_streaming(struct vb2_queue *vq)
382 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
383 struct vpbe_layer *layer = fh->layer;
384 struct vpbe_display *disp = fh->disp_dev;
387 if (!vb2_is_streaming(vq))
390 /* release all active buffers */
391 spin_lock_irqsave(&disp->dma_queue_lock, flags);
392 if (layer->cur_frm == layer->next_frm) {
393 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_ERROR);
395 if (layer->cur_frm != NULL)
396 vb2_buffer_done(&layer->cur_frm->vb,
397 VB2_BUF_STATE_ERROR);
398 if (layer->next_frm != NULL)
399 vb2_buffer_done(&layer->next_frm->vb,
400 VB2_BUF_STATE_ERROR);
403 while (!list_empty(&layer->dma_queue)) {
404 layer->next_frm = list_entry(layer->dma_queue.next,
405 struct vpbe_disp_buffer, list);
406 list_del(&layer->next_frm->list);
407 vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
409 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
412 static struct vb2_ops video_qops = {
413 .queue_setup = vpbe_buffer_queue_setup,
414 .wait_prepare = vpbe_wait_prepare,
415 .wait_finish = vpbe_wait_finish,
416 .buf_init = vpbe_buffer_init,
417 .buf_prepare = vpbe_buffer_prepare,
418 .start_streaming = vpbe_start_streaming,
419 .stop_streaming = vpbe_stop_streaming,
420 .buf_cleanup = vpbe_buf_cleanup,
421 .buf_queue = vpbe_buffer_queue,
426 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
427 struct vpbe_layer *layer)
429 enum vpbe_display_device_id thiswin, otherwin;
430 thiswin = layer->device_id;
432 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
433 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
434 return disp_dev->dev[otherwin];
437 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
438 struct vpbe_layer *layer)
440 struct osd_layer_config *cfg = &layer->layer_info.config;
441 struct osd_state *osd_device = disp_dev->osd_device;
442 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
446 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
447 /* Set address in the display registers */
448 osd_device->ops.start_layer(osd_device,
449 layer->layer_info.id,
451 disp_dev->cbcr_ofst);
453 ret = osd_device->ops.enable_layer(osd_device,
454 layer->layer_info.id, 0);
456 v4l2_err(&vpbe_dev->v4l2_dev,
457 "Error in enabling osd window layer 0\n");
461 /* Enable the window */
462 layer->layer_info.enable = 1;
463 if (cfg->pixfmt == PIXFMT_NV12) {
464 struct vpbe_layer *otherlayer =
465 _vpbe_display_get_other_win_layer(disp_dev, layer);
467 ret = osd_device->ops.enable_layer(osd_device,
468 otherlayer->layer_info.id, 1);
470 v4l2_err(&vpbe_dev->v4l2_dev,
471 "Error in enabling osd window layer 1\n");
474 otherlayer->layer_info.enable = 1;
480 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
481 struct vpbe_layer *layer,
482 int expected_xsize, int expected_ysize)
484 struct display_layer_info *layer_info = &layer->layer_info;
485 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
486 struct osd_layer_config *cfg = &layer->layer_info.config;
487 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
488 int calculated_xsize;
494 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
497 * Application initially set the image format. Current display
498 * size is obtained from the vpbe display controller. expected_xsize
499 * and expected_ysize are set through S_CROP ioctl. Based on this,
500 * driver will calculate the scale factors for vertical and
501 * horizontal direction so that the image is displayed scaled
502 * and expanded. Application uses expansion to display the image
503 * in a square pixel. Otherwise it is displayed using displays
504 * pixel aspect ratio.It is expected that application chooses
505 * the crop coordinates for cropped or scaled display. if crop
506 * size is less than the image size, it is displayed cropped or
507 * it is displayed scaled and/or expanded.
509 * to begin with, set the crop window same as expected. Later we
510 * will override with scaled window size
513 cfg->xsize = pixfmt->width;
514 cfg->ysize = pixfmt->height;
515 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
516 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
517 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
518 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
520 if (pixfmt->width < expected_xsize) {
521 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
524 else if (h_scale >= 4)
528 cfg->xsize *= h_scale;
529 if (cfg->xsize < expected_xsize) {
530 if ((standard_id & V4L2_STD_525_60) ||
531 (standard_id & V4L2_STD_625_50)) {
532 calculated_xsize = (cfg->xsize *
533 VPBE_DISPLAY_H_EXP_RATIO_N) /
534 VPBE_DISPLAY_H_EXP_RATIO_D;
535 if (calculated_xsize <= expected_xsize) {
537 cfg->xsize = calculated_xsize;
542 layer_info->h_zoom = ZOOM_X2;
543 else if (h_scale == 4)
544 layer_info->h_zoom = ZOOM_X4;
546 layer_info->h_exp = H_EXP_9_OVER_8;
548 /* no scaling, only cropping. Set display area to crop area */
549 cfg->xsize = expected_xsize;
552 if (pixfmt->height < expected_ysize) {
553 v_scale = expected_ysize / pixfmt->height;
556 else if (v_scale >= 4)
560 cfg->ysize *= v_scale;
561 if (cfg->ysize < expected_ysize) {
562 if ((standard_id & V4L2_STD_625_50)) {
563 calculated_xsize = (cfg->ysize *
564 VPBE_DISPLAY_V_EXP_RATIO_N) /
565 VPBE_DISPLAY_V_EXP_RATIO_D;
566 if (calculated_xsize <= expected_ysize) {
568 cfg->ysize = calculated_xsize;
573 layer_info->v_zoom = ZOOM_X2;
574 else if (v_scale == 4)
575 layer_info->v_zoom = ZOOM_X4;
577 layer_info->h_exp = V_EXP_6_OVER_5;
579 /* no scaling, only cropping. Set display area to crop area */
580 cfg->ysize = expected_ysize;
582 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
583 "crop display xsize = %d, ysize = %d\n",
584 cfg->xsize, cfg->ysize);
587 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
588 struct vpbe_layer *layer,
591 struct osd_layer_config *cfg = &layer->layer_info.config;
592 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
594 cfg->xpos = min((unsigned int)left,
595 vpbe_dev->current_timings.xres - cfg->xsize);
596 cfg->ypos = min((unsigned int)top,
597 vpbe_dev->current_timings.yres - cfg->ysize);
599 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
600 "new xpos = %d, ypos = %d\n",
601 cfg->xpos, cfg->ypos);
604 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
607 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
609 if ((c->width == 0) ||
610 ((c->width + c->left) > vpbe_dev->current_timings.xres))
611 c->width = vpbe_dev->current_timings.xres - c->left;
613 if ((c->height == 0) || ((c->height + c->top) >
614 vpbe_dev->current_timings.yres))
615 c->height = vpbe_dev->current_timings.yres - c->top;
617 /* window height must be even for interlaced display */
618 if (vpbe_dev->current_timings.interlaced)
619 c->height &= (~0x01);
625 * If user application provides width and height, and have bytesperline set
626 * to zero, driver calculates bytesperline and sizeimage based on hardware
629 static int vpbe_try_format(struct vpbe_display *disp_dev,
630 struct v4l2_pix_format *pixfmt, int check)
632 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
639 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
640 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
641 /* choose default as V4L2_PIX_FMT_UYVY */
642 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
644 /* Check the field format */
645 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
646 (pixfmt->field != V4L2_FIELD_NONE)) {
647 if (vpbe_dev->current_timings.interlaced)
648 pixfmt->field = V4L2_FIELD_INTERLACED;
650 pixfmt->field = V4L2_FIELD_NONE;
653 if (pixfmt->field == V4L2_FIELD_INTERLACED)
656 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
661 max_width = vpbe_dev->current_timings.xres;
662 max_height = vpbe_dev->current_timings.yres;
666 if (!pixfmt->width || (pixfmt->width < min_width) ||
667 (pixfmt->width > max_width)) {
668 pixfmt->width = vpbe_dev->current_timings.xres;
671 if (!pixfmt->height || (pixfmt->height < min_height) ||
672 (pixfmt->height > max_height)) {
673 pixfmt->height = vpbe_dev->current_timings.yres;
676 if (pixfmt->bytesperline < (pixfmt->width * bpp))
677 pixfmt->bytesperline = pixfmt->width * bpp;
679 /* Make the bytesperline 32 byte aligned */
680 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
682 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
683 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
684 (pixfmt->bytesperline * pixfmt->height >> 1);
686 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
691 static int vpbe_display_querycap(struct file *file, void *priv,
692 struct v4l2_capability *cap)
694 struct vpbe_fh *fh = file->private_data;
695 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
697 cap->version = VPBE_DISPLAY_VERSION_CODE;
698 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
699 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
700 snprintf(cap->driver, sizeof(cap->driver), "%s",
701 dev_name(vpbe_dev->pdev));
702 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
703 dev_name(vpbe_dev->pdev));
704 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
709 static int vpbe_display_s_crop(struct file *file, void *priv,
710 const struct v4l2_crop *crop)
712 struct vpbe_fh *fh = file->private_data;
713 struct vpbe_layer *layer = fh->layer;
714 struct vpbe_display *disp_dev = fh->disp_dev;
715 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
716 struct osd_layer_config *cfg = &layer->layer_info.config;
717 struct osd_state *osd_device = disp_dev->osd_device;
718 struct v4l2_rect rect = crop->c;
721 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
722 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
724 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
725 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
734 vpbe_disp_check_window_params(disp_dev, &rect);
736 osd_device->ops.get_layer_config(osd_device,
737 layer->layer_info.id, cfg);
739 vpbe_disp_calculate_scale_factor(disp_dev, layer,
742 vpbe_disp_adj_position(disp_dev, layer, rect.top,
744 ret = osd_device->ops.set_layer_config(osd_device,
745 layer->layer_info.id, cfg);
747 v4l2_err(&vpbe_dev->v4l2_dev,
748 "Error in set layer config:\n");
752 /* apply zooming and h or v expansion */
753 osd_device->ops.set_zoom(osd_device,
754 layer->layer_info.id,
755 layer->layer_info.h_zoom,
756 layer->layer_info.v_zoom);
757 ret = osd_device->ops.set_vid_expansion(osd_device,
758 layer->layer_info.h_exp,
759 layer->layer_info.v_exp);
761 v4l2_err(&vpbe_dev->v4l2_dev,
762 "Error in set vid expansion:\n");
766 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
767 (layer->layer_info.v_zoom != ZOOM_X1) ||
768 (layer->layer_info.h_exp != H_EXP_OFF) ||
769 (layer->layer_info.v_exp != V_EXP_OFF))
770 /* Enable expansion filter */
771 osd_device->ops.set_interpolation_filter(osd_device, 1);
773 osd_device->ops.set_interpolation_filter(osd_device, 0);
778 static int vpbe_display_g_crop(struct file *file, void *priv,
779 struct v4l2_crop *crop)
781 struct vpbe_fh *fh = file->private_data;
782 struct vpbe_layer *layer = fh->layer;
783 struct osd_layer_config *cfg = &layer->layer_info.config;
784 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
785 struct osd_state *osd_device = fh->disp_dev->osd_device;
786 struct v4l2_rect *rect = &crop->c;
788 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
789 "VIDIOC_G_CROP, layer id = %d\n",
792 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
793 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
796 osd_device->ops.get_layer_config(osd_device,
797 layer->layer_info.id, cfg);
798 rect->top = cfg->ypos;
799 rect->left = cfg->xpos;
800 rect->width = cfg->xsize;
801 rect->height = cfg->ysize;
806 static int vpbe_display_cropcap(struct file *file, void *priv,
807 struct v4l2_cropcap *cropcap)
809 struct vpbe_fh *fh = file->private_data;
810 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
812 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
814 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
815 cropcap->bounds.left = 0;
816 cropcap->bounds.top = 0;
817 cropcap->bounds.width = vpbe_dev->current_timings.xres;
818 cropcap->bounds.height = vpbe_dev->current_timings.yres;
819 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
820 cropcap->defrect = cropcap->bounds;
824 static int vpbe_display_g_fmt(struct file *file, void *priv,
825 struct v4l2_format *fmt)
827 struct vpbe_fh *fh = file->private_data;
828 struct vpbe_layer *layer = fh->layer;
829 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
831 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
832 "VIDIOC_G_FMT, layer id = %d\n",
835 /* If buffer type is video output */
836 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
837 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
840 /* Fill in the information about format */
841 fmt->fmt.pix = layer->pix_fmt;
846 static int vpbe_display_enum_fmt(struct file *file, void *priv,
847 struct v4l2_fmtdesc *fmt)
849 struct vpbe_fh *fh = file->private_data;
850 struct vpbe_layer *layer = fh->layer;
851 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
852 unsigned int index = 0;
854 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
855 "VIDIOC_ENUM_FMT, layer id = %d\n",
857 if (fmt->index > 1) {
858 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
862 /* Fill in the information about format */
864 memset(fmt, 0, sizeof(*fmt));
866 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
868 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
869 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
871 strcpy(fmt->description, "Y/CbCr 4:2:0");
872 fmt->pixelformat = V4L2_PIX_FMT_NV12;
878 static int vpbe_display_s_fmt(struct file *file, void *priv,
879 struct v4l2_format *fmt)
881 struct vpbe_fh *fh = file->private_data;
882 struct vpbe_layer *layer = fh->layer;
883 struct vpbe_display *disp_dev = fh->disp_dev;
884 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
885 struct osd_layer_config *cfg = &layer->layer_info.config;
886 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
887 struct osd_state *osd_device = disp_dev->osd_device;
890 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
891 "VIDIOC_S_FMT, layer id = %d\n",
894 /* If streaming is started, return error */
895 if (layer->started) {
896 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
899 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
900 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
903 /* Check for valid pixel format */
904 ret = vpbe_try_format(disp_dev, pixfmt, 1);
908 /* YUV420 is requested, check availability of the
909 other video window */
911 layer->pix_fmt = *pixfmt;
912 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
913 struct vpbe_layer *otherlayer;
915 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
916 /* if other layer is available, only
917 * claim it, do not configure it
919 ret = osd_device->ops.request_layer(osd_device,
920 otherlayer->layer_info.id);
922 v4l2_err(&vpbe_dev->v4l2_dev,
923 "Display Manager failed to allocate layer\n");
928 /* Get osd layer config */
929 osd_device->ops.get_layer_config(osd_device,
930 layer->layer_info.id, cfg);
931 /* Store the pixel format in the layer object */
932 cfg->xsize = pixfmt->width;
933 cfg->ysize = pixfmt->height;
934 cfg->line_length = pixfmt->bytesperline;
937 cfg->interlaced = vpbe_dev->current_timings.interlaced;
939 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
940 cfg->pixfmt = PIXFMT_YCBCRI;
942 /* Change of the default pixel format for both video windows */
943 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
944 struct vpbe_layer *otherlayer;
945 cfg->pixfmt = PIXFMT_NV12;
946 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
948 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
951 /* Set the layer config in the osd window */
952 ret = osd_device->ops.set_layer_config(osd_device,
953 layer->layer_info.id, cfg);
955 v4l2_err(&vpbe_dev->v4l2_dev,
956 "Error in S_FMT params:\n");
960 /* Readback and fill the local copy of current pix format */
961 osd_device->ops.get_layer_config(osd_device,
962 layer->layer_info.id, cfg);
967 static int vpbe_display_try_fmt(struct file *file, void *priv,
968 struct v4l2_format *fmt)
970 struct vpbe_fh *fh = file->private_data;
971 struct vpbe_display *disp_dev = fh->disp_dev;
972 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
973 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
975 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
977 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
978 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
982 /* Check for valid field format */
983 return vpbe_try_format(disp_dev, pixfmt, 0);
988 * vpbe_display_s_std - Set the given standard in the encoder
990 * Sets the standard if supported by the current encoder. Return the status.
991 * 0 - success & -EINVAL on error
993 static int vpbe_display_s_std(struct file *file, void *priv,
996 struct vpbe_fh *fh = priv;
997 struct vpbe_layer *layer = fh->layer;
998 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1001 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
1003 /* If streaming is started, return error */
1004 if (layer->started) {
1005 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1008 if (NULL != vpbe_dev->ops.s_std) {
1009 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
1011 v4l2_err(&vpbe_dev->v4l2_dev,
1012 "Failed to set standard for sub devices\n");
1023 * vpbe_display_g_std - Get the standard in the current encoder
1025 * Get the standard in the current encoder. Return the status. 0 - success
1028 static int vpbe_display_g_std(struct file *file, void *priv,
1029 v4l2_std_id *std_id)
1031 struct vpbe_fh *fh = priv;
1032 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1034 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
1036 /* Get the standard from the current encoder */
1037 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
1038 *std_id = vpbe_dev->current_timings.std_id;
1046 * vpbe_display_enum_output - enumerate outputs
1048 * Enumerates the outputs available at the vpbe display
1049 * returns the status, -EINVAL if end of output list
1051 static int vpbe_display_enum_output(struct file *file, void *priv,
1052 struct v4l2_output *output)
1054 struct vpbe_fh *fh = priv;
1055 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1058 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1060 /* Enumerate outputs */
1062 if (NULL == vpbe_dev->ops.enum_outputs)
1065 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1067 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1068 "Failed to enumerate outputs\n");
1076 * vpbe_display_s_output - Set output to
1077 * the output specified by the index
1079 static int vpbe_display_s_output(struct file *file, void *priv,
1082 struct vpbe_fh *fh = priv;
1083 struct vpbe_layer *layer = fh->layer;
1084 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1087 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1088 /* If streaming is started, return error */
1089 if (layer->started) {
1090 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1093 if (NULL == vpbe_dev->ops.set_output)
1096 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1098 v4l2_err(&vpbe_dev->v4l2_dev,
1099 "Failed to set output for sub devices\n");
1107 * vpbe_display_g_output - Get output from subdevice
1108 * for a given by the index
1110 static int vpbe_display_g_output(struct file *file, void *priv,
1113 struct vpbe_fh *fh = priv;
1114 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1116 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1117 /* Get the standard from the current encoder */
1118 *i = vpbe_dev->current_out_index;
1124 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1126 * enum the timings in the current encoder. Return the status. 0 - success
1130 vpbe_display_enum_dv_timings(struct file *file, void *priv,
1131 struct v4l2_enum_dv_timings *timings)
1133 struct vpbe_fh *fh = priv;
1134 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1137 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1139 /* Enumerate outputs */
1140 if (NULL == vpbe_dev->ops.enum_dv_timings)
1143 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1145 v4l2_err(&vpbe_dev->v4l2_dev,
1146 "Failed to enumerate dv timings info\n");
1154 * vpbe_display_s_dv_timings - Set the dv timings
1156 * Set the timings in the current encoder. Return the status. 0 - success
1160 vpbe_display_s_dv_timings(struct file *file, void *priv,
1161 struct v4l2_dv_timings *timings)
1163 struct vpbe_fh *fh = priv;
1164 struct vpbe_layer *layer = fh->layer;
1165 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1168 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1171 /* If streaming is started, return error */
1172 if (layer->started) {
1173 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1177 /* Set the given standard in the encoder */
1178 if (!vpbe_dev->ops.s_dv_timings)
1181 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1183 v4l2_err(&vpbe_dev->v4l2_dev,
1184 "Failed to set the dv timings info\n");
1192 * vpbe_display_g_dv_timings - Set the dv timings
1194 * Get the timings in the current encoder. Return the status. 0 - success
1198 vpbe_display_g_dv_timings(struct file *file, void *priv,
1199 struct v4l2_dv_timings *dv_timings)
1201 struct vpbe_fh *fh = priv;
1202 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1204 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1206 /* Get the given standard in the encoder */
1208 if (vpbe_dev->current_timings.timings_type &
1209 VPBE_ENC_DV_TIMINGS) {
1210 *dv_timings = vpbe_dev->current_timings.dv_timings;
1218 static int vpbe_display_streamoff(struct file *file, void *priv,
1219 enum v4l2_buf_type buf_type)
1221 struct vpbe_fh *fh = file->private_data;
1222 struct vpbe_layer *layer = fh->layer;
1223 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1224 struct osd_state *osd_device = fh->disp_dev->osd_device;
1227 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1228 "VIDIOC_STREAMOFF,layer id = %d\n",
1231 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1232 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1236 /* If io is allowed for this file handle, return error */
1237 if (!fh->io_allowed) {
1238 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1242 /* If streaming is not started, return error */
1243 if (!layer->started) {
1244 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1245 " id = %d\n", layer->device_id);
1249 osd_device->ops.disable_layer(osd_device,
1250 layer->layer_info.id);
1252 ret = vb2_streamoff(&layer->buffer_queue, buf_type);
1257 static int vpbe_display_streamon(struct file *file, void *priv,
1258 enum v4l2_buf_type buf_type)
1260 struct vpbe_fh *fh = file->private_data;
1261 struct vpbe_layer *layer = fh->layer;
1262 struct vpbe_display *disp_dev = fh->disp_dev;
1263 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1264 struct osd_state *osd_device = disp_dev->osd_device;
1267 osd_device->ops.disable_layer(osd_device,
1268 layer->layer_info.id);
1270 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1273 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1274 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1278 /* If file handle is not allowed IO, return error */
1279 if (!fh->io_allowed) {
1280 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1283 /* If Streaming is already started, return error */
1284 if (layer->started) {
1285 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1290 * Call vb2_streamon to start streaming
1293 ret = vb2_streamon(&layer->buffer_queue, buf_type);
1295 v4l2_err(&vpbe_dev->v4l2_dev,
1296 "error in vb2_streamon\n");
1302 static int vpbe_display_dqbuf(struct file *file, void *priv,
1303 struct v4l2_buffer *buf)
1305 struct vpbe_fh *fh = file->private_data;
1306 struct vpbe_layer *layer = fh->layer;
1307 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1310 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1311 "VIDIOC_DQBUF, layer id = %d\n",
1314 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1315 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1318 /* If this file handle is not allowed to do IO, return error */
1319 if (!fh->io_allowed) {
1320 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1323 if (file->f_flags & O_NONBLOCK)
1324 /* Call videobuf_dqbuf for non blocking mode */
1325 ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
1327 /* Call videobuf_dqbuf for blocking mode */
1328 ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
1333 static int vpbe_display_qbuf(struct file *file, void *priv,
1334 struct v4l2_buffer *p)
1336 struct vpbe_fh *fh = file->private_data;
1337 struct vpbe_layer *layer = fh->layer;
1338 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1340 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1341 "VIDIOC_QBUF, layer id = %d\n",
1344 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1345 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1349 /* If this file handle is not allowed to do IO, return error */
1350 if (!fh->io_allowed) {
1351 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1355 return vb2_qbuf(&layer->buffer_queue, p);
1358 static int vpbe_display_querybuf(struct file *file, void *priv,
1359 struct v4l2_buffer *buf)
1361 struct vpbe_fh *fh = file->private_data;
1362 struct vpbe_layer *layer = fh->layer;
1363 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1365 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1366 "VIDIOC_QUERYBUF, layer id = %d\n",
1369 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1370 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1373 /* Call vb2_querybuf to get information */
1374 return vb2_querybuf(&layer->buffer_queue, buf);
1377 static int vpbe_display_reqbufs(struct file *file, void *priv,
1378 struct v4l2_requestbuffers *req_buf)
1380 struct vpbe_fh *fh = file->private_data;
1381 struct vpbe_layer *layer = fh->layer;
1382 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1383 struct vb2_queue *q;
1385 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1387 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1388 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1392 /* If io users of the layer is not zero, return error */
1393 if (0 != layer->io_usrs) {
1394 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1397 /* Initialize videobuf queue as per the buffer type */
1398 layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
1399 if (IS_ERR(layer->alloc_ctx)) {
1400 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
1401 return PTR_ERR(layer->alloc_ctx);
1403 q = &layer->buffer_queue;
1404 memset(q, 0, sizeof(*q));
1405 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1406 q->io_modes = VB2_MMAP | VB2_USERPTR;
1408 q->ops = &video_qops;
1409 q->mem_ops = &vb2_dma_contig_memops;
1410 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1411 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1412 q->min_buffers_needed = 1;
1414 ret = vb2_queue_init(q);
1416 v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1417 vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
1420 /* Set io allowed member of file handle to TRUE */
1422 /* Increment io usrs member of layer object to 1 */
1424 /* Store type of memory requested in layer object */
1425 layer->memory = req_buf->memory;
1426 /* Initialize buffer queue */
1427 INIT_LIST_HEAD(&layer->dma_queue);
1428 /* Allocate buffers */
1429 return vb2_reqbufs(q, req_buf);
1433 * vpbe_display_mmap()
1434 * It is used to map kernel space buffers into user spaces
1436 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1438 /* Get the layer object and file handle object */
1439 struct vpbe_fh *fh = filep->private_data;
1440 struct vpbe_layer *layer = fh->layer;
1441 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1444 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1446 if (mutex_lock_interruptible(&layer->opslock))
1447 return -ERESTARTSYS;
1448 ret = vb2_mmap(&layer->buffer_queue, vma);
1449 mutex_unlock(&layer->opslock);
1453 /* vpbe_display_poll(): It is used for select/poll system call
1455 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1457 struct vpbe_fh *fh = filep->private_data;
1458 struct vpbe_layer *layer = fh->layer;
1459 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1460 unsigned int err = 0;
1462 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1463 if (layer->started) {
1464 mutex_lock(&layer->opslock);
1465 err = vb2_poll(&layer->buffer_queue, filep, wait);
1466 mutex_unlock(&layer->opslock);
1472 * vpbe_display_open()
1473 * It creates object of file handle structure and stores it in private_data
1474 * member of filepointer
1476 static int vpbe_display_open(struct file *file)
1478 struct vpbe_fh *fh = NULL;
1479 struct vpbe_layer *layer = video_drvdata(file);
1480 struct video_device *vdev = video_devdata(file);
1481 struct vpbe_display *disp_dev = layer->disp_dev;
1482 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1483 struct osd_state *osd_device = disp_dev->osd_device;
1486 /* Allocate memory for the file handle object */
1487 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1489 v4l2_err(&vpbe_dev->v4l2_dev,
1490 "unable to allocate memory for file handle object\n");
1493 v4l2_fh_init(&fh->fh, vdev);
1494 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1495 "vpbe display open plane = %d\n",
1498 /* store pointer to fh in private_data member of filep */
1499 file->private_data = fh;
1501 fh->disp_dev = disp_dev;
1504 if (mutex_lock_interruptible(&layer->opslock))
1505 return -ERESTARTSYS;
1506 /* First claim the layer for this device */
1507 err = osd_device->ops.request_layer(osd_device,
1508 layer->layer_info.id);
1509 mutex_unlock(&layer->opslock);
1511 /* Couldn't get layer */
1512 v4l2_err(&vpbe_dev->v4l2_dev,
1513 "Display Manager failed to allocate layer\n");
1518 /* Increment layer usrs counter */
1520 /* Set io_allowed member to false */
1522 v4l2_fh_add(&fh->fh);
1523 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1524 "vpbe display device opened successfully\n");
1529 * vpbe_display_release()
1530 * This function deletes buffer queue, frees the buffers and the davinci
1531 * display file * handle
1533 static int vpbe_display_release(struct file *file)
1535 /* Get the layer object and file handle object */
1536 struct vpbe_fh *fh = file->private_data;
1537 struct vpbe_layer *layer = fh->layer;
1538 struct osd_layer_config *cfg = &layer->layer_info.config;
1539 struct vpbe_display *disp_dev = fh->disp_dev;
1540 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1541 struct osd_state *osd_device = disp_dev->osd_device;
1543 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1545 mutex_lock(&layer->opslock);
1546 /* if this instance is doing IO */
1547 if (fh->io_allowed) {
1548 /* Reset io_usrs member of layer object */
1551 osd_device->ops.disable_layer(osd_device,
1552 layer->layer_info.id);
1554 /* Free buffers allocated */
1555 vb2_queue_release(&layer->buffer_queue);
1556 vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
1559 /* Decrement layer usrs counter */
1561 /* If this file handle has initialize encoder device, reset it */
1563 if (cfg->pixfmt == PIXFMT_NV12) {
1564 struct vpbe_layer *otherlayer;
1566 _vpbe_display_get_other_win_layer(disp_dev, layer);
1567 osd_device->ops.disable_layer(osd_device,
1568 otherlayer->layer_info.id);
1569 osd_device->ops.release_layer(osd_device,
1570 otherlayer->layer_info.id);
1572 osd_device->ops.disable_layer(osd_device,
1573 layer->layer_info.id);
1574 osd_device->ops.release_layer(osd_device,
1575 layer->layer_info.id);
1578 v4l2_fh_del(&fh->fh);
1579 v4l2_fh_exit(&fh->fh);
1580 file->private_data = NULL;
1581 mutex_unlock(&layer->opslock);
1583 /* Free memory allocated to file handle object */
1586 disp_dev->cbcr_ofst = 0;
1591 /* vpbe capture ioctl operations */
1592 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1593 .vidioc_querycap = vpbe_display_querycap,
1594 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1595 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1596 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1597 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1598 .vidioc_reqbufs = vpbe_display_reqbufs,
1599 .vidioc_querybuf = vpbe_display_querybuf,
1600 .vidioc_qbuf = vpbe_display_qbuf,
1601 .vidioc_dqbuf = vpbe_display_dqbuf,
1602 .vidioc_streamon = vpbe_display_streamon,
1603 .vidioc_streamoff = vpbe_display_streamoff,
1604 .vidioc_cropcap = vpbe_display_cropcap,
1605 .vidioc_g_crop = vpbe_display_g_crop,
1606 .vidioc_s_crop = vpbe_display_s_crop,
1607 .vidioc_s_std = vpbe_display_s_std,
1608 .vidioc_g_std = vpbe_display_g_std,
1609 .vidioc_enum_output = vpbe_display_enum_output,
1610 .vidioc_s_output = vpbe_display_s_output,
1611 .vidioc_g_output = vpbe_display_g_output,
1612 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1613 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1614 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
1617 static struct v4l2_file_operations vpbe_fops = {
1618 .owner = THIS_MODULE,
1619 .open = vpbe_display_open,
1620 .release = vpbe_display_release,
1621 .unlocked_ioctl = video_ioctl2,
1622 .mmap = vpbe_display_mmap,
1623 .poll = vpbe_display_poll
1626 static int vpbe_device_get(struct device *dev, void *data)
1628 struct platform_device *pdev = to_platform_device(dev);
1629 struct vpbe_display *vpbe_disp = data;
1631 if (strcmp("vpbe_controller", pdev->name) == 0)
1632 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1634 if (strstr(pdev->name, "vpbe-osd") != NULL)
1635 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1640 static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1641 struct platform_device *pdev)
1643 struct vpbe_layer *vpbe_display_layer = NULL;
1644 struct video_device *vbd = NULL;
1646 /* Allocate memory for four plane display objects */
1649 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1651 /* If memory allocation fails, return error */
1652 if (!disp_dev->dev[i]) {
1653 printk(KERN_ERR "ran out of memory\n");
1656 spin_lock_init(&disp_dev->dev[i]->irqlock);
1657 mutex_init(&disp_dev->dev[i]->opslock);
1659 /* Get the pointer to the layer object */
1660 vpbe_display_layer = disp_dev->dev[i];
1661 vbd = &vpbe_display_layer->video_dev;
1662 /* Initialize field of video device */
1663 vbd->release = video_device_release_empty;
1664 vbd->fops = &vpbe_fops;
1665 vbd->ioctl_ops = &vpbe_ioctl_ops;
1667 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1668 vbd->lock = &vpbe_display_layer->opslock;
1669 vbd->vfl_dir = VFL_DIR_TX;
1671 if (disp_dev->vpbe_dev->current_timings.timings_type &
1673 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1675 snprintf(vbd->name, sizeof(vbd->name),
1676 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1677 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1678 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1679 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1681 vpbe_display_layer->device_id = i;
1683 vpbe_display_layer->layer_info.id =
1684 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1690 static int register_device(struct vpbe_layer *vpbe_display_layer,
1691 struct vpbe_display *disp_dev,
1692 struct platform_device *pdev)
1696 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1697 "Trying to register VPBE display device.\n");
1698 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1699 "layer=%x,layer->video_dev=%x\n",
1700 (int)vpbe_display_layer,
1701 (int)&vpbe_display_layer->video_dev);
1703 err = video_register_device(&vpbe_display_layer->video_dev,
1709 vpbe_display_layer->disp_dev = disp_dev;
1710 /* set the driver data in platform device */
1711 platform_set_drvdata(pdev, disp_dev);
1712 video_set_drvdata(&vpbe_display_layer->video_dev,
1713 vpbe_display_layer);
1721 * vpbe_display_probe()
1722 * This function creates device entries by register itself to the V4L2 driver
1723 * and initializes fields of each layer objects
1725 static int vpbe_display_probe(struct platform_device *pdev)
1727 struct vpbe_layer *vpbe_display_layer;
1728 struct vpbe_display *disp_dev;
1729 struct resource *res = NULL;
1735 printk(KERN_DEBUG "vpbe_display_probe\n");
1736 /* Allocate memory for vpbe_display */
1737 disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1742 spin_lock_init(&disp_dev->dma_queue_lock);
1744 * Scan all the platform devices to find the vpbe
1745 * controller device and get the vpbe_dev object
1747 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1751 /* Initialize the vpbe display controller */
1752 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1753 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1754 disp_dev->vpbe_dev);
1756 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1757 "Error initing vpbe\n");
1763 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1764 if (init_vpbe_layer(i, disp_dev, pdev)) {
1770 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1772 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1773 "Unable to get VENC interrupt resource\n");
1779 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1780 VPBE_DISPLAY_DRIVER, disp_dev);
1782 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1783 "Unable to request interrupt\n");
1787 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1788 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1794 printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
1798 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1799 /* Get the pointer to the layer object */
1800 vpbe_display_layer = disp_dev->dev[k];
1801 /* Unregister video device */
1802 if (vpbe_display_layer) {
1803 video_unregister_device(
1804 &vpbe_display_layer->video_dev);
1805 kfree(disp_dev->dev[k]);
1812 * vpbe_display_remove()
1813 * It un-register hardware layer from V4L2 driver
1815 static int vpbe_display_remove(struct platform_device *pdev)
1817 struct vpbe_layer *vpbe_display_layer;
1818 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1819 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1822 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1824 /* deinitialize the vpbe display controller */
1825 if (NULL != vpbe_dev->ops.deinitialize)
1826 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1827 /* un-register device */
1828 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1829 /* Get the pointer to the layer object */
1830 vpbe_display_layer = disp_dev->dev[i];
1831 /* Unregister video device */
1832 video_unregister_device(&vpbe_display_layer->video_dev);
1835 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1836 kfree(disp_dev->dev[i]);
1837 disp_dev->dev[i] = NULL;
1843 static struct platform_driver vpbe_display_driver = {
1845 .name = VPBE_DISPLAY_DRIVER,
1846 .bus = &platform_bus_type,
1848 .probe = vpbe_display_probe,
1849 .remove = vpbe_display_remove,
1852 module_platform_driver(vpbe_display_driver);
1854 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1855 MODULE_LICENSE("GPL");
1856 MODULE_AUTHOR("Texas Instruments");