4 * Copyright (C) 2010 Texas Instruments.
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
12 #include <linux/sched.h>
13 #include <linux/platform_device.h>
14 #include <linux/videodev2.h>
16 #include <media/videobuf-dma-contig.h>
17 #include <media/v4l2-device.h>
19 #include <linux/omap-dma.h>
20 #include <video/omapvrfb.h>
22 #include "omap_voutdef.h"
23 #include "omap_voutlib.h"
24 #include "omap_vout_vrfb.h"
26 #define OMAP_DMA_NO_DEVICE 0
29 * Function for allocating video buffers
31 static int omap_vout_allocate_vrfb_buffers(struct omap_vout_device
*vout
,
32 unsigned int *count
, int startindex
)
36 for (i
= 0; i
< *count
; i
++) {
37 if (!vout
->smsshado_virt_addr
[i
]) {
38 vout
->smsshado_virt_addr
[i
] =
39 omap_vout_alloc_buffer(vout
->smsshado_size
,
40 &vout
->smsshado_phy_addr
[i
]);
42 if (!vout
->smsshado_virt_addr
[i
] && startindex
!= -1) {
43 if (V4L2_MEMORY_MMAP
== vout
->memory
&& i
>= startindex
)
46 if (!vout
->smsshado_virt_addr
[i
]) {
47 for (j
= 0; j
< i
; j
++) {
48 omap_vout_free_buffer(
49 vout
->smsshado_virt_addr
[j
],
51 vout
->smsshado_virt_addr
[j
] = 0;
52 vout
->smsshado_phy_addr
[j
] = 0;
57 memset((void *) vout
->smsshado_virt_addr
[i
], 0,
64 * Wakes up the application once the DMA transfer to VRFB space is completed.
66 static void omap_vout_vrfb_dma_tx_callback(int lch
, u16 ch_status
, void *data
)
68 struct vid_vrfb_dma
*t
= (struct vid_vrfb_dma
*) data
;
71 wake_up_interruptible(&t
->wait
);
77 void omap_vout_free_vrfb_buffers(struct omap_vout_device
*vout
)
81 for (j
= 0; j
< VRFB_NUM_BUFS
; j
++) {
82 if (vout
->smsshado_virt_addr
[j
]) {
83 omap_vout_free_buffer(vout
->smsshado_virt_addr
[j
],
85 vout
->smsshado_virt_addr
[j
] = 0;
86 vout
->smsshado_phy_addr
[j
] = 0;
91 int omap_vout_setup_vrfb_bufs(struct platform_device
*pdev
, int vid_num
,
92 bool static_vrfb_allocation
)
95 struct omap_vout_device
*vout
;
96 struct video_device
*vfd
;
97 int image_width
, image_height
;
98 int vrfb_num_bufs
= VRFB_NUM_BUFS
;
99 struct v4l2_device
*v4l2_dev
= platform_get_drvdata(pdev
);
100 struct omap2video_device
*vid_dev
=
101 container_of(v4l2_dev
, struct omap2video_device
, v4l2_dev
);
103 vout
= vid_dev
->vouts
[vid_num
];
106 for (i
= 0; i
< VRFB_NUM_BUFS
; i
++) {
107 if (omap_vrfb_request_ctx(&vout
->vrfb_context
[i
])) {
108 dev_info(&pdev
->dev
, ": VRFB allocation failed\n");
109 for (j
= 0; j
< i
; j
++)
110 omap_vrfb_release_ctx(&vout
->vrfb_context
[j
]);
116 /* Calculate VRFB memory size */
117 /* allocate for worst case size */
118 image_width
= VID_MAX_WIDTH
/ TILE_SIZE
;
119 if (VID_MAX_WIDTH
% TILE_SIZE
)
122 image_width
= image_width
* TILE_SIZE
;
123 image_height
= VID_MAX_HEIGHT
/ TILE_SIZE
;
125 if (VID_MAX_HEIGHT
% TILE_SIZE
)
128 image_height
= image_height
* TILE_SIZE
;
129 vout
->smsshado_size
= PAGE_ALIGN(image_width
* image_height
* 2 * 2);
132 * Request and Initialize DMA, for DMA based VRFB transfer
134 vout
->vrfb_dma_tx
.dev_id
= OMAP_DMA_NO_DEVICE
;
135 vout
->vrfb_dma_tx
.dma_ch
= -1;
136 vout
->vrfb_dma_tx
.req_status
= DMA_CHAN_ALLOTED
;
137 ret
= omap_request_dma(vout
->vrfb_dma_tx
.dev_id
, "VRFB DMA TX",
138 omap_vout_vrfb_dma_tx_callback
,
139 (void *) &vout
->vrfb_dma_tx
, &vout
->vrfb_dma_tx
.dma_ch
);
141 vout
->vrfb_dma_tx
.req_status
= DMA_CHAN_NOT_ALLOTED
;
143 ": failed to allocate DMA Channel for video%d\n",
146 init_waitqueue_head(&vout
->vrfb_dma_tx
.wait
);
148 /* statically allocated the VRFB buffer is done through
149 commands line aruments */
150 if (static_vrfb_allocation
) {
151 if (omap_vout_allocate_vrfb_buffers(vout
, &vrfb_num_bufs
, -1)) {
153 goto release_vrfb_ctx
;
155 vout
->vrfb_static_allocation
= true;
160 for (j
= 0; j
< VRFB_NUM_BUFS
; j
++)
161 omap_vrfb_release_ctx(&vout
->vrfb_context
[j
]);
163 omap_vout_free_buffers(vout
);
169 * Release the VRFB context once the module exits
171 void omap_vout_release_vrfb(struct omap_vout_device
*vout
)
175 for (i
= 0; i
< VRFB_NUM_BUFS
; i
++)
176 omap_vrfb_release_ctx(&vout
->vrfb_context
[i
]);
178 if (vout
->vrfb_dma_tx
.req_status
== DMA_CHAN_ALLOTED
) {
179 vout
->vrfb_dma_tx
.req_status
= DMA_CHAN_NOT_ALLOTED
;
180 omap_free_dma(vout
->vrfb_dma_tx
.dma_ch
);
185 * Allocate the buffers for the VRFB space. Data is copied from V4L2
186 * buffers to the VRFB buffers using the DMA engine.
188 int omap_vout_vrfb_buffer_setup(struct omap_vout_device
*vout
,
189 unsigned int *count
, unsigned int startindex
)
194 if (!is_rotation_enabled(vout
))
197 /* If rotation is enabled, allocate memory for VRFB space also */
198 *count
= *count
> VRFB_NUM_BUFS
? VRFB_NUM_BUFS
: *count
;
200 /* Allocate the VRFB buffers only if the buffers are not
201 * allocated during init time.
203 if (!vout
->vrfb_static_allocation
)
204 if (omap_vout_allocate_vrfb_buffers(vout
, count
, startindex
))
207 if (vout
->dss_mode
== OMAP_DSS_COLOR_YUV2
||
208 vout
->dss_mode
== OMAP_DSS_COLOR_UYVY
)
213 for (i
= 0; i
< *count
; i
++)
214 omap_vrfb_setup(&vout
->vrfb_context
[i
],
215 vout
->smsshado_phy_addr
[i
], vout
->pix
.width
,
216 vout
->pix
.height
, vout
->bpp
, yuv_mode
);
221 int omap_vout_prepare_vrfb(struct omap_vout_device
*vout
,
222 struct videobuf_buffer
*vb
)
225 struct vid_vrfb_dma
*tx
;
226 enum dss_rotation rotation
;
227 u32 dest_frame_index
= 0, src_element_index
= 0;
228 u32 dest_element_index
= 0, src_frame_index
= 0;
229 u32 elem_count
= 0, frame_count
= 0, pixsize
= 2;
231 if (!is_rotation_enabled(vout
))
234 dmabuf
= vout
->buf_phy_addr
[vb
->i
];
235 /* If rotation is enabled, copy input buffer into VRFB
236 * memory space using DMA. We are copying input buffer
237 * into VRFB memory space of desired angle and DSS will
238 * read image VRFB memory for 0 degree angle
240 pixsize
= vout
->bpp
* vout
->vrfb_bpp
;
242 * DMA transfer in double index mode
246 dest_frame_index
= ((MAX_PIXELS_PER_LINE
* pixsize
) -
247 (vout
->pix
.width
* vout
->bpp
)) + 1;
249 /* Source and destination parameters */
250 src_element_index
= 0;
252 dest_element_index
= 1;
253 /* Number of elements per frame */
254 elem_count
= vout
->pix
.width
* vout
->bpp
;
255 frame_count
= vout
->pix
.height
;
256 tx
= &vout
->vrfb_dma_tx
;
258 omap_set_dma_transfer_params(tx
->dma_ch
, OMAP_DMA_DATA_TYPE_S32
,
259 (elem_count
/ 4), frame_count
, OMAP_DMA_SYNC_ELEMENT
,
261 /* src_port required only for OMAP1 */
262 omap_set_dma_src_params(tx
->dma_ch
, 0, OMAP_DMA_AMODE_POST_INC
,
263 dmabuf
, src_element_index
, src_frame_index
);
264 /*set dma source burst mode for VRFB */
265 omap_set_dma_src_burst_mode(tx
->dma_ch
, OMAP_DMA_DATA_BURST_16
);
266 rotation
= calc_rotation(vout
);
268 /* dest_port required only for OMAP1 */
269 omap_set_dma_dest_params(tx
->dma_ch
, 0, OMAP_DMA_AMODE_DOUBLE_IDX
,
270 vout
->vrfb_context
[vb
->i
].paddr
[0], dest_element_index
,
272 /*set dma dest burst mode for VRFB */
273 omap_set_dma_dest_burst_mode(tx
->dma_ch
, OMAP_DMA_DATA_BURST_16
);
274 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE
, 0x20, 0);
276 omap_start_dma(tx
->dma_ch
);
277 wait_event_interruptible_timeout(tx
->wait
, tx
->tx_status
== 1,
280 if (tx
->tx_status
== 0) {
281 omap_stop_dma(tx
->dma_ch
);
284 /* Store buffers physical address into an array. Addresses
285 * from this array will be used to configure DSS */
286 vout
->queued_buf_addr
[vb
->i
] = (u8
*)
287 vout
->vrfb_context
[vb
->i
].paddr
[rotation
];
292 * Calculate the buffer offsets from which the streaming should
293 * start. This offset calculation is mainly required because of
294 * the VRFB 32 pixels alignment with rotation.
296 void omap_vout_calculate_vrfb_offset(struct omap_vout_device
*vout
)
298 enum dss_rotation rotation
;
299 bool mirroring
= vout
->mirror
;
300 struct v4l2_rect
*crop
= &vout
->crop
;
301 struct v4l2_pix_format
*pix
= &vout
->pix
;
302 int *cropped_offset
= &vout
->cropped_offset
;
303 int vr_ps
= 1, ps
= 2, temp_ps
= 2;
304 int offset
= 0, ctop
= 0, cleft
= 0, line_length
= 0;
306 rotation
= calc_rotation(vout
);
308 if (V4L2_PIX_FMT_YUYV
== pix
->pixelformat
||
309 V4L2_PIX_FMT_UYVY
== pix
->pixelformat
) {
310 if (is_rotation_enabled(vout
)) {
312 * ps - Actual pixel size for YUYV/UYVY for
313 * VRFB/Mirroring is 4 bytes
314 * vr_ps - Virtually pixel size for YUYV/UYVY is
320 ps
= 2; /* otherwise the pixel size is 2 byte */
322 } else if (V4L2_PIX_FMT_RGB32
== pix
->pixelformat
) {
324 } else if (V4L2_PIX_FMT_RGB24
== pix
->pixelformat
) {
330 if (is_rotation_enabled(vout
)) {
331 line_length
= MAX_PIXELS_PER_LINE
;
332 ctop
= (pix
->height
- crop
->height
) - crop
->top
;
333 cleft
= (pix
->width
- crop
->width
) - crop
->left
;
335 line_length
= pix
->width
;
337 vout
->line_length
= line_length
;
339 case dss_rotation_90_degree
:
340 offset
= vout
->vrfb_context
[0].yoffset
*
341 vout
->vrfb_context
[0].bytespp
;
342 temp_ps
= ps
/ vr_ps
;
344 *cropped_offset
= offset
+ line_length
*
345 temp_ps
* cleft
+ crop
->top
* temp_ps
;
347 *cropped_offset
= offset
+ line_length
* temp_ps
*
348 cleft
+ crop
->top
* temp_ps
+ (line_length
*
349 ((crop
->width
/ (vr_ps
)) - 1) * ps
);
352 case dss_rotation_180_degree
:
353 offset
= ((MAX_PIXELS_PER_LINE
* vout
->vrfb_context
[0].yoffset
*
354 vout
->vrfb_context
[0].bytespp
) +
355 (vout
->vrfb_context
[0].xoffset
*
356 vout
->vrfb_context
[0].bytespp
));
358 *cropped_offset
= offset
+ (line_length
* ps
* ctop
) +
359 (cleft
/ vr_ps
) * ps
;
362 *cropped_offset
= offset
+ (line_length
* ps
* ctop
) +
363 (cleft
/ vr_ps
) * ps
+ (line_length
*
364 (crop
->height
- 1) * ps
);
367 case dss_rotation_270_degree
:
368 offset
= MAX_PIXELS_PER_LINE
* vout
->vrfb_context
[0].xoffset
*
369 vout
->vrfb_context
[0].bytespp
;
370 temp_ps
= ps
/ vr_ps
;
372 *cropped_offset
= offset
+ line_length
*
373 temp_ps
* crop
->left
+ ctop
* ps
;
375 *cropped_offset
= offset
+ line_length
*
376 temp_ps
* crop
->left
+ ctop
* ps
+
377 (line_length
* ((crop
->width
/ vr_ps
) - 1) *
381 case dss_rotation_0_degree
:
383 *cropped_offset
= (line_length
* ps
) *
384 crop
->top
+ (crop
->left
/ vr_ps
) * ps
;
386 *cropped_offset
= (line_length
* ps
) *
387 crop
->top
+ (crop
->left
/ vr_ps
) * ps
+
388 (line_length
* (crop
->height
- 1) * ps
);
392 *cropped_offset
= (line_length
* ps
* crop
->top
) /
393 vr_ps
+ (crop
->left
* ps
) / vr_ps
+
394 ((crop
->width
/ vr_ps
) - 1) * ps
;