2 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/interrupt.h>
18 #include <linux/string.h>
19 #include <linux/wait.h>
20 #include <linux/time.h>
21 #include <linux/platform_device.h>
22 #include <linux/irq.h>
24 #include <linux/mutex.h>
25 #include <linux/videodev2.h>
26 #include <linux/slab.h>
28 #include <asm/pgtable.h>
29 #include <mach/cputype.h>
31 #include <media/v4l2-dev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-device.h>
35 #include <media/davinci/vpbe_display.h>
36 #include <media/davinci/vpbe_types.h>
37 #include <media/davinci/vpbe.h>
38 #include <media/davinci/vpbe_venc.h>
39 #include <media/davinci/vpbe_osd.h>
40 #include "vpbe_venc_regs.h"
42 #define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
46 #define VPBE_DEFAULT_NUM_BUFS 3
48 module_param(debug, int, 0644);
50 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
51 struct vpbe_layer *layer);
53 static int venc_is_second_field(struct vpbe_display *disp_dev)
55 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
59 ret = v4l2_subdev_call(vpbe_dev->venc,
65 v4l2_err(&vpbe_dev->v4l2_dev,
66 "Error in getting Field ID 0\n");
71 static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
72 struct vpbe_layer *layer)
74 struct timespec timevalue;
76 if (layer->cur_frm == layer->next_frm)
78 ktime_get_ts(&timevalue);
79 layer->cur_frm->vb.v4l2_buf.timestamp.tv_sec =
81 layer->cur_frm->vb.v4l2_buf.timestamp.tv_usec =
82 timevalue.tv_nsec / NSEC_PER_USEC;
83 vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_DONE);
84 /* Make cur_frm pointing to next_frm */
85 layer->cur_frm = layer->next_frm;
88 static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
89 struct vpbe_layer *layer)
91 struct osd_state *osd_device = disp_obj->osd_device;
94 spin_lock(&disp_obj->dma_queue_lock);
95 if (list_empty(&layer->dma_queue) ||
96 (layer->cur_frm != layer->next_frm)) {
97 spin_unlock(&disp_obj->dma_queue_lock);
101 * one field is displayed configure
102 * the next frame if it is available
103 * otherwise hold on current frame
104 * Get next from the buffer queue
106 layer->next_frm = list_entry(layer->dma_queue.next,
107 struct vpbe_disp_buffer, list);
108 /* Remove that from the buffer queue */
109 list_del(&layer->next_frm->list);
110 spin_unlock(&disp_obj->dma_queue_lock);
111 /* Mark state of the frame to active */
112 layer->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
113 addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb, 0);
114 osd_device->ops.start_layer(osd_device,
115 layer->layer_info.id,
117 disp_obj->cbcr_ofst);
120 /* interrupt service routine */
121 static irqreturn_t venc_isr(int irq, void *arg)
123 struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
124 struct vpbe_layer *layer;
125 static unsigned last_event;
130 if ((NULL == arg) || (NULL == disp_dev->dev[0]))
133 if (venc_is_second_field(disp_dev))
134 event |= VENC_SECOND_FIELD;
136 event |= VENC_FIRST_FIELD;
138 if (event == (last_event & ~VENC_END_OF_FRAME)) {
140 * If the display is non-interlaced, then we need to flag the
141 * end-of-frame event at every interrupt regardless of the
142 * value of the FIDST bit. We can conclude that the display is
143 * non-interlaced if the value of the FIDST bit is unchanged
144 * from the previous interrupt.
146 event |= VENC_END_OF_FRAME;
147 } else if (event == VENC_SECOND_FIELD) {
148 /* end-of-frame for interlaced display */
149 event |= VENC_END_OF_FRAME;
153 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
154 layer = disp_dev->dev[i];
155 /* If streaming is started in this layer */
159 if (layer->layer_first_int) {
160 layer->layer_first_int = 0;
163 /* Check the field format */
164 if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
165 (event & VENC_END_OF_FRAME)) {
166 /* Progressive mode */
168 vpbe_isr_even_field(disp_dev, layer);
169 vpbe_isr_odd_field(disp_dev, layer);
171 /* Interlaced mode */
173 layer->field_id ^= 1;
174 if (event & VENC_FIRST_FIELD)
180 * If field id does not match with store
183 if (fid != layer->field_id) {
184 /* Make them in sync */
185 layer->field_id = fid;
189 * device field id and local field id are
190 * in sync. If this is even field
193 vpbe_isr_even_field(disp_dev, layer);
195 vpbe_isr_odd_field(disp_dev, layer);
203 * vpbe_buffer_prepare()
204 * This is the callback function called from vb2_qbuf() function
205 * the buffer is prepared and user space virtual address is converted into
208 static int vpbe_buffer_prepare(struct vb2_buffer *vb)
210 struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
211 struct vb2_queue *q = vb->vb2_queue;
212 struct vpbe_layer *layer = fh->layer;
213 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
216 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
217 "vpbe_buffer_prepare\n");
219 if (vb->state != VB2_BUF_STATE_ACTIVE &&
220 vb->state != VB2_BUF_STATE_PREPARED) {
221 vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
222 if (vb2_plane_vaddr(vb, 0) &&
223 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
226 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
228 if (!IS_ALIGNED(addr, 8)) {
229 v4l2_err(&vpbe_dev->v4l2_dev,
230 "buffer_prepare:offset is \
231 not aligned to 32 bytes\n");
240 * vpbe_buffer_setup()
241 * This function allocates memory for the buffers
244 vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
245 unsigned int *nbuffers, unsigned int *nplanes,
246 unsigned int sizes[], void *alloc_ctxs[])
249 /* Get the file handle object and layer object */
250 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
251 struct vpbe_layer *layer = fh->layer;
252 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
254 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
256 /* Store number of buffers allocated in numbuffer member */
257 if (*nbuffers < VPBE_DEFAULT_NUM_BUFS)
258 *nbuffers = layer->numbuffers = VPBE_DEFAULT_NUM_BUFS;
261 sizes[0] = layer->pix_fmt.sizeimage;
262 alloc_ctxs[0] = layer->alloc_ctx;
268 * vpbe_buffer_queue()
269 * This function adds the buffer to DMA queue
271 static void vpbe_buffer_queue(struct vb2_buffer *vb)
273 /* Get the file handle object and layer object */
274 struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
275 struct vpbe_disp_buffer *buf = container_of(vb,
276 struct vpbe_disp_buffer, vb);
277 struct vpbe_layer *layer = fh->layer;
278 struct vpbe_display *disp = fh->disp_dev;
279 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
282 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
283 "vpbe_buffer_queue\n");
285 /* add the buffer to the DMA queue */
286 spin_lock_irqsave(&disp->dma_queue_lock, flags);
287 list_add_tail(&buf->list, &layer->dma_queue);
288 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
293 * This function is called from the vb2 layer to free memory allocated to
296 static void vpbe_buf_cleanup(struct vb2_buffer *vb)
298 /* Get the file handle object and layer object */
299 struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
300 struct vpbe_layer *layer = fh->layer;
301 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
302 struct vpbe_disp_buffer *buf = container_of(vb,
303 struct vpbe_disp_buffer, vb);
306 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
307 "vpbe_buf_cleanup\n");
309 spin_lock_irqsave(&layer->irqlock, flags);
310 if (vb->state == VB2_BUF_STATE_ACTIVE)
311 list_del_init(&buf->list);
312 spin_unlock_irqrestore(&layer->irqlock, flags);
315 static void vpbe_wait_prepare(struct vb2_queue *vq)
317 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
318 struct vpbe_layer *layer = fh->layer;
320 mutex_unlock(&layer->opslock);
323 static void vpbe_wait_finish(struct vb2_queue *vq)
325 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
326 struct vpbe_layer *layer = fh->layer;
328 mutex_lock(&layer->opslock);
331 static int vpbe_buffer_init(struct vb2_buffer *vb)
333 struct vpbe_disp_buffer *buf = container_of(vb,
334 struct vpbe_disp_buffer, vb);
336 INIT_LIST_HEAD(&buf->list);
340 static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
342 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
343 struct vpbe_layer *layer = fh->layer;
344 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
347 /* Get the next frame from the buffer queue */
348 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
349 struct vpbe_disp_buffer, list);
350 /* Remove buffer from the buffer queue */
351 list_del(&layer->cur_frm->list);
352 /* Mark state of the current frame to active */
353 layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
354 /* Initialize field_id and started member */
357 /* Set parameters in OSD and VENC */
358 ret = vpbe_set_osd_display_params(fh->disp_dev, layer);
363 * if request format is yuv420 semiplanar, need to
364 * enable both video windows
367 layer->layer_first_int = 1;
372 static int vpbe_stop_streaming(struct vb2_queue *vq)
374 struct vpbe_fh *fh = vb2_get_drv_priv(vq);
375 struct vpbe_layer *layer = fh->layer;
377 if (!vb2_is_streaming(vq))
380 /* release all active buffers */
381 while (!list_empty(&layer->dma_queue)) {
382 layer->next_frm = list_entry(layer->dma_queue.next,
383 struct vpbe_disp_buffer, list);
384 list_del(&layer->next_frm->list);
385 vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
391 static struct vb2_ops video_qops = {
392 .queue_setup = vpbe_buffer_queue_setup,
393 .wait_prepare = vpbe_wait_prepare,
394 .wait_finish = vpbe_wait_finish,
395 .buf_init = vpbe_buffer_init,
396 .buf_prepare = vpbe_buffer_prepare,
397 .start_streaming = vpbe_start_streaming,
398 .stop_streaming = vpbe_stop_streaming,
399 .buf_cleanup = vpbe_buf_cleanup,
400 .buf_queue = vpbe_buffer_queue,
405 _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
406 struct vpbe_layer *layer)
408 enum vpbe_display_device_id thiswin, otherwin;
409 thiswin = layer->device_id;
411 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
412 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
413 return disp_dev->dev[otherwin];
416 static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
417 struct vpbe_layer *layer)
419 struct osd_layer_config *cfg = &layer->layer_info.config;
420 struct osd_state *osd_device = disp_dev->osd_device;
421 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
425 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
426 /* Set address in the display registers */
427 osd_device->ops.start_layer(osd_device,
428 layer->layer_info.id,
430 disp_dev->cbcr_ofst);
432 ret = osd_device->ops.enable_layer(osd_device,
433 layer->layer_info.id, 0);
435 v4l2_err(&vpbe_dev->v4l2_dev,
436 "Error in enabling osd window layer 0\n");
440 /* Enable the window */
441 layer->layer_info.enable = 1;
442 if (cfg->pixfmt == PIXFMT_NV12) {
443 struct vpbe_layer *otherlayer =
444 _vpbe_display_get_other_win_layer(disp_dev, layer);
446 ret = osd_device->ops.enable_layer(osd_device,
447 otherlayer->layer_info.id, 1);
449 v4l2_err(&vpbe_dev->v4l2_dev,
450 "Error in enabling osd window layer 1\n");
453 otherlayer->layer_info.enable = 1;
459 vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
460 struct vpbe_layer *layer,
461 int expected_xsize, int expected_ysize)
463 struct display_layer_info *layer_info = &layer->layer_info;
464 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
465 struct osd_layer_config *cfg = &layer->layer_info.config;
466 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
467 int calculated_xsize;
473 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
476 * Application initially set the image format. Current display
477 * size is obtained from the vpbe display controller. expected_xsize
478 * and expected_ysize are set through S_CROP ioctl. Based on this,
479 * driver will calculate the scale factors for vertical and
480 * horizontal direction so that the image is displayed scaled
481 * and expanded. Application uses expansion to display the image
482 * in a square pixel. Otherwise it is displayed using displays
483 * pixel aspect ratio.It is expected that application chooses
484 * the crop coordinates for cropped or scaled display. if crop
485 * size is less than the image size, it is displayed cropped or
486 * it is displayed scaled and/or expanded.
488 * to begin with, set the crop window same as expected. Later we
489 * will override with scaled window size
492 cfg->xsize = pixfmt->width;
493 cfg->ysize = pixfmt->height;
494 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
495 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
496 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
497 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
499 if (pixfmt->width < expected_xsize) {
500 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
503 else if (h_scale >= 4)
507 cfg->xsize *= h_scale;
508 if (cfg->xsize < expected_xsize) {
509 if ((standard_id & V4L2_STD_525_60) ||
510 (standard_id & V4L2_STD_625_50)) {
511 calculated_xsize = (cfg->xsize *
512 VPBE_DISPLAY_H_EXP_RATIO_N) /
513 VPBE_DISPLAY_H_EXP_RATIO_D;
514 if (calculated_xsize <= expected_xsize) {
516 cfg->xsize = calculated_xsize;
521 layer_info->h_zoom = ZOOM_X2;
522 else if (h_scale == 4)
523 layer_info->h_zoom = ZOOM_X4;
525 layer_info->h_exp = H_EXP_9_OVER_8;
527 /* no scaling, only cropping. Set display area to crop area */
528 cfg->xsize = expected_xsize;
531 if (pixfmt->height < expected_ysize) {
532 v_scale = expected_ysize / pixfmt->height;
535 else if (v_scale >= 4)
539 cfg->ysize *= v_scale;
540 if (cfg->ysize < expected_ysize) {
541 if ((standard_id & V4L2_STD_625_50)) {
542 calculated_xsize = (cfg->ysize *
543 VPBE_DISPLAY_V_EXP_RATIO_N) /
544 VPBE_DISPLAY_V_EXP_RATIO_D;
545 if (calculated_xsize <= expected_ysize) {
547 cfg->ysize = calculated_xsize;
552 layer_info->v_zoom = ZOOM_X2;
553 else if (v_scale == 4)
554 layer_info->v_zoom = ZOOM_X4;
556 layer_info->h_exp = V_EXP_6_OVER_5;
558 /* no scaling, only cropping. Set display area to crop area */
559 cfg->ysize = expected_ysize;
561 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
562 "crop display xsize = %d, ysize = %d\n",
563 cfg->xsize, cfg->ysize);
566 static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
567 struct vpbe_layer *layer,
570 struct osd_layer_config *cfg = &layer->layer_info.config;
571 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
573 cfg->xpos = min((unsigned int)left,
574 vpbe_dev->current_timings.xres - cfg->xsize);
575 cfg->ypos = min((unsigned int)top,
576 vpbe_dev->current_timings.yres - cfg->ysize);
578 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
579 "new xpos = %d, ypos = %d\n",
580 cfg->xpos, cfg->ypos);
583 static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
586 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
588 if ((c->width == 0) ||
589 ((c->width + c->left) > vpbe_dev->current_timings.xres))
590 c->width = vpbe_dev->current_timings.xres - c->left;
592 if ((c->height == 0) || ((c->height + c->top) >
593 vpbe_dev->current_timings.yres))
594 c->height = vpbe_dev->current_timings.yres - c->top;
596 /* window height must be even for interlaced display */
597 if (vpbe_dev->current_timings.interlaced)
598 c->height &= (~0x01);
604 * If user application provides width and height, and have bytesperline set
605 * to zero, driver calculates bytesperline and sizeimage based on hardware
608 static int vpbe_try_format(struct vpbe_display *disp_dev,
609 struct v4l2_pix_format *pixfmt, int check)
611 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
618 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
619 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
620 /* choose default as V4L2_PIX_FMT_UYVY */
621 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
623 /* Check the field format */
624 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
625 (pixfmt->field != V4L2_FIELD_NONE)) {
626 if (vpbe_dev->current_timings.interlaced)
627 pixfmt->field = V4L2_FIELD_INTERLACED;
629 pixfmt->field = V4L2_FIELD_NONE;
632 if (pixfmt->field == V4L2_FIELD_INTERLACED)
635 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
640 max_width = vpbe_dev->current_timings.xres;
641 max_height = vpbe_dev->current_timings.yres;
645 if (!pixfmt->width || (pixfmt->width < min_width) ||
646 (pixfmt->width > max_width)) {
647 pixfmt->width = vpbe_dev->current_timings.xres;
650 if (!pixfmt->height || (pixfmt->height < min_height) ||
651 (pixfmt->height > max_height)) {
652 pixfmt->height = vpbe_dev->current_timings.yres;
655 if (pixfmt->bytesperline < (pixfmt->width * bpp))
656 pixfmt->bytesperline = pixfmt->width * bpp;
658 /* Make the bytesperline 32 byte aligned */
659 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
661 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
662 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
663 (pixfmt->bytesperline * pixfmt->height >> 1);
665 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
670 static int vpbe_display_g_priority(struct file *file, void *priv,
671 enum v4l2_priority *p)
673 struct vpbe_fh *fh = file->private_data;
674 struct vpbe_layer *layer = fh->layer;
676 *p = v4l2_prio_max(&layer->prio);
681 static int vpbe_display_s_priority(struct file *file, void *priv,
682 enum v4l2_priority p)
684 struct vpbe_fh *fh = file->private_data;
685 struct vpbe_layer *layer = fh->layer;
688 ret = v4l2_prio_change(&layer->prio, &fh->prio, p);
693 static int vpbe_display_querycap(struct file *file, void *priv,
694 struct v4l2_capability *cap)
696 struct vpbe_fh *fh = file->private_data;
697 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
699 cap->version = VPBE_DISPLAY_VERSION_CODE;
700 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
701 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
702 snprintf(cap->driver, sizeof(cap->driver), "%s",
703 dev_name(vpbe_dev->pdev));
704 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
705 dev_name(vpbe_dev->pdev));
706 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
711 static int vpbe_display_s_crop(struct file *file, void *priv,
712 const struct v4l2_crop *crop)
714 struct vpbe_fh *fh = file->private_data;
715 struct vpbe_layer *layer = fh->layer;
716 struct vpbe_display *disp_dev = fh->disp_dev;
717 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
718 struct osd_layer_config *cfg = &layer->layer_info.config;
719 struct osd_state *osd_device = disp_dev->osd_device;
720 struct v4l2_rect rect = crop->c;
723 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
724 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
726 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
727 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
736 vpbe_disp_check_window_params(disp_dev, &rect);
738 osd_device->ops.get_layer_config(osd_device,
739 layer->layer_info.id, cfg);
741 vpbe_disp_calculate_scale_factor(disp_dev, layer,
744 vpbe_disp_adj_position(disp_dev, layer, rect.top,
746 ret = osd_device->ops.set_layer_config(osd_device,
747 layer->layer_info.id, cfg);
749 v4l2_err(&vpbe_dev->v4l2_dev,
750 "Error in set layer config:\n");
754 /* apply zooming and h or v expansion */
755 osd_device->ops.set_zoom(osd_device,
756 layer->layer_info.id,
757 layer->layer_info.h_zoom,
758 layer->layer_info.v_zoom);
759 ret = osd_device->ops.set_vid_expansion(osd_device,
760 layer->layer_info.h_exp,
761 layer->layer_info.v_exp);
763 v4l2_err(&vpbe_dev->v4l2_dev,
764 "Error in set vid expansion:\n");
768 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
769 (layer->layer_info.v_zoom != ZOOM_X1) ||
770 (layer->layer_info.h_exp != H_EXP_OFF) ||
771 (layer->layer_info.v_exp != V_EXP_OFF))
772 /* Enable expansion filter */
773 osd_device->ops.set_interpolation_filter(osd_device, 1);
775 osd_device->ops.set_interpolation_filter(osd_device, 0);
780 static int vpbe_display_g_crop(struct file *file, void *priv,
781 struct v4l2_crop *crop)
783 struct vpbe_fh *fh = file->private_data;
784 struct vpbe_layer *layer = fh->layer;
785 struct osd_layer_config *cfg = &layer->layer_info.config;
786 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
787 struct osd_state *osd_device = fh->disp_dev->osd_device;
788 struct v4l2_rect *rect = &crop->c;
790 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
791 "VIDIOC_G_CROP, layer id = %d\n",
794 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
795 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
798 osd_device->ops.get_layer_config(osd_device,
799 layer->layer_info.id, cfg);
800 rect->top = cfg->ypos;
801 rect->left = cfg->xpos;
802 rect->width = cfg->xsize;
803 rect->height = cfg->ysize;
808 static int vpbe_display_cropcap(struct file *file, void *priv,
809 struct v4l2_cropcap *cropcap)
811 struct vpbe_fh *fh = file->private_data;
812 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
814 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
816 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
817 cropcap->bounds.left = 0;
818 cropcap->bounds.top = 0;
819 cropcap->bounds.width = vpbe_dev->current_timings.xres;
820 cropcap->bounds.height = vpbe_dev->current_timings.yres;
821 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
822 cropcap->defrect = cropcap->bounds;
826 static int vpbe_display_g_fmt(struct file *file, void *priv,
827 struct v4l2_format *fmt)
829 struct vpbe_fh *fh = file->private_data;
830 struct vpbe_layer *layer = fh->layer;
831 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
833 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
834 "VIDIOC_G_FMT, layer id = %d\n",
837 /* If buffer type is video output */
838 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
839 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
842 /* Fill in the information about format */
843 fmt->fmt.pix = layer->pix_fmt;
848 static int vpbe_display_enum_fmt(struct file *file, void *priv,
849 struct v4l2_fmtdesc *fmt)
851 struct vpbe_fh *fh = file->private_data;
852 struct vpbe_layer *layer = fh->layer;
853 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
854 unsigned int index = 0;
856 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
857 "VIDIOC_ENUM_FMT, layer id = %d\n",
859 if (fmt->index > 1) {
860 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
864 /* Fill in the information about format */
866 memset(fmt, 0, sizeof(*fmt));
868 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
870 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
871 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
873 strcpy(fmt->description, "Y/CbCr 4:2:0");
874 fmt->pixelformat = V4L2_PIX_FMT_NV12;
880 static int vpbe_display_s_fmt(struct file *file, void *priv,
881 struct v4l2_format *fmt)
883 struct vpbe_fh *fh = file->private_data;
884 struct vpbe_layer *layer = fh->layer;
885 struct vpbe_display *disp_dev = fh->disp_dev;
886 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
887 struct osd_layer_config *cfg = &layer->layer_info.config;
888 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
889 struct osd_state *osd_device = disp_dev->osd_device;
892 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
893 "VIDIOC_S_FMT, layer id = %d\n",
896 /* If streaming is started, return error */
897 if (layer->started) {
898 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
901 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
902 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
905 /* Check for valid pixel format */
906 ret = vpbe_try_format(disp_dev, pixfmt, 1);
910 /* YUV420 is requested, check availability of the
911 other video window */
913 layer->pix_fmt = *pixfmt;
914 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
915 struct vpbe_layer *otherlayer;
917 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
918 /* if other layer is available, only
919 * claim it, do not configure it
921 ret = osd_device->ops.request_layer(osd_device,
922 otherlayer->layer_info.id);
924 v4l2_err(&vpbe_dev->v4l2_dev,
925 "Display Manager failed to allocate layer\n");
930 /* Get osd layer config */
931 osd_device->ops.get_layer_config(osd_device,
932 layer->layer_info.id, cfg);
933 /* Store the pixel format in the layer object */
934 cfg->xsize = pixfmt->width;
935 cfg->ysize = pixfmt->height;
936 cfg->line_length = pixfmt->bytesperline;
939 cfg->interlaced = vpbe_dev->current_timings.interlaced;
941 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
942 cfg->pixfmt = PIXFMT_YCBCRI;
944 /* Change of the default pixel format for both video windows */
945 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
946 struct vpbe_layer *otherlayer;
947 cfg->pixfmt = PIXFMT_NV12;
948 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
950 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
953 /* Set the layer config in the osd window */
954 ret = osd_device->ops.set_layer_config(osd_device,
955 layer->layer_info.id, cfg);
957 v4l2_err(&vpbe_dev->v4l2_dev,
958 "Error in S_FMT params:\n");
962 /* Readback and fill the local copy of current pix format */
963 osd_device->ops.get_layer_config(osd_device,
964 layer->layer_info.id, cfg);
969 static int vpbe_display_try_fmt(struct file *file, void *priv,
970 struct v4l2_format *fmt)
972 struct vpbe_fh *fh = file->private_data;
973 struct vpbe_display *disp_dev = fh->disp_dev;
974 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
975 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
977 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
979 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
980 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
984 /* Check for valid field format */
985 return vpbe_try_format(disp_dev, pixfmt, 0);
990 * vpbe_display_s_std - Set the given standard in the encoder
992 * Sets the standard if supported by the current encoder. Return the status.
993 * 0 - success & -EINVAL on error
995 static int vpbe_display_s_std(struct file *file, void *priv,
998 struct vpbe_fh *fh = priv;
999 struct vpbe_layer *layer = fh->layer;
1000 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1003 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
1005 /* If streaming is started, return error */
1006 if (layer->started) {
1007 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1010 if (NULL != vpbe_dev->ops.s_std) {
1011 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
1013 v4l2_err(&vpbe_dev->v4l2_dev,
1014 "Failed to set standard for sub devices\n");
1025 * vpbe_display_g_std - Get the standard in the current encoder
1027 * Get the standard in the current encoder. Return the status. 0 - success
1030 static int vpbe_display_g_std(struct file *file, void *priv,
1031 v4l2_std_id *std_id)
1033 struct vpbe_fh *fh = priv;
1034 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1036 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
1038 /* Get the standard from the current encoder */
1039 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
1040 *std_id = vpbe_dev->current_timings.std_id;
1048 * vpbe_display_enum_output - enumerate outputs
1050 * Enumerates the outputs available at the vpbe display
1051 * returns the status, -EINVAL if end of output list
1053 static int vpbe_display_enum_output(struct file *file, void *priv,
1054 struct v4l2_output *output)
1056 struct vpbe_fh *fh = priv;
1057 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1060 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1062 /* Enumerate outputs */
1064 if (NULL == vpbe_dev->ops.enum_outputs)
1067 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1069 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1070 "Failed to enumerate outputs\n");
1078 * vpbe_display_s_output - Set output to
1079 * the output specified by the index
1081 static int vpbe_display_s_output(struct file *file, void *priv,
1084 struct vpbe_fh *fh = priv;
1085 struct vpbe_layer *layer = fh->layer;
1086 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1089 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1090 /* If streaming is started, return error */
1091 if (layer->started) {
1092 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1095 if (NULL == vpbe_dev->ops.set_output)
1098 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1100 v4l2_err(&vpbe_dev->v4l2_dev,
1101 "Failed to set output for sub devices\n");
1109 * vpbe_display_g_output - Get output from subdevice
1110 * for a given by the index
1112 static int vpbe_display_g_output(struct file *file, void *priv,
1115 struct vpbe_fh *fh = priv;
1116 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1118 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1119 /* Get the standard from the current encoder */
1120 *i = vpbe_dev->current_out_index;
1126 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1128 * enum the timings in the current encoder. Return the status. 0 - success
1132 vpbe_display_enum_dv_timings(struct file *file, void *priv,
1133 struct v4l2_enum_dv_timings *timings)
1135 struct vpbe_fh *fh = priv;
1136 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1139 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1141 /* Enumerate outputs */
1142 if (NULL == vpbe_dev->ops.enum_dv_timings)
1145 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1147 v4l2_err(&vpbe_dev->v4l2_dev,
1148 "Failed to enumerate dv timings info\n");
1156 * vpbe_display_s_dv_timings - Set the dv timings
1158 * Set the timings in the current encoder. Return the status. 0 - success
1162 vpbe_display_s_dv_timings(struct file *file, void *priv,
1163 struct v4l2_dv_timings *timings)
1165 struct vpbe_fh *fh = priv;
1166 struct vpbe_layer *layer = fh->layer;
1167 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1170 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1173 /* If streaming is started, return error */
1174 if (layer->started) {
1175 v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
1179 /* Set the given standard in the encoder */
1180 if (!vpbe_dev->ops.s_dv_timings)
1183 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1185 v4l2_err(&vpbe_dev->v4l2_dev,
1186 "Failed to set the dv timings info\n");
1194 * vpbe_display_g_dv_timings - Set the dv timings
1196 * Get the timings in the current encoder. Return the status. 0 - success
1200 vpbe_display_g_dv_timings(struct file *file, void *priv,
1201 struct v4l2_dv_timings *dv_timings)
1203 struct vpbe_fh *fh = priv;
1204 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1206 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1208 /* Get the given standard in the encoder */
1210 if (vpbe_dev->current_timings.timings_type &
1211 VPBE_ENC_DV_TIMINGS) {
1212 *dv_timings = vpbe_dev->current_timings.dv_timings;
1220 static int vpbe_display_streamoff(struct file *file, void *priv,
1221 enum v4l2_buf_type buf_type)
1223 struct vpbe_fh *fh = file->private_data;
1224 struct vpbe_layer *layer = fh->layer;
1225 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1226 struct osd_state *osd_device = fh->disp_dev->osd_device;
1229 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1230 "VIDIOC_STREAMOFF,layer id = %d\n",
1233 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1234 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1238 /* If io is allowed for this file handle, return error */
1239 if (!fh->io_allowed) {
1240 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1244 /* If streaming is not started, return error */
1245 if (!layer->started) {
1246 v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
1247 " id = %d\n", layer->device_id);
1251 osd_device->ops.disable_layer(osd_device,
1252 layer->layer_info.id);
1254 ret = vb2_streamoff(&layer->buffer_queue, buf_type);
1259 static int vpbe_display_streamon(struct file *file, void *priv,
1260 enum v4l2_buf_type buf_type)
1262 struct vpbe_fh *fh = file->private_data;
1263 struct vpbe_layer *layer = fh->layer;
1264 struct vpbe_display *disp_dev = fh->disp_dev;
1265 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1266 struct osd_state *osd_device = disp_dev->osd_device;
1269 osd_device->ops.disable_layer(osd_device,
1270 layer->layer_info.id);
1272 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
1275 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1276 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1280 /* If file handle is not allowed IO, return error */
1281 if (!fh->io_allowed) {
1282 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1285 /* If Streaming is already started, return error */
1286 if (layer->started) {
1287 v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
1292 * Call vb2_streamon to start streaming
1295 ret = vb2_streamon(&layer->buffer_queue, buf_type);
1297 v4l2_err(&vpbe_dev->v4l2_dev,
1298 "error in vb2_streamon\n");
1304 static int vpbe_display_dqbuf(struct file *file, void *priv,
1305 struct v4l2_buffer *buf)
1307 struct vpbe_fh *fh = file->private_data;
1308 struct vpbe_layer *layer = fh->layer;
1309 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1312 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1313 "VIDIOC_DQBUF, layer id = %d\n",
1316 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1317 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1320 /* If this file handle is not allowed to do IO, return error */
1321 if (!fh->io_allowed) {
1322 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1325 if (file->f_flags & O_NONBLOCK)
1326 /* Call videobuf_dqbuf for non blocking mode */
1327 ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
1329 /* Call videobuf_dqbuf for blocking mode */
1330 ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
1335 static int vpbe_display_qbuf(struct file *file, void *priv,
1336 struct v4l2_buffer *p)
1338 struct vpbe_fh *fh = file->private_data;
1339 struct vpbe_layer *layer = fh->layer;
1340 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1342 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1343 "VIDIOC_QBUF, layer id = %d\n",
1346 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1347 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1351 /* If this file handle is not allowed to do IO, return error */
1352 if (!fh->io_allowed) {
1353 v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
1357 return vb2_qbuf(&layer->buffer_queue, p);
1360 static int vpbe_display_querybuf(struct file *file, void *priv,
1361 struct v4l2_buffer *buf)
1363 struct vpbe_fh *fh = file->private_data;
1364 struct vpbe_layer *layer = fh->layer;
1365 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1367 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1368 "VIDIOC_QUERYBUF, layer id = %d\n",
1371 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1372 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1375 /* Call vb2_querybuf to get information */
1376 return vb2_querybuf(&layer->buffer_queue, buf);
1379 static int vpbe_display_reqbufs(struct file *file, void *priv,
1380 struct v4l2_requestbuffers *req_buf)
1382 struct vpbe_fh *fh = file->private_data;
1383 struct vpbe_layer *layer = fh->layer;
1384 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1385 struct vb2_queue *q;
1387 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
1389 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1390 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
1394 /* If io users of the layer is not zero, return error */
1395 if (0 != layer->io_usrs) {
1396 v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
1399 /* Initialize videobuf queue as per the buffer type */
1400 layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
1401 if (IS_ERR(layer->alloc_ctx)) {
1402 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
1403 return PTR_ERR(layer->alloc_ctx);
1405 q = &layer->buffer_queue;
1406 memset(q, 0, sizeof(*q));
1407 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1408 q->io_modes = VB2_MMAP | VB2_USERPTR;
1410 q->ops = &video_qops;
1411 q->mem_ops = &vb2_dma_contig_memops;
1412 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1413 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1415 ret = vb2_queue_init(q);
1417 v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1418 vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
1421 /* Set io allowed member of file handle to TRUE */
1423 /* Increment io usrs member of layer object to 1 */
1425 /* Store type of memory requested in layer object */
1426 layer->memory = req_buf->memory;
1427 /* Initialize buffer queue */
1428 INIT_LIST_HEAD(&layer->dma_queue);
1429 /* Allocate buffers */
1430 return vb2_reqbufs(q, req_buf);
1434 * vpbe_display_mmap()
1435 * It is used to map kernel space buffers into user spaces
1437 static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
1439 /* Get the layer object and file handle object */
1440 struct vpbe_fh *fh = filep->private_data;
1441 struct vpbe_layer *layer = fh->layer;
1442 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1445 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
1447 if (mutex_lock_interruptible(&layer->opslock))
1448 return -ERESTARTSYS;
1449 ret = vb2_mmap(&layer->buffer_queue, vma);
1450 mutex_unlock(&layer->opslock);
1454 /* vpbe_display_poll(): It is used for select/poll system call
1456 static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
1458 struct vpbe_fh *fh = filep->private_data;
1459 struct vpbe_layer *layer = fh->layer;
1460 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
1461 unsigned int err = 0;
1463 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
1464 if (layer->started) {
1465 mutex_lock(&layer->opslock);
1466 err = vb2_poll(&layer->buffer_queue, filep, wait);
1467 mutex_unlock(&layer->opslock);
1473 * vpbe_display_open()
1474 * It creates object of file handle structure and stores it in private_data
1475 * member of filepointer
1477 static int vpbe_display_open(struct file *file)
1479 struct vpbe_fh *fh = NULL;
1480 struct vpbe_layer *layer = video_drvdata(file);
1481 struct vpbe_display *disp_dev = layer->disp_dev;
1482 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1483 struct osd_state *osd_device = disp_dev->osd_device;
1486 /* Allocate memory for the file handle object */
1487 fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
1489 v4l2_err(&vpbe_dev->v4l2_dev,
1490 "unable to allocate memory for file handle object\n");
1493 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1494 "vpbe display open plane = %d\n",
1497 /* store pointer to fh in private_data member of filep */
1498 file->private_data = fh;
1500 fh->disp_dev = disp_dev;
1503 if (mutex_lock_interruptible(&layer->opslock))
1504 return -ERESTARTSYS;
1505 /* First claim the layer for this device */
1506 err = osd_device->ops.request_layer(osd_device,
1507 layer->layer_info.id);
1508 mutex_unlock(&layer->opslock);
1510 /* Couldn't get layer */
1511 v4l2_err(&vpbe_dev->v4l2_dev,
1512 "Display Manager failed to allocate layer\n");
1517 /* Increment layer usrs counter */
1519 /* Set io_allowed member to false */
1521 /* Initialize priority of this instance to default priority */
1522 fh->prio = V4L2_PRIORITY_UNSET;
1523 v4l2_prio_open(&layer->prio, &fh->prio);
1524 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1525 "vpbe display device opened successfully\n");
1530 * vpbe_display_release()
1531 * This function deletes buffer queue, frees the buffers and the davinci
1532 * display file * handle
1534 static int vpbe_display_release(struct file *file)
1536 /* Get the layer object and file handle object */
1537 struct vpbe_fh *fh = file->private_data;
1538 struct vpbe_layer *layer = fh->layer;
1539 struct osd_layer_config *cfg = &layer->layer_info.config;
1540 struct vpbe_display *disp_dev = fh->disp_dev;
1541 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1542 struct osd_state *osd_device = disp_dev->osd_device;
1544 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1546 mutex_lock(&layer->opslock);
1547 /* if this instance is doing IO */
1548 if (fh->io_allowed) {
1549 /* Reset io_usrs member of layer object */
1552 osd_device->ops.disable_layer(osd_device,
1553 layer->layer_info.id);
1555 /* Free buffers allocated */
1556 vb2_queue_release(&layer->buffer_queue);
1557 vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
1560 /* Decrement layer usrs counter */
1562 /* If this file handle has initialize encoder device, reset it */
1564 if (cfg->pixfmt == PIXFMT_NV12) {
1565 struct vpbe_layer *otherlayer;
1567 _vpbe_display_get_other_win_layer(disp_dev, layer);
1568 osd_device->ops.disable_layer(osd_device,
1569 otherlayer->layer_info.id);
1570 osd_device->ops.release_layer(osd_device,
1571 otherlayer->layer_info.id);
1573 osd_device->ops.disable_layer(osd_device,
1574 layer->layer_info.id);
1575 osd_device->ops.release_layer(osd_device,
1576 layer->layer_info.id);
1578 /* Close the priority */
1579 v4l2_prio_close(&layer->prio, fh->prio);
1580 file->private_data = NULL;
1581 mutex_unlock(&layer->opslock);
1583 /* Free memory allocated to file handle object */
1586 disp_dev->cbcr_ofst = 0;
1591 /* vpbe capture ioctl operations */
1592 static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1593 .vidioc_querycap = vpbe_display_querycap,
1594 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1595 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1596 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1597 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
1598 .vidioc_reqbufs = vpbe_display_reqbufs,
1599 .vidioc_querybuf = vpbe_display_querybuf,
1600 .vidioc_qbuf = vpbe_display_qbuf,
1601 .vidioc_dqbuf = vpbe_display_dqbuf,
1602 .vidioc_streamon = vpbe_display_streamon,
1603 .vidioc_streamoff = vpbe_display_streamoff,
1604 .vidioc_cropcap = vpbe_display_cropcap,
1605 .vidioc_g_crop = vpbe_display_g_crop,
1606 .vidioc_s_crop = vpbe_display_s_crop,
1607 .vidioc_g_priority = vpbe_display_g_priority,
1608 .vidioc_s_priority = vpbe_display_s_priority,
1609 .vidioc_s_std = vpbe_display_s_std,
1610 .vidioc_g_std = vpbe_display_g_std,
1611 .vidioc_enum_output = vpbe_display_enum_output,
1612 .vidioc_s_output = vpbe_display_s_output,
1613 .vidioc_g_output = vpbe_display_g_output,
1614 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1615 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1616 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
1619 static struct v4l2_file_operations vpbe_fops = {
1620 .owner = THIS_MODULE,
1621 .open = vpbe_display_open,
1622 .release = vpbe_display_release,
1623 .unlocked_ioctl = video_ioctl2,
1624 .mmap = vpbe_display_mmap,
1625 .poll = vpbe_display_poll
1628 static int vpbe_device_get(struct device *dev, void *data)
1630 struct platform_device *pdev = to_platform_device(dev);
1631 struct vpbe_display *vpbe_disp = data;
1633 if (strcmp("vpbe_controller", pdev->name) == 0)
1634 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1636 if (strstr(pdev->name, "vpbe-osd") != NULL)
1637 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1642 static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1643 struct platform_device *pdev)
1645 struct vpbe_layer *vpbe_display_layer = NULL;
1646 struct video_device *vbd = NULL;
1648 /* Allocate memory for four plane display objects */
1651 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1653 /* If memory allocation fails, return error */
1654 if (!disp_dev->dev[i]) {
1655 printk(KERN_ERR "ran out of memory\n");
1658 spin_lock_init(&disp_dev->dev[i]->irqlock);
1659 mutex_init(&disp_dev->dev[i]->opslock);
1661 /* Get the pointer to the layer object */
1662 vpbe_display_layer = disp_dev->dev[i];
1663 vbd = &vpbe_display_layer->video_dev;
1664 /* Initialize field of video device */
1665 vbd->release = video_device_release_empty;
1666 vbd->fops = &vpbe_fops;
1667 vbd->ioctl_ops = &vpbe_ioctl_ops;
1669 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1670 vbd->lock = &vpbe_display_layer->opslock;
1671 vbd->vfl_dir = VFL_DIR_TX;
1673 if (disp_dev->vpbe_dev->current_timings.timings_type &
1675 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1677 snprintf(vbd->name, sizeof(vbd->name),
1678 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1679 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1680 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1681 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1683 vpbe_display_layer->device_id = i;
1685 vpbe_display_layer->layer_info.id =
1686 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1688 /* Initialize prio member of layer object */
1689 v4l2_prio_init(&vpbe_display_layer->prio);
1694 static int register_device(struct vpbe_layer *vpbe_display_layer,
1695 struct vpbe_display *disp_dev,
1696 struct platform_device *pdev)
1700 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1701 "Trying to register VPBE display device.\n");
1702 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1703 "layer=%x,layer->video_dev=%x\n",
1704 (int)vpbe_display_layer,
1705 (int)&vpbe_display_layer->video_dev);
1707 err = video_register_device(&vpbe_display_layer->video_dev,
1713 vpbe_display_layer->disp_dev = disp_dev;
1714 /* set the driver data in platform device */
1715 platform_set_drvdata(pdev, disp_dev);
1716 video_set_drvdata(&vpbe_display_layer->video_dev,
1717 vpbe_display_layer);
1725 * vpbe_display_probe()
1726 * This function creates device entries by register itself to the V4L2 driver
1727 * and initializes fields of each layer objects
1729 static int vpbe_display_probe(struct platform_device *pdev)
1731 struct vpbe_layer *vpbe_display_layer;
1732 struct vpbe_display *disp_dev;
1733 struct resource *res = NULL;
1739 printk(KERN_DEBUG "vpbe_display_probe\n");
1740 /* Allocate memory for vpbe_display */
1741 disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1746 spin_lock_init(&disp_dev->dma_queue_lock);
1748 * Scan all the platform devices to find the vpbe
1749 * controller device and get the vpbe_dev object
1751 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1755 /* Initialize the vpbe display controller */
1756 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1757 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1758 disp_dev->vpbe_dev);
1760 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1761 "Error initing vpbe\n");
1767 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1768 if (init_vpbe_layer(i, disp_dev, pdev)) {
1774 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1776 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1777 "Unable to get VENC interrupt resource\n");
1783 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1784 VPBE_DISPLAY_DRIVER, disp_dev);
1786 v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
1787 "Unable to request interrupt\n");
1791 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1792 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1798 printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
1802 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1803 /* Get the pointer to the layer object */
1804 vpbe_display_layer = disp_dev->dev[k];
1805 /* Unregister video device */
1806 if (vpbe_display_layer) {
1807 video_unregister_device(
1808 &vpbe_display_layer->video_dev);
1809 kfree(disp_dev->dev[k]);
1816 * vpbe_display_remove()
1817 * It un-register hardware layer from V4L2 driver
1819 static int vpbe_display_remove(struct platform_device *pdev)
1821 struct vpbe_layer *vpbe_display_layer;
1822 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1823 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1826 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1828 /* deinitialize the vpbe display controller */
1829 if (NULL != vpbe_dev->ops.deinitialize)
1830 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1831 /* un-register device */
1832 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1833 /* Get the pointer to the layer object */
1834 vpbe_display_layer = disp_dev->dev[i];
1835 /* Unregister video device */
1836 video_unregister_device(&vpbe_display_layer->video_dev);
1839 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1840 kfree(disp_dev->dev[i]);
1841 disp_dev->dev[i] = NULL;
1847 static struct platform_driver vpbe_display_driver = {
1849 .name = VPBE_DISPLAY_DRIVER,
1850 .owner = THIS_MODULE,
1851 .bus = &platform_bus_type,
1853 .probe = vpbe_display_probe,
1854 .remove = vpbe_display_remove,
1857 module_platform_driver(vpbe_display_driver);
1859 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1860 MODULE_LICENSE("GPL");
1861 MODULE_AUTHOR("Texas Instruments");