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