58f65486de332cd893d6175ad98ca405c999a221
[deliverable/linux.git] / drivers / media / platform / coda / coda-common.c
1 /*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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/genalloc.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/irq.h>
22 #include <linux/kfifo.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/videodev2.h>
29 #include <linux/of.h>
30 #include <linux/platform_data/coda.h>
31 #include <linux/reset.h>
32
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-event.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-mem2mem.h>
38 #include <media/videobuf2-core.h>
39 #include <media/videobuf2-dma-contig.h>
40 #include <media/videobuf2-vmalloc.h>
41
42 #include "coda.h"
43
44 #define CODA_NAME "coda"
45
46 #define CODADX6_MAX_INSTANCES 4
47 #define CODA_MAX_FORMATS 4
48
49 #define CODA_ISRAM_SIZE (2048 * 2)
50
51 #define MIN_W 176
52 #define MIN_H 144
53
54 #define S_ALIGN 1 /* multiple of 2 */
55 #define W_ALIGN 1 /* multiple of 2 */
56 #define H_ALIGN 1 /* multiple of 2 */
57
58 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
59
60 int coda_debug;
61 module_param(coda_debug, int, 0644);
62 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
63
64 struct coda_fmt {
65 char *name;
66 u32 fourcc;
67 };
68
69 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
70 {
71 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
72 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
73 writel(data, dev->regs_base + reg);
74 }
75
76 unsigned int coda_read(struct coda_dev *dev, u32 reg)
77 {
78 u32 data;
79
80 data = readl(dev->regs_base + reg);
81 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
82 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
83 return data;
84 }
85
86 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
87 struct vb2_buffer *buf, unsigned int reg_y)
88 {
89 u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
90 u32 base_cb, base_cr;
91
92 switch (q_data->fourcc) {
93 case V4L2_PIX_FMT_YVU420:
94 /* Switch Cb and Cr for YVU420 format */
95 base_cr = base_y + q_data->bytesperline * q_data->height;
96 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
97 break;
98 case V4L2_PIX_FMT_YUV420:
99 case V4L2_PIX_FMT_NV12:
100 default:
101 base_cb = base_y + q_data->bytesperline * q_data->height;
102 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
103 break;
104 case V4L2_PIX_FMT_YUV422P:
105 base_cb = base_y + q_data->bytesperline * q_data->height;
106 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
107 }
108
109 coda_write(ctx->dev, base_y, reg_y);
110 coda_write(ctx->dev, base_cb, reg_y + 4);
111 coda_write(ctx->dev, base_cr, reg_y + 8);
112 }
113
114 /*
115 * Array of all formats supported by any version of Coda:
116 */
117 static const struct coda_fmt coda_formats[] = {
118 {
119 .name = "YUV 4:2:0 Planar, YCbCr",
120 .fourcc = V4L2_PIX_FMT_YUV420,
121 },
122 {
123 .name = "YUV 4:2:0 Planar, YCrCb",
124 .fourcc = V4L2_PIX_FMT_YVU420,
125 },
126 {
127 .name = "YUV 4:2:0 Partial interleaved Y/CbCr",
128 .fourcc = V4L2_PIX_FMT_NV12,
129 },
130 {
131 .name = "YUV 4:2:2 Planar, YCbCr",
132 .fourcc = V4L2_PIX_FMT_YUV422P,
133 },
134 {
135 .name = "H264 Encoded Stream",
136 .fourcc = V4L2_PIX_FMT_H264,
137 },
138 {
139 .name = "MPEG4 Encoded Stream",
140 .fourcc = V4L2_PIX_FMT_MPEG4,
141 },
142 {
143 .name = "JPEG Encoded Images",
144 .fourcc = V4L2_PIX_FMT_JPEG,
145 },
146 };
147
148 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
149 { mode, src_fourcc, dst_fourcc, max_w, max_h }
150
151 /*
152 * Arrays of codecs supported by each given version of Coda:
153 * i.MX27 -> codadx6
154 * i.MX5x -> coda7
155 * i.MX6 -> coda960
156 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
157 */
158 static const struct coda_codec codadx6_codecs[] = {
159 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
160 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
161 };
162
163 static const struct coda_codec coda7_codecs[] = {
164 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
165 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
166 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
167 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
168 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
169 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
170 };
171
172 static const struct coda_codec coda9_codecs[] = {
173 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
174 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
175 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
176 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
177 };
178
179 struct coda_video_device {
180 const char *name;
181 enum coda_inst_type type;
182 const struct coda_context_ops *ops;
183 bool direct;
184 u32 src_formats[CODA_MAX_FORMATS];
185 u32 dst_formats[CODA_MAX_FORMATS];
186 };
187
188 static const struct coda_video_device coda_bit_encoder = {
189 .name = "coda-encoder",
190 .type = CODA_INST_ENCODER,
191 .ops = &coda_bit_encode_ops,
192 .src_formats = {
193 V4L2_PIX_FMT_YUV420,
194 V4L2_PIX_FMT_YVU420,
195 V4L2_PIX_FMT_NV12,
196 },
197 .dst_formats = {
198 V4L2_PIX_FMT_H264,
199 V4L2_PIX_FMT_MPEG4,
200 },
201 };
202
203 static const struct coda_video_device coda_bit_jpeg_encoder = {
204 .name = "coda-jpeg-encoder",
205 .type = CODA_INST_ENCODER,
206 .ops = &coda_bit_encode_ops,
207 .src_formats = {
208 V4L2_PIX_FMT_YUV420,
209 V4L2_PIX_FMT_YVU420,
210 V4L2_PIX_FMT_NV12,
211 V4L2_PIX_FMT_YUV422P,
212 },
213 .dst_formats = {
214 V4L2_PIX_FMT_JPEG,
215 },
216 };
217
218 static const struct coda_video_device coda_bit_decoder = {
219 .name = "coda-decoder",
220 .type = CODA_INST_DECODER,
221 .ops = &coda_bit_decode_ops,
222 .src_formats = {
223 V4L2_PIX_FMT_H264,
224 V4L2_PIX_FMT_MPEG4,
225 },
226 .dst_formats = {
227 V4L2_PIX_FMT_YUV420,
228 V4L2_PIX_FMT_YVU420,
229 V4L2_PIX_FMT_NV12,
230 },
231 };
232
233 static const struct coda_video_device coda_bit_jpeg_decoder = {
234 .name = "coda-jpeg-decoder",
235 .type = CODA_INST_DECODER,
236 .ops = &coda_bit_decode_ops,
237 .src_formats = {
238 V4L2_PIX_FMT_JPEG,
239 },
240 .dst_formats = {
241 V4L2_PIX_FMT_YUV420,
242 V4L2_PIX_FMT_YVU420,
243 V4L2_PIX_FMT_NV12,
244 V4L2_PIX_FMT_YUV422P,
245 },
246 };
247
248 static const struct coda_video_device *codadx6_video_devices[] = {
249 &coda_bit_encoder,
250 };
251
252 static const struct coda_video_device *coda7_video_devices[] = {
253 &coda_bit_jpeg_encoder,
254 &coda_bit_jpeg_decoder,
255 &coda_bit_encoder,
256 &coda_bit_decoder,
257 };
258
259 static const struct coda_video_device *coda9_video_devices[] = {
260 &coda_bit_encoder,
261 &coda_bit_decoder,
262 };
263
264 static bool coda_format_is_yuv(u32 fourcc)
265 {
266 switch (fourcc) {
267 case V4L2_PIX_FMT_YUV420:
268 case V4L2_PIX_FMT_YVU420:
269 case V4L2_PIX_FMT_NV12:
270 case V4L2_PIX_FMT_YUV422P:
271 return true;
272 default:
273 return false;
274 }
275 }
276
277 static const char *coda_format_name(u32 fourcc)
278 {
279 int i;
280
281 for (i = 0; i < ARRAY_SIZE(coda_formats); i++) {
282 if (coda_formats[i].fourcc == fourcc)
283 return coda_formats[i].name;
284 }
285
286 return NULL;
287 }
288
289 /*
290 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
291 * tables.
292 */
293 static u32 coda_format_normalize_yuv(u32 fourcc)
294 {
295 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
296 }
297
298 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
299 int src_fourcc, int dst_fourcc)
300 {
301 const struct coda_codec *codecs = dev->devtype->codecs;
302 int num_codecs = dev->devtype->num_codecs;
303 int k;
304
305 src_fourcc = coda_format_normalize_yuv(src_fourcc);
306 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
307 if (src_fourcc == dst_fourcc)
308 return NULL;
309
310 for (k = 0; k < num_codecs; k++) {
311 if (codecs[k].src_fourcc == src_fourcc &&
312 codecs[k].dst_fourcc == dst_fourcc)
313 break;
314 }
315
316 if (k == num_codecs)
317 return NULL;
318
319 return &codecs[k];
320 }
321
322 static void coda_get_max_dimensions(struct coda_dev *dev,
323 const struct coda_codec *codec,
324 int *max_w, int *max_h)
325 {
326 const struct coda_codec *codecs = dev->devtype->codecs;
327 int num_codecs = dev->devtype->num_codecs;
328 unsigned int w, h;
329 int k;
330
331 if (codec) {
332 w = codec->max_w;
333 h = codec->max_h;
334 } else {
335 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
336 w = max(w, codecs[k].max_w);
337 h = max(h, codecs[k].max_h);
338 }
339 }
340
341 if (max_w)
342 *max_w = w;
343 if (max_h)
344 *max_h = h;
345 }
346
347 const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
348 {
349 struct coda_dev *dev = video_get_drvdata(vdev);
350 unsigned int i = vdev - dev->vfd;
351
352 if (i >= dev->devtype->num_vdevs)
353 return NULL;
354
355 return dev->devtype->vdevs[i];
356 }
357
358 const char *coda_product_name(int product)
359 {
360 static char buf[9];
361
362 switch (product) {
363 case CODA_DX6:
364 return "CodaDx6";
365 case CODA_7541:
366 return "CODA7541";
367 case CODA_960:
368 return "CODA960";
369 default:
370 snprintf(buf, sizeof(buf), "(0x%04x)", product);
371 return buf;
372 }
373 }
374
375 /*
376 * V4L2 ioctl() operations.
377 */
378 static int coda_querycap(struct file *file, void *priv,
379 struct v4l2_capability *cap)
380 {
381 struct coda_ctx *ctx = fh_to_ctx(priv);
382
383 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
384 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
385 sizeof(cap->card));
386 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
387 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
388 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
389
390 return 0;
391 }
392
393 static int coda_enum_fmt(struct file *file, void *priv,
394 struct v4l2_fmtdesc *f)
395 {
396 struct video_device *vdev = video_devdata(file);
397 const struct coda_video_device *cvd = to_coda_video_device(vdev);
398 const u32 *formats;
399 const char *name;
400
401 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
402 formats = cvd->src_formats;
403 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
404 formats = cvd->dst_formats;
405 else
406 return -EINVAL;
407
408 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
409 return -EINVAL;
410
411 name = coda_format_name(formats[f->index]);
412 strlcpy(f->description, name, sizeof(f->description));
413 f->pixelformat = formats[f->index];
414 if (!coda_format_is_yuv(formats[f->index]))
415 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
416
417 return 0;
418 }
419
420 static int coda_g_fmt(struct file *file, void *priv,
421 struct v4l2_format *f)
422 {
423 struct coda_q_data *q_data;
424 struct coda_ctx *ctx = fh_to_ctx(priv);
425
426 q_data = get_q_data(ctx, f->type);
427 if (!q_data)
428 return -EINVAL;
429
430 f->fmt.pix.field = V4L2_FIELD_NONE;
431 f->fmt.pix.pixelformat = q_data->fourcc;
432 f->fmt.pix.width = q_data->width;
433 f->fmt.pix.height = q_data->height;
434 f->fmt.pix.bytesperline = q_data->bytesperline;
435
436 f->fmt.pix.sizeimage = q_data->sizeimage;
437 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
438 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
439 else
440 f->fmt.pix.colorspace = ctx->colorspace;
441
442 return 0;
443 }
444
445 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
446 {
447 struct coda_q_data *q_data;
448 const u32 *formats;
449 int i;
450
451 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
452 formats = ctx->cvd->src_formats;
453 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
454 formats = ctx->cvd->dst_formats;
455 else
456 return -EINVAL;
457
458 for (i = 0; i < CODA_MAX_FORMATS; i++) {
459 if (formats[i] == f->fmt.pix.pixelformat) {
460 f->fmt.pix.pixelformat = formats[i];
461 return 0;
462 }
463 }
464
465 /* Fall back to currently set pixelformat */
466 q_data = get_q_data(ctx, f->type);
467 f->fmt.pix.pixelformat = q_data->fourcc;
468
469 return 0;
470 }
471
472 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
473 u32 width, u32 height)
474 {
475 /*
476 * This is a rough estimate for sensible compressed buffer
477 * sizes (between 1 and 16 bits per pixel). This could be
478 * improved by better format specific worst case estimates.
479 */
480 return round_up(clamp(sizeimage, width * height / 8,
481 width * height * 2), PAGE_SIZE);
482 }
483
484 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
485 struct v4l2_format *f)
486 {
487 struct coda_dev *dev = ctx->dev;
488 unsigned int max_w, max_h;
489 enum v4l2_field field;
490
491 field = f->fmt.pix.field;
492 if (field == V4L2_FIELD_ANY)
493 field = V4L2_FIELD_NONE;
494 else if (V4L2_FIELD_NONE != field)
495 return -EINVAL;
496
497 /* V4L2 specification suggests the driver corrects the format struct
498 * if any of the dimensions is unsupported */
499 f->fmt.pix.field = field;
500
501 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
502 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
503 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
504 S_ALIGN);
505
506 switch (f->fmt.pix.pixelformat) {
507 case V4L2_PIX_FMT_YUV420:
508 case V4L2_PIX_FMT_YVU420:
509 case V4L2_PIX_FMT_NV12:
510 /*
511 * Frame stride must be at least multiple of 8,
512 * but multiple of 16 for h.264 or JPEG 4:2:x
513 */
514 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
515 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
516 f->fmt.pix.height * 3 / 2;
517 break;
518 case V4L2_PIX_FMT_YUV422P:
519 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
520 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
521 f->fmt.pix.height * 2;
522 break;
523 case V4L2_PIX_FMT_JPEG:
524 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
525 /* fallthrough */
526 case V4L2_PIX_FMT_H264:
527 case V4L2_PIX_FMT_MPEG4:
528 f->fmt.pix.bytesperline = 0;
529 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
530 f->fmt.pix.sizeimage,
531 f->fmt.pix.width,
532 f->fmt.pix.height);
533 break;
534 default:
535 BUG();
536 }
537
538 return 0;
539 }
540
541 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
542 struct v4l2_format *f)
543 {
544 struct coda_ctx *ctx = fh_to_ctx(priv);
545 const struct coda_q_data *q_data_src;
546 const struct coda_codec *codec;
547 struct vb2_queue *src_vq;
548 int ret;
549
550 ret = coda_try_pixelformat(ctx, f);
551 if (ret < 0)
552 return ret;
553
554 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
555
556 /*
557 * If the source format is already fixed, only allow the same output
558 * resolution
559 */
560 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
561 if (vb2_is_streaming(src_vq)) {
562 f->fmt.pix.width = q_data_src->width;
563 f->fmt.pix.height = q_data_src->height;
564 }
565
566 f->fmt.pix.colorspace = ctx->colorspace;
567
568 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
569 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
570 f->fmt.pix.pixelformat);
571 if (!codec)
572 return -EINVAL;
573
574 ret = coda_try_fmt(ctx, codec, f);
575 if (ret < 0)
576 return ret;
577
578 /* The h.264 decoder only returns complete 16x16 macroblocks */
579 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
580 f->fmt.pix.width = f->fmt.pix.width;
581 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
582 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
583 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
584 f->fmt.pix.height * 3 / 2;
585 }
586
587 return 0;
588 }
589
590 static int coda_try_fmt_vid_out(struct file *file, void *priv,
591 struct v4l2_format *f)
592 {
593 struct coda_ctx *ctx = fh_to_ctx(priv);
594 struct coda_dev *dev = ctx->dev;
595 const struct coda_q_data *q_data_dst;
596 const struct coda_codec *codec;
597 int ret;
598
599 ret = coda_try_pixelformat(ctx, f);
600 if (ret < 0)
601 return ret;
602
603 switch (f->fmt.pix.colorspace) {
604 case V4L2_COLORSPACE_REC709:
605 case V4L2_COLORSPACE_JPEG:
606 break;
607 default:
608 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
609 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
610 else
611 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
612 }
613
614 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
615 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
616
617 return coda_try_fmt(ctx, codec, f);
618 }
619
620 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
621 {
622 struct coda_q_data *q_data;
623 struct vb2_queue *vq;
624
625 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
626 if (!vq)
627 return -EINVAL;
628
629 q_data = get_q_data(ctx, f->type);
630 if (!q_data)
631 return -EINVAL;
632
633 if (vb2_is_busy(vq)) {
634 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
635 return -EBUSY;
636 }
637
638 q_data->fourcc = f->fmt.pix.pixelformat;
639 q_data->width = f->fmt.pix.width;
640 q_data->height = f->fmt.pix.height;
641 q_data->bytesperline = f->fmt.pix.bytesperline;
642 q_data->sizeimage = f->fmt.pix.sizeimage;
643 q_data->rect.left = 0;
644 q_data->rect.top = 0;
645 q_data->rect.width = f->fmt.pix.width;
646 q_data->rect.height = f->fmt.pix.height;
647
648 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
649 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
650 f->type, q_data->width, q_data->height, q_data->fourcc);
651
652 return 0;
653 }
654
655 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
656 struct v4l2_format *f)
657 {
658 struct coda_ctx *ctx = fh_to_ctx(priv);
659 int ret;
660
661 ret = coda_try_fmt_vid_cap(file, priv, f);
662 if (ret)
663 return ret;
664
665 return coda_s_fmt(ctx, f);
666 }
667
668 static int coda_s_fmt_vid_out(struct file *file, void *priv,
669 struct v4l2_format *f)
670 {
671 struct coda_ctx *ctx = fh_to_ctx(priv);
672 struct v4l2_format f_cap;
673 int ret;
674
675 ret = coda_try_fmt_vid_out(file, priv, f);
676 if (ret)
677 return ret;
678
679 ret = coda_s_fmt(ctx, f);
680 if (ret)
681 return ret;
682
683 ctx->colorspace = f->fmt.pix.colorspace;
684
685 memset(&f_cap, 0, sizeof(f_cap));
686 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
687 coda_g_fmt(file, priv, &f_cap);
688 f_cap.fmt.pix.width = f->fmt.pix.width;
689 f_cap.fmt.pix.height = f->fmt.pix.height;
690
691 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
692 if (ret)
693 return ret;
694
695 return coda_s_fmt(ctx, &f_cap);
696 }
697
698 static int coda_reqbufs(struct file *file, void *priv,
699 struct v4l2_requestbuffers *rb)
700 {
701 struct coda_ctx *ctx = fh_to_ctx(priv);
702 int ret;
703
704 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
705 if (ret)
706 return ret;
707
708 /*
709 * Allow to allocate instance specific per-context buffers, such as
710 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
711 */
712 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
713 return ctx->ops->reqbufs(ctx, rb);
714
715 return 0;
716 }
717
718 static int coda_qbuf(struct file *file, void *priv,
719 struct v4l2_buffer *buf)
720 {
721 struct coda_ctx *ctx = fh_to_ctx(priv);
722
723 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
724 }
725
726 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
727 struct vb2_buffer *buf)
728 {
729 struct vb2_queue *src_vq;
730
731 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
732
733 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
734 (buf->v4l2_buf.sequence == (ctx->qsequence - 1)));
735 }
736
737 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_buffer *buf,
738 enum vb2_buffer_state state)
739 {
740 const struct v4l2_event eos_event = {
741 .type = V4L2_EVENT_EOS
742 };
743
744 if (coda_buf_is_end_of_stream(ctx, buf)) {
745 buf->v4l2_buf.flags |= V4L2_BUF_FLAG_LAST;
746
747 v4l2_event_queue_fh(&ctx->fh, &eos_event);
748 }
749
750 v4l2_m2m_buf_done(buf, state);
751 }
752
753 static int coda_g_selection(struct file *file, void *fh,
754 struct v4l2_selection *s)
755 {
756 struct coda_ctx *ctx = fh_to_ctx(fh);
757 struct coda_q_data *q_data;
758 struct v4l2_rect r, *rsel;
759
760 q_data = get_q_data(ctx, s->type);
761 if (!q_data)
762 return -EINVAL;
763
764 r.left = 0;
765 r.top = 0;
766 r.width = q_data->width;
767 r.height = q_data->height;
768 rsel = &q_data->rect;
769
770 switch (s->target) {
771 case V4L2_SEL_TGT_CROP_DEFAULT:
772 case V4L2_SEL_TGT_CROP_BOUNDS:
773 rsel = &r;
774 /* fallthrough */
775 case V4L2_SEL_TGT_CROP:
776 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
777 return -EINVAL;
778 break;
779 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
780 case V4L2_SEL_TGT_COMPOSE_PADDED:
781 rsel = &r;
782 /* fallthrough */
783 case V4L2_SEL_TGT_COMPOSE:
784 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
785 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
786 return -EINVAL;
787 break;
788 default:
789 return -EINVAL;
790 }
791
792 s->r = *rsel;
793
794 return 0;
795 }
796
797 static int coda_try_decoder_cmd(struct file *file, void *fh,
798 struct v4l2_decoder_cmd *dc)
799 {
800 if (dc->cmd != V4L2_DEC_CMD_STOP)
801 return -EINVAL;
802
803 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
804 return -EINVAL;
805
806 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
807 return -EINVAL;
808
809 return 0;
810 }
811
812 static int coda_decoder_cmd(struct file *file, void *fh,
813 struct v4l2_decoder_cmd *dc)
814 {
815 struct coda_ctx *ctx = fh_to_ctx(fh);
816 int ret;
817
818 ret = coda_try_decoder_cmd(file, fh, dc);
819 if (ret < 0)
820 return ret;
821
822 /* Ignore decoder stop command silently in encoder context */
823 if (ctx->inst_type != CODA_INST_DECODER)
824 return 0;
825
826 /* Set the stream-end flag on this context */
827 coda_bit_stream_end_flag(ctx);
828 ctx->hold = false;
829 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
830
831 return 0;
832 }
833
834 static int coda_subscribe_event(struct v4l2_fh *fh,
835 const struct v4l2_event_subscription *sub)
836 {
837 switch (sub->type) {
838 case V4L2_EVENT_EOS:
839 return v4l2_event_subscribe(fh, sub, 0, NULL);
840 default:
841 return v4l2_ctrl_subscribe_event(fh, sub);
842 }
843 }
844
845 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
846 .vidioc_querycap = coda_querycap,
847
848 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
849 .vidioc_g_fmt_vid_cap = coda_g_fmt,
850 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
851 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
852
853 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
854 .vidioc_g_fmt_vid_out = coda_g_fmt,
855 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
856 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
857
858 .vidioc_reqbufs = coda_reqbufs,
859 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
860
861 .vidioc_qbuf = coda_qbuf,
862 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
863 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
864 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
865
866 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
867 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
868
869 .vidioc_g_selection = coda_g_selection,
870
871 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
872 .vidioc_decoder_cmd = coda_decoder_cmd,
873
874 .vidioc_subscribe_event = coda_subscribe_event,
875 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
876 };
877
878 void coda_set_gdi_regs(struct coda_ctx *ctx)
879 {
880 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
881 struct coda_dev *dev = ctx->dev;
882 int i;
883
884 for (i = 0; i < 16; i++)
885 coda_write(dev, tiled_map->xy2ca_map[i],
886 CODA9_GDI_XY2_CAS_0 + 4 * i);
887 for (i = 0; i < 4; i++)
888 coda_write(dev, tiled_map->xy2ba_map[i],
889 CODA9_GDI_XY2_BA_0 + 4 * i);
890 for (i = 0; i < 16; i++)
891 coda_write(dev, tiled_map->xy2ra_map[i],
892 CODA9_GDI_XY2_RAS_0 + 4 * i);
893 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
894 for (i = 0; i < 32; i++)
895 coda_write(dev, tiled_map->rbc2axi_map[i],
896 CODA9_GDI_RBC2_AXI_0 + 4 * i);
897 }
898
899 /*
900 * Mem-to-mem operations.
901 */
902
903 static void coda_device_run(void *m2m_priv)
904 {
905 struct coda_ctx *ctx = m2m_priv;
906 struct coda_dev *dev = ctx->dev;
907
908 queue_work(dev->workqueue, &ctx->pic_run_work);
909 }
910
911 static void coda_pic_run_work(struct work_struct *work)
912 {
913 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
914 struct coda_dev *dev = ctx->dev;
915 int ret;
916
917 mutex_lock(&ctx->buffer_mutex);
918 mutex_lock(&dev->coda_mutex);
919
920 ret = ctx->ops->prepare_run(ctx);
921 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
922 mutex_unlock(&dev->coda_mutex);
923 mutex_unlock(&ctx->buffer_mutex);
924 /* job_finish scheduled by prepare_decode */
925 return;
926 }
927
928 if (!wait_for_completion_timeout(&ctx->completion,
929 msecs_to_jiffies(1000))) {
930 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
931
932 ctx->hold = true;
933
934 coda_hw_reset(ctx);
935 } else if (!ctx->aborting) {
936 ctx->ops->finish_run(ctx);
937 }
938
939 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
940 ctx->ops->seq_end_work)
941 queue_work(dev->workqueue, &ctx->seq_end_work);
942
943 mutex_unlock(&dev->coda_mutex);
944 mutex_unlock(&ctx->buffer_mutex);
945
946 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
947 }
948
949 static int coda_job_ready(void *m2m_priv)
950 {
951 struct coda_ctx *ctx = m2m_priv;
952
953 /*
954 * For both 'P' and 'key' frame cases 1 picture
955 * and 1 frame are needed. In the decoder case,
956 * the compressed frame can be in the bitstream.
957 */
958 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
959 ctx->inst_type != CODA_INST_DECODER) {
960 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
961 "not ready: not enough video buffers.\n");
962 return 0;
963 }
964
965 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
966 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
967 "not ready: not enough video capture buffers.\n");
968 return 0;
969 }
970
971 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
972 struct list_head *meta;
973 bool stream_end;
974 int num_metas;
975 int src_bufs;
976
977 if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
978 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
979 "%d: not ready: on hold for more buffers.\n",
980 ctx->idx);
981 return 0;
982 }
983
984 stream_end = ctx->bit_stream_param &
985 CODA_BIT_STREAM_END_FLAG;
986
987 num_metas = 0;
988 list_for_each(meta, &ctx->buffer_meta_list)
989 num_metas++;
990
991 src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
992
993 if (!stream_end && (num_metas + src_bufs) < 2) {
994 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
995 "%d: not ready: need 2 buffers available (%d, %d)\n",
996 ctx->idx, num_metas, src_bufs);
997 return 0;
998 }
999
1000
1001 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
1002 !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
1003 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1004 "%d: not ready: not enough bitstream data (%d).\n",
1005 ctx->idx, coda_get_bitstream_payload(ctx));
1006 return 0;
1007 }
1008 }
1009
1010 if (ctx->aborting) {
1011 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1012 "not ready: aborting\n");
1013 return 0;
1014 }
1015
1016 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1017 "job ready\n");
1018 return 1;
1019 }
1020
1021 static void coda_job_abort(void *priv)
1022 {
1023 struct coda_ctx *ctx = priv;
1024
1025 ctx->aborting = 1;
1026
1027 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1028 "Aborting task\n");
1029 }
1030
1031 static void coda_lock(void *m2m_priv)
1032 {
1033 struct coda_ctx *ctx = m2m_priv;
1034 struct coda_dev *pcdev = ctx->dev;
1035
1036 mutex_lock(&pcdev->dev_mutex);
1037 }
1038
1039 static void coda_unlock(void *m2m_priv)
1040 {
1041 struct coda_ctx *ctx = m2m_priv;
1042 struct coda_dev *pcdev = ctx->dev;
1043
1044 mutex_unlock(&pcdev->dev_mutex);
1045 }
1046
1047 static const struct v4l2_m2m_ops coda_m2m_ops = {
1048 .device_run = coda_device_run,
1049 .job_ready = coda_job_ready,
1050 .job_abort = coda_job_abort,
1051 .lock = coda_lock,
1052 .unlock = coda_unlock,
1053 };
1054
1055 static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1056 {
1057 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1058 int luma_map, chro_map, i;
1059
1060 memset(tiled_map, 0, sizeof(*tiled_map));
1061
1062 luma_map = 64;
1063 chro_map = 64;
1064 tiled_map->map_type = tiled_map_type;
1065 for (i = 0; i < 16; i++)
1066 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1067 for (i = 0; i < 4; i++)
1068 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1069 for (i = 0; i < 16; i++)
1070 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1071
1072 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1073 tiled_map->xy2rbc_config = 0;
1074 } else {
1075 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1076 tiled_map_type);
1077 return;
1078 }
1079 }
1080
1081 static void set_default_params(struct coda_ctx *ctx)
1082 {
1083 unsigned int max_w, max_h, usize, csize;
1084
1085 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1086 ctx->cvd->dst_formats[0]);
1087 max_w = min(ctx->codec->max_w, 1920U);
1088 max_h = min(ctx->codec->max_h, 1088U);
1089 usize = max_w * max_h * 3 / 2;
1090 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1091
1092 ctx->params.codec_mode = ctx->codec->mode;
1093 ctx->colorspace = V4L2_COLORSPACE_REC709;
1094 ctx->params.framerate = 30;
1095
1096 /* Default formats for output and input queues */
1097 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1098 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1099 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1100 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1101 ctx->q_data[V4L2_M2M_DST].width = max_w;
1102 ctx->q_data[V4L2_M2M_DST].height = max_h;
1103 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1104 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1105 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1106 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1107 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1108 } else {
1109 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1110 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1111 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1112 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1113 }
1114 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1115 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1116 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1117 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1118
1119 if (ctx->dev->devtype->product == CODA_960)
1120 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
1121 }
1122
1123 /*
1124 * Queue operations
1125 */
1126 static int coda_queue_setup(struct vb2_queue *vq,
1127 const struct v4l2_format *fmt,
1128 unsigned int *nbuffers, unsigned int *nplanes,
1129 unsigned int sizes[], void *alloc_ctxs[])
1130 {
1131 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1132 struct coda_q_data *q_data;
1133 unsigned int size;
1134
1135 q_data = get_q_data(ctx, vq->type);
1136 size = q_data->sizeimage;
1137
1138 *nplanes = 1;
1139 sizes[0] = size;
1140
1141 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1142 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1143
1144 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1145 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1146
1147 return 0;
1148 }
1149
1150 static int coda_buf_prepare(struct vb2_buffer *vb)
1151 {
1152 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1153 struct coda_q_data *q_data;
1154
1155 q_data = get_q_data(ctx, vb->vb2_queue->type);
1156
1157 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1158 v4l2_warn(&ctx->dev->v4l2_dev,
1159 "%s data will not fit into plane (%lu < %lu)\n",
1160 __func__, vb2_plane_size(vb, 0),
1161 (long)q_data->sizeimage);
1162 return -EINVAL;
1163 }
1164
1165 return 0;
1166 }
1167
1168 static void coda_buf_queue(struct vb2_buffer *vb)
1169 {
1170 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1171 struct vb2_queue *vq = vb->vb2_queue;
1172 struct coda_q_data *q_data;
1173
1174 q_data = get_q_data(ctx, vb->vb2_queue->type);
1175
1176 /*
1177 * In the decoder case, immediately try to copy the buffer into the
1178 * bitstream ringbuffer and mark it as ready to be dequeued.
1179 */
1180 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1181 /*
1182 * For backwards compatibility, queuing an empty buffer marks
1183 * the stream end
1184 */
1185 if (vb2_get_plane_payload(vb, 0) == 0)
1186 coda_bit_stream_end_flag(ctx);
1187 mutex_lock(&ctx->bitstream_mutex);
1188 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1189 if (vb2_is_streaming(vb->vb2_queue))
1190 coda_fill_bitstream(ctx, true);
1191 mutex_unlock(&ctx->bitstream_mutex);
1192 } else {
1193 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1194 }
1195 }
1196
1197 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1198 size_t size, const char *name, struct dentry *parent)
1199 {
1200 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1201 GFP_KERNEL);
1202 if (!buf->vaddr) {
1203 v4l2_err(&dev->v4l2_dev,
1204 "Failed to allocate %s buffer of size %u\n",
1205 name, size);
1206 return -ENOMEM;
1207 }
1208
1209 buf->size = size;
1210
1211 if (name && parent) {
1212 buf->blob.data = buf->vaddr;
1213 buf->blob.size = size;
1214 buf->dentry = debugfs_create_blob(name, 0644, parent,
1215 &buf->blob);
1216 if (!buf->dentry)
1217 dev_warn(&dev->plat_dev->dev,
1218 "failed to create debugfs entry %s\n", name);
1219 }
1220
1221 return 0;
1222 }
1223
1224 void coda_free_aux_buf(struct coda_dev *dev,
1225 struct coda_aux_buf *buf)
1226 {
1227 if (buf->vaddr) {
1228 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1229 buf->vaddr, buf->paddr);
1230 buf->vaddr = NULL;
1231 buf->size = 0;
1232 debugfs_remove(buf->dentry);
1233 buf->dentry = NULL;
1234 }
1235 }
1236
1237 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1238 {
1239 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1240 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1241 struct coda_q_data *q_data_src, *q_data_dst;
1242 struct vb2_buffer *buf;
1243 int ret = 0;
1244
1245 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1246 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1247 if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1248 (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1249 ctx->dev->devtype->product == CODA_7541)) {
1250 /* copy the buffers that were queued before streamon */
1251 mutex_lock(&ctx->bitstream_mutex);
1252 coda_fill_bitstream(ctx, false);
1253 mutex_unlock(&ctx->bitstream_mutex);
1254
1255 if (coda_get_bitstream_payload(ctx) < 512) {
1256 ret = -EINVAL;
1257 goto err;
1258 }
1259 } else {
1260 if (count < 1) {
1261 ret = -EINVAL;
1262 goto err;
1263 }
1264 }
1265
1266 ctx->streamon_out = 1;
1267 } else {
1268 if (count < 1) {
1269 ret = -EINVAL;
1270 goto err;
1271 }
1272
1273 ctx->streamon_cap = 1;
1274 }
1275
1276 /* Don't start the coda unless both queues are on */
1277 if (!(ctx->streamon_out & ctx->streamon_cap))
1278 return 0;
1279
1280 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1281 if ((q_data_src->width != q_data_dst->width &&
1282 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1283 (q_data_src->height != q_data_dst->height &&
1284 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1285 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1286 q_data_src->width, q_data_src->height,
1287 q_data_dst->width, q_data_dst->height);
1288 ret = -EINVAL;
1289 goto err;
1290 }
1291
1292 /* Allow BIT decoder device_run with no new buffers queued */
1293 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1294 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1295
1296 ctx->gopcounter = ctx->params.gop_size - 1;
1297
1298 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1299 q_data_dst->fourcc);
1300 if (!ctx->codec) {
1301 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1302 ret = -EINVAL;
1303 goto err;
1304 }
1305
1306 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1307 ctx->params.gop_size = 1;
1308 ctx->gopcounter = ctx->params.gop_size - 1;
1309
1310 ret = ctx->ops->start_streaming(ctx);
1311 if (ctx->inst_type == CODA_INST_DECODER) {
1312 if (ret == -EAGAIN)
1313 return 0;
1314 else if (ret < 0)
1315 goto err;
1316 }
1317
1318 ctx->initialized = 1;
1319 return ret;
1320
1321 err:
1322 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1323 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1324 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1325 } else {
1326 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1327 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1328 }
1329 return ret;
1330 }
1331
1332 static void coda_stop_streaming(struct vb2_queue *q)
1333 {
1334 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1335 struct coda_dev *dev = ctx->dev;
1336 struct vb2_buffer *buf;
1337 bool stop;
1338
1339 stop = ctx->streamon_out && ctx->streamon_cap;
1340
1341 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1342 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1343 "%s: output\n", __func__);
1344 ctx->streamon_out = 0;
1345
1346 coda_bit_stream_end_flag(ctx);
1347
1348 ctx->qsequence = 0;
1349
1350 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1351 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1352 } else {
1353 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1354 "%s: capture\n", __func__);
1355 ctx->streamon_cap = 0;
1356
1357 ctx->osequence = 0;
1358 ctx->sequence_offset = 0;
1359
1360 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1361 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1362 }
1363
1364 if (stop) {
1365 struct coda_buffer_meta *meta;
1366
1367 if (ctx->ops->seq_end_work) {
1368 queue_work(dev->workqueue, &ctx->seq_end_work);
1369 flush_work(&ctx->seq_end_work);
1370 }
1371 mutex_lock(&ctx->bitstream_mutex);
1372 while (!list_empty(&ctx->buffer_meta_list)) {
1373 meta = list_first_entry(&ctx->buffer_meta_list,
1374 struct coda_buffer_meta, list);
1375 list_del(&meta->list);
1376 kfree(meta);
1377 }
1378 mutex_unlock(&ctx->bitstream_mutex);
1379 kfifo_init(&ctx->bitstream_fifo,
1380 ctx->bitstream.vaddr, ctx->bitstream.size);
1381 ctx->initialized = 0;
1382 ctx->runcounter = 0;
1383 ctx->aborting = 0;
1384 }
1385 }
1386
1387 static const struct vb2_ops coda_qops = {
1388 .queue_setup = coda_queue_setup,
1389 .buf_prepare = coda_buf_prepare,
1390 .buf_queue = coda_buf_queue,
1391 .start_streaming = coda_start_streaming,
1392 .stop_streaming = coda_stop_streaming,
1393 .wait_prepare = vb2_ops_wait_prepare,
1394 .wait_finish = vb2_ops_wait_finish,
1395 };
1396
1397 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1398 {
1399 struct coda_ctx *ctx =
1400 container_of(ctrl->handler, struct coda_ctx, ctrls);
1401
1402 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1403 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1404
1405 switch (ctrl->id) {
1406 case V4L2_CID_HFLIP:
1407 if (ctrl->val)
1408 ctx->params.rot_mode |= CODA_MIR_HOR;
1409 else
1410 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1411 break;
1412 case V4L2_CID_VFLIP:
1413 if (ctrl->val)
1414 ctx->params.rot_mode |= CODA_MIR_VER;
1415 else
1416 ctx->params.rot_mode &= ~CODA_MIR_VER;
1417 break;
1418 case V4L2_CID_MPEG_VIDEO_BITRATE:
1419 ctx->params.bitrate = ctrl->val / 1000;
1420 break;
1421 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1422 ctx->params.gop_size = ctrl->val;
1423 break;
1424 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1425 ctx->params.h264_intra_qp = ctrl->val;
1426 break;
1427 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1428 ctx->params.h264_inter_qp = ctrl->val;
1429 break;
1430 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1431 ctx->params.h264_min_qp = ctrl->val;
1432 break;
1433 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1434 ctx->params.h264_max_qp = ctrl->val;
1435 break;
1436 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1437 ctx->params.h264_deblk_alpha = ctrl->val;
1438 break;
1439 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1440 ctx->params.h264_deblk_beta = ctrl->val;
1441 break;
1442 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1443 ctx->params.h264_deblk_enabled = (ctrl->val ==
1444 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1445 break;
1446 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1447 ctx->params.mpeg4_intra_qp = ctrl->val;
1448 break;
1449 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1450 ctx->params.mpeg4_inter_qp = ctrl->val;
1451 break;
1452 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1453 ctx->params.slice_mode = ctrl->val;
1454 break;
1455 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1456 ctx->params.slice_max_mb = ctrl->val;
1457 break;
1458 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1459 ctx->params.slice_max_bits = ctrl->val * 8;
1460 break;
1461 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1462 break;
1463 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1464 ctx->params.intra_refresh = ctrl->val;
1465 break;
1466 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1467 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1468 break;
1469 case V4L2_CID_JPEG_RESTART_INTERVAL:
1470 ctx->params.jpeg_restart_interval = ctrl->val;
1471 break;
1472 default:
1473 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1474 "Invalid control, id=%d, val=%d\n",
1475 ctrl->id, ctrl->val);
1476 return -EINVAL;
1477 }
1478
1479 return 0;
1480 }
1481
1482 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1483 .s_ctrl = coda_s_ctrl,
1484 };
1485
1486 static void coda_encode_ctrls(struct coda_ctx *ctx)
1487 {
1488 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1489 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1490 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1491 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1492 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1493 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1494 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1495 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1496 if (ctx->dev->devtype->product != CODA_960) {
1497 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1498 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1499 }
1500 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1501 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1502 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1503 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1504 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1505 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1506 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1507 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1508 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1509 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1510 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1511 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1512 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1513 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1514 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1515 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1516 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1517 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1518 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1519 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1520 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1521 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1522 500);
1523 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1524 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1525 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1526 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1527 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1528 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1529 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1530 1920 * 1088 / 256, 1, 0);
1531 }
1532
1533 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1534 {
1535 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1536 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1537 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1538 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1539 }
1540
1541 static int coda_ctrls_setup(struct coda_ctx *ctx)
1542 {
1543 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1544
1545 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1546 V4L2_CID_HFLIP, 0, 1, 1, 0);
1547 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1548 V4L2_CID_VFLIP, 0, 1, 1, 0);
1549 if (ctx->inst_type == CODA_INST_ENCODER) {
1550 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1551 coda_jpeg_encode_ctrls(ctx);
1552 else
1553 coda_encode_ctrls(ctx);
1554 }
1555
1556 if (ctx->ctrls.error) {
1557 v4l2_err(&ctx->dev->v4l2_dev,
1558 "control initialization error (%d)",
1559 ctx->ctrls.error);
1560 return -EINVAL;
1561 }
1562
1563 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1564 }
1565
1566 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1567 {
1568 vq->drv_priv = ctx;
1569 vq->ops = &coda_qops;
1570 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1571 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1572 vq->lock = &ctx->dev->dev_mutex;
1573 /* One way to indicate end-of-stream for coda is to set the
1574 * bytesused == 0. However by default videobuf2 handles bytesused
1575 * equal to 0 as a special case and changes its value to the size
1576 * of the buffer. Set the allow_zero_bytesused flag, so
1577 * that videobuf2 will keep the value of bytesused intact.
1578 */
1579 vq->allow_zero_bytesused = 1;
1580
1581 return vb2_queue_init(vq);
1582 }
1583
1584 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1585 struct vb2_queue *dst_vq)
1586 {
1587 int ret;
1588
1589 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1590 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1591 src_vq->mem_ops = &vb2_dma_contig_memops;
1592
1593 ret = coda_queue_init(priv, src_vq);
1594 if (ret)
1595 return ret;
1596
1597 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1598 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1599 dst_vq->mem_ops = &vb2_dma_contig_memops;
1600
1601 return coda_queue_init(priv, dst_vq);
1602 }
1603
1604 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1605 struct vb2_queue *dst_vq)
1606 {
1607 int ret;
1608
1609 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1610 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1611 src_vq->mem_ops = &vb2_vmalloc_memops;
1612
1613 ret = coda_queue_init(priv, src_vq);
1614 if (ret)
1615 return ret;
1616
1617 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1618 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1619 dst_vq->mem_ops = &vb2_dma_contig_memops;
1620
1621 return coda_queue_init(priv, dst_vq);
1622 }
1623
1624 static int coda_next_free_instance(struct coda_dev *dev)
1625 {
1626 int idx = ffz(dev->instance_mask);
1627
1628 if ((idx < 0) ||
1629 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1630 return -EBUSY;
1631
1632 return idx;
1633 }
1634
1635 /*
1636 * File operations
1637 */
1638
1639 static int coda_open(struct file *file)
1640 {
1641 struct video_device *vdev = video_devdata(file);
1642 struct coda_dev *dev = video_get_drvdata(vdev);
1643 struct coda_ctx *ctx = NULL;
1644 char *name;
1645 int ret;
1646 int idx;
1647
1648 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1649 if (!ctx)
1650 return -ENOMEM;
1651
1652 idx = coda_next_free_instance(dev);
1653 if (idx < 0) {
1654 ret = idx;
1655 goto err_coda_max;
1656 }
1657 set_bit(idx, &dev->instance_mask);
1658
1659 name = kasprintf(GFP_KERNEL, "context%d", idx);
1660 if (!name) {
1661 ret = -ENOMEM;
1662 goto err_coda_name_init;
1663 }
1664
1665 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1666 kfree(name);
1667
1668 ctx->cvd = to_coda_video_device(vdev);
1669 ctx->inst_type = ctx->cvd->type;
1670 ctx->ops = ctx->cvd->ops;
1671 ctx->use_bit = !ctx->cvd->direct;
1672 init_completion(&ctx->completion);
1673 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1674 if (ctx->ops->seq_end_work)
1675 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1676 v4l2_fh_init(&ctx->fh, video_devdata(file));
1677 file->private_data = &ctx->fh;
1678 v4l2_fh_add(&ctx->fh);
1679 ctx->dev = dev;
1680 ctx->idx = idx;
1681 switch (dev->devtype->product) {
1682 case CODA_960:
1683 ctx->frame_mem_ctrl = 1 << 12;
1684 /* fallthrough */
1685 case CODA_7541:
1686 ctx->reg_idx = 0;
1687 break;
1688 default:
1689 ctx->reg_idx = idx;
1690 }
1691
1692 /* Power up and upload firmware if necessary */
1693 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1694 if (ret < 0) {
1695 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1696 goto err_pm_get;
1697 }
1698
1699 ret = clk_prepare_enable(dev->clk_per);
1700 if (ret)
1701 goto err_clk_per;
1702
1703 ret = clk_prepare_enable(dev->clk_ahb);
1704 if (ret)
1705 goto err_clk_ahb;
1706
1707 set_default_params(ctx);
1708 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1709 ctx->ops->queue_init);
1710 if (IS_ERR(ctx->fh.m2m_ctx)) {
1711 ret = PTR_ERR(ctx->fh.m2m_ctx);
1712
1713 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1714 __func__, ret);
1715 goto err_ctx_init;
1716 }
1717
1718 ret = coda_ctrls_setup(ctx);
1719 if (ret) {
1720 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1721 goto err_ctrls_setup;
1722 }
1723
1724 ctx->fh.ctrl_handler = &ctx->ctrls;
1725
1726 mutex_init(&ctx->bitstream_mutex);
1727 mutex_init(&ctx->buffer_mutex);
1728 INIT_LIST_HEAD(&ctx->buffer_meta_list);
1729
1730 coda_lock(ctx);
1731 list_add(&ctx->list, &dev->instances);
1732 coda_unlock(ctx);
1733
1734 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1735 ctx->idx, ctx);
1736
1737 return 0;
1738
1739 err_ctrls_setup:
1740 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1741 err_ctx_init:
1742 clk_disable_unprepare(dev->clk_ahb);
1743 err_clk_ahb:
1744 clk_disable_unprepare(dev->clk_per);
1745 err_clk_per:
1746 pm_runtime_put_sync(&dev->plat_dev->dev);
1747 err_pm_get:
1748 v4l2_fh_del(&ctx->fh);
1749 v4l2_fh_exit(&ctx->fh);
1750 clear_bit(ctx->idx, &dev->instance_mask);
1751 err_coda_name_init:
1752 err_coda_max:
1753 kfree(ctx);
1754 return ret;
1755 }
1756
1757 static int coda_release(struct file *file)
1758 {
1759 struct coda_dev *dev = video_drvdata(file);
1760 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1761
1762 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1763 ctx);
1764
1765 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1766 coda_bit_stream_end_flag(ctx);
1767
1768 /* If this instance is running, call .job_abort and wait for it to end */
1769 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1770
1771 /* In case the instance was not running, we still need to call SEQ_END */
1772 if (ctx->initialized && ctx->ops->seq_end_work) {
1773 queue_work(dev->workqueue, &ctx->seq_end_work);
1774 flush_work(&ctx->seq_end_work);
1775 }
1776
1777 coda_lock(ctx);
1778 list_del(&ctx->list);
1779 coda_unlock(ctx);
1780
1781 if (ctx->dev->devtype->product == CODA_DX6)
1782 coda_free_aux_buf(dev, &ctx->workbuf);
1783
1784 v4l2_ctrl_handler_free(&ctx->ctrls);
1785 clk_disable_unprepare(dev->clk_ahb);
1786 clk_disable_unprepare(dev->clk_per);
1787 pm_runtime_put_sync(&dev->plat_dev->dev);
1788 v4l2_fh_del(&ctx->fh);
1789 v4l2_fh_exit(&ctx->fh);
1790 clear_bit(ctx->idx, &dev->instance_mask);
1791 if (ctx->ops->release)
1792 ctx->ops->release(ctx);
1793 debugfs_remove_recursive(ctx->debugfs_entry);
1794 kfree(ctx);
1795
1796 return 0;
1797 }
1798
1799 static const struct v4l2_file_operations coda_fops = {
1800 .owner = THIS_MODULE,
1801 .open = coda_open,
1802 .release = coda_release,
1803 .poll = v4l2_m2m_fop_poll,
1804 .unlocked_ioctl = video_ioctl2,
1805 .mmap = v4l2_m2m_fop_mmap,
1806 };
1807
1808 static int coda_hw_init(struct coda_dev *dev)
1809 {
1810 u32 data;
1811 u16 *p;
1812 int i, ret;
1813
1814 ret = clk_prepare_enable(dev->clk_per);
1815 if (ret)
1816 goto err_clk_per;
1817
1818 ret = clk_prepare_enable(dev->clk_ahb);
1819 if (ret)
1820 goto err_clk_ahb;
1821
1822 if (dev->rstc)
1823 reset_control_reset(dev->rstc);
1824
1825 /*
1826 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1827 * The 16-bit chars in the code buffer are in memory access
1828 * order, re-sort them to CODA order for register download.
1829 * Data in this SRAM survives a reboot.
1830 */
1831 p = (u16 *)dev->codebuf.vaddr;
1832 if (dev->devtype->product == CODA_DX6) {
1833 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1834 data = CODA_DOWN_ADDRESS_SET(i) |
1835 CODA_DOWN_DATA_SET(p[i ^ 1]);
1836 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1837 }
1838 } else {
1839 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1840 data = CODA_DOWN_ADDRESS_SET(i) |
1841 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1842 3 - (i % 4)]);
1843 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1844 }
1845 }
1846
1847 /* Clear registers */
1848 for (i = 0; i < 64; i++)
1849 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1850
1851 /* Tell the BIT where to find everything it needs */
1852 if (dev->devtype->product == CODA_960 ||
1853 dev->devtype->product == CODA_7541) {
1854 coda_write(dev, dev->tempbuf.paddr,
1855 CODA_REG_BIT_TEMP_BUF_ADDR);
1856 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1857 } else {
1858 coda_write(dev, dev->workbuf.paddr,
1859 CODA_REG_BIT_WORK_BUF_ADDR);
1860 }
1861 coda_write(dev, dev->codebuf.paddr,
1862 CODA_REG_BIT_CODE_BUF_ADDR);
1863 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1864
1865 /* Set default values */
1866 switch (dev->devtype->product) {
1867 case CODA_DX6:
1868 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1869 CODA_REG_BIT_STREAM_CTRL);
1870 break;
1871 default:
1872 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1873 CODA_REG_BIT_STREAM_CTRL);
1874 }
1875 if (dev->devtype->product == CODA_960)
1876 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1877 else
1878 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1879
1880 if (dev->devtype->product != CODA_DX6)
1881 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1882
1883 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1884 CODA_REG_BIT_INT_ENABLE);
1885
1886 /* Reset VPU and start processor */
1887 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1888 data |= CODA_REG_RESET_ENABLE;
1889 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1890 udelay(10);
1891 data &= ~CODA_REG_RESET_ENABLE;
1892 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1893 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1894
1895 clk_disable_unprepare(dev->clk_ahb);
1896 clk_disable_unprepare(dev->clk_per);
1897
1898 return 0;
1899
1900 err_clk_ahb:
1901 clk_disable_unprepare(dev->clk_per);
1902 err_clk_per:
1903 return ret;
1904 }
1905
1906 static int coda_register_device(struct coda_dev *dev, int i)
1907 {
1908 struct video_device *vfd = &dev->vfd[i];
1909
1910 if (i >= dev->devtype->num_vdevs)
1911 return -EINVAL;
1912
1913 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
1914 vfd->fops = &coda_fops;
1915 vfd->ioctl_ops = &coda_ioctl_ops;
1916 vfd->release = video_device_release_empty,
1917 vfd->lock = &dev->dev_mutex;
1918 vfd->v4l2_dev = &dev->v4l2_dev;
1919 vfd->vfl_dir = VFL_DIR_M2M;
1920 video_set_drvdata(vfd, dev);
1921
1922 /* Not applicable, use the selection API instead */
1923 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1924 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1925 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1926
1927 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1928 }
1929
1930 static void coda_fw_callback(const struct firmware *fw, void *context)
1931 {
1932 struct coda_dev *dev = context;
1933 struct platform_device *pdev = dev->plat_dev;
1934 int i, ret;
1935
1936 if (!fw) {
1937 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1938 goto put_pm;
1939 }
1940
1941 /* allocate auxiliary per-device code buffer for the BIT processor */
1942 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1943 dev->debugfs_root);
1944 if (ret < 0)
1945 goto put_pm;
1946
1947 /* Copy the whole firmware image to the code buffer */
1948 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1949 release_firmware(fw);
1950
1951 ret = coda_hw_init(dev);
1952 if (ret < 0) {
1953 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1954 goto put_pm;
1955 }
1956
1957 ret = coda_check_firmware(dev);
1958 if (ret < 0)
1959 goto put_pm;
1960
1961 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1962 if (IS_ERR(dev->alloc_ctx)) {
1963 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1964 goto put_pm;
1965 }
1966
1967 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1968 if (IS_ERR(dev->m2m_dev)) {
1969 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1970 goto rel_ctx;
1971 }
1972
1973 for (i = 0; i < dev->devtype->num_vdevs; i++) {
1974 ret = coda_register_device(dev, i);
1975 if (ret) {
1976 v4l2_err(&dev->v4l2_dev,
1977 "Failed to register %s video device: %d\n",
1978 dev->devtype->vdevs[i]->name, ret);
1979 goto rel_vfd;
1980 }
1981 }
1982
1983 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1984 dev->vfd[0].num, dev->vfd[i - 1].num);
1985
1986 pm_runtime_put_sync(&pdev->dev);
1987 return;
1988
1989 rel_vfd:
1990 while (--i >= 0)
1991 video_unregister_device(&dev->vfd[i]);
1992 v4l2_m2m_release(dev->m2m_dev);
1993 rel_ctx:
1994 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1995 put_pm:
1996 pm_runtime_put_sync(&pdev->dev);
1997 }
1998
1999 static int coda_firmware_request(struct coda_dev *dev)
2000 {
2001 char *fw = dev->devtype->firmware;
2002
2003 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2004 coda_product_name(dev->devtype->product));
2005
2006 return request_firmware_nowait(THIS_MODULE, true,
2007 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2008 }
2009
2010 enum coda_platform {
2011 CODA_IMX27,
2012 CODA_IMX53,
2013 CODA_IMX6Q,
2014 CODA_IMX6DL,
2015 };
2016
2017 static const struct coda_devtype coda_devdata[] = {
2018 [CODA_IMX27] = {
2019 .firmware = "v4l-codadx6-imx27.bin",
2020 .product = CODA_DX6,
2021 .codecs = codadx6_codecs,
2022 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2023 .vdevs = codadx6_video_devices,
2024 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2025 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2026 .iram_size = 0xb000,
2027 },
2028 [CODA_IMX53] = {
2029 .firmware = "v4l-coda7541-imx53.bin",
2030 .product = CODA_7541,
2031 .codecs = coda7_codecs,
2032 .num_codecs = ARRAY_SIZE(coda7_codecs),
2033 .vdevs = coda7_video_devices,
2034 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2035 .workbuf_size = 128 * 1024,
2036 .tempbuf_size = 304 * 1024,
2037 .iram_size = 0x14000,
2038 },
2039 [CODA_IMX6Q] = {
2040 .firmware = "v4l-coda960-imx6q.bin",
2041 .product = CODA_960,
2042 .codecs = coda9_codecs,
2043 .num_codecs = ARRAY_SIZE(coda9_codecs),
2044 .vdevs = coda9_video_devices,
2045 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2046 .workbuf_size = 80 * 1024,
2047 .tempbuf_size = 204 * 1024,
2048 .iram_size = 0x21000,
2049 },
2050 [CODA_IMX6DL] = {
2051 .firmware = "v4l-coda960-imx6dl.bin",
2052 .product = CODA_960,
2053 .codecs = coda9_codecs,
2054 .num_codecs = ARRAY_SIZE(coda9_codecs),
2055 .vdevs = coda9_video_devices,
2056 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2057 .workbuf_size = 80 * 1024,
2058 .tempbuf_size = 204 * 1024,
2059 .iram_size = 0x20000,
2060 },
2061 };
2062
2063 static struct platform_device_id coda_platform_ids[] = {
2064 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2065 { /* sentinel */ }
2066 };
2067 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2068
2069 #ifdef CONFIG_OF
2070 static const struct of_device_id coda_dt_ids[] = {
2071 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2072 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2073 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2074 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2075 { /* sentinel */ }
2076 };
2077 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2078 #endif
2079
2080 static int coda_probe(struct platform_device *pdev)
2081 {
2082 const struct of_device_id *of_id =
2083 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2084 const struct platform_device_id *pdev_id;
2085 struct coda_platform_data *pdata = pdev->dev.platform_data;
2086 struct device_node *np = pdev->dev.of_node;
2087 struct gen_pool *pool;
2088 struct coda_dev *dev;
2089 struct resource *res;
2090 int ret, irq;
2091
2092 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2093 if (!dev)
2094 return -ENOMEM;
2095
2096 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2097
2098 if (of_id) {
2099 dev->devtype = of_id->data;
2100 } else if (pdev_id) {
2101 dev->devtype = &coda_devdata[pdev_id->driver_data];
2102 } else {
2103 ret = -EINVAL;
2104 goto err_v4l2_register;
2105 }
2106
2107 spin_lock_init(&dev->irqlock);
2108 INIT_LIST_HEAD(&dev->instances);
2109
2110 dev->plat_dev = pdev;
2111 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2112 if (IS_ERR(dev->clk_per)) {
2113 dev_err(&pdev->dev, "Could not get per clock\n");
2114 return PTR_ERR(dev->clk_per);
2115 }
2116
2117 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2118 if (IS_ERR(dev->clk_ahb)) {
2119 dev_err(&pdev->dev, "Could not get ahb clock\n");
2120 return PTR_ERR(dev->clk_ahb);
2121 }
2122
2123 /* Get memory for physical registers */
2124 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2125 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2126 if (IS_ERR(dev->regs_base))
2127 return PTR_ERR(dev->regs_base);
2128
2129 /* IRQ */
2130 irq = platform_get_irq_byname(pdev, "bit");
2131 if (irq < 0)
2132 irq = platform_get_irq(pdev, 0);
2133 if (irq < 0) {
2134 dev_err(&pdev->dev, "failed to get irq resource\n");
2135 return irq;
2136 }
2137
2138 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2139 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2140 if (ret < 0) {
2141 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2142 return ret;
2143 }
2144
2145 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2146 if (IS_ERR(dev->rstc)) {
2147 ret = PTR_ERR(dev->rstc);
2148 if (ret == -ENOENT || ret == -ENOSYS) {
2149 dev->rstc = NULL;
2150 } else {
2151 dev_err(&pdev->dev, "failed get reset control: %d\n",
2152 ret);
2153 return ret;
2154 }
2155 }
2156
2157 /* Get IRAM pool from device tree or platform data */
2158 pool = of_gen_pool_get(np, "iram", 0);
2159 if (!pool && pdata)
2160 pool = gen_pool_get(pdata->iram_dev);
2161 if (!pool) {
2162 dev_err(&pdev->dev, "iram pool not available\n");
2163 return -ENOMEM;
2164 }
2165 dev->iram_pool = pool;
2166
2167 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2168 if (ret)
2169 return ret;
2170
2171 mutex_init(&dev->dev_mutex);
2172 mutex_init(&dev->coda_mutex);
2173
2174 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2175 if (!dev->debugfs_root)
2176 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2177
2178 /* allocate auxiliary per-device buffers for the BIT processor */
2179 if (dev->devtype->product == CODA_DX6) {
2180 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2181 dev->devtype->workbuf_size, "workbuf",
2182 dev->debugfs_root);
2183 if (ret < 0)
2184 goto err_v4l2_register;
2185 }
2186
2187 if (dev->devtype->tempbuf_size) {
2188 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2189 dev->devtype->tempbuf_size, "tempbuf",
2190 dev->debugfs_root);
2191 if (ret < 0)
2192 goto err_v4l2_register;
2193 }
2194
2195 dev->iram.size = dev->devtype->iram_size;
2196 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2197 &dev->iram.paddr);
2198 if (!dev->iram.vaddr) {
2199 dev_warn(&pdev->dev, "unable to alloc iram\n");
2200 } else {
2201 memset(dev->iram.vaddr, 0, dev->iram.size);
2202 dev->iram.blob.data = dev->iram.vaddr;
2203 dev->iram.blob.size = dev->iram.size;
2204 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2205 dev->debugfs_root,
2206 &dev->iram.blob);
2207 }
2208
2209 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2210 if (!dev->workqueue) {
2211 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2212 ret = -ENOMEM;
2213 goto err_v4l2_register;
2214 }
2215
2216 platform_set_drvdata(pdev, dev);
2217
2218 /*
2219 * Start activated so we can directly call coda_hw_init in
2220 * coda_fw_callback regardless of whether CONFIG_PM is
2221 * enabled or whether the device is associated with a PM domain.
2222 */
2223 pm_runtime_get_noresume(&pdev->dev);
2224 pm_runtime_set_active(&pdev->dev);
2225 pm_runtime_enable(&pdev->dev);
2226
2227 return coda_firmware_request(dev);
2228
2229 err_v4l2_register:
2230 v4l2_device_unregister(&dev->v4l2_dev);
2231 return ret;
2232 }
2233
2234 static int coda_remove(struct platform_device *pdev)
2235 {
2236 struct coda_dev *dev = platform_get_drvdata(pdev);
2237 int i;
2238
2239 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2240 if (video_get_drvdata(&dev->vfd[i]))
2241 video_unregister_device(&dev->vfd[i]);
2242 }
2243 if (dev->m2m_dev)
2244 v4l2_m2m_release(dev->m2m_dev);
2245 pm_runtime_disable(&pdev->dev);
2246 if (dev->alloc_ctx)
2247 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2248 v4l2_device_unregister(&dev->v4l2_dev);
2249 destroy_workqueue(dev->workqueue);
2250 if (dev->iram.vaddr)
2251 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2252 dev->iram.size);
2253 coda_free_aux_buf(dev, &dev->codebuf);
2254 coda_free_aux_buf(dev, &dev->tempbuf);
2255 coda_free_aux_buf(dev, &dev->workbuf);
2256 debugfs_remove_recursive(dev->debugfs_root);
2257 return 0;
2258 }
2259
2260 #ifdef CONFIG_PM
2261 static int coda_runtime_resume(struct device *dev)
2262 {
2263 struct coda_dev *cdev = dev_get_drvdata(dev);
2264 int ret = 0;
2265
2266 if (dev->pm_domain && cdev->codebuf.vaddr) {
2267 ret = coda_hw_init(cdev);
2268 if (ret)
2269 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2270 }
2271
2272 return ret;
2273 }
2274 #endif
2275
2276 static const struct dev_pm_ops coda_pm_ops = {
2277 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2278 };
2279
2280 static struct platform_driver coda_driver = {
2281 .probe = coda_probe,
2282 .remove = coda_remove,
2283 .driver = {
2284 .name = CODA_NAME,
2285 .of_match_table = of_match_ptr(coda_dt_ids),
2286 .pm = &coda_pm_ops,
2287 },
2288 .id_table = coda_platform_ids,
2289 };
2290
2291 module_platform_driver(coda_driver);
2292
2293 MODULE_LICENSE("GPL");
2294 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2295 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");
This page took 0.075066 seconds and 4 git commands to generate.