1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Coda multi-standard codec IP
4 *
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
7 * Xavier Duret
8 */
9
10 #include <linux/clk.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/gcd.h>
15 #include <linux/genalloc.h>
16 #include <linux/idr.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/irq.h>
20 #include <linux/kfifo.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/slab.h>
27 #include <linux/videodev2.h>
28 #include <linux/ratelimit.h>
29 #include <linux/reset.h>
30
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-event.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-mem2mem.h>
36 #include <media/videobuf2-v4l2.h>
37 #include <media/videobuf2-dma-contig.h>
38 #include <media/videobuf2-vmalloc.h>
39
40 #include "coda.h"
41 #include "imx-vdoa.h"
42
43 #define CODA_NAME "coda"
44
45 #define CODADX6_MAX_INSTANCES 4
46 #define CODA_MAX_FORMATS 5
47
48 #define CODA_ISRAM_SIZE (2048 * 2)
49
50 #define MIN_W 48
51 #define MIN_H 16
52
53 #define S_ALIGN 1 /* multiple of 2 */
54 #define W_ALIGN 1 /* multiple of 2 */
55 #define H_ALIGN 1 /* multiple of 2 */
56
57 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
58
file_to_ctx(struct file * filp)59 static inline struct coda_ctx *file_to_ctx(struct file *filp)
60 {
61 return fh_to_ctx(file_to_v4l2_fh(filp));
62 }
63
64 int coda_debug;
65 module_param(coda_debug, int, 0644);
66 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
67
68 static int disable_tiling;
69 module_param(disable_tiling, int, 0644);
70 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
71
72 static int disable_vdoa;
73 module_param(disable_vdoa, int, 0644);
74 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
75
76 static int enable_bwb = 0;
77 module_param(enable_bwb, int, 0644);
78 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
79
coda_write(struct coda_dev * dev,u32 data,u32 reg)80 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
81 {
82 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
83 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
84 writel(data, dev->regs_base + reg);
85 }
86
coda_read(struct coda_dev * dev,u32 reg)87 unsigned int coda_read(struct coda_dev *dev, u32 reg)
88 {
89 u32 data;
90
91 data = readl(dev->regs_base + reg);
92 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
93 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
94 return data;
95 }
96
coda_write_base(struct coda_ctx * ctx,struct coda_q_data * q_data,struct vb2_v4l2_buffer * buf,unsigned int reg_y)97 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
98 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
99 {
100 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
101 u32 base_cb, base_cr;
102
103 switch (q_data->fourcc) {
104 case V4L2_PIX_FMT_YUYV:
105 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
106 case V4L2_PIX_FMT_NV12:
107 case V4L2_PIX_FMT_YUV420:
108 default:
109 base_cb = base_y + q_data->bytesperline * q_data->height;
110 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
111 break;
112 case V4L2_PIX_FMT_YVU420:
113 /* Switch Cb and Cr for YVU420 format */
114 base_cr = base_y + q_data->bytesperline * q_data->height;
115 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
116 break;
117 case V4L2_PIX_FMT_YUV422P:
118 base_cb = base_y + q_data->bytesperline * q_data->height;
119 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
120 }
121
122 coda_write(ctx->dev, base_y, reg_y);
123 coda_write(ctx->dev, base_cb, reg_y + 4);
124 coda_write(ctx->dev, base_cr, reg_y + 8);
125 }
126
127 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
128 { mode, src_fourcc, dst_fourcc, max_w, max_h }
129
130 /*
131 * Arrays of codecs supported by each given version of Coda:
132 * i.MX27 -> codadx6
133 * i.MX51 -> codahx4
134 * i.MX53 -> coda7
135 * i.MX6 -> coda960
136 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
137 */
138 static const struct coda_codec codadx6_codecs[] = {
139 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
140 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
141 };
142
143 static const struct coda_codec codahx4_codecs[] = {
144 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
145 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
146 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
147 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
148 };
149
150 static const struct coda_codec coda7_codecs[] = {
151 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
152 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
153 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
154 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
155 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
156 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
157 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
158 };
159
160 static const struct coda_codec coda9_codecs[] = {
161 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
163 CODA_CODEC(CODA9_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
164 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
165 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
166 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
167 CODA_CODEC(CODA9_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
168 };
169
170 struct coda_video_device {
171 const char *name;
172 enum coda_inst_type type;
173 const struct coda_context_ops *ops;
174 bool direct;
175 u32 src_formats[CODA_MAX_FORMATS];
176 u32 dst_formats[CODA_MAX_FORMATS];
177 };
178
179 static const struct coda_video_device coda_bit_encoder = {
180 .name = "coda-video-encoder",
181 .type = CODA_INST_ENCODER,
182 .ops = &coda_bit_encode_ops,
183 .src_formats = {
184 V4L2_PIX_FMT_NV12,
185 V4L2_PIX_FMT_YUV420,
186 V4L2_PIX_FMT_YVU420,
187 },
188 .dst_formats = {
189 V4L2_PIX_FMT_H264,
190 V4L2_PIX_FMT_MPEG4,
191 },
192 };
193
194 static const struct coda_video_device coda_bit_jpeg_encoder = {
195 .name = "coda-jpeg-encoder",
196 .type = CODA_INST_ENCODER,
197 .ops = &coda_bit_encode_ops,
198 .src_formats = {
199 V4L2_PIX_FMT_NV12,
200 V4L2_PIX_FMT_YUV420,
201 V4L2_PIX_FMT_YVU420,
202 V4L2_PIX_FMT_YUV422P,
203 },
204 .dst_formats = {
205 V4L2_PIX_FMT_JPEG,
206 },
207 };
208
209 static const struct coda_video_device coda_bit_decoder = {
210 .name = "coda-video-decoder",
211 .type = CODA_INST_DECODER,
212 .ops = &coda_bit_decode_ops,
213 .src_formats = {
214 V4L2_PIX_FMT_H264,
215 V4L2_PIX_FMT_MPEG2,
216 V4L2_PIX_FMT_MPEG4,
217 },
218 .dst_formats = {
219 V4L2_PIX_FMT_NV12,
220 V4L2_PIX_FMT_YUV420,
221 V4L2_PIX_FMT_YVU420,
222 /*
223 * If V4L2_PIX_FMT_YUYV should be default,
224 * set_default_params() must be adjusted.
225 */
226 V4L2_PIX_FMT_YUYV,
227 },
228 };
229
230 static const struct coda_video_device coda_bit_jpeg_decoder = {
231 .name = "coda-jpeg-decoder",
232 .type = CODA_INST_DECODER,
233 .ops = &coda_bit_decode_ops,
234 .src_formats = {
235 V4L2_PIX_FMT_JPEG,
236 },
237 .dst_formats = {
238 V4L2_PIX_FMT_NV12,
239 V4L2_PIX_FMT_YUV420,
240 V4L2_PIX_FMT_YVU420,
241 V4L2_PIX_FMT_YUV422P,
242 },
243 };
244
245 static const struct coda_video_device coda9_jpeg_encoder = {
246 .name = "coda-jpeg-encoder",
247 .type = CODA_INST_ENCODER,
248 .ops = &coda9_jpeg_encode_ops,
249 .direct = true,
250 .src_formats = {
251 V4L2_PIX_FMT_NV12,
252 V4L2_PIX_FMT_YUV420,
253 V4L2_PIX_FMT_YVU420,
254 V4L2_PIX_FMT_YUV422P,
255 V4L2_PIX_FMT_GREY,
256 },
257 .dst_formats = {
258 V4L2_PIX_FMT_JPEG,
259 },
260 };
261
262 static const struct coda_video_device coda9_jpeg_decoder = {
263 .name = "coda-jpeg-decoder",
264 .type = CODA_INST_DECODER,
265 .ops = &coda9_jpeg_decode_ops,
266 .direct = true,
267 .src_formats = {
268 V4L2_PIX_FMT_JPEG,
269 },
270 .dst_formats = {
271 V4L2_PIX_FMT_NV12,
272 V4L2_PIX_FMT_YUV420,
273 V4L2_PIX_FMT_YVU420,
274 V4L2_PIX_FMT_YUV422P,
275 },
276 };
277
278 static const struct coda_video_device *codadx6_video_devices[] = {
279 &coda_bit_encoder,
280 };
281
282 static const struct coda_video_device *codahx4_video_devices[] = {
283 &coda_bit_encoder,
284 &coda_bit_decoder,
285 };
286
287 static const struct coda_video_device *coda7_video_devices[] = {
288 &coda_bit_jpeg_encoder,
289 &coda_bit_jpeg_decoder,
290 &coda_bit_encoder,
291 &coda_bit_decoder,
292 };
293
294 static const struct coda_video_device *coda9_video_devices[] = {
295 &coda9_jpeg_encoder,
296 &coda9_jpeg_decoder,
297 &coda_bit_encoder,
298 &coda_bit_decoder,
299 };
300
301 /*
302 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
303 * tables.
304 */
coda_format_normalize_yuv(u32 fourcc)305 static u32 coda_format_normalize_yuv(u32 fourcc)
306 {
307 switch (fourcc) {
308 case V4L2_PIX_FMT_NV12:
309 case V4L2_PIX_FMT_YUV420:
310 case V4L2_PIX_FMT_YVU420:
311 case V4L2_PIX_FMT_YUV422P:
312 case V4L2_PIX_FMT_YUYV:
313 return V4L2_PIX_FMT_YUV420;
314 default:
315 return fourcc;
316 }
317 }
318
coda_find_codec(struct coda_dev * dev,int src_fourcc,int dst_fourcc)319 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
320 int src_fourcc, int dst_fourcc)
321 {
322 const struct coda_codec *codecs = dev->devtype->codecs;
323 int num_codecs = dev->devtype->num_codecs;
324 int k;
325
326 src_fourcc = coda_format_normalize_yuv(src_fourcc);
327 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
328 if (src_fourcc == dst_fourcc)
329 return NULL;
330
331 for (k = 0; k < num_codecs; k++) {
332 if (codecs[k].src_fourcc == src_fourcc &&
333 codecs[k].dst_fourcc == dst_fourcc)
334 break;
335 }
336
337 if (k == num_codecs)
338 return NULL;
339
340 return &codecs[k];
341 }
342
coda_get_max_dimensions(struct coda_dev * dev,const struct coda_codec * codec,int * max_w,int * max_h)343 static void coda_get_max_dimensions(struct coda_dev *dev,
344 const struct coda_codec *codec,
345 int *max_w, int *max_h)
346 {
347 const struct coda_codec *codecs = dev->devtype->codecs;
348 int num_codecs = dev->devtype->num_codecs;
349 unsigned int w, h;
350 int k;
351
352 if (codec) {
353 w = codec->max_w;
354 h = codec->max_h;
355 } else {
356 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
357 w = max(w, codecs[k].max_w);
358 h = max(h, codecs[k].max_h);
359 }
360 }
361
362 if (max_w)
363 *max_w = w;
364 if (max_h)
365 *max_h = h;
366 }
367
to_coda_video_device(struct video_device * vdev)368 static const struct coda_video_device *to_coda_video_device(struct video_device
369 *vdev)
370 {
371 struct coda_dev *dev = video_get_drvdata(vdev);
372 unsigned int i = vdev - dev->vfd;
373
374 if (i >= dev->devtype->num_vdevs)
375 return NULL;
376
377 return dev->devtype->vdevs[i];
378 }
379
coda_product_name(int product)380 const char *coda_product_name(int product)
381 {
382 static char buf[9];
383
384 switch (product) {
385 case CODA_DX6:
386 return "CodaDx6";
387 case CODA_HX4:
388 return "CodaHx4";
389 case CODA_7541:
390 return "CODA7541";
391 case CODA_960:
392 return "CODA960";
393 default:
394 snprintf(buf, sizeof(buf), "(0x%04x)", product);
395 return buf;
396 }
397 }
398
coda_get_vdoa_data(void)399 static struct vdoa_data *coda_get_vdoa_data(void)
400 {
401 struct device_node *vdoa_node;
402 struct platform_device *vdoa_pdev;
403 struct vdoa_data *vdoa_data = NULL;
404
405 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
406 if (!vdoa_node)
407 return NULL;
408
409 vdoa_pdev = of_find_device_by_node(vdoa_node);
410 if (!vdoa_pdev)
411 goto out;
412
413 vdoa_data = platform_get_drvdata(vdoa_pdev);
414 if (!vdoa_data)
415 vdoa_data = ERR_PTR(-EPROBE_DEFER);
416
417 put_device(&vdoa_pdev->dev);
418 out:
419 of_node_put(vdoa_node);
420
421 return vdoa_data;
422 }
423
424 /*
425 * V4L2 ioctl() operations.
426 */
coda_querycap(struct file * file,void * priv,struct v4l2_capability * cap)427 static int coda_querycap(struct file *file, void *priv,
428 struct v4l2_capability *cap)
429 {
430 struct coda_ctx *ctx = file_to_ctx(file);
431
432 strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
433 strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
434 sizeof(cap->card));
435 strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
436 return 0;
437 }
438
439 static const u32 coda_formats_420[CODA_MAX_FORMATS] = {
440 V4L2_PIX_FMT_NV12,
441 V4L2_PIX_FMT_YUV420,
442 V4L2_PIX_FMT_YVU420,
443 };
444
coda_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)445 static int coda_enum_fmt(struct file *file, void *priv,
446 struct v4l2_fmtdesc *f)
447 {
448 struct video_device *vdev = video_devdata(file);
449 const struct coda_video_device *cvd = to_coda_video_device(vdev);
450 struct coda_ctx *ctx = file_to_ctx(file);
451 const u32 *formats;
452
453 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
454 formats = cvd->src_formats;
455 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
456 struct coda_q_data *q_data_src;
457 struct vb2_queue *src_vq;
458
459 formats = cvd->dst_formats;
460
461 /*
462 * If the source format is already fixed, only allow the same
463 * chroma subsampling.
464 */
465 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
466 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
467 V4L2_BUF_TYPE_VIDEO_OUTPUT);
468 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
469 vb2_is_streaming(src_vq)) {
470 if (ctx->params.jpeg_chroma_subsampling ==
471 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
472 formats = coda_formats_420;
473 } else if (ctx->params.jpeg_chroma_subsampling ==
474 V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
475 f->pixelformat = V4L2_PIX_FMT_YUV422P;
476 return f->index ? -EINVAL : 0;
477 }
478 }
479 } else {
480 return -EINVAL;
481 }
482
483 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
484 return -EINVAL;
485
486 /* Skip YUYV if the vdoa is not available */
487 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
488 formats[f->index] == V4L2_PIX_FMT_YUYV)
489 return -EINVAL;
490
491 f->pixelformat = formats[f->index];
492
493 return 0;
494 }
495
coda_g_fmt(struct file * file,void * priv,struct v4l2_format * f)496 static int coda_g_fmt(struct file *file, void *priv,
497 struct v4l2_format *f)
498 {
499 struct coda_q_data *q_data;
500 struct coda_ctx *ctx = file_to_ctx(file);
501
502 q_data = get_q_data(ctx, f->type);
503 if (!q_data)
504 return -EINVAL;
505
506 f->fmt.pix.field = V4L2_FIELD_NONE;
507 f->fmt.pix.pixelformat = q_data->fourcc;
508 f->fmt.pix.width = q_data->width;
509 f->fmt.pix.height = q_data->height;
510 f->fmt.pix.bytesperline = q_data->bytesperline;
511
512 f->fmt.pix.sizeimage = q_data->sizeimage;
513 f->fmt.pix.colorspace = ctx->colorspace;
514 f->fmt.pix.xfer_func = ctx->xfer_func;
515 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
516 f->fmt.pix.quantization = ctx->quantization;
517
518 return 0;
519 }
520
coda_try_pixelformat(struct coda_ctx * ctx,struct v4l2_format * f)521 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
522 {
523 struct coda_q_data *q_data;
524 const u32 *formats;
525 int i;
526
527 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
528 formats = ctx->cvd->src_formats;
529 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
530 formats = ctx->cvd->dst_formats;
531 else
532 return -EINVAL;
533
534 for (i = 0; i < CODA_MAX_FORMATS; i++) {
535 /* Skip YUYV if the vdoa is not available */
536 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
537 formats[i] == V4L2_PIX_FMT_YUYV)
538 continue;
539
540 if (formats[i] == f->fmt.pix.pixelformat) {
541 f->fmt.pix.pixelformat = formats[i];
542 return 0;
543 }
544 }
545
546 /* Fall back to currently set pixelformat */
547 q_data = get_q_data(ctx, f->type);
548 f->fmt.pix.pixelformat = q_data->fourcc;
549
550 return 0;
551 }
552
coda_try_fmt_vdoa(struct coda_ctx * ctx,struct v4l2_format * f,bool * use_vdoa)553 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
554 bool *use_vdoa)
555 {
556 int err;
557
558 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
559 return -EINVAL;
560
561 if (!use_vdoa)
562 return -EINVAL;
563
564 if (!ctx->vdoa) {
565 *use_vdoa = false;
566 return 0;
567 }
568
569 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
570 f->fmt.pix.height, f->fmt.pix.pixelformat);
571 if (err) {
572 *use_vdoa = false;
573 return 0;
574 }
575
576 *use_vdoa = true;
577 return 0;
578 }
579
coda_estimate_sizeimage(struct coda_ctx * ctx,u32 sizeimage,u32 width,u32 height)580 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
581 u32 width, u32 height)
582 {
583 /*
584 * This is a rough estimate for sensible compressed buffer
585 * sizes (between 1 and 16 bits per pixel). This could be
586 * improved by better format specific worst case estimates.
587 */
588 return round_up(clamp(sizeimage, width * height / 8,
589 width * height * 2), PAGE_SIZE);
590 }
591
coda_try_fmt(struct coda_ctx * ctx,const struct coda_codec * codec,struct v4l2_format * f)592 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
593 struct v4l2_format *f)
594 {
595 struct coda_dev *dev = ctx->dev;
596 unsigned int max_w, max_h;
597 enum v4l2_field field;
598
599 field = f->fmt.pix.field;
600 if (field == V4L2_FIELD_ANY)
601 field = V4L2_FIELD_NONE;
602 else if (V4L2_FIELD_NONE != field)
603 return -EINVAL;
604
605 /* V4L2 specification suggests the driver corrects the format struct
606 * if any of the dimensions is unsupported */
607 f->fmt.pix.field = field;
608
609 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
610 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
611 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
612 S_ALIGN);
613
614 switch (f->fmt.pix.pixelformat) {
615 case V4L2_PIX_FMT_NV12:
616 case V4L2_PIX_FMT_YUV420:
617 case V4L2_PIX_FMT_YVU420:
618 /*
619 * Frame stride must be at least multiple of 8,
620 * but multiple of 16 for h.264 or JPEG 4:2:x
621 */
622 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
623 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
624 f->fmt.pix.height * 3 / 2;
625 break;
626 case V4L2_PIX_FMT_YUYV:
627 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
628 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
629 f->fmt.pix.height;
630 break;
631 case V4L2_PIX_FMT_YUV422P:
632 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
633 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
634 f->fmt.pix.height * 2;
635 break;
636 case V4L2_PIX_FMT_GREY:
637 /* keep 16 pixel alignment of 8-bit pixel data */
638 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
639 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
640 break;
641 case V4L2_PIX_FMT_JPEG:
642 case V4L2_PIX_FMT_H264:
643 case V4L2_PIX_FMT_MPEG4:
644 case V4L2_PIX_FMT_MPEG2:
645 f->fmt.pix.bytesperline = 0;
646 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
647 f->fmt.pix.sizeimage,
648 f->fmt.pix.width,
649 f->fmt.pix.height);
650 break;
651 default:
652 BUG();
653 }
654
655 return 0;
656 }
657
coda_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)658 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
659 struct v4l2_format *f)
660 {
661 struct coda_ctx *ctx = file_to_ctx(file);
662 const struct coda_q_data *q_data_src;
663 const struct coda_codec *codec;
664 struct vb2_queue *src_vq;
665 int hscale = 0;
666 int vscale = 0;
667 int ret;
668 bool use_vdoa;
669
670 ret = coda_try_pixelformat(ctx, f);
671 if (ret < 0)
672 return ret;
673
674 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
675
676 /*
677 * If the source format is already fixed, only allow the same output
678 * resolution. When decoding JPEG images, we also have to make sure to
679 * use the same chroma subsampling.
680 */
681 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
682 if (vb2_is_streaming(src_vq)) {
683 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
684 ctx->dev->devtype->product == CODA_960) {
685 hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
686 vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
687 }
688 f->fmt.pix.width = q_data_src->width >> hscale;
689 f->fmt.pix.height = q_data_src->height >> vscale;
690
691 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
692 if (ctx->params.jpeg_chroma_subsampling ==
693 V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
694 f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
695 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
696 else if (ctx->params.jpeg_chroma_subsampling ==
697 V4L2_JPEG_CHROMA_SUBSAMPLING_422)
698 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
699 }
700 }
701
702 f->fmt.pix.colorspace = ctx->colorspace;
703 f->fmt.pix.xfer_func = ctx->xfer_func;
704 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
705 f->fmt.pix.quantization = ctx->quantization;
706
707 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
708 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
709 f->fmt.pix.pixelformat);
710 if (!codec)
711 return -EINVAL;
712
713 ret = coda_try_fmt(ctx, codec, f);
714 if (ret < 0)
715 return ret;
716
717 /* The decoders always write complete macroblocks or MCUs */
718 if (ctx->inst_type == CODA_INST_DECODER) {
719 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16 >> hscale);
720 f->fmt.pix.height = round_up(f->fmt.pix.height, 16 >> vscale);
721 if (codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
722 f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P) {
723 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
724 f->fmt.pix.height * 2;
725 } else {
726 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
727 f->fmt.pix.height * 3 / 2;
728 }
729
730 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
731 if (ret < 0)
732 return ret;
733
734 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
735 if (!use_vdoa)
736 return -EINVAL;
737
738 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
739 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
740 f->fmt.pix.height;
741 }
742 }
743
744 return 0;
745 }
746
coda_set_default_colorspace(struct v4l2_pix_format * fmt)747 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
748 {
749 enum v4l2_colorspace colorspace;
750
751 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
752 colorspace = V4L2_COLORSPACE_JPEG;
753 else if (fmt->width <= 720 && fmt->height <= 576)
754 colorspace = V4L2_COLORSPACE_SMPTE170M;
755 else
756 colorspace = V4L2_COLORSPACE_REC709;
757
758 fmt->colorspace = colorspace;
759 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
760 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
761 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
762 }
763
coda_try_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)764 static int coda_try_fmt_vid_out(struct file *file, void *priv,
765 struct v4l2_format *f)
766 {
767 struct coda_ctx *ctx = file_to_ctx(file);
768 struct coda_dev *dev = ctx->dev;
769 const struct coda_q_data *q_data_dst;
770 const struct coda_codec *codec;
771 int ret;
772
773 ret = coda_try_pixelformat(ctx, f);
774 if (ret < 0)
775 return ret;
776
777 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
778 coda_set_default_colorspace(&f->fmt.pix);
779
780 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
781 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
782
783 return coda_try_fmt(ctx, codec, f);
784 }
785
coda_s_fmt(struct coda_ctx * ctx,struct v4l2_format * f,struct v4l2_rect * r)786 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
787 struct v4l2_rect *r)
788 {
789 struct coda_q_data *q_data;
790 struct vb2_queue *vq;
791
792 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
793 if (!vq)
794 return -EINVAL;
795
796 q_data = get_q_data(ctx, f->type);
797 if (!q_data)
798 return -EINVAL;
799
800 if (vb2_is_busy(vq)) {
801 v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
802 __func__, v4l2_type_names[f->type], vb2_get_num_buffers(vq));
803 return -EBUSY;
804 }
805
806 q_data->fourcc = f->fmt.pix.pixelformat;
807 q_data->width = f->fmt.pix.width;
808 q_data->height = f->fmt.pix.height;
809 q_data->bytesperline = f->fmt.pix.bytesperline;
810 q_data->sizeimage = f->fmt.pix.sizeimage;
811 if (r) {
812 q_data->rect = *r;
813 } else {
814 q_data->rect.left = 0;
815 q_data->rect.top = 0;
816 q_data->rect.width = f->fmt.pix.width;
817 q_data->rect.height = f->fmt.pix.height;
818 }
819
820 switch (f->fmt.pix.pixelformat) {
821 case V4L2_PIX_FMT_YUYV:
822 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
823 break;
824 case V4L2_PIX_FMT_NV12:
825 if (!disable_tiling && ctx->use_bit &&
826 ctx->dev->devtype->product == CODA_960) {
827 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
828 break;
829 }
830 fallthrough;
831 case V4L2_PIX_FMT_YUV420:
832 case V4L2_PIX_FMT_YVU420:
833 case V4L2_PIX_FMT_YUV422P:
834 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
835 break;
836 default:
837 break;
838 }
839
840 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
841 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
842 ctx->use_vdoa)
843 vdoa_context_configure(ctx->vdoa,
844 round_up(f->fmt.pix.width, 16),
845 f->fmt.pix.height,
846 f->fmt.pix.pixelformat);
847 else
848 ctx->use_vdoa = false;
849
850 coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
851 v4l2_type_names[f->type], q_data->width, q_data->height,
852 (char *)&q_data->fourcc,
853 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
854
855 return 0;
856 }
857
coda_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)858 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
859 struct v4l2_format *f)
860 {
861 struct coda_ctx *ctx = file_to_ctx(file);
862 struct coda_q_data *q_data_src;
863 const struct coda_codec *codec;
864 struct v4l2_rect r;
865 int hscale = 0;
866 int vscale = 0;
867 int ret;
868
869 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
870
871 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
872 ctx->dev->devtype->product == CODA_960) {
873 hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
874 vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
875 }
876
877 ret = coda_try_fmt_vid_cap(file, priv, f);
878 if (ret)
879 return ret;
880
881 r.left = 0;
882 r.top = 0;
883 r.width = q_data_src->width >> hscale;
884 r.height = q_data_src->height >> vscale;
885
886 ret = coda_s_fmt(ctx, f, &r);
887 if (ret)
888 return ret;
889
890 if (ctx->inst_type != CODA_INST_ENCODER)
891 return 0;
892
893 /* Setting the coded format determines the selected codec */
894 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
895 f->fmt.pix.pixelformat);
896 if (!codec) {
897 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
898 return -EINVAL;
899 }
900 ctx->codec = codec;
901
902 ctx->colorspace = f->fmt.pix.colorspace;
903 ctx->xfer_func = f->fmt.pix.xfer_func;
904 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
905 ctx->quantization = f->fmt.pix.quantization;
906
907 return 0;
908 }
909
coda_s_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)910 static int coda_s_fmt_vid_out(struct file *file, void *priv,
911 struct v4l2_format *f)
912 {
913 struct coda_ctx *ctx = file_to_ctx(file);
914 const struct coda_codec *codec;
915 struct v4l2_format f_cap;
916 struct vb2_queue *dst_vq;
917 int ret;
918
919 ret = coda_try_fmt_vid_out(file, priv, f);
920 if (ret)
921 return ret;
922
923 ret = coda_s_fmt(ctx, f, NULL);
924 if (ret)
925 return ret;
926
927 ctx->colorspace = f->fmt.pix.colorspace;
928 ctx->xfer_func = f->fmt.pix.xfer_func;
929 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
930 ctx->quantization = f->fmt.pix.quantization;
931
932 if (ctx->inst_type != CODA_INST_DECODER)
933 return 0;
934
935 /* Setting the coded format determines the selected codec */
936 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
937 V4L2_PIX_FMT_YUV420);
938 if (!codec) {
939 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
940 return -EINVAL;
941 }
942 ctx->codec = codec;
943
944 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
945 if (!dst_vq)
946 return -EINVAL;
947
948 /*
949 * Setting the capture queue format is not possible while the capture
950 * queue is still busy. This is not an error, but the user will have to
951 * make sure themselves that the capture format is set correctly before
952 * starting the output queue again.
953 */
954 if (vb2_is_busy(dst_vq))
955 return 0;
956
957 memset(&f_cap, 0, sizeof(f_cap));
958 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
959 coda_g_fmt(file, priv, &f_cap);
960 f_cap.fmt.pix.width = f->fmt.pix.width;
961 f_cap.fmt.pix.height = f->fmt.pix.height;
962
963 return coda_s_fmt_vid_cap(file, priv, &f_cap);
964 }
965
coda_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * rb)966 static int coda_reqbufs(struct file *file, void *priv,
967 struct v4l2_requestbuffers *rb)
968 {
969 struct coda_ctx *ctx = file_to_ctx(file);
970 int ret;
971
972 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
973 if (ret)
974 return ret;
975
976 /*
977 * Allow to allocate instance specific per-context buffers, such as
978 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
979 */
980 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
981 return ctx->ops->reqbufs(ctx, rb);
982
983 return 0;
984 }
985
coda_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)986 static int coda_qbuf(struct file *file, void *priv,
987 struct v4l2_buffer *buf)
988 {
989 struct coda_ctx *ctx = file_to_ctx(file);
990
991 if (ctx->inst_type == CODA_INST_DECODER &&
992 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
993 buf->flags &= ~V4L2_BUF_FLAG_LAST;
994
995 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
996 }
997
coda_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)998 static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
999 {
1000 struct coda_ctx *ctx = file_to_ctx(file);
1001 int ret;
1002
1003 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
1004
1005 if (ctx->inst_type == CODA_INST_DECODER &&
1006 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1007 buf->flags &= ~V4L2_BUF_FLAG_LAST;
1008
1009 return ret;
1010 }
1011
coda_m2m_buf_done(struct coda_ctx * ctx,struct vb2_v4l2_buffer * buf,enum vb2_buffer_state state)1012 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
1013 enum vb2_buffer_state state)
1014 {
1015 const struct v4l2_event eos_event = {
1016 .type = V4L2_EVENT_EOS
1017 };
1018
1019 if (buf->flags & V4L2_BUF_FLAG_LAST)
1020 v4l2_event_queue_fh(&ctx->fh, &eos_event);
1021
1022 v4l2_m2m_buf_done(buf, state);
1023 }
1024
coda_g_selection(struct file * file,void * fh,struct v4l2_selection * s)1025 static int coda_g_selection(struct file *file, void *fh,
1026 struct v4l2_selection *s)
1027 {
1028 struct coda_ctx *ctx = file_to_ctx(file);
1029 struct coda_q_data *q_data;
1030 struct v4l2_rect r, *rsel;
1031
1032 q_data = get_q_data(ctx, s->type);
1033 if (!q_data)
1034 return -EINVAL;
1035
1036 r.left = 0;
1037 r.top = 0;
1038 r.width = q_data->width;
1039 r.height = q_data->height;
1040 rsel = &q_data->rect;
1041
1042 switch (s->target) {
1043 case V4L2_SEL_TGT_CROP_DEFAULT:
1044 case V4L2_SEL_TGT_CROP_BOUNDS:
1045 rsel = &r;
1046 fallthrough;
1047 case V4L2_SEL_TGT_CROP:
1048 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
1049 ctx->inst_type == CODA_INST_DECODER)
1050 return -EINVAL;
1051 break;
1052 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1053 case V4L2_SEL_TGT_COMPOSE_PADDED:
1054 rsel = &r;
1055 fallthrough;
1056 case V4L2_SEL_TGT_COMPOSE:
1057 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1058 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1059 ctx->inst_type == CODA_INST_ENCODER)
1060 return -EINVAL;
1061 break;
1062 default:
1063 return -EINVAL;
1064 }
1065
1066 s->r = *rsel;
1067
1068 return 0;
1069 }
1070
coda_s_selection(struct file * file,void * fh,struct v4l2_selection * s)1071 static int coda_s_selection(struct file *file, void *fh,
1072 struct v4l2_selection *s)
1073 {
1074 struct coda_ctx *ctx = file_to_ctx(file);
1075 struct coda_q_data *q_data;
1076
1077 switch (s->target) {
1078 case V4L2_SEL_TGT_CROP:
1079 if (ctx->inst_type == CODA_INST_ENCODER &&
1080 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1081 q_data = get_q_data(ctx, s->type);
1082 if (!q_data)
1083 return -EINVAL;
1084
1085 s->r.left = 0;
1086 s->r.top = 0;
1087 s->r.width = clamp(s->r.width, 2U, q_data->width);
1088 s->r.height = clamp(s->r.height, 2U, q_data->height);
1089
1090 if (s->flags & V4L2_SEL_FLAG_LE) {
1091 s->r.width = round_up(s->r.width, 2);
1092 s->r.height = round_up(s->r.height, 2);
1093 } else {
1094 s->r.width = round_down(s->r.width, 2);
1095 s->r.height = round_down(s->r.height, 2);
1096 }
1097
1098 q_data->rect = s->r;
1099
1100 coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
1101 s->r.width, s->r.height);
1102
1103 return 0;
1104 }
1105 fallthrough;
1106 case V4L2_SEL_TGT_NATIVE_SIZE:
1107 case V4L2_SEL_TGT_COMPOSE:
1108 return coda_g_selection(file, fh, s);
1109 default:
1110 /* v4l2-compliance expects this to fail for read-only targets */
1111 return -EINVAL;
1112 }
1113 }
1114
coda_wake_up_capture_queue(struct coda_ctx * ctx)1115 static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
1116 {
1117 struct vb2_queue *dst_vq;
1118
1119 coda_dbg(1, ctx, "waking up capture queue\n");
1120
1121 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1122 dst_vq->last_buffer_dequeued = true;
1123 wake_up(&dst_vq->done_wq);
1124 }
1125
coda_encoder_cmd(struct file * file,void * fh,struct v4l2_encoder_cmd * ec)1126 static int coda_encoder_cmd(struct file *file, void *fh,
1127 struct v4l2_encoder_cmd *ec)
1128 {
1129 struct coda_ctx *ctx = file_to_ctx(file);
1130 struct vb2_v4l2_buffer *buf;
1131 int ret;
1132
1133 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
1134 if (ret < 0)
1135 return ret;
1136
1137 mutex_lock(&ctx->wakeup_mutex);
1138 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1139 if (buf) {
1140 /*
1141 * If the last output buffer is still on the queue, make sure
1142 * that decoder finish_run will see the last flag and report it
1143 * to userspace.
1144 */
1145 buf->flags |= V4L2_BUF_FLAG_LAST;
1146 } else {
1147 /* Set the stream-end flag on this context */
1148 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1149
1150 /*
1151 * If the last output buffer has already been taken from the
1152 * queue, wake up the capture queue and signal end of stream
1153 * via the -EPIPE mechanism.
1154 */
1155 coda_wake_up_capture_queue(ctx);
1156 }
1157 mutex_unlock(&ctx->wakeup_mutex);
1158
1159 return 0;
1160 }
1161
coda_mark_last_meta(struct coda_ctx * ctx)1162 static bool coda_mark_last_meta(struct coda_ctx *ctx)
1163 {
1164 struct coda_buffer_meta *meta;
1165
1166 coda_dbg(1, ctx, "marking last meta\n");
1167
1168 spin_lock(&ctx->buffer_meta_lock);
1169 if (list_empty(&ctx->buffer_meta_list)) {
1170 spin_unlock(&ctx->buffer_meta_lock);
1171 return false;
1172 }
1173
1174 meta = list_last_entry(&ctx->buffer_meta_list, struct coda_buffer_meta,
1175 list);
1176 meta->last = true;
1177
1178 spin_unlock(&ctx->buffer_meta_lock);
1179 return true;
1180 }
1181
coda_mark_last_dst_buf(struct coda_ctx * ctx)1182 static bool coda_mark_last_dst_buf(struct coda_ctx *ctx)
1183 {
1184 struct vb2_v4l2_buffer *buf;
1185 struct vb2_buffer *dst_vb;
1186 struct vb2_queue *dst_vq;
1187 unsigned long flags;
1188
1189 coda_dbg(1, ctx, "marking last capture buffer\n");
1190
1191 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1192 spin_lock_irqsave(&dst_vq->done_lock, flags);
1193 if (list_empty(&dst_vq->done_list)) {
1194 spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1195 return false;
1196 }
1197
1198 dst_vb = list_last_entry(&dst_vq->done_list, struct vb2_buffer,
1199 done_entry);
1200 buf = to_vb2_v4l2_buffer(dst_vb);
1201 buf->flags |= V4L2_BUF_FLAG_LAST;
1202
1203 spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1204 return true;
1205 }
1206
coda_decoder_cmd(struct file * file,void * fh,struct v4l2_decoder_cmd * dc)1207 static int coda_decoder_cmd(struct file *file, void *fh,
1208 struct v4l2_decoder_cmd *dc)
1209 {
1210 struct coda_ctx *ctx = file_to_ctx(file);
1211 struct coda_dev *dev = ctx->dev;
1212 struct vb2_v4l2_buffer *buf;
1213 struct vb2_queue *dst_vq;
1214 bool stream_end;
1215 bool wakeup;
1216 int ret;
1217
1218 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
1219 if (ret < 0)
1220 return ret;
1221
1222 switch (dc->cmd) {
1223 case V4L2_DEC_CMD_START:
1224 mutex_lock(&dev->coda_mutex);
1225 mutex_lock(&ctx->bitstream_mutex);
1226 coda_bitstream_flush(ctx);
1227 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1228 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1229 vb2_clear_last_buffer_dequeued(dst_vq);
1230 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1231 coda_fill_bitstream(ctx, NULL);
1232 mutex_unlock(&ctx->bitstream_mutex);
1233 mutex_unlock(&dev->coda_mutex);
1234 break;
1235 case V4L2_DEC_CMD_STOP:
1236 stream_end = false;
1237 wakeup = false;
1238
1239 mutex_lock(&ctx->wakeup_mutex);
1240
1241 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1242 if (buf) {
1243 coda_dbg(1, ctx, "marking last pending buffer\n");
1244
1245 /* Mark last buffer */
1246 buf->flags |= V4L2_BUF_FLAG_LAST;
1247
1248 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
1249 coda_dbg(1, ctx, "all remaining buffers queued\n");
1250 stream_end = true;
1251 }
1252 } else {
1253 if (ctx->use_bit)
1254 if (coda_mark_last_meta(ctx))
1255 stream_end = true;
1256 else
1257 wakeup = true;
1258 else
1259 if (!coda_mark_last_dst_buf(ctx))
1260 wakeup = true;
1261 }
1262
1263 if (stream_end) {
1264 coda_dbg(1, ctx, "all remaining buffers queued\n");
1265
1266 /* Set the stream-end flag on this context */
1267 coda_bit_stream_end_flag(ctx);
1268 ctx->hold = false;
1269 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1270 }
1271
1272 if (wakeup) {
1273 /* If there is no buffer in flight, wake up */
1274 coda_wake_up_capture_queue(ctx);
1275 }
1276
1277 mutex_unlock(&ctx->wakeup_mutex);
1278 break;
1279 default:
1280 return -EINVAL;
1281 }
1282
1283 return 0;
1284 }
1285
coda_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)1286 static int coda_enum_framesizes(struct file *file, void *fh,
1287 struct v4l2_frmsizeenum *fsize)
1288 {
1289 struct coda_ctx *ctx = file_to_ctx(file);
1290 struct coda_q_data *q_data_dst;
1291 const struct coda_codec *codec;
1292
1293 if (fsize->index)
1294 return -EINVAL;
1295
1296 if (coda_format_normalize_yuv(fsize->pixel_format) ==
1297 V4L2_PIX_FMT_YUV420) {
1298 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1299 codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1300 q_data_dst->fourcc);
1301 } else {
1302 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1303 fsize->pixel_format);
1304 }
1305 if (!codec)
1306 return -EINVAL;
1307
1308 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1309 fsize->stepwise.min_width = MIN_W;
1310 fsize->stepwise.max_width = codec->max_w;
1311 fsize->stepwise.step_width = 1;
1312 fsize->stepwise.min_height = MIN_H;
1313 fsize->stepwise.max_height = codec->max_h;
1314 fsize->stepwise.step_height = 1;
1315
1316 return 0;
1317 }
1318
coda_enum_frameintervals(struct file * file,void * fh,struct v4l2_frmivalenum * f)1319 static int coda_enum_frameintervals(struct file *file, void *fh,
1320 struct v4l2_frmivalenum *f)
1321 {
1322 struct coda_ctx *ctx = file_to_ctx(file);
1323 struct coda_q_data *q_data;
1324 const struct coda_codec *codec;
1325
1326 if (f->index)
1327 return -EINVAL;
1328
1329 /* Disallow YUYV if the vdoa is not available */
1330 if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1331 return -EINVAL;
1332
1333 if (coda_format_normalize_yuv(f->pixel_format) == V4L2_PIX_FMT_YUV420) {
1334 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1335 codec = coda_find_codec(ctx->dev, f->pixel_format,
1336 q_data->fourcc);
1337 } else {
1338 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1339 f->pixel_format);
1340 }
1341 if (!codec)
1342 return -EINVAL;
1343
1344 if (f->width < MIN_W || f->width > codec->max_w ||
1345 f->height < MIN_H || f->height > codec->max_h)
1346 return -EINVAL;
1347
1348 f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1349 f->stepwise.min.numerator = 1;
1350 f->stepwise.min.denominator = 65535;
1351 f->stepwise.max.numerator = 65536;
1352 f->stepwise.max.denominator = 1;
1353 f->stepwise.step.numerator = 1;
1354 f->stepwise.step.denominator = 1;
1355
1356 return 0;
1357 }
1358
coda_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1359 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1360 {
1361 struct coda_ctx *ctx = file_to_ctx(file);
1362 struct v4l2_fract *tpf;
1363
1364 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1365 return -EINVAL;
1366
1367 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1368 tpf = &a->parm.output.timeperframe;
1369 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1370 tpf->numerator = 1 + (ctx->params.framerate >>
1371 CODA_FRATE_DIV_OFFSET);
1372
1373 return 0;
1374 }
1375
1376 /*
1377 * Approximate timeperframe v4l2_fract with values that can be written
1378 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1379 */
coda_approximate_timeperframe(struct v4l2_fract * timeperframe)1380 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1381 {
1382 struct v4l2_fract s = *timeperframe;
1383 struct v4l2_fract f0;
1384 struct v4l2_fract f1 = { 1, 0 };
1385 struct v4l2_fract f2 = { 0, 1 };
1386 unsigned int i, div, s_denominator;
1387
1388 /* Lower bound is 1/65535 */
1389 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1390 timeperframe->numerator = 1;
1391 timeperframe->denominator = 65535;
1392 return;
1393 }
1394
1395 /* Upper bound is 65536/1 */
1396 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1397 timeperframe->numerator = 65536;
1398 timeperframe->denominator = 1;
1399 return;
1400 }
1401
1402 /* Reduce fraction to lowest terms */
1403 div = gcd(s.numerator, s.denominator);
1404 if (div > 1) {
1405 s.numerator /= div;
1406 s.denominator /= div;
1407 }
1408
1409 if (s.numerator <= 65536 && s.denominator < 65536) {
1410 *timeperframe = s;
1411 return;
1412 }
1413
1414 /* Find successive convergents from continued fraction expansion */
1415 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1416 f0 = f1;
1417 f1 = f2;
1418
1419 /* Stop when f2 exactly equals timeperframe */
1420 if (s.numerator == 0)
1421 break;
1422
1423 i = s.denominator / s.numerator;
1424
1425 f2.numerator = f0.numerator + i * f1.numerator;
1426 f2.denominator = f0.denominator + i * f2.denominator;
1427
1428 s_denominator = s.numerator;
1429 s.numerator = s.denominator % s.numerator;
1430 s.denominator = s_denominator;
1431 }
1432
1433 *timeperframe = f1;
1434 }
1435
coda_timeperframe_to_frate(struct v4l2_fract * timeperframe)1436 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1437 {
1438 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1439 timeperframe->denominator;
1440 }
1441
coda_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1442 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1443 {
1444 struct coda_ctx *ctx = file_to_ctx(file);
1445 struct v4l2_fract *tpf;
1446
1447 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1448 return -EINVAL;
1449
1450 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1451 tpf = &a->parm.output.timeperframe;
1452 coda_approximate_timeperframe(tpf);
1453 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1454 ctx->params.framerate_changed = true;
1455
1456 return 0;
1457 }
1458
coda_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)1459 static int coda_subscribe_event(struct v4l2_fh *fh,
1460 const struct v4l2_event_subscription *sub)
1461 {
1462 struct coda_ctx *ctx = fh_to_ctx(fh);
1463
1464 switch (sub->type) {
1465 case V4L2_EVENT_EOS:
1466 return v4l2_event_subscribe(fh, sub, 0, NULL);
1467 case V4L2_EVENT_SOURCE_CHANGE:
1468 if (ctx->inst_type == CODA_INST_DECODER)
1469 return v4l2_event_subscribe(fh, sub, 0, NULL);
1470 else
1471 return -EINVAL;
1472 default:
1473 return v4l2_ctrl_subscribe_event(fh, sub);
1474 }
1475 }
1476
1477 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1478 .vidioc_querycap = coda_querycap,
1479
1480 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1481 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1482 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1483 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1484
1485 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1486 .vidioc_g_fmt_vid_out = coda_g_fmt,
1487 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1488 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1489
1490 .vidioc_reqbufs = coda_reqbufs,
1491 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1492
1493 .vidioc_qbuf = coda_qbuf,
1494 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1495 .vidioc_dqbuf = coda_dqbuf,
1496 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1497 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1498
1499 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1500 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1501
1502 .vidioc_g_selection = coda_g_selection,
1503 .vidioc_s_selection = coda_s_selection,
1504
1505 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
1506 .vidioc_encoder_cmd = coda_encoder_cmd,
1507 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
1508 .vidioc_decoder_cmd = coda_decoder_cmd,
1509
1510 .vidioc_g_parm = coda_g_parm,
1511 .vidioc_s_parm = coda_s_parm,
1512
1513 .vidioc_enum_framesizes = coda_enum_framesizes,
1514 .vidioc_enum_frameintervals = coda_enum_frameintervals,
1515
1516 .vidioc_subscribe_event = coda_subscribe_event,
1517 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1518 };
1519
1520 /*
1521 * Mem-to-mem operations.
1522 */
1523
coda_device_run(void * m2m_priv)1524 static void coda_device_run(void *m2m_priv)
1525 {
1526 struct coda_ctx *ctx = m2m_priv;
1527 struct coda_dev *dev = ctx->dev;
1528
1529 queue_work(dev->workqueue, &ctx->pic_run_work);
1530 }
1531
coda_pic_run_work(struct work_struct * work)1532 static void coda_pic_run_work(struct work_struct *work)
1533 {
1534 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1535 struct coda_dev *dev = ctx->dev;
1536 int ret;
1537
1538 mutex_lock(&ctx->buffer_mutex);
1539 mutex_lock(&dev->coda_mutex);
1540
1541 ret = ctx->ops->prepare_run(ctx);
1542 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER)
1543 goto out;
1544
1545 if (!wait_for_completion_timeout(&ctx->completion,
1546 msecs_to_jiffies(1000))) {
1547 if (ctx->use_bit) {
1548 dev_err(dev->dev, "CODA PIC_RUN timeout\n");
1549
1550 ctx->hold = true;
1551
1552 coda_hw_reset(ctx);
1553 }
1554
1555 if (ctx->ops->run_timeout)
1556 ctx->ops->run_timeout(ctx);
1557 } else {
1558 ctx->ops->finish_run(ctx);
1559 }
1560
1561 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1562 ctx->ops->seq_end_work)
1563 queue_work(dev->workqueue, &ctx->seq_end_work);
1564
1565 out:
1566 mutex_unlock(&dev->coda_mutex);
1567 mutex_unlock(&ctx->buffer_mutex);
1568
1569 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1570 }
1571
coda_job_ready(void * m2m_priv)1572 static int coda_job_ready(void *m2m_priv)
1573 {
1574 struct coda_ctx *ctx = m2m_priv;
1575 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1576
1577 /*
1578 * For both 'P' and 'key' frame cases 1 picture
1579 * and 1 frame are needed. In the decoder case,
1580 * the compressed frame can be in the bitstream.
1581 */
1582 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1583 coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1584 return 0;
1585 }
1586
1587 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1588 coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1589 return 0;
1590 }
1591
1592 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1593 bool stream_end = ctx->bit_stream_param &
1594 CODA_BIT_STREAM_END_FLAG;
1595 int num_metas = ctx->num_metas;
1596 struct coda_buffer_meta *meta;
1597 unsigned int count;
1598
1599 count = hweight32(ctx->frm_dis_flg);
1600 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1601 coda_dbg(1, ctx,
1602 "not ready: all internal buffers in use: %d/%d (0x%x)",
1603 count, ctx->num_internal_frames,
1604 ctx->frm_dis_flg);
1605 return 0;
1606 }
1607
1608 if (ctx->hold && !src_bufs) {
1609 coda_dbg(1, ctx,
1610 "not ready: on hold for more buffers.\n");
1611 return 0;
1612 }
1613
1614 if (!stream_end && (num_metas + src_bufs) < 2) {
1615 coda_dbg(1, ctx,
1616 "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1617 num_metas, src_bufs);
1618 return 0;
1619 }
1620
1621 meta = list_first_entry(&ctx->buffer_meta_list,
1622 struct coda_buffer_meta, list);
1623 if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1624 !stream_end) {
1625 coda_dbg(1, ctx,
1626 "not ready: not enough bitstream data to read past %u (%u)\n",
1627 meta->end, ctx->bitstream_fifo.kfifo.in);
1628 return 0;
1629 }
1630 }
1631
1632 if (ctx->aborting) {
1633 coda_dbg(1, ctx, "not ready: aborting\n");
1634 return 0;
1635 }
1636
1637 coda_dbg(2, ctx, "job ready\n");
1638
1639 return 1;
1640 }
1641
coda_job_abort(void * priv)1642 static void coda_job_abort(void *priv)
1643 {
1644 struct coda_ctx *ctx = priv;
1645
1646 ctx->aborting = 1;
1647
1648 coda_dbg(1, ctx, "job abort\n");
1649 }
1650
1651 static const struct v4l2_m2m_ops coda_m2m_ops = {
1652 .device_run = coda_device_run,
1653 .job_ready = coda_job_ready,
1654 .job_abort = coda_job_abort,
1655 };
1656
set_default_params(struct coda_ctx * ctx)1657 static void set_default_params(struct coda_ctx *ctx)
1658 {
1659 unsigned int max_w, max_h, usize, csize;
1660
1661 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1662 ctx->cvd->dst_formats[0]);
1663 max_w = min(ctx->codec->max_w, 1920U);
1664 max_h = min(ctx->codec->max_h, 1088U);
1665 usize = max_w * max_h * 3 / 2;
1666 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1667
1668 ctx->params.codec_mode = ctx->codec->mode;
1669 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG ||
1670 ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG) {
1671 ctx->colorspace = V4L2_COLORSPACE_SRGB;
1672 ctx->xfer_func = V4L2_XFER_FUNC_SRGB;
1673 ctx->ycbcr_enc = V4L2_YCBCR_ENC_601;
1674 ctx->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1675 } else {
1676 ctx->colorspace = V4L2_COLORSPACE_REC709;
1677 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1678 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1679 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1680 }
1681 ctx->params.framerate = 30;
1682
1683 /* Default formats for output and input queues */
1684 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1685 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1686 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1687 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1688 ctx->q_data[V4L2_M2M_DST].width = max_w;
1689 ctx->q_data[V4L2_M2M_DST].height = max_h;
1690 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1691 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1692 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1693 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1694 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1695 } else {
1696 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1697 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1698 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1699 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1700 }
1701 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1702 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1703 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1704 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1705
1706 /*
1707 * Since the RBC2AXI logic only supports a single chroma plane,
1708 * macroblock tiling only works for to NV12 pixel format.
1709 */
1710 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1711 }
1712
1713 /*
1714 * Queue operations
1715 */
coda_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])1716 static int coda_queue_setup(struct vb2_queue *vq,
1717 unsigned int *nbuffers, unsigned int *nplanes,
1718 unsigned int sizes[], struct device *alloc_devs[])
1719 {
1720 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1721 struct coda_q_data *q_data;
1722 unsigned int size;
1723
1724 q_data = get_q_data(ctx, vq->type);
1725 size = q_data->sizeimage;
1726
1727 if (*nplanes)
1728 return sizes[0] < size ? -EINVAL : 0;
1729
1730 *nplanes = 1;
1731 sizes[0] = size;
1732
1733 coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1734 size);
1735
1736 return 0;
1737 }
1738
coda_buf_prepare(struct vb2_buffer * vb)1739 static int coda_buf_prepare(struct vb2_buffer *vb)
1740 {
1741 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1742 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1743 struct coda_q_data *q_data;
1744
1745 q_data = get_q_data(ctx, vb->vb2_queue->type);
1746 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1747 if (vbuf->field == V4L2_FIELD_ANY)
1748 vbuf->field = V4L2_FIELD_NONE;
1749 if (vbuf->field != V4L2_FIELD_NONE) {
1750 v4l2_warn(&ctx->dev->v4l2_dev,
1751 "%s field isn't supported\n", __func__);
1752 return -EINVAL;
1753 }
1754 }
1755
1756 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1757 v4l2_warn(&ctx->dev->v4l2_dev,
1758 "%s data will not fit into plane (%lu < %lu)\n",
1759 __func__, vb2_plane_size(vb, 0),
1760 (long)q_data->sizeimage);
1761 return -EINVAL;
1762 }
1763
1764 return 0;
1765 }
1766
coda_update_menu_ctrl(struct v4l2_ctrl * ctrl,int value)1767 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1768 {
1769 if (!ctrl)
1770 return;
1771
1772 v4l2_ctrl_lock(ctrl);
1773
1774 /*
1775 * Extend the control range if the parsed stream contains a known but
1776 * unsupported value or level.
1777 */
1778 if (value > ctrl->maximum) {
1779 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1780 ctrl->menu_skip_mask & ~(1 << value),
1781 ctrl->default_value);
1782 } else if (value < ctrl->minimum) {
1783 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1784 ctrl->menu_skip_mask & ~(1 << value),
1785 ctrl->default_value);
1786 }
1787
1788 __v4l2_ctrl_s_ctrl(ctrl, value);
1789
1790 v4l2_ctrl_unlock(ctrl);
1791 }
1792
coda_update_profile_level_ctrls(struct coda_ctx * ctx,u8 profile_idc,u8 level_idc)1793 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
1794 u8 level_idc)
1795 {
1796 const char * const *profile_names;
1797 const char * const *level_names;
1798 struct v4l2_ctrl *profile_ctrl;
1799 struct v4l2_ctrl *level_ctrl;
1800 const char *codec_name;
1801 u32 profile_cid;
1802 u32 level_cid;
1803 int profile;
1804 int level;
1805
1806 switch (ctx->codec->src_fourcc) {
1807 case V4L2_PIX_FMT_H264:
1808 codec_name = "H264";
1809 profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
1810 level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
1811 profile_ctrl = ctx->h264_profile_ctrl;
1812 level_ctrl = ctx->h264_level_ctrl;
1813 profile = coda_h264_profile(profile_idc);
1814 level = coda_h264_level(level_idc);
1815 break;
1816 case V4L2_PIX_FMT_MPEG2:
1817 codec_name = "MPEG-2";
1818 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
1819 level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
1820 profile_ctrl = ctx->mpeg2_profile_ctrl;
1821 level_ctrl = ctx->mpeg2_level_ctrl;
1822 profile = coda_mpeg2_profile(profile_idc);
1823 level = coda_mpeg2_level(level_idc);
1824 break;
1825 case V4L2_PIX_FMT_MPEG4:
1826 codec_name = "MPEG-4";
1827 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
1828 level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
1829 profile_ctrl = ctx->mpeg4_profile_ctrl;
1830 level_ctrl = ctx->mpeg4_level_ctrl;
1831 profile = coda_mpeg4_profile(profile_idc);
1832 level = coda_mpeg4_level(level_idc);
1833 break;
1834 default:
1835 return;
1836 }
1837
1838 profile_names = v4l2_ctrl_get_menu(profile_cid);
1839 level_names = v4l2_ctrl_get_menu(level_cid);
1840
1841 if (profile < 0) {
1842 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
1843 codec_name, profile_idc);
1844 } else {
1845 coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
1846 profile_names[profile]);
1847 coda_update_menu_ctrl(profile_ctrl, profile);
1848 }
1849
1850 if (level < 0) {
1851 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
1852 codec_name, level_idc);
1853 } else {
1854 coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
1855 level_names[level]);
1856 coda_update_menu_ctrl(level_ctrl, level);
1857 }
1858 }
1859
coda_queue_source_change_event(struct coda_ctx * ctx)1860 static void coda_queue_source_change_event(struct coda_ctx *ctx)
1861 {
1862 static const struct v4l2_event source_change_event = {
1863 .type = V4L2_EVENT_SOURCE_CHANGE,
1864 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1865 };
1866
1867 v4l2_event_queue_fh(&ctx->fh, &source_change_event);
1868 }
1869
coda_buf_queue(struct vb2_buffer * vb)1870 static void coda_buf_queue(struct vb2_buffer *vb)
1871 {
1872 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1873 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1874 struct vb2_queue *vq = vb->vb2_queue;
1875 struct coda_q_data *q_data;
1876
1877 q_data = get_q_data(ctx, vb->vb2_queue->type);
1878
1879 /*
1880 * In the decoder case, immediately try to copy the buffer into the
1881 * bitstream ringbuffer and mark it as ready to be dequeued.
1882 */
1883 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1884 /*
1885 * For backwards compatibility, queuing an empty buffer marks
1886 * the stream end
1887 */
1888 if (vb2_get_plane_payload(vb, 0) == 0)
1889 coda_bit_stream_end_flag(ctx);
1890
1891 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1892 /*
1893 * Unless already done, try to obtain profile_idc and
1894 * level_idc from the SPS header. This allows to decide
1895 * whether to enable reordering during sequence
1896 * initialization.
1897 */
1898 if (!ctx->params.h264_profile_idc) {
1899 coda_sps_parse_profile(ctx, vb);
1900 coda_update_profile_level_ctrls(ctx,
1901 ctx->params.h264_profile_idc,
1902 ctx->params.h264_level_idc);
1903 }
1904 }
1905
1906 mutex_lock(&ctx->bitstream_mutex);
1907 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1908 if (vb2_is_streaming(vb->vb2_queue))
1909 /* This set buf->sequence = ctx->qsequence++ */
1910 coda_fill_bitstream(ctx, NULL);
1911 mutex_unlock(&ctx->bitstream_mutex);
1912
1913 if (!ctx->initialized) {
1914 /*
1915 * Run sequence initialization in case the queued
1916 * buffer contained headers.
1917 */
1918 if (vb2_is_streaming(vb->vb2_queue) &&
1919 ctx->ops->seq_init_work) {
1920 queue_work(ctx->dev->workqueue,
1921 &ctx->seq_init_work);
1922 flush_work(&ctx->seq_init_work);
1923 }
1924
1925 if (ctx->initialized)
1926 coda_queue_source_change_event(ctx);
1927 }
1928 } else {
1929 if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
1930 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1931 vbuf->sequence = ctx->qsequence++;
1932 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1933 }
1934 }
1935
coda_alloc_aux_buf(struct coda_dev * dev,struct coda_aux_buf * buf,size_t size,const char * name,struct dentry * parent)1936 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1937 size_t size, const char *name, struct dentry *parent)
1938 {
1939 buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
1940 GFP_KERNEL);
1941 if (!buf->vaddr) {
1942 v4l2_err(&dev->v4l2_dev,
1943 "Failed to allocate %s buffer of size %zu\n",
1944 name, size);
1945 return -ENOMEM;
1946 }
1947
1948 buf->size = size;
1949
1950 if (name && parent) {
1951 buf->blob.data = buf->vaddr;
1952 buf->blob.size = size;
1953 buf->dentry = debugfs_create_blob(name, 0444, parent,
1954 &buf->blob);
1955 }
1956
1957 return 0;
1958 }
1959
coda_free_aux_buf(struct coda_dev * dev,struct coda_aux_buf * buf)1960 void coda_free_aux_buf(struct coda_dev *dev,
1961 struct coda_aux_buf *buf)
1962 {
1963 if (buf->vaddr) {
1964 dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
1965 buf->vaddr = NULL;
1966 buf->size = 0;
1967 debugfs_remove(buf->dentry);
1968 buf->dentry = NULL;
1969 }
1970 }
1971
coda_start_streaming(struct vb2_queue * q,unsigned int count)1972 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1973 {
1974 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1975 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1976 struct coda_q_data *q_data_src, *q_data_dst;
1977 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1978 struct vb2_v4l2_buffer *buf;
1979 struct list_head list;
1980 int ret = 0;
1981
1982 if (count < 1)
1983 return -EINVAL;
1984
1985 coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1986
1987 INIT_LIST_HEAD(&list);
1988
1989 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1990 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1991 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1992 /* copy the buffers that were queued before streamon */
1993 mutex_lock(&ctx->bitstream_mutex);
1994 coda_fill_bitstream(ctx, &list);
1995 mutex_unlock(&ctx->bitstream_mutex);
1996
1997 if (ctx->dev->devtype->product != CODA_960 &&
1998 coda_get_bitstream_payload(ctx) < 512) {
1999 v4l2_err(v4l2_dev, "start payload < 512\n");
2000 ret = -EINVAL;
2001 goto err;
2002 }
2003
2004 if (!ctx->initialized) {
2005 /* Run sequence initialization */
2006 if (ctx->ops->seq_init_work) {
2007 queue_work(ctx->dev->workqueue,
2008 &ctx->seq_init_work);
2009 flush_work(&ctx->seq_init_work);
2010 }
2011 }
2012 }
2013
2014 /*
2015 * Check the first input JPEG buffer to determine chroma
2016 * subsampling.
2017 */
2018 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
2019 buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2020 coda_jpeg_decode_header(ctx, &buf->vb2_buf);
2021 /*
2022 * We have to start streaming even if the first buffer
2023 * does not contain a valid JPEG image. The error will
2024 * be caught during device run and will be signalled
2025 * via the capture buffer error flag.
2026 */
2027
2028 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2029 q_data_dst->width = round_up(q_data_src->width, 16);
2030 q_data_dst->height = round_up(q_data_src->height, 16);
2031 q_data_dst->bytesperline = q_data_dst->width;
2032 if (ctx->params.jpeg_chroma_subsampling ==
2033 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
2034 q_data_dst->sizeimage =
2035 q_data_dst->bytesperline *
2036 q_data_dst->height * 3 / 2;
2037 if (q_data_dst->fourcc != V4L2_PIX_FMT_YUV420)
2038 q_data_dst->fourcc = V4L2_PIX_FMT_NV12;
2039 } else {
2040 q_data_dst->sizeimage =
2041 q_data_dst->bytesperline *
2042 q_data_dst->height * 2;
2043 q_data_dst->fourcc = V4L2_PIX_FMT_YUV422P;
2044 }
2045 q_data_dst->rect.left = 0;
2046 q_data_dst->rect.top = 0;
2047 q_data_dst->rect.width = q_data_src->width;
2048 q_data_dst->rect.height = q_data_src->height;
2049 }
2050 ctx->streamon_out = 1;
2051 } else {
2052 ctx->streamon_cap = 1;
2053 }
2054
2055 /* Don't start the coda unless both queues are on */
2056 if (!(ctx->streamon_out && ctx->streamon_cap))
2057 goto out;
2058
2059 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2060 if ((q_data_src->rect.width != q_data_dst->width &&
2061 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
2062 (q_data_src->rect.height != q_data_dst->height &&
2063 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
2064 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
2065 q_data_src->rect.width, q_data_src->rect.height,
2066 q_data_dst->width, q_data_dst->height);
2067 ret = -EINVAL;
2068 goto err;
2069 }
2070
2071 /* Allow BIT decoder device_run with no new buffers queued */
2072 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2073 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
2074
2075 ctx->gopcounter = ctx->params.gop_size - 1;
2076
2077 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
2078 ctx->params.gop_size = 1;
2079 ctx->gopcounter = ctx->params.gop_size - 1;
2080 /* Only decoders have this control */
2081 if (ctx->mb_err_cnt_ctrl)
2082 v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl, 0);
2083
2084 ret = ctx->ops->start_streaming(ctx);
2085 if (ctx->inst_type == CODA_INST_DECODER) {
2086 if (ret == -EAGAIN)
2087 goto out;
2088 }
2089 if (ret < 0)
2090 goto err;
2091
2092 out:
2093 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2094 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2095 list_del(&m2m_buf->list);
2096 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
2097 }
2098 }
2099 return 0;
2100
2101 err:
2102 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2103 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2104 list_del(&m2m_buf->list);
2105 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
2106 }
2107 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2108 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2109 } else {
2110 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2111 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2112 }
2113 return ret;
2114 }
2115
coda_stop_streaming(struct vb2_queue * q)2116 static void coda_stop_streaming(struct vb2_queue *q)
2117 {
2118 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2119 struct coda_dev *dev = ctx->dev;
2120 struct vb2_v4l2_buffer *buf;
2121 bool stop;
2122
2123 stop = ctx->streamon_out && ctx->streamon_cap;
2124
2125 coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
2126
2127 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2128 ctx->streamon_out = 0;
2129
2130 coda_bit_stream_end_flag(ctx);
2131
2132 ctx->qsequence = 0;
2133
2134 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2135 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2136 } else {
2137 ctx->streamon_cap = 0;
2138
2139 ctx->osequence = 0;
2140 ctx->sequence_offset = 0;
2141
2142 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2143 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2144 }
2145
2146 if (stop) {
2147 struct coda_buffer_meta *meta;
2148
2149 if (ctx->ops->seq_end_work) {
2150 queue_work(dev->workqueue, &ctx->seq_end_work);
2151 flush_work(&ctx->seq_end_work);
2152 }
2153 spin_lock(&ctx->buffer_meta_lock);
2154 while (!list_empty(&ctx->buffer_meta_list)) {
2155 meta = list_first_entry(&ctx->buffer_meta_list,
2156 struct coda_buffer_meta, list);
2157 list_del(&meta->list);
2158 kfree(meta);
2159 }
2160 ctx->num_metas = 0;
2161 spin_unlock(&ctx->buffer_meta_lock);
2162 kfifo_init(&ctx->bitstream_fifo,
2163 ctx->bitstream.vaddr, ctx->bitstream.size);
2164 ctx->runcounter = 0;
2165 ctx->aborting = 0;
2166 ctx->hold = false;
2167 }
2168
2169 if (!ctx->streamon_out && !ctx->streamon_cap)
2170 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
2171 }
2172
2173 static const struct vb2_ops coda_qops = {
2174 .queue_setup = coda_queue_setup,
2175 .buf_prepare = coda_buf_prepare,
2176 .buf_queue = coda_buf_queue,
2177 .start_streaming = coda_start_streaming,
2178 .stop_streaming = coda_stop_streaming,
2179 };
2180
coda_s_ctrl(struct v4l2_ctrl * ctrl)2181 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2182 {
2183 const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
2184 struct coda_ctx *ctx =
2185 container_of(ctrl->handler, struct coda_ctx, ctrls);
2186
2187 if (val_names)
2188 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
2189 ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
2190 else
2191 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
2192 ctrl->id, ctrl->name, ctrl->val);
2193
2194 switch (ctrl->id) {
2195 case V4L2_CID_HFLIP:
2196 if (ctrl->val)
2197 ctx->params.rot_mode |= CODA_MIR_HOR;
2198 else
2199 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2200 break;
2201 case V4L2_CID_VFLIP:
2202 if (ctrl->val)
2203 ctx->params.rot_mode |= CODA_MIR_VER;
2204 else
2205 ctx->params.rot_mode &= ~CODA_MIR_VER;
2206 break;
2207 case V4L2_CID_MPEG_VIDEO_BITRATE:
2208 ctx->params.bitrate = ctrl->val / 1000;
2209 ctx->params.bitrate_changed = true;
2210 break;
2211 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2212 ctx->params.gop_size = ctrl->val;
2213 break;
2214 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2215 ctx->params.h264_intra_qp = ctrl->val;
2216 ctx->params.h264_intra_qp_changed = true;
2217 break;
2218 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2219 ctx->params.h264_inter_qp = ctrl->val;
2220 break;
2221 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2222 ctx->params.h264_min_qp = ctrl->val;
2223 break;
2224 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2225 ctx->params.h264_max_qp = ctrl->val;
2226 break;
2227 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2228 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
2229 break;
2230 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2231 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
2232 break;
2233 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2234 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
2235 break;
2236 case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
2237 ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
2238 break;
2239 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2240 ctx->params.frame_rc_enable = ctrl->val;
2241 break;
2242 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
2243 ctx->params.mb_rc_enable = ctrl->val;
2244 break;
2245 case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
2246 ctx->params.h264_chroma_qp_index_offset = ctrl->val;
2247 break;
2248 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
2249 /* TODO: switch between baseline and constrained baseline */
2250 if (ctx->inst_type == CODA_INST_ENCODER)
2251 ctx->params.h264_profile_idc = 66;
2252 break;
2253 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2254 /* nothing to do, this is set by the encoder */
2255 break;
2256 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2257 ctx->params.mpeg4_intra_qp = ctrl->val;
2258 break;
2259 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2260 ctx->params.mpeg4_inter_qp = ctrl->val;
2261 break;
2262 case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
2263 case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
2264 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2265 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
2266 /* nothing to do, these are fixed */
2267 break;
2268 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2269 ctx->params.slice_mode = ctrl->val;
2270 ctx->params.slice_mode_changed = true;
2271 break;
2272 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2273 ctx->params.slice_max_mb = ctrl->val;
2274 ctx->params.slice_mode_changed = true;
2275 break;
2276 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2277 ctx->params.slice_max_bits = ctrl->val * 8;
2278 ctx->params.slice_mode_changed = true;
2279 break;
2280 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2281 break;
2282 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2283 ctx->params.intra_refresh = ctrl->val;
2284 ctx->params.intra_refresh_changed = true;
2285 break;
2286 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
2287 ctx->params.force_ipicture = true;
2288 break;
2289 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2290 coda_set_jpeg_compression_quality(ctx, ctrl->val);
2291 break;
2292 case V4L2_CID_JPEG_RESTART_INTERVAL:
2293 ctx->params.jpeg_restart_interval = ctrl->val;
2294 break;
2295 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
2296 ctx->params.vbv_delay = ctrl->val;
2297 break;
2298 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
2299 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
2300 break;
2301 default:
2302 coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
2303 ctrl->id, ctrl->val);
2304 return -EINVAL;
2305 }
2306
2307 return 0;
2308 }
2309
2310 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
2311 .s_ctrl = coda_s_ctrl,
2312 };
2313
coda_encode_ctrls(struct coda_ctx * ctx)2314 static void coda_encode_ctrls(struct coda_ctx *ctx)
2315 {
2316 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
2317
2318 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2319 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
2320 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2321 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
2322 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2323 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
2324 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2325 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
2326 if (ctx->dev->devtype->product != CODA_960) {
2327 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2328 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2329 }
2330 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2331 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
2332 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2333 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
2334 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2335 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
2336 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2337 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2338 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
2339 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2340 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2341 V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2342 0);
2343 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2344 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
2345 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2346 V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 1);
2347 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2348 V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2349 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2350 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2351 V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE, 0x0,
2352 V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE);
2353 if (ctx->dev->devtype->product == CODA_HX4 ||
2354 ctx->dev->devtype->product == CODA_7541) {
2355 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2356 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2357 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2358 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2359 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2360 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2361 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2362 }
2363 if (ctx->dev->devtype->product == CODA_960) {
2364 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2365 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2366 V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
2367 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
2368 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2369 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2370 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2371 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2372 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0) |
2373 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1) |
2374 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_2)),
2375 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2376 }
2377 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2378 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2379 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2380 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2381 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2382 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2383 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2384 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2385 if (ctx->dev->devtype->product == CODA_HX4 ||
2386 ctx->dev->devtype->product == CODA_7541 ||
2387 ctx->dev->devtype->product == CODA_960) {
2388 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2389 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2390 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2391 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2392 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2393 }
2394 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2395 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2396 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
2397 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2398 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2399 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2400 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2401 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2402 500);
2403 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2404 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2405 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2406 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2407 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2408 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2409 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2410 1920 * 1088 / 256, 1, 0);
2411 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2412 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2413 /*
2414 * The maximum VBV size value is 0x7fffffff bits,
2415 * one bit less than 262144 KiB
2416 */
2417 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2418 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2419 }
2420
coda_jpeg_encode_ctrls(struct coda_ctx * ctx)2421 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2422 {
2423 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2424 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2425 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2426 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2427 }
2428
coda_decode_ctrls(struct coda_ctx * ctx)2429 static void coda_decode_ctrls(struct coda_ctx *ctx)
2430 {
2431 u8 max;
2432
2433 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2434 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2435 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2436 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
2437 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2438 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2439 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2440 if (ctx->h264_profile_ctrl)
2441 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2442
2443 if (ctx->dev->devtype->product == CODA_HX4 ||
2444 ctx->dev->devtype->product == CODA_7541)
2445 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2446 else if (ctx->dev->devtype->product == CODA_960)
2447 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2448 else
2449 return;
2450 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2451 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2452 if (ctx->h264_level_ctrl)
2453 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2454
2455 ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2456 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
2457 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
2458 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
2459 if (ctx->mpeg2_profile_ctrl)
2460 ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2461
2462 ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2463 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
2464 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
2465 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
2466 if (ctx->mpeg2_level_ctrl)
2467 ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2468
2469 ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2470 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2471 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
2472 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
2473 if (ctx->mpeg4_profile_ctrl)
2474 ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2475
2476 ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2477 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2478 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
2479 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2480 if (ctx->mpeg4_level_ctrl)
2481 ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2482 }
2483
2484 static const struct v4l2_ctrl_config coda_mb_err_cnt_ctrl_config = {
2485 .id = V4L2_CID_CODA_MB_ERR_CNT,
2486 .name = "Macroblocks Error Count",
2487 .type = V4L2_CTRL_TYPE_INTEGER,
2488 .min = 0,
2489 .max = 0x7fffffff,
2490 .step = 1,
2491 };
2492
coda_ctrls_setup(struct coda_ctx * ctx)2493 static int coda_ctrls_setup(struct coda_ctx *ctx)
2494 {
2495 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2496
2497 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2498 V4L2_CID_HFLIP, 0, 1, 1, 0);
2499 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2500 V4L2_CID_VFLIP, 0, 1, 1, 0);
2501 if (ctx->inst_type == CODA_INST_ENCODER) {
2502 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2503 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2504 1, 1, 1, 1);
2505 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2506 coda_jpeg_encode_ctrls(ctx);
2507 else
2508 coda_encode_ctrls(ctx);
2509 } else {
2510 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2511 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2512 1, 1, 1, 1);
2513 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2514 coda_decode_ctrls(ctx);
2515
2516 ctx->mb_err_cnt_ctrl = v4l2_ctrl_new_custom(&ctx->ctrls,
2517 &coda_mb_err_cnt_ctrl_config,
2518 NULL);
2519 if (ctx->mb_err_cnt_ctrl)
2520 ctx->mb_err_cnt_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2521 }
2522
2523 if (ctx->ctrls.error) {
2524 v4l2_err(&ctx->dev->v4l2_dev,
2525 "control initialization error (%d)",
2526 ctx->ctrls.error);
2527 return -EINVAL;
2528 }
2529
2530 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2531 }
2532
coda_queue_init(struct coda_ctx * ctx,struct vb2_queue * vq)2533 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2534 {
2535 vq->drv_priv = ctx;
2536 vq->ops = &coda_qops;
2537 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2538 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2539 vq->lock = &ctx->dev->dev_mutex;
2540 /* One way to indicate end-of-stream for coda is to set the
2541 * bytesused == 0. However by default videobuf2 handles bytesused
2542 * equal to 0 as a special case and changes its value to the size
2543 * of the buffer. Set the allow_zero_bytesused flag, so
2544 * that videobuf2 will keep the value of bytesused intact.
2545 */
2546 vq->allow_zero_bytesused = 1;
2547 /*
2548 * We might be fine with no buffers on some of the queues, but that
2549 * would need to be reflected in job_ready(). Currently we expect all
2550 * queues to have at least one buffer queued.
2551 */
2552 vq->min_queued_buffers = 1;
2553 vq->dev = ctx->dev->dev;
2554
2555 return vb2_queue_init(vq);
2556 }
2557
coda_encoder_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)2558 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2559 struct vb2_queue *dst_vq)
2560 {
2561 int ret;
2562
2563 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2564 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2565 src_vq->mem_ops = &vb2_dma_contig_memops;
2566
2567 ret = coda_queue_init(priv, src_vq);
2568 if (ret)
2569 return ret;
2570
2571 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2572 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2573 dst_vq->mem_ops = &vb2_dma_contig_memops;
2574
2575 return coda_queue_init(priv, dst_vq);
2576 }
2577
coda_decoder_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)2578 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2579 struct vb2_queue *dst_vq)
2580 {
2581 int ret;
2582
2583 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2584 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2585 src_vq->mem_ops = &vb2_vmalloc_memops;
2586
2587 ret = coda_queue_init(priv, src_vq);
2588 if (ret)
2589 return ret;
2590
2591 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2592 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2593 dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
2594 dst_vq->mem_ops = &vb2_dma_contig_memops;
2595
2596 return coda_queue_init(priv, dst_vq);
2597 }
2598
2599 /*
2600 * File operations
2601 */
2602
coda_open(struct file * file)2603 static int coda_open(struct file *file)
2604 {
2605 struct video_device *vdev = video_devdata(file);
2606 struct coda_dev *dev = video_get_drvdata(vdev);
2607 struct coda_ctx *ctx;
2608 unsigned int max = ~0;
2609 char *name;
2610 int ret;
2611 int idx;
2612
2613 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2614 if (!ctx)
2615 return -ENOMEM;
2616
2617 if (dev->devtype->product == CODA_DX6)
2618 max = CODADX6_MAX_INSTANCES - 1;
2619 idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2620 if (idx < 0) {
2621 ret = idx;
2622 goto err_coda_max;
2623 }
2624
2625 name = kasprintf(GFP_KERNEL, "context%d", idx);
2626 if (!name) {
2627 ret = -ENOMEM;
2628 goto err_coda_name_init;
2629 }
2630
2631 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2632 kfree(name);
2633
2634 ctx->cvd = to_coda_video_device(vdev);
2635 ctx->inst_type = ctx->cvd->type;
2636 ctx->ops = ctx->cvd->ops;
2637 ctx->use_bit = !ctx->cvd->direct;
2638 init_completion(&ctx->completion);
2639 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2640 if (ctx->ops->seq_init_work)
2641 INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
2642 if (ctx->ops->seq_end_work)
2643 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2644 v4l2_fh_init(&ctx->fh, video_devdata(file));
2645 v4l2_fh_add(&ctx->fh, file);
2646 ctx->dev = dev;
2647 ctx->idx = idx;
2648
2649 coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2650
2651 switch (dev->devtype->product) {
2652 case CODA_960:
2653 /*
2654 * Enabling the BWB when decoding can hang the firmware with
2655 * certain streams. The issue was tracked as ENGR00293425 by
2656 * Freescale. As a workaround, disable BWB for all decoders.
2657 * The enable_bwb module parameter allows to override this.
2658 */
2659 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2660 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2661 fallthrough;
2662 case CODA_HX4:
2663 case CODA_7541:
2664 ctx->reg_idx = 0;
2665 break;
2666 default:
2667 ctx->reg_idx = idx;
2668 }
2669 if (ctx->dev->vdoa && !disable_vdoa) {
2670 ctx->vdoa = vdoa_context_create(dev->vdoa);
2671 if (!ctx->vdoa)
2672 v4l2_warn(&dev->v4l2_dev,
2673 "Failed to create vdoa context: not using vdoa");
2674 }
2675 ctx->use_vdoa = false;
2676
2677 /* Power up and upload firmware if necessary */
2678 ret = pm_runtime_resume_and_get(dev->dev);
2679 if (ret < 0) {
2680 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2681 goto err_pm_get;
2682 }
2683
2684 ret = clk_prepare_enable(dev->clk_per);
2685 if (ret)
2686 goto err_clk_enable;
2687
2688 ret = clk_prepare_enable(dev->clk_ahb);
2689 if (ret)
2690 goto err_clk_ahb;
2691
2692 set_default_params(ctx);
2693 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2694 ctx->ops->queue_init);
2695 if (IS_ERR(ctx->fh.m2m_ctx)) {
2696 ret = PTR_ERR(ctx->fh.m2m_ctx);
2697
2698 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2699 __func__, ret);
2700 goto err_ctx_init;
2701 }
2702
2703 ret = coda_ctrls_setup(ctx);
2704 if (ret) {
2705 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2706 goto err_ctrls_setup;
2707 }
2708
2709 ctx->fh.ctrl_handler = &ctx->ctrls;
2710
2711 mutex_init(&ctx->bitstream_mutex);
2712 mutex_init(&ctx->buffer_mutex);
2713 mutex_init(&ctx->wakeup_mutex);
2714 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2715 spin_lock_init(&ctx->buffer_meta_lock);
2716
2717 return 0;
2718
2719 err_ctrls_setup:
2720 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2721 err_ctx_init:
2722 clk_disable_unprepare(dev->clk_ahb);
2723 err_clk_ahb:
2724 clk_disable_unprepare(dev->clk_per);
2725 err_clk_enable:
2726 pm_runtime_put_sync(dev->dev);
2727 err_pm_get:
2728 v4l2_fh_del(&ctx->fh, file);
2729 v4l2_fh_exit(&ctx->fh);
2730 err_coda_name_init:
2731 ida_free(&dev->ida, ctx->idx);
2732 err_coda_max:
2733 kfree(ctx);
2734 return ret;
2735 }
2736
coda_release(struct file * file)2737 static int coda_release(struct file *file)
2738 {
2739 struct coda_dev *dev = video_drvdata(file);
2740 struct coda_ctx *ctx = file_to_ctx(file);
2741
2742 coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2743
2744 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2745 coda_bit_stream_end_flag(ctx);
2746
2747 /* If this instance is running, call .job_abort and wait for it to end */
2748 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2749
2750 if (ctx->vdoa)
2751 vdoa_context_destroy(ctx->vdoa);
2752
2753 /* In case the instance was not running, we still need to call SEQ_END */
2754 if (ctx->ops->seq_end_work) {
2755 queue_work(dev->workqueue, &ctx->seq_end_work);
2756 flush_work(&ctx->seq_end_work);
2757 }
2758
2759 if (ctx->dev->devtype->product == CODA_DX6)
2760 coda_free_aux_buf(dev, &ctx->workbuf);
2761
2762 v4l2_ctrl_handler_free(&ctx->ctrls);
2763 clk_disable_unprepare(dev->clk_ahb);
2764 clk_disable_unprepare(dev->clk_per);
2765 pm_runtime_put_sync(dev->dev);
2766 v4l2_fh_del(&ctx->fh, file);
2767 v4l2_fh_exit(&ctx->fh);
2768 ida_free(&dev->ida, ctx->idx);
2769 if (ctx->ops->release)
2770 ctx->ops->release(ctx);
2771 debugfs_remove_recursive(ctx->debugfs_entry);
2772 kfree(ctx);
2773
2774 return 0;
2775 }
2776
2777 static const struct v4l2_file_operations coda_fops = {
2778 .owner = THIS_MODULE,
2779 .open = coda_open,
2780 .release = coda_release,
2781 .poll = v4l2_m2m_fop_poll,
2782 .unlocked_ioctl = video_ioctl2,
2783 .mmap = v4l2_m2m_fop_mmap,
2784 };
2785
coda_hw_init(struct coda_dev * dev)2786 static int coda_hw_init(struct coda_dev *dev)
2787 {
2788 u32 data;
2789 u16 *p;
2790 int i, ret;
2791
2792 ret = clk_prepare_enable(dev->clk_per);
2793 if (ret)
2794 goto err_clk_per;
2795
2796 ret = clk_prepare_enable(dev->clk_ahb);
2797 if (ret)
2798 goto err_clk_ahb;
2799
2800 reset_control_reset(dev->rstc);
2801
2802 /*
2803 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2804 * The 16-bit chars in the code buffer are in memory access
2805 * order, re-sort them to CODA order for register download.
2806 * Data in this SRAM survives a reboot.
2807 */
2808 p = (u16 *)dev->codebuf.vaddr;
2809 if (dev->devtype->product == CODA_DX6) {
2810 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2811 data = CODA_DOWN_ADDRESS_SET(i) |
2812 CODA_DOWN_DATA_SET(p[i ^ 1]);
2813 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2814 }
2815 } else {
2816 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2817 data = CODA_DOWN_ADDRESS_SET(i) |
2818 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2819 3 - (i % 4)]);
2820 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2821 }
2822 }
2823
2824 /* Clear registers */
2825 for (i = 0; i < 64; i++)
2826 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2827
2828 /* Tell the BIT where to find everything it needs */
2829 if (dev->devtype->product == CODA_960 ||
2830 dev->devtype->product == CODA_7541 ||
2831 dev->devtype->product == CODA_HX4) {
2832 coda_write(dev, dev->tempbuf.paddr,
2833 CODA_REG_BIT_TEMP_BUF_ADDR);
2834 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2835 } else {
2836 coda_write(dev, dev->workbuf.paddr,
2837 CODA_REG_BIT_WORK_BUF_ADDR);
2838 }
2839 coda_write(dev, dev->codebuf.paddr,
2840 CODA_REG_BIT_CODE_BUF_ADDR);
2841 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2842
2843 /* Set default values */
2844 switch (dev->devtype->product) {
2845 case CODA_DX6:
2846 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2847 CODA_REG_BIT_STREAM_CTRL);
2848 break;
2849 default:
2850 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2851 CODA_REG_BIT_STREAM_CTRL);
2852 }
2853 if (dev->devtype->product == CODA_960)
2854 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2855 CODA_REG_BIT_FRAME_MEM_CTRL);
2856 else
2857 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2858
2859 if (dev->devtype->product != CODA_DX6)
2860 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2861
2862 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2863 CODA_REG_BIT_INT_ENABLE);
2864
2865 /* Reset VPU and start processor */
2866 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2867 data |= CODA_REG_RESET_ENABLE;
2868 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2869 udelay(10);
2870 data &= ~CODA_REG_RESET_ENABLE;
2871 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2872 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2873
2874 clk_disable_unprepare(dev->clk_ahb);
2875 clk_disable_unprepare(dev->clk_per);
2876
2877 return 0;
2878
2879 err_clk_ahb:
2880 clk_disable_unprepare(dev->clk_per);
2881 err_clk_per:
2882 return ret;
2883 }
2884
coda_register_device(struct coda_dev * dev,int i)2885 static int coda_register_device(struct coda_dev *dev, int i)
2886 {
2887 struct video_device *vfd = &dev->vfd[i];
2888 const char *name;
2889 int ret;
2890
2891 if (i >= dev->devtype->num_vdevs)
2892 return -EINVAL;
2893 name = dev->devtype->vdevs[i]->name;
2894
2895 strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2896 vfd->fops = &coda_fops;
2897 vfd->ioctl_ops = &coda_ioctl_ops;
2898 vfd->release = video_device_release_empty;
2899 vfd->lock = &dev->dev_mutex;
2900 vfd->v4l2_dev = &dev->v4l2_dev;
2901 vfd->vfl_dir = VFL_DIR_M2M;
2902 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2903 video_set_drvdata(vfd, dev);
2904
2905 /* Not applicable, use the selection API instead */
2906 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2907 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2908 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2909
2910 if (dev->devtype->vdevs[i]->type == CODA_INST_ENCODER) {
2911 v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
2912 v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
2913 if (dev->devtype->vdevs[i]->dst_formats[0] == V4L2_PIX_FMT_JPEG) {
2914 v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
2915 v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
2916 v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
2917 }
2918 } else {
2919 v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
2920 v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
2921 v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMESIZES);
2922 v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
2923 v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
2924 v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
2925 }
2926
2927 ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
2928 if (!ret)
2929 v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
2930 name, video_device_node_name(vfd));
2931 return ret;
2932 }
2933
coda_copy_firmware(struct coda_dev * dev,const u8 * const buf,size_t size)2934 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2935 size_t size)
2936 {
2937 u32 *src = (u32 *)buf;
2938
2939 /* Check if the firmware has a 16-byte Freescale header, skip it */
2940 if (buf[0] == 'M' && buf[1] == 'X')
2941 src += 4;
2942 /*
2943 * Check whether the firmware is in native order or pre-reordered for
2944 * memory access. The first instruction opcode always is 0xe40e.
2945 */
2946 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2947 u32 *dst = dev->codebuf.vaddr;
2948 int i;
2949
2950 /* Firmware in native order, reorder while copying */
2951 if (dev->devtype->product == CODA_DX6) {
2952 for (i = 0; i < (size - 16) / 4; i++)
2953 dst[i] = (src[i] << 16) | (src[i] >> 16);
2954 } else {
2955 for (i = 0; i < (size - 16) / 4; i += 2) {
2956 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2957 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2958 }
2959 }
2960 } else {
2961 /* Copy the already reordered firmware image */
2962 memcpy(dev->codebuf.vaddr, src, size);
2963 }
2964 }
2965
2966 static void coda_fw_callback(const struct firmware *fw, void *context);
2967
coda_firmware_request(struct coda_dev * dev)2968 static int coda_firmware_request(struct coda_dev *dev)
2969 {
2970 char *fw;
2971
2972 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2973 return -EINVAL;
2974
2975 fw = dev->devtype->firmware[dev->firmware];
2976
2977 dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
2978 coda_product_name(dev->devtype->product));
2979
2980 return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
2981 GFP_KERNEL, dev, coda_fw_callback);
2982 }
2983
coda_fw_callback(const struct firmware * fw,void * context)2984 static void coda_fw_callback(const struct firmware *fw, void *context)
2985 {
2986 struct coda_dev *dev = context;
2987 int i, ret;
2988
2989 if (!fw) {
2990 dev->firmware++;
2991 ret = coda_firmware_request(dev);
2992 if (ret < 0) {
2993 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2994 goto put_pm;
2995 }
2996 return;
2997 }
2998 if (dev->firmware > 0) {
2999 /*
3000 * Since we can't suppress warnings for failed asynchronous
3001 * firmware requests, report that the fallback firmware was
3002 * found.
3003 */
3004 dev_info(dev->dev, "Using fallback firmware %s\n",
3005 dev->devtype->firmware[dev->firmware]);
3006 }
3007
3008 /* allocate auxiliary per-device code buffer for the BIT processor */
3009 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3010 dev->debugfs_root);
3011 if (ret < 0)
3012 goto put_pm;
3013
3014 coda_copy_firmware(dev, fw->data, fw->size);
3015 release_firmware(fw);
3016
3017 ret = coda_hw_init(dev);
3018 if (ret < 0) {
3019 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3020 goto put_pm;
3021 }
3022
3023 ret = coda_check_firmware(dev);
3024 if (ret < 0)
3025 goto put_pm;
3026
3027 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3028 if (IS_ERR(dev->m2m_dev)) {
3029 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3030 goto put_pm;
3031 }
3032
3033 for (i = 0; i < dev->devtype->num_vdevs; i++) {
3034 ret = coda_register_device(dev, i);
3035 if (ret) {
3036 v4l2_err(&dev->v4l2_dev,
3037 "Failed to register %s video device: %d\n",
3038 dev->devtype->vdevs[i]->name, ret);
3039 goto rel_vfd;
3040 }
3041 }
3042
3043 pm_runtime_put_sync(dev->dev);
3044 return;
3045
3046 rel_vfd:
3047 while (--i >= 0)
3048 video_unregister_device(&dev->vfd[i]);
3049 v4l2_m2m_release(dev->m2m_dev);
3050 put_pm:
3051 pm_runtime_put_sync(dev->dev);
3052 }
3053
3054 enum coda_platform {
3055 CODA_IMX27,
3056 CODA_IMX51,
3057 CODA_IMX53,
3058 CODA_IMX6Q,
3059 CODA_IMX6DL,
3060 };
3061
3062 static const struct coda_devtype coda_devdata[] = {
3063 [CODA_IMX27] = {
3064 .firmware = {
3065 "vpu_fw_imx27_TO2.bin",
3066 "vpu/vpu_fw_imx27_TO2.bin",
3067 "v4l-codadx6-imx27.bin"
3068 },
3069 .product = CODA_DX6,
3070 .codecs = codadx6_codecs,
3071 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3072 .vdevs = codadx6_video_devices,
3073 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
3074 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
3075 .iram_size = 0xb000,
3076 },
3077 [CODA_IMX51] = {
3078 .firmware = {
3079 "vpu_fw_imx51.bin",
3080 "vpu/vpu_fw_imx51.bin",
3081 "v4l-codahx4-imx51.bin"
3082 },
3083 .product = CODA_HX4,
3084 .codecs = codahx4_codecs,
3085 .num_codecs = ARRAY_SIZE(codahx4_codecs),
3086 .vdevs = codahx4_video_devices,
3087 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
3088 .workbuf_size = 128 * 1024,
3089 .tempbuf_size = 304 * 1024,
3090 .iram_size = 0x14000,
3091 },
3092 [CODA_IMX53] = {
3093 .firmware = {
3094 "vpu_fw_imx53.bin",
3095 "vpu/vpu_fw_imx53.bin",
3096 "v4l-coda7541-imx53.bin"
3097 },
3098 .product = CODA_7541,
3099 .codecs = coda7_codecs,
3100 .num_codecs = ARRAY_SIZE(coda7_codecs),
3101 .vdevs = coda7_video_devices,
3102 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
3103 .workbuf_size = 128 * 1024,
3104 .tempbuf_size = 304 * 1024,
3105 .iram_size = 0x14000,
3106 },
3107 [CODA_IMX6Q] = {
3108 .firmware = {
3109 "vpu_fw_imx6q.bin",
3110 "vpu/vpu_fw_imx6q.bin",
3111 "v4l-coda960-imx6q.bin"
3112 },
3113 .product = CODA_960,
3114 .codecs = coda9_codecs,
3115 .num_codecs = ARRAY_SIZE(coda9_codecs),
3116 .vdevs = coda9_video_devices,
3117 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
3118 .workbuf_size = 80 * 1024,
3119 .tempbuf_size = 204 * 1024,
3120 .iram_size = 0x21000,
3121 },
3122 [CODA_IMX6DL] = {
3123 .firmware = {
3124 "vpu_fw_imx6d.bin",
3125 "vpu/vpu_fw_imx6d.bin",
3126 "v4l-coda960-imx6dl.bin"
3127 },
3128 .product = CODA_960,
3129 .codecs = coda9_codecs,
3130 .num_codecs = ARRAY_SIZE(coda9_codecs),
3131 .vdevs = coda9_video_devices,
3132 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
3133 .workbuf_size = 80 * 1024,
3134 .tempbuf_size = 204 * 1024,
3135 .iram_size = 0x1f000, /* leave 4k for suspend code */
3136 },
3137 };
3138
3139 static const struct of_device_id coda_dt_ids[] = {
3140 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
3141 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
3142 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
3143 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3144 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
3145 { /* sentinel */ }
3146 };
3147 MODULE_DEVICE_TABLE(of, coda_dt_ids);
3148
coda_probe(struct platform_device * pdev)3149 static int coda_probe(struct platform_device *pdev)
3150 {
3151 struct device_node *np = pdev->dev.of_node;
3152 struct gen_pool *pool;
3153 struct coda_dev *dev;
3154 int ret, irq;
3155
3156 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3157 if (!dev)
3158 return -ENOMEM;
3159
3160 dev->devtype = of_device_get_match_data(&pdev->dev);
3161
3162 dev->dev = &pdev->dev;
3163 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3164 if (IS_ERR(dev->clk_per)) {
3165 dev_err(&pdev->dev, "Could not get per clock\n");
3166 return PTR_ERR(dev->clk_per);
3167 }
3168
3169 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3170 if (IS_ERR(dev->clk_ahb)) {
3171 dev_err(&pdev->dev, "Could not get ahb clock\n");
3172 return PTR_ERR(dev->clk_ahb);
3173 }
3174
3175 /* Get memory for physical registers */
3176 dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
3177 if (IS_ERR(dev->regs_base))
3178 return PTR_ERR(dev->regs_base);
3179
3180 /* IRQ */
3181 irq = platform_get_irq_byname(pdev, "bit");
3182 if (irq < 0)
3183 irq = platform_get_irq(pdev, 0);
3184 if (irq < 0)
3185 return irq;
3186
3187 ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
3188 CODA_NAME "-video", dev);
3189 if (ret < 0) {
3190 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3191 return ret;
3192 }
3193
3194 /* JPEG IRQ */
3195 if (dev->devtype->product == CODA_960) {
3196 irq = platform_get_irq_byname(pdev, "jpeg");
3197 if (irq < 0)
3198 return irq;
3199
3200 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3201 coda9_jpeg_irq_handler,
3202 IRQF_ONESHOT, CODA_NAME "-jpeg",
3203 dev);
3204 if (ret < 0) {
3205 dev_err(&pdev->dev, "failed to request jpeg irq\n");
3206 return ret;
3207 }
3208 }
3209
3210 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
3211 NULL);
3212 if (IS_ERR(dev->rstc)) {
3213 ret = PTR_ERR(dev->rstc);
3214 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3215 return ret;
3216 }
3217
3218 /* Get IRAM pool from device tree */
3219 pool = of_gen_pool_get(np, "iram", 0);
3220 if (!pool) {
3221 dev_err(&pdev->dev, "iram pool not available\n");
3222 return -ENOMEM;
3223 }
3224 dev->iram_pool = pool;
3225
3226 /* Get vdoa_data if supported by the platform */
3227 dev->vdoa = coda_get_vdoa_data();
3228 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
3229 return -EPROBE_DEFER;
3230
3231 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3232 if (ret)
3233 return ret;
3234
3235 ratelimit_default_init(&dev->mb_err_rs);
3236 mutex_init(&dev->dev_mutex);
3237 mutex_init(&dev->coda_mutex);
3238 ida_init(&dev->ida);
3239
3240 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3241
3242 /* allocate auxiliary per-device buffers for the BIT processor */
3243 if (dev->devtype->product == CODA_DX6) {
3244 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3245 dev->devtype->workbuf_size, "workbuf",
3246 dev->debugfs_root);
3247 if (ret < 0)
3248 goto err_v4l2_register;
3249 }
3250
3251 if (dev->devtype->tempbuf_size) {
3252 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3253 dev->devtype->tempbuf_size, "tempbuf",
3254 dev->debugfs_root);
3255 if (ret < 0)
3256 goto err_v4l2_register;
3257 }
3258
3259 dev->iram.size = dev->devtype->iram_size;
3260 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3261 &dev->iram.paddr);
3262 if (!dev->iram.vaddr) {
3263 dev_warn(&pdev->dev, "unable to alloc iram\n");
3264 } else {
3265 memset(dev->iram.vaddr, 0, dev->iram.size);
3266 dev->iram.blob.data = dev->iram.vaddr;
3267 dev->iram.blob.size = dev->iram.size;
3268 dev->iram.dentry = debugfs_create_blob("iram", 0444,
3269 dev->debugfs_root,
3270 &dev->iram.blob);
3271 }
3272
3273 dev->workqueue = alloc_ordered_workqueue("coda", WQ_MEM_RECLAIM);
3274 if (!dev->workqueue) {
3275 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3276 ret = -ENOMEM;
3277 goto err_v4l2_register;
3278 }
3279
3280 platform_set_drvdata(pdev, dev);
3281
3282 /*
3283 * Start activated so we can directly call coda_hw_init in
3284 * coda_fw_callback regardless of whether CONFIG_PM is
3285 * enabled or whether the device is associated with a PM domain.
3286 */
3287 pm_runtime_get_noresume(&pdev->dev);
3288 pm_runtime_set_active(&pdev->dev);
3289 pm_runtime_enable(&pdev->dev);
3290
3291 ret = coda_firmware_request(dev);
3292 if (ret)
3293 goto err_alloc_workqueue;
3294 return 0;
3295
3296 err_alloc_workqueue:
3297 pm_runtime_disable(&pdev->dev);
3298 pm_runtime_put_noidle(&pdev->dev);
3299 destroy_workqueue(dev->workqueue);
3300 err_v4l2_register:
3301 v4l2_device_unregister(&dev->v4l2_dev);
3302 return ret;
3303 }
3304
coda_remove(struct platform_device * pdev)3305 static void coda_remove(struct platform_device *pdev)
3306 {
3307 struct coda_dev *dev = platform_get_drvdata(pdev);
3308 int i;
3309
3310 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
3311 if (video_get_drvdata(&dev->vfd[i]))
3312 video_unregister_device(&dev->vfd[i]);
3313 }
3314 if (dev->m2m_dev)
3315 v4l2_m2m_release(dev->m2m_dev);
3316 pm_runtime_disable(&pdev->dev);
3317 v4l2_device_unregister(&dev->v4l2_dev);
3318 destroy_workqueue(dev->workqueue);
3319 if (dev->iram.vaddr)
3320 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3321 dev->iram.size);
3322 coda_free_aux_buf(dev, &dev->codebuf);
3323 coda_free_aux_buf(dev, &dev->tempbuf);
3324 coda_free_aux_buf(dev, &dev->workbuf);
3325 debugfs_remove_recursive(dev->debugfs_root);
3326 ida_destroy(&dev->ida);
3327 }
3328
3329 #ifdef CONFIG_PM
coda_runtime_resume(struct device * dev)3330 static int coda_runtime_resume(struct device *dev)
3331 {
3332 struct coda_dev *cdev = dev_get_drvdata(dev);
3333 int ret = 0;
3334
3335 if (dev->pm_domain && cdev->codebuf.vaddr) {
3336 ret = coda_hw_init(cdev);
3337 if (ret)
3338 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3339 }
3340
3341 return ret;
3342 }
3343 #endif
3344
3345 static const struct dev_pm_ops coda_pm_ops = {
3346 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3347 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
3348 };
3349
3350 static struct platform_driver coda_driver = {
3351 .probe = coda_probe,
3352 .remove = coda_remove,
3353 .driver = {
3354 .name = CODA_NAME,
3355 .of_match_table = coda_dt_ids,
3356 .pm = &coda_pm_ops,
3357 },
3358 };
3359
3360 module_platform_driver(coda_driver);
3361
3362 MODULE_LICENSE("GPL");
3363 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3364 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");
3365