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 void 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);
403 static struct vb2_ops video_qops = {
404 .queue_setup = vpbe_buffer_queue_setup,
405 .wait_prepare = vpbe_wait_prepare,
406 .wait_finish = vpbe_wait_finish,
407 .buf_init = vpbe_buffer_init,
408 .buf_prepare = vpbe_buffer_prepare,
409 .start_streaming = vpbe_start_streaming,
410 .stop_streaming = vpbe_stop_streaming,
411 .buf_cleanup = vpbe_buf_cleanup,
412 .buf_queue = vpbe_buffer_queue,
417 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
418 struct vpbe_layer *layer)
420 enum vpbe_display_device_id thiswin, otherwin;
421 thiswin = layer->device_id;
423 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
424 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
425 return disp_dev->dev[otherwin];
428 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
429 struct vpbe_layer *layer)
431 struct osd_layer_config *cfg = &layer->layer_info.config;
432 struct osd_state *osd_device = disp_dev->osd_device;
433 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
437 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
438 /* Set address in the display registers */
439 osd_device->ops.start_layer(osd_device,
440 layer->layer_info.id,
442 disp_dev->cbcr_ofst);
444 ret = osd_device->ops.enable_layer(osd_device,
445 layer->layer_info.id, 0);
447 v4l2_err(&vpbe_dev->v4l2_dev,
448 "Error in enabling osd window layer 0\n");
452 /* Enable the window */
453 layer->layer_info.enable = 1;
454 if (cfg->pixfmt == PIXFMT_NV12) {
455 struct vpbe_layer *otherlayer =
456 _vpbe_display_get_other_win_layer(disp_dev, layer);
458 ret = osd_device->ops.enable_layer(osd_device,
459 otherlayer->layer_info.id, 1);
461 v4l2_err(&vpbe_dev->v4l2_dev,
462 "Error in enabling osd window layer 1\n");
465 otherlayer->layer_info.enable = 1;
471 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
472 struct vpbe_layer *layer,
473 int expected_xsize, int expected_ysize)
475 struct display_layer_info *layer_info = &layer->layer_info;
476 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
477 struct osd_layer_config *cfg = &layer->layer_info.config;
478 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
479 int calculated_xsize;
485 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
488 * Application initially set the image format. Current display
489 * size is obtained from the vpbe display controller. expected_xsize
490 * and expected_ysize are set through S_CROP ioctl. Based on this,
491 * driver will calculate the scale factors for vertical and
492 * horizontal direction so that the image is displayed scaled
493 * and expanded. Application uses expansion to display the image
494 * in a square pixel. Otherwise it is displayed using displays
495 * pixel aspect ratio.It is expected that application chooses
496 * the crop coordinates for cropped or scaled display. if crop
497 * size is less than the image size, it is displayed cropped or
498 * it is displayed scaled and/or expanded.
500 * to begin with, set the crop window same as expected. Later we
501 * will override with scaled window size
504 cfg->xsize = pixfmt->width;
505 cfg->ysize = pixfmt->height;
506 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
507 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
508 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
509 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
511 if (pixfmt->width < expected_xsize) {
512 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
515 else if (h_scale >= 4)
519 cfg->xsize *= h_scale;
520 if (cfg->xsize < expected_xsize) {
521 if ((standard_id & V4L2_STD_525_60) ||
522 (standard_id & V4L2_STD_625_50)) {
523 calculated_xsize = (cfg->xsize *
524 VPBE_DISPLAY_H_EXP_RATIO_N) /
525 VPBE_DISPLAY_H_EXP_RATIO_D;
526 if (calculated_xsize <= expected_xsize) {
528 cfg->xsize = calculated_xsize;
533 layer_info->h_zoom = ZOOM_X2;
534 else if (h_scale == 4)
535 layer_info->h_zoom = ZOOM_X4;
537 layer_info->h_exp = H_EXP_9_OVER_8;
539 /* no scaling, only cropping. Set display area to crop area */
540 cfg->xsize = expected_xsize;
543 if (pixfmt->height < expected_ysize) {
544 v_scale = expected_ysize / pixfmt->height;
547 else if (v_scale >= 4)
551 cfg->ysize *= v_scale;
552 if (cfg->ysize < expected_ysize) {
553 if ((standard_id & V4L2_STD_625_50)) {
554 calculated_xsize = (cfg->ysize *
555 VPBE_DISPLAY_V_EXP_RATIO_N) /
556 VPBE_DISPLAY_V_EXP_RATIO_D;
557 if (calculated_xsize <= expected_ysize) {
559 cfg->ysize = calculated_xsize;
564 layer_info->v_zoom = ZOOM_X2;
565 else if (v_scale == 4)
566 layer_info->v_zoom = ZOOM_X4;
568 layer_info->h_exp = V_EXP_6_OVER_5;
570 /* no scaling, only cropping. Set display area to crop area */
571 cfg->ysize = expected_ysize;
573 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
574 "crop display xsize = %d, ysize = %d\n",
575 cfg->xsize, cfg->ysize);
578 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
579 struct vpbe_layer *layer,
582 struct osd_layer_config *cfg = &layer->layer_info.config;
583 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
585 cfg->xpos = min((unsigned int)left,
586 vpbe_dev->current_timings.xres - cfg->xsize);
587 cfg->ypos = min((unsigned int)top,
588 vpbe_dev->current_timings.yres - cfg->ysize);
590 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
591 "new xpos = %d, ypos = %d\n",
592 cfg->xpos, cfg->ypos);
595 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
598 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
600 if ((c->width == 0) ||
601 ((c->width + c->left) > vpbe_dev->current_timings.xres))
602 c->width = vpbe_dev->current_timings.xres - c->left;
604 if ((c->height == 0) || ((c->height + c->top) >
605 vpbe_dev->current_timings.yres))
606 c->height = vpbe_dev->current_timings.yres - c->top;
608 /* window height must be even for interlaced display */
609 if (vpbe_dev->current_timings.interlaced)
610 c->height &= (~0x01);
616 * If user application provides width and height, and have bytesperline set
617 * to zero, driver calculates bytesperline and sizeimage based on hardware
620 static int vpbe_try_format(struct vpbe_display *disp_dev,
621 struct v4l2_pix_format *pixfmt, int check)
623 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
630 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
631 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
632 /* choose default as V4L2_PIX_FMT_UYVY */
633 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
635 /* Check the field format */
636 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
637 (pixfmt->field != V4L2_FIELD_NONE)) {
638 if (vpbe_dev->current_timings.interlaced)
639 pixfmt->field = V4L2_FIELD_INTERLACED;
641 pixfmt->field = V4L2_FIELD_NONE;
644 if (pixfmt->field == V4L2_FIELD_INTERLACED)
647 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
652 max_width = vpbe_dev->current_timings.xres;
653 max_height = vpbe_dev->current_timings.yres;
657 if (!pixfmt->width || (pixfmt->width < min_width) ||
658 (pixfmt->width > max_width)) {
659 pixfmt->width = vpbe_dev->current_timings.xres;
662 if (!pixfmt->height || (pixfmt->height < min_height) ||
663 (pixfmt->height > max_height)) {
664 pixfmt->height = vpbe_dev->current_timings.yres;
667 if (pixfmt->bytesperline < (pixfmt->width * bpp))
668 pixfmt->bytesperline = pixfmt->width * bpp;
670 /* Make the bytesperline 32 byte aligned */
671 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
673 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
674 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
675 (pixfmt->bytesperline * pixfmt->height >> 1);
677 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
682 static int vpbe_display_querycap(struct file *file, void *priv,
683 struct v4l2_capability *cap)
685 struct vpbe_fh *fh = file->private_data;
686 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
688 cap->version = VPBE_DISPLAY_VERSION_CODE;
689 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
690 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
691 snprintf(cap->driver, sizeof(cap->driver), "%s",
692 dev_name(vpbe_dev->pdev));
693 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
694 dev_name(vpbe_dev->pdev));
695 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
700 static int vpbe_display_s_crop(struct file *file, void *priv,
701 const struct v4l2_crop *crop)
703 struct vpbe_fh *fh = file->private_data;
704 struct vpbe_layer *layer = fh->layer;
705 struct vpbe_display *disp_dev = fh->disp_dev;
706 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
707 struct osd_layer_config *cfg = &layer->layer_info.config;
708 struct osd_state *osd_device = disp_dev->osd_device;
709 struct v4l2_rect rect = crop->c;
712 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
713 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
715 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
716 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
725 vpbe_disp_check_window_params(disp_dev, &rect);
727 osd_device->ops.get_layer_config(osd_device,
728 layer->layer_info.id, cfg);
730 vpbe_disp_calculate_scale_factor(disp_dev, layer,
733 vpbe_disp_adj_position(disp_dev, layer, rect.top,
735 ret = osd_device->ops.set_layer_config(osd_device,
736 layer->layer_info.id, cfg);
738 v4l2_err(&vpbe_dev->v4l2_dev,
739 "Error in set layer config:\n");
743 /* apply zooming and h or v expansion */
744 osd_device->ops.set_zoom(osd_device,
745 layer->layer_info.id,
746 layer->layer_info.h_zoom,
747 layer->layer_info.v_zoom);
748 ret = osd_device->ops.set_vid_expansion(osd_device,
749 layer->layer_info.h_exp,
750 layer->layer_info.v_exp);
752 v4l2_err(&vpbe_dev->v4l2_dev,
753 "Error in set vid expansion:\n");
757 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
758 (layer->layer_info.v_zoom != ZOOM_X1) ||
759 (layer->layer_info.h_exp != H_EXP_OFF) ||
760 (layer->layer_info.v_exp != V_EXP_OFF))
761 /* Enable expansion filter */
762 osd_device->ops.set_interpolation_filter(osd_device, 1);
764 osd_device->ops.set_interpolation_filter(osd_device, 0);
769 static int vpbe_display_g_crop(struct file *file, void *priv,
770 struct v4l2_crop *crop)
772 struct vpbe_fh *fh = file->private_data;
773 struct vpbe_layer *layer = fh->layer;
774 struct osd_layer_config *cfg = &layer->layer_info.config;
775 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
776 struct osd_state *osd_device = fh->disp_dev->osd_device;
777 struct v4l2_rect *rect = &crop->c;
779 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
780 "VIDIOC_G_CROP, layer id = %d\n",
783 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
784 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
787 osd_device->ops.get_layer_config(osd_device,
788 layer->layer_info.id, cfg);
789 rect->top = cfg->ypos;
790 rect->left = cfg->xpos;
791 rect->width = cfg->xsize;
792 rect->height = cfg->ysize;
797 static int vpbe_display_cropcap(struct file *file, void *priv,
798 struct v4l2_cropcap *cropcap)
800 struct vpbe_fh *fh = file->private_data;
801 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
803 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
805 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
806 cropcap->bounds.left = 0;
807 cropcap->bounds.top = 0;
808 cropcap->bounds.width = vpbe_dev->current_timings.xres;
809 cropcap->bounds.height = vpbe_dev->current_timings.yres;
810 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
811 cropcap->defrect = cropcap->bounds;
815 static int vpbe_display_g_fmt(struct file *file, void *priv,
816 struct v4l2_format *fmt)
818 struct vpbe_fh *fh = file->private_data;
819 struct vpbe_layer *layer = fh->layer;
820 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
822 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
823 "VIDIOC_G_FMT, layer id = %d\n",
826 /* If buffer type is video output */
827 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
828 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
831 /* Fill in the information about format */
832 fmt->fmt.pix = layer->pix_fmt;
837 static int vpbe_display_enum_fmt(struct file *file, void *priv,
838 struct v4l2_fmtdesc *fmt)
840 struct vpbe_fh *fh = file->private_data;
841 struct vpbe_layer *layer = fh->layer;
842 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
843 unsigned int index = 0;
845 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
846 "VIDIOC_ENUM_FMT, layer id = %d\n",
848 if (fmt->index > 1) {
849 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
853 /* Fill in the information about format */
855 memset(fmt, 0, sizeof(*fmt));
857 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
859 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
860 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
862 strcpy(fmt->description, "Y/CbCr 4:2:0");
863 fmt->pixelformat = V4L2_PIX_FMT_NV12;
869 static int vpbe_display_s_fmt(struct file *file, void *priv,
870 struct v4l2_format *fmt)
872 struct vpbe_fh *fh = file->private_data;
873 struct vpbe_layer *layer = fh->layer;
874 struct vpbe_display *disp_dev = fh->disp_dev;
875 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
876 struct osd_layer_config *cfg = &layer->layer_info.config;
877 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
878 struct osd_state *osd_device = disp_dev->osd_device;
881 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
882 "VIDIOC_S_FMT, layer id = %d\n",
885 /* If streaming is started, return error */
886 if (layer->started) {
887 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
890 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
891 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
894 /* Check for valid pixel format */
895 ret = vpbe_try_format(disp_dev, pixfmt, 1);
899 /* YUV420 is requested, check availability of the
900 other video window */
902 layer->pix_fmt = *pixfmt;
903 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
904 struct vpbe_layer *otherlayer;
906 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
907 /* if other layer is available, only
908 * claim it, do not configure it
910 ret = osd_device->ops.request_layer(osd_device,
911 otherlayer->layer_info.id);
913 v4l2_err(&vpbe_dev->v4l2_dev,
914 "Display Manager failed to allocate layer\n");
919 /* Get osd layer config */
920 osd_device->ops.get_layer_config(osd_device,
921 layer->layer_info.id, cfg);
922 /* Store the pixel format in the layer object */
923 cfg->xsize = pixfmt->width;
924 cfg->ysize = pixfmt->height;
925 cfg->line_length = pixfmt->bytesperline;
928 cfg->interlaced = vpbe_dev->current_timings.interlaced;
930 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
931 cfg->pixfmt = PIXFMT_YCBCRI;
933 /* Change of the default pixel format for both video windows */
934 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
935 struct vpbe_layer *otherlayer;
936 cfg->pixfmt = PIXFMT_NV12;
937 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
939 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
942 /* Set the layer config in the osd window */
943 ret = osd_device->ops.set_layer_config(osd_device,
944 layer->layer_info.id, cfg);
946 v4l2_err(&vpbe_dev->v4l2_dev,
947 "Error in S_FMT params:\n");
951 /* Readback and fill the local copy of current pix format */
952 osd_device->ops.get_layer_config(osd_device,
953 layer->layer_info.id, cfg);
958 static int vpbe_display_try_fmt(struct file *file, void *priv,
959 struct v4l2_format *fmt)
961 struct vpbe_fh *fh = file->private_data;
962 struct vpbe_display *disp_dev = fh->disp_dev;
963 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
964 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
966 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
968 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
969 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
973 /* Check for valid field format */
974 return vpbe_try_format(disp_dev, pixfmt, 0);
979 * vpbe_display_s_std - Set the given standard in the encoder
981 * Sets the standard if supported by the current encoder. Return the status.
982 * 0 - success & -EINVAL on error
984 static int vpbe_display_s_std(struct file *file, void *priv,
987 struct vpbe_fh *fh = priv;
988 struct vpbe_layer *layer = fh->layer;
989 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
992 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
994 /* If streaming is started, return error */
995 if (layer->started) {
996 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
999 if (NULL != vpbe_dev->ops.s_std) {
1000 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
1002 v4l2_err(&vpbe_dev->v4l2_dev,
1003 "Failed to set standard for sub devices\n");
1014 * vpbe_display_g_std - Get the standard in the current encoder
1016 * Get the standard in the current encoder. Return the status. 0 - success
1019 static int vpbe_display_g_std(struct file *file, void *priv,
1020 v4l2_std_id *std_id)
1022 struct vpbe_fh *fh = priv;
1023 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1025 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
1027 /* Get the standard from the current encoder */
1028 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
1029 *std_id = vpbe_dev->current_timings.std_id;
1037 * vpbe_display_enum_output - enumerate outputs
1039 * Enumerates the outputs available at the vpbe display
1040 * returns the status, -EINVAL if end of output list
1042 static int vpbe_display_enum_output(struct file *file, void *priv,
1043 struct v4l2_output *output)
1045 struct vpbe_fh *fh = priv;
1046 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1049 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1051 /* Enumerate outputs */
1053 if (NULL == vpbe_dev->ops.enum_outputs)
1056 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1058 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1059 "Failed to enumerate outputs\n");
1067 * vpbe_display_s_output - Set output to
1068 * the output specified by the index
1070 static int vpbe_display_s_output(struct file *file, void *priv,
1073 struct vpbe_fh *fh = priv;
1074 struct vpbe_layer *layer = fh->layer;
1075 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1078 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1079 /* If streaming is started, return error */
1080 if (layer->started) {
1081 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1084 if (NULL == vpbe_dev->ops.set_output)
1087 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1089 v4l2_err(&vpbe_dev->v4l2_dev,
1090 "Failed to set output for sub devices\n");
1098 * vpbe_display_g_output - Get output from subdevice
1099 * for a given by the index
1101 static int vpbe_display_g_output(struct file *file, void *priv,
1104 struct vpbe_fh *fh = priv;
1105 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1107 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1108 /* Get the standard from the current encoder */
1109 *i = vpbe_dev->current_out_index;
1115 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1117 * enum the timings in the current encoder. Return the status. 0 - success
1121 vpbe_display_enum_dv_timings(struct file *file, void *priv,
1122 struct v4l2_enum_dv_timings *timings)
1124 struct vpbe_fh *fh = priv;
1125 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1128 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1130 /* Enumerate outputs */
1131 if (NULL == vpbe_dev->ops.enum_dv_timings)
1134 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1136 v4l2_err(&vpbe_dev->v4l2_dev,
1137 "Failed to enumerate dv timings info\n");
1145 * vpbe_display_s_dv_timings - Set the dv timings
1147 * Set the timings in the current encoder. Return the status. 0 - success
1151 vpbe_display_s_dv_timings(struct file *file, void *priv,
1152 struct v4l2_dv_timings *timings)
1154 struct vpbe_fh *fh = priv;
1155 struct vpbe_layer *layer = fh->layer;
1156 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1159 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1162 /* If streaming is started, return error */
1163 if (layer->started) {
1164 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1168 /* Set the given standard in the encoder */
1169 if (!vpbe_dev->ops.s_dv_timings)
1172 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1174 v4l2_err(&vpbe_dev->v4l2_dev,
1175 "Failed to set the dv timings info\n");
1183 * vpbe_display_g_dv_timings - Set the dv timings
1185 * Get the timings in the current encoder. Return the status. 0 - success
1189 vpbe_display_g_dv_timings(struct file *file, void *priv,
1190 struct v4l2_dv_timings *dv_timings)
1192 struct vpbe_fh *fh = priv;
1193 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1195 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1197 /* Get the given standard in the encoder */
1199 if (vpbe_dev->current_timings.timings_type &
1200 VPBE_ENC_DV_TIMINGS) {
1201 *dv_timings = vpbe_dev->current_timings.dv_timings;
1209 static int vpbe_display_streamoff(struct file *file, void *priv,
1210 enum v4l2_buf_type buf_type)
1212 struct vpbe_fh *fh = file->private_data;
1213 struct vpbe_layer *layer = fh->layer;
1214 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1215 struct osd_state *osd_device = fh->disp_dev->osd_device;
1218 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1219 "VIDIOC_STREAMOFF,layer id = %d\n",
1222 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1223 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1227 /* If io is allowed for this file handle, return error */
1228 if (!fh->io_allowed) {
1229 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1233 /* If streaming is not started, return error */
1234 if (!layer->started) {
1235 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1236 " id = %d\n", layer->device_id);
1240 osd_device->ops.disable_layer(osd_device,
1241 layer->layer_info.id);
1243 ret = vb2_streamoff(&layer->buffer_queue, buf_type);
1248 static int vpbe_display_streamon(struct file *file, void *priv,
1249 enum v4l2_buf_type buf_type)
1251 struct vpbe_fh *fh = file->private_data;
1252 struct vpbe_layer *layer = fh->layer;
1253 struct vpbe_display *disp_dev = fh->disp_dev;
1254 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1255 struct osd_state *osd_device = disp_dev->osd_device;
1258 osd_device->ops.disable_layer(osd_device,
1259 layer->layer_info.id);
1261 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1264 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1265 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1269 /* If file handle is not allowed IO, return error */
1270 if (!fh->io_allowed) {
1271 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1274 /* If Streaming is already started, return error */
1275 if (layer->started) {
1276 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1281 * Call vb2_streamon to start streaming
1284 ret = vb2_streamon(&layer->buffer_queue, buf_type);
1286 v4l2_err(&vpbe_dev->v4l2_dev,
1287 "error in vb2_streamon\n");
1293 static int vpbe_display_dqbuf(struct file *file, void *priv,
1294 struct v4l2_buffer *buf)
1296 struct vpbe_fh *fh = file->private_data;
1297 struct vpbe_layer *layer = fh->layer;
1298 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1301 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1302 "VIDIOC_DQBUF, layer id = %d\n",
1305 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1306 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1309 /* If this file handle is not allowed to do IO, return error */
1310 if (!fh->io_allowed) {
1311 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1314 if (file->f_flags & O_NONBLOCK)
1315 /* Call videobuf_dqbuf for non blocking mode */
1316 ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
1318 /* Call videobuf_dqbuf for blocking mode */
1319 ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
1324 static int vpbe_display_qbuf(struct file *file, void *priv,
1325 struct v4l2_buffer *p)
1327 struct vpbe_fh *fh = file->private_data;
1328 struct vpbe_layer *layer = fh->layer;
1329 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1331 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1332 "VIDIOC_QBUF, layer id = %d\n",
1335 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1336 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1340 /* If this file handle is not allowed to do IO, return error */
1341 if (!fh->io_allowed) {
1342 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1346 return vb2_qbuf(&layer->buffer_queue, p);
1349 static int vpbe_display_querybuf(struct file *file, void *priv,
1350 struct v4l2_buffer *buf)
1352 struct vpbe_fh *fh = file->private_data;
1353 struct vpbe_layer *layer = fh->layer;
1354 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1356 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1357 "VIDIOC_QUERYBUF, layer id = %d\n",
1360 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1361 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1364 /* Call vb2_querybuf to get information */
1365 return vb2_querybuf(&layer->buffer_queue, buf);
1368 static int vpbe_display_reqbufs(struct file *file, void *priv,
1369 struct v4l2_requestbuffers *req_buf)
1371 struct vpbe_fh *fh = file->private_data;
1372 struct vpbe_layer *layer = fh->layer;
1373 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1374 struct vb2_queue *q;
1376 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1378 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1379 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1383 /* If io users of the layer is not zero, return error */
1384 if (0 != layer->io_usrs) {
1385 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1388 /* Initialize videobuf queue as per the buffer type */
1389 layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
1390 if (IS_ERR(layer->alloc_ctx)) {
1391 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
1392 return PTR_ERR(layer->alloc_ctx);
1394 q = &layer->buffer_queue;
1395 memset(q, 0, sizeof(*q));
1396 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1397 q->io_modes = VB2_MMAP | VB2_USERPTR;
1399 q->ops = &video_qops;
1400 q->mem_ops = &vb2_dma_contig_memops;
1401 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1402 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1403 q->min_buffers_needed = 1;
1405 ret = vb2_queue_init(q);
1407 v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1408 vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
1411 /* Set io allowed member of file handle to TRUE */
1413 /* Increment io usrs member of layer object to 1 */
1415 /* Store type of memory requested in layer object */
1416 layer->memory = req_buf->memory;
1417 /* Initialize buffer queue */
1418 INIT_LIST_HEAD(&layer->dma_queue);
1419 /* Allocate buffers */
1420 return vb2_reqbufs(q, req_buf);
1424 * vpbe_display_mmap()
1425 * It is used to map kernel space buffers into user spaces
1427 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1429 /* Get the layer object and file handle object */
1430 struct vpbe_fh *fh = filep->private_data;
1431 struct vpbe_layer *layer = fh->layer;
1432 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1435 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1437 if (mutex_lock_interruptible(&layer->opslock))
1438 return -ERESTARTSYS;
1439 ret = vb2_mmap(&layer->buffer_queue, vma);
1440 mutex_unlock(&layer->opslock);
1444 /* vpbe_display_poll(): It is used for select/poll system call
1446 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1448 struct vpbe_fh *fh = filep->private_data;
1449 struct vpbe_layer *layer = fh->layer;
1450 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1451 unsigned int err = 0;
1453 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1454 if (layer->started) {
1455 mutex_lock(&layer->opslock);
1456 err = vb2_poll(&layer->buffer_queue, filep, wait);
1457 mutex_unlock(&layer->opslock);
1463 * vpbe_display_open()
1464 * It creates object of file handle structure and stores it in private_data
1465 * member of filepointer
1467 static int vpbe_display_open(struct file *file)
1469 struct vpbe_fh *fh = NULL;
1470 struct vpbe_layer *layer = video_drvdata(file);
1471 struct video_device *vdev = video_devdata(file);
1472 struct vpbe_display *disp_dev = layer->disp_dev;
1473 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1474 struct osd_state *osd_device = disp_dev->osd_device;
1477 /* Allocate memory for the file handle object */
1478 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1480 v4l2_err(&vpbe_dev->v4l2_dev,
1481 "unable to allocate memory for file handle object\n");
1484 v4l2_fh_init(&fh->fh, vdev);
1485 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1486 "vpbe display open plane = %d\n",
1489 /* store pointer to fh in private_data member of filep */
1490 file->private_data = fh;
1492 fh->disp_dev = disp_dev;
1495 if (mutex_lock_interruptible(&layer->opslock))
1496 return -ERESTARTSYS;
1497 /* First claim the layer for this device */
1498 err = osd_device->ops.request_layer(osd_device,
1499 layer->layer_info.id);
1500 mutex_unlock(&layer->opslock);
1502 /* Couldn't get layer */
1503 v4l2_err(&vpbe_dev->v4l2_dev,
1504 "Display Manager failed to allocate layer\n");
1509 /* Increment layer usrs counter */
1511 /* Set io_allowed member to false */
1513 v4l2_fh_add(&fh->fh);
1514 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1515 "vpbe display device opened successfully\n");
1520 * vpbe_display_release()
1521 * This function deletes buffer queue, frees the buffers and the davinci
1522 * display file * handle
1524 static int vpbe_display_release(struct file *file)
1526 /* Get the layer object and file handle object */
1527 struct vpbe_fh *fh = file->private_data;
1528 struct vpbe_layer *layer = fh->layer;
1529 struct osd_layer_config *cfg = &layer->layer_info.config;
1530 struct vpbe_display *disp_dev = fh->disp_dev;
1531 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1532 struct osd_state *osd_device = disp_dev->osd_device;
1534 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1536 mutex_lock(&layer->opslock);
1537 /* if this instance is doing IO */
1538 if (fh->io_allowed) {
1539 /* Reset io_usrs member of layer object */
1542 osd_device->ops.disable_layer(osd_device,
1543 layer->layer_info.id);
1545 /* Free buffers allocated */
1546 vb2_queue_release(&layer->buffer_queue);
1547 vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
1550 /* Decrement layer usrs counter */
1552 /* If this file handle has initialize encoder device, reset it */
1554 if (cfg->pixfmt == PIXFMT_NV12) {
1555 struct vpbe_layer *otherlayer;
1557 _vpbe_display_get_other_win_layer(disp_dev, layer);
1558 osd_device->ops.disable_layer(osd_device,
1559 otherlayer->layer_info.id);
1560 osd_device->ops.release_layer(osd_device,
1561 otherlayer->layer_info.id);
1563 osd_device->ops.disable_layer(osd_device,
1564 layer->layer_info.id);
1565 osd_device->ops.release_layer(osd_device,
1566 layer->layer_info.id);
1569 v4l2_fh_del(&fh->fh);
1570 v4l2_fh_exit(&fh->fh);
1571 file->private_data = NULL;
1572 mutex_unlock(&layer->opslock);
1574 /* Free memory allocated to file handle object */
1577 disp_dev->cbcr_ofst = 0;
1582 /* vpbe capture ioctl operations */
1583 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1584 .vidioc_querycap = vpbe_display_querycap,
1585 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1586 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1587 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1588 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1589 .vidioc_reqbufs = vpbe_display_reqbufs,
1590 .vidioc_querybuf = vpbe_display_querybuf,
1591 .vidioc_qbuf = vpbe_display_qbuf,
1592 .vidioc_dqbuf = vpbe_display_dqbuf,
1593 .vidioc_streamon = vpbe_display_streamon,
1594 .vidioc_streamoff = vpbe_display_streamoff,
1595 .vidioc_cropcap = vpbe_display_cropcap,
1596 .vidioc_g_crop = vpbe_display_g_crop,
1597 .vidioc_s_crop = vpbe_display_s_crop,
1598 .vidioc_s_std = vpbe_display_s_std,
1599 .vidioc_g_std = vpbe_display_g_std,
1600 .vidioc_enum_output = vpbe_display_enum_output,
1601 .vidioc_s_output = vpbe_display_s_output,
1602 .vidioc_g_output = vpbe_display_g_output,
1603 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1604 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1605 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
1608 static struct v4l2_file_operations vpbe_fops = {
1609 .owner = THIS_MODULE,
1610 .open = vpbe_display_open,
1611 .release = vpbe_display_release,
1612 .unlocked_ioctl = video_ioctl2,
1613 .mmap = vpbe_display_mmap,
1614 .poll = vpbe_display_poll
1617 static int vpbe_device_get(struct device *dev, void *data)
1619 struct platform_device *pdev = to_platform_device(dev);
1620 struct vpbe_display *vpbe_disp = data;
1622 if (strcmp("vpbe_controller", pdev->name) == 0)
1623 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1625 if (strstr(pdev->name, "vpbe-osd") != NULL)
1626 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1631 static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1632 struct platform_device *pdev)
1634 struct vpbe_layer *vpbe_display_layer = NULL;
1635 struct video_device *vbd = NULL;
1637 /* Allocate memory for four plane display objects */
1640 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1642 /* If memory allocation fails, return error */
1643 if (!disp_dev->dev[i]) {
1644 printk(KERN_ERR "ran out of memory\n");
1647 spin_lock_init(&disp_dev->dev[i]->irqlock);
1648 mutex_init(&disp_dev->dev[i]->opslock);
1650 /* Get the pointer to the layer object */
1651 vpbe_display_layer = disp_dev->dev[i];
1652 vbd = &vpbe_display_layer->video_dev;
1653 /* Initialize field of video device */
1654 vbd->release = video_device_release_empty;
1655 vbd->fops = &vpbe_fops;
1656 vbd->ioctl_ops = &vpbe_ioctl_ops;
1658 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1659 vbd->lock = &vpbe_display_layer->opslock;
1660 vbd->vfl_dir = VFL_DIR_TX;
1662 if (disp_dev->vpbe_dev->current_timings.timings_type &
1664 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1666 snprintf(vbd->name, sizeof(vbd->name),
1667 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1668 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1669 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1670 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1672 vpbe_display_layer->device_id = i;
1674 vpbe_display_layer->layer_info.id =
1675 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1681 static int register_device(struct vpbe_layer *vpbe_display_layer,
1682 struct vpbe_display *disp_dev,
1683 struct platform_device *pdev)
1687 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1688 "Trying to register VPBE display device.\n");
1689 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1690 "layer=%x,layer->video_dev=%x\n",
1691 (int)vpbe_display_layer,
1692 (int)&vpbe_display_layer->video_dev);
1694 err = video_register_device(&vpbe_display_layer->video_dev,
1700 vpbe_display_layer->disp_dev = disp_dev;
1701 /* set the driver data in platform device */
1702 platform_set_drvdata(pdev, disp_dev);
1703 set_bit(V4L2_FL_USE_FH_PRIO, &vpbe_display_layer->video_dev.flags);
1704 video_set_drvdata(&vpbe_display_layer->video_dev,
1705 vpbe_display_layer);
1713 * vpbe_display_probe()
1714 * This function creates device entries by register itself to the V4L2 driver
1715 * and initializes fields of each layer objects
1717 static int vpbe_display_probe(struct platform_device *pdev)
1719 struct vpbe_layer *vpbe_display_layer;
1720 struct vpbe_display *disp_dev;
1721 struct resource *res = NULL;
1727 printk(KERN_DEBUG "vpbe_display_probe\n");
1728 /* Allocate memory for vpbe_display */
1729 disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1734 spin_lock_init(&disp_dev->dma_queue_lock);
1736 * Scan all the platform devices to find the vpbe
1737 * controller device and get the vpbe_dev object
1739 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1743 /* Initialize the vpbe display controller */
1744 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1745 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1746 disp_dev->vpbe_dev);
1748 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1749 "Error initing vpbe\n");
1755 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1756 if (init_vpbe_layer(i, disp_dev, pdev)) {
1762 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1764 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1765 "Unable to get VENC interrupt resource\n");
1771 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1772 VPBE_DISPLAY_DRIVER, disp_dev);
1774 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1775 "Unable to request interrupt\n");
1779 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1780 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1786 printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
1790 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1791 /* Get the pointer to the layer object */
1792 vpbe_display_layer = disp_dev->dev[k];
1793 /* Unregister video device */
1794 if (vpbe_display_layer) {
1795 video_unregister_device(
1796 &vpbe_display_layer->video_dev);
1797 kfree(disp_dev->dev[k]);
1804 * vpbe_display_remove()
1805 * It un-register hardware layer from V4L2 driver
1807 static int vpbe_display_remove(struct platform_device *pdev)
1809 struct vpbe_layer *vpbe_display_layer;
1810 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1811 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1814 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1816 /* deinitialize the vpbe display controller */
1817 if (NULL != vpbe_dev->ops.deinitialize)
1818 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1819 /* un-register device */
1820 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1821 /* Get the pointer to the layer object */
1822 vpbe_display_layer = disp_dev->dev[i];
1823 /* Unregister video device */
1824 video_unregister_device(&vpbe_display_layer->video_dev);
1827 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1828 kfree(disp_dev->dev[i]);
1829 disp_dev->dev[i] = NULL;
1835 static struct platform_driver vpbe_display_driver = {
1837 .name = VPBE_DISPLAY_DRIVER,
1838 .owner = THIS_MODULE,
1839 .bus = &platform_bus_type,
1841 .probe = vpbe_display_probe,
1842 .remove = vpbe_display_remove,
1845 module_platform_driver(vpbe_display_driver);
1847 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1848 MODULE_LICENSE("GPL");
1849 MODULE_AUTHOR("Texas Instruments");