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