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);
362 * if request format is yuv420 semiplanar, need to
363 * enable both video windows
366 layer->layer_first_int = 1;
371 static int vpbe_stop_streaming(struct vb2_queue *vq)
373 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
374 struct vpbe_layer *layer = fh->layer;
375 struct vpbe_display *disp = fh->disp_dev;
378 if (!vb2_is_streaming(vq))
381 /* release all active buffers */
382 spin_lock_irqsave(&disp->dma_queue_lock, flags);
383 if (layer->cur_frm == layer->next_frm) {
384 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_ERROR);
386 if (layer->cur_frm != NULL)
387 vb2_buffer_done(&layer->cur_frm->vb,
388 VB2_BUF_STATE_ERROR);
389 if (layer->next_frm != NULL)
390 vb2_buffer_done(&layer->next_frm->vb,
391 VB2_BUF_STATE_ERROR);
394 while (!list_empty(&layer->dma_queue)) {
395 layer->next_frm = list_entry(layer->dma_queue.next,
396 struct vpbe_disp_buffer, list);
397 list_del(&layer->next_frm->list);
398 vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
400 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
404 static struct vb2_ops video_qops = {
405 .queue_setup = vpbe_buffer_queue_setup,
406 .wait_prepare = vpbe_wait_prepare,
407 .wait_finish = vpbe_wait_finish,
408 .buf_init = vpbe_buffer_init,
409 .buf_prepare = vpbe_buffer_prepare,
410 .start_streaming = vpbe_start_streaming,
411 .stop_streaming = vpbe_stop_streaming,
412 .buf_cleanup = vpbe_buf_cleanup,
413 .buf_queue = vpbe_buffer_queue,
418 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
419 struct vpbe_layer *layer)
421 enum vpbe_display_device_id thiswin, otherwin;
422 thiswin = layer->device_id;
424 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
425 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
426 return disp_dev->dev[otherwin];
429 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
430 struct vpbe_layer *layer)
432 struct osd_layer_config *cfg = &layer->layer_info.config;
433 struct osd_state *osd_device = disp_dev->osd_device;
434 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
438 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
439 /* Set address in the display registers */
440 osd_device->ops.start_layer(osd_device,
441 layer->layer_info.id,
443 disp_dev->cbcr_ofst);
445 ret = osd_device->ops.enable_layer(osd_device,
446 layer->layer_info.id, 0);
448 v4l2_err(&vpbe_dev->v4l2_dev,
449 "Error in enabling osd window layer 0\n");
453 /* Enable the window */
454 layer->layer_info.enable = 1;
455 if (cfg->pixfmt == PIXFMT_NV12) {
456 struct vpbe_layer *otherlayer =
457 _vpbe_display_get_other_win_layer(disp_dev, layer);
459 ret = osd_device->ops.enable_layer(osd_device,
460 otherlayer->layer_info.id, 1);
462 v4l2_err(&vpbe_dev->v4l2_dev,
463 "Error in enabling osd window layer 1\n");
466 otherlayer->layer_info.enable = 1;
472 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
473 struct vpbe_layer *layer,
474 int expected_xsize, int expected_ysize)
476 struct display_layer_info *layer_info = &layer->layer_info;
477 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
478 struct osd_layer_config *cfg = &layer->layer_info.config;
479 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
480 int calculated_xsize;
486 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
489 * Application initially set the image format. Current display
490 * size is obtained from the vpbe display controller. expected_xsize
491 * and expected_ysize are set through S_CROP ioctl. Based on this,
492 * driver will calculate the scale factors for vertical and
493 * horizontal direction so that the image is displayed scaled
494 * and expanded. Application uses expansion to display the image
495 * in a square pixel. Otherwise it is displayed using displays
496 * pixel aspect ratio.It is expected that application chooses
497 * the crop coordinates for cropped or scaled display. if crop
498 * size is less than the image size, it is displayed cropped or
499 * it is displayed scaled and/or expanded.
501 * to begin with, set the crop window same as expected. Later we
502 * will override with scaled window size
505 cfg->xsize = pixfmt->width;
506 cfg->ysize = pixfmt->height;
507 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
508 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
509 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
510 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
512 if (pixfmt->width < expected_xsize) {
513 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
516 else if (h_scale >= 4)
520 cfg->xsize *= h_scale;
521 if (cfg->xsize < expected_xsize) {
522 if ((standard_id & V4L2_STD_525_60) ||
523 (standard_id & V4L2_STD_625_50)) {
524 calculated_xsize = (cfg->xsize *
525 VPBE_DISPLAY_H_EXP_RATIO_N) /
526 VPBE_DISPLAY_H_EXP_RATIO_D;
527 if (calculated_xsize <= expected_xsize) {
529 cfg->xsize = calculated_xsize;
534 layer_info->h_zoom = ZOOM_X2;
535 else if (h_scale == 4)
536 layer_info->h_zoom = ZOOM_X4;
538 layer_info->h_exp = H_EXP_9_OVER_8;
540 /* no scaling, only cropping. Set display area to crop area */
541 cfg->xsize = expected_xsize;
544 if (pixfmt->height < expected_ysize) {
545 v_scale = expected_ysize / pixfmt->height;
548 else if (v_scale >= 4)
552 cfg->ysize *= v_scale;
553 if (cfg->ysize < expected_ysize) {
554 if ((standard_id & V4L2_STD_625_50)) {
555 calculated_xsize = (cfg->ysize *
556 VPBE_DISPLAY_V_EXP_RATIO_N) /
557 VPBE_DISPLAY_V_EXP_RATIO_D;
558 if (calculated_xsize <= expected_ysize) {
560 cfg->ysize = calculated_xsize;
565 layer_info->v_zoom = ZOOM_X2;
566 else if (v_scale == 4)
567 layer_info->v_zoom = ZOOM_X4;
569 layer_info->h_exp = V_EXP_6_OVER_5;
571 /* no scaling, only cropping. Set display area to crop area */
572 cfg->ysize = expected_ysize;
574 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
575 "crop display xsize = %d, ysize = %d\n",
576 cfg->xsize, cfg->ysize);
579 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
580 struct vpbe_layer *layer,
583 struct osd_layer_config *cfg = &layer->layer_info.config;
584 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
586 cfg->xpos = min((unsigned int)left,
587 vpbe_dev->current_timings.xres - cfg->xsize);
588 cfg->ypos = min((unsigned int)top,
589 vpbe_dev->current_timings.yres - cfg->ysize);
591 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
592 "new xpos = %d, ypos = %d\n",
593 cfg->xpos, cfg->ypos);
596 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
599 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
601 if ((c->width == 0) ||
602 ((c->width + c->left) > vpbe_dev->current_timings.xres))
603 c->width = vpbe_dev->current_timings.xres - c->left;
605 if ((c->height == 0) || ((c->height + c->top) >
606 vpbe_dev->current_timings.yres))
607 c->height = vpbe_dev->current_timings.yres - c->top;
609 /* window height must be even for interlaced display */
610 if (vpbe_dev->current_timings.interlaced)
611 c->height &= (~0x01);
617 * If user application provides width and height, and have bytesperline set
618 * to zero, driver calculates bytesperline and sizeimage based on hardware
621 static int vpbe_try_format(struct vpbe_display *disp_dev,
622 struct v4l2_pix_format *pixfmt, int check)
624 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
631 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
632 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
633 /* choose default as V4L2_PIX_FMT_UYVY */
634 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
636 /* Check the field format */
637 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
638 (pixfmt->field != V4L2_FIELD_NONE)) {
639 if (vpbe_dev->current_timings.interlaced)
640 pixfmt->field = V4L2_FIELD_INTERLACED;
642 pixfmt->field = V4L2_FIELD_NONE;
645 if (pixfmt->field == V4L2_FIELD_INTERLACED)
648 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
653 max_width = vpbe_dev->current_timings.xres;
654 max_height = vpbe_dev->current_timings.yres;
658 if (!pixfmt->width || (pixfmt->width < min_width) ||
659 (pixfmt->width > max_width)) {
660 pixfmt->width = vpbe_dev->current_timings.xres;
663 if (!pixfmt->height || (pixfmt->height < min_height) ||
664 (pixfmt->height > max_height)) {
665 pixfmt->height = vpbe_dev->current_timings.yres;
668 if (pixfmt->bytesperline < (pixfmt->width * bpp))
669 pixfmt->bytesperline = pixfmt->width * bpp;
671 /* Make the bytesperline 32 byte aligned */
672 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
674 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
675 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
676 (pixfmt->bytesperline * pixfmt->height >> 1);
678 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
683 static int vpbe_display_g_priority(struct file *file, void *priv,
684 enum v4l2_priority *p)
686 struct vpbe_fh *fh = file->private_data;
687 struct vpbe_layer *layer = fh->layer;
689 *p = v4l2_prio_max(&layer->prio);
694 static int vpbe_display_s_priority(struct file *file, void *priv,
695 enum v4l2_priority p)
697 struct vpbe_fh *fh = file->private_data;
698 struct vpbe_layer *layer = fh->layer;
701 ret = v4l2_prio_change(&layer->prio, &fh->prio, p);
706 static int vpbe_display_querycap(struct file *file, void *priv,
707 struct v4l2_capability *cap)
709 struct vpbe_fh *fh = file->private_data;
710 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
712 cap->version = VPBE_DISPLAY_VERSION_CODE;
713 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
714 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
715 snprintf(cap->driver, sizeof(cap->driver), "%s",
716 dev_name(vpbe_dev->pdev));
717 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
718 dev_name(vpbe_dev->pdev));
719 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
724 static int vpbe_display_s_crop(struct file *file, void *priv,
725 const struct v4l2_crop *crop)
727 struct vpbe_fh *fh = file->private_data;
728 struct vpbe_layer *layer = fh->layer;
729 struct vpbe_display *disp_dev = fh->disp_dev;
730 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
731 struct osd_layer_config *cfg = &layer->layer_info.config;
732 struct osd_state *osd_device = disp_dev->osd_device;
733 struct v4l2_rect rect = crop->c;
736 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
737 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
739 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
740 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
749 vpbe_disp_check_window_params(disp_dev, &rect);
751 osd_device->ops.get_layer_config(osd_device,
752 layer->layer_info.id, cfg);
754 vpbe_disp_calculate_scale_factor(disp_dev, layer,
757 vpbe_disp_adj_position(disp_dev, layer, rect.top,
759 ret = osd_device->ops.set_layer_config(osd_device,
760 layer->layer_info.id, cfg);
762 v4l2_err(&vpbe_dev->v4l2_dev,
763 "Error in set layer config:\n");
767 /* apply zooming and h or v expansion */
768 osd_device->ops.set_zoom(osd_device,
769 layer->layer_info.id,
770 layer->layer_info.h_zoom,
771 layer->layer_info.v_zoom);
772 ret = osd_device->ops.set_vid_expansion(osd_device,
773 layer->layer_info.h_exp,
774 layer->layer_info.v_exp);
776 v4l2_err(&vpbe_dev->v4l2_dev,
777 "Error in set vid expansion:\n");
781 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
782 (layer->layer_info.v_zoom != ZOOM_X1) ||
783 (layer->layer_info.h_exp != H_EXP_OFF) ||
784 (layer->layer_info.v_exp != V_EXP_OFF))
785 /* Enable expansion filter */
786 osd_device->ops.set_interpolation_filter(osd_device, 1);
788 osd_device->ops.set_interpolation_filter(osd_device, 0);
793 static int vpbe_display_g_crop(struct file *file, void *priv,
794 struct v4l2_crop *crop)
796 struct vpbe_fh *fh = file->private_data;
797 struct vpbe_layer *layer = fh->layer;
798 struct osd_layer_config *cfg = &layer->layer_info.config;
799 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
800 struct osd_state *osd_device = fh->disp_dev->osd_device;
801 struct v4l2_rect *rect = &crop->c;
803 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
804 "VIDIOC_G_CROP, layer id = %d\n",
807 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
808 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
811 osd_device->ops.get_layer_config(osd_device,
812 layer->layer_info.id, cfg);
813 rect->top = cfg->ypos;
814 rect->left = cfg->xpos;
815 rect->width = cfg->xsize;
816 rect->height = cfg->ysize;
821 static int vpbe_display_cropcap(struct file *file, void *priv,
822 struct v4l2_cropcap *cropcap)
824 struct vpbe_fh *fh = file->private_data;
825 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
827 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
829 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
830 cropcap->bounds.left = 0;
831 cropcap->bounds.top = 0;
832 cropcap->bounds.width = vpbe_dev->current_timings.xres;
833 cropcap->bounds.height = vpbe_dev->current_timings.yres;
834 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
835 cropcap->defrect = cropcap->bounds;
839 static int vpbe_display_g_fmt(struct file *file, void *priv,
840 struct v4l2_format *fmt)
842 struct vpbe_fh *fh = file->private_data;
843 struct vpbe_layer *layer = fh->layer;
844 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
846 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
847 "VIDIOC_G_FMT, layer id = %d\n",
850 /* If buffer type is video output */
851 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
852 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
855 /* Fill in the information about format */
856 fmt->fmt.pix = layer->pix_fmt;
861 static int vpbe_display_enum_fmt(struct file *file, void *priv,
862 struct v4l2_fmtdesc *fmt)
864 struct vpbe_fh *fh = file->private_data;
865 struct vpbe_layer *layer = fh->layer;
866 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
867 unsigned int index = 0;
869 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
870 "VIDIOC_ENUM_FMT, layer id = %d\n",
872 if (fmt->index > 1) {
873 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
877 /* Fill in the information about format */
879 memset(fmt, 0, sizeof(*fmt));
881 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
883 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
884 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
886 strcpy(fmt->description, "Y/CbCr 4:2:0");
887 fmt->pixelformat = V4L2_PIX_FMT_NV12;
893 static int vpbe_display_s_fmt(struct file *file, void *priv,
894 struct v4l2_format *fmt)
896 struct vpbe_fh *fh = file->private_data;
897 struct vpbe_layer *layer = fh->layer;
898 struct vpbe_display *disp_dev = fh->disp_dev;
899 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
900 struct osd_layer_config *cfg = &layer->layer_info.config;
901 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
902 struct osd_state *osd_device = disp_dev->osd_device;
905 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
906 "VIDIOC_S_FMT, layer id = %d\n",
909 /* If streaming is started, return error */
910 if (layer->started) {
911 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
914 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
915 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
918 /* Check for valid pixel format */
919 ret = vpbe_try_format(disp_dev, pixfmt, 1);
923 /* YUV420 is requested, check availability of the
924 other video window */
926 layer->pix_fmt = *pixfmt;
927 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
928 struct vpbe_layer *otherlayer;
930 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
931 /* if other layer is available, only
932 * claim it, do not configure it
934 ret = osd_device->ops.request_layer(osd_device,
935 otherlayer->layer_info.id);
937 v4l2_err(&vpbe_dev->v4l2_dev,
938 "Display Manager failed to allocate layer\n");
943 /* Get osd layer config */
944 osd_device->ops.get_layer_config(osd_device,
945 layer->layer_info.id, cfg);
946 /* Store the pixel format in the layer object */
947 cfg->xsize = pixfmt->width;
948 cfg->ysize = pixfmt->height;
949 cfg->line_length = pixfmt->bytesperline;
952 cfg->interlaced = vpbe_dev->current_timings.interlaced;
954 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
955 cfg->pixfmt = PIXFMT_YCBCRI;
957 /* Change of the default pixel format for both video windows */
958 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
959 struct vpbe_layer *otherlayer;
960 cfg->pixfmt = PIXFMT_NV12;
961 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
963 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
966 /* Set the layer config in the osd window */
967 ret = osd_device->ops.set_layer_config(osd_device,
968 layer->layer_info.id, cfg);
970 v4l2_err(&vpbe_dev->v4l2_dev,
971 "Error in S_FMT params:\n");
975 /* Readback and fill the local copy of current pix format */
976 osd_device->ops.get_layer_config(osd_device,
977 layer->layer_info.id, cfg);
982 static int vpbe_display_try_fmt(struct file *file, void *priv,
983 struct v4l2_format *fmt)
985 struct vpbe_fh *fh = file->private_data;
986 struct vpbe_display *disp_dev = fh->disp_dev;
987 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
988 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
990 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
992 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
993 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
997 /* Check for valid field format */
998 return vpbe_try_format(disp_dev, pixfmt, 0);
1003 * vpbe_display_s_std - Set the given standard in the encoder
1005 * Sets the standard if supported by the current encoder. Return the status.
1006 * 0 - success & -EINVAL on error
1008 static int vpbe_display_s_std(struct file *file, void *priv,
1011 struct vpbe_fh *fh = priv;
1012 struct vpbe_layer *layer = fh->layer;
1013 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1016 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
1018 /* If streaming is started, return error */
1019 if (layer->started) {
1020 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1023 if (NULL != vpbe_dev->ops.s_std) {
1024 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
1026 v4l2_err(&vpbe_dev->v4l2_dev,
1027 "Failed to set standard for sub devices\n");
1038 * vpbe_display_g_std - Get the standard in the current encoder
1040 * Get the standard in the current encoder. Return the status. 0 - success
1043 static int vpbe_display_g_std(struct file *file, void *priv,
1044 v4l2_std_id *std_id)
1046 struct vpbe_fh *fh = priv;
1047 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1049 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
1051 /* Get the standard from the current encoder */
1052 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
1053 *std_id = vpbe_dev->current_timings.std_id;
1061 * vpbe_display_enum_output - enumerate outputs
1063 * Enumerates the outputs available at the vpbe display
1064 * returns the status, -EINVAL if end of output list
1066 static int vpbe_display_enum_output(struct file *file, void *priv,
1067 struct v4l2_output *output)
1069 struct vpbe_fh *fh = priv;
1070 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1073 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1075 /* Enumerate outputs */
1077 if (NULL == vpbe_dev->ops.enum_outputs)
1080 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1082 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1083 "Failed to enumerate outputs\n");
1091 * vpbe_display_s_output - Set output to
1092 * the output specified by the index
1094 static int vpbe_display_s_output(struct file *file, void *priv,
1097 struct vpbe_fh *fh = priv;
1098 struct vpbe_layer *layer = fh->layer;
1099 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1102 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1103 /* If streaming is started, return error */
1104 if (layer->started) {
1105 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1108 if (NULL == vpbe_dev->ops.set_output)
1111 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1113 v4l2_err(&vpbe_dev->v4l2_dev,
1114 "Failed to set output for sub devices\n");
1122 * vpbe_display_g_output - Get output from subdevice
1123 * for a given by the index
1125 static int vpbe_display_g_output(struct file *file, void *priv,
1128 struct vpbe_fh *fh = priv;
1129 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1131 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1132 /* Get the standard from the current encoder */
1133 *i = vpbe_dev->current_out_index;
1139 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1141 * enum the timings in the current encoder. Return the status. 0 - success
1145 vpbe_display_enum_dv_timings(struct file *file, void *priv,
1146 struct v4l2_enum_dv_timings *timings)
1148 struct vpbe_fh *fh = priv;
1149 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1152 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1154 /* Enumerate outputs */
1155 if (NULL == vpbe_dev->ops.enum_dv_timings)
1158 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1160 v4l2_err(&vpbe_dev->v4l2_dev,
1161 "Failed to enumerate dv timings info\n");
1169 * vpbe_display_s_dv_timings - Set the dv timings
1171 * Set the timings in the current encoder. Return the status. 0 - success
1175 vpbe_display_s_dv_timings(struct file *file, void *priv,
1176 struct v4l2_dv_timings *timings)
1178 struct vpbe_fh *fh = priv;
1179 struct vpbe_layer *layer = fh->layer;
1180 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1183 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1186 /* If streaming is started, return error */
1187 if (layer->started) {
1188 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1192 /* Set the given standard in the encoder */
1193 if (!vpbe_dev->ops.s_dv_timings)
1196 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1198 v4l2_err(&vpbe_dev->v4l2_dev,
1199 "Failed to set the dv timings info\n");
1207 * vpbe_display_g_dv_timings - Set the dv timings
1209 * Get the timings in the current encoder. Return the status. 0 - success
1213 vpbe_display_g_dv_timings(struct file *file, void *priv,
1214 struct v4l2_dv_timings *dv_timings)
1216 struct vpbe_fh *fh = priv;
1217 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1219 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1221 /* Get the given standard in the encoder */
1223 if (vpbe_dev->current_timings.timings_type &
1224 VPBE_ENC_DV_TIMINGS) {
1225 *dv_timings = vpbe_dev->current_timings.dv_timings;
1233 static int vpbe_display_streamoff(struct file *file, void *priv,
1234 enum v4l2_buf_type buf_type)
1236 struct vpbe_fh *fh = file->private_data;
1237 struct vpbe_layer *layer = fh->layer;
1238 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1239 struct osd_state *osd_device = fh->disp_dev->osd_device;
1242 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1243 "VIDIOC_STREAMOFF,layer id = %d\n",
1246 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1247 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1251 /* If io is allowed for this file handle, return error */
1252 if (!fh->io_allowed) {
1253 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1257 /* If streaming is not started, return error */
1258 if (!layer->started) {
1259 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1260 " id = %d\n", layer->device_id);
1264 osd_device->ops.disable_layer(osd_device,
1265 layer->layer_info.id);
1267 ret = vb2_streamoff(&layer->buffer_queue, buf_type);
1272 static int vpbe_display_streamon(struct file *file, void *priv,
1273 enum v4l2_buf_type buf_type)
1275 struct vpbe_fh *fh = file->private_data;
1276 struct vpbe_layer *layer = fh->layer;
1277 struct vpbe_display *disp_dev = fh->disp_dev;
1278 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1279 struct osd_state *osd_device = disp_dev->osd_device;
1282 osd_device->ops.disable_layer(osd_device,
1283 layer->layer_info.id);
1285 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1288 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1289 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1293 /* If file handle is not allowed IO, return error */
1294 if (!fh->io_allowed) {
1295 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1298 /* If Streaming is already started, return error */
1299 if (layer->started) {
1300 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1305 * Call vb2_streamon to start streaming
1308 ret = vb2_streamon(&layer->buffer_queue, buf_type);
1310 v4l2_err(&vpbe_dev->v4l2_dev,
1311 "error in vb2_streamon\n");
1317 static int vpbe_display_dqbuf(struct file *file, void *priv,
1318 struct v4l2_buffer *buf)
1320 struct vpbe_fh *fh = file->private_data;
1321 struct vpbe_layer *layer = fh->layer;
1322 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1325 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1326 "VIDIOC_DQBUF, layer id = %d\n",
1329 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1330 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1333 /* If this file handle is not allowed to do IO, return error */
1334 if (!fh->io_allowed) {
1335 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1338 if (file->f_flags & O_NONBLOCK)
1339 /* Call videobuf_dqbuf for non blocking mode */
1340 ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
1342 /* Call videobuf_dqbuf for blocking mode */
1343 ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
1348 static int vpbe_display_qbuf(struct file *file, void *priv,
1349 struct v4l2_buffer *p)
1351 struct vpbe_fh *fh = file->private_data;
1352 struct vpbe_layer *layer = fh->layer;
1353 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1355 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1356 "VIDIOC_QBUF, layer id = %d\n",
1359 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1360 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1364 /* If this file handle is not allowed to do IO, return error */
1365 if (!fh->io_allowed) {
1366 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1370 return vb2_qbuf(&layer->buffer_queue, p);
1373 static int vpbe_display_querybuf(struct file *file, void *priv,
1374 struct v4l2_buffer *buf)
1376 struct vpbe_fh *fh = file->private_data;
1377 struct vpbe_layer *layer = fh->layer;
1378 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1380 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1381 "VIDIOC_QUERYBUF, layer id = %d\n",
1384 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1385 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1388 /* Call vb2_querybuf to get information */
1389 return vb2_querybuf(&layer->buffer_queue, buf);
1392 static int vpbe_display_reqbufs(struct file *file, void *priv,
1393 struct v4l2_requestbuffers *req_buf)
1395 struct vpbe_fh *fh = file->private_data;
1396 struct vpbe_layer *layer = fh->layer;
1397 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1398 struct vb2_queue *q;
1400 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1402 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1403 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1407 /* If io users of the layer is not zero, return error */
1408 if (0 != layer->io_usrs) {
1409 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1412 /* Initialize videobuf queue as per the buffer type */
1413 layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
1414 if (IS_ERR(layer->alloc_ctx)) {
1415 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
1416 return PTR_ERR(layer->alloc_ctx);
1418 q = &layer->buffer_queue;
1419 memset(q, 0, sizeof(*q));
1420 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1421 q->io_modes = VB2_MMAP | VB2_USERPTR;
1423 q->ops = &video_qops;
1424 q->mem_ops = &vb2_dma_contig_memops;
1425 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1426 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1427 q->min_buffers_needed = 1;
1429 ret = vb2_queue_init(q);
1431 v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1432 vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
1435 /* Set io allowed member of file handle to TRUE */
1437 /* Increment io usrs member of layer object to 1 */
1439 /* Store type of memory requested in layer object */
1440 layer->memory = req_buf->memory;
1441 /* Initialize buffer queue */
1442 INIT_LIST_HEAD(&layer->dma_queue);
1443 /* Allocate buffers */
1444 return vb2_reqbufs(q, req_buf);
1448 * vpbe_display_mmap()
1449 * It is used to map kernel space buffers into user spaces
1451 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1453 /* Get the layer object and file handle object */
1454 struct vpbe_fh *fh = filep->private_data;
1455 struct vpbe_layer *layer = fh->layer;
1456 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1459 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1461 if (mutex_lock_interruptible(&layer->opslock))
1462 return -ERESTARTSYS;
1463 ret = vb2_mmap(&layer->buffer_queue, vma);
1464 mutex_unlock(&layer->opslock);
1468 /* vpbe_display_poll(): It is used for select/poll system call
1470 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1472 struct vpbe_fh *fh = filep->private_data;
1473 struct vpbe_layer *layer = fh->layer;
1474 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1475 unsigned int err = 0;
1477 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1478 if (layer->started) {
1479 mutex_lock(&layer->opslock);
1480 err = vb2_poll(&layer->buffer_queue, filep, wait);
1481 mutex_unlock(&layer->opslock);
1487 * vpbe_display_open()
1488 * It creates object of file handle structure and stores it in private_data
1489 * member of filepointer
1491 static int vpbe_display_open(struct file *file)
1493 struct vpbe_fh *fh = NULL;
1494 struct vpbe_layer *layer = video_drvdata(file);
1495 struct vpbe_display *disp_dev = layer->disp_dev;
1496 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1497 struct osd_state *osd_device = disp_dev->osd_device;
1500 /* Allocate memory for the file handle object */
1501 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1503 v4l2_err(&vpbe_dev->v4l2_dev,
1504 "unable to allocate memory for file handle object\n");
1507 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1508 "vpbe display open plane = %d\n",
1511 /* store pointer to fh in private_data member of filep */
1512 file->private_data = fh;
1514 fh->disp_dev = disp_dev;
1517 if (mutex_lock_interruptible(&layer->opslock))
1518 return -ERESTARTSYS;
1519 /* First claim the layer for this device */
1520 err = osd_device->ops.request_layer(osd_device,
1521 layer->layer_info.id);
1522 mutex_unlock(&layer->opslock);
1524 /* Couldn't get layer */
1525 v4l2_err(&vpbe_dev->v4l2_dev,
1526 "Display Manager failed to allocate layer\n");
1531 /* Increment layer usrs counter */
1533 /* Set io_allowed member to false */
1535 /* Initialize priority of this instance to default priority */
1536 fh->prio = V4L2_PRIORITY_UNSET;
1537 v4l2_prio_open(&layer->prio, &fh->prio);
1538 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1539 "vpbe display device opened successfully\n");
1544 * vpbe_display_release()
1545 * This function deletes buffer queue, frees the buffers and the davinci
1546 * display file * handle
1548 static int vpbe_display_release(struct file *file)
1550 /* Get the layer object and file handle object */
1551 struct vpbe_fh *fh = file->private_data;
1552 struct vpbe_layer *layer = fh->layer;
1553 struct osd_layer_config *cfg = &layer->layer_info.config;
1554 struct vpbe_display *disp_dev = fh->disp_dev;
1555 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1556 struct osd_state *osd_device = disp_dev->osd_device;
1558 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1560 mutex_lock(&layer->opslock);
1561 /* if this instance is doing IO */
1562 if (fh->io_allowed) {
1563 /* Reset io_usrs member of layer object */
1566 osd_device->ops.disable_layer(osd_device,
1567 layer->layer_info.id);
1569 /* Free buffers allocated */
1570 vb2_queue_release(&layer->buffer_queue);
1571 vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
1574 /* Decrement layer usrs counter */
1576 /* If this file handle has initialize encoder device, reset it */
1578 if (cfg->pixfmt == PIXFMT_NV12) {
1579 struct vpbe_layer *otherlayer;
1581 _vpbe_display_get_other_win_layer(disp_dev, layer);
1582 osd_device->ops.disable_layer(osd_device,
1583 otherlayer->layer_info.id);
1584 osd_device->ops.release_layer(osd_device,
1585 otherlayer->layer_info.id);
1587 osd_device->ops.disable_layer(osd_device,
1588 layer->layer_info.id);
1589 osd_device->ops.release_layer(osd_device,
1590 layer->layer_info.id);
1592 /* Close the priority */
1593 v4l2_prio_close(&layer->prio, fh->prio);
1594 file->private_data = NULL;
1595 mutex_unlock(&layer->opslock);
1597 /* Free memory allocated to file handle object */
1600 disp_dev->cbcr_ofst = 0;
1605 /* vpbe capture ioctl operations */
1606 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1607 .vidioc_querycap = vpbe_display_querycap,
1608 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1609 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1610 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1611 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1612 .vidioc_reqbufs = vpbe_display_reqbufs,
1613 .vidioc_querybuf = vpbe_display_querybuf,
1614 .vidioc_qbuf = vpbe_display_qbuf,
1615 .vidioc_dqbuf = vpbe_display_dqbuf,
1616 .vidioc_streamon = vpbe_display_streamon,
1617 .vidioc_streamoff = vpbe_display_streamoff,
1618 .vidioc_cropcap = vpbe_display_cropcap,
1619 .vidioc_g_crop = vpbe_display_g_crop,
1620 .vidioc_s_crop = vpbe_display_s_crop,
1621 .vidioc_g_priority = vpbe_display_g_priority,
1622 .vidioc_s_priority = vpbe_display_s_priority,
1623 .vidioc_s_std = vpbe_display_s_std,
1624 .vidioc_g_std = vpbe_display_g_std,
1625 .vidioc_enum_output = vpbe_display_enum_output,
1626 .vidioc_s_output = vpbe_display_s_output,
1627 .vidioc_g_output = vpbe_display_g_output,
1628 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1629 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1630 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
1633 static struct v4l2_file_operations vpbe_fops = {
1634 .owner = THIS_MODULE,
1635 .open = vpbe_display_open,
1636 .release = vpbe_display_release,
1637 .unlocked_ioctl = video_ioctl2,
1638 .mmap = vpbe_display_mmap,
1639 .poll = vpbe_display_poll
1642 static int vpbe_device_get(struct device *dev, void *data)
1644 struct platform_device *pdev = to_platform_device(dev);
1645 struct vpbe_display *vpbe_disp = data;
1647 if (strcmp("vpbe_controller", pdev->name) == 0)
1648 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1650 if (strstr(pdev->name, "vpbe-osd") != NULL)
1651 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1656 static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1657 struct platform_device *pdev)
1659 struct vpbe_layer *vpbe_display_layer = NULL;
1660 struct video_device *vbd = NULL;
1662 /* Allocate memory for four plane display objects */
1665 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1667 /* If memory allocation fails, return error */
1668 if (!disp_dev->dev[i]) {
1669 printk(KERN_ERR "ran out of memory\n");
1672 spin_lock_init(&disp_dev->dev[i]->irqlock);
1673 mutex_init(&disp_dev->dev[i]->opslock);
1675 /* Get the pointer to the layer object */
1676 vpbe_display_layer = disp_dev->dev[i];
1677 vbd = &vpbe_display_layer->video_dev;
1678 /* Initialize field of video device */
1679 vbd->release = video_device_release_empty;
1680 vbd->fops = &vpbe_fops;
1681 vbd->ioctl_ops = &vpbe_ioctl_ops;
1683 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1684 vbd->lock = &vpbe_display_layer->opslock;
1685 vbd->vfl_dir = VFL_DIR_TX;
1687 if (disp_dev->vpbe_dev->current_timings.timings_type &
1689 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1691 snprintf(vbd->name, sizeof(vbd->name),
1692 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1693 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1694 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1695 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1697 vpbe_display_layer->device_id = i;
1699 vpbe_display_layer->layer_info.id =
1700 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1702 /* Initialize prio member of layer object */
1703 v4l2_prio_init(&vpbe_display_layer->prio);
1708 static int register_device(struct vpbe_layer *vpbe_display_layer,
1709 struct vpbe_display *disp_dev,
1710 struct platform_device *pdev)
1714 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1715 "Trying to register VPBE display device.\n");
1716 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1717 "layer=%x,layer->video_dev=%x\n",
1718 (int)vpbe_display_layer,
1719 (int)&vpbe_display_layer->video_dev);
1721 err = video_register_device(&vpbe_display_layer->video_dev,
1727 vpbe_display_layer->disp_dev = disp_dev;
1728 /* set the driver data in platform device */
1729 platform_set_drvdata(pdev, disp_dev);
1730 video_set_drvdata(&vpbe_display_layer->video_dev,
1731 vpbe_display_layer);
1739 * vpbe_display_probe()
1740 * This function creates device entries by register itself to the V4L2 driver
1741 * and initializes fields of each layer objects
1743 static int vpbe_display_probe(struct platform_device *pdev)
1745 struct vpbe_layer *vpbe_display_layer;
1746 struct vpbe_display *disp_dev;
1747 struct resource *res = NULL;
1753 printk(KERN_DEBUG "vpbe_display_probe\n");
1754 /* Allocate memory for vpbe_display */
1755 disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1760 spin_lock_init(&disp_dev->dma_queue_lock);
1762 * Scan all the platform devices to find the vpbe
1763 * controller device and get the vpbe_dev object
1765 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1769 /* Initialize the vpbe display controller */
1770 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1771 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1772 disp_dev->vpbe_dev);
1774 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1775 "Error initing vpbe\n");
1781 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1782 if (init_vpbe_layer(i, disp_dev, pdev)) {
1788 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1790 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1791 "Unable to get VENC interrupt resource\n");
1797 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1798 VPBE_DISPLAY_DRIVER, disp_dev);
1800 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1801 "Unable to request interrupt\n");
1805 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1806 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1812 printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
1816 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1817 /* Get the pointer to the layer object */
1818 vpbe_display_layer = disp_dev->dev[k];
1819 /* Unregister video device */
1820 if (vpbe_display_layer) {
1821 video_unregister_device(
1822 &vpbe_display_layer->video_dev);
1823 kfree(disp_dev->dev[k]);
1830 * vpbe_display_remove()
1831 * It un-register hardware layer from V4L2 driver
1833 static int vpbe_display_remove(struct platform_device *pdev)
1835 struct vpbe_layer *vpbe_display_layer;
1836 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1837 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1840 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1842 /* deinitialize the vpbe display controller */
1843 if (NULL != vpbe_dev->ops.deinitialize)
1844 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1845 /* un-register device */
1846 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1847 /* Get the pointer to the layer object */
1848 vpbe_display_layer = disp_dev->dev[i];
1849 /* Unregister video device */
1850 video_unregister_device(&vpbe_display_layer->video_dev);
1853 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1854 kfree(disp_dev->dev[i]);
1855 disp_dev->dev[i] = NULL;
1861 static struct platform_driver vpbe_display_driver = {
1863 .name = VPBE_DISPLAY_DRIVER,
1864 .owner = THIS_MODULE,
1865 .bus = &platform_bus_type,
1867 .probe = vpbe_display_probe,
1868 .remove = vpbe_display_remove,
1871 module_platform_driver(vpbe_display_driver);
1873 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1874 MODULE_LICENSE("GPL");
1875 MODULE_AUTHOR("Texas Instruments");