linux/drivers/media/platform/sh_veu.c
<<
>>
Prefs
   1/*
   2 * sh-mobile VEU mem2mem driver
   3 *
   4 * Copyright (C) 2012 Renesas Electronics Corporation
   5 * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
   6 * Copyright (C) 2008 Magnus Damm
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the version 2 of the GNU General Public License as
  10 * published by the Free Software Foundation
  11 */
  12
  13#include <linux/err.h>
  14#include <linux/fs.h>
  15#include <linux/kernel.h>
  16#include <linux/module.h>
  17#include <linux/interrupt.h>
  18#include <linux/io.h>
  19#include <linux/platform_device.h>
  20#include <linux/pm_runtime.h>
  21#include <linux/slab.h>
  22#include <linux/types.h>
  23#include <linux/videodev2.h>
  24
  25#include <media/v4l2-dev.h>
  26#include <media/v4l2-device.h>
  27#include <media/v4l2-ioctl.h>
  28#include <media/v4l2-mem2mem.h>
  29#include <media/v4l2-image-sizes.h>
  30#include <media/videobuf2-dma-contig.h>
  31
  32#define VEU_STR 0x00 /* start register */
  33#define VEU_SWR 0x10 /* src: line length */
  34#define VEU_SSR 0x14 /* src: image size */
  35#define VEU_SAYR 0x18 /* src: y/rgb plane address */
  36#define VEU_SACR 0x1c /* src: c plane address */
  37#define VEU_BSSR 0x20 /* bundle mode register */
  38#define VEU_EDWR 0x30 /* dst: line length */
  39#define VEU_DAYR 0x34 /* dst: y/rgb plane address */
  40#define VEU_DACR 0x38 /* dst: c plane address */
  41#define VEU_TRCR 0x50 /* transform control */
  42#define VEU_RFCR 0x54 /* resize scale */
  43#define VEU_RFSR 0x58 /* resize clip */
  44#define VEU_ENHR 0x5c /* enhance */
  45#define VEU_FMCR 0x70 /* filter mode */
  46#define VEU_VTCR 0x74 /* lowpass vertical */
  47#define VEU_HTCR 0x78 /* lowpass horizontal */
  48#define VEU_APCR 0x80 /* color match */
  49#define VEU_ECCR 0x84 /* color replace */
  50#define VEU_AFXR 0x90 /* fixed mode */
  51#define VEU_SWPR 0x94 /* swap */
  52#define VEU_EIER 0xa0 /* interrupt mask */
  53#define VEU_EVTR 0xa4 /* interrupt event */
  54#define VEU_STAR 0xb0 /* status */
  55#define VEU_BSRR 0xb4 /* reset */
  56
  57#define VEU_MCR00 0x200 /* color conversion matrix coefficient 00 */
  58#define VEU_MCR01 0x204 /* color conversion matrix coefficient 01 */
  59#define VEU_MCR02 0x208 /* color conversion matrix coefficient 02 */
  60#define VEU_MCR10 0x20c /* color conversion matrix coefficient 10 */
  61#define VEU_MCR11 0x210 /* color conversion matrix coefficient 11 */
  62#define VEU_MCR12 0x214 /* color conversion matrix coefficient 12 */
  63#define VEU_MCR20 0x218 /* color conversion matrix coefficient 20 */
  64#define VEU_MCR21 0x21c /* color conversion matrix coefficient 21 */
  65#define VEU_MCR22 0x220 /* color conversion matrix coefficient 22 */
  66#define VEU_COFFR 0x224 /* color conversion offset */
  67#define VEU_CBR   0x228 /* color conversion clip */
  68
  69/*
  70 * 4092x4092 max size is the normal case. In some cases it can be reduced to
  71 * 2048x2048, in other cases it can be 4092x8188 or even 8188x8188.
  72 */
  73#define MAX_W 4092
  74#define MAX_H 4092
  75#define MIN_W 8
  76#define MIN_H 8
  77#define ALIGN_W 4
  78
  79/* 3 buffers of 2048 x 1536 - 3 megapixels @ 16bpp */
  80#define VIDEO_MEM_LIMIT ALIGN(2048 * 1536 * 2 * 3, 1024 * 1024)
  81
  82#define MEM2MEM_DEF_TRANSLEN 1
  83
  84struct sh_veu_dev;
  85
  86struct sh_veu_file {
  87        struct sh_veu_dev *veu_dev;
  88        bool cfg_needed;
  89};
  90
  91struct sh_veu_format {
  92        char *name;
  93        u32 fourcc;
  94        unsigned int depth;
  95        unsigned int ydepth;
  96};
  97
  98/* video data format */
  99struct sh_veu_vfmt {
 100        /* Replace with v4l2_rect */
 101        struct v4l2_rect                frame;
 102        unsigned int                    bytesperline;
 103        unsigned int                    offset_y;
 104        unsigned int                    offset_c;
 105        const struct sh_veu_format      *fmt;
 106};
 107
 108struct sh_veu_dev {
 109        struct v4l2_device v4l2_dev;
 110        struct video_device vdev;
 111        struct v4l2_m2m_dev *m2m_dev;
 112        struct device *dev;
 113        struct v4l2_m2m_ctx *m2m_ctx;
 114        struct sh_veu_vfmt vfmt_out;
 115        struct sh_veu_vfmt vfmt_in;
 116        /* Only single user per direction so far */
 117        struct sh_veu_file *capture;
 118        struct sh_veu_file *output;
 119        struct mutex fop_lock;
 120        void __iomem *base;
 121        struct vb2_alloc_ctx *alloc_ctx;
 122        spinlock_t lock;
 123        bool is_2h;
 124        unsigned int xaction;
 125        bool aborting;
 126};
 127
 128enum sh_veu_fmt_idx {
 129        SH_VEU_FMT_NV12,
 130        SH_VEU_FMT_NV16,
 131        SH_VEU_FMT_NV24,
 132        SH_VEU_FMT_RGB332,
 133        SH_VEU_FMT_RGB444,
 134        SH_VEU_FMT_RGB565,
 135        SH_VEU_FMT_RGB666,
 136        SH_VEU_FMT_RGB24,
 137};
 138
 139#define DEFAULT_IN_WIDTH        VGA_WIDTH
 140#define DEFAULT_IN_HEIGHT       VGA_HEIGHT
 141#define DEFAULT_IN_FMTIDX       SH_VEU_FMT_NV12
 142#define DEFAULT_OUT_WIDTH       VGA_WIDTH
 143#define DEFAULT_OUT_HEIGHT      VGA_HEIGHT
 144#define DEFAULT_OUT_FMTIDX      SH_VEU_FMT_RGB565
 145
 146/*
 147 * Alignment: Y-plane should be 4-byte aligned for NV12 and NV16, and 8-byte
 148 * aligned for NV24.
 149 */
 150static const struct sh_veu_format sh_veu_fmt[] = {
 151        [SH_VEU_FMT_NV12]   = { .ydepth = 8, .depth = 12, .name = "NV12", .fourcc = V4L2_PIX_FMT_NV12 },
 152        [SH_VEU_FMT_NV16]   = { .ydepth = 8, .depth = 16, .name = "NV16", .fourcc = V4L2_PIX_FMT_NV16 },
 153        [SH_VEU_FMT_NV24]   = { .ydepth = 8, .depth = 24, .name = "NV24", .fourcc = V4L2_PIX_FMT_NV24 },
 154        [SH_VEU_FMT_RGB332] = { .ydepth = 8, .depth = 8, .name = "RGB332", .fourcc = V4L2_PIX_FMT_RGB332 },
 155        [SH_VEU_FMT_RGB444] = { .ydepth = 16, .depth = 16, .name = "RGB444", .fourcc = V4L2_PIX_FMT_RGB444 },
 156        [SH_VEU_FMT_RGB565] = { .ydepth = 16, .depth = 16, .name = "RGB565", .fourcc = V4L2_PIX_FMT_RGB565 },
 157        [SH_VEU_FMT_RGB666] = { .ydepth = 32, .depth = 32, .name = "BGR666", .fourcc = V4L2_PIX_FMT_BGR666 },
 158        [SH_VEU_FMT_RGB24]  = { .ydepth = 24, .depth = 24, .name = "RGB24", .fourcc = V4L2_PIX_FMT_RGB24 },
 159};
 160
 161#define DEFAULT_IN_VFMT (struct sh_veu_vfmt){                                           \
 162        .frame = {                                                                      \
 163                .width = VGA_WIDTH,                                                     \
 164                .height = VGA_HEIGHT,                                                   \
 165        },                                                                              \
 166        .bytesperline = (VGA_WIDTH * sh_veu_fmt[DEFAULT_IN_FMTIDX].ydepth) >> 3,        \
 167        .fmt = &sh_veu_fmt[DEFAULT_IN_FMTIDX],                                          \
 168}
 169
 170#define DEFAULT_OUT_VFMT (struct sh_veu_vfmt){                                          \
 171        .frame = {                                                                      \
 172                .width = VGA_WIDTH,                                                     \
 173                .height = VGA_HEIGHT,                                                   \
 174        },                                                                              \
 175        .bytesperline = (VGA_WIDTH * sh_veu_fmt[DEFAULT_OUT_FMTIDX].ydepth) >> 3,       \
 176        .fmt = &sh_veu_fmt[DEFAULT_OUT_FMTIDX],                                         \
 177}
 178
 179/*
 180 * TODO: add support for further output formats:
 181 *      SH_VEU_FMT_NV12,
 182 *      SH_VEU_FMT_NV16,
 183 *      SH_VEU_FMT_NV24,
 184 *      SH_VEU_FMT_RGB332,
 185 *      SH_VEU_FMT_RGB444,
 186 *      SH_VEU_FMT_RGB666,
 187 *      SH_VEU_FMT_RGB24,
 188 */
 189
 190static const int sh_veu_fmt_out[] = {
 191        SH_VEU_FMT_RGB565,
 192};
 193
 194/*
 195 * TODO: add support for further input formats:
 196 *      SH_VEU_FMT_NV16,
 197 *      SH_VEU_FMT_NV24,
 198 *      SH_VEU_FMT_RGB565,
 199 *      SH_VEU_FMT_RGB666,
 200 *      SH_VEU_FMT_RGB24,
 201 */
 202static const int sh_veu_fmt_in[] = {
 203        SH_VEU_FMT_NV12,
 204};
 205
 206static enum v4l2_colorspace sh_veu_4cc2cspace(u32 fourcc)
 207{
 208        switch (fourcc) {
 209        default:
 210                BUG();
 211        case V4L2_PIX_FMT_NV12:
 212        case V4L2_PIX_FMT_NV16:
 213        case V4L2_PIX_FMT_NV24:
 214                return V4L2_COLORSPACE_JPEG;
 215        case V4L2_PIX_FMT_RGB332:
 216        case V4L2_PIX_FMT_RGB444:
 217        case V4L2_PIX_FMT_RGB565:
 218        case V4L2_PIX_FMT_BGR666:
 219        case V4L2_PIX_FMT_RGB24:
 220                return V4L2_COLORSPACE_SRGB;
 221        }
 222}
 223
 224static u32 sh_veu_reg_read(struct sh_veu_dev *veu, unsigned int reg)
 225{
 226        return ioread32(veu->base + reg);
 227}
 228
 229static void sh_veu_reg_write(struct sh_veu_dev *veu, unsigned int reg,
 230                             u32 value)
 231{
 232        iowrite32(value, veu->base + reg);
 233}
 234
 235                /* ========== mem2mem callbacks ========== */
 236
 237static void sh_veu_job_abort(void *priv)
 238{
 239        struct sh_veu_dev *veu = priv;
 240
 241        /* Will cancel the transaction in the next interrupt handler */
 242        veu->aborting = true;
 243}
 244
 245static void sh_veu_process(struct sh_veu_dev *veu,
 246                           struct vb2_buffer *src_buf,
 247                           struct vb2_buffer *dst_buf)
 248{
 249        dma_addr_t addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
 250
 251        sh_veu_reg_write(veu, VEU_DAYR, addr + veu->vfmt_out.offset_y);
 252        sh_veu_reg_write(veu, VEU_DACR, veu->vfmt_out.offset_c ?
 253                         addr + veu->vfmt_out.offset_c : 0);
 254        dev_dbg(veu->dev, "%s(): dst base %lx, y: %x, c: %x\n", __func__,
 255                (unsigned long)addr,
 256                veu->vfmt_out.offset_y, veu->vfmt_out.offset_c);
 257
 258        addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
 259        sh_veu_reg_write(veu, VEU_SAYR, addr + veu->vfmt_in.offset_y);
 260        sh_veu_reg_write(veu, VEU_SACR, veu->vfmt_in.offset_c ?
 261                         addr + veu->vfmt_in.offset_c : 0);
 262        dev_dbg(veu->dev, "%s(): src base %lx, y: %x, c: %x\n", __func__,
 263                (unsigned long)addr,
 264                veu->vfmt_in.offset_y, veu->vfmt_in.offset_c);
 265
 266        sh_veu_reg_write(veu, VEU_STR, 1);
 267
 268        sh_veu_reg_write(veu, VEU_EIER, 1); /* enable interrupt in VEU */
 269}
 270
 271/**
 272 * sh_veu_device_run() - prepares and starts the device
 273 *
 274 * This will be called by the framework when it decides to schedule a particular
 275 * instance.
 276 */
 277static void sh_veu_device_run(void *priv)
 278{
 279        struct sh_veu_dev *veu = priv;
 280        struct vb2_buffer *src_buf, *dst_buf;
 281
 282        src_buf = v4l2_m2m_next_src_buf(veu->m2m_ctx);
 283        dst_buf = v4l2_m2m_next_dst_buf(veu->m2m_ctx);
 284
 285        if (src_buf && dst_buf)
 286                sh_veu_process(veu, src_buf, dst_buf);
 287}
 288
 289                /* ========== video ioctls ========== */
 290
 291static bool sh_veu_is_streamer(struct sh_veu_dev *veu, struct sh_veu_file *veu_file,
 292                               enum v4l2_buf_type type)
 293{
 294        return (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
 295                veu_file == veu->capture) ||
 296                (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
 297                 veu_file == veu->output);
 298}
 299
 300static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
 301                             struct vb2_queue *dst_vq);
 302
 303/*
 304 * It is not unusual to have video nodes open()ed multiple times. While some
 305 * V4L2 operations are non-intrusive, like querying formats and various
 306 * parameters, others, like setting formats, starting and stopping streaming,
 307 * queuing and dequeuing buffers, directly affect hardware configuration and /
 308 * or execution. This function verifies availability of the requested interface
 309 * and, if available, reserves it for the requesting user.
 310 */
 311static int sh_veu_stream_init(struct sh_veu_dev *veu, struct sh_veu_file *veu_file,
 312                              enum v4l2_buf_type type)
 313{
 314        struct sh_veu_file **stream;
 315
 316        switch (type) {
 317        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 318                stream = &veu->capture;
 319                break;
 320        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 321                stream = &veu->output;
 322                break;
 323        default:
 324                return -EINVAL;
 325        }
 326
 327        if (*stream == veu_file)
 328                return 0;
 329
 330        if (*stream)
 331                return -EBUSY;
 332
 333        *stream = veu_file;
 334
 335        return 0;
 336}
 337
 338static int sh_veu_context_init(struct sh_veu_dev *veu)
 339{
 340        if (veu->m2m_ctx)
 341                return 0;
 342
 343        veu->m2m_ctx = v4l2_m2m_ctx_init(veu->m2m_dev, veu,
 344                                         sh_veu_queue_init);
 345
 346        return PTR_ERR_OR_ZERO(veu->m2m_ctx);
 347}
 348
 349static int sh_veu_querycap(struct file *file, void *priv,
 350                           struct v4l2_capability *cap)
 351{
 352        strlcpy(cap->driver, "sh-veu", sizeof(cap->driver));
 353        strlcpy(cap->card, "sh-mobile VEU", sizeof(cap->card));
 354        strlcpy(cap->bus_info, "platform:sh-veu", sizeof(cap->bus_info));
 355        cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
 356        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 357
 358        return 0;
 359}
 360
 361static int sh_veu_enum_fmt(struct v4l2_fmtdesc *f, const int *fmt, int fmt_num)
 362{
 363        if (f->index >= fmt_num)
 364                return -EINVAL;
 365
 366        strlcpy(f->description, sh_veu_fmt[fmt[f->index]].name, sizeof(f->description));
 367        f->pixelformat = sh_veu_fmt[fmt[f->index]].fourcc;
 368        return 0;
 369}
 370
 371static int sh_veu_enum_fmt_vid_cap(struct file *file, void *priv,
 372                                   struct v4l2_fmtdesc *f)
 373{
 374        return sh_veu_enum_fmt(f, sh_veu_fmt_out, ARRAY_SIZE(sh_veu_fmt_out));
 375}
 376
 377static int sh_veu_enum_fmt_vid_out(struct file *file, void *priv,
 378                                   struct v4l2_fmtdesc *f)
 379{
 380        return sh_veu_enum_fmt(f, sh_veu_fmt_in, ARRAY_SIZE(sh_veu_fmt_in));
 381}
 382
 383static struct sh_veu_vfmt *sh_veu_get_vfmt(struct sh_veu_dev *veu,
 384                                           enum v4l2_buf_type type)
 385{
 386        switch (type) {
 387        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 388                return &veu->vfmt_out;
 389        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 390                return &veu->vfmt_in;
 391        default:
 392                return NULL;
 393        }
 394}
 395
 396static int sh_veu_g_fmt(struct sh_veu_file *veu_file, struct v4l2_format *f)
 397{
 398        struct v4l2_pix_format *pix = &f->fmt.pix;
 399        struct sh_veu_dev *veu = veu_file->veu_dev;
 400        struct sh_veu_vfmt *vfmt;
 401
 402        vfmt = sh_veu_get_vfmt(veu, f->type);
 403
 404        pix->width              = vfmt->frame.width;
 405        pix->height             = vfmt->frame.height;
 406        pix->field              = V4L2_FIELD_NONE;
 407        pix->pixelformat        = vfmt->fmt->fourcc;
 408        pix->colorspace         = sh_veu_4cc2cspace(pix->pixelformat);
 409        pix->bytesperline       = vfmt->bytesperline;
 410        pix->sizeimage          = vfmt->bytesperline * pix->height *
 411                vfmt->fmt->depth / vfmt->fmt->ydepth;
 412        dev_dbg(veu->dev, "%s(): type: %d, size %u @ %ux%u, fmt %x\n", __func__,
 413                f->type, pix->sizeimage, pix->width, pix->height, pix->pixelformat);
 414
 415        return 0;
 416}
 417
 418static int sh_veu_g_fmt_vid_out(struct file *file, void *priv,
 419                                struct v4l2_format *f)
 420{
 421        return sh_veu_g_fmt(priv, f);
 422}
 423
 424static int sh_veu_g_fmt_vid_cap(struct file *file, void *priv,
 425                                struct v4l2_format *f)
 426{
 427        return sh_veu_g_fmt(priv, f);
 428}
 429
 430static int sh_veu_try_fmt(struct v4l2_format *f, const struct sh_veu_format *fmt)
 431{
 432        struct v4l2_pix_format *pix = &f->fmt.pix;
 433        unsigned int y_bytes_used;
 434
 435        /*
 436         * V4L2 specification suggests, that the driver should correct the
 437         * format struct if any of the dimensions is unsupported
 438         */
 439        switch (pix->field) {
 440        default:
 441        case V4L2_FIELD_ANY:
 442                pix->field = V4L2_FIELD_NONE;
 443                /* fall through: continue handling V4L2_FIELD_NONE */
 444        case V4L2_FIELD_NONE:
 445                break;
 446        }
 447
 448        v4l_bound_align_image(&pix->width, MIN_W, MAX_W, ALIGN_W,
 449                              &pix->height, MIN_H, MAX_H, 0, 0);
 450
 451        y_bytes_used = (pix->width * fmt->ydepth) >> 3;
 452
 453        if (pix->bytesperline < y_bytes_used)
 454                pix->bytesperline = y_bytes_used;
 455        pix->sizeimage = pix->height * pix->bytesperline * fmt->depth / fmt->ydepth;
 456
 457        pix->pixelformat        = fmt->fourcc;
 458        pix->colorspace         = sh_veu_4cc2cspace(pix->pixelformat);
 459
 460        pr_debug("%s(): type: %d, size %u\n", __func__, f->type, pix->sizeimage);
 461
 462        return 0;
 463}
 464
 465static const struct sh_veu_format *sh_veu_find_fmt(const struct v4l2_format *f)
 466{
 467        const int *fmt;
 468        int i, n, dflt;
 469
 470        pr_debug("%s(%d;%d)\n", __func__, f->type, f->fmt.pix.field);
 471
 472        switch (f->type) {
 473        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 474                fmt = sh_veu_fmt_out;
 475                n = ARRAY_SIZE(sh_veu_fmt_out);
 476                dflt = DEFAULT_OUT_FMTIDX;
 477                break;
 478        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 479        default:
 480                fmt = sh_veu_fmt_in;
 481                n = ARRAY_SIZE(sh_veu_fmt_in);
 482                dflt = DEFAULT_IN_FMTIDX;
 483                break;
 484        }
 485
 486        for (i = 0; i < n; i++)
 487                if (sh_veu_fmt[fmt[i]].fourcc == f->fmt.pix.pixelformat)
 488                        return &sh_veu_fmt[fmt[i]];
 489
 490        return &sh_veu_fmt[dflt];
 491}
 492
 493static int sh_veu_try_fmt_vid_cap(struct file *file, void *priv,
 494                                  struct v4l2_format *f)
 495{
 496        const struct sh_veu_format *fmt;
 497
 498        fmt = sh_veu_find_fmt(f);
 499        if (!fmt)
 500                /* wrong buffer type */
 501                return -EINVAL;
 502
 503        return sh_veu_try_fmt(f, fmt);
 504}
 505
 506static int sh_veu_try_fmt_vid_out(struct file *file, void *priv,
 507                                  struct v4l2_format *f)
 508{
 509        const struct sh_veu_format *fmt;
 510
 511        fmt = sh_veu_find_fmt(f);
 512        if (!fmt)
 513                /* wrong buffer type */
 514                return -EINVAL;
 515
 516        return sh_veu_try_fmt(f, fmt);
 517}
 518
 519static void sh_veu_colour_offset(struct sh_veu_dev *veu, struct sh_veu_vfmt *vfmt)
 520{
 521        /* dst_left and dst_top validity will be verified in CROP / COMPOSE */
 522        unsigned int left = vfmt->frame.left & ~0x03;
 523        unsigned int top = vfmt->frame.top;
 524        dma_addr_t offset = ((left * veu->vfmt_out.fmt->depth) >> 3) +
 525                top * veu->vfmt_out.bytesperline;
 526        unsigned int y_line;
 527
 528        vfmt->offset_y = offset;
 529
 530        switch (vfmt->fmt->fourcc) {
 531        case V4L2_PIX_FMT_NV12:
 532        case V4L2_PIX_FMT_NV16:
 533        case V4L2_PIX_FMT_NV24:
 534                y_line = ALIGN(vfmt->frame.width, 16);
 535                vfmt->offset_c = offset + y_line * vfmt->frame.height;
 536                break;
 537        case V4L2_PIX_FMT_RGB332:
 538        case V4L2_PIX_FMT_RGB444:
 539        case V4L2_PIX_FMT_RGB565:
 540        case V4L2_PIX_FMT_BGR666:
 541        case V4L2_PIX_FMT_RGB24:
 542                vfmt->offset_c = 0;
 543                break;
 544        default:
 545                BUG();
 546        }
 547}
 548
 549static int sh_veu_s_fmt(struct sh_veu_file *veu_file, struct v4l2_format *f)
 550{
 551        struct v4l2_pix_format *pix = &f->fmt.pix;
 552        struct sh_veu_dev *veu = veu_file->veu_dev;
 553        struct sh_veu_vfmt *vfmt;
 554        struct vb2_queue *vq;
 555        int ret = sh_veu_context_init(veu);
 556        if (ret < 0)
 557                return ret;
 558
 559        vq = v4l2_m2m_get_vq(veu->m2m_ctx, f->type);
 560        if (!vq)
 561                return -EINVAL;
 562
 563        if (vb2_is_busy(vq)) {
 564                v4l2_err(&veu_file->veu_dev->v4l2_dev, "%s queue busy\n", __func__);
 565                return -EBUSY;
 566        }
 567
 568        vfmt = sh_veu_get_vfmt(veu, f->type);
 569        /* called after try_fmt(), hence vfmt != NULL. Implicit BUG_ON() below */
 570
 571        vfmt->fmt               = sh_veu_find_fmt(f);
 572        /* vfmt->fmt != NULL following the same argument as above */
 573        vfmt->frame.width       = pix->width;
 574        vfmt->frame.height      = pix->height;
 575        vfmt->bytesperline      = pix->bytesperline;
 576
 577        sh_veu_colour_offset(veu, vfmt);
 578
 579        /*
 580         * We could also verify and require configuration only if any parameters
 581         * actually have changed, but it is unlikely, that the user requests the
 582         * same configuration several times without closing the device.
 583         */
 584        veu_file->cfg_needed = true;
 585
 586        dev_dbg(veu->dev,
 587                "Setting format for type %d, wxh: %dx%d, fmt: %x\n",
 588                f->type, pix->width, pix->height, vfmt->fmt->fourcc);
 589
 590        return 0;
 591}
 592
 593static int sh_veu_s_fmt_vid_cap(struct file *file, void *priv,
 594                                struct v4l2_format *f)
 595{
 596        int ret = sh_veu_try_fmt_vid_cap(file, priv, f);
 597        if (ret)
 598                return ret;
 599
 600        return sh_veu_s_fmt(priv, f);
 601}
 602
 603static int sh_veu_s_fmt_vid_out(struct file *file, void *priv,
 604                                struct v4l2_format *f)
 605{
 606        int ret = sh_veu_try_fmt_vid_out(file, priv, f);
 607        if (ret)
 608                return ret;
 609
 610        return sh_veu_s_fmt(priv, f);
 611}
 612
 613static int sh_veu_reqbufs(struct file *file, void *priv,
 614                          struct v4l2_requestbuffers *reqbufs)
 615{
 616        struct sh_veu_file *veu_file = priv;
 617        struct sh_veu_dev *veu = veu_file->veu_dev;
 618        int ret = sh_veu_context_init(veu);
 619        if (ret < 0)
 620                return ret;
 621
 622        ret = sh_veu_stream_init(veu, veu_file, reqbufs->type);
 623        if (ret < 0)
 624                return ret;
 625
 626        return v4l2_m2m_reqbufs(file, veu->m2m_ctx, reqbufs);
 627}
 628
 629static int sh_veu_querybuf(struct file *file, void *priv,
 630                           struct v4l2_buffer *buf)
 631{
 632        struct sh_veu_file *veu_file = priv;
 633
 634        if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
 635                return -EBUSY;
 636
 637        return v4l2_m2m_querybuf(file, veu_file->veu_dev->m2m_ctx, buf);
 638}
 639
 640static int sh_veu_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 641{
 642        struct sh_veu_file *veu_file = priv;
 643
 644        dev_dbg(veu_file->veu_dev->dev, "%s(%d)\n", __func__, buf->type);
 645        if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
 646                return -EBUSY;
 647
 648        return v4l2_m2m_qbuf(file, veu_file->veu_dev->m2m_ctx, buf);
 649}
 650
 651static int sh_veu_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 652{
 653        struct sh_veu_file *veu_file = priv;
 654
 655        dev_dbg(veu_file->veu_dev->dev, "%s(%d)\n", __func__, buf->type);
 656        if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
 657                return -EBUSY;
 658
 659        return v4l2_m2m_dqbuf(file, veu_file->veu_dev->m2m_ctx, buf);
 660}
 661
 662static void sh_veu_calc_scale(struct sh_veu_dev *veu,
 663                              int size_in, int size_out, int crop_out,
 664                              u32 *mant, u32 *frac, u32 *rep)
 665{
 666        u32 fixpoint;
 667
 668        /* calculate FRAC and MANT */
 669        *rep = *mant = *frac = 0;
 670
 671        if (size_in == size_out) {
 672                if (crop_out != size_out)
 673                        *mant = 1; /* needed for cropping */
 674                return;
 675        }
 676
 677        /* VEU2H special upscale */
 678        if (veu->is_2h && size_out > size_in) {
 679                u32 fixpoint = (4096 * size_in) / size_out;
 680                *mant = fixpoint / 4096;
 681                *frac = (fixpoint - (*mant * 4096)) & ~0x07;
 682
 683                switch (*frac) {
 684                case 0x800:
 685                        *rep = 1;
 686                        break;
 687                case 0x400:
 688                        *rep = 3;
 689                        break;
 690                case 0x200:
 691                        *rep = 7;
 692                        break;
 693                }
 694                if (*rep)
 695                        return;
 696        }
 697
 698        fixpoint = (4096 * (size_in - 1)) / (size_out + 1);
 699        *mant = fixpoint / 4096;
 700        *frac = fixpoint - (*mant * 4096);
 701
 702        if (*frac & 0x07) {
 703                /*
 704                 * FIXME: do we really have to round down twice in the
 705                 * up-scaling case?
 706                 */
 707                *frac &= ~0x07;
 708                if (size_out > size_in)
 709                        *frac -= 8; /* round down if scaling up */
 710                else
 711                        *frac += 8; /* round up if scaling down */
 712        }
 713}
 714
 715static unsigned long sh_veu_scale_v(struct sh_veu_dev *veu,
 716                                    int size_in, int size_out, int crop_out)
 717{
 718        u32 mant, frac, value, rep;
 719
 720        sh_veu_calc_scale(veu, size_in, size_out, crop_out, &mant, &frac, &rep);
 721
 722        /* set scale */
 723        value = (sh_veu_reg_read(veu, VEU_RFCR) & ~0xffff0000) |
 724                (((mant << 12) | frac) << 16);
 725
 726        sh_veu_reg_write(veu, VEU_RFCR, value);
 727
 728        /* set clip */
 729        value = (sh_veu_reg_read(veu, VEU_RFSR) & ~0xffff0000) |
 730                (((rep << 12) | crop_out) << 16);
 731
 732        sh_veu_reg_write(veu, VEU_RFSR, value);
 733
 734        return ALIGN((size_in * crop_out) / size_out, 4);
 735}
 736
 737static unsigned long sh_veu_scale_h(struct sh_veu_dev *veu,
 738                                    int size_in, int size_out, int crop_out)
 739{
 740        u32 mant, frac, value, rep;
 741
 742        sh_veu_calc_scale(veu, size_in, size_out, crop_out, &mant, &frac, &rep);
 743
 744        /* set scale */
 745        value = (sh_veu_reg_read(veu, VEU_RFCR) & ~0xffff) |
 746                (mant << 12) | frac;
 747
 748        sh_veu_reg_write(veu, VEU_RFCR, value);
 749
 750        /* set clip */
 751        value = (sh_veu_reg_read(veu, VEU_RFSR) & ~0xffff) |
 752                (rep << 12) | crop_out;
 753
 754        sh_veu_reg_write(veu, VEU_RFSR, value);
 755
 756        return ALIGN((size_in * crop_out) / size_out, 4);
 757}
 758
 759static void sh_veu_configure(struct sh_veu_dev *veu)
 760{
 761        u32 src_width, src_stride, src_height;
 762        u32 dst_width, dst_stride, dst_height;
 763        u32 real_w, real_h;
 764
 765        /* reset VEU */
 766        sh_veu_reg_write(veu, VEU_BSRR, 0x100);
 767
 768        src_width = veu->vfmt_in.frame.width;
 769        src_height = veu->vfmt_in.frame.height;
 770        src_stride = ALIGN(veu->vfmt_in.frame.width, 16);
 771
 772        dst_width = real_w = veu->vfmt_out.frame.width;
 773        dst_height = real_h = veu->vfmt_out.frame.height;
 774        /* Datasheet is unclear - whether it's always number of bytes or not */
 775        dst_stride = veu->vfmt_out.bytesperline;
 776
 777        /*
 778         * So far real_w == dst_width && real_h == dst_height, but it wasn't
 779         * necessarily the case in the original vidix driver, so, it may change
 780         * here in the future too.
 781         */
 782        src_width = sh_veu_scale_h(veu, src_width, real_w, dst_width);
 783        src_height = sh_veu_scale_v(veu, src_height, real_h, dst_height);
 784
 785        sh_veu_reg_write(veu, VEU_SWR, src_stride);
 786        sh_veu_reg_write(veu, VEU_SSR, src_width | (src_height << 16));
 787        sh_veu_reg_write(veu, VEU_BSSR, 0); /* not using bundle mode */
 788
 789        sh_veu_reg_write(veu, VEU_EDWR, dst_stride);
 790        sh_veu_reg_write(veu, VEU_DACR, 0); /* unused for RGB */
 791
 792        sh_veu_reg_write(veu, VEU_SWPR, 0x67);
 793        sh_veu_reg_write(veu, VEU_TRCR, (6 << 16) | (0 << 14) | 2 | 4);
 794
 795        if (veu->is_2h) {
 796                sh_veu_reg_write(veu, VEU_MCR00, 0x0cc5);
 797                sh_veu_reg_write(veu, VEU_MCR01, 0x0950);
 798                sh_veu_reg_write(veu, VEU_MCR02, 0x0000);
 799
 800                sh_veu_reg_write(veu, VEU_MCR10, 0x397f);
 801                sh_veu_reg_write(veu, VEU_MCR11, 0x0950);
 802                sh_veu_reg_write(veu, VEU_MCR12, 0x3ccd);
 803
 804                sh_veu_reg_write(veu, VEU_MCR20, 0x0000);
 805                sh_veu_reg_write(veu, VEU_MCR21, 0x0950);
 806                sh_veu_reg_write(veu, VEU_MCR22, 0x1023);
 807
 808                sh_veu_reg_write(veu, VEU_COFFR, 0x00800010);
 809        }
 810}
 811
 812static int sh_veu_streamon(struct file *file, void *priv,
 813                           enum v4l2_buf_type type)
 814{
 815        struct sh_veu_file *veu_file = priv;
 816
 817        if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, type))
 818                return -EBUSY;
 819
 820        if (veu_file->cfg_needed) {
 821                struct sh_veu_dev *veu = veu_file->veu_dev;
 822                veu_file->cfg_needed = false;
 823                sh_veu_configure(veu_file->veu_dev);
 824                veu->xaction = 0;
 825                veu->aborting = false;
 826        }
 827
 828        return v4l2_m2m_streamon(file, veu_file->veu_dev->m2m_ctx, type);
 829}
 830
 831static int sh_veu_streamoff(struct file *file, void *priv,
 832                            enum v4l2_buf_type type)
 833{
 834        struct sh_veu_file *veu_file = priv;
 835
 836        if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, type))
 837                return -EBUSY;
 838
 839        return v4l2_m2m_streamoff(file, veu_file->veu_dev->m2m_ctx, type);
 840}
 841
 842static const struct v4l2_ioctl_ops sh_veu_ioctl_ops = {
 843        .vidioc_querycap        = sh_veu_querycap,
 844
 845        .vidioc_enum_fmt_vid_cap = sh_veu_enum_fmt_vid_cap,
 846        .vidioc_g_fmt_vid_cap   = sh_veu_g_fmt_vid_cap,
 847        .vidioc_try_fmt_vid_cap = sh_veu_try_fmt_vid_cap,
 848        .vidioc_s_fmt_vid_cap   = sh_veu_s_fmt_vid_cap,
 849
 850        .vidioc_enum_fmt_vid_out = sh_veu_enum_fmt_vid_out,
 851        .vidioc_g_fmt_vid_out   = sh_veu_g_fmt_vid_out,
 852        .vidioc_try_fmt_vid_out = sh_veu_try_fmt_vid_out,
 853        .vidioc_s_fmt_vid_out   = sh_veu_s_fmt_vid_out,
 854
 855        .vidioc_reqbufs         = sh_veu_reqbufs,
 856        .vidioc_querybuf        = sh_veu_querybuf,
 857
 858        .vidioc_qbuf            = sh_veu_qbuf,
 859        .vidioc_dqbuf           = sh_veu_dqbuf,
 860
 861        .vidioc_streamon        = sh_veu_streamon,
 862        .vidioc_streamoff       = sh_veu_streamoff,
 863};
 864
 865                /* ========== Queue operations ========== */
 866
 867static int sh_veu_queue_setup(struct vb2_queue *vq,
 868                              const struct v4l2_format *f,
 869                              unsigned int *nbuffers, unsigned int *nplanes,
 870                              unsigned int sizes[], void *alloc_ctxs[])
 871{
 872        struct sh_veu_dev *veu = vb2_get_drv_priv(vq);
 873        struct sh_veu_vfmt *vfmt;
 874        unsigned int size, count = *nbuffers;
 875
 876        if (f) {
 877                const struct v4l2_pix_format *pix = &f->fmt.pix;
 878                const struct sh_veu_format *fmt = sh_veu_find_fmt(f);
 879                struct v4l2_format ftmp = *f;
 880
 881                if (fmt->fourcc != pix->pixelformat)
 882                        return -EINVAL;
 883                sh_veu_try_fmt(&ftmp, fmt);
 884                if (ftmp.fmt.pix.width != pix->width ||
 885                    ftmp.fmt.pix.height != pix->height)
 886                        return -EINVAL;
 887                size = pix->bytesperline ? pix->bytesperline * pix->height * fmt->depth / fmt->ydepth :
 888                        pix->width * pix->height * fmt->depth / fmt->ydepth;
 889        } else {
 890                vfmt = sh_veu_get_vfmt(veu, vq->type);
 891                size = vfmt->bytesperline * vfmt->frame.height * vfmt->fmt->depth / vfmt->fmt->ydepth;
 892        }
 893
 894        if (count < 2)
 895                *nbuffers = count = 2;
 896
 897        if (size * count > VIDEO_MEM_LIMIT) {
 898                count = VIDEO_MEM_LIMIT / size;
 899                *nbuffers = count;
 900        }
 901
 902        *nplanes = 1;
 903        sizes[0] = size;
 904        alloc_ctxs[0] = veu->alloc_ctx;
 905
 906        dev_dbg(veu->dev, "get %d buffer(s) of size %d each.\n", count, size);
 907
 908        return 0;
 909}
 910
 911static int sh_veu_buf_prepare(struct vb2_buffer *vb)
 912{
 913        struct sh_veu_dev *veu = vb2_get_drv_priv(vb->vb2_queue);
 914        struct sh_veu_vfmt *vfmt;
 915        unsigned int sizeimage;
 916
 917        vfmt = sh_veu_get_vfmt(veu, vb->vb2_queue->type);
 918        sizeimage = vfmt->bytesperline * vfmt->frame.height *
 919                vfmt->fmt->depth / vfmt->fmt->ydepth;
 920
 921        if (vb2_plane_size(vb, 0) < sizeimage) {
 922                dev_dbg(veu->dev, "%s data will not fit into plane (%lu < %u)\n",
 923                        __func__, vb2_plane_size(vb, 0), sizeimage);
 924                return -EINVAL;
 925        }
 926
 927        vb2_set_plane_payload(vb, 0, sizeimage);
 928
 929        return 0;
 930}
 931
 932static void sh_veu_buf_queue(struct vb2_buffer *vb)
 933{
 934        struct sh_veu_dev *veu = vb2_get_drv_priv(vb->vb2_queue);
 935        dev_dbg(veu->dev, "%s(%d)\n", __func__, vb->v4l2_buf.type);
 936        v4l2_m2m_buf_queue(veu->m2m_ctx, vb);
 937}
 938
 939static const struct vb2_ops sh_veu_qops = {
 940        .queue_setup     = sh_veu_queue_setup,
 941        .buf_prepare     = sh_veu_buf_prepare,
 942        .buf_queue       = sh_veu_buf_queue,
 943        .wait_prepare    = vb2_ops_wait_prepare,
 944        .wait_finish     = vb2_ops_wait_finish,
 945};
 946
 947static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
 948                             struct vb2_queue *dst_vq)
 949{
 950        struct sh_veu_dev *veu = priv;
 951        int ret;
 952
 953        memset(src_vq, 0, sizeof(*src_vq));
 954        src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 955        src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
 956        src_vq->drv_priv = veu;
 957        src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
 958        src_vq->ops = &sh_veu_qops;
 959        src_vq->mem_ops = &vb2_dma_contig_memops;
 960        src_vq->lock = &veu->fop_lock;
 961
 962        ret = vb2_queue_init(src_vq);
 963        if (ret < 0)
 964                return ret;
 965
 966        memset(dst_vq, 0, sizeof(*dst_vq));
 967        dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 968        dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
 969        dst_vq->drv_priv = veu;
 970        dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
 971        dst_vq->ops = &sh_veu_qops;
 972        dst_vq->mem_ops = &vb2_dma_contig_memops;
 973        dst_vq->lock = &veu->fop_lock;
 974
 975        return vb2_queue_init(dst_vq);
 976}
 977
 978                /* ========== File operations ========== */
 979
 980static int sh_veu_open(struct file *file)
 981{
 982        struct sh_veu_dev *veu = video_drvdata(file);
 983        struct sh_veu_file *veu_file;
 984
 985        veu_file = kzalloc(sizeof(*veu_file), GFP_KERNEL);
 986        if (!veu_file)
 987                return -ENOMEM;
 988
 989        veu_file->veu_dev = veu;
 990        veu_file->cfg_needed = true;
 991
 992        file->private_data = veu_file;
 993
 994        pm_runtime_get_sync(veu->dev);
 995
 996        dev_dbg(veu->dev, "Created instance %p\n", veu_file);
 997
 998        return 0;
 999}
