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