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