1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/clk.h>
15#include <linux/debugfs.h>
16#include <linux/delay.h>
17#include <linux/firmware.h>
18#include <linux/gcd.h>
19#include <linux/genalloc.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/irq.h>
23#include <linux/kfifo.h>
24#include <linux/module.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/slab.h>
29#include <linux/videodev2.h>
30#include <linux/of.h>
31#include <linux/platform_data/media/coda.h>
32#include <linux/reset.h>
33
34#include <media/v4l2-ctrls.h>
35#include <media/v4l2-device.h>
36#include <media/v4l2-event.h>
37#include <media/v4l2-ioctl.h>
38#include <media/v4l2-mem2mem.h>
39#include <media/videobuf2-v4l2.h>
40#include <media/videobuf2-dma-contig.h>
41#include <media/videobuf2-vmalloc.h>
42
43#include "coda.h"
44#include "imx-vdoa.h"
45
46#define CODA_NAME "coda"
47
48#define CODADX6_MAX_INSTANCES 4
49#define CODA_MAX_FORMATS 4
50
51#define CODA_ISRAM_SIZE (2048 * 2)
52
53#define MIN_W 176
54#define MIN_H 144
55
56#define S_ALIGN 1
57#define W_ALIGN 1
58#define H_ALIGN 1
59
60#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
61
62int coda_debug;
63module_param(coda_debug, int, 0644);
64MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
65
66static int disable_tiling;
67module_param(disable_tiling, int, 0644);
68MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
69
70static int disable_vdoa;
71module_param(disable_vdoa, int, 0644);
72MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
73
74static int enable_bwb = 0;
75module_param(enable_bwb, int, 0644);
76MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
77
78void coda_write(struct coda_dev *dev, u32 data, u32 reg)
79{
80 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
81 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
82 writel(data, dev->regs_base + reg);
83}
84
85unsigned int coda_read(struct coda_dev *dev, u32 reg)
86{
87 u32 data;
88
89 data = readl(dev->regs_base + reg);
90 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
91 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
92 return data;
93}
94
95void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
96 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
97{
98 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
99 u32 base_cb, base_cr;
100
101 switch (q_data->fourcc) {
102 case V4L2_PIX_FMT_YUYV:
103
104 case V4L2_PIX_FMT_NV12:
105 case V4L2_PIX_FMT_YUV420:
106 default:
107 base_cb = base_y + q_data->bytesperline * q_data->height;
108 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
109 break;
110 case V4L2_PIX_FMT_YVU420:
111
112 base_cr = base_y + q_data->bytesperline * q_data->height;
113 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
114 break;
115 case V4L2_PIX_FMT_YUV422P:
116 base_cb = base_y + q_data->bytesperline * q_data->height;
117 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
118 }
119
120 coda_write(ctx->dev, base_y, reg_y);
121 coda_write(ctx->dev, base_cb, reg_y + 4);
122 coda_write(ctx->dev, base_cr, reg_y + 8);
123}
124
125#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
126 { mode, src_fourcc, dst_fourcc, max_w, max_h }
127
128
129
130
131
132
133
134
135
136static const struct coda_codec codadx6_codecs[] = {
137 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
138 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
139};
140
141static const struct coda_codec codahx4_codecs[] = {
142 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
143 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
144 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
145 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
146};
147
148static const struct coda_codec coda7_codecs[] = {
149 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
150 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
151 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
152 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
153 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
154 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
155 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
156};
157
158static const struct coda_codec coda9_codecs[] = {
159 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
160 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
163 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
164};
165
166struct coda_video_device {
167 const char *name;
168 enum coda_inst_type type;
169 const struct coda_context_ops *ops;
170 bool direct;
171 u32 src_formats[CODA_MAX_FORMATS];
172 u32 dst_formats[CODA_MAX_FORMATS];
173};
174
175static const struct coda_video_device coda_bit_encoder = {
176 .name = "coda-encoder",
177 .type = CODA_INST_ENCODER,
178 .ops = &coda_bit_encode_ops,
179 .src_formats = {
180 V4L2_PIX_FMT_NV12,
181 V4L2_PIX_FMT_YUV420,
182 V4L2_PIX_FMT_YVU420,
183 },
184 .dst_formats = {
185 V4L2_PIX_FMT_H264,
186 V4L2_PIX_FMT_MPEG4,
187 },
188};
189
190static const struct coda_video_device coda_bit_jpeg_encoder = {
191 .name = "coda-jpeg-encoder",
192 .type = CODA_INST_ENCODER,
193 .ops = &coda_bit_encode_ops,
194 .src_formats = {
195 V4L2_PIX_FMT_NV12,
196 V4L2_PIX_FMT_YUV420,
197 V4L2_PIX_FMT_YVU420,
198 V4L2_PIX_FMT_YUV422P,
199 },
200 .dst_formats = {
201 V4L2_PIX_FMT_JPEG,
202 },
203};
204
205static const struct coda_video_device coda_bit_decoder = {
206 .name = "coda-decoder",
207 .type = CODA_INST_DECODER,
208 .ops = &coda_bit_decode_ops,
209 .src_formats = {
210 V4L2_PIX_FMT_H264,
211 V4L2_PIX_FMT_MPEG2,
212 V4L2_PIX_FMT_MPEG4,
213 },
214 .dst_formats = {
215 V4L2_PIX_FMT_NV12,
216 V4L2_PIX_FMT_YUV420,
217 V4L2_PIX_FMT_YVU420,
218
219
220
221
222 V4L2_PIX_FMT_YUYV,
223 },
224};
225
226static const struct coda_video_device coda_bit_jpeg_decoder = {
227 .name = "coda-jpeg-decoder",
228 .type = CODA_INST_DECODER,
229 .ops = &coda_bit_decode_ops,
230 .src_formats = {
231 V4L2_PIX_FMT_JPEG,
232 },
233 .dst_formats = {
234 V4L2_PIX_FMT_NV12,
235 V4L2_PIX_FMT_YUV420,
236 V4L2_PIX_FMT_YVU420,
237 V4L2_PIX_FMT_YUV422P,
238 },
239};
240
241static const struct coda_video_device *codadx6_video_devices[] = {
242 &coda_bit_encoder,
243};
244
245static const struct coda_video_device *codahx4_video_devices[] = {
246 &coda_bit_encoder,
247 &coda_bit_decoder,
248};
249
250static const struct coda_video_device *coda7_video_devices[] = {
251 &coda_bit_jpeg_encoder,
252 &coda_bit_jpeg_decoder,
253 &coda_bit_encoder,
254 &coda_bit_decoder,
255};
256
257static const struct coda_video_device *coda9_video_devices[] = {
258 &coda_bit_encoder,
259 &coda_bit_decoder,
260};
261
262
263
264
265
266static u32 coda_format_normalize_yuv(u32 fourcc)
267{
268 switch (fourcc) {
269 case V4L2_PIX_FMT_NV12:
270 case V4L2_PIX_FMT_YUV420:
271 case V4L2_PIX_FMT_YVU420:
272 case V4L2_PIX_FMT_YUV422P:
273 case V4L2_PIX_FMT_YUYV:
274 return V4L2_PIX_FMT_YUV420;
275 default:
276 return fourcc;
277 }
278}
279
280static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
281 int src_fourcc, int dst_fourcc)
282{
283 const struct coda_codec *codecs = dev->devtype->codecs;
284 int num_codecs = dev->devtype->num_codecs;
285 int k;
286
287 src_fourcc = coda_format_normalize_yuv(src_fourcc);
288 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
289 if (src_fourcc == dst_fourcc)
290 return NULL;
291
292 for (k = 0; k < num_codecs; k++) {
293 if (codecs[k].src_fourcc == src_fourcc &&
294 codecs[k].dst_fourcc == dst_fourcc)
295 break;
296 }
297
298 if (k == num_codecs)
299 return NULL;
300
301 return &codecs[k];
302}
303
304static void coda_get_max_dimensions(struct coda_dev *dev,
305 const struct coda_codec *codec,
306 int *max_w, int *max_h)
307{
308 const struct coda_codec *codecs = dev->devtype->codecs;
309 int num_codecs = dev->devtype->num_codecs;
310 unsigned int w, h;
311 int k;
312
313 if (codec) {
314 w = codec->max_w;
315 h = codec->max_h;
316 } else {
317 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
318 w = max(w, codecs[k].max_w);
319 h = max(h, codecs[k].max_h);
320 }
321 }
322
323 if (max_w)
324 *max_w = w;
325 if (max_h)
326 *max_h = h;
327}
328
329static const struct coda_video_device *to_coda_video_device(struct video_device
330 *vdev)
331{
332 struct coda_dev *dev = video_get_drvdata(vdev);
333 unsigned int i = vdev - dev->vfd;
334
335 if (i >= dev->devtype->num_vdevs)
336 return NULL;
337
338 return dev->devtype->vdevs[i];
339}
340
341const char *coda_product_name(int product)
342{
343 static char buf[9];
344
345 switch (product) {
346 case CODA_DX6:
347 return "CodaDx6";
348 case CODA_HX4:
349 return "CodaHx4";
350 case CODA_7541:
351 return "CODA7541";
352 case CODA_960:
353 return "CODA960";
354 default:
355 snprintf(buf, sizeof(buf), "(0x%04x)", product);
356 return buf;
357 }
358}
359
360static struct vdoa_data *coda_get_vdoa_data(void)
361{
362 struct device_node *vdoa_node;
363 struct platform_device *vdoa_pdev;
364 struct vdoa_data *vdoa_data = NULL;
365
366 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
367 if (!vdoa_node)
368 return NULL;
369
370 vdoa_pdev = of_find_device_by_node(vdoa_node);
371 if (!vdoa_pdev)
372 goto out;
373
374 vdoa_data = platform_get_drvdata(vdoa_pdev);
375 if (!vdoa_data)
376 vdoa_data = ERR_PTR(-EPROBE_DEFER);
377
378out:
379 if (vdoa_node)
380 of_node_put(vdoa_node);
381
382 return vdoa_data;
383}
384
385
386
387
388static int coda_querycap(struct file *file, void *priv,
389 struct v4l2_capability *cap)
390{
391 struct coda_ctx *ctx = fh_to_ctx(priv);
392
393 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
394 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
395 sizeof(cap->card));
396 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
397 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
398 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
399
400 return 0;
401}
402
403static int coda_enum_fmt(struct file *file, void *priv,
404 struct v4l2_fmtdesc *f)
405{
406 struct video_device *vdev = video_devdata(file);
407 const struct coda_video_device *cvd = to_coda_video_device(vdev);
408 struct coda_ctx *ctx = fh_to_ctx(priv);
409 const u32 *formats;
410
411 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
412 formats = cvd->src_formats;
413 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
414 formats = cvd->dst_formats;
415 else
416 return -EINVAL;
417
418 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
419 return -EINVAL;
420
421
422 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
423 formats[f->index] == V4L2_PIX_FMT_YUYV)
424 return -EINVAL;
425
426 f->pixelformat = formats[f->index];
427
428 return 0;
429}
430
431static int coda_g_fmt(struct file *file, void *priv,
432 struct v4l2_format *f)
433{
434 struct coda_q_data *q_data;
435 struct coda_ctx *ctx = fh_to_ctx(priv);
436
437 q_data = get_q_data(ctx, f->type);
438 if (!q_data)
439 return -EINVAL;
440
441 f->fmt.pix.field = V4L2_FIELD_NONE;
442 f->fmt.pix.pixelformat = q_data->fourcc;
443 f->fmt.pix.width = q_data->width;
444 f->fmt.pix.height = q_data->height;
445 f->fmt.pix.bytesperline = q_data->bytesperline;
446
447 f->fmt.pix.sizeimage = q_data->sizeimage;
448 f->fmt.pix.colorspace = ctx->colorspace;
449 f->fmt.pix.xfer_func = ctx->xfer_func;
450 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
451 f->fmt.pix.quantization = ctx->quantization;
452
453 return 0;
454}
455
456static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
457{
458 struct coda_q_data *q_data;
459 const u32 *formats;
460 int i;
461
462 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
463 formats = ctx->cvd->src_formats;
464 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
465 formats = ctx->cvd->dst_formats;
466 else
467 return -EINVAL;
468
469 for (i = 0; i < CODA_MAX_FORMATS; i++) {
470
471 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
472 formats[i] == V4L2_PIX_FMT_YUYV)
473 continue;
474
475 if (formats[i] == f->fmt.pix.pixelformat) {
476 f->fmt.pix.pixelformat = formats[i];
477 return 0;
478 }
479 }
480
481
482 q_data = get_q_data(ctx, f->type);
483 f->fmt.pix.pixelformat = q_data->fourcc;
484
485 return 0;
486}
487
488static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
489 bool *use_vdoa)
490{
491 int err;
492
493 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
494 return -EINVAL;
495
496 if (!use_vdoa)
497 return -EINVAL;
498
499 if (!ctx->vdoa) {
500 *use_vdoa = false;
501 return 0;
502 }
503
504 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
505 f->fmt.pix.height, f->fmt.pix.pixelformat);
506 if (err) {
507 *use_vdoa = false;
508 return 0;
509 }
510
511 *use_vdoa = true;
512 return 0;
513}
514
515static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
516 u32 width, u32 height)
517{
518
519
520
521
522
523 return round_up(clamp(sizeimage, width * height / 8,
524 width * height * 2), PAGE_SIZE);
525}
526
527static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
528 struct v4l2_format *f)
529{
530 struct coda_dev *dev = ctx->dev;
531 unsigned int max_w, max_h;
532 enum v4l2_field field;
533
534 field = f->fmt.pix.field;
535 if (field == V4L2_FIELD_ANY)
536 field = V4L2_FIELD_NONE;
537 else if (V4L2_FIELD_NONE != field)
538 return -EINVAL;
539
540
541
542 f->fmt.pix.field = field;
543
544 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
545 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
546 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
547 S_ALIGN);
548
549 switch (f->fmt.pix.pixelformat) {
550 case V4L2_PIX_FMT_NV12:
551 case V4L2_PIX_FMT_YUV420:
552 case V4L2_PIX_FMT_YVU420:
553
554
555
556
557 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
558 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
559 f->fmt.pix.height * 3 / 2;
560 break;
561 case V4L2_PIX_FMT_YUYV:
562 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
563 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
564 f->fmt.pix.height;
565 break;
566 case V4L2_PIX_FMT_YUV422P:
567 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
568 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
569 f->fmt.pix.height * 2;
570 break;
571 case V4L2_PIX_FMT_JPEG:
572 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
573
574 case V4L2_PIX_FMT_H264:
575 case V4L2_PIX_FMT_MPEG4:
576 case V4L2_PIX_FMT_MPEG2:
577 f->fmt.pix.bytesperline = 0;
578 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
579 f->fmt.pix.sizeimage,
580 f->fmt.pix.width,
581 f->fmt.pix.height);
582 break;
583 default:
584 BUG();
585 }
586
587 return 0;
588}
589
590static int coda_try_fmt_vid_cap(struct file *file, void *priv,
591 struct v4l2_format *f)
592{
593 struct coda_ctx *ctx = fh_to_ctx(priv);
594 const struct coda_q_data *q_data_src;
595 const struct coda_codec *codec;
596 struct vb2_queue *src_vq;
597 int ret;
598 bool use_vdoa;
599
600 ret = coda_try_pixelformat(ctx, f);
601 if (ret < 0)
602 return ret;
603
604 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
605
606
607
608
609
610 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
611 if (vb2_is_streaming(src_vq)) {
612 f->fmt.pix.width = q_data_src->width;
613 f->fmt.pix.height = q_data_src->height;
614 }
615
616 f->fmt.pix.colorspace = ctx->colorspace;
617 f->fmt.pix.xfer_func = ctx->xfer_func;
618 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
619 f->fmt.pix.quantization = ctx->quantization;
620
621 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
622 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
623 f->fmt.pix.pixelformat);
624 if (!codec)
625 return -EINVAL;
626
627 ret = coda_try_fmt(ctx, codec, f);
628 if (ret < 0)
629 return ret;
630
631
632 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
633 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
634 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
635 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
636 f->fmt.pix.height * 3 / 2;
637
638 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
639 if (ret < 0)
640 return ret;
641
642 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
643 if (!use_vdoa)
644 return -EINVAL;
645
646 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
647 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
648 f->fmt.pix.height;
649 }
650 }
651
652 return 0;
653}
654
655static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
656{
657 enum v4l2_colorspace colorspace;
658
659 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
660 colorspace = V4L2_COLORSPACE_JPEG;
661 else if (fmt->width <= 720 && fmt->height <= 576)
662 colorspace = V4L2_COLORSPACE_SMPTE170M;
663 else
664 colorspace = V4L2_COLORSPACE_REC709;
665
666 fmt->colorspace = colorspace;
667 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
668 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
669 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
670}
671
672static int coda_try_fmt_vid_out(struct file *file, void *priv,
673 struct v4l2_format *f)
674{
675 struct coda_ctx *ctx = fh_to_ctx(priv);
676 struct coda_dev *dev = ctx->dev;
677 const struct coda_q_data *q_data_dst;
678 const struct coda_codec *codec;
679 int ret;
680
681 ret = coda_try_pixelformat(ctx, f);
682 if (ret < 0)
683 return ret;
684
685 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
686 coda_set_default_colorspace(&f->fmt.pix);
687
688 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
689 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
690
691 return coda_try_fmt(ctx, codec, f);
692}
693
694static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
695 struct v4l2_rect *r)
696{
697 struct coda_q_data *q_data;
698 struct vb2_queue *vq;
699
700 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
701 if (!vq)
702 return -EINVAL;
703
704 q_data = get_q_data(ctx, f->type);
705 if (!q_data)
706 return -EINVAL;
707
708 if (vb2_is_busy(vq)) {
709 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
710 return -EBUSY;
711 }
712
713 q_data->fourcc = f->fmt.pix.pixelformat;
714 q_data->width = f->fmt.pix.width;
715 q_data->height = f->fmt.pix.height;
716 q_data->bytesperline = f->fmt.pix.bytesperline;
717 q_data->sizeimage = f->fmt.pix.sizeimage;
718 if (r) {
719 q_data->rect = *r;
720 } else {
721 q_data->rect.left = 0;
722 q_data->rect.top = 0;
723 q_data->rect.width = f->fmt.pix.width;
724 q_data->rect.height = f->fmt.pix.height;
725 }
726
727 switch (f->fmt.pix.pixelformat) {
728 case V4L2_PIX_FMT_YUYV:
729 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
730 break;
731 case V4L2_PIX_FMT_NV12:
732 if (!disable_tiling) {
733 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
734 break;
735 }
736
737 case V4L2_PIX_FMT_YUV420:
738 case V4L2_PIX_FMT_YVU420:
739 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
740 break;
741 default:
742 break;
743 }
744
745 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
746 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
747 ctx->use_vdoa)
748 vdoa_context_configure(ctx->vdoa,
749 round_up(f->fmt.pix.width, 16),
750 f->fmt.pix.height,
751 f->fmt.pix.pixelformat);
752 else
753 ctx->use_vdoa = false;
754
755 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
756 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
757 f->type, q_data->width, q_data->height,
758 (char *)&q_data->fourcc,
759 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
760
761 return 0;
762}
763
764static int coda_s_fmt_vid_cap(struct file *file, void *priv,
765 struct v4l2_format *f)
766{
767 struct coda_ctx *ctx = fh_to_ctx(priv);
768 struct coda_q_data *q_data_src;
769 struct v4l2_rect r;
770 int ret;
771
772 ret = coda_try_fmt_vid_cap(file, priv, f);
773 if (ret)
774 return ret;
775
776 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
777 r.left = 0;
778 r.top = 0;
779 r.width = q_data_src->width;
780 r.height = q_data_src->height;
781
782 ret = coda_s_fmt(ctx, f, &r);
783 if (ret)
784 return ret;
785
786 if (ctx->inst_type != CODA_INST_ENCODER)
787 return 0;
788
789 ctx->colorspace = f->fmt.pix.colorspace;
790 ctx->xfer_func = f->fmt.pix.xfer_func;
791 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
792 ctx->quantization = f->fmt.pix.quantization;
793
794 return 0;
795}
796
797static int coda_s_fmt_vid_out(struct file *file, void *priv,
798 struct v4l2_format *f)
799{
800 struct coda_ctx *ctx = fh_to_ctx(priv);
801 struct v4l2_format f_cap;
802 struct vb2_queue *dst_vq;
803 int ret;
804
805 ret = coda_try_fmt_vid_out(file, priv, f);
806 if (ret)
807 return ret;
808
809 ret = coda_s_fmt(ctx, f, NULL);
810 if (ret)
811 return ret;
812
813 if (ctx->inst_type != CODA_INST_DECODER)
814 return 0;
815
816 ctx->colorspace = f->fmt.pix.colorspace;
817 ctx->xfer_func = f->fmt.pix.xfer_func;
818 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
819 ctx->quantization = f->fmt.pix.quantization;
820
821 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
822 if (!dst_vq)
823 return -EINVAL;
824
825
826
827
828
829
830
831 if (vb2_is_busy(dst_vq))
832 return 0;
833
834 memset(&f_cap, 0, sizeof(f_cap));
835 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
836 coda_g_fmt(file, priv, &f_cap);
837 f_cap.fmt.pix.width = f->fmt.pix.width;
838 f_cap.fmt.pix.height = f->fmt.pix.height;
839
840 return coda_s_fmt_vid_cap(file, priv, &f_cap);
841}
842
843static int coda_reqbufs(struct file *file, void *priv,
844 struct v4l2_requestbuffers *rb)
845{
846 struct coda_ctx *ctx = fh_to_ctx(priv);
847 int ret;
848
849 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
850 if (ret)
851 return ret;
852
853
854
855
856
857 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
858 return ctx->ops->reqbufs(ctx, rb);
859
860 return 0;
861}
862
863static int coda_qbuf(struct file *file, void *priv,
864 struct v4l2_buffer *buf)
865{
866 struct coda_ctx *ctx = fh_to_ctx(priv);
867
868 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
869}
870
871static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
872 struct vb2_v4l2_buffer *buf)
873{
874 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
875 (buf->sequence == (ctx->qsequence - 1)));
876}
877
878void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
879 enum vb2_buffer_state state)
880{
881 const struct v4l2_event eos_event = {
882 .type = V4L2_EVENT_EOS
883 };
884
885 if (coda_buf_is_end_of_stream(ctx, buf)) {
886 buf->flags |= V4L2_BUF_FLAG_LAST;
887
888 v4l2_event_queue_fh(&ctx->fh, &eos_event);
889 }
890
891 v4l2_m2m_buf_done(buf, state);
892}
893
894static int coda_g_selection(struct file *file, void *fh,
895 struct v4l2_selection *s)
896{
897 struct coda_ctx *ctx = fh_to_ctx(fh);
898 struct coda_q_data *q_data;
899 struct v4l2_rect r, *rsel;
900
901 q_data = get_q_data(ctx, s->type);
902 if (!q_data)
903 return -EINVAL;
904
905 r.left = 0;
906 r.top = 0;
907 r.width = q_data->width;
908 r.height = q_data->height;
909 rsel = &q_data->rect;
910
911 switch (s->target) {
912 case V4L2_SEL_TGT_CROP_DEFAULT:
913 case V4L2_SEL_TGT_CROP_BOUNDS:
914 rsel = &r;
915
916 case V4L2_SEL_TGT_CROP:
917 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
918 return -EINVAL;
919 break;
920 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
921 case V4L2_SEL_TGT_COMPOSE_PADDED:
922 rsel = &r;
923
924 case V4L2_SEL_TGT_COMPOSE:
925 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
926 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
927 return -EINVAL;
928 break;
929 default:
930 return -EINVAL;
931 }
932
933 s->r = *rsel;
934
935 return 0;
936}
937
938static int coda_try_encoder_cmd(struct file *file, void *fh,
939 struct v4l2_encoder_cmd *ec)
940{
941 if (ec->cmd != V4L2_ENC_CMD_STOP)
942 return -EINVAL;
943
944 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
945 return -EINVAL;
946
947 return 0;
948}
949
950static int coda_encoder_cmd(struct file *file, void *fh,
951 struct v4l2_encoder_cmd *ec)
952{
953 struct coda_ctx *ctx = fh_to_ctx(fh);
954 struct vb2_queue *dst_vq;
955 int ret;
956
957 ret = coda_try_encoder_cmd(file, fh, ec);
958 if (ret < 0)
959 return ret;
960
961
962 if (ctx->inst_type != CODA_INST_ENCODER)
963 return 0;
964
965
966 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
967
968
969 if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) {
970 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
971 V4L2_BUF_TYPE_VIDEO_CAPTURE);
972 dst_vq->last_buffer_dequeued = true;
973 wake_up(&dst_vq->done_wq);
974 }
975
976 return 0;
977}
978
979static int coda_try_decoder_cmd(struct file *file, void *fh,
980 struct v4l2_decoder_cmd *dc)
981{
982 if (dc->cmd != V4L2_DEC_CMD_STOP)
983 return -EINVAL;
984
985 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
986 return -EINVAL;
987
988 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
989 return -EINVAL;
990
991 return 0;
992}
993
994static int coda_decoder_cmd(struct file *file, void *fh,
995 struct v4l2_decoder_cmd *dc)
996{
997 struct coda_ctx *ctx = fh_to_ctx(fh);
998 int ret;
999
1000 ret = coda_try_decoder_cmd(file, fh, dc);
1001 if (ret < 0)
1002 return ret;
1003
1004
1005 if (ctx->inst_type != CODA_INST_DECODER)
1006 return 0;
1007
1008
1009 coda_bit_stream_end_flag(ctx);
1010 ctx->hold = false;
1011 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1012
1013 return 0;
1014}
1015
1016static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1017{
1018 struct coda_ctx *ctx = fh_to_ctx(fh);
1019 struct v4l2_fract *tpf;
1020
1021 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1022 return -EINVAL;
1023
1024 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1025 tpf = &a->parm.output.timeperframe;
1026 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1027 tpf->numerator = 1 + (ctx->params.framerate >>
1028 CODA_FRATE_DIV_OFFSET);
1029
1030 return 0;
1031}
1032
1033
1034
1035
1036
1037static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1038{
1039 struct v4l2_fract s = *timeperframe;
1040 struct v4l2_fract f0;
1041 struct v4l2_fract f1 = { 1, 0 };
1042 struct v4l2_fract f2 = { 0, 1 };
1043 unsigned int i, div, s_denominator;
1044
1045
1046 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1047 timeperframe->numerator = 1;
1048 timeperframe->denominator = 65535;
1049 return;
1050 }
1051
1052
1053 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1054 timeperframe->numerator = 1;
1055 timeperframe->denominator = 0;
1056 return;
1057 }
1058
1059
1060 div = gcd(s.numerator, s.denominator);
1061 if (div > 1) {
1062 s.numerator /= div;
1063 s.denominator /= div;
1064 }
1065
1066 if (s.numerator <= 65536 && s.denominator < 65536) {
1067 *timeperframe = s;
1068 return;
1069 }
1070
1071
1072 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1073 f0 = f1;
1074 f1 = f2;
1075
1076
1077 if (s.numerator == 0)
1078 break;
1079
1080 i = s.denominator / s.numerator;
1081
1082 f2.numerator = f0.numerator + i * f1.numerator;
1083 f2.denominator = f0.denominator + i * f2.denominator;
1084
1085 s_denominator = s.numerator;
1086 s.numerator = s.denominator % s.numerator;
1087 s.denominator = s_denominator;
1088 }
1089
1090 *timeperframe = f1;
1091}
1092
1093static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1094{
1095 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1096 timeperframe->denominator;
1097}
1098
1099static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1100{
1101 struct coda_ctx *ctx = fh_to_ctx(fh);
1102 struct v4l2_fract *tpf;
1103
1104 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1105 return -EINVAL;
1106
1107 tpf = &a->parm.output.timeperframe;
1108 coda_approximate_timeperframe(tpf);
1109 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1110
1111 return 0;
1112}
1113
1114static int coda_subscribe_event(struct v4l2_fh *fh,
1115 const struct v4l2_event_subscription *sub)
1116{
1117 switch (sub->type) {
1118 case V4L2_EVENT_EOS:
1119 return v4l2_event_subscribe(fh, sub, 0, NULL);
1120 default:
1121 return v4l2_ctrl_subscribe_event(fh, sub);
1122 }
1123}
1124
1125static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1126 .vidioc_querycap = coda_querycap,
1127
1128 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1129 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1130 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1131 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1132
1133 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1134 .vidioc_g_fmt_vid_out = coda_g_fmt,
1135 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1136 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1137
1138 .vidioc_reqbufs = coda_reqbufs,
1139 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1140
1141 .vidioc_qbuf = coda_qbuf,
1142 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1143 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
1144 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1145 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1146
1147 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1148 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1149
1150 .vidioc_g_selection = coda_g_selection,
1151
1152 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1153 .vidioc_encoder_cmd = coda_encoder_cmd,
1154 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1155 .vidioc_decoder_cmd = coda_decoder_cmd,
1156
1157 .vidioc_g_parm = coda_g_parm,
1158 .vidioc_s_parm = coda_s_parm,
1159
1160 .vidioc_subscribe_event = coda_subscribe_event,
1161 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1162};
1163
1164
1165
1166
1167
1168static void coda_device_run(void *m2m_priv)
1169{
1170 struct coda_ctx *ctx = m2m_priv;
1171 struct coda_dev *dev = ctx->dev;
1172
1173 queue_work(dev->workqueue, &ctx->pic_run_work);
1174}
1175
1176static void coda_pic_run_work(struct work_struct *work)
1177{
1178 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1179 struct coda_dev *dev = ctx->dev;
1180 int ret;
1181
1182 mutex_lock(&ctx->buffer_mutex);
1183 mutex_lock(&dev->coda_mutex);
1184
1185 ret = ctx->ops->prepare_run(ctx);
1186 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1187 mutex_unlock(&dev->coda_mutex);
1188 mutex_unlock(&ctx->buffer_mutex);
1189
1190 return;
1191 }
1192
1193 if (!wait_for_completion_timeout(&ctx->completion,
1194 msecs_to_jiffies(1000))) {
1195 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1196
1197 ctx->hold = true;
1198
1199 coda_hw_reset(ctx);
1200
1201 if (ctx->ops->run_timeout)
1202 ctx->ops->run_timeout(ctx);
1203 } else if (!ctx->aborting) {
1204 ctx->ops->finish_run(ctx);
1205 }
1206
1207 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1208 ctx->ops->seq_end_work)
1209 queue_work(dev->workqueue, &ctx->seq_end_work);
1210
1211 mutex_unlock(&dev->coda_mutex);
1212 mutex_unlock(&ctx->buffer_mutex);
1213
1214 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1215}
1216
1217static int coda_job_ready(void *m2m_priv)
1218{
1219 struct coda_ctx *ctx = m2m_priv;
1220 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1221
1222
1223
1224
1225
1226
1227 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1228 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1229 "not ready: not enough video buffers.\n");
1230 return 0;
1231 }
1232
1233 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1234 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1235 "not ready: not enough video capture buffers.\n");
1236 return 0;
1237 }
1238
1239 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1240 bool stream_end = ctx->bit_stream_param &
1241 CODA_BIT_STREAM_END_FLAG;
1242 int num_metas = ctx->num_metas;
1243 unsigned int count;
1244
1245 count = hweight32(ctx->frm_dis_flg);
1246 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1247 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1248 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1249 ctx->idx, count, ctx->num_internal_frames,
1250 ctx->frm_dis_flg);
1251 return 0;
1252 }
1253
1254 if (ctx->hold && !src_bufs) {
1255 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1256 "%d: not ready: on hold for more buffers.\n",
1257 ctx->idx);
1258 return 0;
1259 }
1260
1261 if (!stream_end && (num_metas + src_bufs) < 2) {
1262 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1263 "%d: not ready: need 2 buffers available (%d, %d)\n",
1264 ctx->idx, num_metas, src_bufs);
1265 return 0;
1266 }
1267
1268
1269 if (!src_bufs && !stream_end &&
1270 (coda_get_bitstream_payload(ctx) < 512)) {
1271 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1272 "%d: not ready: not enough bitstream data (%d).\n",
1273 ctx->idx, coda_get_bitstream_payload(ctx));
1274 return 0;
1275 }
1276 }
1277
1278 if (ctx->aborting) {
1279 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1280 "not ready: aborting\n");
1281 return 0;
1282 }
1283
1284 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1285 "job ready\n");
1286
1287 return 1;
1288}
1289
1290static void coda_job_abort(void *priv)
1291{
1292 struct coda_ctx *ctx = priv;
1293
1294 ctx->aborting = 1;
1295
1296 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1297 "Aborting task\n");
1298}
1299
1300static void coda_lock(void *m2m_priv)
1301{
1302 struct coda_ctx *ctx = m2m_priv;
1303 struct coda_dev *pcdev = ctx->dev;
1304
1305 mutex_lock(&pcdev->dev_mutex);
1306}
1307
1308static void coda_unlock(void *m2m_priv)
1309{
1310 struct coda_ctx *ctx = m2m_priv;
1311 struct coda_dev *pcdev = ctx->dev;
1312
1313 mutex_unlock(&pcdev->dev_mutex);
1314}
1315
1316static const struct v4l2_m2m_ops coda_m2m_ops = {
1317 .device_run = coda_device_run,
1318 .job_ready = coda_job_ready,
1319 .job_abort = coda_job_abort,
1320 .lock = coda_lock,
1321 .unlock = coda_unlock,
1322};
1323
1324static void set_default_params(struct coda_ctx *ctx)
1325{
1326 unsigned int max_w, max_h, usize, csize;
1327
1328 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1329 ctx->cvd->dst_formats[0]);
1330 max_w = min(ctx->codec->max_w, 1920U);
1331 max_h = min(ctx->codec->max_h, 1088U);
1332 usize = max_w * max_h * 3 / 2;
1333 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1334
1335 ctx->params.codec_mode = ctx->codec->mode;
1336 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1337 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1338 else
1339 ctx->colorspace = V4L2_COLORSPACE_REC709;
1340 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1341 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1342 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1343 ctx->params.framerate = 30;
1344
1345
1346 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1347 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1348 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1349 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1350 ctx->q_data[V4L2_M2M_DST].width = max_w;
1351 ctx->q_data[V4L2_M2M_DST].height = max_h;
1352 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1353 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1354 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1355 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1356 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1357 } else {
1358 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1359 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1360 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1361 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1362 }
1363 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1364 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1365 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1366 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1367
1368
1369
1370
1371
1372 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1373}
1374
1375
1376
1377
1378static int coda_queue_setup(struct vb2_queue *vq,
1379 unsigned int *nbuffers, unsigned int *nplanes,
1380 unsigned int sizes[], struct device *alloc_devs[])
1381{
1382 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1383 struct coda_q_data *q_data;
1384 unsigned int size;
1385
1386 q_data = get_q_data(ctx, vq->type);
1387 size = q_data->sizeimage;
1388
1389 *nplanes = 1;
1390 sizes[0] = size;
1391
1392 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1393 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1394
1395 return 0;
1396}
1397
1398static int coda_buf_prepare(struct vb2_buffer *vb)
1399{
1400 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1401 struct coda_q_data *q_data;
1402
1403 q_data = get_q_data(ctx, vb->vb2_queue->type);
1404
1405 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1406 v4l2_warn(&ctx->dev->v4l2_dev,
1407 "%s data will not fit into plane (%lu < %lu)\n",
1408 __func__, vb2_plane_size(vb, 0),
1409 (long)q_data->sizeimage);
1410 return -EINVAL;
1411 }
1412
1413 return 0;
1414}
1415
1416static void coda_buf_queue(struct vb2_buffer *vb)
1417{
1418 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1419 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1420 struct vb2_queue *vq = vb->vb2_queue;
1421 struct coda_q_data *q_data;
1422
1423 q_data = get_q_data(ctx, vb->vb2_queue->type);
1424
1425
1426
1427
1428
1429 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1430
1431
1432
1433
1434 if (vb2_get_plane_payload(vb, 0) == 0)
1435 coda_bit_stream_end_flag(ctx);
1436
1437 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1438
1439
1440
1441
1442
1443
1444 if (!ctx->params.h264_profile_idc)
1445 coda_sps_parse_profile(ctx, vb);
1446 }
1447
1448 mutex_lock(&ctx->bitstream_mutex);
1449 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1450 if (vb2_is_streaming(vb->vb2_queue))
1451
1452 coda_fill_bitstream(ctx, NULL);
1453 mutex_unlock(&ctx->bitstream_mutex);
1454 } else {
1455 if (ctx->inst_type == CODA_INST_ENCODER &&
1456 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1457 vbuf->sequence = ctx->qsequence++;
1458 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1459 }
1460}
1461
1462int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1463 size_t size, const char *name, struct dentry *parent)
1464{
1465 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1466 GFP_KERNEL);
1467 if (!buf->vaddr) {
1468 v4l2_err(&dev->v4l2_dev,
1469 "Failed to allocate %s buffer of size %zu\n",
1470 name, size);
1471 return -ENOMEM;
1472 }
1473
1474 buf->size = size;
1475
1476 if (name && parent) {
1477 buf->blob.data = buf->vaddr;
1478 buf->blob.size = size;
1479 buf->dentry = debugfs_create_blob(name, 0644, parent,
1480 &buf->blob);
1481 if (!buf->dentry)
1482 dev_warn(&dev->plat_dev->dev,
1483 "failed to create debugfs entry %s\n", name);
1484 }
1485
1486 return 0;
1487}
1488
1489void coda_free_aux_buf(struct coda_dev *dev,
1490 struct coda_aux_buf *buf)
1491{
1492 if (buf->vaddr) {
1493 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1494 buf->vaddr, buf->paddr);
1495 buf->vaddr = NULL;
1496 buf->size = 0;
1497 debugfs_remove(buf->dentry);
1498 buf->dentry = NULL;
1499 }
1500}
1501
1502static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1503{
1504 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1505 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1506 struct coda_q_data *q_data_src, *q_data_dst;
1507 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1508 struct vb2_v4l2_buffer *buf;
1509 struct list_head list;
1510 int ret = 0;
1511
1512 if (count < 1)
1513 return -EINVAL;
1514
1515 INIT_LIST_HEAD(&list);
1516
1517 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1518 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1519 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1520
1521 mutex_lock(&ctx->bitstream_mutex);
1522 coda_fill_bitstream(ctx, &list);
1523 mutex_unlock(&ctx->bitstream_mutex);
1524
1525 if (coda_get_bitstream_payload(ctx) < 512) {
1526 ret = -EINVAL;
1527 goto err;
1528 }
1529 }
1530
1531 ctx->streamon_out = 1;
1532 } else {
1533 ctx->streamon_cap = 1;
1534 }
1535
1536
1537 if (!(ctx->streamon_out && ctx->streamon_cap))
1538 goto out;
1539
1540 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1541 if ((q_data_src->width != q_data_dst->width &&
1542 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1543 (q_data_src->height != q_data_dst->height &&
1544 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1545 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1546 q_data_src->width, q_data_src->height,
1547 q_data_dst->width, q_data_dst->height);
1548 ret = -EINVAL;
1549 goto err;
1550 }
1551
1552
1553 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1554 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1555
1556 ctx->gopcounter = ctx->params.gop_size - 1;
1557
1558 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1559 q_data_dst->fourcc);
1560 if (!ctx->codec) {
1561 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1562 ret = -EINVAL;
1563 goto err;
1564 }
1565
1566 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1567 ctx->params.gop_size = 1;
1568 ctx->gopcounter = ctx->params.gop_size - 1;
1569
1570 ret = ctx->ops->start_streaming(ctx);
1571 if (ctx->inst_type == CODA_INST_DECODER) {
1572 if (ret == -EAGAIN)
1573 goto out;
1574 }
1575 if (ret < 0)
1576 goto err;
1577
1578out:
1579 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1580 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1581 list_del(&m2m_buf->list);
1582 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1583 }
1584 }
1585 return 0;
1586
1587err:
1588 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1589 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1590 list_del(&m2m_buf->list);
1591 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1592 }
1593 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1594 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1595 } else {
1596 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1597 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1598 }
1599 return ret;
1600}
1601
1602static void coda_stop_streaming(struct vb2_queue *q)
1603{
1604 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1605 struct coda_dev *dev = ctx->dev;
1606 struct vb2_v4l2_buffer *buf;
1607 unsigned long flags;
1608 bool stop;
1609
1610 stop = ctx->streamon_out && ctx->streamon_cap;
1611
1612 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1613 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1614 "%s: output\n", __func__);
1615 ctx->streamon_out = 0;
1616
1617 coda_bit_stream_end_flag(ctx);
1618
1619 ctx->qsequence = 0;
1620
1621 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1622 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1623 } else {
1624 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1625 "%s: capture\n", __func__);
1626 ctx->streamon_cap = 0;
1627
1628 ctx->osequence = 0;
1629 ctx->sequence_offset = 0;
1630
1631 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1632 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1633 }
1634
1635 if (stop) {
1636 struct coda_buffer_meta *meta;
1637
1638 if (ctx->ops->seq_end_work) {
1639 queue_work(dev->workqueue, &ctx->seq_end_work);
1640 flush_work(&ctx->seq_end_work);
1641 }
1642 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1643 while (!list_empty(&ctx->buffer_meta_list)) {
1644 meta = list_first_entry(&ctx->buffer_meta_list,
1645 struct coda_buffer_meta, list);
1646 list_del(&meta->list);
1647 kfree(meta);
1648 }
1649 ctx->num_metas = 0;
1650 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1651 kfifo_init(&ctx->bitstream_fifo,
1652 ctx->bitstream.vaddr, ctx->bitstream.size);
1653 ctx->runcounter = 0;
1654 ctx->aborting = 0;
1655 }
1656
1657 if (!ctx->streamon_out && !ctx->streamon_cap)
1658 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1659}
1660
1661static const struct vb2_ops coda_qops = {
1662 .queue_setup = coda_queue_setup,
1663 .buf_prepare = coda_buf_prepare,
1664 .buf_queue = coda_buf_queue,
1665 .start_streaming = coda_start_streaming,
1666 .stop_streaming = coda_stop_streaming,
1667 .wait_prepare = vb2_ops_wait_prepare,
1668 .wait_finish = vb2_ops_wait_finish,
1669};
1670
1671static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1672{
1673 struct coda_ctx *ctx =
1674 container_of(ctrl->handler, struct coda_ctx, ctrls);
1675
1676 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1677 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1678
1679 switch (ctrl->id) {
1680 case V4L2_CID_HFLIP:
1681 if (ctrl->val)
1682 ctx->params.rot_mode |= CODA_MIR_HOR;
1683 else
1684 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1685 break;
1686 case V4L2_CID_VFLIP:
1687 if (ctrl->val)
1688 ctx->params.rot_mode |= CODA_MIR_VER;
1689 else
1690 ctx->params.rot_mode &= ~CODA_MIR_VER;
1691 break;
1692 case V4L2_CID_MPEG_VIDEO_BITRATE:
1693 ctx->params.bitrate = ctrl->val / 1000;
1694 break;
1695 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1696 ctx->params.gop_size = ctrl->val;
1697 break;
1698 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1699 ctx->params.h264_intra_qp = ctrl->val;
1700 break;
1701 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1702 ctx->params.h264_inter_qp = ctrl->val;
1703 break;
1704 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1705 ctx->params.h264_min_qp = ctrl->val;
1706 break;
1707 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1708 ctx->params.h264_max_qp = ctrl->val;
1709 break;
1710 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1711 ctx->params.h264_deblk_alpha = ctrl->val;
1712 break;
1713 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1714 ctx->params.h264_deblk_beta = ctrl->val;
1715 break;
1716 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1717 ctx->params.h264_deblk_enabled = (ctrl->val ==
1718 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1719 break;
1720 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1721
1722 ctx->params.h264_profile_idc = 66;
1723 break;
1724 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1725
1726 break;
1727 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1728 ctx->params.mpeg4_intra_qp = ctrl->val;
1729 break;
1730 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1731 ctx->params.mpeg4_inter_qp = ctrl->val;
1732 break;
1733 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1734 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1735
1736 break;
1737 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1738 ctx->params.slice_mode = ctrl->val;
1739 break;
1740 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1741 ctx->params.slice_max_mb = ctrl->val;
1742 break;
1743 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1744 ctx->params.slice_max_bits = ctrl->val * 8;
1745 break;
1746 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1747 break;
1748 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1749 ctx->params.intra_refresh = ctrl->val;
1750 break;
1751 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1752 ctx->params.force_ipicture = true;
1753 break;
1754 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1755 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1756 break;
1757 case V4L2_CID_JPEG_RESTART_INTERVAL:
1758 ctx->params.jpeg_restart_interval = ctrl->val;
1759 break;
1760 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1761 ctx->params.vbv_delay = ctrl->val;
1762 break;
1763 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1764 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1765 break;
1766 default:
1767 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1768 "Invalid control, id=%d, val=%d\n",
1769 ctrl->id, ctrl->val);
1770 return -EINVAL;
1771 }
1772
1773 return 0;
1774}
1775
1776static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1777 .s_ctrl = coda_s_ctrl,
1778};
1779
1780static void coda_encode_ctrls(struct coda_ctx *ctx)
1781{
1782 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
1783
1784 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1785 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1786 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1787 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
1788 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1789 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1790 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1791 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1792 if (ctx->dev->devtype->product != CODA_960) {
1793 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1794 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1795 }
1796 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1797 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1798 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1799 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1800 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1801 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1802 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1803 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1804 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1805 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1806 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1807 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1808 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
1809 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
1810 if (ctx->dev->devtype->product == CODA_HX4 ||
1811 ctx->dev->devtype->product == CODA_7541) {
1812 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1813 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1814 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
1815 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1816 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1817 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
1818 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
1819 }
1820 if (ctx->dev->devtype->product == CODA_960) {
1821 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1822 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1823 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
1824 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1825 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1826 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1827 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
1828 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
1829 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1830 }
1831 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1832 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1833 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1834 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1835 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1836 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
1837 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
1838 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
1839 if (ctx->dev->devtype->product == CODA_HX4 ||
1840 ctx->dev->devtype->product == CODA_7541 ||
1841 ctx->dev->devtype->product == CODA_960) {
1842 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1843 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
1844 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
1845 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
1846 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
1847 }
1848 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1849 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1850 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1851 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1852 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1853 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1854 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1855 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1856 500);
1857 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1858 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1859 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1860 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1861 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1862 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1863 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1864 1920 * 1088 / 256, 1, 0);
1865 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1866 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1867
1868
1869
1870
1871 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1872 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
1873}
1874
1875static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1876{
1877 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1878 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1879 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1880 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1881}
1882
1883static int coda_ctrls_setup(struct coda_ctx *ctx)
1884{
1885 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1886
1887 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1888 V4L2_CID_HFLIP, 0, 1, 1, 0);
1889 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1890 V4L2_CID_VFLIP, 0, 1, 1, 0);
1891 if (ctx->inst_type == CODA_INST_ENCODER) {
1892 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1893 coda_jpeg_encode_ctrls(ctx);
1894 else
1895 coda_encode_ctrls(ctx);
1896 }
1897
1898 if (ctx->ctrls.error) {
1899 v4l2_err(&ctx->dev->v4l2_dev,
1900 "control initialization error (%d)",
1901 ctx->ctrls.error);
1902 return -EINVAL;
1903 }
1904
1905 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1906}
1907
1908static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1909{
1910 vq->drv_priv = ctx;
1911 vq->ops = &coda_qops;
1912 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1913 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1914 vq->lock = &ctx->dev->dev_mutex;
1915
1916
1917
1918
1919
1920
1921 vq->allow_zero_bytesused = 1;
1922
1923
1924
1925
1926
1927 vq->min_buffers_needed = 1;
1928 vq->dev = &ctx->dev->plat_dev->dev;
1929
1930 return vb2_queue_init(vq);
1931}
1932
1933int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1934 struct vb2_queue *dst_vq)
1935{
1936 int ret;
1937
1938 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1939 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1940 src_vq->mem_ops = &vb2_dma_contig_memops;
1941
1942 ret = coda_queue_init(priv, src_vq);
1943 if (ret)
1944 return ret;
1945
1946 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1947 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1948 dst_vq->mem_ops = &vb2_dma_contig_memops;
1949
1950 return coda_queue_init(priv, dst_vq);
1951}
1952
1953int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1954 struct vb2_queue *dst_vq)
1955{
1956 int ret;
1957
1958 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1959 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1960 src_vq->mem_ops = &vb2_vmalloc_memops;
1961
1962 ret = coda_queue_init(priv, src_vq);
1963 if (ret)
1964 return ret;
1965
1966 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1967 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1968 dst_vq->mem_ops = &vb2_dma_contig_memops;
1969
1970 return coda_queue_init(priv, dst_vq);
1971}
1972
1973static int coda_next_free_instance(struct coda_dev *dev)
1974{
1975 int idx = ffz(dev->instance_mask);
1976
1977 if ((idx < 0) ||
1978 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1979 return -EBUSY;
1980
1981 return idx;
1982}
1983
1984
1985
1986
1987
1988static int coda_open(struct file *file)
1989{
1990 struct video_device *vdev = video_devdata(file);
1991 struct coda_dev *dev = video_get_drvdata(vdev);
1992 struct coda_ctx *ctx = NULL;
1993 char *name;
1994 int ret;
1995 int idx;
1996
1997 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1998 if (!ctx)
1999 return -ENOMEM;
2000
2001 idx = coda_next_free_instance(dev);
2002 if (idx < 0) {
2003 ret = idx;
2004 goto err_coda_max;
2005 }
2006 set_bit(idx, &dev->instance_mask);
2007
2008 name = kasprintf(GFP_KERNEL, "context%d", idx);
2009 if (!name) {
2010 ret = -ENOMEM;
2011 goto err_coda_name_init;
2012 }
2013
2014 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2015 kfree(name);
2016
2017 ctx->cvd = to_coda_video_device(vdev);
2018 ctx->inst_type = ctx->cvd->type;
2019 ctx->ops = ctx->cvd->ops;
2020 ctx->use_bit = !ctx->cvd->direct;
2021 init_completion(&ctx->completion);
2022 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2023 if (ctx->ops->seq_end_work)
2024 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2025 v4l2_fh_init(&ctx->fh, video_devdata(file));
2026 file->private_data = &ctx->fh;
2027 v4l2_fh_add(&ctx->fh);
2028 ctx->dev = dev;
2029 ctx->idx = idx;
2030 switch (dev->devtype->product) {
2031 case CODA_960:
2032
2033
2034
2035
2036
2037
2038 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2039 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2040
2041 case CODA_HX4:
2042 case CODA_7541:
2043 ctx->reg_idx = 0;
2044 break;
2045 default:
2046 ctx->reg_idx = idx;
2047 }
2048 if (ctx->dev->vdoa && !disable_vdoa) {
2049 ctx->vdoa = vdoa_context_create(dev->vdoa);
2050 if (!ctx->vdoa)
2051 v4l2_warn(&dev->v4l2_dev,
2052 "Failed to create vdoa context: not using vdoa");
2053 }
2054 ctx->use_vdoa = false;
2055
2056
2057 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2058 if (ret < 0) {
2059 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2060 goto err_pm_get;
2061 }
2062
2063 ret = clk_prepare_enable(dev->clk_per);
2064 if (ret)
2065 goto err_clk_per;
2066
2067 ret = clk_prepare_enable(dev->clk_ahb);
2068 if (ret)
2069 goto err_clk_ahb;
2070
2071 set_default_params(ctx);
2072 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2073 ctx->ops->queue_init);
2074 if (IS_ERR(ctx->fh.m2m_ctx)) {
2075 ret = PTR_ERR(ctx->fh.m2m_ctx);
2076
2077 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2078 __func__, ret);
2079 goto err_ctx_init;
2080 }
2081
2082 ret = coda_ctrls_setup(ctx);
2083 if (ret) {
2084 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2085 goto err_ctrls_setup;
2086 }
2087
2088 ctx->fh.ctrl_handler = &ctx->ctrls;
2089
2090 mutex_init(&ctx->bitstream_mutex);
2091 mutex_init(&ctx->buffer_mutex);
2092 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2093 spin_lock_init(&ctx->buffer_meta_lock);
2094
2095 coda_lock(ctx);
2096 list_add(&ctx->list, &dev->instances);
2097 coda_unlock(ctx);
2098
2099 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2100 ctx->idx, ctx);
2101
2102 return 0;
2103
2104err_ctrls_setup:
2105 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2106err_ctx_init:
2107 clk_disable_unprepare(dev->clk_ahb);
2108err_clk_ahb:
2109 clk_disable_unprepare(dev->clk_per);
2110err_clk_per:
2111 pm_runtime_put_sync(&dev->plat_dev->dev);
2112err_pm_get:
2113 v4l2_fh_del(&ctx->fh);
2114 v4l2_fh_exit(&ctx->fh);
2115 clear_bit(ctx->idx, &dev->instance_mask);
2116err_coda_name_init:
2117err_coda_max:
2118 kfree(ctx);
2119 return ret;
2120}
2121
2122static int coda_release(struct file *file)
2123{
2124 struct coda_dev *dev = video_drvdata(file);
2125 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2126
2127 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2128 ctx);
2129
2130 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2131 coda_bit_stream_end_flag(ctx);
2132
2133
2134 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2135
2136 if (ctx->vdoa)
2137 vdoa_context_destroy(ctx->vdoa);
2138
2139
2140 if (ctx->ops->seq_end_work) {
2141 queue_work(dev->workqueue, &ctx->seq_end_work);
2142 flush_work(&ctx->seq_end_work);
2143 }
2144
2145 coda_lock(ctx);
2146 list_del(&ctx->list);
2147 coda_unlock(ctx);
2148
2149 if (ctx->dev->devtype->product == CODA_DX6)
2150 coda_free_aux_buf(dev, &ctx->workbuf);
2151
2152 v4l2_ctrl_handler_free(&ctx->ctrls);
2153 clk_disable_unprepare(dev->clk_ahb);
2154 clk_disable_unprepare(dev->clk_per);
2155 pm_runtime_put_sync(&dev->plat_dev->dev);
2156 v4l2_fh_del(&ctx->fh);
2157 v4l2_fh_exit(&ctx->fh);
2158 clear_bit(ctx->idx, &dev->instance_mask);
2159 if (ctx->ops->release)
2160 ctx->ops->release(ctx);
2161 debugfs_remove_recursive(ctx->debugfs_entry);
2162 kfree(ctx);
2163
2164 return 0;
2165}
2166
2167static const struct v4l2_file_operations coda_fops = {
2168 .owner = THIS_MODULE,
2169 .open = coda_open,
2170 .release = coda_release,
2171 .poll = v4l2_m2m_fop_poll,
2172 .unlocked_ioctl = video_ioctl2,
2173 .mmap = v4l2_m2m_fop_mmap,
2174};
2175
2176static int coda_hw_init(struct coda_dev *dev)
2177{
2178 u32 data;
2179 u16 *p;
2180 int i, ret;
2181
2182 ret = clk_prepare_enable(dev->clk_per);
2183 if (ret)
2184 goto err_clk_per;
2185
2186 ret = clk_prepare_enable(dev->clk_ahb);
2187 if (ret)
2188 goto err_clk_ahb;
2189
2190 reset_control_reset(dev->rstc);
2191
2192
2193
2194
2195
2196
2197
2198 p = (u16 *)dev->codebuf.vaddr;
2199 if (dev->devtype->product == CODA_DX6) {
2200 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2201 data = CODA_DOWN_ADDRESS_SET(i) |
2202 CODA_DOWN_DATA_SET(p[i ^ 1]);
2203 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2204 }
2205 } else {
2206 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2207 data = CODA_DOWN_ADDRESS_SET(i) |
2208 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2209 3 - (i % 4)]);
2210 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2211 }
2212 }
2213
2214
2215 for (i = 0; i < 64; i++)
2216 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2217
2218
2219 if (dev->devtype->product == CODA_960 ||
2220 dev->devtype->product == CODA_7541 ||
2221 dev->devtype->product == CODA_HX4) {
2222 coda_write(dev, dev->tempbuf.paddr,
2223 CODA_REG_BIT_TEMP_BUF_ADDR);
2224 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2225 } else {
2226 coda_write(dev, dev->workbuf.paddr,
2227 CODA_REG_BIT_WORK_BUF_ADDR);
2228 }
2229 coda_write(dev, dev->codebuf.paddr,
2230 CODA_REG_BIT_CODE_BUF_ADDR);
2231 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2232
2233
2234 switch (dev->devtype->product) {
2235 case CODA_DX6:
2236 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2237 CODA_REG_BIT_STREAM_CTRL);
2238 break;
2239 default:
2240 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2241 CODA_REG_BIT_STREAM_CTRL);
2242 }
2243 if (dev->devtype->product == CODA_960)
2244 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2245 CODA_REG_BIT_FRAME_MEM_CTRL);
2246 else
2247 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2248
2249 if (dev->devtype->product != CODA_DX6)
2250 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2251
2252 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2253 CODA_REG_BIT_INT_ENABLE);
2254
2255
2256 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2257 data |= CODA_REG_RESET_ENABLE;
2258 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2259 udelay(10);
2260 data &= ~CODA_REG_RESET_ENABLE;
2261 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2262 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2263
2264 clk_disable_unprepare(dev->clk_ahb);
2265 clk_disable_unprepare(dev->clk_per);
2266
2267 return 0;
2268
2269err_clk_ahb:
2270 clk_disable_unprepare(dev->clk_per);
2271err_clk_per:
2272 return ret;
2273}
2274
2275static int coda_register_device(struct coda_dev *dev, int i)
2276{
2277 struct video_device *vfd = &dev->vfd[i];
2278
2279 if (i >= dev->devtype->num_vdevs)
2280 return -EINVAL;
2281
2282 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2283 vfd->fops = &coda_fops;
2284 vfd->ioctl_ops = &coda_ioctl_ops;
2285 vfd->release = video_device_release_empty,
2286 vfd->lock = &dev->dev_mutex;
2287 vfd->v4l2_dev = &dev->v4l2_dev;
2288 vfd->vfl_dir = VFL_DIR_M2M;
2289 video_set_drvdata(vfd, dev);
2290
2291
2292 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2293 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2294 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2295
2296 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2297}
2298
2299static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2300 size_t size)
2301{
2302 u32 *src = (u32 *)buf;
2303
2304
2305 if (buf[0] == 'M' && buf[1] == 'X')
2306 src += 4;
2307
2308
2309
2310
2311 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2312 u32 *dst = dev->codebuf.vaddr;
2313 int i;
2314
2315
2316 if (dev->devtype->product == CODA_DX6) {
2317 for (i = 0; i < (size - 16) / 4; i++)
2318 dst[i] = (src[i] << 16) | (src[i] >> 16);
2319 } else {
2320 for (i = 0; i < (size - 16) / 4; i += 2) {
2321 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2322 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2323 }
2324 }
2325 } else {
2326
2327 memcpy(dev->codebuf.vaddr, src, size);
2328 }
2329}
2330
2331static void coda_fw_callback(const struct firmware *fw, void *context);
2332
2333static int coda_firmware_request(struct coda_dev *dev)
2334{
2335 char *fw;
2336
2337 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2338 return -EINVAL;
2339
2340 fw = dev->devtype->firmware[dev->firmware];
2341
2342 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2343 coda_product_name(dev->devtype->product));
2344
2345 return request_firmware_nowait(THIS_MODULE, true, fw,
2346 &dev->plat_dev->dev, GFP_KERNEL, dev,
2347 coda_fw_callback);
2348}
2349
2350static void coda_fw_callback(const struct firmware *fw, void *context)
2351{
2352 struct coda_dev *dev = context;
2353 struct platform_device *pdev = dev->plat_dev;
2354 int i, ret;
2355
2356 if (!fw) {
2357 dev->firmware++;
2358 ret = coda_firmware_request(dev);
2359 if (ret < 0) {
2360 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2361 goto put_pm;
2362 }
2363 return;
2364 }
2365 if (dev->firmware > 0) {
2366
2367
2368
2369
2370
2371 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2372 dev->devtype->firmware[dev->firmware]);
2373 }
2374
2375
2376 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2377 dev->debugfs_root);
2378 if (ret < 0)
2379 goto put_pm;
2380
2381 coda_copy_firmware(dev, fw->data, fw->size);
2382 release_firmware(fw);
2383
2384 ret = coda_hw_init(dev);
2385 if (ret < 0) {
2386 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2387 goto put_pm;
2388 }
2389
2390 ret = coda_check_firmware(dev);
2391 if (ret < 0)
2392 goto put_pm;
2393
2394 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2395 if (IS_ERR(dev->m2m_dev)) {
2396 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2397 goto put_pm;
2398 }
2399
2400 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2401 ret = coda_register_device(dev, i);
2402 if (ret) {
2403 v4l2_err(&dev->v4l2_dev,
2404 "Failed to register %s video device: %d\n",
2405 dev->devtype->vdevs[i]->name, ret);
2406 goto rel_vfd;
2407 }
2408 }
2409
2410 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
2411 dev->vfd[0].num, dev->vfd[i - 1].num);
2412
2413 pm_runtime_put_sync(&pdev->dev);
2414 return;
2415
2416rel_vfd:
2417 while (--i >= 0)
2418 video_unregister_device(&dev->vfd[i]);
2419 v4l2_m2m_release(dev->m2m_dev);
2420put_pm:
2421 pm_runtime_put_sync(&pdev->dev);
2422}
2423
2424enum coda_platform {
2425 CODA_IMX27,
2426 CODA_IMX51,
2427 CODA_IMX53,
2428 CODA_IMX6Q,
2429 CODA_IMX6DL,
2430};
2431
2432static const struct coda_devtype coda_devdata[] = {
2433 [CODA_IMX27] = {
2434 .firmware = {
2435 "vpu_fw_imx27_TO2.bin",
2436 "vpu/vpu_fw_imx27_TO2.bin",
2437 "v4l-codadx6-imx27.bin"
2438 },
2439 .product = CODA_DX6,
2440 .codecs = codadx6_codecs,
2441 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2442 .vdevs = codadx6_video_devices,
2443 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2444 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2445 .iram_size = 0xb000,
2446 },
2447 [CODA_IMX51] = {
2448 .firmware = {
2449 "vpu_fw_imx51.bin",
2450 "vpu/vpu_fw_imx51.bin",
2451 "v4l-codahx4-imx51.bin"
2452 },
2453 .product = CODA_HX4,
2454 .codecs = codahx4_codecs,
2455 .num_codecs = ARRAY_SIZE(codahx4_codecs),
2456 .vdevs = codahx4_video_devices,
2457 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
2458 .workbuf_size = 128 * 1024,
2459 .tempbuf_size = 304 * 1024,
2460 .iram_size = 0x14000,
2461 },
2462 [CODA_IMX53] = {
2463 .firmware = {
2464 "vpu_fw_imx53.bin",
2465 "vpu/vpu_fw_imx53.bin",
2466 "v4l-coda7541-imx53.bin"
2467 },
2468 .product = CODA_7541,
2469 .codecs = coda7_codecs,
2470 .num_codecs = ARRAY_SIZE(coda7_codecs),
2471 .vdevs = coda7_video_devices,
2472 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2473 .workbuf_size = 128 * 1024,
2474 .tempbuf_size = 304 * 1024,
2475 .iram_size = 0x14000,
2476 },
2477 [CODA_IMX6Q] = {
2478 .firmware = {
2479 "vpu_fw_imx6q.bin",
2480 "vpu/vpu_fw_imx6q.bin",
2481 "v4l-coda960-imx6q.bin"
2482 },
2483 .product = CODA_960,
2484 .codecs = coda9_codecs,
2485 .num_codecs = ARRAY_SIZE(coda9_codecs),
2486 .vdevs = coda9_video_devices,
2487 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2488 .workbuf_size = 80 * 1024,
2489 .tempbuf_size = 204 * 1024,
2490 .iram_size = 0x21000,
2491 },
2492 [CODA_IMX6DL] = {
2493 .firmware = {
2494 "vpu_fw_imx6d.bin",
2495 "vpu/vpu_fw_imx6d.bin",
2496 "v4l-coda960-imx6dl.bin"
2497 },
2498 .product = CODA_960,
2499 .codecs = coda9_codecs,
2500 .num_codecs = ARRAY_SIZE(coda9_codecs),
2501 .vdevs = coda9_video_devices,
2502 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2503 .workbuf_size = 80 * 1024,
2504 .tempbuf_size = 204 * 1024,
2505 .iram_size = 0x1f000,
2506 },
2507};
2508
2509static const struct platform_device_id coda_platform_ids[] = {
2510 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2511 { }
2512};
2513MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2514
2515#ifdef CONFIG_OF
2516static const struct of_device_id coda_dt_ids[] = {
2517 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2518 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
2519 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2520 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2521 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2522 { }
2523};
2524MODULE_DEVICE_TABLE(of, coda_dt_ids);
2525#endif
2526
2527static int coda_probe(struct platform_device *pdev)
2528{
2529 const struct of_device_id *of_id =
2530 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2531 const struct platform_device_id *pdev_id;
2532 struct coda_platform_data *pdata = pdev->dev.platform_data;
2533 struct device_node *np = pdev->dev.of_node;
2534 struct gen_pool *pool;
2535 struct coda_dev *dev;
2536 struct resource *res;
2537 int ret, irq;
2538
2539 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2540 if (!dev)
2541 return -ENOMEM;
2542
2543 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2544
2545 if (of_id)
2546 dev->devtype = of_id->data;
2547 else if (pdev_id)
2548 dev->devtype = &coda_devdata[pdev_id->driver_data];
2549 else
2550 return -EINVAL;
2551
2552 spin_lock_init(&dev->irqlock);
2553 INIT_LIST_HEAD(&dev->instances);
2554
2555 dev->plat_dev = pdev;
2556 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2557 if (IS_ERR(dev->clk_per)) {
2558 dev_err(&pdev->dev, "Could not get per clock\n");
2559 return PTR_ERR(dev->clk_per);
2560 }
2561
2562 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2563 if (IS_ERR(dev->clk_ahb)) {
2564 dev_err(&pdev->dev, "Could not get ahb clock\n");
2565 return PTR_ERR(dev->clk_ahb);
2566 }
2567
2568
2569 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2570 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2571 if (IS_ERR(dev->regs_base))
2572 return PTR_ERR(dev->regs_base);
2573
2574
2575 irq = platform_get_irq_byname(pdev, "bit");
2576 if (irq < 0)
2577 irq = platform_get_irq(pdev, 0);
2578 if (irq < 0) {
2579 dev_err(&pdev->dev, "failed to get irq resource\n");
2580 return irq;
2581 }
2582
2583 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2584 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2585 if (ret < 0) {
2586 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2587 return ret;
2588 }
2589
2590 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
2591 NULL);
2592 if (IS_ERR(dev->rstc)) {
2593 ret = PTR_ERR(dev->rstc);
2594 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
2595 return ret;
2596 }
2597
2598
2599 pool = of_gen_pool_get(np, "iram", 0);
2600 if (!pool && pdata)
2601 pool = gen_pool_get(pdata->iram_dev, NULL);
2602 if (!pool) {
2603 dev_err(&pdev->dev, "iram pool not available\n");
2604 return -ENOMEM;
2605 }
2606 dev->iram_pool = pool;
2607
2608
2609 dev->vdoa = coda_get_vdoa_data();
2610 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2611 return -EPROBE_DEFER;
2612
2613 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2614 if (ret)
2615 return ret;
2616
2617 mutex_init(&dev->dev_mutex);
2618 mutex_init(&dev->coda_mutex);
2619
2620 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2621 if (!dev->debugfs_root)
2622 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2623
2624
2625 if (dev->devtype->product == CODA_DX6) {
2626 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2627 dev->devtype->workbuf_size, "workbuf",
2628 dev->debugfs_root);
2629 if (ret < 0)
2630 goto err_v4l2_register;
2631 }
2632
2633 if (dev->devtype->tempbuf_size) {
2634 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2635 dev->devtype->tempbuf_size, "tempbuf",
2636 dev->debugfs_root);
2637 if (ret < 0)
2638 goto err_v4l2_register;
2639 }
2640
2641 dev->iram.size = dev->devtype->iram_size;
2642 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2643 &dev->iram.paddr);
2644 if (!dev->iram.vaddr) {
2645 dev_warn(&pdev->dev, "unable to alloc iram\n");
2646 } else {
2647 memset(dev->iram.vaddr, 0, dev->iram.size);
2648 dev->iram.blob.data = dev->iram.vaddr;
2649 dev->iram.blob.size = dev->iram.size;
2650 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2651 dev->debugfs_root,
2652 &dev->iram.blob);
2653 }
2654
2655 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2656 if (!dev->workqueue) {
2657 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2658 ret = -ENOMEM;
2659 goto err_v4l2_register;
2660 }
2661
2662 platform_set_drvdata(pdev, dev);
2663
2664
2665
2666
2667
2668
2669 pm_runtime_get_noresume(&pdev->dev);
2670 pm_runtime_set_active(&pdev->dev);
2671 pm_runtime_enable(&pdev->dev);
2672
2673 ret = coda_firmware_request(dev);
2674 if (ret)
2675 goto err_alloc_workqueue;
2676 return 0;
2677
2678err_alloc_workqueue:
2679 destroy_workqueue(dev->workqueue);
2680err_v4l2_register:
2681 v4l2_device_unregister(&dev->v4l2_dev);
2682 return ret;
2683}
2684
2685static int coda_remove(struct platform_device *pdev)
2686{
2687 struct coda_dev *dev = platform_get_drvdata(pdev);
2688 int i;
2689
2690 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2691 if (video_get_drvdata(&dev->vfd[i]))
2692 video_unregister_device(&dev->vfd[i]);
2693 }
2694 if (dev->m2m_dev)
2695 v4l2_m2m_release(dev->m2m_dev);
2696 pm_runtime_disable(&pdev->dev);
2697 v4l2_device_unregister(&dev->v4l2_dev);
2698 destroy_workqueue(dev->workqueue);
2699 if (dev->iram.vaddr)
2700 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2701 dev->iram.size);
2702 coda_free_aux_buf(dev, &dev->codebuf);
2703 coda_free_aux_buf(dev, &dev->tempbuf);
2704 coda_free_aux_buf(dev, &dev->workbuf);
2705 debugfs_remove_recursive(dev->debugfs_root);
2706 return 0;
2707}
2708
2709#ifdef CONFIG_PM
2710static int coda_runtime_resume(struct device *dev)
2711{
2712 struct coda_dev *cdev = dev_get_drvdata(dev);
2713 int ret = 0;
2714
2715 if (dev->pm_domain && cdev->codebuf.vaddr) {
2716 ret = coda_hw_init(cdev);
2717 if (ret)
2718 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2719 }
2720
2721 return ret;
2722}
2723#endif
2724
2725static const struct dev_pm_ops coda_pm_ops = {
2726 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2727};
2728
2729static struct platform_driver coda_driver = {
2730 .probe = coda_probe,
2731 .remove = coda_remove,
2732 .driver = {
2733 .name = CODA_NAME,
2734 .of_match_table = of_match_ptr(coda_dt_ids),
2735 .pm = &coda_pm_ops,
2736 },
2737 .id_table = coda_platform_ids,
2738};
2739
2740module_platform_driver(coda_driver);
2741
2742MODULE_LICENSE("GPL");
2743MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2744MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");
2745