Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / media / video / davinci / vpbe_display.c
blob1f3b1c72925297efd8cb549dc206b18b77eab497
1 /*
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>
23 #include <linux/mm.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"
44 static int debug;
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;
53 int ret;
54 int val;
56 ret = v4l2_subdev_call(vpbe_dev->venc,
57 core,
58 ioctl,
59 VENC_GET_FLD,
60 &val);
61 if (ret < 0) {
62 v4l2_err(&vpbe_dev->v4l2_dev,
63 "Error in getting Field ID 0\n");
65 return val;
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)
74 return;
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;
88 unsigned long addr;
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);
94 return;
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,
105 queue);
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,
114 addr,
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;
124 unsigned event = 0;
125 int fid;
126 int i;
128 if ((NULL == arg) || (NULL == disp_dev->dev[0]))
129 return IRQ_HANDLED;
131 if (venc_is_second_field(disp_dev))
132 event |= VENC_SECOND_FIELD;
133 else
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;
149 last_event = event;
151 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
152 layer = disp_dev->dev[i];
153 /* If streaming is started in this layer */
154 if (!layer->started)
155 continue;
157 if (layer->layer_first_int) {
158 layer->layer_first_int = 0;
159 continue;
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);
168 } else {
169 /* Interlaced mode */
171 layer->field_id ^= 1;
172 if (event & VENC_FIRST_FIELD)
173 fid = 0;
174 else
175 fid = 1;
178 * If field id does not match with store
179 * field id
181 if (fid != layer->field_id) {
182 /* Make them in sync */
183 layer->field_id = fid;
184 continue;
187 * device field id and local field id are
188 * in sync. If this is even field
190 if (0 == fid)
191 vpbe_isr_even_field(disp_dev, layer);
192 else /* odd field */
193 vpbe_isr_odd_field(disp_dev, layer);
197 return IRQ_HANDLED;
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
204 * physical address
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;
213 unsigned long addr;
214 int ret;
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;
224 vb->field = field;
226 ret = videobuf_iolock(q, vb, NULL);
227 if (ret < 0) {
228 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to map \
229 user address\n");
230 return -EINVAL;
233 addr = videobuf_to_dma_contig(vb);
235 if (q->streaming) {
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");
240 return -EINVAL;
243 vb->state = VIDEOBUF_PREPARED;
245 return 0;
249 * vpbe_buffer_setup()
250 * This function allocates memory for the buffers
252 static int vpbe_buffer_setup(struct videobuf_queue *q,
253 unsigned int *count,
254 unsigned int *size)
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;
269 return 0;
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;
284 unsigned long flags;
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
300 * the buffers
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,
326 static
327 struct vpbe_layer*
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;
345 unsigned long addr;
346 int ret;
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,
352 addr,
353 disp_dev->cbcr_ofst);
355 ret = osd_device->ops.enable_layer(osd_device,
356 layer->layer_info.id, 0);
357 if (ret < 0) {
358 v4l2_err(&vpbe_dev->v4l2_dev,
359 "Error in enabling osd window layer 0\n");
360 return -1;
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);
371 if (ret < 0) {
372 v4l2_err(&vpbe_dev->v4l2_dev,
373 "Error in enabling osd window layer 1\n");
374 return -1;
376 otherlayer->layer_info.enable = 1;
378 return 0;
381 static void
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;
391 int h_exp = 0;
392 int v_exp = 0;
393 int h_scale;
394 int v_scale;
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;
424 if (h_scale < 2)
425 h_scale = 1;
426 else if (h_scale >= 4)
427 h_scale = 4;
428 else
429 h_scale = 2;
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) {
438 h_exp = 1;
439 cfg->xsize = calculated_xsize;
443 if (h_scale == 2)
444 layer_info->h_zoom = ZOOM_X2;
445 else if (h_scale == 4)
446 layer_info->h_zoom = ZOOM_X4;
447 if (h_exp)
448 layer_info->h_exp = H_EXP_9_OVER_8;
449 } else {
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;
456 if (v_scale < 2)
457 v_scale = 1;
458 else if (v_scale >= 4)
459 v_scale = 4;
460 else
461 v_scale = 2;
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) {
469 v_exp = 1;
470 cfg->ysize = calculated_xsize;
474 if (v_scale == 2)
475 layer_info->v_zoom = ZOOM_X2;
476 else if (v_scale == 4)
477 layer_info->v_zoom = ZOOM_X4;
478 if (v_exp)
479 layer_info->h_exp = V_EXP_6_OVER_5;
480 } else {
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,
491 int top, int left)
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,
507 struct v4l2_rect *c)
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);
526 * vpbe_try_format()
527 * If user application provides width and height, and have bytesperline set
528 * to zero, driver calculates bytesperline and sizeimage based on hardware
529 * limits.
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;
535 int min_height = 1;
536 int min_width = 32;
537 int max_height;
538 int max_width;
539 int bpp;
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;
551 else
552 pixfmt->field = V4L2_FIELD_NONE;
555 if (pixfmt->field == V4L2_FIELD_INTERLACED)
556 min_height = 2;
558 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
559 bpp = 1;
560 else
561 bpp = 2;
563 max_width = vpbe_dev->current_timings.xres;
564 max_height = vpbe_dev->current_timings.yres;
566 min_width /= bpp;
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);
587 else
588 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
590 return 0;
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);
601 return 0;
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;
609 int ret;
611 ret = v4l2_prio_change(&layer->prio, &fh->prio, p);
613 return ret;
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));
628 return 0;
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;
641 int ret;
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");
648 return -EINVAL;
651 if (rect->top < 0)
652 rect->top = 0;
653 if (rect->left < 0)
654 rect->left = 0;
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,
662 rect->width,
663 rect->height);
664 vpbe_disp_adj_position(disp_dev, layer, rect->top,
665 rect->left);
666 ret = osd_device->ops.set_layer_config(osd_device,
667 layer->layer_info.id, cfg);
668 if (ret < 0) {
669 v4l2_err(&vpbe_dev->v4l2_dev,
670 "Error in set layer config:\n");
671 return -EINVAL;
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);
682 if (ret < 0) {
683 v4l2_err(&vpbe_dev->v4l2_dev,
684 "Error in set vid expansion:\n");
685 return -EINVAL;
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);
694 else
695 osd_device->ops.set_interpolation_filter(osd_device, 0);
697 return 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;
709 int ret;
711 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
712 "VIDIOC_G_CROP, layer id = %d\n",
713 layer->device_id);
715 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
716 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
717 ret = -EINVAL;
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;
726 return 0;
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;
744 return 0;
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",
756 layer->device_id);
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");
761 return -EINVAL;
763 /* Fill in the information about format */
764 fmt->fmt.pix = layer->pix_fmt;
766 return 0;
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",
779 layer->device_id);
780 if (fmt->index > 1) {
781 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
782 return -EINVAL;
785 /* Fill in the information about format */
786 index = fmt->index;
787 memset(fmt, 0, sizeof(*fmt));
788 fmt->index = index;
789 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
790 if (index == 0) {
791 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
792 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
793 } else {
794 strcpy(fmt->description, "Y/CbCr 4:2:0");
795 fmt->pixelformat = V4L2_PIX_FMT_NV12;
798 return 0;
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;
811 int ret;
813 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
814 "VIDIOC_S_FMT, layer id = %d\n",
815 layer->device_id);
817 /* If streaming is started, return error */
818 if (layer->started) {
819 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
820 return -EBUSY;
822 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
823 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
824 return -EINVAL;
826 /* Check for valid pixel format */
827 ret = vpbe_try_format(disp_dev, pixfmt, 1);
828 if (ret)
829 return ret;
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;
843 cfg->ypos = 0;
844 cfg->xpos = 0;
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,
855 layer);
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);
862 if (ret < 0) {
863 v4l2_err(&vpbe_dev->v4l2_dev,
864 "Error in S_FMT params:\n");
865 return -EINVAL;
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);
872 return 0;
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");
887 return -EINVAL;
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,
902 v4l2_std_id *std_id)
904 struct vpbe_fh *fh = priv;
905 struct vpbe_layer *layer = fh->layer;
906 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
907 int ret;
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");
914 return -EBUSY;
916 if (NULL != vpbe_dev->ops.s_std) {
917 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
918 if (ret) {
919 v4l2_err(&vpbe_dev->v4l2_dev,
920 "Failed to set standard for sub devices\n");
921 return -EINVAL;
923 } else {
924 return -EINVAL;
927 return 0;
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
934 * -EINVAL on error
936 static int vpbe_display_g_std(struct file *file, void *priv,
937 v4l2_std_id *std_id)
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;
947 return 0;
950 return -EINVAL;
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;
964 int ret;
966 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
968 /* Enumerate outputs */
970 if (NULL == vpbe_dev->ops.enum_outputs)
971 return -EINVAL;
973 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
974 if (ret) {
975 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
976 "Failed to enumerate outputs\n");
977 return -EINVAL;
980 return 0;
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,
988 unsigned int i)
990 struct vpbe_fh *fh = priv;
991 struct vpbe_layer *layer = fh->layer;
992 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
993 int ret;
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");
999 return -EBUSY;
1001 if (NULL == vpbe_dev->ops.set_output)
1002 return -EINVAL;
1004 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1005 if (ret) {
1006 v4l2_err(&vpbe_dev->v4l2_dev,
1007 "Failed to set output for sub devices\n");
1008 return -EINVAL;
1011 return 0;
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,
1019 unsigned int *i)
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;
1028 return 0;
1032 * vpbe_display_enum_dv_presets - Enumerate the dv presets
1034 * enum the preset in the current encoder. Return the status. 0 - success
1035 * -EINVAL on error
1037 static int
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;
1043 int ret;
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)
1049 return -EINVAL;
1051 ret = vpbe_dev->ops.enum_dv_presets(vpbe_dev, preset);
1052 if (ret) {
1053 v4l2_err(&vpbe_dev->v4l2_dev,
1054 "Failed to enumerate dv presets info\n");
1055 return -EINVAL;
1058 return 0;
1062 * vpbe_display_s_dv_preset - Set the dv presets
1064 * Set the preset in the current encoder. Return the status. 0 - success
1065 * -EINVAL on error
1067 static int
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;
1074 int ret;
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");
1082 return -EBUSY;
1085 /* Set the given standard in the encoder */
1086 if (NULL != vpbe_dev->ops.s_dv_preset)
1087 return -EINVAL;
1089 ret = vpbe_dev->ops.s_dv_preset(vpbe_dev, preset);
1090 if (ret) {
1091 v4l2_err(&vpbe_dev->v4l2_dev,
1092 "Failed to set the dv presets info\n");
1093 return -EINVAL;
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;
1100 return 0;
1104 * vpbe_display_g_dv_preset - Set the dv presets
1106 * Get the preset in the current encoder. Return the status. 0 - success
1107 * -EINVAL on error
1109 static int
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) {
1122 dv_preset->preset =
1123 vpbe_dev->current_timings.timings.dv_preset;
1124 } else {
1125 return -EINVAL;
1128 return 0;
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;
1138 int ret;
1140 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1141 "VIDIOC_STREAMOFF,layer id = %d\n",
1142 layer->device_id);
1144 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1145 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1146 return -EINVAL;
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");
1152 return -EACCES;
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);
1159 return -EINVAL;
1162 osd_device->ops.disable_layer(osd_device,
1163 layer->layer_info.id);
1164 layer->started = 0;
1165 ret = videobuf_streamoff(&layer->buffer_queue);
1167 return ret;
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;
1178 int ret;
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",
1184 layer->device_id);
1186 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1187 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1188 return -EINVAL;
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");
1194 return -EACCES;
1196 /* If Streaming is already started, return error */
1197 if (layer->started) {
1198 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1199 return -EBUSY;
1203 * Call videobuf_streamon to start streaming
1204 * in videobuf
1206 ret = videobuf_streamon(&layer->buffer_queue);
1207 if (ret) {
1208 v4l2_err(&vpbe_dev->v4l2_dev,
1209 "error in videobuf_streamon\n");
1210 return ret;
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");
1215 goto streamoff;
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);
1229 if (ret < 0)
1230 goto streamoff;
1233 * if request format is yuv420 semiplanar, need to
1234 * enable both video windows
1236 layer->started = 1;
1238 layer->layer_first_int = 1;
1240 return ret;
1241 streamoff:
1242 ret = videobuf_streamoff(&layer->buffer_queue);
1243 return ret;
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;
1252 int ret;
1254 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1255 "VIDIOC_DQBUF, layer id = %d\n",
1256 layer->device_id);
1258 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1259 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1260 return -EINVAL;
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");
1265 return -EACCES;
1267 if (file->f_flags & O_NONBLOCK)
1268 /* Call videobuf_dqbuf for non blocking mode */
1269 ret = videobuf_dqbuf(&layer->buffer_queue, buf, 1);
1270 else
1271 /* Call videobuf_dqbuf for blocking mode */
1272 ret = videobuf_dqbuf(&layer->buffer_queue, buf, 0);
1274 return ret;
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",
1286 layer->device_id);
1288 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1289 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1290 return -EINVAL;
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");
1296 return -EACCES;
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;
1308 int ret;
1310 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1311 "VIDIOC_QUERYBUF, layer id = %d\n",
1312 layer->device_id);
1314 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1315 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1316 return -EINVAL;
1319 /* Call videobuf_querybuf to get information */
1320 ret = videobuf_querybuf(&layer->buffer_queue, buf);
1322 return ret;
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;
1331 int ret;
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");
1337 return -EINVAL;
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");
1343 return -EBUSY;
1345 /* Initialize videobuf queue as per the buffer type */
1346 videobuf_queue_dma_contig_init(&layer->buffer_queue,
1347 &video_qops,
1348 vpbe_dev->pdev,
1349 &layer->irqlock,
1350 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1351 layer->pix_fmt.field,
1352 sizeof(struct videobuf_buffer),
1353 fh, NULL);
1355 /* Set io allowed member of file handle to TRUE */
1356 fh->io_allowed = 1;
1357 /* Increment io usrs member of layer object to 1 */
1358 layer->io_usrs = 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);
1366 return ret;
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;
1380 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1382 return videobuf_mmap_mapper(&layer->buffer_queue, vma);
1385 /* vpbe_display_poll(): It is used for select/poll system call
1387 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1389 struct vpbe_fh *fh = filep->private_data;
1390 struct vpbe_layer *layer = fh->layer;
1391 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1392 unsigned int err = 0;
1394 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1395 if (layer->started)
1396 err = videobuf_poll_stream(filep, &layer->buffer_queue, wait);
1397 return err;
1401 * vpbe_display_open()
1402 * It creates object of file handle structure and stores it in private_data
1403 * member of filepointer
1405 static int vpbe_display_open(struct file *file)
1407 struct vpbe_fh *fh = NULL;
1408 struct vpbe_layer *layer = video_drvdata(file);
1409 struct vpbe_display *disp_dev = layer->disp_dev;
1410 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1411 struct osd_state *osd_device = disp_dev->osd_device;
1412 int err;
1414 /* Allocate memory for the file handle object */
1415 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1416 if (fh == NULL) {
1417 v4l2_err(&vpbe_dev->v4l2_dev,
1418 "unable to allocate memory for file handle object\n");
1419 return -ENOMEM;
1421 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1422 "vpbe display open plane = %d\n",
1423 layer->device_id);
1425 /* store pointer to fh in private_data member of filep */
1426 file->private_data = fh;
1427 fh->layer = layer;
1428 fh->disp_dev = disp_dev;
1430 if (!layer->usrs) {
1432 /* First claim the layer for this device */
1433 err = osd_device->ops.request_layer(osd_device,
1434 layer->layer_info.id);
1435 if (err < 0) {
1436 /* Couldn't get layer */
1437 v4l2_err(&vpbe_dev->v4l2_dev,
1438 "Display Manager failed to allocate layer\n");
1439 kfree(fh);
1440 return -EINVAL;
1443 /* Increment layer usrs counter */
1444 layer->usrs++;
1445 /* Set io_allowed member to false */
1446 fh->io_allowed = 0;
1447 /* Initialize priority of this instance to default priority */
1448 fh->prio = V4L2_PRIORITY_UNSET;
1449 v4l2_prio_open(&layer->prio, &fh->prio);
1450 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1451 "vpbe display device opened successfully\n");
1452 return 0;
1456 * vpbe_display_release()
1457 * This function deletes buffer queue, frees the buffers and the davinci
1458 * display file * handle
1460 static int vpbe_display_release(struct file *file)
1462 /* Get the layer object and file handle object */
1463 struct vpbe_fh *fh = file->private_data;
1464 struct vpbe_layer *layer = fh->layer;
1465 struct osd_layer_config *cfg = &layer->layer_info.config;
1466 struct vpbe_display *disp_dev = fh->disp_dev;
1467 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1468 struct osd_state *osd_device = disp_dev->osd_device;
1470 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1472 /* if this instance is doing IO */
1473 if (fh->io_allowed) {
1474 /* Reset io_usrs member of layer object */
1475 layer->io_usrs = 0;
1477 osd_device->ops.disable_layer(osd_device,
1478 layer->layer_info.id);
1479 layer->started = 0;
1480 /* Free buffers allocated */
1481 videobuf_queue_cancel(&layer->buffer_queue);
1482 videobuf_mmap_free(&layer->buffer_queue);
1485 /* Decrement layer usrs counter */
1486 layer->usrs--;
1487 /* If this file handle has initialize encoder device, reset it */
1488 if (!layer->usrs) {
1489 if (cfg->pixfmt == PIXFMT_NV12) {
1490 struct vpbe_layer *otherlayer;
1491 otherlayer =
1492 _vpbe_display_get_other_win_layer(disp_dev, layer);
1493 osd_device->ops.disable_layer(osd_device,
1494 otherlayer->layer_info.id);
1495 osd_device->ops.release_layer(osd_device,
1496 otherlayer->layer_info.id);
1498 osd_device->ops.disable_layer(osd_device,
1499 layer->layer_info.id);
1500 osd_device->ops.release_layer(osd_device,
1501 layer->layer_info.id);
1503 /* Close the priority */
1504 v4l2_prio_close(&layer->prio, fh->prio);
1505 file->private_data = NULL;
1507 /* Free memory allocated to file handle object */
1508 kfree(fh);
1510 disp_dev->cbcr_ofst = 0;
1512 return 0;
1515 #ifdef CONFIG_VIDEO_ADV_DEBUG
1516 static int vpbe_display_g_register(struct file *file, void *priv,
1517 struct v4l2_dbg_register *reg)
1519 struct v4l2_dbg_match *match = &reg->match;
1521 if (match->type >= 2) {
1522 v4l2_subdev_call(vpbe_dev->venc,
1523 core,
1524 g_register,
1525 reg);
1528 return 0;
1531 static int vpbe_display_s_register(struct file *file, void *priv,
1532 struct v4l2_dbg_register *reg)
1534 return 0;
1536 #endif
1538 /* vpbe capture ioctl operations */
1539 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1540 .vidioc_querycap = vpbe_display_querycap,
1541 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1542 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1543 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1544 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1545 .vidioc_reqbufs = vpbe_display_reqbufs,
1546 .vidioc_querybuf = vpbe_display_querybuf,
1547 .vidioc_qbuf = vpbe_display_qbuf,
1548 .vidioc_dqbuf = vpbe_display_dqbuf,
1549 .vidioc_streamon = vpbe_display_streamon,
1550 .vidioc_streamoff = vpbe_display_streamoff,
1551 .vidioc_cropcap = vpbe_display_cropcap,
1552 .vidioc_g_crop = vpbe_display_g_crop,
1553 .vidioc_s_crop = vpbe_display_s_crop,
1554 .vidioc_g_priority = vpbe_display_g_priority,
1555 .vidioc_s_priority = vpbe_display_s_priority,
1556 .vidioc_s_std = vpbe_display_s_std,
1557 .vidioc_g_std = vpbe_display_g_std,
1558 .vidioc_enum_output = vpbe_display_enum_output,
1559 .vidioc_s_output = vpbe_display_s_output,
1560 .vidioc_g_output = vpbe_display_g_output,
1561 .vidioc_s_dv_preset = vpbe_display_s_dv_preset,
1562 .vidioc_g_dv_preset = vpbe_display_g_dv_preset,
1563 .vidioc_enum_dv_presets = vpbe_display_enum_dv_presets,
1564 #ifdef CONFIG_VIDEO_ADV_DEBUG
1565 .vidioc_g_register = vpbe_display_g_register,
1566 .vidioc_s_register = vpbe_display_s_register,
1567 #endif
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 (strcmp("vpbe-osd", pdev->name) == 0)
1588 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1590 return 0;
1593 static __devinit 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 */
1601 disp_dev->dev[i] =
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");
1607 return -ENOMEM;
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;
1619 vbd->minor = -1;
1620 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1621 vbd->lock = &vpbe_display_layer->opslock;
1623 if (disp_dev->vpbe_dev->current_timings.timings_type &
1624 VPBE_ENC_STD) {
1625 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1626 vbd->current_norm =
1627 disp_dev->vpbe_dev->
1628 current_timings.timings.std_id;
1629 } else
1630 vbd->current_norm = 0;
1632 snprintf(vbd->name, sizeof(vbd->name),
1633 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1634 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1635 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1636 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1638 vpbe_display_layer->device_id = i;
1640 vpbe_display_layer->layer_info.id =
1641 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1643 /* Initialize prio member of layer object */
1644 v4l2_prio_init(&vpbe_display_layer->prio);
1646 return 0;
1649 static __devinit int register_device(struct vpbe_layer *vpbe_display_layer,
1650 struct vpbe_display *disp_dev,
1651 struct platform_device *pdev) {
1652 int err;
1654 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1655 "Trying to register VPBE display device.\n");
1656 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1657 "layer=%x,layer->video_dev=%x\n",
1658 (int)vpbe_display_layer,
1659 (int)&vpbe_display_layer->video_dev);
1661 err = video_register_device(&vpbe_display_layer->video_dev,
1662 VFL_TYPE_GRABBER,
1663 -1);
1664 if (err)
1665 return -ENODEV;
1667 vpbe_display_layer->disp_dev = disp_dev;
1668 /* set the driver data in platform device */
1669 platform_set_drvdata(pdev, disp_dev);
1670 video_set_drvdata(&vpbe_display_layer->video_dev,
1671 vpbe_display_layer);
1673 return 0;
1679 * vpbe_display_probe()
1680 * This function creates device entries by register itself to the V4L2 driver
1681 * and initializes fields of each layer objects
1683 static __devinit int vpbe_display_probe(struct platform_device *pdev)
1685 struct vpbe_layer *vpbe_display_layer;
1686 struct vpbe_display *disp_dev;
1687 struct resource *res = NULL;
1688 int k;
1689 int i;
1690 int err;
1691 int irq;
1693 printk(KERN_DEBUG "vpbe_display_probe\n");
1694 /* Allocate memory for vpbe_display */
1695 disp_dev = kzalloc(sizeof(struct vpbe_display), GFP_KERNEL);
1696 if (!disp_dev) {
1697 printk(KERN_ERR "ran out of memory\n");
1698 return -ENOMEM;
1701 spin_lock_init(&disp_dev->dma_queue_lock);
1703 * Scan all the platform devices to find the vpbe
1704 * controller device and get the vpbe_dev object
1706 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1707 vpbe_device_get);
1708 if (err < 0)
1709 return err;
1710 /* Initialize the vpbe display controller */
1711 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1712 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1713 disp_dev->vpbe_dev);
1714 if (err) {
1715 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1716 "Error initing vpbe\n");
1717 err = -ENOMEM;
1718 goto probe_out;
1722 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1723 if (init_vpbe_layer(i, disp_dev, pdev)) {
1724 err = -ENODEV;
1725 goto probe_out;
1729 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1730 if (!res) {
1731 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1732 "Unable to get VENC interrupt resource\n");
1733 err = -ENODEV;
1734 goto probe_out;
1737 irq = res->start;
1738 if (request_irq(irq, venc_isr, IRQF_DISABLED, VPBE_DISPLAY_DRIVER,
1739 disp_dev)) {
1740 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1741 "Unable to request interrupt\n");
1742 err = -ENODEV;
1743 goto probe_out;
1746 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1747 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1748 err = -ENODEV;
1749 goto probe_out_irq;
1753 printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
1754 return 0;
1756 probe_out_irq:
1757 free_irq(res->start, disp_dev);
1758 probe_out:
1759 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1760 /* Get the pointer to the layer object */
1761 vpbe_display_layer = disp_dev->dev[k];
1762 /* Unregister video device */
1763 if (vpbe_display_layer) {
1764 video_unregister_device(
1765 &vpbe_display_layer->video_dev);
1766 kfree(disp_dev->dev[k]);
1769 kfree(disp_dev);
1770 return err;
1774 * vpbe_display_remove()
1775 * It un-register hardware layer from V4L2 driver
1777 static int vpbe_display_remove(struct platform_device *pdev)
1779 struct vpbe_layer *vpbe_display_layer;
1780 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1781 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1782 struct resource *res;
1783 int i;
1785 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1787 /* unregister irq */
1788 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1789 free_irq(res->start, disp_dev);
1791 /* deinitialize the vpbe display controller */
1792 if (NULL != vpbe_dev->ops.deinitialize)
1793 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1794 /* un-register device */
1795 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1796 /* Get the pointer to the layer object */
1797 vpbe_display_layer = disp_dev->dev[i];
1798 /* Unregister video device */
1799 video_unregister_device(&vpbe_display_layer->video_dev);
1802 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1803 kfree(disp_dev->dev[i]);
1804 disp_dev->dev[i] = NULL;
1807 return 0;
1810 static struct platform_driver vpbe_display_driver = {
1811 .driver = {
1812 .name = VPBE_DISPLAY_DRIVER,
1813 .owner = THIS_MODULE,
1814 .bus = &platform_bus_type,
1816 .probe = vpbe_display_probe,
1817 .remove = __devexit_p(vpbe_display_remove),
1820 module_platform_driver(vpbe_display_driver);
1822 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1823 MODULE_LICENSE("GPL");
1824 MODULE_AUTHOR("Texas Instruments");