root/drivers/media/platform/sh_veu.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sh_veu_4cc2cspace
  2. sh_veu_reg_read
  3. sh_veu_reg_write
  4. sh_veu_job_abort
  5. sh_veu_process
  6. sh_veu_device_run
  7. sh_veu_is_streamer
  8. sh_veu_stream_init
  9. sh_veu_context_init
  10. sh_veu_querycap
  11. sh_veu_enum_fmt
  12. sh_veu_enum_fmt_vid_cap
  13. sh_veu_enum_fmt_vid_out
  14. sh_veu_get_vfmt
  15. sh_veu_g_fmt
  16. sh_veu_g_fmt_vid_out
  17. sh_veu_g_fmt_vid_cap
  18. sh_veu_try_fmt
  19. sh_veu_find_fmt
  20. sh_veu_try_fmt_vid_cap
  21. sh_veu_try_fmt_vid_out
  22. sh_veu_colour_offset
  23. sh_veu_s_fmt
  24. sh_veu_s_fmt_vid_cap
  25. sh_veu_s_fmt_vid_out
  26. sh_veu_reqbufs
  27. sh_veu_querybuf
  28. sh_veu_qbuf
  29. sh_veu_dqbuf
  30. sh_veu_calc_scale
  31. sh_veu_scale_v
  32. sh_veu_scale_h
  33. sh_veu_configure
  34. sh_veu_streamon
  35. sh_veu_streamoff
  36. sh_veu_queue_setup
  37. sh_veu_buf_prepare
  38. sh_veu_buf_queue
  39. sh_veu_queue_init
  40. sh_veu_open
  41. sh_veu_release
  42. sh_veu_poll
  43. sh_veu_mmap
  44. sh_veu_bh
  45. sh_veu_isr
  46. sh_veu_probe
  47. sh_veu_remove

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

/* [<][>][^][v][top][bottom][index][help] */