Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us