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 vb2_queue *q = vb->vb2_queue;
211 struct vpbe_layer *layer = vb2_get_drv_priv(q);
212 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
215 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
216 "vpbe_buffer_prepare\n");
218 if (vb->state != VB2_BUF_STATE_ACTIVE &&
219 vb->state != VB2_BUF_STATE_PREPARED) {
220 vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
221 if (vb2_plane_vaddr(vb, 0) &&
222 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
225 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
227 if (!IS_ALIGNED(addr, 8)) {
228 v4l2_err(&vpbe_dev->v4l2_dev,
229 "buffer_prepare:offset is \
230 not aligned to 32 bytes\n");
239 * vpbe_buffer_setup()
240 * This function allocates memory for the buffers
243 vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
244 unsigned int *nbuffers, unsigned int *nplanes,
245 unsigned int sizes[], void *alloc_ctxs[])
248 /* Get the file handle object and layer object */
249 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
250 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
252 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
254 /* Store number of buffers allocated in numbuffer member */
255 if (*nbuffers < VPBE_DEFAULT_NUM_BUFS)
256 *nbuffers = layer->numbuffers = VPBE_DEFAULT_NUM_BUFS;
259 sizes[0] = layer->pix_fmt.sizeimage;
260 alloc_ctxs[0] = layer->alloc_ctx;
266 * vpbe_buffer_queue()
267 * This function adds the buffer to DMA queue
269 static void vpbe_buffer_queue(struct vb2_buffer *vb)
271 /* Get the file handle object and layer object */
272 struct vpbe_disp_buffer *buf = container_of(vb,
273 struct vpbe_disp_buffer, vb);
274 struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
275 struct vpbe_display *disp = layer->disp_dev;
276 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
279 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
280 "vpbe_buffer_queue\n");
282 /* add the buffer to the DMA queue */
283 spin_lock_irqsave(&disp->dma_queue_lock, flags);
284 list_add_tail(&buf->list, &layer->dma_queue);
285 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
290 * This function is called from the vb2 layer to free memory allocated to
293 static void vpbe_buf_cleanup(struct vb2_buffer *vb)
295 /* Get the file handle object and layer object */
296 struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
297 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
298 struct vpbe_disp_buffer *buf = container_of(vb,
299 struct vpbe_disp_buffer, vb);
302 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
303 "vpbe_buf_cleanup\n");
305 spin_lock_irqsave(&layer->irqlock, flags);
306 if (vb->state == VB2_BUF_STATE_ACTIVE)
307 list_del_init(&buf->list);
308 spin_unlock_irqrestore(&layer->irqlock, flags);
311 static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
313 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
316 /* Get the next frame from the buffer queue */
317 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
318 struct vpbe_disp_buffer, list);
319 /* Remove buffer from the buffer queue */
320 list_del(&layer->cur_frm->list);
321 /* Mark state of the current frame to active */
322 layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
323 /* Initialize field_id and started member */
326 /* Set parameters in OSD and VENC */
327 ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
329 struct vpbe_disp_buffer *buf, *tmp;
331 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_QUEUED);
332 list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
333 list_del(&buf->list);
334 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
341 * if request format is yuv420 semiplanar, need to
342 * enable both video windows
345 layer->layer_first_int = 1;
350 static void vpbe_stop_streaming(struct vb2_queue *vq)
352 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
353 struct vpbe_display *disp = layer->disp_dev;
356 if (!vb2_is_streaming(vq))
359 /* release all active buffers */
360 spin_lock_irqsave(&disp->dma_queue_lock, flags);
361 if (layer->cur_frm == layer->next_frm) {
362 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_ERROR);
364 if (layer->cur_frm != NULL)
365 vb2_buffer_done(&layer->cur_frm->vb,
366 VB2_BUF_STATE_ERROR);
367 if (layer->next_frm != NULL)
368 vb2_buffer_done(&layer->next_frm->vb,
369 VB2_BUF_STATE_ERROR);
372 while (!list_empty(&layer->dma_queue)) {
373 layer->next_frm = list_entry(layer->dma_queue.next,
374 struct vpbe_disp_buffer, list);
375 list_del(&layer->next_frm->list);
376 vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
378 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
381 static struct vb2_ops video_qops = {
382 .queue_setup = vpbe_buffer_queue_setup,
383 .wait_prepare = vb2_ops_wait_prepare,
384 .wait_finish = vb2_ops_wait_finish,
385 .buf_prepare = vpbe_buffer_prepare,
386 .start_streaming = vpbe_start_streaming,
387 .stop_streaming = vpbe_stop_streaming,
388 .buf_cleanup = vpbe_buf_cleanup,
389 .buf_queue = vpbe_buffer_queue,
394 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
395 struct vpbe_layer *layer)
397 enum vpbe_display_device_id thiswin, otherwin;
398 thiswin = layer->device_id;
400 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
401 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
402 return disp_dev->dev[otherwin];
405 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
406 struct vpbe_layer *layer)
408 struct osd_layer_config *cfg = &layer->layer_info.config;
409 struct osd_state *osd_device = disp_dev->osd_device;
410 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
414 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
415 /* Set address in the display registers */
416 osd_device->ops.start_layer(osd_device,
417 layer->layer_info.id,
419 disp_dev->cbcr_ofst);
421 ret = osd_device->ops.enable_layer(osd_device,
422 layer->layer_info.id, 0);
424 v4l2_err(&vpbe_dev->v4l2_dev,
425 "Error in enabling osd window layer 0\n");
429 /* Enable the window */
430 layer->layer_info.enable = 1;
431 if (cfg->pixfmt == PIXFMT_NV12) {
432 struct vpbe_layer *otherlayer =
433 _vpbe_display_get_other_win_layer(disp_dev, layer);
435 ret = osd_device->ops.enable_layer(osd_device,
436 otherlayer->layer_info.id, 1);
438 v4l2_err(&vpbe_dev->v4l2_dev,
439 "Error in enabling osd window layer 1\n");
442 otherlayer->layer_info.enable = 1;
448 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
449 struct vpbe_layer *layer,
450 int expected_xsize, int expected_ysize)
452 struct display_layer_info *layer_info = &layer->layer_info;
453 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
454 struct osd_layer_config *cfg = &layer->layer_info.config;
455 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
456 int calculated_xsize;
462 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
465 * Application initially set the image format. Current display
466 * size is obtained from the vpbe display controller. expected_xsize
467 * and expected_ysize are set through S_CROP ioctl. Based on this,
468 * driver will calculate the scale factors for vertical and
469 * horizontal direction so that the image is displayed scaled
470 * and expanded. Application uses expansion to display the image
471 * in a square pixel. Otherwise it is displayed using displays
472 * pixel aspect ratio.It is expected that application chooses
473 * the crop coordinates for cropped or scaled display. if crop
474 * size is less than the image size, it is displayed cropped or
475 * it is displayed scaled and/or expanded.
477 * to begin with, set the crop window same as expected. Later we
478 * will override with scaled window size
481 cfg->xsize = pixfmt->width;
482 cfg->ysize = pixfmt->height;
483 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
484 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
485 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
486 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
488 if (pixfmt->width < expected_xsize) {
489 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
492 else if (h_scale >= 4)
496 cfg->xsize *= h_scale;
497 if (cfg->xsize < expected_xsize) {
498 if ((standard_id & V4L2_STD_525_60) ||
499 (standard_id & V4L2_STD_625_50)) {
500 calculated_xsize = (cfg->xsize *
501 VPBE_DISPLAY_H_EXP_RATIO_N) /
502 VPBE_DISPLAY_H_EXP_RATIO_D;
503 if (calculated_xsize <= expected_xsize) {
505 cfg->xsize = calculated_xsize;
510 layer_info->h_zoom = ZOOM_X2;
511 else if (h_scale == 4)
512 layer_info->h_zoom = ZOOM_X4;
514 layer_info->h_exp = H_EXP_9_OVER_8;
516 /* no scaling, only cropping. Set display area to crop area */
517 cfg->xsize = expected_xsize;
520 if (pixfmt->height < expected_ysize) {
521 v_scale = expected_ysize / pixfmt->height;
524 else if (v_scale >= 4)
528 cfg->ysize *= v_scale;
529 if (cfg->ysize < expected_ysize) {
530 if ((standard_id & V4L2_STD_625_50)) {
531 calculated_xsize = (cfg->ysize *
532 VPBE_DISPLAY_V_EXP_RATIO_N) /
533 VPBE_DISPLAY_V_EXP_RATIO_D;
534 if (calculated_xsize <= expected_ysize) {
536 cfg->ysize = calculated_xsize;
541 layer_info->v_zoom = ZOOM_X2;
542 else if (v_scale == 4)
543 layer_info->v_zoom = ZOOM_X4;
545 layer_info->h_exp = V_EXP_6_OVER_5;
547 /* no scaling, only cropping. Set display area to crop area */
548 cfg->ysize = expected_ysize;
550 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
551 "crop display xsize = %d, ysize = %d\n",
552 cfg->xsize, cfg->ysize);
555 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
556 struct vpbe_layer *layer,
559 struct osd_layer_config *cfg = &layer->layer_info.config;
560 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
562 cfg->xpos = min((unsigned int)left,
563 vpbe_dev->current_timings.xres - cfg->xsize);
564 cfg->ypos = min((unsigned int)top,
565 vpbe_dev->current_timings.yres - cfg->ysize);
567 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
568 "new xpos = %d, ypos = %d\n",
569 cfg->xpos, cfg->ypos);
572 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
575 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
577 if ((c->width == 0) ||
578 ((c->width + c->left) > vpbe_dev->current_timings.xres))
579 c->width = vpbe_dev->current_timings.xres - c->left;
581 if ((c->height == 0) || ((c->height + c->top) >
582 vpbe_dev->current_timings.yres))
583 c->height = vpbe_dev->current_timings.yres - c->top;
585 /* window height must be even for interlaced display */
586 if (vpbe_dev->current_timings.interlaced)
587 c->height &= (~0x01);
593 * If user application provides width and height, and have bytesperline set
594 * to zero, driver calculates bytesperline and sizeimage based on hardware
597 static int vpbe_try_format(struct vpbe_display *disp_dev,
598 struct v4l2_pix_format *pixfmt, int check)
600 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
607 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
608 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
609 /* choose default as V4L2_PIX_FMT_UYVY */
610 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
612 /* Check the field format */
613 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
614 (pixfmt->field != V4L2_FIELD_NONE)) {
615 if (vpbe_dev->current_timings.interlaced)
616 pixfmt->field = V4L2_FIELD_INTERLACED;
618 pixfmt->field = V4L2_FIELD_NONE;
621 if (pixfmt->field == V4L2_FIELD_INTERLACED)
624 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
629 max_width = vpbe_dev->current_timings.xres;
630 max_height = vpbe_dev->current_timings.yres;
634 if (!pixfmt->width || (pixfmt->width < min_width) ||
635 (pixfmt->width > max_width)) {
636 pixfmt->width = vpbe_dev->current_timings.xres;
639 if (!pixfmt->height || (pixfmt->height < min_height) ||
640 (pixfmt->height > max_height)) {
641 pixfmt->height = vpbe_dev->current_timings.yres;
644 if (pixfmt->bytesperline < (pixfmt->width * bpp))
645 pixfmt->bytesperline = pixfmt->width * bpp;
647 /* Make the bytesperline 32 byte aligned */
648 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
650 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
651 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
652 (pixfmt->bytesperline * pixfmt->height >> 1);
654 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
659 static int vpbe_display_querycap(struct file *file, void *priv,
660 struct v4l2_capability *cap)
662 struct vpbe_fh *fh = file->private_data;
663 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
665 cap->version = VPBE_DISPLAY_VERSION_CODE;
666 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
667 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
668 snprintf(cap->driver, sizeof(cap->driver), "%s",
669 dev_name(vpbe_dev->pdev));
670 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
671 dev_name(vpbe_dev->pdev));
672 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
677 static int vpbe_display_s_crop(struct file *file, void *priv,
678 const struct v4l2_crop *crop)
680 struct vpbe_fh *fh = file->private_data;
681 struct vpbe_layer *layer = fh->layer;
682 struct vpbe_display *disp_dev = fh->disp_dev;
683 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
684 struct osd_layer_config *cfg = &layer->layer_info.config;
685 struct osd_state *osd_device = disp_dev->osd_device;
686 struct v4l2_rect rect = crop->c;
689 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
690 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
692 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
693 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
702 vpbe_disp_check_window_params(disp_dev, &rect);
704 osd_device->ops.get_layer_config(osd_device,
705 layer->layer_info.id, cfg);
707 vpbe_disp_calculate_scale_factor(disp_dev, layer,
710 vpbe_disp_adj_position(disp_dev, layer, rect.top,
712 ret = osd_device->ops.set_layer_config(osd_device,
713 layer->layer_info.id, cfg);
715 v4l2_err(&vpbe_dev->v4l2_dev,
716 "Error in set layer config:\n");
720 /* apply zooming and h or v expansion */
721 osd_device->ops.set_zoom(osd_device,
722 layer->layer_info.id,
723 layer->layer_info.h_zoom,
724 layer->layer_info.v_zoom);
725 ret = osd_device->ops.set_vid_expansion(osd_device,
726 layer->layer_info.h_exp,
727 layer->layer_info.v_exp);
729 v4l2_err(&vpbe_dev->v4l2_dev,
730 "Error in set vid expansion:\n");
734 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
735 (layer->layer_info.v_zoom != ZOOM_X1) ||
736 (layer->layer_info.h_exp != H_EXP_OFF) ||
737 (layer->layer_info.v_exp != V_EXP_OFF))
738 /* Enable expansion filter */
739 osd_device->ops.set_interpolation_filter(osd_device, 1);
741 osd_device->ops.set_interpolation_filter(osd_device, 0);
746 static int vpbe_display_g_crop(struct file *file, void *priv,
747 struct v4l2_crop *crop)
749 struct vpbe_fh *fh = file->private_data;
750 struct vpbe_layer *layer = fh->layer;
751 struct osd_layer_config *cfg = &layer->layer_info.config;
752 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
753 struct osd_state *osd_device = fh->disp_dev->osd_device;
754 struct v4l2_rect *rect = &crop->c;
756 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
757 "VIDIOC_G_CROP, layer id = %d\n",
760 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
761 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
764 osd_device->ops.get_layer_config(osd_device,
765 layer->layer_info.id, cfg);
766 rect->top = cfg->ypos;
767 rect->left = cfg->xpos;
768 rect->width = cfg->xsize;
769 rect->height = cfg->ysize;
774 static int vpbe_display_cropcap(struct file *file, void *priv,
775 struct v4l2_cropcap *cropcap)
777 struct vpbe_fh *fh = file->private_data;
778 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
780 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
782 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
783 cropcap->bounds.left = 0;
784 cropcap->bounds.top = 0;
785 cropcap->bounds.width = vpbe_dev->current_timings.xres;
786 cropcap->bounds.height = vpbe_dev->current_timings.yres;
787 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
788 cropcap->defrect = cropcap->bounds;
792 static int vpbe_display_g_fmt(struct file *file, void *priv,
793 struct v4l2_format *fmt)
795 struct vpbe_fh *fh = file->private_data;
796 struct vpbe_layer *layer = fh->layer;
797 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
799 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
800 "VIDIOC_G_FMT, layer id = %d\n",
803 /* If buffer type is video output */
804 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
805 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
808 /* Fill in the information about format */
809 fmt->fmt.pix = layer->pix_fmt;
814 static int vpbe_display_enum_fmt(struct file *file, void *priv,
815 struct v4l2_fmtdesc *fmt)
817 struct vpbe_fh *fh = file->private_data;
818 struct vpbe_layer *layer = fh->layer;
819 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
820 unsigned int index = 0;
822 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
823 "VIDIOC_ENUM_FMT, layer id = %d\n",
825 if (fmt->index > 1) {
826 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
830 /* Fill in the information about format */
832 memset(fmt, 0, sizeof(*fmt));
834 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
836 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
837 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
839 strcpy(fmt->description, "Y/CbCr 4:2:0");
840 fmt->pixelformat = V4L2_PIX_FMT_NV12;
846 static int vpbe_display_s_fmt(struct file *file, void *priv,
847 struct v4l2_format *fmt)
849 struct vpbe_fh *fh = file->private_data;
850 struct vpbe_layer *layer = fh->layer;
851 struct vpbe_display *disp_dev = fh->disp_dev;
852 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
853 struct osd_layer_config *cfg = &layer->layer_info.config;
854 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
855 struct osd_state *osd_device = disp_dev->osd_device;
858 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
859 "VIDIOC_S_FMT, layer id = %d\n",
862 /* If streaming is started, return error */
863 if (layer->started) {
864 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
867 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
868 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
871 /* Check for valid pixel format */
872 ret = vpbe_try_format(disp_dev, pixfmt, 1);
876 /* YUV420 is requested, check availability of the
877 other video window */
879 layer->pix_fmt = *pixfmt;
880 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
881 struct vpbe_layer *otherlayer;
883 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
884 /* if other layer is available, only
885 * claim it, do not configure it
887 ret = osd_device->ops.request_layer(osd_device,
888 otherlayer->layer_info.id);
890 v4l2_err(&vpbe_dev->v4l2_dev,
891 "Display Manager failed to allocate layer\n");
896 /* Get osd layer config */
897 osd_device->ops.get_layer_config(osd_device,
898 layer->layer_info.id, cfg);
899 /* Store the pixel format in the layer object */
900 cfg->xsize = pixfmt->width;
901 cfg->ysize = pixfmt->height;
902 cfg->line_length = pixfmt->bytesperline;
905 cfg->interlaced = vpbe_dev->current_timings.interlaced;
907 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
908 cfg->pixfmt = PIXFMT_YCBCRI;
910 /* Change of the default pixel format for both video windows */
911 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
912 struct vpbe_layer *otherlayer;
913 cfg->pixfmt = PIXFMT_NV12;
914 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
916 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
919 /* Set the layer config in the osd window */
920 ret = osd_device->ops.set_layer_config(osd_device,
921 layer->layer_info.id, cfg);
923 v4l2_err(&vpbe_dev->v4l2_dev,
924 "Error in S_FMT params:\n");
928 /* Readback and fill the local copy of current pix format */
929 osd_device->ops.get_layer_config(osd_device,
930 layer->layer_info.id, cfg);
935 static int vpbe_display_try_fmt(struct file *file, void *priv,
936 struct v4l2_format *fmt)
938 struct vpbe_fh *fh = file->private_data;
939 struct vpbe_display *disp_dev = fh->disp_dev;
940 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
941 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
943 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
945 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
946 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
950 /* Check for valid field format */
951 return vpbe_try_format(disp_dev, pixfmt, 0);
956 * vpbe_display_s_std - Set the given standard in the encoder
958 * Sets the standard if supported by the current encoder. Return the status.
959 * 0 - success & -EINVAL on error
961 static int vpbe_display_s_std(struct file *file, void *priv,
964 struct vpbe_fh *fh = priv;
965 struct vpbe_layer *layer = fh->layer;
966 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
969 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
971 /* If streaming is started, return error */
972 if (layer->started) {
973 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
976 if (NULL != vpbe_dev->ops.s_std) {
977 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
979 v4l2_err(&vpbe_dev->v4l2_dev,
980 "Failed to set standard for sub devices\n");
991 * vpbe_display_g_std - Get the standard in the current encoder
993 * Get the standard in the current encoder. Return the status. 0 - success
996 static int vpbe_display_g_std(struct file *file, void *priv,
999 struct vpbe_fh *fh = priv;
1000 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1002 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
1004 /* Get the standard from the current encoder */
1005 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
1006 *std_id = vpbe_dev->current_timings.std_id;
1014 * vpbe_display_enum_output - enumerate outputs
1016 * Enumerates the outputs available at the vpbe display
1017 * returns the status, -EINVAL if end of output list
1019 static int vpbe_display_enum_output(struct file *file, void *priv,
1020 struct v4l2_output *output)
1022 struct vpbe_fh *fh = priv;
1023 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1026 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1028 /* Enumerate outputs */
1030 if (NULL == vpbe_dev->ops.enum_outputs)
1033 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1035 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1036 "Failed to enumerate outputs\n");
1044 * vpbe_display_s_output - Set output to
1045 * the output specified by the index
1047 static int vpbe_display_s_output(struct file *file, void *priv,
1050 struct vpbe_fh *fh = priv;
1051 struct vpbe_layer *layer = fh->layer;
1052 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1055 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1056 /* If streaming is started, return error */
1057 if (layer->started) {
1058 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1061 if (NULL == vpbe_dev->ops.set_output)
1064 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1066 v4l2_err(&vpbe_dev->v4l2_dev,
1067 "Failed to set output for sub devices\n");
1075 * vpbe_display_g_output - Get output from subdevice
1076 * for a given by the index
1078 static int vpbe_display_g_output(struct file *file, void *priv,
1081 struct vpbe_fh *fh = priv;
1082 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1084 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1085 /* Get the standard from the current encoder */
1086 *i = vpbe_dev->current_out_index;
1092 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1094 * enum the timings in the current encoder. Return the status. 0 - success
1098 vpbe_display_enum_dv_timings(struct file *file, void *priv,
1099 struct v4l2_enum_dv_timings *timings)
1101 struct vpbe_fh *fh = priv;
1102 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1105 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1107 /* Enumerate outputs */
1108 if (NULL == vpbe_dev->ops.enum_dv_timings)
1111 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1113 v4l2_err(&vpbe_dev->v4l2_dev,
1114 "Failed to enumerate dv timings info\n");
1122 * vpbe_display_s_dv_timings - Set the dv timings
1124 * Set the timings in the current encoder. Return the status. 0 - success
1128 vpbe_display_s_dv_timings(struct file *file, void *priv,
1129 struct v4l2_dv_timings *timings)
1131 struct vpbe_fh *fh = priv;
1132 struct vpbe_layer *layer = fh->layer;
1133 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1136 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1139 /* If streaming is started, return error */
1140 if (layer->started) {
1141 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1145 /* Set the given standard in the encoder */
1146 if (!vpbe_dev->ops.s_dv_timings)
1149 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1151 v4l2_err(&vpbe_dev->v4l2_dev,
1152 "Failed to set the dv timings info\n");
1160 * vpbe_display_g_dv_timings - Set the dv timings
1162 * Get the timings in the current encoder. Return the status. 0 - success
1166 vpbe_display_g_dv_timings(struct file *file, void *priv,
1167 struct v4l2_dv_timings *dv_timings)
1169 struct vpbe_fh *fh = priv;
1170 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1172 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1174 /* Get the given standard in the encoder */
1176 if (vpbe_dev->current_timings.timings_type &
1177 VPBE_ENC_DV_TIMINGS) {
1178 *dv_timings = vpbe_dev->current_timings.dv_timings;
1186 static int vpbe_display_streamoff(struct file *file, void *priv,
1187 enum v4l2_buf_type buf_type)
1189 struct vpbe_fh *fh = file->private_data;
1190 struct vpbe_layer *layer = fh->layer;
1191 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1192 struct osd_state *osd_device = fh->disp_dev->osd_device;
1195 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1196 "VIDIOC_STREAMOFF,layer id = %d\n",
1199 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1200 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1204 /* If io is allowed for this file handle, return error */
1205 if (!fh->io_allowed) {
1206 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1210 /* If streaming is not started, return error */
1211 if (!layer->started) {
1212 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1213 " id = %d\n", layer->device_id);
1217 osd_device->ops.disable_layer(osd_device,
1218 layer->layer_info.id);
1220 ret = vb2_streamoff(&layer->buffer_queue, buf_type);
1225 static int vpbe_display_streamon(struct file *file, void *priv,
1226 enum v4l2_buf_type buf_type)
1228 struct vpbe_fh *fh = file->private_data;
1229 struct vpbe_layer *layer = fh->layer;
1230 struct vpbe_display *disp_dev = fh->disp_dev;
1231 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1232 struct osd_state *osd_device = disp_dev->osd_device;
1235 osd_device->ops.disable_layer(osd_device,
1236 layer->layer_info.id);
1238 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1241 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1242 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1246 /* If file handle is not allowed IO, return error */
1247 if (!fh->io_allowed) {
1248 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1251 /* If Streaming is already started, return error */
1252 if (layer->started) {
1253 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1258 * Call vb2_streamon to start streaming
1261 ret = vb2_streamon(&layer->buffer_queue, buf_type);
1263 v4l2_err(&vpbe_dev->v4l2_dev,
1264 "error in vb2_streamon\n");
1270 static int vpbe_display_dqbuf(struct file *file, void *priv,
1271 struct v4l2_buffer *buf)
1273 struct vpbe_fh *fh = file->private_data;
1274 struct vpbe_layer *layer = fh->layer;
1275 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1278 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1279 "VIDIOC_DQBUF, layer id = %d\n",
1282 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1283 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1286 /* If this file handle is not allowed to do IO, return error */
1287 if (!fh->io_allowed) {
1288 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1291 if (file->f_flags & O_NONBLOCK)
1292 /* Call videobuf_dqbuf for non blocking mode */
1293 ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
1295 /* Call videobuf_dqbuf for blocking mode */
1296 ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
1301 static int vpbe_display_qbuf(struct file *file, void *priv,
1302 struct v4l2_buffer *p)
1304 struct vpbe_fh *fh = file->private_data;
1305 struct vpbe_layer *layer = fh->layer;
1306 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1308 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1309 "VIDIOC_QBUF, layer id = %d\n",
1312 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1313 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1317 /* If this file handle is not allowed to do IO, return error */
1318 if (!fh->io_allowed) {
1319 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1323 return vb2_qbuf(&layer->buffer_queue, p);
1326 static int vpbe_display_querybuf(struct file *file, void *priv,
1327 struct v4l2_buffer *buf)
1329 struct vpbe_fh *fh = file->private_data;
1330 struct vpbe_layer *layer = fh->layer;
1331 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1333 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1334 "VIDIOC_QUERYBUF, layer id = %d\n",
1337 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1338 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1341 /* Call vb2_querybuf to get information */
1342 return vb2_querybuf(&layer->buffer_queue, buf);
1345 static int vpbe_display_reqbufs(struct file *file, void *priv,
1346 struct v4l2_requestbuffers *req_buf)
1348 struct vpbe_fh *fh = file->private_data;
1349 struct vpbe_layer *layer = fh->layer;
1350 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1352 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1354 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1355 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1359 /* If io users of the layer is not zero, return error */
1360 if (0 != layer->io_usrs) {
1361 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1364 /* Set io allowed member of file handle to TRUE */
1366 /* Increment io usrs member of layer object to 1 */
1368 /* Store type of memory requested in layer object */
1369 layer->memory = req_buf->memory;
1370 /* Allocate buffers */
1371 return vb2_reqbufs(&layer->buffer_queue, req_buf);
1375 * vpbe_display_mmap()
1376 * It is used to map kernel space buffers into user spaces
1378 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1380 /* Get the layer object and file handle object */
1381 struct vpbe_fh *fh = filep->private_data;
1382 struct vpbe_layer *layer = fh->layer;
1383 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1386 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1388 if (mutex_lock_interruptible(&layer->opslock))
1389 return -ERESTARTSYS;
1390 ret = vb2_mmap(&layer->buffer_queue, vma);
1391 mutex_unlock(&layer->opslock);
1395 /* vpbe_display_poll(): It is used for select/poll system call
1397 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1399 struct vpbe_fh *fh = filep->private_data;
1400 struct vpbe_layer *layer = fh->layer;
1401 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1402 unsigned int err = 0;
1404 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1405 if (layer->started) {
1406 mutex_lock(&layer->opslock);
1407 err = vb2_poll(&layer->buffer_queue, filep, wait);
1408 mutex_unlock(&layer->opslock);
1414 * vpbe_display_open()
1415 * It creates object of file handle structure and stores it in private_data
1416 * member of filepointer
1418 static int vpbe_display_open(struct file *file)
1420 struct vpbe_fh *fh = NULL;
1421 struct vpbe_layer *layer = video_drvdata(file);
1422 struct video_device *vdev = video_devdata(file);
1423 struct vpbe_display *disp_dev = layer->disp_dev;
1424 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1425 struct osd_state *osd_device = disp_dev->osd_device;
1428 /* Allocate memory for the file handle object */
1429 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1431 v4l2_err(&vpbe_dev->v4l2_dev,
1432 "unable to allocate memory for file handle object\n");
1435 v4l2_fh_init(&fh->fh, vdev);
1436 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1437 "vpbe display open plane = %d\n",
1440 /* store pointer to fh in private_data member of filep */
1441 file->private_data = fh;
1443 fh->disp_dev = disp_dev;
1446 if (mutex_lock_interruptible(&layer->opslock))
1447 return -ERESTARTSYS;
1448 /* First claim the layer for this device */
1449 err = osd_device->ops.request_layer(osd_device,
1450 layer->layer_info.id);
1451 mutex_unlock(&layer->opslock);
1453 /* Couldn't get layer */
1454 v4l2_err(&vpbe_dev->v4l2_dev,
1455 "Display Manager failed to allocate layer\n");
1460 /* Increment layer usrs counter */
1462 /* Set io_allowed member to false */
1464 v4l2_fh_add(&fh->fh);
1465 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1466 "vpbe display device opened successfully\n");
1471 * vpbe_display_release()
1472 * This function deletes buffer queue, frees the buffers and the davinci
1473 * display file * handle
1475 static int vpbe_display_release(struct file *file)
1477 /* Get the layer object and file handle object */
1478 struct vpbe_fh *fh = file->private_data;
1479 struct vpbe_layer *layer = fh->layer;
1480 struct osd_layer_config *cfg = &layer->layer_info.config;
1481 struct vpbe_display *disp_dev = fh->disp_dev;
1482 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1483 struct osd_state *osd_device = disp_dev->osd_device;
1485 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1487 mutex_lock(&layer->opslock);
1488 /* if this instance is doing IO */
1489 if (fh->io_allowed) {
1490 /* Reset io_usrs member of layer object */
1493 osd_device->ops.disable_layer(osd_device,
1494 layer->layer_info.id);
1498 /* Decrement layer usrs counter */
1500 /* If this file handle has initialize encoder device, reset it */
1502 if (cfg->pixfmt == PIXFMT_NV12) {
1503 struct vpbe_layer *otherlayer;
1505 _vpbe_display_get_other_win_layer(disp_dev, layer);
1506 osd_device->ops.disable_layer(osd_device,
1507 otherlayer->layer_info.id);
1508 osd_device->ops.release_layer(osd_device,
1509 otherlayer->layer_info.id);
1511 osd_device->ops.disable_layer(osd_device,
1512 layer->layer_info.id);
1513 osd_device->ops.release_layer(osd_device,
1514 layer->layer_info.id);
1517 v4l2_fh_del(&fh->fh);
1518 v4l2_fh_exit(&fh->fh);
1519 file->private_data = NULL;
1520 mutex_unlock(&layer->opslock);
1522 /* Free memory allocated to file handle object */
1525 disp_dev->cbcr_ofst = 0;
1530 /* vpbe capture ioctl operations */
1531 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1532 .vidioc_querycap = vpbe_display_querycap,
1533 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1534 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1535 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1536 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1537 .vidioc_reqbufs = vpbe_display_reqbufs,
1538 .vidioc_querybuf = vpbe_display_querybuf,
1539 .vidioc_qbuf = vpbe_display_qbuf,
1540 .vidioc_dqbuf = vpbe_display_dqbuf,
1541 .vidioc_streamon = vpbe_display_streamon,
1542 .vidioc_streamoff = vpbe_display_streamoff,
1543 .vidioc_cropcap = vpbe_display_cropcap,
1544 .vidioc_g_crop = vpbe_display_g_crop,
1545 .vidioc_s_crop = vpbe_display_s_crop,
1546 .vidioc_s_std = vpbe_display_s_std,
1547 .vidioc_g_std = vpbe_display_g_std,
1548 .vidioc_enum_output = vpbe_display_enum_output,
1549 .vidioc_s_output = vpbe_display_s_output,
1550 .vidioc_g_output = vpbe_display_g_output,
1551 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1552 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1553 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
1556 static struct v4l2_file_operations vpbe_fops = {
1557 .owner = THIS_MODULE,
1558 .open = vpbe_display_open,
1559 .release = vpbe_display_release,
1560 .unlocked_ioctl = video_ioctl2,
1561 .mmap = vpbe_display_mmap,
1562 .poll = vpbe_display_poll
1565 static int vpbe_device_get(struct device *dev, void *data)
1567 struct platform_device *pdev = to_platform_device(dev);
1568 struct vpbe_display *vpbe_disp = data;
1570 if (strcmp("vpbe_controller", pdev->name) == 0)
1571 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1573 if (strstr(pdev->name, "vpbe-osd") != NULL)
1574 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1579 static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1580 struct platform_device *pdev)
1582 struct vpbe_layer *vpbe_display_layer = NULL;
1583 struct video_device *vbd = NULL;
1585 /* Allocate memory for four plane display objects */
1588 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1590 /* If memory allocation fails, return error */
1591 if (!disp_dev->dev[i]) {
1592 printk(KERN_ERR "ran out of memory\n");
1595 spin_lock_init(&disp_dev->dev[i]->irqlock);
1596 mutex_init(&disp_dev->dev[i]->opslock);
1598 /* Get the pointer to the layer object */
1599 vpbe_display_layer = disp_dev->dev[i];
1600 vbd = &vpbe_display_layer->video_dev;
1601 /* Initialize field of video device */
1602 vbd->release = video_device_release_empty;
1603 vbd->fops = &vpbe_fops;
1604 vbd->ioctl_ops = &vpbe_ioctl_ops;
1606 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1607 vbd->lock = &vpbe_display_layer->opslock;
1608 vbd->vfl_dir = VFL_DIR_TX;
1610 if (disp_dev->vpbe_dev->current_timings.timings_type &
1612 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1614 snprintf(vbd->name, sizeof(vbd->name),
1615 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1616 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1617 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1618 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1620 vpbe_display_layer->device_id = i;
1622 vpbe_display_layer->layer_info.id =
1623 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1629 static int register_device(struct vpbe_layer *vpbe_display_layer,
1630 struct vpbe_display *disp_dev,
1631 struct platform_device *pdev)
1635 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1636 "Trying to register VPBE display device.\n");
1637 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1638 "layer=%x,layer->video_dev=%x\n",
1639 (int)vpbe_display_layer,
1640 (int)&vpbe_display_layer->video_dev);
1642 err = video_register_device(&vpbe_display_layer->video_dev,
1648 vpbe_display_layer->disp_dev = disp_dev;
1649 /* set the driver data in platform device */
1650 platform_set_drvdata(pdev, disp_dev);
1651 video_set_drvdata(&vpbe_display_layer->video_dev,
1652 vpbe_display_layer);
1660 * vpbe_display_probe()
1661 * This function creates device entries by register itself to the V4L2 driver
1662 * and initializes fields of each layer objects
1664 static int vpbe_display_probe(struct platform_device *pdev)
1666 struct vpbe_display *disp_dev;
1667 struct v4l2_device *v4l2_dev;
1668 struct resource *res = NULL;
1669 struct vb2_queue *q;
1675 printk(KERN_DEBUG "vpbe_display_probe\n");
1676 /* Allocate memory for vpbe_display */
1677 disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1682 spin_lock_init(&disp_dev->dma_queue_lock);
1684 * Scan all the platform devices to find the vpbe
1685 * controller device and get the vpbe_dev object
1687 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1692 v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1693 /* Initialize the vpbe display controller */
1694 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1695 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1696 disp_dev->vpbe_dev);
1698 v4l2_err(v4l2_dev, "Error initing vpbe\n");
1704 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1705 if (init_vpbe_layer(i, disp_dev, pdev)) {
1711 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1713 v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
1719 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1720 VPBE_DISPLAY_DRIVER, disp_dev);
1722 v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
1726 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1727 /* initialize vb2 queue */
1728 q = &disp_dev->dev[i]->buffer_queue;
1729 memset(q, 0, sizeof(*q));
1730 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1731 q->io_modes = VB2_MMAP | VB2_USERPTR;
1732 q->drv_priv = disp_dev->dev[i];
1733 q->ops = &video_qops;
1734 q->mem_ops = &vb2_dma_contig_memops;
1735 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1736 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1737 q->min_buffers_needed = 1;
1738 q->lock = &disp_dev->dev[i]->opslock;
1739 err = vb2_queue_init(q);
1741 v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
1745 disp_dev->dev[i]->alloc_ctx =
1746 vb2_dma_contig_init_ctx(disp_dev->vpbe_dev->pdev);
1747 if (IS_ERR(disp_dev->dev[i]->alloc_ctx)) {
1748 v4l2_err(v4l2_dev, "Failed to get the context\n");
1749 err = PTR_ERR(disp_dev->dev[i]->alloc_ctx);
1753 INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
1755 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1761 v4l2_dbg(1, debug, v4l2_dev,
1762 "Successfully completed the probing of vpbe v4l2 device\n");
1767 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1768 /* Unregister video device */
1769 if (disp_dev->dev[k] != NULL) {
1770 vb2_dma_contig_cleanup_ctx(disp_dev->dev[k]->alloc_ctx);
1771 video_unregister_device(&disp_dev->dev[k]->video_dev);
1772 kfree(disp_dev->dev[k]);
1779 * vpbe_display_remove()
1780 * It un-register hardware layer from V4L2 driver
1782 static int vpbe_display_remove(struct platform_device *pdev)
1784 struct vpbe_layer *vpbe_display_layer;
1785 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1786 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1789 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1791 /* deinitialize the vpbe display controller */
1792 if (NULL != vpbe_dev->ops.deinitialize)
1793 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1794 /* un-register device */
1795 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1796 /* Get the pointer to the layer object */
1797 vpbe_display_layer = disp_dev->dev[i];
1798 vb2_dma_contig_cleanup_ctx(vpbe_display_layer->alloc_ctx);
1799 /* Unregister video device */
1800 video_unregister_device(&vpbe_display_layer->video_dev);
1803 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1804 kfree(disp_dev->dev[i]);
1805 disp_dev->dev[i] = NULL;
1811 static struct platform_driver vpbe_display_driver = {
1813 .name = VPBE_DISPLAY_DRIVER,
1814 .owner = THIS_MODULE,
1815 .bus = &platform_bus_type,
1817 .probe = vpbe_display_probe,
1818 .remove = vpbe_display_remove,
1821 module_platform_driver(vpbe_display_driver);
1823 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1824 MODULE_LICENSE("GPL");
1825 MODULE_AUTHOR("Texas Instruments");