]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/media/platform/omap/omap_vout_vrfb.c
Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[karo-tx-linux.git] / drivers / media / platform / omap / omap_vout_vrfb.c
1 /*
2  * omap_vout_vrfb.c
3  *
4  * Copyright (C) 2010 Texas Instruments.
5  *
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.
9  *
10  */
11
12 #include <linux/sched.h>
13 #include <linux/platform_device.h>
14 #include <linux/videodev2.h>
15
16 #include <media/videobuf-dma-contig.h>
17 #include <media/v4l2-device.h>
18
19 #include <video/omapvrfb.h>
20
21 #include "omap_voutdef.h"
22 #include "omap_voutlib.h"
23 #include "omap_vout_vrfb.h"
24
25 #define OMAP_DMA_NO_DEVICE      0
26
27 /*
28  * Function for allocating video buffers
29  */
30 static int omap_vout_allocate_vrfb_buffers(struct omap_vout_device *vout,
31                 unsigned int *count, int startindex)
32 {
33         int i, j;
34
35         for (i = 0; i < *count; i++) {
36                 if (!vout->smsshado_virt_addr[i]) {
37                         vout->smsshado_virt_addr[i] =
38                                 omap_vout_alloc_buffer(vout->smsshado_size,
39                                                 &vout->smsshado_phy_addr[i]);
40                 }
41                 if (!vout->smsshado_virt_addr[i] && startindex != -1) {
42                         if (V4L2_MEMORY_MMAP == vout->memory && i >= startindex)
43                                 break;
44                 }
45                 if (!vout->smsshado_virt_addr[i]) {
46                         for (j = 0; j < i; j++) {
47                                 omap_vout_free_buffer(
48                                                 vout->smsshado_virt_addr[j],
49                                                 vout->smsshado_size);
50                                 vout->smsshado_virt_addr[j] = 0;
51                                 vout->smsshado_phy_addr[j] = 0;
52                         }
53                         *count = 0;
54                         return -ENOMEM;
55                 }
56                 memset((void *) vout->smsshado_virt_addr[i], 0,
57                                 vout->smsshado_size);
58         }
59         return 0;
60 }
61
62 /*
63  * Wakes up the application once the DMA transfer to VRFB space is completed.
64  */
65 static void omap_vout_vrfb_dma_tx_callback(void *data)
66 {
67         struct vid_vrfb_dma *t = (struct vid_vrfb_dma *) data;
68
69         t->tx_status = 1;
70         wake_up_interruptible(&t->wait);
71 }
72
73 /*
74  * Free VRFB buffers
75  */
76 void omap_vout_free_vrfb_buffers(struct omap_vout_device *vout)
77 {
78         int j;
79
80         for (j = 0; j < VRFB_NUM_BUFS; j++) {
81                 if (vout->smsshado_virt_addr[j]) {
82                         omap_vout_free_buffer(vout->smsshado_virt_addr[j],
83                                               vout->smsshado_size);
84                         vout->smsshado_virt_addr[j] = 0;
85                         vout->smsshado_phy_addr[j] = 0;
86                 }
87         }
88 }
89
90 int omap_vout_setup_vrfb_bufs(struct platform_device *pdev, int vid_num,
91                               bool static_vrfb_allocation)
92 {
93         int ret = 0, i, j;
94         struct omap_vout_device *vout;
95         struct video_device *vfd;
96         dma_cap_mask_t mask;
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);
102
103         vout = vid_dev->vouts[vid_num];
104         vfd = vout->vfd;
105
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]);
111                         ret = -ENOMEM;
112                         goto free_buffers;
113                 }
114         }
115
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)
120                 image_width++;
121
122         image_width = image_width * TILE_SIZE;
123         image_height = VID_MAX_HEIGHT / TILE_SIZE;
124
125         if (VID_MAX_HEIGHT % TILE_SIZE)
126                 image_height++;
127
128         image_height = image_height * TILE_SIZE;
129         vout->smsshado_size = PAGE_ALIGN(image_width * image_height * 2 * 2);
130
131         /*
132          * Request and Initialize DMA, for DMA based VRFB transfer
133          */
134         dma_cap_zero(mask);
135         dma_cap_set(DMA_INTERLEAVE, mask);
136         vout->vrfb_dma_tx.chan = dma_request_chan_by_mask(&mask);
137         if (IS_ERR(vout->vrfb_dma_tx.chan)) {
138                 vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
139         } else {
140                 size_t xt_size = sizeof(struct dma_interleaved_template) +
141                                  sizeof(struct data_chunk);
142
143                 vout->vrfb_dma_tx.xt = kzalloc(xt_size, GFP_KERNEL);
144                 if (!vout->vrfb_dma_tx.xt) {
145                         dma_release_channel(vout->vrfb_dma_tx.chan);
146                         vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
147                 }
148         }
149
150         if (vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED)
151                 dev_info(&pdev->dev,
152                          ": failed to allocate DMA Channel for video%d\n",
153                          vfd->minor);
154
155         init_waitqueue_head(&vout->vrfb_dma_tx.wait);
156
157         /* statically allocated the VRFB buffer is done through
158            commands line aruments */
159         if (static_vrfb_allocation) {
160                 if (omap_vout_allocate_vrfb_buffers(vout, &vrfb_num_bufs, -1)) {
161                         ret =  -ENOMEM;
162                         goto release_vrfb_ctx;
163                 }
164                 vout->vrfb_static_allocation = true;
165         }
166         return 0;
167
168 release_vrfb_ctx:
169         for (j = 0; j < VRFB_NUM_BUFS; j++)
170                 omap_vrfb_release_ctx(&vout->vrfb_context[j]);
171 free_buffers:
172         omap_vout_free_buffers(vout);
173
174         return ret;
175 }
176
177 /*
178  * Release the VRFB context once the module exits
179  */
180 void omap_vout_release_vrfb(struct omap_vout_device *vout)
181 {
182         int i;
183
184         for (i = 0; i < VRFB_NUM_BUFS; i++)
185                 omap_vrfb_release_ctx(&vout->vrfb_context[i]);
186
187         if (vout->vrfb_dma_tx.req_status == DMA_CHAN_ALLOTED) {
188                 vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
189                 kfree(vout->vrfb_dma_tx.xt);
190                 dmaengine_terminate_sync(vout->vrfb_dma_tx.chan);
191                 dma_release_channel(vout->vrfb_dma_tx.chan);
192         }
193 }
194
195 /*
196  * Allocate the buffers for the VRFB space.  Data is copied from V4L2
197  * buffers to the VRFB buffers using the DMA engine.
198  */
199 int omap_vout_vrfb_buffer_setup(struct omap_vout_device *vout,
200                           unsigned int *count, unsigned int startindex)
201 {
202         int i;
203         bool yuv_mode;
204
205         if (!is_rotation_enabled(vout))
206                 return 0;
207
208         /* If rotation is enabled, allocate memory for VRFB space also */
209         *count = *count > VRFB_NUM_BUFS ? VRFB_NUM_BUFS : *count;
210
211         /* Allocate the VRFB buffers only if the buffers are not
212          * allocated during init time.
213          */
214         if (!vout->vrfb_static_allocation)
215                 if (omap_vout_allocate_vrfb_buffers(vout, count, startindex))
216                         return -ENOMEM;
217
218         if (vout->dss_mode == OMAP_DSS_COLOR_YUV2 ||
219                         vout->dss_mode == OMAP_DSS_COLOR_UYVY)
220                 yuv_mode = true;
221         else
222                 yuv_mode = false;
223
224         for (i = 0; i < *count; i++)
225                 omap_vrfb_setup(&vout->vrfb_context[i],
226                                 vout->smsshado_phy_addr[i], vout->pix.width,
227                                 vout->pix.height, vout->bpp, yuv_mode);
228
229         return 0;
230 }
231
232 int omap_vout_prepare_vrfb(struct omap_vout_device *vout,
233                            struct videobuf_buffer *vb)
234 {
235         struct dma_async_tx_descriptor *tx;
236         enum dma_ctrl_flags flags;
237         struct dma_chan *chan = vout->vrfb_dma_tx.chan;
238         struct dma_device *dmadev = chan->device;
239         struct dma_interleaved_template *xt = vout->vrfb_dma_tx.xt;
240         dma_cookie_t cookie;
241         enum dma_status status;
242         enum dss_rotation rotation;
243         size_t dst_icg;
244         u32 pixsize;
245
246         if (!is_rotation_enabled(vout))
247                 return 0;
248
249         /* If rotation is enabled, copy input buffer into VRFB
250          * memory space using DMA. We are copying input buffer
251          * into VRFB memory space of desired angle and DSS will
252          * read image VRFB memory for 0 degree angle
253          */
254
255         pixsize = vout->bpp * vout->vrfb_bpp;
256         dst_icg = ((MAX_PIXELS_PER_LINE * pixsize) -
257                   (vout->pix.width * vout->bpp)) + 1;
258
259         xt->src_start = vout->buf_phy_addr[vb->i];
260         xt->dst_start = vout->vrfb_context[vb->i].paddr[0];
261
262         xt->numf = vout->pix.height;
263         xt->frame_size = 1;
264         xt->sgl[0].size = vout->pix.width * vout->bpp;
265         xt->sgl[0].icg = dst_icg;
266
267         xt->dir = DMA_MEM_TO_MEM;
268         xt->src_sgl = false;
269         xt->src_inc = true;
270         xt->dst_sgl = true;
271         xt->dst_inc = true;
272
273         tx = dmadev->device_prep_interleaved_dma(chan, xt, flags);
274         if (tx == NULL) {
275                 pr_err("%s: DMA interleaved prep error\n", __func__);
276                 return -EINVAL;
277         }
278
279         tx->callback = omap_vout_vrfb_dma_tx_callback;
280         tx->callback_param = &vout->vrfb_dma_tx;
281
282         cookie = dmaengine_submit(tx);
283         if (dma_submit_error(cookie)) {
284                 pr_err("%s: dmaengine_submit failed (%d)\n", __func__, cookie);
285                 return -EINVAL;
286         }
287
288         vout->vrfb_dma_tx.tx_status = 0;
289         dma_async_issue_pending(chan);
290
291         wait_event_interruptible_timeout(vout->vrfb_dma_tx.wait,
292                                          vout->vrfb_dma_tx.tx_status == 1,
293                                          VRFB_TX_TIMEOUT);
294
295         status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
296
297         if (vout->vrfb_dma_tx.tx_status == 0) {
298                 pr_err("%s: Timeout while waiting for DMA\n", __func__);
299                 dmaengine_terminate_sync(chan);
300                 return -EINVAL;
301         } else if (status != DMA_COMPLETE) {
302                 pr_err("%s: DMA completion %s status\n", __func__,
303                        status == DMA_ERROR ? "error" : "busy");
304                 dmaengine_terminate_sync(chan);
305                 return -EINVAL;
306         }
307
308         /* Store buffers physical address into an array. Addresses
309          * from this array will be used to configure DSS */
310         rotation = calc_rotation(vout);
311         vout->queued_buf_addr[vb->i] = (u8 *)
312                 vout->vrfb_context[vb->i].paddr[rotation];
313         return 0;
314 }
315
316 /*
317  * Calculate the buffer offsets from which the streaming should
318  * start. This offset calculation is mainly required because of
319  * the VRFB 32 pixels alignment with rotation.
320  */
321 void omap_vout_calculate_vrfb_offset(struct omap_vout_device *vout)
322 {
323         enum dss_rotation rotation;
324         bool mirroring = vout->mirror;
325         struct v4l2_rect *crop = &vout->crop;
326         struct v4l2_pix_format *pix = &vout->pix;
327         int *cropped_offset = &vout->cropped_offset;
328         int vr_ps = 1, ps = 2, temp_ps = 2;
329         int offset = 0, ctop = 0, cleft = 0, line_length = 0;
330
331         rotation = calc_rotation(vout);
332
333         if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
334                         V4L2_PIX_FMT_UYVY == pix->pixelformat) {
335                 if (is_rotation_enabled(vout)) {
336                         /*
337                          * ps    - Actual pixel size for YUYV/UYVY for
338                          *         VRFB/Mirroring is 4 bytes
339                          * vr_ps - Virtually pixel size for YUYV/UYVY is
340                          *         2 bytes
341                          */
342                         ps = 4;
343                         vr_ps = 2;
344                 } else {
345                         ps = 2; /* otherwise the pixel size is 2 byte */
346                 }
347         } else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat) {
348                 ps = 4;
349         } else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat) {
350                 ps = 3;
351         }
352         vout->ps = ps;
353         vout->vr_ps = vr_ps;
354
355         if (is_rotation_enabled(vout)) {
356                 line_length = MAX_PIXELS_PER_LINE;
357                 ctop = (pix->height - crop->height) - crop->top;
358                 cleft = (pix->width - crop->width) - crop->left;
359         } else {
360                 line_length = pix->width;
361         }
362         vout->line_length = line_length;
363         switch (rotation) {
364         case dss_rotation_90_degree:
365                 offset = vout->vrfb_context[0].yoffset *
366                         vout->vrfb_context[0].bytespp;
367                 temp_ps = ps / vr_ps;
368                 if (!mirroring) {
369                         *cropped_offset = offset + line_length *
370                                 temp_ps * cleft + crop->top * temp_ps;
371                 } else {
372                         *cropped_offset = offset + line_length * temp_ps *
373                                 cleft + crop->top * temp_ps + (line_length *
374                                 ((crop->width / (vr_ps)) - 1) * ps);
375                 }
376                 break;
377         case dss_rotation_180_degree:
378                 offset = ((MAX_PIXELS_PER_LINE * vout->vrfb_context[0].yoffset *
379                         vout->vrfb_context[0].bytespp) +
380                         (vout->vrfb_context[0].xoffset *
381                         vout->vrfb_context[0].bytespp));
382                 if (!mirroring) {
383                         *cropped_offset = offset + (line_length * ps * ctop) +
384                                 (cleft / vr_ps) * ps;
385
386                 } else {
387                         *cropped_offset = offset + (line_length * ps * ctop) +
388                                 (cleft / vr_ps) * ps + (line_length *
389                                 (crop->height - 1) * ps);
390                 }
391                 break;
392         case dss_rotation_270_degree:
393                 offset = MAX_PIXELS_PER_LINE * vout->vrfb_context[0].xoffset *
394                         vout->vrfb_context[0].bytespp;
395                 temp_ps = ps / vr_ps;
396                 if (!mirroring) {
397                         *cropped_offset = offset + line_length *
398                             temp_ps * crop->left + ctop * ps;
399                 } else {
400                         *cropped_offset = offset + line_length *
401                                 temp_ps * crop->left + ctop * ps +
402                                 (line_length * ((crop->width / vr_ps) - 1) *
403                                  ps);
404                 }
405                 break;
406         case dss_rotation_0_degree:
407                 if (!mirroring) {
408                         *cropped_offset = (line_length * ps) *
409                                 crop->top + (crop->left / vr_ps) * ps;
410                 } else {
411                         *cropped_offset = (line_length * ps) *
412                                 crop->top + (crop->left / vr_ps) * ps +
413                                 (line_length * (crop->height - 1) * ps);
414                 }
415                 break;
416         default:
417                 *cropped_offset = (line_length * ps * crop->top) /
418                         vr_ps + (crop->left * ps) / vr_ps +
419                         ((crop->width / vr_ps) - 1) * ps;
420                 break;
421         }
422 }