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 void vpbe_wait_prepare(struct vb2_queue *vq)
313 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
315 mutex_unlock(&layer->opslock);
318 static void vpbe_wait_finish(struct vb2_queue *vq)
320 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
322 mutex_lock(&layer->opslock);
325 static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
327 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
330 /* Get the next frame from the buffer queue */
331 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
332 struct vpbe_disp_buffer, list);
333 /* Remove buffer from the buffer queue */
334 list_del(&layer->cur_frm->list);
335 /* Mark state of the current frame to active */
336 layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
337 /* Initialize field_id and started member */
340 /* Set parameters in OSD and VENC */
341 ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
343 struct vpbe_disp_buffer *buf, *tmp;
345 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_QUEUED);
346 list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
347 list_del(&buf->list);
348 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
355 * if request format is yuv420 semiplanar, need to
356 * enable both video windows
359 layer->layer_first_int = 1;
364 static void vpbe_stop_streaming(struct vb2_queue *vq)
366 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
367 struct vpbe_display *disp = layer->disp_dev;
370 if (!vb2_is_streaming(vq))
373 /* release all active buffers */
374 spin_lock_irqsave(&disp->dma_queue_lock, flags);
375 if (layer->cur_frm == layer->next_frm) {
376 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_ERROR);
378 if (layer->cur_frm != NULL)
379 vb2_buffer_done(&layer->cur_frm->vb,
380 VB2_BUF_STATE_ERROR);
381 if (layer->next_frm != NULL)
382 vb2_buffer_done(&layer->next_frm->vb,
383 VB2_BUF_STATE_ERROR);
386 while (!list_empty(&layer->dma_queue)) {
387 layer->next_frm = list_entry(layer->dma_queue.next,
388 struct vpbe_disp_buffer, list);
389 list_del(&layer->next_frm->list);
390 vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
392 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
395 static struct vb2_ops video_qops = {
396 .queue_setup = vpbe_buffer_queue_setup,
397 .wait_prepare = vpbe_wait_prepare,
398 .wait_finish = vpbe_wait_finish,
399 .buf_prepare = vpbe_buffer_prepare,
400 .start_streaming = vpbe_start_streaming,
401 .stop_streaming = vpbe_stop_streaming,
402 .buf_cleanup = vpbe_buf_cleanup,
403 .buf_queue = vpbe_buffer_queue,
408 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
409 struct vpbe_layer *layer)
411 enum vpbe_display_device_id thiswin, otherwin;
412 thiswin = layer->device_id;
414 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
415 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
416 return disp_dev->dev[otherwin];
419 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
420 struct vpbe_layer *layer)
422 struct osd_layer_config *cfg = &layer->layer_info.config;
423 struct osd_state *osd_device = disp_dev->osd_device;
424 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
428 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
429 /* Set address in the display registers */
430 osd_device->ops.start_layer(osd_device,
431 layer->layer_info.id,
433 disp_dev->cbcr_ofst);
435 ret = osd_device->ops.enable_layer(osd_device,
436 layer->layer_info.id, 0);
438 v4l2_err(&vpbe_dev->v4l2_dev,
439 "Error in enabling osd window layer 0\n");
443 /* Enable the window */
444 layer->layer_info.enable = 1;
445 if (cfg->pixfmt == PIXFMT_NV12) {
446 struct vpbe_layer *otherlayer =
447 _vpbe_display_get_other_win_layer(disp_dev, layer);
449 ret = osd_device->ops.enable_layer(osd_device,
450 otherlayer->layer_info.id, 1);
452 v4l2_err(&vpbe_dev->v4l2_dev,
453 "Error in enabling osd window layer 1\n");
456 otherlayer->layer_info.enable = 1;
462 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
463 struct vpbe_layer *layer,
464 int expected_xsize, int expected_ysize)
466 struct display_layer_info *layer_info = &layer->layer_info;
467 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
468 struct osd_layer_config *cfg = &layer->layer_info.config;
469 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
470 int calculated_xsize;
476 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
479 * Application initially set the image format. Current display
480 * size is obtained from the vpbe display controller. expected_xsize
481 * and expected_ysize are set through S_CROP ioctl. Based on this,
482 * driver will calculate the scale factors for vertical and
483 * horizontal direction so that the image is displayed scaled
484 * and expanded. Application uses expansion to display the image
485 * in a square pixel. Otherwise it is displayed using displays
486 * pixel aspect ratio.It is expected that application chooses
487 * the crop coordinates for cropped or scaled display. if crop
488 * size is less than the image size, it is displayed cropped or
489 * it is displayed scaled and/or expanded.
491 * to begin with, set the crop window same as expected. Later we
492 * will override with scaled window size
495 cfg->xsize = pixfmt->width;
496 cfg->ysize = pixfmt->height;
497 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
498 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
499 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
500 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
502 if (pixfmt->width < expected_xsize) {
503 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
506 else if (h_scale >= 4)
510 cfg->xsize *= h_scale;
511 if (cfg->xsize < expected_xsize) {
512 if ((standard_id & V4L2_STD_525_60) ||
513 (standard_id & V4L2_STD_625_50)) {
514 calculated_xsize = (cfg->xsize *
515 VPBE_DISPLAY_H_EXP_RATIO_N) /
516 VPBE_DISPLAY_H_EXP_RATIO_D;
517 if (calculated_xsize <= expected_xsize) {
519 cfg->xsize = calculated_xsize;
524 layer_info->h_zoom = ZOOM_X2;
525 else if (h_scale == 4)
526 layer_info->h_zoom = ZOOM_X4;
528 layer_info->h_exp = H_EXP_9_OVER_8;
530 /* no scaling, only cropping. Set display area to crop area */
531 cfg->xsize = expected_xsize;
534 if (pixfmt->height < expected_ysize) {
535 v_scale = expected_ysize / pixfmt->height;
538 else if (v_scale >= 4)
542 cfg->ysize *= v_scale;
543 if (cfg->ysize < expected_ysize) {
544 if ((standard_id & V4L2_STD_625_50)) {
545 calculated_xsize = (cfg->ysize *
546 VPBE_DISPLAY_V_EXP_RATIO_N) /
547 VPBE_DISPLAY_V_EXP_RATIO_D;
548 if (calculated_xsize <= expected_ysize) {
550 cfg->ysize = calculated_xsize;
555 layer_info->v_zoom = ZOOM_X2;
556 else if (v_scale == 4)
557 layer_info->v_zoom = ZOOM_X4;
559 layer_info->h_exp = V_EXP_6_OVER_5;
561 /* no scaling, only cropping. Set display area to crop area */
562 cfg->ysize = expected_ysize;
564 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
565 "crop display xsize = %d, ysize = %d\n",
566 cfg->xsize, cfg->ysize);
569 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
570 struct vpbe_layer *layer,
573 struct osd_layer_config *cfg = &layer->layer_info.config;
574 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
576 cfg->xpos = min((unsigned int)left,
577 vpbe_dev->current_timings.xres - cfg->xsize);
578 cfg->ypos = min((unsigned int)top,
579 vpbe_dev->current_timings.yres - cfg->ysize);
581 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
582 "new xpos = %d, ypos = %d\n",
583 cfg->xpos, cfg->ypos);
586 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
589 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
591 if ((c->width == 0) ||
592 ((c->width + c->left) > vpbe_dev->current_timings.xres))
593 c->width = vpbe_dev->current_timings.xres - c->left;
595 if ((c->height == 0) || ((c->height + c->top) >
596 vpbe_dev->current_timings.yres))
597 c->height = vpbe_dev->current_timings.yres - c->top;
599 /* window height must be even for interlaced display */
600 if (vpbe_dev->current_timings.interlaced)
601 c->height &= (~0x01);
607 * If user application provides width and height, and have bytesperline set
608 * to zero, driver calculates bytesperline and sizeimage based on hardware
611 static int vpbe_try_format(struct vpbe_display *disp_dev,
612 struct v4l2_pix_format *pixfmt, int check)
614 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
621 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
622 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
623 /* choose default as V4L2_PIX_FMT_UYVY */
624 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
626 /* Check the field format */
627 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
628 (pixfmt->field != V4L2_FIELD_NONE)) {
629 if (vpbe_dev->current_timings.interlaced)
630 pixfmt->field = V4L2_FIELD_INTERLACED;
632 pixfmt->field = V4L2_FIELD_NONE;
635 if (pixfmt->field == V4L2_FIELD_INTERLACED)
638 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
643 max_width = vpbe_dev->current_timings.xres;
644 max_height = vpbe_dev->current_timings.yres;
648 if (!pixfmt->width || (pixfmt->width < min_width) ||
649 (pixfmt->width > max_width)) {
650 pixfmt->width = vpbe_dev->current_timings.xres;
653 if (!pixfmt->height || (pixfmt->height < min_height) ||
654 (pixfmt->height > max_height)) {
655 pixfmt->height = vpbe_dev->current_timings.yres;
658 if (pixfmt->bytesperline < (pixfmt->width * bpp))
659 pixfmt->bytesperline = pixfmt->width * bpp;
661 /* Make the bytesperline 32 byte aligned */
662 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
664 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
665 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
666 (pixfmt->bytesperline * pixfmt->height >> 1);
668 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
673 static int vpbe_display_querycap(struct file *file, void *priv,
674 struct v4l2_capability *cap)
676 struct vpbe_fh *fh = file->private_data;
677 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
679 cap->version = VPBE_DISPLAY_VERSION_CODE;
680 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
681 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
682 snprintf(cap->driver, sizeof(cap->driver), "%s",
683 dev_name(vpbe_dev->pdev));
684 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
685 dev_name(vpbe_dev->pdev));
686 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
691 static int vpbe_display_s_crop(struct file *file, void *priv,
692 const struct v4l2_crop *crop)
694 struct vpbe_fh *fh = file->private_data;
695 struct vpbe_layer *layer = fh->layer;
696 struct vpbe_display *disp_dev = fh->disp_dev;
697 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
698 struct osd_layer_config *cfg = &layer->layer_info.config;
699 struct osd_state *osd_device = disp_dev->osd_device;
700 struct v4l2_rect rect = crop->c;
703 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
704 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
706 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
707 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
716 vpbe_disp_check_window_params(disp_dev, &rect);
718 osd_device->ops.get_layer_config(osd_device,
719 layer->layer_info.id, cfg);
721 vpbe_disp_calculate_scale_factor(disp_dev, layer,
724 vpbe_disp_adj_position(disp_dev, layer, rect.top,
726 ret = osd_device->ops.set_layer_config(osd_device,
727 layer->layer_info.id, cfg);
729 v4l2_err(&vpbe_dev->v4l2_dev,
730 "Error in set layer config:\n");
734 /* apply zooming and h or v expansion */
735 osd_device->ops.set_zoom(osd_device,
736 layer->layer_info.id,
737 layer->layer_info.h_zoom,
738 layer->layer_info.v_zoom);
739 ret = osd_device->ops.set_vid_expansion(osd_device,
740 layer->layer_info.h_exp,
741 layer->layer_info.v_exp);
743 v4l2_err(&vpbe_dev->v4l2_dev,
744 "Error in set vid expansion:\n");
748 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
749 (layer->layer_info.v_zoom != ZOOM_X1) ||
750 (layer->layer_info.h_exp != H_EXP_OFF) ||
751 (layer->layer_info.v_exp != V_EXP_OFF))
752 /* Enable expansion filter */
753 osd_device->ops.set_interpolation_filter(osd_device, 1);
755 osd_device->ops.set_interpolation_filter(osd_device, 0);
760 static int vpbe_display_g_crop(struct file *file, void *priv,
761 struct v4l2_crop *crop)
763 struct vpbe_fh *fh = file->private_data;
764 struct vpbe_layer *layer = fh->layer;
765 struct osd_layer_config *cfg = &layer->layer_info.config;
766 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
767 struct osd_state *osd_device = fh->disp_dev->osd_device;
768 struct v4l2_rect *rect = &crop->c;
770 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
771 "VIDIOC_G_CROP, layer id = %d\n",
774 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
775 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
778 osd_device->ops.get_layer_config(osd_device,
779 layer->layer_info.id, cfg);
780 rect->top = cfg->ypos;
781 rect->left = cfg->xpos;
782 rect->width = cfg->xsize;
783 rect->height = cfg->ysize;
788 static int vpbe_display_cropcap(struct file *file, void *priv,
789 struct v4l2_cropcap *cropcap)
791 struct vpbe_fh *fh = file->private_data;
792 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
794 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
796 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
797 cropcap->bounds.left = 0;
798 cropcap->bounds.top = 0;
799 cropcap->bounds.width = vpbe_dev->current_timings.xres;
800 cropcap->bounds.height = vpbe_dev->current_timings.yres;
801 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
802 cropcap->defrect = cropcap->bounds;
806 static int vpbe_display_g_fmt(struct file *file, void *priv,
807 struct v4l2_format *fmt)
809 struct vpbe_fh *fh = file->private_data;
810 struct vpbe_layer *layer = fh->layer;
811 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
813 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
814 "VIDIOC_G_FMT, layer id = %d\n",
817 /* If buffer type is video output */
818 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
819 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
822 /* Fill in the information about format */
823 fmt->fmt.pix = layer->pix_fmt;
828 static int vpbe_display_enum_fmt(struct file *file, void *priv,
829 struct v4l2_fmtdesc *fmt)
831 struct vpbe_fh *fh = file->private_data;
832 struct vpbe_layer *layer = fh->layer;
833 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
834 unsigned int index = 0;
836 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
837 "VIDIOC_ENUM_FMT, layer id = %d\n",
839 if (fmt->index > 1) {
840 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
844 /* Fill in the information about format */
846 memset(fmt, 0, sizeof(*fmt));
848 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
850 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
851 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
853 strcpy(fmt->description, "Y/CbCr 4:2:0");
854 fmt->pixelformat = V4L2_PIX_FMT_NV12;
860 static int vpbe_display_s_fmt(struct file *file, void *priv,
861 struct v4l2_format *fmt)
863 struct vpbe_fh *fh = file->private_data;
864 struct vpbe_layer *layer = fh->layer;
865 struct vpbe_display *disp_dev = fh->disp_dev;
866 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
867 struct osd_layer_config *cfg = &layer->layer_info.config;
868 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
869 struct osd_state *osd_device = disp_dev->osd_device;
872 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
873 "VIDIOC_S_FMT, layer id = %d\n",
876 /* If streaming is started, return error */
877 if (layer->started) {
878 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
881 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
882 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
885 /* Check for valid pixel format */
886 ret = vpbe_try_format(disp_dev, pixfmt, 1);
890 /* YUV420 is requested, check availability of the
891 other video window */
893 layer->pix_fmt = *pixfmt;
894 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
895 struct vpbe_layer *otherlayer;
897 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
898 /* if other layer is available, only
899 * claim it, do not configure it
901 ret = osd_device->ops.request_layer(osd_device,
902 otherlayer->layer_info.id);
904 v4l2_err(&vpbe_dev->v4l2_dev,
905 "Display Manager failed to allocate layer\n");
910 /* Get osd layer config */
911 osd_device->ops.get_layer_config(osd_device,
912 layer->layer_info.id, cfg);
913 /* Store the pixel format in the layer object */
914 cfg->xsize = pixfmt->width;
915 cfg->ysize = pixfmt->height;
916 cfg->line_length = pixfmt->bytesperline;
919 cfg->interlaced = vpbe_dev->current_timings.interlaced;
921 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
922 cfg->pixfmt = PIXFMT_YCBCRI;
924 /* Change of the default pixel format for both video windows */
925 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
926 struct vpbe_layer *otherlayer;
927 cfg->pixfmt = PIXFMT_NV12;
928 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
930 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
933 /* Set the layer config in the osd window */
934 ret = osd_device->ops.set_layer_config(osd_device,
935 layer->layer_info.id, cfg);
937 v4l2_err(&vpbe_dev->v4l2_dev,
938 "Error in S_FMT params:\n");
942 /* Readback and fill the local copy of current pix format */
943 osd_device->ops.get_layer_config(osd_device,
944 layer->layer_info.id, cfg);
949 static int vpbe_display_try_fmt(struct file *file, void *priv,
950 struct v4l2_format *fmt)
952 struct vpbe_fh *fh = file->private_data;
953 struct vpbe_display *disp_dev = fh->disp_dev;
954 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
955 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
957 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
959 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
960 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
964 /* Check for valid field format */
965 return vpbe_try_format(disp_dev, pixfmt, 0);
970 * vpbe_display_s_std - Set the given standard in the encoder
972 * Sets the standard if supported by the current encoder. Return the status.
973 * 0 - success & -EINVAL on error
975 static int vpbe_display_s_std(struct file *file, void *priv,
978 struct vpbe_fh *fh = priv;
979 struct vpbe_layer *layer = fh->layer;
980 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
983 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
985 /* If streaming is started, return error */
986 if (layer->started) {
987 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
990 if (NULL != vpbe_dev->ops.s_std) {
991 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
993 v4l2_err(&vpbe_dev->v4l2_dev,
994 "Failed to set standard for sub devices\n");
1005 * vpbe_display_g_std - Get the standard in the current encoder
1007 * Get the standard in the current encoder. Return the status. 0 - success
1010 static int vpbe_display_g_std(struct file *file, void *priv,
1011 v4l2_std_id *std_id)
1013 struct vpbe_fh *fh = priv;
1014 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1016 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
1018 /* Get the standard from the current encoder */
1019 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
1020 *std_id = vpbe_dev->current_timings.std_id;
1028 * vpbe_display_enum_output - enumerate outputs
1030 * Enumerates the outputs available at the vpbe display
1031 * returns the status, -EINVAL if end of output list
1033 static int vpbe_display_enum_output(struct file *file, void *priv,
1034 struct v4l2_output *output)
1036 struct vpbe_fh *fh = priv;
1037 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1040 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1042 /* Enumerate outputs */
1044 if (NULL == vpbe_dev->ops.enum_outputs)
1047 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1049 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1050 "Failed to enumerate outputs\n");
1058 * vpbe_display_s_output - Set output to
1059 * the output specified by the index
1061 static int vpbe_display_s_output(struct file *file, void *priv,
1064 struct vpbe_fh *fh = priv;
1065 struct vpbe_layer *layer = fh->layer;
1066 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1069 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1070 /* If streaming is started, return error */
1071 if (layer->started) {
1072 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1075 if (NULL == vpbe_dev->ops.set_output)
1078 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1080 v4l2_err(&vpbe_dev->v4l2_dev,
1081 "Failed to set output for sub devices\n");
1089 * vpbe_display_g_output - Get output from subdevice
1090 * for a given by the index
1092 static int vpbe_display_g_output(struct file *file, void *priv,
1095 struct vpbe_fh *fh = priv;
1096 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1098 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1099 /* Get the standard from the current encoder */
1100 *i = vpbe_dev->current_out_index;
1106 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1108 * enum the timings in the current encoder. Return the status. 0 - success
1112 vpbe_display_enum_dv_timings(struct file *file, void *priv,
1113 struct v4l2_enum_dv_timings *timings)
1115 struct vpbe_fh *fh = priv;
1116 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1119 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1121 /* Enumerate outputs */
1122 if (NULL == vpbe_dev->ops.enum_dv_timings)
1125 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1127 v4l2_err(&vpbe_dev->v4l2_dev,
1128 "Failed to enumerate dv timings info\n");
1136 * vpbe_display_s_dv_timings - Set the dv timings
1138 * Set the timings in the current encoder. Return the status. 0 - success
1142 vpbe_display_s_dv_timings(struct file *file, void *priv,
1143 struct v4l2_dv_timings *timings)
1145 struct vpbe_fh *fh = priv;
1146 struct vpbe_layer *layer = fh->layer;
1147 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1150 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1153 /* If streaming is started, return error */
1154 if (layer->started) {
1155 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1159 /* Set the given standard in the encoder */
1160 if (!vpbe_dev->ops.s_dv_timings)
1163 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1165 v4l2_err(&vpbe_dev->v4l2_dev,
1166 "Failed to set the dv timings info\n");
1174 * vpbe_display_g_dv_timings - Set the dv timings
1176 * Get the timings in the current encoder. Return the status. 0 - success
1180 vpbe_display_g_dv_timings(struct file *file, void *priv,
1181 struct v4l2_dv_timings *dv_timings)
1183 struct vpbe_fh *fh = priv;
1184 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1186 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1188 /* Get the given standard in the encoder */
1190 if (vpbe_dev->current_timings.timings_type &
1191 VPBE_ENC_DV_TIMINGS) {
1192 *dv_timings = vpbe_dev->current_timings.dv_timings;
1200 static int vpbe_display_streamoff(struct file *file, void *priv,
1201 enum v4l2_buf_type buf_type)
1203 struct vpbe_fh *fh = file->private_data;
1204 struct vpbe_layer *layer = fh->layer;
1205 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1206 struct osd_state *osd_device = fh->disp_dev->osd_device;
1209 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1210 "VIDIOC_STREAMOFF,layer id = %d\n",
1213 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1214 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1218 /* If io is allowed for this file handle, return error */
1219 if (!fh->io_allowed) {
1220 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1224 /* If streaming is not started, return error */
1225 if (!layer->started) {
1226 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1227 " id = %d\n", layer->device_id);
1231 osd_device->ops.disable_layer(osd_device,
1232 layer->layer_info.id);
1234 ret = vb2_streamoff(&layer->buffer_queue, buf_type);
1239 static int vpbe_display_streamon(struct file *file, void *priv,
1240 enum v4l2_buf_type buf_type)
1242 struct vpbe_fh *fh = file->private_data;
1243 struct vpbe_layer *layer = fh->layer;
1244 struct vpbe_display *disp_dev = fh->disp_dev;
1245 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1246 struct osd_state *osd_device = disp_dev->osd_device;
1249 osd_device->ops.disable_layer(osd_device,
1250 layer->layer_info.id);
1252 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1255 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1256 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1260 /* If file handle is not allowed IO, return error */
1261 if (!fh->io_allowed) {
1262 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1265 /* If Streaming is already started, return error */
1266 if (layer->started) {
1267 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1272 * Call vb2_streamon to start streaming
1275 ret = vb2_streamon(&layer->buffer_queue, buf_type);
1277 v4l2_err(&vpbe_dev->v4l2_dev,
1278 "error in vb2_streamon\n");
1284 static int vpbe_display_dqbuf(struct file *file, void *priv,
1285 struct v4l2_buffer *buf)
1287 struct vpbe_fh *fh = file->private_data;
1288 struct vpbe_layer *layer = fh->layer;
1289 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1292 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1293 "VIDIOC_DQBUF, layer id = %d\n",
1296 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1297 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1300 /* If this file handle is not allowed to do IO, return error */
1301 if (!fh->io_allowed) {
1302 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1305 if (file->f_flags & O_NONBLOCK)
1306 /* Call videobuf_dqbuf for non blocking mode */
1307 ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
1309 /* Call videobuf_dqbuf for blocking mode */
1310 ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
1315 static int vpbe_display_qbuf(struct file *file, void *priv,
1316 struct v4l2_buffer *p)
1318 struct vpbe_fh *fh = file->private_data;
1319 struct vpbe_layer *layer = fh->layer;
1320 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1322 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1323 "VIDIOC_QBUF, layer id = %d\n",
1326 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1327 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1331 /* If this file handle is not allowed to do IO, return error */
1332 if (!fh->io_allowed) {
1333 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1337 return vb2_qbuf(&layer->buffer_queue, p);
1340 static int vpbe_display_querybuf(struct file *file, void *priv,
1341 struct v4l2_buffer *buf)
1343 struct vpbe_fh *fh = file->private_data;
1344 struct vpbe_layer *layer = fh->layer;
1345 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1347 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1348 "VIDIOC_QUERYBUF, layer id = %d\n",
1351 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1352 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1355 /* Call vb2_querybuf to get information */
1356 return vb2_querybuf(&layer->buffer_queue, buf);
1359 static int vpbe_display_reqbufs(struct file *file, void *priv,
1360 struct v4l2_requestbuffers *req_buf)
1362 struct vpbe_fh *fh = file->private_data;
1363 struct vpbe_layer *layer = fh->layer;
1364 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1366 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1368 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1369 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1373 /* If io users of the layer is not zero, return error */
1374 if (0 != layer->io_usrs) {
1375 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1378 /* Set io allowed member of file handle to TRUE */
1380 /* Increment io usrs member of layer object to 1 */
1382 /* Store type of memory requested in layer object */
1383 layer->memory = req_buf->memory;
1384 /* Allocate buffers */
1385 return vb2_reqbufs(&layer->buffer_queue, req_buf);
1389 * vpbe_display_mmap()
1390 * It is used to map kernel space buffers into user spaces
1392 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1394 /* Get the layer object and file handle object */
1395 struct vpbe_fh *fh = filep->private_data;
1396 struct vpbe_layer *layer = fh->layer;
1397 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1400 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1402 if (mutex_lock_interruptible(&layer->opslock))
1403 return -ERESTARTSYS;
1404 ret = vb2_mmap(&layer->buffer_queue, vma);
1405 mutex_unlock(&layer->opslock);
1409 /* vpbe_display_poll(): It is used for select/poll system call
1411 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1413 struct vpbe_fh *fh = filep->private_data;
1414 struct vpbe_layer *layer = fh->layer;
1415 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1416 unsigned int err = 0;
1418 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1419 if (layer->started) {
1420 mutex_lock(&layer->opslock);
1421 err = vb2_poll(&layer->buffer_queue, filep, wait);
1422 mutex_unlock(&layer->opslock);
1428 * vpbe_display_open()
1429 * It creates object of file handle structure and stores it in private_data
1430 * member of filepointer
1432 static int vpbe_display_open(struct file *file)
1434 struct vpbe_fh *fh = NULL;
1435 struct vpbe_layer *layer = video_drvdata(file);
1436 struct video_device *vdev = video_devdata(file);
1437 struct vpbe_display *disp_dev = layer->disp_dev;
1438 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1439 struct osd_state *osd_device = disp_dev->osd_device;
1442 /* Allocate memory for the file handle object */
1443 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1445 v4l2_err(&vpbe_dev->v4l2_dev,
1446 "unable to allocate memory for file handle object\n");
1449 v4l2_fh_init(&fh->fh, vdev);
1450 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1451 "vpbe display open plane = %d\n",
1454 /* store pointer to fh in private_data member of filep */
1455 file->private_data = fh;
1457 fh->disp_dev = disp_dev;
1460 if (mutex_lock_interruptible(&layer->opslock))
1461 return -ERESTARTSYS;
1462 /* First claim the layer for this device */
1463 err = osd_device->ops.request_layer(osd_device,
1464 layer->layer_info.id);
1465 mutex_unlock(&layer->opslock);
1467 /* Couldn't get layer */
1468 v4l2_err(&vpbe_dev->v4l2_dev,
1469 "Display Manager failed to allocate layer\n");
1474 /* Increment layer usrs counter */
1476 /* Set io_allowed member to false */
1478 v4l2_fh_add(&fh->fh);
1479 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1480 "vpbe display device opened successfully\n");
1485 * vpbe_display_release()
1486 * This function deletes buffer queue, frees the buffers and the davinci
1487 * display file * handle
1489 static int vpbe_display_release(struct file *file)
1491 /* Get the layer object and file handle object */
1492 struct vpbe_fh *fh = file->private_data;
1493 struct vpbe_layer *layer = fh->layer;
1494 struct osd_layer_config *cfg = &layer->layer_info.config;
1495 struct vpbe_display *disp_dev = fh->disp_dev;
1496 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1497 struct osd_state *osd_device = disp_dev->osd_device;
1499 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1501 mutex_lock(&layer->opslock);
1502 /* if this instance is doing IO */
1503 if (fh->io_allowed) {
1504 /* Reset io_usrs member of layer object */
1507 osd_device->ops.disable_layer(osd_device,
1508 layer->layer_info.id);
1512 /* Decrement layer usrs counter */
1514 /* If this file handle has initialize encoder device, reset it */
1516 if (cfg->pixfmt == PIXFMT_NV12) {
1517 struct vpbe_layer *otherlayer;
1519 _vpbe_display_get_other_win_layer(disp_dev, layer);
1520 osd_device->ops.disable_layer(osd_device,
1521 otherlayer->layer_info.id);
1522 osd_device->ops.release_layer(osd_device,
1523 otherlayer->layer_info.id);
1525 osd_device->ops.disable_layer(osd_device,
1526 layer->layer_info.id);
1527 osd_device->ops.release_layer(osd_device,
1528 layer->layer_info.id);
1531 v4l2_fh_del(&fh->fh);
1532 v4l2_fh_exit(&fh->fh);
1533 file->private_data = NULL;
1534 mutex_unlock(&layer->opslock);
1536 /* Free memory allocated to file handle object */
1539 disp_dev->cbcr_ofst = 0;
1544 /* vpbe capture ioctl operations */
1545 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1546 .vidioc_querycap = vpbe_display_querycap,
1547 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1548 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1549 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1550 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1551 .vidioc_reqbufs = vpbe_display_reqbufs,
1552 .vidioc_querybuf = vpbe_display_querybuf,
1553 .vidioc_qbuf = vpbe_display_qbuf,
1554 .vidioc_dqbuf = vpbe_display_dqbuf,
1555 .vidioc_streamon = vpbe_display_streamon,
1556 .vidioc_streamoff = vpbe_display_streamoff,
1557 .vidioc_cropcap = vpbe_display_cropcap,
1558 .vidioc_g_crop = vpbe_display_g_crop,
1559 .vidioc_s_crop = vpbe_display_s_crop,
1560 .vidioc_s_std = vpbe_display_s_std,
1561 .vidioc_g_std = vpbe_display_g_std,
1562 .vidioc_enum_output = vpbe_display_enum_output,
1563 .vidioc_s_output = vpbe_display_s_output,
1564 .vidioc_g_output = vpbe_display_g_output,
1565 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1566 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1567 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
1570 static struct v4l2_file_operations vpbe_fops = {
1571 .owner = THIS_MODULE,
1572 .open = vpbe_display_open,
1573 .release = vpbe_display_release,
1574 .unlocked_ioctl = video_ioctl2,
1575 .mmap = vpbe_display_mmap,
1576 .poll = vpbe_display_poll
1579 static int vpbe_device_get(struct device *dev, void *data)
1581 struct platform_device *pdev = to_platform_device(dev);
1582 struct vpbe_display *vpbe_disp = data;
1584 if (strcmp("vpbe_controller", pdev->name) == 0)
1585 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1587 if (strstr(pdev->name, "vpbe-osd") != NULL)
1588 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1593 static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1594 struct platform_device *pdev)
1596 struct vpbe_layer *vpbe_display_layer = NULL;
1597 struct video_device *vbd = NULL;
1599 /* Allocate memory for four plane display objects */
1602 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1604 /* If memory allocation fails, return error */
1605 if (!disp_dev->dev[i]) {
1606 printk(KERN_ERR "ran out of memory\n");
1609 spin_lock_init(&disp_dev->dev[i]->irqlock);
1610 mutex_init(&disp_dev->dev[i]->opslock);
1612 /* Get the pointer to the layer object */
1613 vpbe_display_layer = disp_dev->dev[i];
1614 vbd = &vpbe_display_layer->video_dev;
1615 /* Initialize field of video device */
1616 vbd->release = video_device_release_empty;
1617 vbd->fops = &vpbe_fops;
1618 vbd->ioctl_ops = &vpbe_ioctl_ops;
1620 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1621 vbd->lock = &vpbe_display_layer->opslock;
1622 vbd->vfl_dir = VFL_DIR_TX;
1624 if (disp_dev->vpbe_dev->current_timings.timings_type &
1626 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1628 snprintf(vbd->name, sizeof(vbd->name),
1629 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1630 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1631 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1632 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1634 vpbe_display_layer->device_id = i;
1636 vpbe_display_layer->layer_info.id =
1637 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1643 static int register_device(struct vpbe_layer *vpbe_display_layer,
1644 struct vpbe_display *disp_dev,
1645 struct platform_device *pdev)
1649 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1650 "Trying to register VPBE display device.\n");
1651 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1652 "layer=%x,layer->video_dev=%x\n",
1653 (int)vpbe_display_layer,
1654 (int)&vpbe_display_layer->video_dev);
1656 err = video_register_device(&vpbe_display_layer->video_dev,
1662 vpbe_display_layer->disp_dev = disp_dev;
1663 /* set the driver data in platform device */
1664 platform_set_drvdata(pdev, disp_dev);
1665 video_set_drvdata(&vpbe_display_layer->video_dev,
1666 vpbe_display_layer);
1674 * vpbe_display_probe()
1675 * This function creates device entries by register itself to the V4L2 driver
1676 * and initializes fields of each layer objects
1678 static int vpbe_display_probe(struct platform_device *pdev)
1680 struct vpbe_display *disp_dev;
1681 struct v4l2_device *v4l2_dev;
1682 struct resource *res = NULL;
1683 struct vb2_queue *q;
1689 printk(KERN_DEBUG "vpbe_display_probe\n");
1690 /* Allocate memory for vpbe_display */
1691 disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1696 spin_lock_init(&disp_dev->dma_queue_lock);
1698 * Scan all the platform devices to find the vpbe
1699 * controller device and get the vpbe_dev object
1701 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1706 v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1707 /* Initialize the vpbe display controller */
1708 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1709 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1710 disp_dev->vpbe_dev);
1712 v4l2_err(v4l2_dev, "Error initing vpbe\n");
1718 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1719 if (init_vpbe_layer(i, disp_dev, pdev)) {
1725 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1727 v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
1733 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1734 VPBE_DISPLAY_DRIVER, disp_dev);
1736 v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
1740 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1741 /* initialize vb2 queue */
1742 q = &disp_dev->dev[i]->buffer_queue;
1743 memset(q, 0, sizeof(*q));
1744 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1745 q->io_modes = VB2_MMAP | VB2_USERPTR;
1746 q->drv_priv = disp_dev->dev[i];
1747 q->ops = &video_qops;
1748 q->mem_ops = &vb2_dma_contig_memops;
1749 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1750 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1751 q->min_buffers_needed = 1;
1753 err = vb2_queue_init(q);
1755 v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
1759 disp_dev->dev[i]->alloc_ctx =
1760 vb2_dma_contig_init_ctx(disp_dev->vpbe_dev->pdev);
1761 if (IS_ERR(disp_dev->dev[i]->alloc_ctx)) {
1762 v4l2_err(v4l2_dev, "Failed to get the context\n");
1763 err = PTR_ERR(disp_dev->dev[i]->alloc_ctx);
1767 INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
1769 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1775 v4l2_dbg(1, debug, v4l2_dev,
1776 "Successfully completed the probing of vpbe v4l2 device\n");
1781 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1782 /* Unregister video device */
1783 if (disp_dev->dev[k] != NULL) {
1784 vb2_dma_contig_cleanup_ctx(disp_dev->dev[k]->alloc_ctx);
1785 video_unregister_device(&disp_dev->dev[k]->video_dev);
1786 kfree(disp_dev->dev[k]);
1793 * vpbe_display_remove()
1794 * It un-register hardware layer from V4L2 driver
1796 static int vpbe_display_remove(struct platform_device *pdev)
1798 struct vpbe_layer *vpbe_display_layer;
1799 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1800 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1803 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1805 /* deinitialize the vpbe display controller */
1806 if (NULL != vpbe_dev->ops.deinitialize)
1807 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1808 /* un-register device */
1809 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1810 /* Get the pointer to the layer object */
1811 vpbe_display_layer = disp_dev->dev[i];
1812 vb2_dma_contig_cleanup_ctx(vpbe_display_layer->alloc_ctx);
1813 /* Unregister video device */
1814 video_unregister_device(&vpbe_display_layer->video_dev);
1817 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1818 kfree(disp_dev->dev[i]);
1819 disp_dev->dev[i] = NULL;
1825 static struct platform_driver vpbe_display_driver = {
1827 .name = VPBE_DISPLAY_DRIVER,
1828 .owner = THIS_MODULE,
1829 .bus = &platform_bus_type,
1831 .probe = vpbe_display_probe,
1832 .remove = vpbe_display_remove,
1835 module_platform_driver(vpbe_display_driver);
1837 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1838 MODULE_LICENSE("GPL");
1839 MODULE_AUTHOR("Texas Instruments");