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