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 /* If buffer queue is empty, return error */
348 if (list_empty(&layer
->dma_queue
)) {
349 v4l2_err(&vpbe_dev
->v4l2_dev
, "buffer queue is empty\n");
352 /* Get the next frame from the buffer queue */
353 layer
->next_frm
= layer
->cur_frm
= list_entry(layer
->dma_queue
.next
,
354 struct vpbe_disp_buffer
, list
);
355 /* Remove buffer from the buffer queue */
356 list_del(&layer
->cur_frm
->list
);
357 /* Mark state of the current frame to active */
358 layer
->cur_frm
->vb
.state
= VB2_BUF_STATE_ACTIVE
;
359 /* Initialize field_id and started member */
362 /* Set parameters in OSD and VENC */
363 ret
= vpbe_set_osd_display_params(fh
->disp_dev
, layer
);
368 * if request format is yuv420 semiplanar, need to
369 * enable both video windows
372 layer
->layer_first_int
= 1;
377 static int vpbe_stop_streaming(struct vb2_queue
*vq
)
379 struct vpbe_fh
*fh
= vb2_get_drv_priv(vq
);
380 struct vpbe_layer
*layer
= fh
->layer
;
382 if (!vb2_is_streaming(vq
))
385 /* release all active buffers */
386 while (!list_empty(&layer
->dma_queue
)) {
387 layer
->next_frm
= list_entry(layer
->dma_queue
.next
,
388 struct vpbe_disp_buffer
, list
);
389 list_del(&layer
->next_frm
->list
);
390 vb2_buffer_done(&layer
->next_frm
->vb
, VB2_BUF_STATE_ERROR
);
396 static struct vb2_ops video_qops
= {
397 .queue_setup
= vpbe_buffer_queue_setup
,
398 .wait_prepare
= vpbe_wait_prepare
,
399 .wait_finish
= vpbe_wait_finish
,
400 .buf_init
= vpbe_buffer_init
,
401 .buf_prepare
= vpbe_buffer_prepare
,
402 .start_streaming
= vpbe_start_streaming
,
403 .stop_streaming
= vpbe_stop_streaming
,
404 .buf_cleanup
= vpbe_buf_cleanup
,
405 .buf_queue
= vpbe_buffer_queue
,
410 _vpbe_display_get_other_win_layer(struct vpbe_display
*disp_dev
,
411 struct vpbe_layer
*layer
)
413 enum vpbe_display_device_id thiswin
, otherwin
;
414 thiswin
= layer
->device_id
;
416 otherwin
= (thiswin
== VPBE_DISPLAY_DEVICE_0
) ?
417 VPBE_DISPLAY_DEVICE_1
: VPBE_DISPLAY_DEVICE_0
;
418 return disp_dev
->dev
[otherwin
];
421 static int vpbe_set_osd_display_params(struct vpbe_display
*disp_dev
,
422 struct vpbe_layer
*layer
)
424 struct osd_layer_config
*cfg
= &layer
->layer_info
.config
;
425 struct osd_state
*osd_device
= disp_dev
->osd_device
;
426 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
430 addr
= vb2_dma_contig_plane_dma_addr(&layer
->cur_frm
->vb
, 0);
431 /* Set address in the display registers */
432 osd_device
->ops
.start_layer(osd_device
,
433 layer
->layer_info
.id
,
435 disp_dev
->cbcr_ofst
);
437 ret
= osd_device
->ops
.enable_layer(osd_device
,
438 layer
->layer_info
.id
, 0);
440 v4l2_err(&vpbe_dev
->v4l2_dev
,
441 "Error in enabling osd window layer 0\n");
445 /* Enable the window */
446 layer
->layer_info
.enable
= 1;
447 if (cfg
->pixfmt
== PIXFMT_NV12
) {
448 struct vpbe_layer
*otherlayer
=
449 _vpbe_display_get_other_win_layer(disp_dev
, layer
);
451 ret
= osd_device
->ops
.enable_layer(osd_device
,
452 otherlayer
->layer_info
.id
, 1);
454 v4l2_err(&vpbe_dev
->v4l2_dev
,
455 "Error in enabling osd window layer 1\n");
458 otherlayer
->layer_info
.enable
= 1;
464 vpbe_disp_calculate_scale_factor(struct vpbe_display
*disp_dev
,
465 struct vpbe_layer
*layer
,
466 int expected_xsize
, int expected_ysize
)
468 struct display_layer_info
*layer_info
= &layer
->layer_info
;
469 struct v4l2_pix_format
*pixfmt
= &layer
->pix_fmt
;
470 struct osd_layer_config
*cfg
= &layer
->layer_info
.config
;
471 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
472 int calculated_xsize
;
478 v4l2_std_id standard_id
= vpbe_dev
->current_timings
.std_id
;
481 * Application initially set the image format. Current display
482 * size is obtained from the vpbe display controller. expected_xsize
483 * and expected_ysize are set through S_CROP ioctl. Based on this,
484 * driver will calculate the scale factors for vertical and
485 * horizontal direction so that the image is displayed scaled
486 * and expanded. Application uses expansion to display the image
487 * in a square pixel. Otherwise it is displayed using displays
488 * pixel aspect ratio.It is expected that application chooses
489 * the crop coordinates for cropped or scaled display. if crop
490 * size is less than the image size, it is displayed cropped or
491 * it is displayed scaled and/or expanded.
493 * to begin with, set the crop window same as expected. Later we
494 * will override with scaled window size
497 cfg
->xsize
= pixfmt
->width
;
498 cfg
->ysize
= pixfmt
->height
;
499 layer_info
->h_zoom
= ZOOM_X1
; /* no horizontal zoom */
500 layer_info
->v_zoom
= ZOOM_X1
; /* no horizontal zoom */
501 layer_info
->h_exp
= H_EXP_OFF
; /* no horizontal zoom */
502 layer_info
->v_exp
= V_EXP_OFF
; /* no horizontal zoom */
504 if (pixfmt
->width
< expected_xsize
) {
505 h_scale
= vpbe_dev
->current_timings
.xres
/ pixfmt
->width
;
508 else if (h_scale
>= 4)
512 cfg
->xsize
*= h_scale
;
513 if (cfg
->xsize
< expected_xsize
) {
514 if ((standard_id
& V4L2_STD_525_60
) ||
515 (standard_id
& V4L2_STD_625_50
)) {
516 calculated_xsize
= (cfg
->xsize
*
517 VPBE_DISPLAY_H_EXP_RATIO_N
) /
518 VPBE_DISPLAY_H_EXP_RATIO_D
;
519 if (calculated_xsize
<= expected_xsize
) {
521 cfg
->xsize
= calculated_xsize
;
526 layer_info
->h_zoom
= ZOOM_X2
;
527 else if (h_scale
== 4)
528 layer_info
->h_zoom
= ZOOM_X4
;
530 layer_info
->h_exp
= H_EXP_9_OVER_8
;
532 /* no scaling, only cropping. Set display area to crop area */
533 cfg
->xsize
= expected_xsize
;
536 if (pixfmt
->height
< expected_ysize
) {
537 v_scale
= expected_ysize
/ pixfmt
->height
;
540 else if (v_scale
>= 4)
544 cfg
->ysize
*= v_scale
;
545 if (cfg
->ysize
< expected_ysize
) {
546 if ((standard_id
& V4L2_STD_625_50
)) {
547 calculated_xsize
= (cfg
->ysize
*
548 VPBE_DISPLAY_V_EXP_RATIO_N
) /
549 VPBE_DISPLAY_V_EXP_RATIO_D
;
550 if (calculated_xsize
<= expected_ysize
) {
552 cfg
->ysize
= calculated_xsize
;
557 layer_info
->v_zoom
= ZOOM_X2
;
558 else if (v_scale
== 4)
559 layer_info
->v_zoom
= ZOOM_X4
;
561 layer_info
->h_exp
= V_EXP_6_OVER_5
;
563 /* no scaling, only cropping. Set display area to crop area */
564 cfg
->ysize
= expected_ysize
;
566 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
567 "crop display xsize = %d, ysize = %d\n",
568 cfg
->xsize
, cfg
->ysize
);
571 static void vpbe_disp_adj_position(struct vpbe_display
*disp_dev
,
572 struct vpbe_layer
*layer
,
575 struct osd_layer_config
*cfg
= &layer
->layer_info
.config
;
576 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
578 cfg
->xpos
= min((unsigned int)left
,
579 vpbe_dev
->current_timings
.xres
- cfg
->xsize
);
580 cfg
->ypos
= min((unsigned int)top
,
581 vpbe_dev
->current_timings
.yres
- cfg
->ysize
);
583 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
584 "new xpos = %d, ypos = %d\n",
585 cfg
->xpos
, cfg
->ypos
);
588 static void vpbe_disp_check_window_params(struct vpbe_display
*disp_dev
,
591 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
593 if ((c
->width
== 0) ||
594 ((c
->width
+ c
->left
) > vpbe_dev
->current_timings
.xres
))
595 c
->width
= vpbe_dev
->current_timings
.xres
- c
->left
;
597 if ((c
->height
== 0) || ((c
->height
+ c
->top
) >
598 vpbe_dev
->current_timings
.yres
))
599 c
->height
= vpbe_dev
->current_timings
.yres
- c
->top
;
601 /* window height must be even for interlaced display */
602 if (vpbe_dev
->current_timings
.interlaced
)
603 c
->height
&= (~0x01);
609 * If user application provides width and height, and have bytesperline set
610 * to zero, driver calculates bytesperline and sizeimage based on hardware
613 static int vpbe_try_format(struct vpbe_display
*disp_dev
,
614 struct v4l2_pix_format
*pixfmt
, int check
)
616 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
623 if ((pixfmt
->pixelformat
!= V4L2_PIX_FMT_UYVY
) &&
624 (pixfmt
->pixelformat
!= V4L2_PIX_FMT_NV12
))
625 /* choose default as V4L2_PIX_FMT_UYVY */
626 pixfmt
->pixelformat
= V4L2_PIX_FMT_UYVY
;
628 /* Check the field format */
629 if ((pixfmt
->field
!= V4L2_FIELD_INTERLACED
) &&
630 (pixfmt
->field
!= V4L2_FIELD_NONE
)) {
631 if (vpbe_dev
->current_timings
.interlaced
)
632 pixfmt
->field
= V4L2_FIELD_INTERLACED
;
634 pixfmt
->field
= V4L2_FIELD_NONE
;
637 if (pixfmt
->field
== V4L2_FIELD_INTERLACED
)
640 if (pixfmt
->pixelformat
== V4L2_PIX_FMT_NV12
)
645 max_width
= vpbe_dev
->current_timings
.xres
;
646 max_height
= vpbe_dev
->current_timings
.yres
;
650 if (!pixfmt
->width
|| (pixfmt
->width
< min_width
) ||
651 (pixfmt
->width
> max_width
)) {
652 pixfmt
->width
= vpbe_dev
->current_timings
.xres
;
655 if (!pixfmt
->height
|| (pixfmt
->height
< min_height
) ||
656 (pixfmt
->height
> max_height
)) {
657 pixfmt
->height
= vpbe_dev
->current_timings
.yres
;
660 if (pixfmt
->bytesperline
< (pixfmt
->width
* bpp
))
661 pixfmt
->bytesperline
= pixfmt
->width
* bpp
;
663 /* Make the bytesperline 32 byte aligned */
664 pixfmt
->bytesperline
= ((pixfmt
->width
* bpp
+ 31) & ~31);
666 if (pixfmt
->pixelformat
== V4L2_PIX_FMT_NV12
)
667 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
+
668 (pixfmt
->bytesperline
* pixfmt
->height
>> 1);
670 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
675 static int vpbe_display_g_priority(struct file
*file
, void *priv
,
676 enum v4l2_priority
*p
)
678 struct vpbe_fh
*fh
= file
->private_data
;
679 struct vpbe_layer
*layer
= fh
->layer
;
681 *p
= v4l2_prio_max(&layer
->prio
);
686 static int vpbe_display_s_priority(struct file
*file
, void *priv
,
687 enum v4l2_priority p
)
689 struct vpbe_fh
*fh
= file
->private_data
;
690 struct vpbe_layer
*layer
= fh
->layer
;
693 ret
= v4l2_prio_change(&layer
->prio
, &fh
->prio
, p
);
698 static int vpbe_display_querycap(struct file
*file
, void *priv
,
699 struct v4l2_capability
*cap
)
701 struct vpbe_fh
*fh
= file
->private_data
;
702 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
704 cap
->version
= VPBE_DISPLAY_VERSION_CODE
;
705 cap
->device_caps
= V4L2_CAP_VIDEO_OUTPUT
| V4L2_CAP_STREAMING
;
706 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
707 snprintf(cap
->driver
, sizeof(cap
->driver
), "%s",
708 dev_name(vpbe_dev
->pdev
));
709 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
710 dev_name(vpbe_dev
->pdev
));
711 strlcpy(cap
->card
, vpbe_dev
->cfg
->module_name
, sizeof(cap
->card
));
716 static int vpbe_display_s_crop(struct file
*file
, void *priv
,
717 const struct v4l2_crop
*crop
)
719 struct vpbe_fh
*fh
= file
->private_data
;
720 struct vpbe_layer
*layer
= fh
->layer
;
721 struct vpbe_display
*disp_dev
= fh
->disp_dev
;
722 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
723 struct osd_layer_config
*cfg
= &layer
->layer_info
.config
;
724 struct osd_state
*osd_device
= disp_dev
->osd_device
;
725 struct v4l2_rect rect
= crop
->c
;
728 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
729 "VIDIOC_S_CROP, layer id = %d\n", layer
->device_id
);
731 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
732 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buf type\n");
741 vpbe_disp_check_window_params(disp_dev
, &rect
);
743 osd_device
->ops
.get_layer_config(osd_device
,
744 layer
->layer_info
.id
, cfg
);
746 vpbe_disp_calculate_scale_factor(disp_dev
, layer
,
749 vpbe_disp_adj_position(disp_dev
, layer
, rect
.top
,
751 ret
= osd_device
->ops
.set_layer_config(osd_device
,
752 layer
->layer_info
.id
, cfg
);
754 v4l2_err(&vpbe_dev
->v4l2_dev
,
755 "Error in set layer config:\n");
759 /* apply zooming and h or v expansion */
760 osd_device
->ops
.set_zoom(osd_device
,
761 layer
->layer_info
.id
,
762 layer
->layer_info
.h_zoom
,
763 layer
->layer_info
.v_zoom
);
764 ret
= osd_device
->ops
.set_vid_expansion(osd_device
,
765 layer
->layer_info
.h_exp
,
766 layer
->layer_info
.v_exp
);
768 v4l2_err(&vpbe_dev
->v4l2_dev
,
769 "Error in set vid expansion:\n");
773 if ((layer
->layer_info
.h_zoom
!= ZOOM_X1
) ||
774 (layer
->layer_info
.v_zoom
!= ZOOM_X1
) ||
775 (layer
->layer_info
.h_exp
!= H_EXP_OFF
) ||
776 (layer
->layer_info
.v_exp
!= V_EXP_OFF
))
777 /* Enable expansion filter */
778 osd_device
->ops
.set_interpolation_filter(osd_device
, 1);
780 osd_device
->ops
.set_interpolation_filter(osd_device
, 0);
785 static int vpbe_display_g_crop(struct file
*file
, void *priv
,
786 struct v4l2_crop
*crop
)
788 struct vpbe_fh
*fh
= file
->private_data
;
789 struct vpbe_layer
*layer
= fh
->layer
;
790 struct osd_layer_config
*cfg
= &layer
->layer_info
.config
;
791 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
792 struct osd_state
*osd_device
= fh
->disp_dev
->osd_device
;
793 struct v4l2_rect
*rect
= &crop
->c
;
796 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
797 "VIDIOC_G_CROP, layer id = %d\n",
800 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
801 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buf type\n");
804 osd_device
->ops
.get_layer_config(osd_device
,
805 layer
->layer_info
.id
, cfg
);
806 rect
->top
= cfg
->ypos
;
807 rect
->left
= cfg
->xpos
;
808 rect
->width
= cfg
->xsize
;
809 rect
->height
= cfg
->ysize
;
814 static int vpbe_display_cropcap(struct file
*file
, void *priv
,
815 struct v4l2_cropcap
*cropcap
)
817 struct vpbe_fh
*fh
= file
->private_data
;
818 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
820 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_CROPCAP ioctl\n");
822 cropcap
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
823 cropcap
->bounds
.left
= 0;
824 cropcap
->bounds
.top
= 0;
825 cropcap
->bounds
.width
= vpbe_dev
->current_timings
.xres
;
826 cropcap
->bounds
.height
= vpbe_dev
->current_timings
.yres
;
827 cropcap
->pixelaspect
= vpbe_dev
->current_timings
.aspect
;
828 cropcap
->defrect
= cropcap
->bounds
;
832 static int vpbe_display_g_fmt(struct file
*file
, void *priv
,
833 struct v4l2_format
*fmt
)
835 struct vpbe_fh
*fh
= file
->private_data
;
836 struct vpbe_layer
*layer
= fh
->layer
;
837 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
839 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
840 "VIDIOC_G_FMT, layer id = %d\n",
843 /* If buffer type is video output */
844 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= fmt
->type
) {
845 v4l2_err(&vpbe_dev
->v4l2_dev
, "invalid type\n");
848 /* Fill in the information about format */
849 fmt
->fmt
.pix
= layer
->pix_fmt
;
854 static int vpbe_display_enum_fmt(struct file
*file
, void *priv
,
855 struct v4l2_fmtdesc
*fmt
)
857 struct vpbe_fh
*fh
= file
->private_data
;
858 struct vpbe_layer
*layer
= fh
->layer
;
859 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
860 unsigned int index
= 0;
862 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
863 "VIDIOC_ENUM_FMT, layer id = %d\n",
865 if (fmt
->index
> 1) {
866 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid format index\n");
870 /* Fill in the information about format */
872 memset(fmt
, 0, sizeof(*fmt
));
874 fmt
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
876 strcpy(fmt
->description
, "YUV 4:2:2 - UYVY");
877 fmt
->pixelformat
= V4L2_PIX_FMT_UYVY
;
879 strcpy(fmt
->description
, "Y/CbCr 4:2:0");
880 fmt
->pixelformat
= V4L2_PIX_FMT_NV12
;
886 static int vpbe_display_s_fmt(struct file
*file
, void *priv
,
887 struct v4l2_format
*fmt
)
889 struct vpbe_fh
*fh
= file
->private_data
;
890 struct vpbe_layer
*layer
= fh
->layer
;
891 struct vpbe_display
*disp_dev
= fh
->disp_dev
;
892 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
893 struct osd_layer_config
*cfg
= &layer
->layer_info
.config
;
894 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
895 struct osd_state
*osd_device
= disp_dev
->osd_device
;
898 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
899 "VIDIOC_S_FMT, layer id = %d\n",
902 /* If streaming is started, return error */
903 if (layer
->started
) {
904 v4l2_err(&vpbe_dev
->v4l2_dev
, "Streaming is started\n");
907 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= fmt
->type
) {
908 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "invalid type\n");
911 /* Check for valid pixel format */
912 ret
= vpbe_try_format(disp_dev
, pixfmt
, 1);
916 /* YUV420 is requested, check availability of the
917 other video window */
919 layer
->pix_fmt
= *pixfmt
;
921 /* Get osd layer config */
922 osd_device
->ops
.get_layer_config(osd_device
,
923 layer
->layer_info
.id
, cfg
);
924 /* Store the pixel format in the layer object */
925 cfg
->xsize
= pixfmt
->width
;
926 cfg
->ysize
= pixfmt
->height
;
927 cfg
->line_length
= pixfmt
->bytesperline
;
930 cfg
->interlaced
= vpbe_dev
->current_timings
.interlaced
;
932 if (V4L2_PIX_FMT_UYVY
== pixfmt
->pixelformat
)
933 cfg
->pixfmt
= PIXFMT_YCbCrI
;
935 /* Change of the default pixel format for both video windows */
936 if (V4L2_PIX_FMT_NV12
== pixfmt
->pixelformat
) {
937 struct vpbe_layer
*otherlayer
;
938 cfg
->pixfmt
= PIXFMT_NV12
;
939 otherlayer
= _vpbe_display_get_other_win_layer(disp_dev
,
941 otherlayer
->layer_info
.config
.pixfmt
= PIXFMT_NV12
;
944 /* Set the layer config in the osd window */
945 ret
= osd_device
->ops
.set_layer_config(osd_device
,
946 layer
->layer_info
.id
, cfg
);
948 v4l2_err(&vpbe_dev
->v4l2_dev
,
949 "Error in S_FMT params:\n");
953 /* Readback and fill the local copy of current pix format */
954 osd_device
->ops
.get_layer_config(osd_device
,
955 layer
->layer_info
.id
, cfg
);
960 static int vpbe_display_try_fmt(struct file
*file
, void *priv
,
961 struct v4l2_format
*fmt
)
963 struct vpbe_fh
*fh
= file
->private_data
;
964 struct vpbe_display
*disp_dev
= fh
->disp_dev
;
965 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
966 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
968 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_TRY_FMT\n");
970 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= fmt
->type
) {
971 v4l2_err(&vpbe_dev
->v4l2_dev
, "invalid type\n");
975 /* Check for valid field format */
976 return vpbe_try_format(disp_dev
, pixfmt
, 0);
981 * vpbe_display_s_std - Set the given standard in the encoder
983 * Sets the standard if supported by the current encoder. Return the status.
984 * 0 - success & -EINVAL on error
986 static int vpbe_display_s_std(struct file
*file
, void *priv
,
989 struct vpbe_fh
*fh
= priv
;
990 struct vpbe_layer
*layer
= fh
->layer
;
991 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
994 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_S_STD\n");
996 /* If streaming is started, return error */
997 if (layer
->started
) {
998 v4l2_err(&vpbe_dev
->v4l2_dev
, "Streaming is started\n");
1001 if (NULL
!= vpbe_dev
->ops
.s_std
) {
1002 ret
= vpbe_dev
->ops
.s_std(vpbe_dev
, std_id
);
1004 v4l2_err(&vpbe_dev
->v4l2_dev
,
1005 "Failed to set standard for sub devices\n");
1016 * vpbe_display_g_std - Get the standard in the current encoder
1018 * Get the standard in the current encoder. Return the status. 0 - success
1021 static int vpbe_display_g_std(struct file
*file
, void *priv
,
1022 v4l2_std_id
*std_id
)
1024 struct vpbe_fh
*fh
= priv
;
1025 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1027 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_G_STD\n");
1029 /* Get the standard from the current encoder */
1030 if (vpbe_dev
->current_timings
.timings_type
& VPBE_ENC_STD
) {
1031 *std_id
= vpbe_dev
->current_timings
.std_id
;
1039 * vpbe_display_enum_output - enumerate outputs
1041 * Enumerates the outputs available at the vpbe display
1042 * returns the status, -EINVAL if end of output list
1044 static int vpbe_display_enum_output(struct file
*file
, void *priv
,
1045 struct v4l2_output
*output
)
1047 struct vpbe_fh
*fh
= priv
;
1048 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1051 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_ENUM_OUTPUT\n");
1053 /* Enumerate outputs */
1055 if (NULL
== vpbe_dev
->ops
.enum_outputs
)
1058 ret
= vpbe_dev
->ops
.enum_outputs(vpbe_dev
, output
);
1060 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
1061 "Failed to enumerate outputs\n");
1069 * vpbe_display_s_output - Set output to
1070 * the output specified by the index
1072 static int vpbe_display_s_output(struct file
*file
, void *priv
,
1075 struct vpbe_fh
*fh
= priv
;
1076 struct vpbe_layer
*layer
= fh
->layer
;
1077 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1080 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_S_OUTPUT\n");
1081 /* If streaming is started, return error */
1082 if (layer
->started
) {
1083 v4l2_err(&vpbe_dev
->v4l2_dev
, "Streaming is started\n");
1086 if (NULL
== vpbe_dev
->ops
.set_output
)
1089 ret
= vpbe_dev
->ops
.set_output(vpbe_dev
, i
);
1091 v4l2_err(&vpbe_dev
->v4l2_dev
,
1092 "Failed to set output for sub devices\n");
1100 * vpbe_display_g_output - Get output from subdevice
1101 * for a given by the index
1103 static int vpbe_display_g_output(struct file
*file
, void *priv
,
1106 struct vpbe_fh
*fh
= priv
;
1107 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1109 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_G_OUTPUT\n");
1110 /* Get the standard from the current encoder */
1111 *i
= vpbe_dev
->current_out_index
;
1117 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1119 * enum the timings in the current encoder. Return the status. 0 - success
1123 vpbe_display_enum_dv_timings(struct file
*file
, void *priv
,
1124 struct v4l2_enum_dv_timings
*timings
)
1126 struct vpbe_fh
*fh
= priv
;
1127 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1130 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_ENUM_DV_TIMINGS\n");
1132 /* Enumerate outputs */
1133 if (NULL
== vpbe_dev
->ops
.enum_dv_timings
)
1136 ret
= vpbe_dev
->ops
.enum_dv_timings(vpbe_dev
, timings
);
1138 v4l2_err(&vpbe_dev
->v4l2_dev
,
1139 "Failed to enumerate dv timings info\n");
1147 * vpbe_display_s_dv_timings - Set the dv timings
1149 * Set the timings in the current encoder. Return the status. 0 - success
1153 vpbe_display_s_dv_timings(struct file
*file
, void *priv
,
1154 struct v4l2_dv_timings
*timings
)
1156 struct vpbe_fh
*fh
= priv
;
1157 struct vpbe_layer
*layer
= fh
->layer
;
1158 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1161 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_S_DV_TIMINGS\n");
1164 /* If streaming is started, return error */
1165 if (layer
->started
) {
1166 v4l2_err(&vpbe_dev
->v4l2_dev
, "Streaming is started\n");
1170 /* Set the given standard in the encoder */
1171 if (!vpbe_dev
->ops
.s_dv_timings
)
1174 ret
= vpbe_dev
->ops
.s_dv_timings(vpbe_dev
, timings
);
1176 v4l2_err(&vpbe_dev
->v4l2_dev
,
1177 "Failed to set the dv timings info\n");
1180 /* set the current norm to zero to be consistent. If STD is used
1181 * v4l2 layer will set the norm properly on successful s_std call
1183 layer
->video_dev
.current_norm
= 0;
1189 * vpbe_display_g_dv_timings - Set the dv timings
1191 * Get the timings in the current encoder. Return the status. 0 - success
1195 vpbe_display_g_dv_timings(struct file
*file
, void *priv
,
1196 struct v4l2_dv_timings
*dv_timings
)
1198 struct vpbe_fh
*fh
= priv
;
1199 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1201 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_G_DV_TIMINGS\n");
1203 /* Get the given standard in the encoder */
1205 if (vpbe_dev
->current_timings
.timings_type
&
1206 VPBE_ENC_CUSTOM_TIMINGS
) {
1207 *dv_timings
= vpbe_dev
->current_timings
.dv_timings
;
1215 static int vpbe_display_streamoff(struct file
*file
, void *priv
,
1216 enum v4l2_buf_type buf_type
)
1218 struct vpbe_fh
*fh
= file
->private_data
;
1219 struct vpbe_layer
*layer
= fh
->layer
;
1220 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1221 struct osd_state
*osd_device
= fh
->disp_dev
->osd_device
;
1224 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
1225 "VIDIOC_STREAMOFF,layer id = %d\n",
1228 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= buf_type
) {
1229 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buffer type\n");
1233 /* If io is allowed for this file handle, return error */
1234 if (!fh
->io_allowed
) {
1235 v4l2_err(&vpbe_dev
->v4l2_dev
, "No io_allowed\n");
1239 /* If streaming is not started, return error */
1240 if (!layer
->started
) {
1241 v4l2_err(&vpbe_dev
->v4l2_dev
, "streaming not started in layer"
1242 " id = %d\n", layer
->device_id
);
1246 osd_device
->ops
.disable_layer(osd_device
,
1247 layer
->layer_info
.id
);
1249 ret
= vb2_streamoff(&layer
->buffer_queue
, buf_type
);
1254 static int vpbe_display_streamon(struct file
*file
, void *priv
,
1255 enum v4l2_buf_type buf_type
)
1257 struct vpbe_fh
*fh
= file
->private_data
;
1258 struct vpbe_layer
*layer
= fh
->layer
;
1259 struct vpbe_display
*disp_dev
= fh
->disp_dev
;
1260 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1261 struct osd_state
*osd_device
= disp_dev
->osd_device
;
1264 osd_device
->ops
.disable_layer(osd_device
,
1265 layer
->layer_info
.id
);
1267 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "VIDIOC_STREAMON, layerid=%d\n",
1270 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= buf_type
) {
1271 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buffer type\n");
1275 /* If file handle is not allowed IO, return error */
1276 if (!fh
->io_allowed
) {
1277 v4l2_err(&vpbe_dev
->v4l2_dev
, "No io_allowed\n");
1280 /* If Streaming is already started, return error */
1281 if (layer
->started
) {
1282 v4l2_err(&vpbe_dev
->v4l2_dev
, "layer is already streaming\n");
1287 * Call vb2_streamon to start streaming
1290 ret
= vb2_streamon(&layer
->buffer_queue
, buf_type
);
1292 v4l2_err(&vpbe_dev
->v4l2_dev
,
1293 "error in vb2_streamon\n");
1299 static int vpbe_display_dqbuf(struct file
*file
, void *priv
,
1300 struct v4l2_buffer
*buf
)
1302 struct vpbe_fh
*fh
= file
->private_data
;
1303 struct vpbe_layer
*layer
= fh
->layer
;
1304 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1307 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
1308 "VIDIOC_DQBUF, layer id = %d\n",
1311 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= buf
->type
) {
1312 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buffer type\n");
1315 /* If this file handle is not allowed to do IO, return error */
1316 if (!fh
->io_allowed
) {
1317 v4l2_err(&vpbe_dev
->v4l2_dev
, "No io_allowed\n");
1320 if (file
->f_flags
& O_NONBLOCK
)
1321 /* Call videobuf_dqbuf for non blocking mode */
1322 ret
= vb2_dqbuf(&layer
->buffer_queue
, buf
, 1);
1324 /* Call videobuf_dqbuf for blocking mode */
1325 ret
= vb2_dqbuf(&layer
->buffer_queue
, buf
, 0);
1330 static int vpbe_display_qbuf(struct file
*file
, void *priv
,
1331 struct v4l2_buffer
*p
)
1333 struct vpbe_fh
*fh
= file
->private_data
;
1334 struct vpbe_layer
*layer
= fh
->layer
;
1335 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1337 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
1338 "VIDIOC_QBUF, layer id = %d\n",
1341 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= p
->type
) {
1342 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buffer type\n");
1346 /* If this file handle is not allowed to do IO, return error */
1347 if (!fh
->io_allowed
) {
1348 v4l2_err(&vpbe_dev
->v4l2_dev
, "No io_allowed\n");
1352 return vb2_qbuf(&layer
->buffer_queue
, p
);
1355 static int vpbe_display_querybuf(struct file
*file
, void *priv
,
1356 struct v4l2_buffer
*buf
)
1358 struct vpbe_fh
*fh
= file
->private_data
;
1359 struct vpbe_layer
*layer
= fh
->layer
;
1360 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1362 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
1363 "VIDIOC_QUERYBUF, layer id = %d\n",
1366 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= buf
->type
) {
1367 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buffer type\n");
1370 /* Call vb2_querybuf to get information */
1371 return vb2_querybuf(&layer
->buffer_queue
, buf
);
1374 static int vpbe_display_reqbufs(struct file
*file
, void *priv
,
1375 struct v4l2_requestbuffers
*req_buf
)
1377 struct vpbe_fh
*fh
= file
->private_data
;
1378 struct vpbe_layer
*layer
= fh
->layer
;
1379 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1380 struct vb2_queue
*q
;
1382 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "vpbe_display_reqbufs\n");
1384 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= req_buf
->type
) {
1385 v4l2_err(&vpbe_dev
->v4l2_dev
, "Invalid buffer type\n");
1389 /* If io users of the layer is not zero, return error */
1390 if (0 != layer
->io_usrs
) {
1391 v4l2_err(&vpbe_dev
->v4l2_dev
, "not IO user\n");
1394 /* Initialize videobuf queue as per the buffer type */
1395 layer
->alloc_ctx
= vb2_dma_contig_init_ctx(vpbe_dev
->pdev
);
1396 if (!layer
->alloc_ctx
) {
1397 v4l2_err(&vpbe_dev
->v4l2_dev
, "Failed to get the context\n");
1400 q
= &layer
->buffer_queue
;
1401 memset(q
, 0, sizeof(*q
));
1402 q
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1403 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1405 q
->ops
= &video_qops
;
1406 q
->mem_ops
= &vb2_dma_contig_memops
;
1407 q
->buf_struct_size
= sizeof(struct vpbe_disp_buffer
);
1409 ret
= vb2_queue_init(q
);
1411 v4l2_err(&vpbe_dev
->v4l2_dev
, "vb2_queue_init() failed\n");
1412 vb2_dma_contig_cleanup_ctx(layer
->alloc_ctx
);
1415 /* Set io allowed member of file handle to TRUE */
1417 /* Increment io usrs member of layer object to 1 */
1419 /* Store type of memory requested in layer object */
1420 layer
->memory
= req_buf
->memory
;
1421 /* Initialize buffer queue */
1422 INIT_LIST_HEAD(&layer
->dma_queue
);
1423 /* Allocate buffers */
1424 return vb2_reqbufs(q
, req_buf
);
1428 * vpbe_display_mmap()
1429 * It is used to map kernel space buffers into user spaces
1431 static int vpbe_display_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
1433 /* Get the layer object and file handle object */
1434 struct vpbe_fh
*fh
= filep
->private_data
;
1435 struct vpbe_layer
*layer
= fh
->layer
;
1436 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1439 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "vpbe_display_mmap\n");
1441 if (mutex_lock_interruptible(&layer
->opslock
))
1442 return -ERESTARTSYS
;
1443 ret
= vb2_mmap(&layer
->buffer_queue
, vma
);
1444 mutex_unlock(&layer
->opslock
);
1448 /* vpbe_display_poll(): It is used for select/poll system call
1450 static unsigned int vpbe_display_poll(struct file
*filep
, poll_table
*wait
)
1452 struct vpbe_fh
*fh
= filep
->private_data
;
1453 struct vpbe_layer
*layer
= fh
->layer
;
1454 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1455 unsigned int err
= 0;
1457 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "vpbe_display_poll\n");
1458 if (layer
->started
) {
1459 mutex_lock(&layer
->opslock
);
1460 err
= vb2_poll(&layer
->buffer_queue
, filep
, wait
);
1461 mutex_unlock(&layer
->opslock
);
1467 * vpbe_display_open()
1468 * It creates object of file handle structure and stores it in private_data
1469 * member of filepointer
1471 static int vpbe_display_open(struct file
*file
)
1473 struct vpbe_fh
*fh
= NULL
;
1474 struct vpbe_layer
*layer
= video_drvdata(file
);
1475 struct vpbe_display
*disp_dev
= layer
->disp_dev
;
1476 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
1477 struct osd_state
*osd_device
= disp_dev
->osd_device
;
1480 /* Allocate memory for the file handle object */
1481 fh
= kmalloc(sizeof(struct vpbe_fh
), GFP_KERNEL
);
1483 v4l2_err(&vpbe_dev
->v4l2_dev
,
1484 "unable to allocate memory for file handle object\n");
1487 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
1488 "vpbe display open plane = %d\n",
1491 /* store pointer to fh in private_data member of filep */
1492 file
->private_data
= fh
;
1494 fh
->disp_dev
= disp_dev
;
1497 if (mutex_lock_interruptible(&layer
->opslock
))
1498 return -ERESTARTSYS
;
1499 /* First claim the layer for this device */
1500 err
= osd_device
->ops
.request_layer(osd_device
,
1501 layer
->layer_info
.id
);
1502 mutex_unlock(&layer
->opslock
);
1504 /* Couldn't get layer */
1505 v4l2_err(&vpbe_dev
->v4l2_dev
,
1506 "Display Manager failed to allocate layer\n");
1511 /* Increment layer usrs counter */
1513 /* Set io_allowed member to false */
1515 /* Initialize priority of this instance to default priority */
1516 fh
->prio
= V4L2_PRIORITY_UNSET
;
1517 v4l2_prio_open(&layer
->prio
, &fh
->prio
);
1518 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
,
1519 "vpbe display device opened successfully\n");
1524 * vpbe_display_release()
1525 * This function deletes buffer queue, frees the buffers and the davinci
1526 * display file * handle
1528 static int vpbe_display_release(struct file
*file
)
1530 /* Get the layer object and file handle object */
1531 struct vpbe_fh
*fh
= file
->private_data
;
1532 struct vpbe_layer
*layer
= fh
->layer
;
1533 struct osd_layer_config
*cfg
= &layer
->layer_info
.config
;
1534 struct vpbe_display
*disp_dev
= fh
->disp_dev
;
1535 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
1536 struct osd_state
*osd_device
= disp_dev
->osd_device
;
1538 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "vpbe_display_release\n");
1540 mutex_lock(&layer
->opslock
);
1541 /* if this instance is doing IO */
1542 if (fh
->io_allowed
) {
1543 /* Reset io_usrs member of layer object */
1546 osd_device
->ops
.disable_layer(osd_device
,
1547 layer
->layer_info
.id
);
1549 /* Free buffers allocated */
1550 vb2_queue_release(&layer
->buffer_queue
);
1551 vb2_dma_contig_cleanup_ctx(&layer
->buffer_queue
);
1554 /* Decrement layer usrs counter */
1556 /* If this file handle has initialize encoder device, reset it */
1558 if (cfg
->pixfmt
== PIXFMT_NV12
) {
1559 struct vpbe_layer
*otherlayer
;
1561 _vpbe_display_get_other_win_layer(disp_dev
, layer
);
1562 osd_device
->ops
.disable_layer(osd_device
,
1563 otherlayer
->layer_info
.id
);
1564 osd_device
->ops
.release_layer(osd_device
,
1565 otherlayer
->layer_info
.id
);
1567 osd_device
->ops
.disable_layer(osd_device
,
1568 layer
->layer_info
.id
);
1569 osd_device
->ops
.release_layer(osd_device
,
1570 layer
->layer_info
.id
);
1572 /* Close the priority */
1573 v4l2_prio_close(&layer
->prio
, fh
->prio
);
1574 file
->private_data
= NULL
;
1575 mutex_unlock(&layer
->opslock
);
1577 /* Free memory allocated to file handle object */
1580 disp_dev
->cbcr_ofst
= 0;
1585 #ifdef CONFIG_VIDEO_ADV_DEBUG
1586 static int vpbe_display_g_register(struct file
*file
, void *priv
,
1587 struct v4l2_dbg_register
*reg
)
1589 struct v4l2_dbg_match
*match
= ®
->match
;
1590 struct vpbe_fh
*fh
= file
->private_data
;
1591 struct vpbe_device
*vpbe_dev
= fh
->disp_dev
->vpbe_dev
;
1593 if (match
->type
>= 2) {
1594 v4l2_subdev_call(vpbe_dev
->venc
,
1603 static int vpbe_display_s_register(struct file
*file
, void *priv
,
1604 struct v4l2_dbg_register
*reg
)
1610 /* vpbe capture ioctl operations */
1611 static const struct v4l2_ioctl_ops vpbe_ioctl_ops
= {
1612 .vidioc_querycap
= vpbe_display_querycap
,
1613 .vidioc_g_fmt_vid_out
= vpbe_display_g_fmt
,
1614 .vidioc_enum_fmt_vid_out
= vpbe_display_enum_fmt
,
1615 .vidioc_s_fmt_vid_out
= vpbe_display_s_fmt
,
1616 .vidioc_try_fmt_vid_out
= vpbe_display_try_fmt
,
1617 .vidioc_reqbufs
= vpbe_display_reqbufs
,
1618 .vidioc_querybuf
= vpbe_display_querybuf
,
1619 .vidioc_qbuf
= vpbe_display_qbuf
,
1620 .vidioc_dqbuf
= vpbe_display_dqbuf
,
1621 .vidioc_streamon
= vpbe_display_streamon
,
1622 .vidioc_streamoff
= vpbe_display_streamoff
,
1623 .vidioc_cropcap
= vpbe_display_cropcap
,
1624 .vidioc_g_crop
= vpbe_display_g_crop
,
1625 .vidioc_s_crop
= vpbe_display_s_crop
,
1626 .vidioc_g_priority
= vpbe_display_g_priority
,
1627 .vidioc_s_priority
= vpbe_display_s_priority
,
1628 .vidioc_s_std
= vpbe_display_s_std
,
1629 .vidioc_g_std
= vpbe_display_g_std
,
1630 .vidioc_enum_output
= vpbe_display_enum_output
,
1631 .vidioc_s_output
= vpbe_display_s_output
,
1632 .vidioc_g_output
= vpbe_display_g_output
,
1633 .vidioc_s_dv_timings
= vpbe_display_s_dv_timings
,
1634 .vidioc_g_dv_timings
= vpbe_display_g_dv_timings
,
1635 .vidioc_enum_dv_timings
= vpbe_display_enum_dv_timings
,
1636 #ifdef CONFIG_VIDEO_ADV_DEBUG
1637 .vidioc_g_register
= vpbe_display_g_register
,
1638 .vidioc_s_register
= vpbe_display_s_register
,
1642 static struct v4l2_file_operations vpbe_fops
= {
1643 .owner
= THIS_MODULE
,
1644 .open
= vpbe_display_open
,
1645 .release
= vpbe_display_release
,
1646 .unlocked_ioctl
= video_ioctl2
,
1647 .mmap
= vpbe_display_mmap
,
1648 .poll
= vpbe_display_poll
1651 static int vpbe_device_get(struct device
*dev
, void *data
)
1653 struct platform_device
*pdev
= to_platform_device(dev
);
1654 struct vpbe_display
*vpbe_disp
= data
;
1656 if (strcmp("vpbe_controller", pdev
->name
) == 0)
1657 vpbe_disp
->vpbe_dev
= platform_get_drvdata(pdev
);
1659 if (strstr(pdev
->name
, "vpbe-osd") != NULL
)
1660 vpbe_disp
->osd_device
= platform_get_drvdata(pdev
);
1665 static __devinit
int init_vpbe_layer(int i
, struct vpbe_display
*disp_dev
,
1666 struct platform_device
*pdev
)
1668 struct vpbe_layer
*vpbe_display_layer
= NULL
;
1669 struct video_device
*vbd
= NULL
;
1671 /* Allocate memory for four plane display objects */
1674 kzalloc(sizeof(struct vpbe_layer
), GFP_KERNEL
);
1676 /* If memory allocation fails, return error */
1677 if (!disp_dev
->dev
[i
]) {
1678 printk(KERN_ERR
"ran out of memory\n");
1681 spin_lock_init(&disp_dev
->dev
[i
]->irqlock
);
1682 mutex_init(&disp_dev
->dev
[i
]->opslock
);
1684 /* Get the pointer to the layer object */
1685 vpbe_display_layer
= disp_dev
->dev
[i
];
1686 vbd
= &vpbe_display_layer
->video_dev
;
1687 /* Initialize field of video device */
1688 vbd
->release
= video_device_release_empty
;
1689 vbd
->fops
= &vpbe_fops
;
1690 vbd
->ioctl_ops
= &vpbe_ioctl_ops
;
1692 vbd
->v4l2_dev
= &disp_dev
->vpbe_dev
->v4l2_dev
;
1693 vbd
->lock
= &vpbe_display_layer
->opslock
;
1694 vbd
->vfl_dir
= VFL_DIR_TX
;
1696 if (disp_dev
->vpbe_dev
->current_timings
.timings_type
&
1698 vbd
->tvnorms
= (V4L2_STD_525_60
| V4L2_STD_625_50
);
1700 disp_dev
->vpbe_dev
->current_timings
.std_id
;
1702 vbd
->current_norm
= 0;
1704 snprintf(vbd
->name
, sizeof(vbd
->name
),
1705 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1706 (VPBE_DISPLAY_VERSION_CODE
>> 16) & 0xff,
1707 (VPBE_DISPLAY_VERSION_CODE
>> 8) & 0xff,
1708 (VPBE_DISPLAY_VERSION_CODE
) & 0xff);
1710 vpbe_display_layer
->device_id
= i
;
1712 vpbe_display_layer
->layer_info
.id
=
1713 ((i
== VPBE_DISPLAY_DEVICE_0
) ? WIN_VID0
: WIN_VID1
);
1715 /* Initialize prio member of layer object */
1716 v4l2_prio_init(&vpbe_display_layer
->prio
);
1721 static __devinit
int register_device(struct vpbe_layer
*vpbe_display_layer
,
1722 struct vpbe_display
*disp_dev
,
1723 struct platform_device
*pdev
) {
1726 v4l2_info(&disp_dev
->vpbe_dev
->v4l2_dev
,
1727 "Trying to register VPBE display device.\n");
1728 v4l2_info(&disp_dev
->vpbe_dev
->v4l2_dev
,
1729 "layer=%x,layer->video_dev=%x\n",
1730 (int)vpbe_display_layer
,
1731 (int)&vpbe_display_layer
->video_dev
);
1733 err
= video_register_device(&vpbe_display_layer
->video_dev
,
1739 vpbe_display_layer
->disp_dev
= disp_dev
;
1740 /* set the driver data in platform device */
1741 platform_set_drvdata(pdev
, disp_dev
);
1742 video_set_drvdata(&vpbe_display_layer
->video_dev
,
1743 vpbe_display_layer
);
1751 * vpbe_display_probe()
1752 * This function creates device entries by register itself to the V4L2 driver
1753 * and initializes fields of each layer objects
1755 static __devinit
int vpbe_display_probe(struct platform_device
*pdev
)
1757 struct vpbe_layer
*vpbe_display_layer
;
1758 struct vpbe_display
*disp_dev
;
1759 struct resource
*res
= NULL
;
1765 printk(KERN_DEBUG
"vpbe_display_probe\n");
1766 /* Allocate memory for vpbe_display */
1767 disp_dev
= kzalloc(sizeof(struct vpbe_display
), GFP_KERNEL
);
1769 printk(KERN_ERR
"ran out of memory\n");
1773 spin_lock_init(&disp_dev
->dma_queue_lock
);
1775 * Scan all the platform devices to find the vpbe
1776 * controller device and get the vpbe_dev object
1778 err
= bus_for_each_dev(&platform_bus_type
, NULL
, disp_dev
,
1782 /* Initialize the vpbe display controller */
1783 if (NULL
!= disp_dev
->vpbe_dev
->ops
.initialize
) {
1784 err
= disp_dev
->vpbe_dev
->ops
.initialize(&pdev
->dev
,
1785 disp_dev
->vpbe_dev
);
1787 v4l2_err(&disp_dev
->vpbe_dev
->v4l2_dev
,
1788 "Error initing vpbe\n");
1794 for (i
= 0; i
< VPBE_DISPLAY_MAX_DEVICES
; i
++) {
1795 if (init_vpbe_layer(i
, disp_dev
, pdev
)) {
1801 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1803 v4l2_err(&disp_dev
->vpbe_dev
->v4l2_dev
,
1804 "Unable to get VENC interrupt resource\n");
1810 if (request_irq(irq
, venc_isr
, IRQF_DISABLED
, VPBE_DISPLAY_DRIVER
,
1812 v4l2_err(&disp_dev
->vpbe_dev
->v4l2_dev
,
1813 "Unable to request interrupt\n");
1818 for (i
= 0; i
< VPBE_DISPLAY_MAX_DEVICES
; i
++) {
1819 if (register_device(disp_dev
->dev
[i
], disp_dev
, pdev
)) {
1825 printk(KERN_DEBUG
"Successfully completed the probing of vpbe v4l2 device\n");
1829 free_irq(res
->start
, disp_dev
);
1831 for (k
= 0; k
< VPBE_DISPLAY_MAX_DEVICES
; k
++) {
1832 /* Get the pointer to the layer object */
1833 vpbe_display_layer
= disp_dev
->dev
[k
];
1834 /* Unregister video device */
1835 if (vpbe_display_layer
) {
1836 video_unregister_device(
1837 &vpbe_display_layer
->video_dev
);
1838 kfree(disp_dev
->dev
[k
]);
1846 * vpbe_display_remove()
1847 * It un-register hardware layer from V4L2 driver
1849 static int vpbe_display_remove(struct platform_device
*pdev
)
1851 struct vpbe_layer
*vpbe_display_layer
;
1852 struct vpbe_display
*disp_dev
= platform_get_drvdata(pdev
);
1853 struct vpbe_device
*vpbe_dev
= disp_dev
->vpbe_dev
;
1854 struct resource
*res
;
1857 v4l2_dbg(1, debug
, &vpbe_dev
->v4l2_dev
, "vpbe_display_remove\n");
1859 /* unregister irq */
1860 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1861 free_irq(res
->start
, disp_dev
);
1863 /* deinitialize the vpbe display controller */
1864 if (NULL
!= vpbe_dev
->ops
.deinitialize
)
1865 vpbe_dev
->ops
.deinitialize(&pdev
->dev
, vpbe_dev
);
1866 /* un-register device */
1867 for (i
= 0; i
< VPBE_DISPLAY_MAX_DEVICES
; i
++) {
1868 /* Get the pointer to the layer object */
1869 vpbe_display_layer
= disp_dev
->dev
[i
];
1870 /* Unregister video device */
1871 video_unregister_device(&vpbe_display_layer
->video_dev
);
1874 for (i
= 0; i
< VPBE_DISPLAY_MAX_DEVICES
; i
++) {
1875 kfree(disp_dev
->dev
[i
]);
1876 disp_dev
->dev
[i
] = NULL
;
1882 static struct platform_driver vpbe_display_driver
= {
1884 .name
= VPBE_DISPLAY_DRIVER
,
1885 .owner
= THIS_MODULE
,
1886 .bus
= &platform_bus_type
,
1888 .probe
= vpbe_display_probe
,
1889 .remove
= __devexit_p(vpbe_display_remove
),
1892 module_platform_driver(vpbe_display_driver
);
1894 MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1895 MODULE_LICENSE("GPL");
1896 MODULE_AUTHOR("Texas Instruments");