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