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