1000
1001static int sh_veu_release(struct file *file)
1002{
1003        struct sh_veu_dev *veu = video_drvdata(file);
1004        struct sh_veu_file *veu_file = file->private_data;
1005
1006        dev_dbg(veu->dev, "Releasing instance %p\n", veu_file);
1007
1008        if (veu_file == veu->capture) {
1009                veu->capture = NULL;
1010                vb2_queue_release(v4l2_m2m_get_vq(veu->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE));
1011        }
1012
1013        if (veu_file == veu->output) {
1014                veu->output = NULL;
1015                vb2_queue_release(v4l2_m2m_get_vq(veu->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT));
1016        }
1017
1018        if (!veu->output && !veu->capture && veu->m2m_ctx) {
1019                v4l2_m2m_ctx_release(veu->m2m_ctx);
1020                veu->m2m_ctx = NULL;
1021        }
1022
1023        pm_runtime_put(veu->dev);
1024
1025        kfree(veu_file);
1026
1027        return 0;
1028}
1029
1030static unsigned int sh_veu_poll(struct file *file,
1031                                struct poll_table_struct *wait)
1032{
1033        struct sh_veu_file *veu_file = file->private_data;
1034
1035        return v4l2_m2m_poll(file, veu_file->veu_dev->m2m_ctx, wait);
1036}
1037
1038static int sh_veu_mmap(struct file *file, struct vm_area_struct *vma)
1039{
1040        struct sh_veu_file *veu_file = file->private_data;
1041
1042        return v4l2_m2m_mmap(file, veu_file->veu_dev->m2m_ctx, vma);
1043}
1044
1045static const struct v4l2_file_operations sh_veu_fops = {
1046        .owner          = THIS_MODULE,
1047        .open           = sh_veu_open,
1048        .release        = sh_veu_release,
1049        .poll           = sh_veu_poll,
1050        .unlocked_ioctl = video_ioctl2,
1051        .mmap           = sh_veu_mmap,
1052};
1053
1054static const struct video_device sh_veu_videodev = {
1055        .name           = "sh-veu",
1056        .fops           = &sh_veu_fops,
1057        .ioctl_ops      = &sh_veu_ioctl_ops,
1058        .minor          = -1,
1059        .release        = video_device_release_empty,
1060        .vfl_dir        = VFL_DIR_M2M,
1061};
1062
1063static const struct v4l2_m2m_ops sh_veu_m2m_ops = {
1064        .device_run     = sh_veu_device_run,
1065        .job_abort      = sh_veu_job_abort,
1066};
1067
1068static irqreturn_t sh_veu_bh(int irq, void *dev_id)
1069{
1070        struct sh_veu_dev *veu = dev_id;
1071
1072        if (veu->xaction == MEM2MEM_DEF_TRANSLEN || veu->aborting) {
1073                v4l2_m2m_job_finish(veu->m2m_dev, veu->m2m_ctx);
1074                veu->xaction = 0;
1075        } else {
1076                sh_veu_device_run(veu);
1077        }
1078
1079        return IRQ_HANDLED;
1080}
1081
1082static irqreturn_t sh_veu_isr(int irq, void *dev_id)
1083{
1084        struct sh_veu_dev *veu = dev_id;
1085        struct vb2_buffer *dst;
1086        struct vb2_buffer *src;
1087        u32 status = sh_veu_reg_read(veu, VEU_EVTR);
1088
1089        /* bundle read mode not used */
1090        if (!(status & 1))
1091                return IRQ_NONE;
1092
1093        /* disable interrupt in VEU */
1094        sh_veu_reg_write(veu, VEU_EIER, 0);
1095        /* halt operation */
1096        sh_veu_reg_write(veu, VEU_STR, 0);
1097        /* ack int, write 0 to clear bits */
1098        sh_veu_reg_write(veu, VEU_EVTR, status & ~1);
1099
1100        /* conversion completed */
1101        dst = v4l2_m2m_dst_buf_remove(veu->m2m_ctx);
1102        src = v4l2_m2m_src_buf_remove(veu->m2m_ctx);
1103        if (!src || !dst)
1104                return IRQ_NONE;
1105
1106        spin_lock(&veu->lock);
1107        v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
1108        v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
1109        spin_unlock(&veu->lock);
1110
1111        veu->xaction++;
1112
1113        return IRQ_WAKE_THREAD;
1114}
1115
1116static int sh_veu_probe(struct platform_device *pdev)
1117{
1118        struct sh_veu_dev *veu;
1119        struct resource *reg_res;
1120        struct video_device *vdev;
1121        int irq, ret;
1122
1123        reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1124        irq = platform_get_irq(pdev, 0);
1125
1126        if (!reg_res || irq <= 0) {
1127                dev_err(&pdev->dev, "Insufficient VEU platform information.\n");
1128                return -ENODEV;
1129        }
1130
1131        veu = devm_kzalloc(&pdev->dev, sizeof(*veu), GFP_KERNEL);
1132        if (!veu)
1133                return -ENOMEM;
1134
1135        veu->is_2h = resource_size(reg_res) == 0x22c;
1136
1137        veu->base = devm_ioremap_resource(&pdev->dev, reg_res);
1138        if (IS_ERR(veu->base))
1139                return PTR_ERR(veu->base);
1140
1141        ret = devm_request_threaded_irq(&pdev->dev, irq, sh_veu_isr, sh_veu_bh,
1142                                        0, "veu", veu);
1143        if (ret < 0)
1144                return ret;
1145
1146        ret = v4l2_device_register(&pdev->dev, &veu->v4l2_dev);
1147        if (ret < 0) {
1148                dev_err(&pdev->dev, "Error registering v4l2 device\n");
1149                return ret;
1150        }
1151
1152        vdev = &veu->vdev;
1153
1154        veu->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1155        if (IS_ERR(veu->alloc_ctx)) {
1156                ret = PTR_ERR(veu->alloc_ctx);
1157                goto einitctx;
1158        }
1159
1160        *vdev = sh_veu_videodev;
1161        vdev->v4l2_dev = &veu->v4l2_dev;
1162        spin_lock_init(&veu->lock);
1163        mutex_init(&veu->fop_lock);
1164        vdev->lock = &veu->fop_lock;
1165
1166        video_set_drvdata(vdev, veu);
1167
1168        veu->dev        = &pdev->dev;
1169        veu->vfmt_out   = DEFAULT_OUT_VFMT;
1170        veu->vfmt_in    = DEFAULT_IN_VFMT;
1171
1172        veu->m2m_dev = v4l2_m2m_init(&sh_veu_m2m_ops);
1173        if (IS_ERR(veu->m2m_dev)) {
1174                ret = PTR_ERR(veu->m2m_dev);
1175                v4l2_err(&veu->v4l2_dev, "Failed to init mem2mem device: %d\n", ret);
1176                goto em2minit;
1177        }
1178
1179        pm_runtime_enable(&pdev->dev);
1180        pm_runtime_resume(&pdev->dev);
1181
1182        ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1183        pm_runtime_suspend(&pdev->dev);
1184        if (ret < 0)
1185                goto evidreg;
1186
1187        return ret;
1188
1189evidreg:
1190        pm_runtime_disable(&pdev->dev);
1191        v4l2_m2m_release(veu->m2m_dev);
1192em2minit:
1193        vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
1194einitctx:
1195        v4l2_device_unregister(&veu->v4l2_dev);
1196        return ret;
1197}
1198
1199static int sh_veu_remove(struct platform_device *pdev)
1200{
1201        struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1202        struct sh_veu_dev *veu = container_of(v4l2_dev,
1203                                              struct sh_veu_dev, v4l2_dev);
1204
1205        video_unregister_device(&veu->vdev);
1206        pm_runtime_disable(&pdev->dev);
1207        v4l2_m2m_release(veu->m2m_dev);
1208        vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
1209        v4l2_device_unregister(&veu->v4l2_dev);
1210
1211        return 0;
1212}
1213
1214static struct platform_driver __refdata sh_veu_pdrv = {
1215        .remove         = sh_veu_remove,
1216        .driver         = {
1217                .name   = "sh_veu",
1218        },
1219};
1220
1221module_platform_driver_probe(sh_veu_pdrv, sh_veu_probe);
1222
1223MODULE_DESCRIPTION("sh-mobile VEU mem2mem driver");
1224MODULE_AUTHOR("Guennadi Liakhovetski, <g.liakhovetski@gmx.de>");
1225MODULE_LICENSE("GPL v2");
1226