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 venc_is_second_field(struct vpbe_display *disp_dev)
52 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
56 ret = v4l2_subdev_call(vpbe_dev->venc,
62 v4l2_err(&vpbe_dev->v4l2_dev,
63 "Error in getting Field ID 0\n");
68 static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
69 struct vpbe_layer *layer)
71 struct timespec timevalue;
73 if (layer->cur_frm == layer->next_frm)
75 ktime_get_ts(&timevalue);
76 layer->cur_frm->ts.tv_sec = timevalue.tv_sec;
77 layer->cur_frm->ts.tv_usec = timevalue.tv_nsec / NSEC_PER_USEC;
78 layer->cur_frm->state = VIDEOBUF_DONE;
79 wake_up_interruptible(&layer->cur_frm->done);
80 /* Make cur_frm pointing to next_frm */
81 layer->cur_frm = layer->next_frm;
84 static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
85 struct vpbe_layer *layer)
87 struct osd_state *osd_device = disp_obj->osd_device;
90 spin_lock(&disp_obj->dma_queue_lock);
91 if (list_empty(&layer->dma_queue) ||
92 (layer->cur_frm != layer->next_frm)) {
93 spin_unlock(&disp_obj->dma_queue_lock);
97 * one field is displayed configure
98 * the next frame if it is available
99 * otherwise hold on current frame
100 * Get next from the buffer queue
102 layer->next_frm = list_entry(
103 layer->dma_queue.next,
104 struct videobuf_buffer,
106 /* Remove that from the buffer queue */
107 list_del(&layer->next_frm->queue);
108 spin_unlock(&disp_obj->dma_queue_lock);
109 /* Mark state of the frame to active */
110 layer->next_frm->state = VIDEOBUF_ACTIVE;
111 addr = videobuf_to_dma_contig(layer->next_frm);
112 osd_device->ops.start_layer(osd_device,
113 layer->layer_info.id,
115 disp_obj->cbcr_ofst);
118 /* interrupt service routine */
119 static irqreturn_t venc_isr(int irq, void *arg)
121 struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
122 struct vpbe_layer *layer;
123 static unsigned last_event;
128 if ((NULL == arg) || (NULL == disp_dev->dev[0]))
131 if (venc_is_second_field(disp_dev))
132 event |= VENC_SECOND_FIELD;
134 event |= VENC_FIRST_FIELD;
136 if (event == (last_event & ~VENC_END_OF_FRAME)) {
138 * If the display is non-interlaced, then we need to flag the
139 * end-of-frame event at every interrupt regardless of the
140 * value of the FIDST bit. We can conclude that the display is
141 * non-interlaced if the value of the FIDST bit is unchanged
142 * from the previous interrupt.
144 event |= VENC_END_OF_FRAME;
145 } else if (event == VENC_SECOND_FIELD) {
146 /* end-of-frame for interlaced display */
147 event |= VENC_END_OF_FRAME;
151 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
152 layer = disp_dev->dev[i];
153 /* If streaming is started in this layer */
157 if (layer->layer_first_int) {
158 layer->layer_first_int = 0;
161 /* Check the field format */
162 if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
163 (event & VENC_END_OF_FRAME)) {
164 /* Progressive mode */
166 vpbe_isr_even_field(disp_dev, layer);
167 vpbe_isr_odd_field(disp_dev, layer);
169 /* Interlaced mode */
171 layer->field_id ^= 1;
172 if (event & VENC_FIRST_FIELD)
178 * If field id does not match with store
181 if (fid != layer->field_id) {
182 /* Make them in sync */
183 layer->field_id = fid;
187 * device field id and local field id are
188 * in sync. If this is even field
191 vpbe_isr_even_field(disp_dev, layer);
193 vpbe_isr_odd_field(disp_dev, layer);
201 * vpbe_buffer_prepare()
202 * This is the callback function called from videobuf_qbuf() function
203 * the buffer is prepared and user space virtual address is converted into
206 static int vpbe_buffer_prepare(struct videobuf_queue *q,
207 struct videobuf_buffer *vb,
208 enum v4l2_field field)
210 struct vpbe_fh *fh = q->priv_data;
211 struct vpbe_layer *layer = fh->layer;
212 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
216 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
217 "vpbe_buffer_prepare\n");
219 /* If buffer is not initialized, initialize it */
220 if (VIDEOBUF_NEEDS_INIT == vb->state) {
221 vb->width = layer->pix_fmt.width;
222 vb->height = layer->pix_fmt.height;
223 vb->size = layer->pix_fmt.sizeimage;
226 ret = videobuf_iolock(q, vb, NULL);
228 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to map \
233 addr = videobuf_to_dma_contig(vb);
236 if (!IS_ALIGNED(addr, 8)) {
237 v4l2_err(&vpbe_dev->v4l2_dev,
238 "buffer_prepare:offset is \
239 not aligned to 32 bytes\n");
243 vb->state = VIDEOBUF_PREPARED;
249 * vpbe_buffer_setup()
250 * This function allocates memory for the buffers
252 static int vpbe_buffer_setup(struct videobuf_queue *q,
256 /* Get the file handle object and layer object */
257 struct vpbe_fh *fh = q->priv_data;
258 struct vpbe_layer *layer = fh->layer;
259 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
261 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
263 *size = layer->pix_fmt.sizeimage;
265 /* Store number of buffers allocated in numbuffer member */
266 if (*count < VPBE_DEFAULT_NUM_BUFS)
267 *count = layer->numbuffers = VPBE_DEFAULT_NUM_BUFS;
273 * vpbe_buffer_queue()
274 * This function adds the buffer to DMA queue
276 static void vpbe_buffer_queue(struct videobuf_queue *q,
277 struct videobuf_buffer *vb)
279 /* Get the file handle object and layer object */
280 struct vpbe_fh *fh = q->priv_data;
281 struct vpbe_layer *layer = fh->layer;
282 struct vpbe_display *disp = fh->disp_dev;
283 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
286 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
287 "vpbe_buffer_queue\n");
289 /* add the buffer to the DMA queue */
290 spin_lock_irqsave(&disp->dma_queue_lock, flags);
291 list_add_tail(&vb->queue, &layer->dma_queue);
292 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
293 /* Change state of the buffer */
294 vb->state = VIDEOBUF_QUEUED;
298 * vpbe_buffer_release()
299 * This function is called from the videobuf layer to free memory allocated to
302 static void vpbe_buffer_release(struct videobuf_queue *q,
303 struct videobuf_buffer *vb)
305 /* Get the file handle object and layer object */
306 struct vpbe_fh *fh = q->priv_data;
307 struct vpbe_layer *layer = fh->layer;
308 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
310 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
311 "vpbe_buffer_release\n");
313 if (V4L2_MEMORY_USERPTR != layer->memory)
314 videobuf_dma_contig_free(q, vb);
316 vb->state = VIDEOBUF_NEEDS_INIT;
319 static struct videobuf_queue_ops video_qops = {
320 .buf_setup = vpbe_buffer_setup,
321 .buf_prepare = vpbe_buffer_prepare,
322 .buf_queue = vpbe_buffer_queue,
323 .buf_release = vpbe_buffer_release,
328 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
329 struct vpbe_layer *layer)
331 enum vpbe_display_device_id thiswin, otherwin;
332 thiswin = layer->device_id;
334 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
335 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
336 return disp_dev->dev[otherwin];
339 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
340 struct vpbe_layer *layer)
342 struct osd_layer_config *cfg = &layer->layer_info.config;
343 struct osd_state *osd_device = disp_dev->osd_device;
344 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
348 addr = videobuf_to_dma_contig(layer->cur_frm);
349 /* Set address in the display registers */
350 osd_device->ops.start_layer(osd_device,
351 layer->layer_info.id,
353 disp_dev->cbcr_ofst);
355 ret = osd_device->ops.enable_layer(osd_device,
356 layer->layer_info.id, 0);
358 v4l2_err(&vpbe_dev->v4l2_dev,
359 "Error in enabling osd window layer 0\n");
363 /* Enable the window */
364 layer->layer_info.enable = 1;
365 if (cfg->pixfmt == PIXFMT_NV12) {
366 struct vpbe_layer *otherlayer =
367 _vpbe_display_get_other_win_layer(disp_dev, layer);
369 ret = osd_device->ops.enable_layer(osd_device,
370 otherlayer->layer_info.id, 1);
372 v4l2_err(&vpbe_dev->v4l2_dev,
373 "Error in enabling osd window layer 1\n");
376 otherlayer->layer_info.enable = 1;
382 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
383 struct vpbe_layer *layer,
384 int expected_xsize, int expected_ysize)
386 struct display_layer_info *layer_info = &layer->layer_info;
387 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
388 struct osd_layer_config *cfg = &layer->layer_info.config;
389 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
390 int calculated_xsize;
396 v4l2_std_id standard_id = vpbe_dev->current_timings.timings.std_id;
399 * Application initially set the image format. Current display
400 * size is obtained from the vpbe display controller. expected_xsize
401 * and expected_ysize are set through S_CROP ioctl. Based on this,
402 * driver will calculate the scale factors for vertical and
403 * horizontal direction so that the image is displayed scaled
404 * and expanded. Application uses expansion to display the image
405 * in a square pixel. Otherwise it is displayed using displays
406 * pixel aspect ratio.It is expected that application chooses
407 * the crop coordinates for cropped or scaled display. if crop
408 * size is less than the image size, it is displayed cropped or
409 * it is displayed scaled and/or expanded.
411 * to begin with, set the crop window same as expected. Later we
412 * will override with scaled window size
415 cfg->xsize = pixfmt->width;
416 cfg->ysize = pixfmt->height;
417 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
418 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
419 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
420 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
422 if (pixfmt->width < expected_xsize) {
423 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
426 else if (h_scale >= 4)
430 cfg->xsize *= h_scale;
431 if (cfg->xsize < expected_xsize) {
432 if ((standard_id & V4L2_STD_525_60) ||
433 (standard_id & V4L2_STD_625_50)) {
434 calculated_xsize = (cfg->xsize *
435 VPBE_DISPLAY_H_EXP_RATIO_N) /
436 VPBE_DISPLAY_H_EXP_RATIO_D;
437 if (calculated_xsize <= expected_xsize) {
439 cfg->xsize = calculated_xsize;
444 layer_info->h_zoom = ZOOM_X2;
445 else if (h_scale == 4)
446 layer_info->h_zoom = ZOOM_X4;
448 layer_info->h_exp = H_EXP_9_OVER_8;
450 /* no scaling, only cropping. Set display area to crop area */
451 cfg->xsize = expected_xsize;
454 if (pixfmt->height < expected_ysize) {
455 v_scale = expected_ysize / pixfmt->height;
458 else if (v_scale >= 4)
462 cfg->ysize *= v_scale;
463 if (cfg->ysize < expected_ysize) {
464 if ((standard_id & V4L2_STD_625_50)) {
465 calculated_xsize = (cfg->ysize *
466 VPBE_DISPLAY_V_EXP_RATIO_N) /
467 VPBE_DISPLAY_V_EXP_RATIO_D;
468 if (calculated_xsize <= expected_ysize) {
470 cfg->ysize = calculated_xsize;
475 layer_info->v_zoom = ZOOM_X2;
476 else if (v_scale == 4)
477 layer_info->v_zoom = ZOOM_X4;
479 layer_info->h_exp = V_EXP_6_OVER_5;
481 /* no scaling, only cropping. Set display area to crop area */
482 cfg->ysize = expected_ysize;
484 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
485 "crop display xsize = %d, ysize = %d\n",
486 cfg->xsize, cfg->ysize);
489 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
490 struct vpbe_layer *layer,
493 struct osd_layer_config *cfg = &layer->layer_info.config;
494 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
496 cfg->xpos = min((unsigned int)left,
497 vpbe_dev->current_timings.xres - cfg->xsize);
498 cfg->ypos = min((unsigned int)top,
499 vpbe_dev->current_timings.yres - cfg->ysize);
501 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
502 "new xpos = %d, ypos = %d\n",
503 cfg->xpos, cfg->ypos);
506 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
509 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
511 if ((c->width == 0) ||
512 ((c->width + c->left) > vpbe_dev->current_timings.xres))
513 c->width = vpbe_dev->current_timings.xres - c->left;
515 if ((c->height == 0) || ((c->height + c->top) >
516 vpbe_dev->current_timings.yres))
517 c->height = vpbe_dev->current_timings.yres - c->top;
519 /* window height must be even for interlaced display */
520 if (vpbe_dev->current_timings.interlaced)
521 c->height &= (~0x01);
527 * If user application provides width and height, and have bytesperline set
528 * to zero, driver calculates bytesperline and sizeimage based on hardware
531 static int vpbe_try_format(struct vpbe_display *disp_dev,
532 struct v4l2_pix_format *pixfmt, int check)
534 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
541 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
542 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
543 /* choose default as V4L2_PIX_FMT_UYVY */
544 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
546 /* Check the field format */
547 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
548 (pixfmt->field != V4L2_FIELD_NONE)) {
549 if (vpbe_dev->current_timings.interlaced)
550 pixfmt->field = V4L2_FIELD_INTERLACED;
552 pixfmt->field = V4L2_FIELD_NONE;
555 if (pixfmt->field == V4L2_FIELD_INTERLACED)
558 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
563 max_width = vpbe_dev->current_timings.xres;
564 max_height = vpbe_dev->current_timings.yres;
568 if (!pixfmt->width || (pixfmt->width < min_width) ||
569 (pixfmt->width > max_width)) {
570 pixfmt->width = vpbe_dev->current_timings.xres;
573 if (!pixfmt->height || (pixfmt->height < min_height) ||
574 (pixfmt->height > max_height)) {
575 pixfmt->height = vpbe_dev->current_timings.yres;
578 if (pixfmt->bytesperline < (pixfmt->width * bpp))
579 pixfmt->bytesperline = pixfmt->width * bpp;
581 /* Make the bytesperline 32 byte aligned */
582 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
584 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
585 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
586 (pixfmt->bytesperline * pixfmt->height >> 1);
588 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
593 static int vpbe_display_g_priority(struct file *file, void *priv,
594 enum v4l2_priority *p)
596 struct vpbe_fh *fh = file->private_data;
597 struct vpbe_layer *layer = fh->layer;
599 *p = v4l2_prio_max(&layer->prio);
604 static int vpbe_display_s_priority(struct file *file, void *priv,
605 enum v4l2_priority p)
607 struct vpbe_fh *fh = file->private_data;
608 struct vpbe_layer *layer = fh->layer;
611 ret = v4l2_prio_change(&layer->prio, &fh->prio, p);
616 static int vpbe_display_querycap(struct file *file, void *priv,
617 struct v4l2_capability *cap)
619 struct vpbe_fh *fh = file->private_data;
620 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
622 cap->version = VPBE_DISPLAY_VERSION_CODE;
623 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
624 strlcpy(cap->driver, VPBE_DISPLAY_DRIVER, sizeof(cap->driver));
625 strlcpy(cap->bus_info, "platform", sizeof(cap->bus_info));
626 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
631 static int vpbe_display_s_crop(struct file *file, void *priv,
632 struct v4l2_crop *crop)
634 struct vpbe_fh *fh = file->private_data;
635 struct vpbe_layer *layer = fh->layer;
636 struct vpbe_display *disp_dev = fh->disp_dev;
637 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
638 struct osd_layer_config *cfg = &layer->layer_info.config;
639 struct osd_state *osd_device = disp_dev->osd_device;
640 struct v4l2_rect *rect = &crop->c;
643 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
644 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
646 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
647 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
656 vpbe_disp_check_window_params(disp_dev, rect);
658 osd_device->ops.get_layer_config(osd_device,
659 layer->layer_info.id, cfg);
661 vpbe_disp_calculate_scale_factor(disp_dev, layer,
664 vpbe_disp_adj_position(disp_dev, layer, rect->top,
666 ret = osd_device->ops.set_layer_config(osd_device,
667 layer->layer_info.id, cfg);
669 v4l2_err(&vpbe_dev->v4l2_dev,
670 "Error in set layer config:\n");
674 /* apply zooming and h or v expansion */
675 osd_device->ops.set_zoom(osd_device,
676 layer->layer_info.id,
677 layer->layer_info.h_zoom,
678 layer->layer_info.v_zoom);
679 ret = osd_device->ops.set_vid_expansion(osd_device,
680 layer->layer_info.h_exp,
681 layer->layer_info.v_exp);
683 v4l2_err(&vpbe_dev->v4l2_dev,
684 "Error in set vid expansion:\n");
688 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
689 (layer->layer_info.v_zoom != ZOOM_X1) ||
690 (layer->layer_info.h_exp != H_EXP_OFF) ||
691 (layer->layer_info.v_exp != V_EXP_OFF))
692 /* Enable expansion filter */
693 osd_device->ops.set_interpolation_filter(osd_device, 1);
695 osd_device->ops.set_interpolation_filter(osd_device, 0);
700 static int vpbe_display_g_crop(struct file *file, void *priv,
701 struct v4l2_crop *crop)
703 struct vpbe_fh *fh = file->private_data;
704 struct vpbe_layer *layer = fh->layer;
705 struct osd_layer_config *cfg = &layer->layer_info.config;
706 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
707 struct osd_state *osd_device = fh->disp_dev->osd_device;
708 struct v4l2_rect *rect = &crop->c;
711 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
712 "VIDIOC_G_CROP, layer id = %d\n",
715 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
716 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
719 osd_device->ops.get_layer_config(osd_device,
720 layer->layer_info.id, cfg);
721 rect->top = cfg->ypos;
722 rect->left = cfg->xpos;
723 rect->width = cfg->xsize;
724 rect->height = cfg->ysize;
729 static int vpbe_display_cropcap(struct file *file, void *priv,
730 struct v4l2_cropcap *cropcap)
732 struct vpbe_fh *fh = file->private_data;
733 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
735 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
737 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
738 cropcap->bounds.left = 0;
739 cropcap->bounds.top = 0;
740 cropcap->bounds.width = vpbe_dev->current_timings.xres;
741 cropcap->bounds.height = vpbe_dev->current_timings.yres;
742 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
743 cropcap->defrect = cropcap->bounds;
747 static int vpbe_display_g_fmt(struct file *file, void *priv,
748 struct v4l2_format *fmt)
750 struct vpbe_fh *fh = file->private_data;
751 struct vpbe_layer *layer = fh->layer;
752 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
754 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
755 "VIDIOC_G_FMT, layer id = %d\n",
758 /* If buffer type is video output */
759 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
760 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
763 /* Fill in the information about format */
764 fmt->fmt.pix = layer->pix_fmt;
769 static int vpbe_display_enum_fmt(struct file *file, void *priv,
770 struct v4l2_fmtdesc *fmt)
772 struct vpbe_fh *fh = file->private_data;
773 struct vpbe_layer *layer = fh->layer;
774 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
775 unsigned int index = 0;
777 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
778 "VIDIOC_ENUM_FMT, layer id = %d\n",
780 if (fmt->index > 1) {
781 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
785 /* Fill in the information about format */
787 memset(fmt, 0, sizeof(*fmt));
789 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
791 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
792 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
794 strcpy(fmt->description, "Y/CbCr 4:2:0");
795 fmt->pixelformat = V4L2_PIX_FMT_NV12;
801 static int vpbe_display_s_fmt(struct file *file, void *priv,
802 struct v4l2_format *fmt)
804 struct vpbe_fh *fh = file->private_data;
805 struct vpbe_layer *layer = fh->layer;
806 struct vpbe_display *disp_dev = fh->disp_dev;
807 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
808 struct osd_layer_config *cfg = &layer->layer_info.config;
809 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
810 struct osd_state *osd_device = disp_dev->osd_device;
813 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
814 "VIDIOC_S_FMT, layer id = %d\n",
817 /* If streaming is started, return error */
818 if (layer->started) {
819 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
822 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
823 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
826 /* Check for valid pixel format */
827 ret = vpbe_try_format(disp_dev, pixfmt, 1);
831 /* YUV420 is requested, check availability of the
832 other video window */
834 layer->pix_fmt = *pixfmt;
836 /* Get osd layer config */
837 osd_device->ops.get_layer_config(osd_device,
838 layer->layer_info.id, cfg);
839 /* Store the pixel format in the layer object */
840 cfg->xsize = pixfmt->width;
841 cfg->ysize = pixfmt->height;
842 cfg->line_length = pixfmt->bytesperline;
845 cfg->interlaced = vpbe_dev->current_timings.interlaced;
847 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
848 cfg->pixfmt = PIXFMT_YCbCrI;
850 /* Change of the default pixel format for both video windows */
851 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
852 struct vpbe_layer *otherlayer;
853 cfg->pixfmt = PIXFMT_NV12;
854 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
856 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
859 /* Set the layer config in the osd window */
860 ret = osd_device->ops.set_layer_config(osd_device,
861 layer->layer_info.id, cfg);
863 v4l2_err(&vpbe_dev->v4l2_dev,
864 "Error in S_FMT params:\n");
868 /* Readback and fill the local copy of current pix format */
869 osd_device->ops.get_layer_config(osd_device,
870 layer->layer_info.id, cfg);
875 static int vpbe_display_try_fmt(struct file *file, void *priv,
876 struct v4l2_format *fmt)
878 struct vpbe_fh *fh = file->private_data;
879 struct vpbe_display *disp_dev = fh->disp_dev;
880 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
881 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
883 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
885 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
886 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
890 /* Check for valid field format */
891 return vpbe_try_format(disp_dev, pixfmt, 0);
896 * vpbe_display_s_std - Set the given standard in the encoder
898 * Sets the standard if supported by the current encoder. Return the status.
899 * 0 - success & -EINVAL on error
901 static int vpbe_display_s_std(struct file *file, void *priv,
904 struct vpbe_fh *fh = priv;
905 struct vpbe_layer *layer = fh->layer;
906 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
909 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
911 /* If streaming is started, return error */
912 if (layer->started) {
913 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
916 if (NULL != vpbe_dev->ops.s_std) {
917 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
919 v4l2_err(&vpbe_dev->v4l2_dev,
920 "Failed to set standard for sub devices\n");
931 * vpbe_display_g_std - Get the standard in the current encoder
933 * Get the standard in the current encoder. Return the status. 0 - success
936 static int vpbe_display_g_std(struct file *file, void *priv,
939 struct vpbe_fh *fh = priv;
940 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
942 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
944 /* Get the standard from the current encoder */
945 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
946 *std_id = vpbe_dev->current_timings.timings.std_id;
954 * vpbe_display_enum_output - enumerate outputs
956 * Enumerates the outputs available at the vpbe display
957 * returns the status, -EINVAL if end of output list
959 static int vpbe_display_enum_output(struct file *file, void *priv,
960 struct v4l2_output *output)
962 struct vpbe_fh *fh = priv;
963 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
966 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
968 /* Enumerate outputs */
970 if (NULL == vpbe_dev->ops.enum_outputs)
973 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
975 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
976 "Failed to enumerate outputs\n");
984 * vpbe_display_s_output - Set output to
985 * the output specified by the index
987 static int vpbe_display_s_output(struct file *file, void *priv,
990 struct vpbe_fh *fh = priv;
991 struct vpbe_layer *layer = fh->layer;
992 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
995 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
996 /* If streaming is started, return error */
997 if (layer->started) {
998 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1001 if (NULL == vpbe_dev->ops.set_output)
1004 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1006 v4l2_err(&vpbe_dev->v4l2_dev,
1007 "Failed to set output for sub devices\n");
1015 * vpbe_display_g_output - Get output from subdevice
1016 * for a given by the index
1018 static int vpbe_display_g_output(struct file *file, void *priv,
1021 struct vpbe_fh *fh = priv;
1022 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1024 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1025 /* Get the standard from the current encoder */
1026 *i = vpbe_dev->current_out_index;
1032 * vpbe_display_enum_dv_presets - Enumerate the dv presets
1034 * enum the preset in the current encoder. Return the status. 0 - success
1038 vpbe_display_enum_dv_presets(struct file *file, void *priv,
1039 struct v4l2_dv_enum_preset *preset)
1041 struct vpbe_fh *fh = priv;
1042 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1045 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_PRESETS\n");
1047 /* Enumerate outputs */
1048 if (NULL == vpbe_dev->ops.enum_dv_presets)
1051 ret = vpbe_dev->ops.enum_dv_presets(vpbe_dev, preset);
1053 v4l2_err(&vpbe_dev->v4l2_dev,
1054 "Failed to enumerate dv presets info\n");
1062 * vpbe_display_s_dv_preset - Set the dv presets
1064 * Set the preset in the current encoder. Return the status. 0 - success
1068 vpbe_display_s_dv_preset(struct file *file, void *priv,
1069 struct v4l2_dv_preset *preset)
1071 struct vpbe_fh *fh = priv;
1072 struct vpbe_layer *layer = fh->layer;
1073 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1076 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_PRESETS\n");
1079 /* If streaming is started, return error */
1080 if (layer->started) {
1081 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1085 /* Set the given standard in the encoder */
1086 if (!vpbe_dev->ops.s_dv_preset)
1089 ret = vpbe_dev->ops.s_dv_preset(vpbe_dev, preset);
1091 v4l2_err(&vpbe_dev->v4l2_dev,
1092 "Failed to set the dv presets info\n");
1095 /* set the current norm to zero to be consistent. If STD is used
1096 * v4l2 layer will set the norm properly on successful s_std call
1098 layer->video_dev.current_norm = 0;
1104 * vpbe_display_g_dv_preset - Set the dv presets
1106 * Get the preset in the current encoder. Return the status. 0 - success
1110 vpbe_display_g_dv_preset(struct file *file, void *priv,
1111 struct v4l2_dv_preset *dv_preset)
1113 struct vpbe_fh *fh = priv;
1114 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1116 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_PRESETS\n");
1118 /* Get the given standard in the encoder */
1120 if (vpbe_dev->current_timings.timings_type &
1121 VPBE_ENC_DV_PRESET) {
1123 vpbe_dev->current_timings.timings.dv_preset;
1131 static int vpbe_display_streamoff(struct file *file, void *priv,
1132 enum v4l2_buf_type buf_type)
1134 struct vpbe_fh *fh = file->private_data;
1135 struct vpbe_layer *layer = fh->layer;
1136 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1137 struct osd_state *osd_device = fh->disp_dev->osd_device;
1140 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1141 "VIDIOC_STREAMOFF,layer id = %d\n",
1144 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1145 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1149 /* If io is allowed for this file handle, return error */
1150 if (!fh->io_allowed) {
1151 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1155 /* If streaming is not started, return error */
1156 if (!layer->started) {
1157 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1158 " id = %d\n", layer->device_id);
1162 osd_device->ops.disable_layer(osd_device,
1163 layer->layer_info.id);
1165 ret = videobuf_streamoff(&layer->buffer_queue);
1170 static int vpbe_display_streamon(struct file *file, void *priv,
1171 enum v4l2_buf_type buf_type)
1173 struct vpbe_fh *fh = file->private_data;
1174 struct vpbe_layer *layer = fh->layer;
1175 struct vpbe_display *disp_dev = fh->disp_dev;
1176 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1177 struct osd_state *osd_device = disp_dev->osd_device;
1180 osd_device->ops.disable_layer(osd_device,
1181 layer->layer_info.id);
1183 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1186 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1187 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1191 /* If file handle is not allowed IO, return error */
1192 if (!fh->io_allowed) {
1193 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1196 /* If Streaming is already started, return error */
1197 if (layer->started) {
1198 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1203 * Call videobuf_streamon to start streaming
1206 ret = videobuf_streamon(&layer->buffer_queue);
1208 v4l2_err(&vpbe_dev->v4l2_dev,
1209 "error in videobuf_streamon\n");
1212 /* If buffer queue is empty, return error */
1213 if (list_empty(&layer->dma_queue)) {
1214 v4l2_err(&vpbe_dev->v4l2_dev, "buffer queue is empty\n");
1217 /* Get the next frame from the buffer queue */
1218 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
1219 struct videobuf_buffer, queue);
1220 /* Remove buffer from the buffer queue */
1221 list_del(&layer->cur_frm->queue);
1222 /* Mark state of the current frame to active */
1223 layer->cur_frm->state = VIDEOBUF_ACTIVE;
1224 /* Initialize field_id and started member */
1225 layer->field_id = 0;
1227 /* Set parameters in OSD and VENC */
1228 ret = vpbe_set_osd_display_params(disp_dev, layer);
1233 * if request format is yuv420 semiplanar, need to
1234 * enable both video windows
1238 layer->layer_first_int = 1;
1242 ret = videobuf_streamoff(&layer->buffer_queue);
1246 static int vpbe_display_dqbuf(struct file *file, void *priv,
1247 struct v4l2_buffer *buf)
1249 struct vpbe_fh *fh = file->private_data;
1250 struct vpbe_layer *layer = fh->layer;
1251 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1254 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1255 "VIDIOC_DQBUF, layer id = %d\n",
1258 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1259 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1262 /* If this file handle is not allowed to do IO, return error */
1263 if (!fh->io_allowed) {
1264 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1267 if (file->f_flags & O_NONBLOCK)
1268 /* Call videobuf_dqbuf for non blocking mode */
1269 ret = videobuf_dqbuf(&layer->buffer_queue, buf, 1);
1271 /* Call videobuf_dqbuf for blocking mode */
1272 ret = videobuf_dqbuf(&layer->buffer_queue, buf, 0);
1277 static int vpbe_display_qbuf(struct file *file, void *priv,
1278 struct v4l2_buffer *p)
1280 struct vpbe_fh *fh = file->private_data;
1281 struct vpbe_layer *layer = fh->layer;
1282 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1284 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1285 "VIDIOC_QBUF, layer id = %d\n",
1288 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1289 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1293 /* If this file handle is not allowed to do IO, return error */
1294 if (!fh->io_allowed) {
1295 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1299 return videobuf_qbuf(&layer->buffer_queue, p);
1302 static int vpbe_display_querybuf(struct file *file, void *priv,
1303 struct v4l2_buffer *buf)
1305 struct vpbe_fh *fh = file->private_data;
1306 struct vpbe_layer *layer = fh->layer;
1307 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1310 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1311 "VIDIOC_QUERYBUF, layer id = %d\n",
1314 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1315 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1319 /* Call videobuf_querybuf to get information */
1320 ret = videobuf_querybuf(&layer->buffer_queue, buf);
1325 static int vpbe_display_reqbufs(struct file *file, void *priv,
1326 struct v4l2_requestbuffers *req_buf)
1328 struct vpbe_fh *fh = file->private_data;
1329 struct vpbe_layer *layer = fh->layer;
1330 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1333 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1335 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1336 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1340 /* If io users of the layer is not zero, return error */
1341 if (0 != layer->io_usrs) {
1342 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1345 /* Initialize videobuf queue as per the buffer type */
1346 videobuf_queue_dma_contig_init(&layer->buffer_queue,
1350 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1351 layer->pix_fmt.field,
1352 sizeof(struct videobuf_buffer),
1355 /* Set io allowed member of file handle to TRUE */
1357 /* Increment io usrs member of layer object to 1 */
1359 /* Store type of memory requested in layer object */
1360 layer->memory = req_buf->memory;
1361 /* Initialize buffer queue */
1362 INIT_LIST_HEAD(&layer->dma_queue);
1363 /* Allocate buffers */
1364 ret = videobuf_reqbufs(&layer->buffer_queue, req_buf);
1370 * vpbe_display_mmap()
1371 * It is used to map kernel space buffers into user spaces
1373 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1375 /* Get the layer object and file handle object */
1376 struct vpbe_fh *fh = filep->private_data;
1377 struct vpbe_layer *layer = fh->layer;
1378 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1381 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1383 if (mutex_lock_interruptible(&layer->opslock))
1384 return -ERESTARTSYS;
1385 ret = videobuf_mmap_mapper(&layer->buffer_queue, vma);
1386 mutex_unlock(&layer->opslock);
1390 /* vpbe_display_poll(): It is used for select/poll system call
1392 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1394 struct vpbe_fh *fh = filep->private_data;
1395 struct vpbe_layer *layer = fh->layer;
1396 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1397 unsigned int err = 0;
1399 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1400 if (layer->started) {
1401 mutex_lock(&layer->opslock);
1402 err = videobuf_poll_stream(filep, &layer->buffer_queue, wait);
1403 mutex_unlock(&layer->opslock);
1409 * vpbe_display_open()
1410 * It creates object of file handle structure and stores it in private_data
1411 * member of filepointer
1413 static int vpbe_display_open(struct file *file)
1415 struct vpbe_fh *fh = NULL;
1416 struct vpbe_layer *layer = video_drvdata(file);
1417 struct vpbe_display *disp_dev = layer->disp_dev;
1418 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1419 struct osd_state *osd_device = disp_dev->osd_device;
1422 /* Allocate memory for the file handle object */
1423 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1425 v4l2_err(&vpbe_dev->v4l2_dev,
1426 "unable to allocate memory for file handle object\n");
1429 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1430 "vpbe display open plane = %d\n",
1433 /* store pointer to fh in private_data member of filep */
1434 file->private_data = fh;
1436 fh->disp_dev = disp_dev;
1439 if (mutex_lock_interruptible(&layer->opslock))
1440 return -ERESTARTSYS;
1441 /* First claim the layer for this device */
1442 err = osd_device->ops.request_layer(osd_device,
1443 layer->layer_info.id);
1444 mutex_unlock(&layer->opslock);
1446 /* Couldn't get layer */
1447 v4l2_err(&vpbe_dev->v4l2_dev,
1448 "Display Manager failed to allocate layer\n");
1453 /* Increment layer usrs counter */
1455 /* Set io_allowed member to false */
1457 /* Initialize priority of this instance to default priority */
1458 fh->prio = V4L2_PRIORITY_UNSET;
1459 v4l2_prio_open(&layer->prio, &fh->prio);
1460 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1461 "vpbe display device opened successfully\n");
1466 * vpbe_display_release()
1467 * This function deletes buffer queue, frees the buffers and the davinci
1468 * display file * handle
1470 static int vpbe_display_release(struct file *file)
1472 /* Get the layer object and file handle object */
1473 struct vpbe_fh *fh = file->private_data;
1474 struct vpbe_layer *layer = fh->layer;
1475 struct osd_layer_config *cfg = &layer->layer_info.config;
1476 struct vpbe_display *disp_dev = fh->disp_dev;
1477 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1478 struct osd_state *osd_device = disp_dev->osd_device;
1480 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1482 mutex_lock(&layer->opslock);
1483 /* if this instance is doing IO */
1484 if (fh->io_allowed) {
1485 /* Reset io_usrs member of layer object */
1488 osd_device->ops.disable_layer(osd_device,
1489 layer->layer_info.id);
1491 /* Free buffers allocated */
1492 videobuf_queue_cancel(&layer->buffer_queue);
1493 videobuf_mmap_free(&layer->buffer_queue);
1496 /* Decrement layer usrs counter */
1498 /* If this file handle has initialize encoder device, reset it */
1500 if (cfg->pixfmt == PIXFMT_NV12) {
1501 struct vpbe_layer *otherlayer;
1503 _vpbe_display_get_other_win_layer(disp_dev, layer);
1504 osd_device->ops.disable_layer(osd_device,
1505 otherlayer->layer_info.id);
1506 osd_device->ops.release_layer(osd_device,
1507 otherlayer->layer_info.id);
1509 osd_device->ops.disable_layer(osd_device,
1510 layer->layer_info.id);
1511 osd_device->ops.release_layer(osd_device,
1512 layer->layer_info.id);
1514 /* Close the priority */
1515 v4l2_prio_close(&layer->prio, fh->prio);
1516 file->private_data = NULL;
1517 mutex_unlock(&layer->opslock);
1519 /* Free memory allocated to file handle object */
1522 disp_dev->cbcr_ofst = 0;
1527 #ifdef CONFIG_VIDEO_ADV_DEBUG
1528 static int vpbe_display_g_register(struct file *file, void *priv,
1529 struct v4l2_dbg_register *reg)
1531 struct v4l2_dbg_match *match = ®->match;
1532 struct vpbe_fh *fh = file->private_data;
1533 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1535 if (match->type >= 2) {
1536 v4l2_subdev_call(vpbe_dev->venc,
1545 static int vpbe_display_s_register(struct file *file, void *priv,
1546 struct v4l2_dbg_register *reg)
1552 /* vpbe capture ioctl operations */
1553 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1554 .vidioc_querycap = vpbe_display_querycap,
1555 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1556 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1557 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1558 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1559 .vidioc_reqbufs = vpbe_display_reqbufs,
1560 .vidioc_querybuf = vpbe_display_querybuf,
1561 .vidioc_qbuf = vpbe_display_qbuf,
1562 .vidioc_dqbuf = vpbe_display_dqbuf,
1563 .vidioc_streamon = vpbe_display_streamon,
1564 .vidioc_streamoff = vpbe_display_streamoff,
1565 .vidioc_cropcap = vpbe_display_cropcap,
1566 .vidioc_g_crop = vpbe_display_g_crop,
1567 .vidioc_s_crop = vpbe_display_s_crop,
1568 .vidioc_g_priority = vpbe_display_g_priority,
1569 .vidioc_s_priority = vpbe_display_s_priority,
1570 .vidioc_s_std = vpbe_display_s_std,
1571 .vidioc_g_std = vpbe_display_g_std,
1572 .vidioc_enum_output = vpbe_display_enum_output,
1573 .vidioc_s_output = vpbe_display_s_output,
1574 .vidioc_g_output = vpbe_display_g_output,
1575 .vidioc_s_dv_preset = vpbe_display_s_dv_preset,
1576 .vidioc_g_dv_preset = vpbe_display_g_dv_preset,
1577 .vidioc_enum_dv_presets = vpbe_display_enum_dv_presets,
1578 #ifdef CONFIG_VIDEO_ADV_DEBUG
1579 .vidioc_g_register = vpbe_display_g_register,
1580 .vidioc_s_register = vpbe_display_s_register,
1584 static struct v4l2_file_operations vpbe_fops = {
1585 .owner = THIS_MODULE,
1586 .open = vpbe_display_open,
1587 .release = vpbe_display_release,
1588 .unlocked_ioctl = video_ioctl2,
1589 .mmap = vpbe_display_mmap,
1590 .poll = vpbe_display_poll
1593 static int vpbe_device_get(struct device *dev, void *data)
1595 struct platform_device *pdev = to_platform_device(dev);
1596 struct vpbe_display *vpbe_disp = data;
1598 if (strcmp("vpbe_controller", pdev->name) == 0)
1599 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1601 if (strcmp("vpbe-osd", pdev->name) == 0)
1602 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1607 static __devinit int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1608 struct platform_device *pdev)
1610 struct vpbe_layer *vpbe_display_layer = NULL;
1611 struct video_device *vbd = NULL;
1613 /* Allocate memory for four plane display objects */
1616 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1618 /* If memory allocation fails, return error */
1619 if (!disp_dev->dev[i]) {
1620 printk(KERN_ERR "ran out of memory\n");
1623 spin_lock_init(&disp_dev->dev[i]->irqlock);
1624 mutex_init(&disp_dev->dev[i]->opslock);
1626 /* Get the pointer to the layer object */
1627 vpbe_display_layer = disp_dev->dev[i];
1628 vbd = &vpbe_display_layer->video_dev;
1629 /* Initialize field of video device */
1630 vbd->release = video_device_release_empty;
1631 vbd->fops = &vpbe_fops;
1632 vbd->ioctl_ops = &vpbe_ioctl_ops;
1634 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1635 vbd->lock = &vpbe_display_layer->opslock;
1637 if (disp_dev->vpbe_dev->current_timings.timings_type &
1639 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1641 disp_dev->vpbe_dev->
1642 current_timings.timings.std_id;
1644 vbd->current_norm = 0;
1646 snprintf(vbd->name, sizeof(vbd->name),
1647 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1648 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1649 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1650 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1652 vpbe_display_layer->device_id = i;
1654 vpbe_display_layer->layer_info.id =
1655 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1657 /* Initialize prio member of layer object */
1658 v4l2_prio_init(&vpbe_display_layer->prio);
1663 static __devinit int register_device(struct vpbe_layer *vpbe_display_layer,
1664 struct vpbe_display *disp_dev,
1665 struct platform_device *pdev) {
1668 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1669 "Trying to register VPBE display device.\n");
1670 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1671 "layer=%x,layer->video_dev=%x\n",
1672 (int)vpbe_display_layer,
1673 (int)&vpbe_display_layer->video_dev);
1675 err = video_register_device(&vpbe_display_layer->video_dev,
1681 vpbe_display_layer->disp_dev = disp_dev;
1682 /* set the driver data in platform device */
1683 platform_set_drvdata(pdev, disp_dev);
1684 video_set_drvdata(&vpbe_display_layer->video_dev,
1685 vpbe_display_layer);
1693 * vpbe_display_probe()
1694 * This function creates device entries by register itself to the V4L2 driver
1695 * and initializes fields of each layer objects
1697 static __devinit int vpbe_display_probe(struct platform_device *pdev)
1699 struct vpbe_layer *vpbe_display_layer;
1700 struct vpbe_display *disp_dev;
1701 struct resource *res = NULL;
1707 printk(KERN_DEBUG "vpbe_display_probe\n");
1708 /* Allocate memory for vpbe_display */
1709 disp_dev = kzalloc(sizeof(struct vpbe_display), GFP_KERNEL);
1711 printk(KERN_ERR "ran out of memory\n");
1715 spin_lock_init(&disp_dev->dma_queue_lock);
1717 * Scan all the platform devices to find the vpbe
1718 * controller device and get the vpbe_dev object
1720 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1724 /* Initialize the vpbe display controller */
1725 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1726 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1727 disp_dev->vpbe_dev);
1729 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1730 "Error initing vpbe\n");
1736 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1737 if (init_vpbe_layer(i, disp_dev, pdev)) {
1743 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1745 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1746 "Unable to get VENC interrupt resource\n");
1752 if (request_irq(irq, venc_isr, IRQF_DISABLED, VPBE_DISPLAY_DRIVER,
1754 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1755 "Unable to request interrupt\n");
1760 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1761 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1767 printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
1771 free_irq(res->start, disp_dev);
1773 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1774 /* Get the pointer to the layer object */
1775 vpbe_display_layer = disp_dev->dev[k];
1776 /* Unregister video device */
1777 if (vpbe_display_layer) {
1778 video_unregister_device(
1779 &vpbe_display_layer->video_dev);
1780 kfree(disp_dev->dev[k]);
1788 * vpbe_display_remove()
1789 * It un-register hardware layer from V4L2 driver
1791 static int vpbe_display_remove(struct platform_device *pdev)
1793 struct vpbe_layer *vpbe_display_layer;
1794 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1795 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1796 struct resource *res;
1799 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1801 /* unregister irq */
1802 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1803 free_irq(res->start, disp_dev);
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 /* Unregister video device */
1813 video_unregister_device(&vpbe_display_layer->video_dev);
1816 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1817 kfree(disp_dev->dev[i]);
1818 disp_dev->dev[i] = NULL;
1824 static struct platform_driver vpbe_display_driver = {
1826 .name = VPBE_DISPLAY_DRIVER,
1827 .owner = THIS_MODULE,
1828 .bus = &platform_bus_type,
1830 .probe = vpbe_display_probe,
1831 .remove = __devexit_p(vpbe_display_remove),
1834 module_platform_driver(vpbe_display_driver);
1836 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1837 MODULE_LICENSE("GPL");
1838 MODULE_AUTHOR("Texas Instruments");